raspberry-pi.patch 3.2 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704737057370673707737087370973710737117371273713737147371573716737177371873719737207372173722737237372473725737267372773728737297373073731737327373373734737357373673737737387373973740737417374273743737447374573746737477374873749737507375173752737537375473755737567375773758737597376073761737627376373764737657376673767737687376973770737717377273773737747377573776737777377873779737807378173782737837378473785737867378773788737897379073791737927379373794737957379673797737987379973800738017380273803738047380573806738077380873809738107381173812738137381473815738167381773818738197382073821738227382373824738257382673827738287382973830738317383273833738347383573836738377383873839738407384173842738437384473845738467384773848738497385073851738527385373854738557385673857738587385973860738617386273863738647386573866738677386873869738707387173872738737387473875738767387773878738797388073881738827388373884738857388673887738887388973890738917389273893738947389573896738977389873899739007390173902739037390473905739067390773908739097391073911739127391373914739157391673917739187391973920739217392273923739247392573926739277392873929739307393173932739337393473935739367393773938739397394073941739427394373944739457394673947739487394973950739517395273953739547395573956739577395873959739607396173962739637396473965739667396773968739697397073971739727397373974739757397673977739787397973980739817398273983739847398573986739877398873989739907399173992739937399473995739967399773998739997400074001740027400374004740057400674007740087400974010740117401274013740147401574016740177401874019740207402174022740237402474025740267402774028740297403074031740327403374034740357403674037740387403974040740417404274043740447404574046740477404874049740507405174052740537405474055740567405774058740597406074061740627406374064740657406674067740687406974070740717407274073740747407574076740777407874079740807408174082740837408474085740867408774088740897409074091740927409374094740957409674097740987409974100741017410274103741047410574106741077410874109741107411174112741137411474115741167411774118741197412074121741227412374124741257412674127741287412974130741317413274133741347413574136741377413874139741407414174142741437414474145741467414774148741497415074151741527415374154741557415674157741587415974160741617416274163741647416574166741677416874169741707417174172741737417474175741767417774178741797418074181741827418374184741857418674187741887418974190741917419274193741947419574196741977419874199742007420174202742037420474205742067420774208742097421074211742127421374214742157421674217742187421974220742217422274223742247422574226742277422874229742307423174232742337423474235742367423774238742397424074241742427424374244742457424674247742487424974250742517425274253742547425574256742577425874259742607426174262742637426474265742667426774268742697427074271742727427374274742757427674277742787427974280742817428274283742847428574286742877428874289742907429174292742937429474295742967429774298742997430074301743027430374304743057430674307743087430974310743117431274313743147431574316743177431874319743207432174322743237432474325743267432774328743297433074331743327433374334743357433674337743387433974340743417434274343743447434574346743477434874349743507435174352743537435474355743567435774358743597436074361743627436374364743657436674367743687436974370743717437274373743747437574376743777437874379743807438174382743837438474385743867438774388743897439074391743927439374394743957439674397743987439974400744017440274403744047440574406744077440874409744107441174412744137441474415744167441774418744197442074421744227442374424744257442674427744287442974430744317443274433744347443574436744377443874439744407444174442744437444474445744467444774448744497445074451744527445374454744557445674457744587445974460744617446274463744647446574466744677446874469744707447174472744737447474475744767447774478744797448074481744827448374484744857448674487744887448974490744917449274493744947449574496744977449874499745007450174502745037450474505745067450774508745097451074511745127451374514745157451674517745187451974520745217452274523745247452574526745277452874529745307453174532745337453474535745367453774538745397454074541745427454374544745457454674547745487454974550745517455274553745547455574556745577455874559745607456174562745637456474565745667456774568745697457074571745727457374574745757457674577745787457974580745817458274583745847458574586745877458874589745907459174592745937459474595745967459774598745997460074601746027460374604746057460674607746087460974610746117461274613746147461574616746177461874619746207462174622746237462474625746267462774628746297463074631746327463374634746357463674637746387463974640746417464274643746447464574646746477464874649746507465174652746537465474655746567465774658746597466074661746627466374664746657466674667746687466974670746717467274673746747467574676746777467874679746807468174682746837468474685746867468774688746897469074691746927469374694746957469674697746987469974700747017470274703747047470574706747077470874709747107471174712747137471474715747167471774718747197472074721747227472374724747257472674727747287472974730747317473274733747347473574736747377473874739747407474174742747437474474745747467474774748747497475074751747527475374754747557475674757747587475974760747617476274763747647476574766747677476874769747707477174772747737477474775747767477774778747797478074781747827478374784747857478674787747887478974790747917479274793747947479574796747977479874799748007480174802748037480474805748067480774808748097481074811748127481374814748157481674817748187481974820748217482274823748247482574826748277482874829748307483174832748337483474835748367483774838748397484074841748427484374844748457484674847748487484974850748517485274853748547485574856748577485874859748607486174862748637486474865748667486774868748697487074871748727487374874748757487674877748787487974880748817488274883748847488574886748877488874889748907489174892748937489474895748967489774898748997490074901749027490374904749057490674907749087490974910749117491274913749147491574916749177491874919749207492174922749237492474925749267492774928749297493074931749327493374934749357493674937749387493974940749417494274943749447494574946749477494874949749507495174952749537495474955749567495774958749597496074961749627496374964749657496674967749687496974970749717497274973749747497574976749777497874979749807498174982749837498474985749867498774988749897499074991749927499374994749957499674997749987499975000750017500275003750047500575006750077500875009750107501175012750137501475015750167501775018750197502075021750227502375024750257502675027750287502975030750317503275033750347503575036750377503875039750407504175042750437504475045750467504775048750497505075051750527505375054750557505675057750587505975060750617506275063750647506575066750677506875069750707507175072750737507475075750767507775078750797508075081750827508375084750857508675087750887508975090750917509275093750947509575096750977509875099751007510175102751037510475105751067510775108751097511075111751127511375114751157511675117751187511975120751217512275123751247512575126751277512875129751307513175132751337513475135751367513775138751397514075141751427514375144751457514675147751487514975150751517515275153751547515575156751577515875159751607516175162751637516475165751667516775168751697517075171751727517375174751757517675177751787517975180751817518275183751847518575186751877518875189751907519175192751937519475195751967519775198751997520075201752027520375204752057520675207752087520975210752117521275213752147521575216752177521875219752207522175222752237522475225752267522775228752297523075231752327523375234752357523675237752387523975240752417524275243752447524575246752477524875249752507525175252752537525475255752567525775258752597526075261752627526375264752657526675267752687526975270752717527275273752747527575276752777527875279752807528175282752837528475285752867528775288752897529075291752927529375294752957529675297752987529975300753017530275303753047530575306753077530875309753107531175312753137531475315753167531775318753197532075321753227532375324753257532675327753287532975330753317533275333753347533575336753377533875339753407534175342753437534475345753467534775348753497535075351753527535375354753557535675357753587535975360753617536275363753647536575366753677536875369753707537175372753737537475375753767537775378753797538075381753827538375384753857538675387753887538975390753917539275393753947539575396753977539875399754007540175402754037540475405754067540775408754097541075411754127541375414754157541675417754187541975420754217542275423754247542575426754277542875429754307543175432754337543475435754367543775438754397544075441754427544375444754457544675447754487544975450754517545275453754547545575456754577545875459754607546175462754637546475465754667546775468754697547075471754727547375474754757547675477754787547975480754817548275483754847548575486754877548875489754907549175492754937549475495754967549775498754997550075501755027550375504755057550675507755087550975510755117551275513755147551575516755177551875519755207552175522755237552475525755267552775528755297553075531755327553375534755357553675537755387553975540755417554275543755447554575546755477554875549755507555175552755537555475555755567555775558755597556075561755627556375564755657556675567755687556975570755717557275573755747557575576755777557875579755807558175582755837558475585755867558775588755897559075591755927559375594755957559675597755987559975600756017560275603756047560575606756077560875609756107561175612756137561475615756167561775618756197562075621756227562375624756257562675627756287562975630756317563275633756347563575636756377563875639756407564175642756437564475645756467564775648756497565075651756527565375654756557565675657756587565975660756617566275663756647566575666756677566875669756707567175672756737567475675756767567775678756797568075681756827568375684756857568675687756887568975690756917569275693756947569575696756977569875699757007570175702757037570475705757067570775708757097571075711757127571375714757157571675717757187571975720757217572275723757247572575726757277572875729757307573175732757337573475735757367573775738757397574075741757427574375744757457574675747757487574975750757517575275753757547575575756757577575875759757607576175762757637576475765757667576775768757697577075771757727577375774757757577675777757787577975780757817578275783757847578575786757877578875789757907579175792757937579475795757967579775798757997580075801758027580375804758057580675807758087580975810758117581275813758147581575816758177581875819758207582175822758237582475825758267582775828758297583075831758327583375834758357583675837758387583975840758417584275843758447584575846758477584875849758507585175852758537585475855758567585775858758597586075861758627586375864758657586675867758687586975870758717587275873758747587575876758777587875879758807588175882758837588475885758867588775888758897589075891758927589375894758957589675897758987589975900759017590275903759047590575906759077590875909759107591175912759137591475915759167591775918759197592075921759227592375924759257592675927759287592975930759317593275933759347593575936759377593875939759407594175942759437594475945759467594775948759497595075951759527595375954759557595675957759587595975960759617596275963759647596575966759677596875969759707597175972759737597475975759767597775978759797598075981759827598375984759857598675987759887598975990759917599275993759947599575996759977599875999760007600176002760037600476005760067600776008760097601076011760127601376014760157601676017760187601976020760217602276023760247602576026760277602876029760307603176032760337603476035760367603776038760397604076041760427604376044760457604676047760487604976050760517605276053760547605576056760577605876059760607606176062760637606476065760667606776068760697607076071760727607376074760757607676077760787607976080760817608276083760847608576086760877608876089760907609176092760937609476095760967609776098760997610076101761027610376104761057610676107761087610976110761117611276113761147611576116761177611876119761207612176122761237612476125761267612776128761297613076131761327613376134761357613676137761387613976140761417614276143761447614576146761477614876149761507615176152761537615476155761567615776158761597616076161761627616376164761657616676167761687616976170761717617276173761747617576176761777617876179761807618176182761837618476185761867618776188761897619076191761927619376194761957619676197761987619976200762017620276203762047620576206762077620876209762107621176212762137621476215762167621776218762197622076221762227622376224762257622676227762287622976230762317623276233762347623576236762377623876239762407624176242762437624476245762467624776248762497625076251762527625376254762557625676257762587625976260762617626276263762647626576266762677626876269762707627176272762737627476275762767627776278762797628076281762827628376284762857628676287762887628976290762917629276293762947629576296762977629876299763007630176302763037630476305763067630776308763097631076311763127631376314763157631676317763187631976320763217632276323763247632576326763277632876329763307633176332763337633476335763367633776338763397634076341763427634376344763457634676347763487634976350763517635276353763547635576356763577635876359763607636176362763637636476365763667636776368763697637076371763727637376374763757637676377763787637976380763817638276383763847638576386763877638876389763907639176392763937639476395763967639776398763997640076401764027640376404764057640676407764087640976410764117641276413764147641576416764177641876419764207642176422764237642476425764267642776428764297643076431764327643376434764357643676437764387643976440764417644276443764447644576446764477644876449764507645176452764537645476455764567645776458764597646076461764627646376464764657646676467764687646976470764717647276473764747647576476764777647876479764807648176482764837648476485764867648776488764897649076491764927649376494764957649676497764987649976500765017650276503765047650576506765077650876509765107651176512765137651476515765167651776518765197652076521765227652376524765257652676527765287652976530765317653276533765347653576536765377653876539765407654176542765437654476545765467654776548765497655076551765527655376554765557655676557765587655976560765617656276563765647656576566765677656876569765707657176572765737657476575765767657776578765797658076581765827658376584765857658676587765887658976590765917659276593765947659576596765977659876599766007660176602766037660476605766067660776608766097661076611766127661376614766157661676617766187661976620766217662276623766247662576626766277662876629766307663176632766337663476635766367663776638766397664076641766427664376644766457664676647766487664976650766517665276653766547665576656766577665876659766607666176662766637666476665766667666776668766697667076671766727667376674766757667676677766787667976680766817668276683766847668576686766877668876689766907669176692766937669476695766967669776698766997670076701767027670376704767057670676707767087670976710767117671276713767147671576716767177671876719767207672176722767237672476725767267672776728767297673076731767327673376734767357673676737767387673976740767417674276743767447674576746767477674876749767507675176752767537675476755767567675776758767597676076761767627676376764767657676676767767687676976770767717677276773767747677576776767777677876779767807678176782767837678476785767867678776788767897679076791767927679376794767957679676797767987679976800768017680276803768047680576806768077680876809768107681176812768137681476815768167681776818768197682076821768227682376824768257682676827768287682976830768317683276833768347683576836768377683876839768407684176842768437684476845768467684776848768497685076851768527685376854768557685676857768587685976860768617686276863768647686576866768677686876869768707687176872768737687476875768767687776878768797688076881768827688376884768857688676887768887688976890768917689276893768947689576896768977689876899769007690176902769037690476905769067690776908769097691076911769127691376914769157691676917769187691976920769217692276923769247692576926769277692876929769307693176932769337693476935769367693776938769397694076941769427694376944769457694676947769487694976950769517695276953769547695576956769577695876959769607696176962769637696476965769667696776968769697697076971769727697376974769757697676977769787697976980769817698276983769847698576986769877698876989769907699176992769937699476995769967699776998769997700077001770027700377004770057700677007770087700977010770117701277013770147701577016770177701877019770207702177022770237702477025770267702777028770297703077031770327703377034770357703677037770387703977040770417704277043770447704577046770477704877049770507705177052770537705477055770567705777058770597706077061770627706377064770657706677067770687706977070770717707277073770747707577076770777707877079770807708177082770837708477085770867708777088770897709077091770927709377094770957709677097770987709977100771017710277103771047710577106771077710877109771107711177112771137711477115771167711777118771197712077121771227712377124771257712677127771287712977130771317713277133771347713577136771377713877139771407714177142771437714477145771467714777148771497715077151771527715377154771557715677157771587715977160771617716277163771647716577166771677716877169771707717177172771737717477175771767717777178771797718077181771827718377184771857718677187771887718977190771917719277193771947719577196771977719877199772007720177202772037720477205772067720777208772097721077211772127721377214772157721677217772187721977220772217722277223772247722577226772277722877229772307723177232772337723477235772367723777238772397724077241772427724377244772457724677247772487724977250772517725277253772547725577256772577725877259772607726177262772637726477265772667726777268772697727077271772727727377274772757727677277772787727977280772817728277283772847728577286772877728877289772907729177292772937729477295772967729777298772997730077301773027730377304773057730677307773087730977310773117731277313773147731577316773177731877319773207732177322773237732477325773267732777328773297733077331773327733377334773357733677337773387733977340773417734277343773447734577346773477734877349773507735177352773537735477355773567735777358773597736077361773627736377364773657736677367773687736977370773717737277373773747737577376773777737877379773807738177382773837738477385773867738777388773897739077391773927739377394773957739677397773987739977400774017740277403774047740577406774077740877409774107741177412774137741477415774167741777418774197742077421774227742377424774257742677427774287742977430774317743277433774347743577436774377743877439774407744177442774437744477445774467744777448774497745077451774527745377454774557745677457774587745977460774617746277463774647746577466774677746877469774707747177472774737747477475774767747777478774797748077481774827748377484774857748677487774887748977490774917749277493774947749577496774977749877499775007750177502775037750477505775067750777508775097751077511775127751377514775157751677517775187751977520775217752277523775247752577526775277752877529775307753177532775337753477535775367753777538775397754077541775427754377544775457754677547775487754977550775517755277553775547755577556775577755877559775607756177562775637756477565775667756777568775697757077571775727757377574775757757677577775787757977580775817758277583775847758577586775877758877589775907759177592775937759477595775967759777598775997760077601776027760377604776057760677607776087760977610776117761277613776147761577616776177761877619776207762177622776237762477625776267762777628776297763077631776327763377634776357763677637776387763977640776417764277643776447764577646776477764877649776507765177652776537765477655776567765777658776597766077661776627766377664776657766677667776687766977670776717767277673776747767577676776777767877679776807768177682776837768477685776867768777688776897769077691776927769377694776957769677697776987769977700777017770277703777047770577706777077770877709777107771177712777137771477715777167771777718777197772077721777227772377724777257772677727777287772977730777317773277733777347773577736777377773877739777407774177742777437774477745777467774777748777497775077751777527775377754777557775677757777587775977760777617776277763777647776577766777677776877769777707777177772777737777477775777767777777778777797778077781777827778377784777857778677787777887778977790777917779277793777947779577796777977779877799778007780177802778037780477805778067780777808778097781077811778127781377814778157781677817778187781977820778217782277823778247782577826778277782877829778307783177832778337783477835778367783777838778397784077841778427784377844778457784677847778487784977850778517785277853778547785577856778577785877859778607786177862778637786477865778667786777868778697787077871778727787377874778757787677877778787787977880778817788277883778847788577886778877788877889778907789177892778937789477895778967789777898778997790077901779027790377904779057790677907779087790977910779117791277913779147791577916779177791877919779207792177922779237792477925779267792777928779297793077931779327793377934779357793677937779387793977940779417794277943779447794577946779477794877949779507795177952779537795477955779567795777958779597796077961779627796377964779657796677967779687796977970779717797277973779747797577976779777797877979779807798177982779837798477985779867798777988779897799077991779927799377994779957799677997779987799978000780017800278003780047800578006780077800878009780107801178012780137801478015780167801778018780197802078021780227802378024780257802678027780287802978030780317803278033780347803578036780377803878039780407804178042780437804478045780467804778048780497805078051780527805378054780557805678057780587805978060780617806278063780647806578066780677806878069780707807178072780737807478075780767807778078780797808078081780827808378084780857808678087780887808978090780917809278093780947809578096780977809878099781007810178102781037810478105781067810778108781097811078111781127811378114781157811678117781187811978120781217812278123781247812578126781277812878129781307813178132781337813478135781367813778138781397814078141781427814378144781457814678147781487814978150781517815278153781547815578156781577815878159781607816178162781637816478165781667816778168781697817078171781727817378174781757817678177781787817978180781817818278183781847818578186781877818878189781907819178192781937819478195781967819778198781997820078201782027820378204782057820678207782087820978210782117821278213782147821578216782177821878219782207822178222782237822478225782267822778228782297823078231782327823378234782357823678237782387823978240782417824278243782447824578246782477824878249782507825178252782537825478255782567825778258782597826078261782627826378264782657826678267782687826978270782717827278273782747827578276782777827878279782807828178282782837828478285782867828778288782897829078291782927829378294782957829678297782987829978300783017830278303783047830578306783077830878309783107831178312783137831478315783167831778318783197832078321783227832378324783257832678327783287832978330783317833278333783347833578336783377833878339783407834178342783437834478345783467834778348783497835078351783527835378354783557835678357783587835978360783617836278363783647836578366783677836878369783707837178372783737837478375783767837778378783797838078381783827838378384783857838678387783887838978390783917839278393783947839578396783977839878399784007840178402784037840478405784067840778408784097841078411784127841378414784157841678417784187841978420784217842278423784247842578426784277842878429784307843178432784337843478435784367843778438784397844078441784427844378444784457844678447784487844978450784517845278453784547845578456784577845878459784607846178462784637846478465784667846778468784697847078471784727847378474784757847678477784787847978480784817848278483784847848578486784877848878489784907849178492784937849478495784967849778498784997850078501785027850378504785057850678507785087850978510785117851278513785147851578516785177851878519785207852178522785237852478525785267852778528785297853078531785327853378534785357853678537785387853978540785417854278543785447854578546785477854878549785507855178552785537855478555785567855778558785597856078561785627856378564785657856678567785687856978570785717857278573785747857578576785777857878579785807858178582785837858478585785867858778588785897859078591785927859378594785957859678597785987859978600786017860278603786047860578606786077860878609786107861178612786137861478615786167861778618786197862078621786227862378624786257862678627786287862978630786317863278633786347863578636786377863878639786407864178642786437864478645786467864778648786497865078651786527865378654786557865678657786587865978660786617866278663786647866578666786677866878669786707867178672786737867478675786767867778678786797868078681786827868378684786857868678687786887868978690786917869278693786947869578696786977869878699787007870178702787037870478705787067870778708787097871078711787127871378714787157871678717787187871978720787217872278723787247872578726787277872878729787307873178732787337873478735787367873778738787397874078741787427874378744787457874678747787487874978750787517875278753787547875578756787577875878759787607876178762787637876478765787667876778768787697877078771787727877378774787757877678777787787877978780787817878278783787847878578786787877878878789787907879178792787937879478795787967879778798787997880078801788027880378804788057880678807788087880978810788117881278813788147881578816788177881878819788207882178822788237882478825788267882778828788297883078831788327883378834788357883678837788387883978840788417884278843788447884578846788477884878849788507885178852788537885478855788567885778858788597886078861788627886378864788657886678867788687886978870788717887278873788747887578876788777887878879788807888178882788837888478885788867888778888788897889078891788927889378894788957889678897788987889978900789017890278903789047890578906789077890878909789107891178912789137891478915789167891778918789197892078921789227892378924789257892678927789287892978930789317893278933789347893578936789377893878939789407894178942789437894478945789467894778948789497895078951789527895378954789557895678957789587895978960789617896278963789647896578966789677896878969789707897178972789737897478975789767897778978789797898078981789827898378984789857898678987789887898978990789917899278993789947899578996789977899878999790007900179002790037900479005790067900779008790097901079011790127901379014790157901679017790187901979020790217902279023790247902579026790277902879029790307903179032790337903479035790367903779038790397904079041790427904379044790457904679047790487904979050790517905279053790547905579056790577905879059790607906179062790637906479065790667906779068790697907079071790727907379074790757907679077790787907979080790817908279083790847908579086790877908879089790907909179092790937909479095790967909779098790997910079101791027910379104791057910679107791087910979110791117911279113791147911579116791177911879119791207912179122791237912479125791267912779128791297913079131791327913379134791357913679137791387913979140791417914279143791447914579146791477914879149791507915179152791537915479155791567915779158791597916079161791627916379164791657916679167791687916979170791717917279173791747917579176791777917879179791807918179182791837918479185791867918779188791897919079191791927919379194791957919679197791987919979200792017920279203792047920579206792077920879209792107921179212792137921479215792167921779218792197922079221792227922379224792257922679227792287922979230792317923279233792347923579236792377923879239792407924179242792437924479245792467924779248792497925079251792527925379254792557925679257792587925979260792617926279263792647926579266792677926879269792707927179272792737927479275792767927779278792797928079281792827928379284792857928679287792887928979290792917929279293792947929579296792977929879299793007930179302793037930479305793067930779308793097931079311793127931379314793157931679317793187931979320793217932279323793247932579326793277932879329793307933179332793337933479335793367933779338793397934079341793427934379344793457934679347793487934979350793517935279353793547935579356793577935879359793607936179362793637936479365793667936779368793697937079371793727937379374793757937679377793787937979380793817938279383793847938579386793877938879389793907939179392793937939479395793967939779398793997940079401794027940379404794057940679407794087940979410794117941279413794147941579416794177941879419794207942179422794237942479425794267942779428794297943079431794327943379434794357943679437794387943979440794417944279443794447944579446794477944879449794507945179452794537945479455794567945779458794597946079461794627946379464794657946679467794687946979470794717947279473794747947579476794777947879479794807948179482794837948479485794867948779488794897949079491794927949379494794957949679497794987949979500795017950279503795047950579506795077950879509795107951179512795137951479515795167951779518795197952079521795227952379524795257952679527795287952979530795317953279533795347953579536795377953879539795407954179542795437954479545795467954779548795497955079551795527955379554795557955679557795587955979560795617956279563795647956579566795677956879569795707957179572795737957479575795767957779578795797958079581795827958379584795857958679587795887958979590795917959279593795947959579596795977959879599796007960179602796037960479605796067960779608796097961079611796127961379614796157961679617796187961979620796217962279623796247962579626796277962879629796307963179632796337963479635796367963779638796397964079641796427964379644796457964679647796487964979650796517965279653796547965579656796577965879659796607966179662796637966479665796667966779668796697967079671796727967379674796757967679677796787967979680796817968279683796847968579686796877968879689796907969179692796937969479695796967969779698796997970079701797027970379704797057970679707797087970979710797117971279713797147971579716797177971879719797207972179722797237972479725797267972779728797297973079731797327973379734797357973679737797387973979740797417974279743797447974579746797477974879749797507975179752797537975479755797567975779758797597976079761797627976379764797657976679767797687976979770797717977279773797747977579776797777977879779797807978179782797837978479785797867978779788797897979079791797927979379794797957979679797797987979979800798017980279803798047980579806798077980879809798107981179812798137981479815798167981779818798197982079821798227982379824798257982679827798287982979830798317983279833798347983579836798377983879839798407984179842798437984479845798467984779848798497985079851798527985379854798557985679857798587985979860798617986279863798647986579866798677986879869798707987179872798737987479875798767987779878798797988079881798827988379884798857988679887798887988979890798917989279893798947989579896798977989879899799007990179902799037990479905799067990779908799097991079911799127991379914799157991679917799187991979920799217992279923799247992579926799277992879929799307993179932799337993479935799367993779938799397994079941799427994379944799457994679947799487994979950799517995279953799547995579956799577995879959799607996179962799637996479965799667996779968799697997079971799727997379974799757997679977799787997979980799817998279983799847998579986799877998879989799907999179992799937999479995799967999779998799998000080001800028000380004800058000680007800088000980010800118001280013800148001580016800178001880019800208002180022800238002480025800268002780028800298003080031800328003380034800358003680037800388003980040800418004280043800448004580046800478004880049800508005180052800538005480055800568005780058800598006080061800628006380064800658006680067800688006980070800718007280073800748007580076800778007880079800808008180082800838008480085800868008780088800898009080091800928009380094800958009680097800988009980100801018010280103801048010580106801078010880109801108011180112801138011480115801168011780118801198012080121801228012380124801258012680127801288012980130801318013280133801348013580136801378013880139801408014180142801438014480145801468014780148801498015080151801528015380154801558015680157801588015980160801618016280163801648016580166801678016880169801708017180172801738017480175801768017780178801798018080181801828018380184801858018680187801888018980190801918019280193801948019580196801978019880199802008020180202802038020480205802068020780208802098021080211802128021380214802158021680217802188021980220802218022280223802248022580226802278022880229802308023180232802338023480235802368023780238802398024080241802428024380244802458024680247802488024980250802518025280253802548025580256802578025880259802608026180262802638026480265802668026780268802698027080271802728027380274802758027680277802788027980280802818028280283802848028580286802878028880289802908029180292802938029480295802968029780298802998030080301803028030380304803058030680307803088030980310803118031280313803148031580316803178031880319803208032180322803238032480325803268032780328803298033080331803328033380334803358033680337803388033980340803418034280343803448034580346803478034880349803508035180352803538035480355803568035780358803598036080361803628036380364803658036680367803688036980370803718037280373803748037580376803778037880379803808038180382803838038480385803868038780388803898039080391803928039380394803958039680397803988039980400804018040280403804048040580406804078040880409804108041180412804138041480415804168041780418804198042080421804228042380424804258042680427804288042980430804318043280433804348043580436804378043880439804408044180442804438044480445804468044780448804498045080451804528045380454804558045680457804588045980460804618046280463804648046580466804678046880469804708047180472804738047480475804768047780478804798048080481804828048380484804858048680487804888048980490804918049280493804948049580496804978049880499805008050180502805038050480505805068050780508805098051080511805128051380514805158051680517805188051980520805218052280523805248052580526805278052880529805308053180532805338053480535805368053780538805398054080541805428054380544805458054680547805488054980550805518055280553805548055580556805578055880559805608056180562805638056480565805668056780568805698057080571805728057380574805758057680577805788057980580805818058280583805848058580586805878058880589805908059180592805938059480595805968059780598805998060080601806028060380604806058060680607806088060980610806118061280613806148061580616806178061880619806208062180622806238062480625806268062780628806298063080631806328063380634806358063680637806388063980640806418064280643806448064580646806478064880649806508065180652806538065480655806568065780658806598066080661806628066380664806658066680667806688066980670806718067280673806748067580676806778067880679806808068180682806838068480685806868068780688806898069080691806928069380694806958069680697806988069980700807018070280703807048070580706807078070880709807108071180712807138071480715807168071780718807198072080721807228072380724807258072680727807288072980730807318073280733807348073580736807378073880739807408074180742807438074480745807468074780748807498075080751807528075380754807558075680757807588075980760807618076280763807648076580766807678076880769807708077180772807738077480775807768077780778807798078080781807828078380784807858078680787807888078980790807918079280793807948079580796807978079880799808008080180802808038080480805808068080780808808098081080811808128081380814808158081680817808188081980820808218082280823808248082580826808278082880829808308083180832808338083480835808368083780838808398084080841808428084380844808458084680847808488084980850808518085280853808548085580856808578085880859808608086180862808638086480865808668086780868808698087080871808728087380874808758087680877808788087980880808818088280883808848088580886808878088880889808908089180892808938089480895808968089780898808998090080901809028090380904809058090680907809088090980910809118091280913809148091580916809178091880919809208092180922809238092480925809268092780928809298093080931809328093380934809358093680937809388093980940809418094280943809448094580946809478094880949809508095180952809538095480955809568095780958809598096080961809628096380964809658096680967809688096980970809718097280973809748097580976809778097880979809808098180982809838098480985809868098780988809898099080991809928099380994809958099680997809988099981000810018100281003810048100581006810078100881009810108101181012810138101481015810168101781018810198102081021810228102381024810258102681027810288102981030810318103281033810348103581036810378103881039810408104181042810438104481045810468104781048810498105081051810528105381054810558105681057810588105981060810618106281063810648106581066810678106881069810708107181072810738107481075810768107781078810798108081081810828108381084810858108681087810888108981090810918109281093810948109581096810978109881099811008110181102811038110481105811068110781108811098111081111811128111381114811158111681117811188111981120811218112281123811248112581126811278112881129811308113181132811338113481135811368113781138811398114081141811428114381144811458114681147811488114981150811518115281153811548115581156811578115881159811608116181162811638116481165811668116781168811698117081171811728117381174811758117681177811788117981180811818118281183811848118581186811878118881189811908119181192811938119481195811968119781198811998120081201812028120381204812058120681207812088120981210812118121281213812148121581216812178121881219812208122181222812238122481225812268122781228812298123081231812328123381234812358123681237812388123981240812418124281243812448124581246812478124881249812508125181252812538125481255812568125781258812598126081261812628126381264812658126681267812688126981270812718127281273812748127581276812778127881279812808128181282812838128481285812868128781288812898129081291812928129381294812958129681297812988129981300813018130281303813048130581306813078130881309813108131181312813138131481315813168131781318813198132081321813228132381324813258132681327813288132981330813318133281333813348133581336813378133881339813408134181342813438134481345813468134781348813498135081351813528135381354813558135681357813588135981360813618136281363813648136581366813678136881369813708137181372813738137481375813768137781378813798138081381813828138381384813858138681387813888138981390813918139281393813948139581396813978139881399814008140181402814038140481405814068140781408814098141081411814128141381414814158141681417814188141981420814218142281423814248142581426814278142881429814308143181432814338143481435814368143781438814398144081441814428144381444814458144681447814488144981450814518145281453814548145581456814578145881459814608146181462814638146481465814668146781468814698147081471814728147381474814758147681477814788147981480814818148281483814848148581486814878148881489814908149181492814938149481495814968149781498814998150081501815028150381504815058150681507815088150981510815118151281513815148151581516815178151881519815208152181522815238152481525815268152781528815298153081531815328153381534815358153681537815388153981540815418154281543815448154581546815478154881549815508155181552815538155481555815568155781558815598156081561815628156381564815658156681567815688156981570815718157281573815748157581576815778157881579815808158181582815838158481585815868158781588815898159081591815928159381594815958159681597815988159981600816018160281603816048160581606816078160881609816108161181612816138161481615816168161781618816198162081621816228162381624816258162681627816288162981630816318163281633816348163581636816378163881639816408164181642816438164481645816468164781648816498165081651816528165381654816558165681657816588165981660816618166281663816648166581666816678166881669816708167181672816738167481675816768167781678816798168081681816828168381684816858168681687816888168981690816918169281693816948169581696816978169881699817008170181702817038170481705817068170781708817098171081711817128171381714817158171681717817188171981720817218172281723817248172581726817278172881729817308173181732817338173481735817368173781738817398174081741817428174381744817458174681747817488174981750817518175281753817548175581756817578175881759817608176181762817638176481765817668176781768817698177081771817728177381774817758177681777817788177981780817818178281783817848178581786817878178881789817908179181792817938179481795817968179781798817998180081801818028180381804818058180681807818088180981810818118181281813818148181581816818178181881819818208182181822818238182481825818268182781828818298183081831818328183381834818358183681837818388183981840818418184281843818448184581846818478184881849818508185181852818538185481855818568185781858818598186081861818628186381864818658186681867818688186981870818718187281873818748187581876818778187881879818808188181882818838188481885818868188781888818898189081891818928189381894818958189681897818988189981900819018190281903819048190581906819078190881909819108191181912819138191481915819168191781918819198192081921819228192381924819258192681927819288192981930819318193281933819348193581936819378193881939819408194181942819438194481945819468194781948819498195081951819528195381954819558195681957819588195981960819618196281963819648196581966819678196881969819708197181972819738197481975819768197781978819798198081981819828198381984819858198681987819888198981990819918199281993819948199581996819978199881999820008200182002820038200482005820068200782008820098201082011820128201382014820158201682017820188201982020820218202282023820248202582026820278202882029820308203182032820338203482035820368203782038820398204082041820428204382044820458204682047820488204982050820518205282053820548205582056820578205882059820608206182062820638206482065820668206782068820698207082071820728207382074820758207682077820788207982080820818208282083820848208582086820878208882089820908209182092820938209482095820968209782098820998210082101821028210382104821058210682107821088210982110821118211282113821148211582116821178211882119821208212182122821238212482125821268212782128821298213082131821328213382134821358213682137821388213982140821418214282143821448214582146821478214882149821508215182152821538215482155821568215782158821598216082161821628216382164821658216682167821688216982170821718217282173821748217582176821778217882179821808218182182821838218482185821868218782188821898219082191821928219382194821958219682197821988219982200822018220282203822048220582206822078220882209822108221182212822138221482215822168221782218822198222082221822228222382224822258222682227822288222982230822318223282233822348223582236822378223882239822408224182242822438224482245822468224782248822498225082251822528225382254822558225682257822588225982260822618226282263822648226582266822678226882269822708227182272822738227482275822768227782278822798228082281822828228382284822858228682287822888228982290822918229282293822948229582296822978229882299823008230182302823038230482305823068230782308823098231082311823128231382314823158231682317823188231982320823218232282323823248232582326823278232882329823308233182332823338233482335823368233782338823398234082341823428234382344823458234682347823488234982350823518235282353823548235582356823578235882359823608236182362823638236482365823668236782368823698237082371823728237382374823758237682377823788237982380823818238282383823848238582386823878238882389823908239182392823938239482395823968239782398823998240082401824028240382404824058240682407824088240982410824118241282413824148241582416824178241882419824208242182422824238242482425824268242782428824298243082431824328243382434824358243682437824388243982440824418244282443824448244582446824478244882449824508245182452824538245482455824568245782458824598246082461824628246382464824658246682467824688246982470824718247282473824748247582476824778247882479824808248182482824838248482485824868248782488824898249082491824928249382494824958249682497824988249982500825018250282503825048250582506825078250882509825108251182512825138251482515825168251782518825198252082521825228252382524825258252682527825288252982530825318253282533825348253582536825378253882539825408254182542825438254482545825468254782548825498255082551825528255382554825558255682557825588255982560825618256282563825648256582566825678256882569825708257182572825738257482575825768257782578825798258082581825828258382584825858258682587825888258982590825918259282593825948259582596825978259882599826008260182602826038260482605826068260782608826098261082611826128261382614826158261682617826188261982620826218262282623826248262582626826278262882629826308263182632826338263482635826368263782638826398264082641826428264382644826458264682647826488264982650826518265282653826548265582656826578265882659826608266182662826638266482665826668266782668826698267082671826728267382674826758267682677826788267982680826818268282683826848268582686826878268882689826908269182692826938269482695826968269782698826998270082701827028270382704827058270682707827088270982710827118271282713827148271582716827178271882719827208272182722827238272482725827268272782728827298273082731827328273382734827358273682737827388273982740827418274282743827448274582746827478274882749827508275182752827538275482755827568275782758827598276082761827628276382764827658276682767827688276982770827718277282773827748277582776827778277882779827808278182782827838278482785827868278782788827898279082791827928279382794827958279682797827988279982800828018280282803828048280582806828078280882809828108281182812828138281482815828168281782818828198282082821828228282382824828258282682827828288282982830828318283282833828348283582836828378283882839828408284182842828438284482845828468284782848828498285082851828528285382854828558285682857828588285982860828618286282863828648286582866828678286882869828708287182872828738287482875828768287782878828798288082881828828288382884828858288682887828888288982890828918289282893828948289582896828978289882899829008290182902829038290482905829068290782908829098291082911829128291382914829158291682917829188291982920829218292282923829248292582926829278292882929829308293182932829338293482935829368293782938829398294082941829428294382944829458294682947829488294982950829518295282953829548295582956829578295882959829608296182962829638296482965829668296782968829698297082971829728297382974829758297682977829788297982980829818298282983829848298582986829878298882989829908299182992829938299482995829968299782998829998300083001830028300383004830058300683007830088300983010830118301283013830148301583016830178301883019830208302183022830238302483025830268302783028830298303083031830328303383034830358303683037830388303983040830418304283043830448304583046830478304883049830508305183052830538305483055830568305783058830598306083061830628306383064830658306683067830688306983070830718307283073830748307583076830778307883079830808308183082830838308483085830868308783088830898309083091830928309383094830958309683097830988309983100831018310283103831048310583106831078310883109831108311183112831138311483115831168311783118831198312083121831228312383124831258312683127831288312983130831318313283133831348313583136831378313883139831408314183142831438314483145831468314783148831498315083151831528315383154831558315683157831588315983160831618316283163831648316583166831678316883169831708317183172831738317483175831768317783178831798318083181831828318383184831858318683187831888318983190831918319283193831948319583196831978319883199832008320183202832038320483205832068320783208832098321083211832128321383214832158321683217832188321983220832218322283223832248322583226832278322883229832308323183232832338323483235832368323783238832398324083241832428324383244832458324683247832488324983250832518325283253832548325583256832578325883259832608326183262832638326483265832668326783268832698327083271832728327383274832758327683277832788327983280832818328283283832848328583286832878328883289832908329183292832938329483295832968329783298832998330083301833028330383304833058330683307833088330983310833118331283313833148331583316833178331883319833208332183322833238332483325833268332783328833298333083331833328333383334833358333683337833388333983340833418334283343833448334583346833478334883349833508335183352833538335483355833568335783358833598336083361833628336383364833658336683367833688336983370833718337283373833748337583376833778337883379833808338183382833838338483385833868338783388833898339083391833928339383394833958339683397833988339983400834018340283403834048340583406834078340883409834108341183412834138341483415834168341783418834198342083421834228342383424834258342683427834288342983430834318343283433834348343583436834378343883439834408344183442834438344483445834468344783448834498345083451834528345383454834558345683457834588345983460834618346283463834648346583466834678346883469834708347183472834738347483475834768347783478834798348083481834828348383484834858348683487834888348983490834918349283493834948349583496834978349883499835008350183502835038350483505835068350783508835098351083511835128351383514835158351683517835188351983520835218352283523835248352583526835278352883529835308353183532835338353483535835368353783538835398354083541835428354383544835458354683547835488354983550835518355283553835548355583556835578355883559835608356183562835638356483565835668356783568835698357083571835728357383574835758357683577835788357983580835818358283583835848358583586835878358883589835908359183592835938359483595835968359783598835998360083601836028360383604836058360683607836088360983610836118361283613836148361583616836178361883619836208362183622836238362483625836268362783628836298363083631836328363383634836358363683637836388363983640836418364283643836448364583646836478364883649836508365183652836538365483655836568365783658836598366083661836628366383664836658366683667836688366983670836718367283673836748367583676836778367883679836808368183682836838368483685836868368783688836898369083691836928369383694836958369683697836988369983700837018370283703837048370583706837078370883709837108371183712837138371483715837168371783718837198372083721837228372383724837258372683727837288372983730837318373283733837348373583736837378373883739837408374183742837438374483745837468374783748837498375083751837528375383754837558375683757837588375983760837618376283763837648376583766837678376883769837708377183772837738377483775837768377783778837798378083781837828378383784837858378683787837888378983790837918379283793837948379583796837978379883799838008380183802838038380483805838068380783808838098381083811838128381383814838158381683817838188381983820838218382283823838248382583826838278382883829838308383183832838338383483835838368383783838838398384083841838428384383844838458384683847838488384983850838518385283853838548385583856838578385883859838608386183862838638386483865838668386783868838698387083871838728387383874838758387683877838788387983880838818388283883838848388583886838878388883889838908389183892838938389483895838968389783898838998390083901839028390383904839058390683907839088390983910839118391283913839148391583916839178391883919839208392183922839238392483925839268392783928839298393083931839328393383934839358393683937839388393983940839418394283943839448394583946839478394883949839508395183952839538395483955839568395783958839598396083961839628396383964839658396683967839688396983970839718397283973839748397583976839778397883979839808398183982839838398483985839868398783988839898399083991839928399383994839958399683997839988399984000840018400284003840048400584006840078400884009840108401184012840138401484015840168401784018840198402084021840228402384024840258402684027840288402984030840318403284033840348403584036840378403884039840408404184042840438404484045840468404784048840498405084051840528405384054840558405684057840588405984060840618406284063840648406584066840678406884069840708407184072840738407484075840768407784078840798408084081840828408384084840858408684087840888408984090840918409284093840948409584096840978409884099841008410184102841038410484105841068410784108841098411084111841128411384114841158411684117841188411984120841218412284123841248412584126841278412884129841308413184132841338413484135841368413784138841398414084141841428414384144841458414684147841488414984150841518415284153841548415584156841578415884159841608416184162841638416484165841668416784168841698417084171841728417384174841758417684177841788417984180841818418284183841848418584186841878418884189841908419184192841938419484195841968419784198841998420084201842028420384204842058420684207842088420984210842118421284213842148421584216842178421884219842208422184222842238422484225842268422784228842298423084231842328423384234842358423684237842388423984240842418424284243842448424584246842478424884249842508425184252842538425484255842568425784258842598426084261842628426384264842658426684267842688426984270842718427284273842748427584276842778427884279842808428184282842838428484285842868428784288842898429084291842928429384294842958429684297842988429984300843018430284303843048430584306843078430884309843108431184312843138431484315843168431784318843198432084321843228432384324843258432684327843288432984330843318433284333843348433584336843378433884339843408434184342843438434484345843468434784348843498435084351843528435384354843558435684357843588435984360843618436284363843648436584366843678436884369843708437184372843738437484375843768437784378843798438084381843828438384384843858438684387843888438984390843918439284393843948439584396843978439884399844008440184402844038440484405844068440784408844098441084411844128441384414844158441684417844188441984420844218442284423844248442584426844278442884429844308443184432844338443484435844368443784438844398444084441844428444384444844458444684447844488444984450844518445284453844548445584456844578445884459844608446184462844638446484465844668446784468844698447084471844728447384474844758447684477844788447984480844818448284483844848448584486844878448884489844908449184492844938449484495844968449784498844998450084501845028450384504845058450684507845088450984510845118451284513845148451584516845178451884519845208452184522845238452484525845268452784528845298453084531845328453384534845358453684537845388453984540845418454284543845448454584546845478454884549845508455184552845538455484555845568455784558845598456084561845628456384564845658456684567845688456984570845718457284573845748457584576845778457884579845808458184582845838458484585845868458784588845898459084591845928459384594845958459684597845988459984600846018460284603846048460584606846078460884609846108461184612846138461484615846168461784618846198462084621846228462384624846258462684627846288462984630846318463284633846348463584636846378463884639846408464184642846438464484645846468464784648846498465084651846528465384654846558465684657846588465984660846618466284663846648466584666846678466884669846708467184672846738467484675846768467784678846798468084681846828468384684846858468684687846888468984690846918469284693846948469584696846978469884699847008470184702847038470484705847068470784708847098471084711847128471384714847158471684717847188471984720847218472284723847248472584726847278472884729847308473184732847338473484735847368473784738847398474084741847428474384744847458474684747847488474984750847518475284753847548475584756847578475884759847608476184762847638476484765847668476784768847698477084771847728477384774847758477684777847788477984780847818478284783847848478584786847878478884789847908479184792847938479484795847968479784798847998480084801848028480384804848058480684807848088480984810848118481284813848148481584816848178481884819848208482184822848238482484825848268482784828848298483084831848328483384834848358483684837848388483984840848418484284843848448484584846848478484884849848508485184852848538485484855848568485784858848598486084861848628486384864848658486684867848688486984870848718487284873848748487584876848778487884879848808488184882848838488484885848868488784888848898489084891848928489384894848958489684897848988489984900849018490284903849048490584906849078490884909849108491184912849138491484915849168491784918849198492084921849228492384924849258492684927849288492984930849318493284933849348493584936849378493884939849408494184942849438494484945849468494784948849498495084951849528495384954849558495684957849588495984960849618496284963849648496584966849678496884969849708497184972849738497484975849768497784978849798498084981849828498384984849858498684987849888498984990849918499284993849948499584996849978499884999850008500185002850038500485005850068500785008850098501085011850128501385014850158501685017850188501985020850218502285023850248502585026850278502885029850308503185032850338503485035850368503785038850398504085041850428504385044850458504685047850488504985050850518505285053850548505585056850578505885059850608506185062850638506485065850668506785068850698507085071850728507385074850758507685077850788507985080850818508285083850848508585086850878508885089850908509185092850938509485095850968509785098850998510085101851028510385104851058510685107851088510985110851118511285113851148511585116851178511885119851208512185122851238512485125851268512785128851298513085131851328513385134851358513685137851388513985140851418514285143851448514585146851478514885149851508515185152851538515485155851568515785158851598516085161851628516385164851658516685167851688516985170851718517285173851748517585176851778517885179851808518185182851838518485185851868518785188851898519085191851928519385194851958519685197851988519985200852018520285203852048520585206852078520885209852108521185212852138521485215852168521785218852198522085221852228522385224852258522685227852288522985230852318523285233852348523585236852378523885239852408524185242852438524485245852468524785248852498525085251852528525385254852558525685257852588525985260852618526285263852648526585266852678526885269852708527185272852738527485275852768527785278852798528085281852828528385284852858528685287852888528985290852918529285293852948529585296852978529885299853008530185302853038530485305853068530785308853098531085311853128531385314853158531685317853188531985320853218532285323853248532585326853278532885329853308533185332853338533485335853368533785338853398534085341853428534385344853458534685347853488534985350853518535285353853548535585356853578535885359853608536185362853638536485365853668536785368853698537085371853728537385374853758537685377853788537985380853818538285383853848538585386853878538885389853908539185392853938539485395853968539785398853998540085401854028540385404854058540685407854088540985410854118541285413854148541585416854178541885419854208542185422854238542485425854268542785428854298543085431854328543385434854358543685437854388543985440854418544285443854448544585446854478544885449854508545185452854538545485455854568545785458854598546085461854628546385464854658546685467854688546985470854718547285473854748547585476854778547885479854808548185482854838548485485854868548785488854898549085491854928549385494854958549685497854988549985500855018550285503855048550585506855078550885509855108551185512855138551485515855168551785518855198552085521855228552385524855258552685527855288552985530855318553285533855348553585536855378553885539855408554185542855438554485545855468554785548855498555085551855528555385554855558555685557855588555985560855618556285563855648556585566855678556885569855708557185572855738557485575855768557785578855798558085581855828558385584855858558685587855888558985590855918559285593855948559585596855978559885599856008560185602856038560485605856068560785608856098561085611856128561385614856158561685617856188561985620856218562285623856248562585626856278562885629856308563185632856338563485635856368563785638856398564085641856428564385644856458564685647856488564985650856518565285653856548565585656856578565885659856608566185662856638566485665856668566785668856698567085671856728567385674856758567685677856788567985680856818568285683856848568585686856878568885689856908569185692856938569485695856968569785698856998570085701857028570385704857058570685707857088570985710857118571285713857148571585716857178571885719857208572185722857238572485725857268572785728857298573085731857328573385734857358573685737857388573985740857418574285743857448574585746857478574885749857508575185752857538575485755857568575785758857598576085761857628576385764857658576685767857688576985770857718577285773857748577585776857778577885779857808578185782857838578485785857868578785788857898579085791857928579385794857958579685797857988579985800858018580285803858048580585806858078580885809858108581185812858138581485815858168581785818858198582085821858228582385824858258582685827858288582985830858318583285833858348583585836858378583885839858408584185842858438584485845858468584785848858498585085851858528585385854858558585685857858588585985860858618586285863858648586585866858678586885869858708587185872858738587485875858768587785878858798588085881858828588385884858858588685887858888588985890858918589285893858948589585896858978589885899859008590185902859038590485905859068590785908859098591085911859128591385914859158591685917859188591985920859218592285923859248592585926859278592885929859308593185932859338593485935859368593785938859398594085941859428594385944859458594685947859488594985950859518595285953859548595585956859578595885959859608596185962859638596485965859668596785968859698597085971859728597385974859758597685977859788597985980859818598285983859848598585986859878598885989859908599185992859938599485995859968599785998859998600086001860028600386004860058600686007860088600986010860118601286013860148601586016860178601886019860208602186022860238602486025860268602786028860298603086031860328603386034860358603686037860388603986040860418604286043860448604586046860478604886049860508605186052860538605486055860568605786058860598606086061860628606386064860658606686067860688606986070860718607286073860748607586076860778607886079860808608186082860838608486085860868608786088860898609086091860928609386094860958609686097860988609986100861018610286103861048610586106861078610886109861108611186112861138611486115861168611786118861198612086121861228612386124861258612686127861288612986130861318613286133861348613586136861378613886139861408614186142861438614486145861468614786148861498615086151861528615386154861558615686157861588615986160861618616286163861648616586166861678616886169861708617186172861738617486175861768617786178861798618086181861828618386184861858618686187861888618986190861918619286193861948619586196861978619886199862008620186202862038620486205862068620786208862098621086211862128621386214862158621686217862188621986220862218622286223862248622586226862278622886229862308623186232862338623486235862368623786238862398624086241862428624386244862458624686247862488624986250862518625286253862548625586256862578625886259862608626186262862638626486265862668626786268862698627086271862728627386274862758627686277862788627986280862818628286283862848628586286862878628886289862908629186292862938629486295862968629786298862998630086301863028630386304863058630686307863088630986310863118631286313863148631586316863178631886319863208632186322863238632486325863268632786328863298633086331863328633386334863358633686337863388633986340863418634286343863448634586346863478634886349863508635186352863538635486355863568635786358863598636086361863628636386364863658636686367863688636986370863718637286373863748637586376863778637886379863808638186382863838638486385863868638786388863898639086391863928639386394863958639686397863988639986400864018640286403864048640586406864078640886409864108641186412864138641486415864168641786418864198642086421864228642386424864258642686427864288642986430864318643286433864348643586436864378643886439864408644186442864438644486445864468644786448864498645086451864528645386454864558645686457864588645986460864618646286463864648646586466864678646886469864708647186472864738647486475864768647786478864798648086481864828648386484864858648686487864888648986490864918649286493864948649586496864978649886499865008650186502865038650486505865068650786508865098651086511865128651386514865158651686517865188651986520865218652286523865248652586526865278652886529865308653186532865338653486535865368653786538865398654086541865428654386544865458654686547865488654986550865518655286553865548655586556865578655886559865608656186562865638656486565865668656786568865698657086571865728657386574865758657686577865788657986580865818658286583865848658586586865878658886589865908659186592865938659486595865968659786598865998660086601866028660386604866058660686607866088660986610866118661286613866148661586616866178661886619866208662186622866238662486625866268662786628866298663086631866328663386634866358663686637866388663986640866418664286643866448664586646866478664886649866508665186652866538665486655866568665786658866598666086661866628666386664866658666686667866688666986670866718667286673866748667586676866778667886679866808668186682866838668486685866868668786688866898669086691866928669386694866958669686697866988669986700867018670286703867048670586706867078670886709867108671186712867138671486715867168671786718867198672086721867228672386724867258672686727867288672986730867318673286733867348673586736867378673886739867408674186742867438674486745867468674786748867498675086751867528675386754867558675686757867588675986760867618676286763867648676586766867678676886769867708677186772867738677486775867768677786778867798678086781867828678386784867858678686787867888678986790867918679286793867948679586796867978679886799868008680186802868038680486805868068680786808868098681086811868128681386814868158681686817868188681986820868218682286823868248682586826868278682886829868308683186832868338683486835868368683786838868398684086841868428684386844868458684686847868488684986850868518685286853868548685586856868578685886859868608686186862868638686486865868668686786868868698687086871868728687386874868758687686877868788687986880868818688286883868848688586886868878688886889868908689186892868938689486895868968689786898868998690086901869028690386904869058690686907869088690986910869118691286913869148691586916869178691886919869208692186922869238692486925869268692786928869298693086931869328693386934869358693686937869388693986940869418694286943869448694586946869478694886949869508695186952869538695486955869568695786958869598696086961869628696386964869658696686967869688696986970869718697286973869748697586976869778697886979869808698186982869838698486985869868698786988869898699086991869928699386994869958699686997869988699987000870018700287003870048700587006870078700887009870108701187012870138701487015870168701787018870198702087021870228702387024870258702687027870288702987030870318703287033870348703587036870378703887039870408704187042870438704487045870468704787048870498705087051870528705387054870558705687057870588705987060870618706287063870648706587066870678706887069870708707187072870738707487075870768707787078870798708087081870828708387084870858708687087870888708987090870918709287093870948709587096870978709887099871008710187102871038710487105871068710787108871098711087111871128711387114871158711687117871188711987120871218712287123871248712587126871278712887129871308713187132871338713487135871368713787138871398714087141871428714387144871458714687147871488714987150871518715287153871548715587156871578715887159871608716187162871638716487165871668716787168871698717087171871728717387174871758717687177871788717987180871818718287183871848718587186871878718887189871908719187192871938719487195871968719787198871998720087201872028720387204872058720687207872088720987210872118721287213872148721587216872178721887219872208722187222872238722487225872268722787228872298723087231872328723387234872358723687237872388723987240872418724287243872448724587246872478724887249872508725187252872538725487255872568725787258872598726087261872628726387264872658726687267872688726987270872718727287273872748727587276872778727887279872808728187282872838728487285872868728787288872898729087291872928729387294872958729687297872988729987300873018730287303873048730587306873078730887309873108731187312873138731487315873168731787318873198732087321873228732387324873258732687327873288732987330873318733287333873348733587336873378733887339873408734187342873438734487345873468734787348873498735087351873528735387354873558735687357873588735987360873618736287363873648736587366873678736887369873708737187372873738737487375873768737787378873798738087381873828738387384873858738687387873888738987390873918739287393873948739587396873978739887399874008740187402874038740487405874068740787408874098741087411874128741387414874158741687417874188741987420874218742287423874248742587426874278742887429874308743187432874338743487435874368743787438874398744087441874428744387444874458744687447874488744987450874518745287453874548745587456874578745887459874608746187462874638746487465874668746787468874698747087471874728747387474874758747687477874788747987480874818748287483874848748587486874878748887489874908749187492874938749487495874968749787498874998750087501875028750387504875058750687507875088750987510875118751287513875148751587516875178751887519875208752187522875238752487525875268752787528875298753087531875328753387534875358753687537875388753987540875418754287543875448754587546875478754887549875508755187552875538755487555875568755787558875598756087561875628756387564875658756687567875688756987570875718757287573875748757587576875778757887579875808758187582875838758487585875868758787588875898759087591875928759387594875958759687597875988759987600876018760287603876048760587606876078760887609876108761187612876138761487615876168761787618876198762087621876228762387624876258762687627876288762987630876318763287633876348763587636876378763887639876408764187642876438764487645876468764787648876498765087651876528765387654876558765687657876588765987660876618766287663876648766587666876678766887669876708767187672876738767487675876768767787678876798768087681876828768387684876858768687687876888768987690876918769287693876948769587696876978769887699877008770187702877038770487705877068770787708877098771087711877128771387714877158771687717877188771987720877218772287723877248772587726877278772887729877308773187732877338773487735877368773787738877398774087741877428774387744877458774687747877488774987750877518775287753877548775587756877578775887759877608776187762877638776487765877668776787768877698777087771877728777387774877758777687777877788777987780877818778287783877848778587786877878778887789877908779187792877938779487795877968779787798877998780087801878028780387804878058780687807878088780987810878118781287813878148781587816878178781887819878208782187822878238782487825878268782787828878298783087831878328783387834878358783687837878388783987840878418784287843878448784587846878478784887849878508785187852878538785487855878568785787858878598786087861878628786387864878658786687867878688786987870878718787287873878748787587876878778787887879878808788187882878838788487885878868788787888878898789087891878928789387894878958789687897878988789987900879018790287903879048790587906879078790887909879108791187912879138791487915879168791787918879198792087921879228792387924879258792687927879288792987930879318793287933879348793587936879378793887939879408794187942879438794487945879468794787948879498795087951879528795387954879558795687957879588795987960879618796287963879648796587966879678796887969879708797187972879738797487975879768797787978879798798087981879828798387984879858798687987879888798987990879918799287993879948799587996879978799887999880008800188002880038800488005880068800788008880098801088011880128801388014880158801688017880188801988020880218802288023880248802588026880278802888029880308803188032880338803488035880368803788038880398804088041880428804388044880458804688047880488804988050880518805288053880548805588056880578805888059880608806188062880638806488065880668806788068880698807088071880728807388074880758807688077880788807988080880818808288083880848808588086880878808888089880908809188092880938809488095880968809788098880998810088101881028810388104881058810688107881088810988110881118811288113881148811588116881178811888119881208812188122881238812488125881268812788128881298813088131881328813388134881358813688137881388813988140881418814288143881448814588146881478814888149881508815188152881538815488155881568815788158881598816088161881628816388164881658816688167881688816988170881718817288173881748817588176881778817888179881808818188182881838818488185881868818788188881898819088191881928819388194881958819688197881988819988200882018820288203882048820588206882078820888209882108821188212882138821488215882168821788218882198822088221882228822388224882258822688227882288822988230882318823288233882348823588236882378823888239882408824188242882438824488245882468824788248882498825088251882528825388254882558825688257882588825988260882618826288263882648826588266882678826888269882708827188272882738827488275882768827788278882798828088281882828828388284882858828688287882888828988290882918829288293882948829588296882978829888299883008830188302883038830488305883068830788308883098831088311883128831388314883158831688317883188831988320883218832288323883248832588326883278832888329883308833188332883338833488335883368833788338883398834088341883428834388344883458834688347883488834988350883518835288353883548835588356883578835888359883608836188362883638836488365883668836788368883698837088371883728837388374883758837688377883788837988380883818838288383883848838588386883878838888389883908839188392883938839488395883968839788398883998840088401884028840388404884058840688407884088840988410884118841288413884148841588416884178841888419884208842188422884238842488425884268842788428884298843088431884328843388434884358843688437884388843988440884418844288443884448844588446884478844888449884508845188452884538845488455884568845788458884598846088461884628846388464884658846688467884688846988470884718847288473884748847588476884778847888479884808848188482884838848488485884868848788488884898849088491884928849388494884958849688497884988849988500885018850288503885048850588506885078850888509885108851188512885138851488515885168851788518885198852088521885228852388524885258852688527885288852988530885318853288533885348853588536885378853888539885408854188542885438854488545885468854788548885498855088551885528855388554885558855688557885588855988560885618856288563885648856588566885678856888569885708857188572885738857488575885768857788578885798858088581885828858388584885858858688587885888858988590885918859288593885948859588596885978859888599886008860188602886038860488605886068860788608886098861088611886128861388614886158861688617886188861988620886218862288623886248862588626886278862888629886308863188632886338863488635886368863788638886398864088641886428864388644886458864688647886488864988650886518865288653886548865588656886578865888659886608866188662886638866488665886668866788668886698867088671886728867388674886758867688677886788867988680886818868288683886848868588686886878868888689886908869188692886938869488695886968869788698886998870088701887028870388704887058870688707887088870988710887118871288713887148871588716887178871888719887208872188722887238872488725887268872788728887298873088731887328873388734887358873688737887388873988740887418874288743887448874588746887478874888749887508875188752887538875488755887568875788758887598876088761887628876388764887658876688767887688876988770887718877288773887748877588776887778877888779887808878188782887838878488785887868878788788887898879088791887928879388794887958879688797887988879988800888018880288803888048880588806888078880888809888108881188812888138881488815888168881788818888198882088821888228882388824888258882688827888288882988830888318883288833888348883588836888378883888839888408884188842888438884488845888468884788848888498885088851888528885388854888558885688857888588885988860888618886288863888648886588866888678886888869888708887188872888738887488875888768887788878888798888088881888828888388884888858888688887888888888988890888918889288893888948889588896888978889888899889008890188902889038890488905889068890788908889098891088911889128891388914889158891688917889188891988920889218892288923889248892588926889278892888929889308893188932889338893488935889368893788938889398894088941889428894388944889458894688947889488894988950889518895288953889548895588956889578895888959889608896188962889638896488965889668896788968889698897088971889728897388974889758897688977889788897988980889818898288983889848898588986889878898888989889908899188992889938899488995889968899788998889998900089001890028900389004890058900689007890088900989010890118901289013890148901589016890178901889019890208902189022890238902489025890268902789028890298903089031890328903389034890358903689037890388903989040890418904289043890448904589046890478904889049890508905189052890538905489055890568905789058890598906089061890628906389064890658906689067890688906989070890718907289073890748907589076890778907889079890808908189082890838908489085890868908789088890898909089091890928909389094890958909689097890988909989100891018910289103891048910589106891078910889109891108911189112891138911489115891168911789118891198912089121891228912389124891258912689127891288912989130891318913289133891348913589136891378913889139891408914189142891438914489145891468914789148891498915089151891528915389154891558915689157891588915989160891618916289163891648916589166891678916889169891708917189172891738917489175891768917789178891798918089181891828918389184891858918689187891888918989190891918919289193891948919589196891978919889199892008920189202892038920489205892068920789208892098921089211892128921389214892158921689217892188921989220892218922289223892248922589226892278922889229892308923189232892338923489235892368923789238892398924089241892428924389244892458924689247892488924989250892518925289253892548925589256892578925889259892608926189262892638926489265892668926789268892698927089271892728927389274892758927689277892788927989280892818928289283892848928589286892878928889289892908929189292892938929489295892968929789298892998930089301893028930389304893058930689307893088930989310893118931289313893148931589316893178931889319893208932189322893238932489325893268932789328893298933089331893328933389334893358933689337893388933989340893418934289343893448934589346893478934889349893508935189352893538935489355893568935789358893598936089361893628936389364893658936689367893688936989370893718937289373893748937589376893778937889379893808938189382893838938489385893868938789388893898939089391893928939389394893958939689397893988939989400894018940289403894048940589406894078940889409894108941189412894138941489415894168941789418894198942089421894228942389424894258942689427894288942989430894318943289433894348943589436894378943889439894408944189442894438944489445894468944789448894498945089451894528945389454894558945689457894588945989460894618946289463894648946589466894678946889469894708947189472894738947489475894768947789478894798948089481894828948389484894858948689487894888948989490894918949289493894948949589496894978949889499895008950189502895038950489505895068950789508895098951089511895128951389514895158951689517895188951989520895218952289523895248952589526895278952889529895308953189532895338953489535895368953789538895398954089541895428954389544895458954689547895488954989550895518955289553895548955589556895578955889559895608956189562895638956489565895668956789568895698957089571895728957389574895758957689577895788957989580895818958289583895848958589586895878958889589895908959189592895938959489595895968959789598895998960089601896028960389604896058960689607896088960989610896118961289613896148961589616896178961889619896208962189622896238962489625896268962789628896298963089631896328963389634896358963689637896388963989640896418964289643896448964589646896478964889649896508965189652896538965489655896568965789658896598966089661896628966389664896658966689667896688966989670896718967289673896748967589676896778967889679896808968189682896838968489685896868968789688896898969089691896928969389694896958969689697896988969989700897018970289703897048970589706897078970889709897108971189712897138971489715897168971789718897198972089721897228972389724897258972689727897288972989730897318973289733897348973589736897378973889739897408974189742897438974489745897468974789748897498975089751897528975389754897558975689757897588975989760897618976289763897648976589766897678976889769897708977189772897738977489775897768977789778897798978089781897828978389784897858978689787897888978989790897918979289793897948979589796897978979889799898008980189802898038980489805898068980789808898098981089811898128981389814898158981689817898188981989820898218982289823898248982589826898278982889829898308983189832898338983489835898368983789838898398984089841898428984389844898458984689847898488984989850898518985289853898548985589856898578985889859898608986189862898638986489865898668986789868898698987089871898728987389874898758987689877898788987989880898818988289883898848988589886898878988889889898908989189892898938989489895898968989789898898998990089901899028990389904899058990689907899088990989910899118991289913899148991589916899178991889919899208992189922899238992489925899268992789928899298993089931899328993389934899358993689937899388993989940899418994289943899448994589946899478994889949899508995189952899538995489955899568995789958899598996089961899628996389964899658996689967899688996989970899718997289973899748997589976899778997889979899808998189982899838998489985899868998789988899898999089991899928999389994899958999689997899988999990000900019000290003900049000590006900079000890009900109001190012900139001490015900169001790018900199002090021900229002390024900259002690027900289002990030900319003290033900349003590036900379003890039900409004190042900439004490045900469004790048900499005090051900529005390054900559005690057900589005990060900619006290063900649006590066900679006890069900709007190072900739007490075900769007790078900799008090081900829008390084900859008690087900889008990090900919009290093900949009590096900979009890099901009010190102901039010490105901069010790108901099011090111901129011390114901159011690117901189011990120901219012290123901249012590126901279012890129901309013190132901339013490135901369013790138901399014090141901429014390144901459014690147901489014990150901519015290153901549015590156901579015890159901609016190162901639016490165901669016790168901699017090171901729017390174901759017690177901789017990180901819018290183901849018590186901879018890189901909019190192901939019490195901969019790198901999020090201902029020390204902059020690207902089020990210902119021290213902149021590216902179021890219902209022190222902239022490225902269022790228902299023090231902329023390234902359023690237902389023990240902419024290243902449024590246902479024890249902509025190252902539025490255902569025790258902599026090261902629026390264902659026690267902689026990270902719027290273902749027590276902779027890279902809028190282902839028490285902869028790288902899029090291902929029390294902959029690297902989029990300903019030290303903049030590306903079030890309903109031190312903139031490315903169031790318903199032090321903229032390324903259032690327903289032990330903319033290333903349033590336903379033890339903409034190342903439034490345903469034790348903499035090351903529035390354903559035690357903589035990360903619036290363903649036590366903679036890369903709037190372903739037490375903769037790378903799038090381903829038390384903859038690387903889038990390903919039290393903949039590396903979039890399904009040190402904039040490405904069040790408904099041090411904129041390414904159041690417904189041990420904219042290423904249042590426904279042890429904309043190432904339043490435904369043790438904399044090441904429044390444904459044690447904489044990450904519045290453904549045590456904579045890459904609046190462904639046490465904669046790468904699047090471904729047390474904759047690477904789047990480904819048290483904849048590486904879048890489904909049190492904939049490495904969049790498904999050090501905029050390504905059050690507905089050990510905119051290513905149051590516905179051890519905209052190522905239052490525905269052790528905299053090531905329053390534905359053690537905389053990540905419054290543905449054590546905479054890549905509055190552905539055490555905569055790558905599056090561905629056390564905659056690567905689056990570905719057290573905749057590576905779057890579905809058190582905839058490585905869058790588905899059090591905929059390594905959059690597905989059990600906019060290603906049060590606906079060890609906109061190612906139061490615906169061790618906199062090621906229062390624906259062690627906289062990630906319063290633906349063590636906379063890639906409064190642906439064490645906469064790648906499065090651906529065390654906559065690657906589065990660906619066290663906649066590666906679066890669906709067190672906739067490675906769067790678906799068090681906829068390684906859068690687906889068990690906919069290693906949069590696906979069890699907009070190702907039070490705907069070790708907099071090711907129071390714907159071690717907189071990720907219072290723907249072590726907279072890729907309073190732907339073490735907369073790738907399074090741907429074390744907459074690747907489074990750907519075290753907549075590756907579075890759907609076190762907639076490765907669076790768907699077090771907729077390774907759077690777907789077990780907819078290783907849078590786907879078890789907909079190792907939079490795907969079790798907999080090801908029080390804908059080690807908089080990810908119081290813908149081590816908179081890819908209082190822908239082490825908269082790828908299083090831908329083390834908359083690837908389083990840908419084290843908449084590846908479084890849908509085190852908539085490855908569085790858908599086090861908629086390864908659086690867908689086990870908719087290873908749087590876908779087890879908809088190882908839088490885908869088790888908899089090891908929089390894908959089690897908989089990900909019090290903909049090590906909079090890909909109091190912909139091490915909169091790918909199092090921909229092390924909259092690927909289092990930909319093290933909349093590936909379093890939909409094190942909439094490945909469094790948909499095090951909529095390954909559095690957909589095990960909619096290963909649096590966909679096890969909709097190972909739097490975909769097790978909799098090981909829098390984909859098690987909889098990990909919099290993909949099590996909979099890999910009100191002910039100491005910069100791008910099101091011910129101391014910159101691017910189101991020910219102291023910249102591026910279102891029910309103191032910339103491035910369103791038910399104091041910429104391044910459104691047910489104991050910519105291053910549105591056910579105891059910609106191062910639106491065910669106791068910699107091071910729107391074910759107691077910789107991080910819108291083910849108591086910879108891089910909109191092910939109491095910969109791098910999110091101911029110391104911059110691107911089110991110911119111291113911149111591116911179111891119911209112191122911239112491125911269112791128911299113091131911329113391134911359113691137911389113991140911419114291143911449114591146911479114891149911509115191152911539115491155911569115791158911599116091161911629116391164911659116691167911689116991170911719117291173911749117591176911779117891179911809118191182911839118491185911869118791188911899119091191911929119391194911959119691197911989119991200912019120291203912049120591206912079120891209912109121191212912139121491215912169121791218912199122091221912229122391224912259122691227912289122991230912319123291233912349123591236912379123891239912409124191242912439124491245912469124791248912499125091251912529125391254912559125691257912589125991260912619126291263912649126591266912679126891269912709127191272912739127491275912769127791278912799128091281912829128391284912859128691287912889128991290912919129291293912949129591296912979129891299913009130191302913039130491305913069130791308913099131091311913129131391314913159131691317913189131991320913219132291323913249132591326913279132891329913309133191332913339133491335913369133791338913399134091341913429134391344913459134691347913489134991350913519135291353913549135591356913579135891359913609136191362913639136491365913669136791368913699137091371913729137391374913759137691377913789137991380913819138291383913849138591386913879138891389913909139191392913939139491395913969139791398913999140091401914029140391404914059140691407914089140991410914119141291413914149141591416914179141891419914209142191422914239142491425914269142791428914299143091431914329143391434914359143691437914389143991440914419144291443914449144591446914479144891449914509145191452914539145491455914569145791458914599146091461914629146391464914659146691467914689146991470914719147291473914749147591476914779147891479914809148191482914839148491485914869148791488914899149091491914929149391494914959149691497914989149991500915019150291503915049150591506915079150891509915109151191512915139151491515915169151791518915199152091521915229152391524915259152691527915289152991530915319153291533915349153591536915379153891539915409154191542915439154491545915469154791548915499155091551915529155391554915559155691557915589155991560915619156291563915649156591566915679156891569915709157191572915739157491575915769157791578915799158091581915829158391584915859158691587915889158991590915919159291593915949159591596915979159891599916009160191602916039160491605916069160791608916099161091611916129161391614916159161691617916189161991620916219162291623916249162591626916279162891629916309163191632916339163491635916369163791638916399164091641916429164391644916459164691647916489164991650916519165291653916549165591656916579165891659916609166191662916639166491665916669166791668916699167091671916729167391674916759167691677916789167991680916819168291683916849168591686916879168891689916909169191692916939169491695916969169791698916999170091701917029170391704917059170691707917089170991710917119171291713917149171591716917179171891719917209172191722917239172491725917269172791728917299173091731917329173391734917359173691737917389173991740917419174291743917449174591746917479174891749917509175191752917539175491755917569175791758917599176091761917629176391764917659176691767917689176991770917719177291773917749177591776917779177891779917809178191782917839178491785917869178791788917899179091791917929179391794917959179691797917989179991800918019180291803918049180591806918079180891809918109181191812918139181491815918169181791818918199182091821918229182391824918259182691827918289182991830918319183291833918349183591836918379183891839918409184191842918439184491845918469184791848918499185091851918529185391854918559185691857918589185991860918619186291863918649186591866918679186891869918709187191872918739187491875918769187791878918799188091881918829188391884918859188691887918889188991890918919189291893918949189591896918979189891899919009190191902919039190491905919069190791908919099191091911919129191391914919159191691917919189191991920919219192291923919249192591926919279192891929919309193191932919339193491935919369193791938919399194091941919429194391944919459194691947919489194991950919519195291953919549195591956919579195891959919609196191962919639196491965919669196791968919699197091971919729197391974919759197691977919789197991980919819198291983919849198591986919879198891989919909199191992919939199491995919969199791998919999200092001920029200392004920059200692007920089200992010920119201292013920149201592016920179201892019920209202192022920239202492025920269202792028920299203092031920329203392034920359203692037920389203992040920419204292043920449204592046920479204892049920509205192052920539205492055920569205792058920599206092061920629206392064920659206692067920689206992070920719207292073920749207592076920779207892079920809208192082920839208492085920869208792088920899209092091920929209392094920959209692097920989209992100921019210292103921049210592106921079210892109921109211192112921139211492115921169211792118921199212092121921229212392124921259212692127921289212992130921319213292133921349213592136921379213892139921409214192142921439214492145921469214792148921499215092151921529215392154921559215692157921589215992160921619216292163921649216592166921679216892169921709217192172921739217492175921769217792178921799218092181921829218392184921859218692187921889218992190921919219292193921949219592196921979219892199922009220192202922039220492205922069220792208922099221092211922129221392214922159221692217922189221992220922219222292223922249222592226922279222892229922309223192232922339223492235922369223792238922399224092241922429224392244922459224692247922489224992250922519225292253922549225592256922579225892259922609226192262922639226492265922669226792268922699227092271922729227392274922759227692277922789227992280922819228292283922849228592286922879228892289922909229192292922939229492295922969229792298922999230092301923029230392304923059230692307923089230992310923119231292313923149231592316923179231892319923209232192322923239232492325923269232792328923299233092331923329233392334923359233692337923389233992340923419234292343923449234592346923479234892349923509235192352923539235492355923569235792358923599236092361923629236392364923659236692367923689236992370923719237292373923749237592376923779237892379923809238192382923839238492385923869238792388923899239092391923929239392394923959239692397923989239992400924019240292403924049240592406924079240892409924109241192412924139241492415924169241792418924199242092421924229242392424924259242692427924289242992430924319243292433924349243592436924379243892439924409244192442924439244492445924469244792448924499245092451924529245392454924559245692457924589245992460924619246292463924649246592466924679246892469924709247192472924739247492475924769247792478924799248092481924829248392484924859248692487924889248992490924919249292493924949249592496924979249892499925009250192502925039250492505925069250792508925099251092511925129251392514925159251692517925189251992520925219252292523925249252592526925279252892529925309253192532925339253492535925369253792538925399254092541925429254392544925459254692547925489254992550925519255292553925549255592556925579255892559925609256192562925639256492565925669256792568925699257092571925729257392574925759257692577925789257992580925819258292583925849258592586925879258892589925909259192592925939259492595925969259792598925999260092601926029260392604926059260692607926089260992610926119261292613926149261592616926179261892619926209262192622926239262492625926269262792628926299263092631926329263392634926359263692637926389263992640926419264292643926449264592646926479264892649926509265192652926539265492655926569265792658926599266092661926629266392664926659266692667926689266992670926719267292673926749267592676926779267892679926809268192682926839268492685926869268792688926899269092691926929269392694926959269692697926989269992700927019270292703927049270592706927079270892709927109271192712927139271492715927169271792718927199272092721927229272392724927259272692727927289272992730927319273292733927349273592736927379273892739927409274192742927439274492745927469274792748927499275092751927529275392754927559275692757927589275992760927619276292763927649276592766927679276892769927709277192772927739277492775927769277792778927799278092781927829278392784927859278692787927889278992790927919279292793927949279592796927979279892799928009280192802928039280492805928069280792808928099281092811928129281392814928159281692817928189281992820928219282292823928249282592826928279282892829928309283192832928339283492835928369283792838928399284092841928429284392844928459284692847928489284992850928519285292853928549285592856928579285892859928609286192862928639286492865928669286792868928699287092871928729287392874928759287692877928789287992880928819288292883928849288592886928879288892889928909289192892928939289492895928969289792898928999290092901929029290392904929059290692907929089290992910929119291292913929149291592916929179291892919929209292192922929239292492925929269292792928929299293092931929329293392934929359293692937929389293992940929419294292943929449294592946929479294892949929509295192952929539295492955929569295792958929599296092961929629296392964929659296692967929689296992970929719297292973929749297592976929779297892979929809298192982929839298492985929869298792988929899299092991929929299392994929959299692997929989299993000930019300293003930049300593006930079300893009930109301193012930139301493015930169301793018930199302093021930229302393024930259302693027930289302993030930319303293033930349303593036930379303893039930409304193042930439304493045930469304793048930499305093051930529305393054930559305693057930589305993060930619306293063930649306593066930679306893069930709307193072930739307493075930769307793078930799308093081930829308393084930859308693087930889308993090930919309293093930949309593096930979309893099931009310193102931039310493105931069310793108931099311093111931129311393114931159311693117931189311993120931219312293123931249312593126931279312893129931309313193132931339313493135931369313793138931399314093141931429314393144931459314693147931489314993150931519315293153931549315593156931579315893159931609316193162931639316493165931669316793168931699317093171931729317393174931759317693177931789317993180931819318293183931849318593186931879318893189931909319193192931939319493195931969319793198931999320093201932029320393204932059320693207932089320993210932119321293213932149321593216932179321893219932209322193222932239322493225932269322793228932299323093231932329323393234932359323693237932389323993240932419324293243932449324593246932479324893249932509325193252932539325493255932569325793258932599326093261932629326393264932659326693267932689326993270932719327293273932749327593276932779327893279932809328193282932839328493285932869328793288932899329093291932929329393294932959329693297932989329993300933019330293303933049330593306933079330893309933109331193312933139331493315933169331793318933199332093321933229332393324933259332693327933289332993330933319333293333933349333593336933379333893339933409334193342933439334493345933469334793348933499335093351933529335393354933559335693357933589335993360933619336293363933649336593366933679336893369933709337193372933739337493375933769337793378933799338093381933829338393384933859338693387933889338993390933919339293393933949339593396933979339893399934009340193402934039340493405934069340793408934099341093411934129341393414934159341693417934189341993420934219342293423934249342593426934279342893429934309343193432934339343493435934369343793438934399344093441934429344393444934459344693447934489344993450934519345293453934549345593456934579345893459934609346193462934639346493465934669346793468934699347093471934729347393474934759347693477934789347993480934819348293483934849348593486934879348893489934909349193492934939349493495934969349793498934999350093501935029350393504935059350693507935089350993510935119351293513935149351593516935179351893519935209352193522935239352493525935269352793528935299353093531935329353393534935359353693537935389353993540935419354293543935449354593546935479354893549935509355193552935539355493555935569355793558935599356093561935629356393564935659356693567935689356993570935719357293573935749357593576935779357893579935809358193582935839358493585935869358793588935899359093591935929359393594935959359693597935989359993600936019360293603936049360593606936079360893609936109361193612936139361493615936169361793618936199362093621936229362393624936259362693627936289362993630936319363293633936349363593636936379363893639936409364193642936439364493645936469364793648936499365093651936529365393654936559365693657936589365993660936619366293663936649366593666936679366893669936709367193672936739367493675936769367793678936799368093681936829368393684936859368693687936889368993690936919369293693936949369593696936979369893699937009370193702937039370493705937069370793708937099371093711937129371393714937159371693717937189371993720937219372293723937249372593726937279372893729937309373193732937339373493735937369373793738937399374093741937429374393744937459374693747937489374993750937519375293753937549375593756937579375893759937609376193762937639376493765937669376793768937699377093771937729377393774937759377693777937789377993780937819378293783937849378593786937879378893789937909379193792937939379493795937969379793798937999380093801938029380393804938059380693807938089380993810938119381293813938149381593816938179381893819938209382193822938239382493825938269382793828938299383093831938329383393834938359383693837938389383993840938419384293843938449384593846938479384893849938509385193852938539385493855938569385793858938599386093861938629386393864938659386693867938689386993870938719387293873938749387593876938779387893879938809388193882938839388493885938869388793888938899389093891938929389393894938959389693897938989389993900939019390293903939049390593906939079390893909939109391193912939139391493915939169391793918939199392093921939229392393924939259392693927939289392993930939319393293933939349393593936939379393893939939409394193942939439394493945939469394793948939499395093951939529395393954939559395693957939589395993960939619396293963939649396593966939679396893969939709397193972939739397493975939769397793978939799398093981939829398393984939859398693987939889398993990939919399293993939949399593996939979399893999940009400194002940039400494005940069400794008940099401094011940129401394014940159401694017940189401994020940219402294023940249402594026940279402894029940309403194032940339403494035940369403794038940399404094041940429404394044940459404694047940489404994050940519405294053940549405594056940579405894059940609406194062940639406494065940669406794068940699407094071940729407394074940759407694077940789407994080940819408294083940849408594086940879408894089940909409194092940939409494095940969409794098940999410094101941029410394104941059410694107941089410994110941119411294113941149411594116941179411894119941209412194122941239412494125941269412794128941299413094131941329413394134941359413694137941389413994140941419414294143941449414594146941479414894149941509415194152941539415494155941569415794158941599416094161941629416394164941659416694167941689416994170941719417294173941749417594176941779417894179941809418194182941839418494185941869418794188941899419094191941929419394194941959419694197941989419994200942019420294203942049420594206942079420894209942109421194212942139421494215942169421794218942199422094221942229422394224942259422694227942289422994230942319423294233942349423594236942379423894239942409424194242942439424494245942469424794248942499425094251942529425394254942559425694257942589425994260942619426294263942649426594266942679426894269942709427194272942739427494275942769427794278942799428094281942829428394284942859428694287942889428994290942919429294293942949429594296942979429894299943009430194302943039430494305943069430794308943099431094311943129431394314943159431694317943189431994320943219432294323943249432594326943279432894329943309433194332943339433494335943369433794338943399434094341943429434394344943459434694347943489434994350943519435294353943549435594356943579435894359943609436194362943639436494365943669436794368943699437094371943729437394374943759437694377943789437994380943819438294383943849438594386943879438894389943909439194392943939439494395943969439794398943999440094401944029440394404944059440694407944089440994410944119441294413944149441594416944179441894419944209442194422944239442494425944269442794428944299443094431944329443394434944359443694437944389443994440944419444294443944449444594446944479444894449944509445194452944539445494455944569445794458944599446094461944629446394464944659446694467944689446994470944719447294473944749447594476944779447894479944809448194482944839448494485944869448794488944899449094491944929449394494944959449694497944989449994500945019450294503945049450594506945079450894509945109451194512945139451494515945169451794518945199452094521945229452394524945259452694527945289452994530945319453294533945349453594536945379453894539945409454194542945439454494545945469454794548945499455094551945529455394554945559455694557945589455994560945619456294563945649456594566945679456894569945709457194572945739457494575945769457794578945799458094581945829458394584945859458694587945889458994590945919459294593945949459594596945979459894599946009460194602946039460494605946069460794608946099461094611946129461394614946159461694617946189461994620946219462294623946249462594626946279462894629946309463194632946339463494635946369463794638946399464094641946429464394644946459464694647946489464994650946519465294653946549465594656946579465894659946609466194662946639466494665946669466794668946699467094671946729467394674946759467694677946789467994680946819468294683946849468594686946879468894689946909469194692946939469494695946969469794698946999470094701947029470394704947059470694707947089470994710947119471294713947149471594716947179471894719947209472194722947239472494725947269472794728947299473094731947329473394734947359473694737947389473994740947419474294743947449474594746947479474894749947509475194752947539475494755947569475794758947599476094761947629476394764947659476694767947689476994770947719477294773947749477594776947779477894779947809478194782947839478494785947869478794788947899479094791947929479394794947959479694797947989479994800948019480294803948049480594806948079480894809948109481194812948139481494815948169481794818948199482094821948229482394824948259482694827948289482994830948319483294833948349483594836948379483894839948409484194842948439484494845948469484794848948499485094851948529485394854948559485694857948589485994860948619486294863948649486594866948679486894869948709487194872948739487494875948769487794878948799488094881948829488394884948859488694887948889488994890948919489294893948949489594896948979489894899949009490194902949039490494905949069490794908949099491094911949129491394914949159491694917949189491994920949219492294923949249492594926949279492894929949309493194932949339493494935949369493794938949399494094941949429494394944949459494694947949489494994950949519495294953949549495594956949579495894959949609496194962949639496494965949669496794968949699497094971949729497394974949759497694977949789497994980949819498294983949849498594986949879498894989949909499194992949939499494995949969499794998949999500095001950029500395004950059500695007950089500995010950119501295013950149501595016950179501895019950209502195022950239502495025950269502795028950299503095031950329503395034950359503695037950389503995040950419504295043950449504595046950479504895049950509505195052950539505495055950569505795058950599506095061950629506395064950659506695067950689506995070950719507295073950749507595076950779507895079950809508195082950839508495085950869508795088950899509095091950929509395094950959509695097950989509995100951019510295103951049510595106951079510895109951109511195112951139511495115951169511795118951199512095121951229512395124951259512695127951289512995130951319513295133951349513595136951379513895139951409514195142951439514495145951469514795148951499515095151951529515395154951559515695157951589515995160951619516295163951649516595166951679516895169951709517195172951739517495175951769517795178951799518095181951829518395184951859518695187951889518995190951919519295193951949519595196951979519895199952009520195202952039520495205952069520795208952099521095211952129521395214952159521695217952189521995220952219522295223952249522595226952279522895229952309523195232952339523495235952369523795238952399524095241952429524395244952459524695247952489524995250952519525295253952549525595256952579525895259952609526195262952639526495265952669526795268952699527095271952729527395274952759527695277952789527995280952819528295283952849528595286952879528895289952909529195292952939529495295952969529795298952999530095301953029530395304953059530695307953089530995310953119531295313953149531595316953179531895319953209532195322953239532495325953269532795328953299533095331953329533395334953359533695337953389533995340953419534295343953449534595346953479534895349953509535195352953539535495355953569535795358953599536095361953629536395364953659536695367953689536995370953719537295373953749537595376953779537895379953809538195382953839538495385953869538795388953899539095391953929539395394953959539695397953989539995400954019540295403954049540595406954079540895409954109541195412954139541495415954169541795418954199542095421954229542395424954259542695427954289542995430954319543295433954349543595436954379543895439954409544195442954439544495445954469544795448954499545095451954529545395454954559545695457954589545995460954619546295463954649546595466954679546895469954709547195472954739547495475954769547795478954799548095481954829548395484954859548695487954889548995490954919549295493954949549595496954979549895499955009550195502955039550495505955069550795508955099551095511955129551395514955159551695517955189551995520955219552295523955249552595526955279552895529955309553195532955339553495535955369553795538955399554095541955429554395544955459554695547955489554995550955519555295553955549555595556955579555895559955609556195562955639556495565955669556795568955699557095571955729557395574955759557695577955789557995580955819558295583955849558595586955879558895589955909559195592955939559495595955969559795598955999560095601956029560395604956059560695607956089560995610956119561295613956149561595616956179561895619956209562195622956239562495625956269562795628956299563095631956329563395634956359563695637956389563995640956419564295643956449564595646956479564895649956509565195652956539565495655956569565795658956599566095661956629566395664956659566695667956689566995670956719567295673956749567595676956779567895679956809568195682956839568495685956869568795688956899569095691956929569395694956959569695697956989569995700957019570295703957049570595706957079570895709957109571195712957139571495715957169571795718957199572095721957229572395724957259572695727957289572995730957319573295733957349573595736957379573895739957409574195742957439574495745957469574795748957499575095751957529575395754957559575695757957589575995760957619576295763957649576595766957679576895769957709577195772957739577495775957769577795778957799578095781957829578395784957859578695787957889578995790957919579295793957949579595796957979579895799958009580195802958039580495805958069580795808958099581095811958129581395814958159581695817958189581995820958219582295823958249582595826958279582895829958309583195832958339583495835958369583795838958399584095841958429584395844958459584695847958489584995850958519585295853958549585595856958579585895859958609586195862958639586495865958669586795868958699587095871958729587395874958759587695877958789587995880958819588295883958849588595886958879588895889958909589195892958939589495895958969589795898958999590095901959029590395904959059590695907959089590995910959119591295913959149591595916959179591895919959209592195922959239592495925959269592795928959299593095931959329593395934959359593695937959389593995940959419594295943959449594595946959479594895949959509595195952959539595495955959569595795958959599596095961959629596395964959659596695967959689596995970959719597295973959749597595976959779597895979959809598195982959839598495985959869598795988959899599095991959929599395994959959599695997959989599996000960019600296003960049600596006960079600896009960109601196012960139601496015960169601796018960199602096021960229602396024960259602696027960289602996030960319603296033960349603596036960379603896039960409604196042960439604496045960469604796048960499605096051960529605396054960559605696057960589605996060960619606296063960649606596066960679606896069960709607196072960739607496075960769607796078960799608096081960829608396084960859608696087960889608996090960919609296093960949609596096960979609896099961009610196102961039610496105961069610796108961099611096111961129611396114961159611696117961189611996120961219612296123961249612596126961279612896129961309613196132961339613496135961369613796138961399614096141961429614396144961459614696147961489614996150961519615296153961549615596156961579615896159961609616196162961639616496165961669616796168961699617096171961729617396174961759617696177961789617996180961819618296183961849618596186961879618896189961909619196192961939619496195961969619796198961999620096201962029620396204962059620696207962089620996210962119621296213962149621596216962179621896219962209622196222962239622496225962269622796228962299623096231962329623396234962359623696237962389623996240962419624296243962449624596246962479624896249962509625196252962539625496255962569625796258962599626096261962629626396264962659626696267962689626996270962719627296273962749627596276962779627896279962809628196282962839628496285962869628796288962899629096291962929629396294962959629696297962989629996300963019630296303963049630596306963079630896309963109631196312963139631496315963169631796318963199632096321963229632396324963259632696327963289632996330963319633296333963349633596336963379633896339963409634196342963439634496345963469634796348963499635096351963529635396354963559635696357963589635996360963619636296363963649636596366963679636896369963709637196372963739637496375963769637796378963799638096381963829638396384963859638696387963889638996390963919639296393963949639596396963979639896399964009640196402964039640496405964069640796408964099641096411964129641396414964159641696417964189641996420964219642296423964249642596426964279642896429964309643196432964339643496435964369643796438964399644096441964429644396444964459644696447964489644996450964519645296453964549645596456964579645896459964609646196462964639646496465964669646796468964699647096471964729647396474964759647696477964789647996480964819648296483964849648596486964879648896489964909649196492964939649496495964969649796498964999650096501965029650396504965059650696507965089650996510965119651296513965149651596516965179651896519965209652196522965239652496525965269652796528965299653096531965329653396534965359653696537965389653996540965419654296543965449654596546965479654896549965509655196552965539655496555965569655796558965599656096561965629656396564965659656696567965689656996570965719657296573965749657596576965779657896579965809658196582965839658496585965869658796588965899659096591965929659396594965959659696597965989659996600966019660296603966049660596606966079660896609966109661196612966139661496615966169661796618966199662096621966229662396624966259662696627966289662996630966319663296633966349663596636966379663896639966409664196642966439664496645966469664796648966499665096651966529665396654966559665696657966589665996660966619666296663966649666596666966679666896669966709667196672966739667496675966769667796678966799668096681966829668396684966859668696687966889668996690966919669296693966949669596696966979669896699967009670196702967039670496705967069670796708967099671096711967129671396714967159671696717967189671996720967219672296723967249672596726967279672896729967309673196732967339673496735967369673796738967399674096741967429674396744967459674696747967489674996750967519675296753967549675596756967579675896759967609676196762967639676496765967669676796768967699677096771967729677396774967759677696777967789677996780967819678296783967849678596786967879678896789967909679196792967939679496795967969679796798967999680096801968029680396804968059680696807968089680996810968119681296813968149681596816968179681896819968209682196822968239682496825968269682796828968299683096831968329683396834968359683696837968389683996840968419684296843968449684596846968479684896849968509685196852968539685496855968569685796858968599686096861968629686396864968659686696867968689686996870968719687296873968749687596876968779687896879968809688196882968839688496885968869688796888968899689096891968929689396894968959689696897968989689996900969019690296903969049690596906969079690896909969109691196912969139691496915969169691796918969199692096921969229692396924969259692696927969289692996930969319693296933969349693596936969379693896939969409694196942969439694496945969469694796948969499695096951969529695396954969559695696957969589695996960969619696296963969649696596966969679696896969969709697196972969739697496975969769697796978969799698096981969829698396984969859698696987969889698996990969919699296993969949699596996969979699896999970009700197002970039700497005970069700797008970099701097011970129701397014970159701697017970189701997020970219702297023970249702597026970279702897029970309703197032970339703497035970369703797038970399704097041970429704397044970459704697047970489704997050970519705297053970549705597056970579705897059970609706197062970639706497065970669706797068970699707097071970729707397074970759707697077970789707997080970819708297083970849708597086970879708897089970909709197092970939709497095970969709797098970999710097101971029710397104971059710697107971089710997110971119711297113971149711597116971179711897119971209712197122971239712497125971269712797128971299713097131971329713397134971359713697137971389713997140971419714297143971449714597146971479714897149971509715197152971539715497155971569715797158971599716097161971629716397164971659716697167971689716997170971719717297173971749717597176971779717897179971809718197182971839718497185971869718797188971899719097191971929719397194971959719697197971989719997200972019720297203972049720597206972079720897209972109721197212972139721497215972169721797218972199722097221972229722397224972259722697227972289722997230972319723297233972349723597236972379723897239972409724197242972439724497245972469724797248972499725097251972529725397254972559725697257972589725997260972619726297263972649726597266972679726897269972709727197272972739727497275972769727797278972799728097281972829728397284972859728697287972889728997290972919729297293972949729597296972979729897299973009730197302973039730497305973069730797308973099731097311973129731397314973159731697317973189731997320973219732297323973249732597326973279732897329973309733197332973339733497335973369733797338973399734097341973429734397344973459734697347973489734997350973519735297353973549735597356973579735897359973609736197362973639736497365973669736797368973699737097371973729737397374973759737697377973789737997380973819738297383973849738597386973879738897389973909739197392973939739497395973969739797398973999740097401974029740397404974059740697407974089740997410974119741297413974149741597416974179741897419974209742197422974239742497425974269742797428974299743097431974329743397434974359743697437974389743997440974419744297443974449744597446974479744897449974509745197452974539745497455974569745797458974599746097461974629746397464974659746697467974689746997470974719747297473974749747597476974779747897479974809748197482974839748497485974869748797488974899749097491974929749397494974959749697497974989749997500975019750297503975049750597506975079750897509975109751197512975139751497515975169751797518975199752097521975229752397524975259752697527975289752997530975319753297533975349753597536975379753897539975409754197542975439754497545975469754797548975499755097551975529755397554975559755697557975589755997560975619756297563975649756597566975679756897569975709757197572975739757497575975769757797578975799758097581975829758397584975859758697587975889758997590975919759297593975949759597596975979759897599976009760197602976039760497605976069760797608976099761097611976129761397614976159761697617976189761997620976219762297623976249762597626976279762897629976309763197632976339763497635976369763797638976399764097641976429764397644976459764697647976489764997650976519765297653976549765597656976579765897659976609766197662976639766497665976669766797668976699767097671976729767397674976759767697677976789767997680976819768297683976849768597686976879768897689976909769197692976939769497695976969769797698976999770097701977029770397704977059770697707977089770997710977119771297713977149771597716977179771897719977209772197722977239772497725977269772797728977299773097731977329773397734977359773697737977389773997740977419774297743977449774597746977479774897749977509775197752977539775497755977569775797758977599776097761977629776397764977659776697767977689776997770977719777297773977749777597776977779777897779977809778197782977839778497785977869778797788977899779097791977929779397794977959779697797977989779997800978019780297803978049780597806978079780897809978109781197812978139781497815978169781797818978199782097821978229782397824978259782697827978289782997830978319783297833978349783597836978379783897839978409784197842978439784497845978469784797848978499785097851978529785397854978559785697857978589785997860978619786297863978649786597866978679786897869978709787197872978739787497875978769787797878978799788097881978829788397884978859788697887978889788997890978919789297893978949789597896978979789897899979009790197902979039790497905979069790797908979099791097911979129791397914979159791697917979189791997920979219792297923979249792597926979279792897929979309793197932979339793497935979369793797938979399794097941979429794397944979459794697947979489794997950979519795297953979549795597956979579795897959979609796197962979639796497965979669796797968979699797097971979729797397974979759797697977979789797997980979819798297983979849798597986979879798897989979909799197992979939799497995979969799797998979999800098001980029800398004980059800698007980089800998010980119801298013980149801598016980179801898019980209802198022980239802498025980269802798028980299803098031980329803398034980359803698037980389803998040980419804298043980449804598046980479804898049980509805198052980539805498055980569805798058980599806098061980629806398064980659806698067980689806998070980719807298073980749807598076980779807898079980809808198082980839808498085980869808798088980899809098091980929809398094980959809698097980989809998100981019810298103981049810598106981079810898109981109811198112981139811498115981169811798118981199812098121981229812398124981259812698127981289812998130981319813298133981349813598136981379813898139981409814198142981439814498145981469814798148981499815098151981529815398154981559815698157981589815998160981619816298163981649816598166981679816898169981709817198172981739817498175981769817798178981799818098181981829818398184981859818698187981889818998190981919819298193981949819598196981979819898199982009820198202982039820498205982069820798208982099821098211982129821398214982159821698217982189821998220982219822298223982249822598226982279822898229982309823198232982339823498235982369823798238982399824098241982429824398244982459824698247982489824998250982519825298253982549825598256982579825898259982609826198262982639826498265982669826798268982699827098271982729827398274982759827698277982789827998280982819828298283982849828598286982879828898289982909829198292982939829498295982969829798298982999830098301983029830398304983059830698307983089830998310983119831298313983149831598316983179831898319983209832198322983239832498325983269832798328983299833098331983329833398334983359833698337983389833998340983419834298343983449834598346983479834898349983509835198352983539835498355983569835798358983599836098361983629836398364983659836698367983689836998370983719837298373983749837598376983779837898379983809838198382983839838498385983869838798388983899839098391983929839398394983959839698397983989839998400984019840298403984049840598406984079840898409984109841198412984139841498415984169841798418984199842098421984229842398424984259842698427984289842998430984319843298433984349843598436984379843898439984409844198442984439844498445984469844798448984499845098451984529845398454984559845698457984589845998460984619846298463984649846598466984679846898469984709847198472984739847498475984769847798478984799848098481984829848398484984859848698487984889848998490984919849298493984949849598496984979849898499985009850198502985039850498505985069850798508985099851098511985129851398514985159851698517985189851998520985219852298523985249852598526985279852898529985309853198532985339853498535985369853798538985399854098541985429854398544985459854698547985489854998550985519855298553985549855598556985579855898559985609856198562985639856498565985669856798568985699857098571985729857398574985759857698577985789857998580985819858298583985849858598586985879858898589985909859198592985939859498595985969859798598985999860098601986029860398604986059860698607986089860998610986119861298613986149861598616986179861898619986209862198622986239862498625986269862798628986299863098631986329863398634986359863698637986389863998640986419864298643986449864598646986479864898649986509865198652986539865498655986569865798658986599866098661986629866398664986659866698667986689866998670986719867298673986749867598676986779867898679986809868198682986839868498685986869868798688986899869098691986929869398694986959869698697986989869998700987019870298703987049870598706987079870898709987109871198712987139871498715987169871798718987199872098721987229872398724987259872698727987289872998730987319873298733987349873598736987379873898739987409874198742987439874498745987469874798748987499875098751987529875398754987559875698757987589875998760987619876298763987649876598766987679876898769987709877198772987739877498775987769877798778987799878098781987829878398784987859878698787987889878998790987919879298793987949879598796987979879898799988009880198802988039880498805988069880798808988099881098811988129881398814988159881698817988189881998820988219882298823988249882598826988279882898829988309883198832988339883498835988369883798838988399884098841988429884398844988459884698847988489884998850988519885298853988549885598856988579885898859988609886198862988639886498865988669886798868988699887098871988729887398874988759887698877988789887998880988819888298883988849888598886988879888898889988909889198892988939889498895988969889798898988999890098901989029890398904989059890698907989089890998910989119891298913989149891598916989179891898919989209892198922989239892498925989269892798928989299893098931989329893398934989359893698937989389893998940989419894298943989449894598946989479894898949989509895198952989539895498955989569895798958989599896098961989629896398964989659896698967989689896998970989719897298973989749897598976989779897898979989809898198982989839898498985989869898798988989899899098991989929899398994989959899698997989989899999000990019900299003990049900599006990079900899009990109901199012990139901499015990169901799018990199902099021990229902399024990259902699027990289902999030990319903299033990349903599036990379903899039990409904199042990439904499045990469904799048990499905099051990529905399054990559905699057990589905999060990619906299063990649906599066990679906899069990709907199072990739907499075990769907799078990799908099081990829908399084990859908699087990889908999090990919909299093990949909599096990979909899099991009910199102991039910499105991069910799108991099911099111991129911399114991159911699117991189911999120991219912299123991249912599126991279912899129991309913199132991339913499135991369913799138991399914099141991429914399144991459914699147991489914999150991519915299153991549915599156991579915899159991609916199162991639916499165991669916799168991699917099171991729917399174991759917699177991789917999180991819918299183991849918599186991879918899189991909919199192991939919499195991969919799198991999920099201992029920399204992059920699207992089920999210992119921299213992149921599216992179921899219992209922199222992239922499225992269922799228992299923099231992329923399234992359923699237992389923999240992419924299243992449924599246992479924899249992509925199252992539925499255992569925799258992599926099261992629926399264992659926699267992689926999270992719927299273992749927599276992779927899279992809928199282992839928499285992869928799288992899929099291992929929399294992959929699297992989929999300993019930299303993049930599306993079930899309993109931199312993139931499315993169931799318993199932099321993229932399324993259932699327993289932999330993319933299333993349933599336993379933899339993409934199342993439934499345993469934799348993499935099351993529935399354993559935699357993589935999360993619936299363993649936599366993679936899369993709937199372993739937499375993769937799378993799938099381993829938399384993859938699387993889938999390993919939299393993949939599396993979939899399994009940199402994039940499405994069940799408994099941099411994129941399414994159941699417994189941999420994219942299423994249942599426994279942899429994309943199432994339943499435994369943799438994399944099441994429944399444994459944699447994489944999450994519945299453994549945599456994579945899459994609946199462994639946499465994669946799468994699947099471994729947399474994759947699477994789947999480994819948299483994849948599486994879948899489994909949199492994939949499495994969949799498994999950099501995029950399504995059950699507995089950999510995119951299513995149951599516995179951899519995209952199522995239952499525995269952799528995299953099531995329953399534995359953699537995389953999540995419954299543995449954599546995479954899549995509955199552995539955499555995569955799558995599956099561995629956399564995659956699567995689956999570995719957299573995749957599576995779957899579995809958199582995839958499585995869958799588995899959099591995929959399594995959959699597995989959999600996019960299603996049960599606996079960899609996109961199612996139961499615996169961799618996199962099621996229962399624996259962699627996289962999630996319963299633996349963599636996379963899639996409964199642996439964499645996469964799648996499965099651996529965399654996559965699657996589965999660996619966299663996649966599666996679966899669996709967199672996739967499675996769967799678996799968099681996829968399684996859968699687996889968999690996919969299693996949969599696996979969899699997009970199702997039970499705997069970799708997099971099711997129971399714997159971699717997189971999720997219972299723997249972599726997279972899729997309973199732997339973499735997369973799738997399974099741997429974399744997459974699747997489974999750997519975299753997549975599756997579975899759997609976199762997639976499765997669976799768997699977099771997729977399774997759977699777997789977999780997819978299783997849978599786997879978899789997909979199792997939979499795997969979799798997999980099801998029980399804998059980699807998089980999810998119981299813998149981599816998179981899819998209982199822998239982499825998269982799828998299983099831998329983399834998359983699837998389983999840998419984299843998449984599846998479984899849998509985199852998539985499855998569985799858998599986099861998629986399864998659986699867998689986999870998719987299873998749987599876998779987899879998809988199882998839988499885998869988799888998899989099891998929989399894998959989699897998989989999900999019990299903999049990599906999079990899909999109991199912999139991499915999169991799918999199992099921999229992399924999259992699927999289992999930999319993299933999349993599936999379993899939999409994199942999439994499945999469994799948999499995099951999529995399954999559995699957999589995999960999619996299963999649996599966999679996899969999709997199972999739997499975999769997799978999799998099981999829998399984999859998699987999889998999990999919999299993999949999599996999979999899999100000100001100002100003100004100005100006100007100008100009100010100011100012100013100014100015100016100017100018100019100020100021100022100023100024100025100026100027100028100029100030100031100032100033100034100035100036100037100038100039100040100041100042100043100044100045100046100047100048100049100050100051100052100053100054100055100056100057100058100059100060100061100062100063100064100065100066100067100068100069100070100071100072100073100074100075100076100077100078100079100080100081100082100083100084100085100086100087100088100089100090100091100092100093100094100095100096100097100098100099100100100101100102100103100104100105100106100107100108100109100110100111100112100113100114100115100116100117100118100119100120100121100122100123100124100125100126100127100128100129100130100131100132100133100134100135100136100137100138100139100140100141100142100143100144100145100146100147100148100149100150100151100152100153100154100155100156100157100158100159100160100161100162100163100164100165100166100167100168100169100170100171100172100173100174100175100176100177100178100179100180100181100182100183100184100185100186100187100188100189100190100191100192100193100194100195100196100197100198100199100200100201100202100203100204100205100206100207100208100209100210100211100212100213100214100215100216100217100218100219100220100221100222100223100224100225100226100227100228100229100230100231100232100233100234100235100236100237100238100239100240100241100242100243100244100245100246100247100248100249100250100251100252100253100254100255100256100257100258100259100260100261100262100263100264100265100266100267100268100269100270100271100272100273100274100275100276100277100278100279100280100281100282100283100284100285100286100287100288100289100290100291100292100293100294100295100296100297100298100299100300100301100302100303100304100305100306100307100308100309100310100311100312100313100314100315100316100317100318100319100320100321100322100323100324100325100326100327100328100329100330100331100332100333100334100335100336100337100338100339100340100341100342100343100344100345100346100347100348100349100350100351100352100353100354100355100356100357100358100359100360100361100362100363100364100365100366100367100368100369100370100371100372100373100374100375100376100377100378100379100380100381100382100383100384100385100386100387100388100389100390100391100392100393100394100395100396100397100398100399100400100401100402100403100404100405100406100407100408100409100410100411100412100413100414100415100416100417100418100419100420100421100422100423100424100425100426100427100428100429100430100431100432100433100434100435100436100437100438100439100440100441100442100443100444100445100446100447100448100449100450100451100452100453100454100455100456100457100458100459100460100461100462100463100464100465100466100467100468100469100470100471100472100473100474100475100476100477100478100479100480100481100482100483100484100485100486100487100488100489100490100491100492100493100494100495100496100497100498100499100500100501100502100503100504100505100506100507100508100509100510100511100512100513100514100515100516100517100518100519100520100521100522100523100524100525100526100527100528100529100530100531100532100533100534100535100536100537100538100539100540100541100542100543100544100545100546100547100548100549100550100551100552100553100554100555100556100557100558100559100560100561100562100563100564100565100566100567100568100569100570100571100572100573100574100575100576100577100578100579100580100581100582100583100584100585100586100587100588100589100590100591100592100593100594100595100596100597100598100599100600100601100602100603100604100605100606100607100608100609100610100611100612100613100614100615100616100617100618100619100620100621100622100623100624100625100626100627100628100629100630100631100632100633100634100635100636100637100638100639100640100641100642100643100644100645100646100647100648100649100650100651100652100653100654100655100656100657100658100659100660100661100662100663100664100665100666100667100668100669100670100671100672100673100674100675100676100677100678100679100680100681100682100683100684100685100686100687100688100689100690100691100692100693100694100695100696100697100698100699100700100701100702100703100704100705100706100707100708100709100710100711100712100713100714100715100716100717100718100719100720100721100722100723100724100725100726100727100728100729100730100731100732100733100734100735100736100737100738100739100740100741100742100743100744100745100746100747100748100749100750100751100752100753100754100755100756100757100758100759100760100761100762100763100764100765100766100767100768100769100770100771100772100773100774100775100776100777100778100779100780100781100782100783100784100785100786100787100788100789100790100791100792100793100794100795100796100797100798100799100800100801100802100803100804100805100806100807100808100809100810100811100812100813100814100815100816100817100818100819100820100821100822100823100824100825100826100827100828100829100830100831100832100833100834100835100836100837100838100839100840100841100842100843100844100845100846100847100848100849100850100851100852100853100854100855100856100857100858100859100860100861100862100863100864100865100866100867100868100869100870100871100872100873100874100875100876100877100878100879100880100881100882100883100884100885100886100887100888100889100890100891100892100893100894100895100896100897100898100899100900100901100902100903100904100905100906100907100908100909100910100911100912100913100914100915100916100917100918100919100920100921100922100923100924100925100926100927100928100929100930100931100932100933100934100935100936100937100938100939100940100941100942100943100944100945100946100947100948100949100950100951100952100953100954100955100956100957100958100959100960100961100962100963100964100965100966100967100968100969100970100971100972100973100974100975100976100977100978100979100980100981100982100983100984100985100986100987100988100989100990100991100992100993100994100995100996100997100998100999101000101001101002101003101004101005101006101007101008101009101010101011101012101013101014101015101016101017101018101019101020101021101022101023101024101025101026101027101028101029101030101031101032101033101034101035101036101037101038101039101040101041101042101043101044101045101046101047101048101049101050101051101052101053101054101055101056101057101058101059101060101061101062101063101064101065101066101067101068101069101070101071101072101073101074101075101076101077101078101079101080101081101082101083101084101085101086101087101088101089101090101091101092101093101094101095101096101097101098101099101100101101101102101103101104101105101106101107101108101109101110101111101112101113101114101115101116101117101118101119101120101121101122101123101124101125101126101127101128101129101130101131101132101133101134101135101136101137101138101139101140101141101142101143101144101145101146101147101148101149101150101151101152101153101154101155101156101157101158101159101160101161101162101163101164101165101166101167101168101169101170101171101172101173101174101175101176101177101178101179101180101181101182101183101184101185101186101187101188101189101190101191101192101193101194101195101196101197101198101199101200101201101202101203101204101205101206101207101208101209101210101211101212101213101214101215101216101217101218101219101220101221101222101223101224101225101226101227101228101229101230101231101232101233101234101235101236101237101238101239101240101241101242101243101244101245101246101247101248101249101250101251101252101253101254101255101256101257101258101259101260101261101262101263101264101265101266101267101268101269101270101271101272101273101274101275101276101277101278101279101280101281101282101283101284101285101286101287101288101289101290101291101292101293101294101295101296101297101298101299101300101301101302101303101304101305101306101307101308101309101310101311101312101313101314101315101316101317101318101319101320101321101322101323101324101325101326101327101328101329101330101331101332101333101334101335101336101337101338101339101340101341101342101343101344101345101346101347101348101349101350101351101352101353101354101355101356101357101358101359101360101361101362101363101364101365101366101367101368101369101370101371101372101373101374101375101376101377101378101379101380101381101382101383101384101385101386101387101388101389101390101391101392101393101394101395101396101397101398101399101400101401101402101403101404101405101406101407101408101409101410101411101412101413101414101415101416101417101418101419101420101421101422101423101424101425101426101427101428101429101430101431101432101433101434101435101436101437101438101439101440101441101442101443101444101445101446101447101448101449101450101451101452101453101454101455101456101457101458101459101460101461101462101463101464101465101466101467101468101469101470101471101472101473101474101475101476101477101478101479101480101481101482101483101484101485101486101487101488101489101490101491101492101493101494101495101496101497101498101499101500101501101502101503101504101505101506101507101508101509101510101511101512101513101514101515101516101517101518101519101520101521101522101523101524101525101526101527101528101529101530101531101532101533101534101535101536101537101538101539101540101541101542101543101544101545101546101547101548101549101550101551101552101553101554101555101556101557101558101559101560101561101562101563101564101565101566101567101568101569101570101571101572101573101574101575101576101577101578101579101580101581101582101583101584101585101586101587101588101589101590101591101592101593101594101595101596101597101598101599101600101601101602101603101604101605101606101607101608101609101610101611101612101613101614101615101616101617101618101619101620101621101622101623101624101625101626101627101628101629101630101631101632101633101634101635101636101637101638101639101640101641101642101643101644101645101646101647101648101649101650101651101652101653101654101655101656101657101658101659101660101661101662101663101664101665101666101667101668101669101670101671101672101673101674101675101676101677101678101679101680101681101682101683101684101685101686101687101688101689101690101691101692101693101694101695101696101697101698101699101700101701101702101703101704101705101706101707101708101709101710101711101712101713101714101715101716101717101718101719101720101721101722101723101724101725101726101727101728101729101730101731101732101733101734101735101736101737101738101739101740101741101742101743101744101745101746101747101748101749101750101751101752101753101754101755101756101757101758101759101760101761101762101763101764101765101766101767101768101769101770101771101772101773101774101775101776101777101778101779101780101781101782101783101784101785101786101787101788101789101790101791101792101793101794101795101796101797101798101799101800101801101802101803101804101805101806101807101808101809101810101811101812101813101814101815101816101817101818101819101820101821101822101823101824101825101826101827101828101829101830101831101832101833101834101835101836101837101838101839101840101841101842101843101844101845101846101847101848101849101850101851101852101853101854101855101856101857101858101859101860101861101862101863101864101865101866101867101868101869101870101871101872101873101874101875101876101877101878101879101880101881101882101883101884101885101886101887101888101889101890101891101892101893101894101895101896101897101898101899101900101901101902101903101904101905101906101907101908101909101910101911101912101913101914101915101916101917101918101919101920101921101922101923101924101925101926101927101928101929101930101931101932101933101934101935101936101937101938101939101940101941101942101943101944101945101946101947101948101949101950101951101952101953101954101955101956101957101958101959101960101961101962101963101964101965101966101967101968101969101970101971101972101973101974101975101976101977101978101979101980101981101982101983101984101985101986101987101988101989101990101991101992101993101994101995101996101997101998101999102000102001102002102003102004102005102006102007102008102009102010102011102012102013102014102015102016102017102018102019102020102021102022102023102024102025102026102027102028102029102030102031102032102033102034102035102036102037102038102039102040102041102042102043102044102045102046102047102048102049102050102051102052102053102054102055102056102057102058102059102060102061102062102063102064102065102066102067102068102069102070102071102072102073102074102075102076102077102078102079102080102081102082102083102084102085102086102087102088102089102090102091102092102093102094102095102096102097102098102099102100102101102102102103102104102105102106102107102108102109102110102111102112102113102114102115102116102117102118102119102120102121102122102123102124102125102126102127102128102129102130102131102132102133102134102135102136102137102138102139102140102141102142102143102144102145102146102147102148102149102150102151102152102153102154102155102156102157102158102159102160102161102162102163102164102165102166102167102168102169102170102171102172102173102174102175102176102177102178102179102180102181102182102183102184102185102186102187102188102189102190102191102192102193102194102195102196102197102198102199102200102201102202102203102204102205102206102207102208102209102210102211102212102213102214102215102216102217102218102219102220102221102222102223102224102225102226102227102228102229102230102231102232102233102234102235102236102237102238102239102240102241102242102243102244102245102246102247102248102249102250102251102252102253102254102255102256102257102258102259102260102261102262102263102264102265102266102267102268102269102270102271102272102273102274102275102276102277102278102279102280102281102282102283102284102285102286102287102288102289102290102291102292102293102294102295102296102297102298102299102300102301102302102303102304102305102306102307102308102309102310102311102312102313102314102315102316102317102318102319102320102321102322102323102324102325102326102327102328102329102330102331102332102333102334102335102336102337102338102339102340102341102342102343102344102345102346102347102348102349102350102351102352102353102354102355102356102357102358102359102360102361102362102363102364102365102366102367102368102369102370102371102372102373102374102375102376102377102378102379102380102381102382102383102384102385102386102387102388102389102390102391102392102393102394102395102396102397102398102399102400102401102402102403102404102405102406102407102408102409102410102411102412102413102414102415102416102417102418102419102420102421102422102423102424102425102426102427102428102429102430102431102432102433102434102435102436102437102438102439102440102441102442102443102444102445102446102447102448102449102450102451102452102453102454102455102456102457102458102459102460102461102462102463102464102465102466102467102468102469102470102471102472102473102474102475102476102477102478102479102480102481102482102483102484102485102486102487102488102489102490102491102492102493102494102495102496102497102498102499102500102501102502102503102504102505102506102507102508102509102510102511102512102513102514102515102516102517102518102519102520102521102522102523102524102525102526102527102528102529102530102531102532102533102534102535102536102537102538102539102540102541102542102543102544102545102546102547102548102549102550102551102552102553102554102555102556102557102558102559102560102561102562102563102564102565102566102567102568102569102570102571102572102573102574102575102576102577102578102579102580102581102582102583102584102585102586102587102588102589102590102591102592102593102594102595102596102597102598102599102600102601102602102603102604102605102606102607102608102609102610102611102612102613102614102615102616102617102618102619102620102621102622102623102624102625102626102627102628102629102630102631102632102633102634102635102636102637102638102639102640102641102642102643102644102645102646102647102648102649102650102651102652102653102654102655102656102657102658102659102660102661102662102663102664102665102666102667102668102669102670102671102672102673102674102675102676102677102678102679102680102681102682102683102684102685102686102687102688102689102690102691102692102693102694102695102696102697102698102699102700102701102702102703102704102705102706102707102708102709102710102711102712102713102714102715102716102717102718102719102720102721102722102723102724102725102726102727102728102729102730102731102732102733102734102735102736102737102738102739102740102741102742102743102744102745102746102747102748102749102750102751102752102753102754102755102756102757102758102759102760102761102762102763102764102765102766102767102768102769102770102771102772102773102774102775102776102777102778102779102780102781102782102783102784102785102786102787102788102789102790102791102792102793102794102795102796102797102798102799102800102801102802102803102804102805102806102807102808102809102810102811102812102813102814102815102816102817102818102819102820102821102822102823102824102825102826102827102828102829102830102831102832102833102834102835102836102837102838102839102840102841102842102843102844102845102846102847102848102849102850102851102852102853102854102855102856102857102858102859102860102861102862102863102864102865102866102867102868102869102870102871102872102873102874102875102876102877102878102879102880102881102882102883102884102885102886102887102888102889102890102891102892102893102894102895102896102897102898102899102900102901102902102903102904102905102906102907102908102909102910102911102912102913102914102915102916102917102918102919102920102921102922102923102924102925102926102927102928102929102930102931102932102933102934102935102936102937102938102939102940102941102942102943102944102945102946102947102948102949102950102951102952102953102954102955102956102957102958102959102960102961102962102963102964102965102966102967102968102969102970102971102972102973102974102975102976102977102978102979102980102981102982102983102984102985102986102987102988102989102990102991102992102993102994102995102996102997102998102999103000103001103002103003103004103005103006103007103008103009103010103011103012103013103014103015103016103017103018103019103020103021103022103023103024103025103026103027103028103029103030103031103032103033103034103035103036103037103038103039103040103041103042103043103044103045103046103047103048103049103050103051103052103053103054103055103056103057103058103059103060103061103062103063103064103065103066103067103068103069103070103071103072103073103074103075103076103077103078103079103080103081103082103083103084103085103086103087103088103089103090103091103092103093103094103095103096103097103098103099103100103101103102103103103104103105103106103107103108103109103110103111103112103113103114103115103116103117103118103119103120103121103122103123103124103125103126103127103128103129103130103131103132103133103134103135103136103137103138103139103140103141103142103143103144103145103146103147103148103149103150103151103152103153103154103155103156103157103158103159103160103161103162103163103164103165103166103167103168103169103170103171103172103173103174103175103176103177103178103179103180103181103182103183103184103185103186103187103188103189103190103191103192103193103194103195103196103197103198103199103200103201103202103203103204103205103206103207103208103209103210103211103212103213103214103215103216103217103218103219103220103221103222103223103224103225103226103227103228103229103230103231103232103233103234103235103236103237103238103239103240103241103242103243103244103245103246103247103248103249103250103251103252103253103254103255103256103257103258103259103260103261103262103263103264103265103266103267103268103269103270103271103272103273103274103275103276103277103278103279103280103281103282103283103284103285103286103287103288103289103290103291103292103293103294103295103296103297103298103299103300103301103302103303103304103305103306103307103308103309103310103311103312103313103314103315103316103317103318103319103320103321103322103323103324103325103326103327103328103329103330103331103332103333103334103335103336103337103338103339103340103341103342103343103344103345103346103347103348103349103350103351103352103353103354103355103356103357103358103359103360103361103362103363103364103365103366103367103368103369103370103371103372103373103374103375103376103377103378103379103380103381103382103383103384103385103386103387103388103389103390103391103392103393103394103395103396103397103398103399103400103401103402103403103404103405103406103407103408103409103410103411103412103413103414103415103416103417103418103419103420103421103422103423103424103425103426103427103428103429103430103431103432103433103434103435103436103437103438103439103440103441103442103443103444103445103446103447103448103449103450103451103452103453103454103455103456103457103458103459103460103461103462103463103464103465103466103467103468103469103470103471103472103473103474103475103476103477103478103479103480103481103482103483103484103485103486103487103488103489103490103491103492103493103494103495103496103497103498103499103500103501103502103503103504103505103506103507103508103509103510103511103512103513103514103515103516103517103518103519103520103521103522103523103524103525103526103527103528103529103530103531103532103533103534103535103536103537103538103539103540103541103542103543103544103545103546103547103548103549103550103551103552103553103554103555103556103557103558103559103560103561103562103563103564103565103566103567103568103569103570103571103572103573103574103575103576103577103578103579103580103581103582103583103584103585103586103587103588103589103590103591103592103593103594103595103596103597103598103599103600103601103602103603103604103605103606103607103608103609103610103611103612103613103614103615103616103617103618103619103620103621103622103623103624103625103626103627103628103629103630103631103632103633103634103635103636103637103638103639103640103641103642103643103644103645103646103647103648103649103650103651103652103653103654103655103656103657103658103659103660103661103662103663103664103665103666103667103668103669103670103671103672103673103674103675103676103677103678103679103680103681103682103683103684103685103686103687103688103689103690103691103692103693103694103695103696103697103698103699103700103701103702103703103704103705103706103707103708103709103710103711103712103713103714103715103716103717103718103719103720103721103722103723103724103725103726103727103728103729103730103731103732103733103734103735103736103737103738103739103740103741103742103743103744103745103746103747103748103749103750103751103752103753103754103755103756103757103758103759103760103761103762103763103764103765103766103767103768103769103770103771103772103773103774103775103776103777103778103779103780103781103782103783103784103785103786103787103788103789103790103791103792103793103794103795103796103797103798103799103800103801103802103803103804103805103806103807103808103809103810103811103812103813103814103815103816103817103818103819103820103821103822103823103824103825103826103827103828103829103830103831103832103833103834103835103836103837103838103839103840103841103842103843103844103845103846103847103848103849103850103851103852103853103854103855103856103857103858103859103860103861103862103863103864103865103866103867103868103869103870103871103872103873103874103875103876103877103878103879103880103881103882103883103884103885103886103887103888103889103890103891103892103893103894103895103896103897103898103899103900103901103902103903103904103905103906103907103908103909103910103911103912103913103914103915103916103917103918103919103920103921103922103923103924103925103926103927103928103929103930103931103932103933103934103935103936103937103938103939103940103941103942103943103944103945103946103947103948103949103950103951103952103953103954103955103956103957103958103959103960103961103962103963103964103965103966103967103968103969103970103971103972103973103974103975103976103977103978103979103980103981103982103983103984103985103986103987103988103989103990103991103992103993103994103995103996103997103998103999104000104001104002104003104004104005104006104007104008104009104010104011104012104013104014104015104016104017104018104019104020104021104022104023104024104025104026104027104028104029104030104031104032104033104034104035104036104037104038104039104040104041104042104043104044104045104046104047104048104049104050104051104052104053104054104055104056104057104058104059104060104061104062104063104064104065104066104067104068104069104070104071104072104073104074104075104076104077104078104079104080104081104082104083104084104085104086104087104088104089104090104091104092104093104094104095104096104097104098104099104100104101104102104103104104104105104106104107104108104109104110104111104112104113104114104115104116104117104118104119104120104121104122104123104124104125104126104127104128104129104130104131104132104133104134104135104136104137104138104139104140104141104142104143104144104145104146104147104148104149104150104151104152104153104154104155104156104157104158104159104160104161104162104163104164104165104166104167104168104169104170104171104172104173104174104175104176104177104178104179104180104181104182104183104184104185104186104187104188104189104190104191104192104193104194104195104196104197104198104199104200104201104202104203104204104205104206104207104208104209104210104211104212104213104214104215104216104217104218104219104220104221104222104223104224104225104226104227104228104229104230104231104232104233104234104235104236104237104238104239104240104241104242104243104244104245104246104247104248104249104250104251104252104253104254104255104256104257104258104259104260104261104262104263104264104265104266104267104268104269104270104271104272104273104274104275104276104277104278104279104280104281104282104283104284104285104286104287104288104289104290104291104292104293104294104295104296104297104298104299104300104301104302104303104304104305104306104307104308104309104310104311104312104313104314104315104316104317104318104319104320104321104322104323104324104325104326104327104328104329104330104331104332104333104334104335104336104337104338104339104340104341104342104343104344104345104346104347104348104349104350104351104352104353104354104355104356104357104358104359104360104361104362104363104364104365104366104367104368104369104370104371104372104373104374104375104376104377104378104379104380104381104382104383104384104385104386104387104388104389104390104391104392104393104394104395104396104397104398104399104400104401104402104403104404104405104406104407104408104409104410104411104412104413104414104415104416104417104418104419104420104421104422104423104424104425104426104427104428104429104430104431104432104433104434104435104436104437104438104439104440104441104442104443104444104445104446104447104448104449104450104451104452104453104454104455104456104457104458104459104460104461104462104463104464104465104466104467104468104469104470104471104472104473104474104475104476104477104478104479104480104481104482104483104484104485104486104487104488104489104490104491104492104493104494104495104496104497104498104499104500104501104502104503104504104505104506104507104508104509104510104511104512104513104514104515104516104517104518104519104520104521104522104523104524104525104526104527104528104529104530104531104532104533104534104535104536104537104538104539104540104541104542104543104544104545104546104547104548104549104550104551104552104553104554104555104556104557104558104559104560104561104562104563104564104565104566104567104568104569104570104571104572104573104574104575104576104577104578104579104580104581104582104583104584104585104586104587104588104589104590104591104592104593104594104595104596104597104598104599104600104601104602104603104604104605104606104607104608104609104610104611104612104613104614104615104616104617104618104619104620104621104622104623104624104625104626104627104628104629104630104631104632104633104634104635104636104637104638104639104640104641104642104643104644104645104646104647104648104649104650104651104652104653104654104655104656104657104658104659104660104661104662104663104664104665104666104667104668104669104670104671104672104673104674104675104676104677104678104679104680104681104682104683104684104685104686104687104688104689104690104691104692104693104694104695104696104697104698104699104700104701104702104703104704104705104706104707104708104709104710104711104712104713104714104715104716104717104718104719104720104721104722104723104724104725104726104727104728104729104730104731104732104733104734104735104736104737104738104739104740104741104742104743104744104745104746104747104748104749104750104751104752104753104754104755104756104757104758104759104760104761104762104763104764104765104766104767104768104769104770104771104772104773104774104775104776104777104778104779104780104781104782104783104784104785104786104787104788104789104790104791104792104793104794104795104796104797104798104799104800104801104802104803104804104805104806104807104808104809104810104811104812104813104814104815104816104817104818104819104820104821104822104823104824104825104826104827104828104829104830104831104832104833104834104835104836104837104838104839104840104841104842104843104844104845104846104847104848104849104850104851104852104853104854104855104856104857104858104859104860104861104862104863104864104865104866104867104868104869104870104871104872104873104874104875104876104877104878104879104880104881104882104883104884104885104886104887104888104889104890104891104892104893104894104895104896104897104898104899104900104901104902104903104904104905104906104907104908104909104910104911104912104913104914104915104916104917104918104919104920104921104922104923104924104925104926104927104928104929104930104931104932104933104934104935104936104937104938104939104940104941104942104943104944104945104946104947104948104949104950104951104952104953104954104955104956104957104958104959104960104961104962104963104964104965104966104967104968104969104970104971104972104973104974104975104976104977104978104979104980104981104982104983104984104985104986104987104988104989104990104991104992104993104994104995104996104997104998104999105000105001105002105003105004105005105006105007105008105009105010105011105012105013105014105015105016105017105018105019105020105021105022105023105024105025105026105027105028105029105030105031105032105033105034105035105036105037105038105039105040105041105042105043105044105045105046105047105048105049105050105051105052105053105054105055105056105057105058105059105060105061105062105063105064105065105066105067105068105069105070105071105072105073105074105075105076105077105078105079105080105081105082105083105084105085105086105087105088105089105090105091105092105093105094105095105096105097105098105099105100105101105102105103105104105105105106105107105108105109105110105111105112105113105114105115105116105117105118105119105120105121105122105123105124105125105126105127105128105129105130105131105132105133105134105135105136105137105138105139105140105141105142105143105144105145105146105147105148105149105150105151105152105153105154105155105156105157105158105159105160105161105162105163105164105165105166105167105168105169105170105171105172105173105174105175105176105177105178105179105180105181105182105183105184105185105186105187105188105189105190105191105192105193105194105195105196105197105198105199105200105201105202105203105204105205105206105207105208105209105210105211105212105213105214105215105216105217105218105219105220105221105222105223105224105225105226105227105228105229105230105231105232105233105234105235105236105237105238105239105240105241105242105243105244105245105246105247105248105249105250105251105252105253105254105255105256105257105258105259105260105261105262105263105264105265105266105267105268105269105270105271105272105273105274105275105276105277105278105279105280105281105282105283105284105285105286105287105288105289105290105291105292105293105294105295105296105297105298105299105300105301105302105303105304105305105306105307105308105309105310105311105312105313105314105315105316105317105318105319105320105321105322105323105324105325105326105327105328105329105330105331105332105333105334105335105336105337105338105339105340105341105342105343105344105345105346105347105348105349105350105351105352105353105354105355105356105357105358105359105360105361105362105363105364105365105366105367105368105369105370105371105372105373105374105375105376105377105378105379105380105381105382105383105384105385105386105387105388105389105390105391105392105393105394105395105396105397105398105399105400105401105402105403105404105405105406105407105408105409105410105411105412105413105414105415105416105417105418105419105420105421105422105423105424105425105426105427105428105429105430105431105432105433105434105435105436105437105438105439105440105441105442105443105444105445105446105447105448105449105450105451105452105453105454105455105456105457105458105459105460105461105462105463105464105465105466105467105468105469105470105471105472105473105474105475105476105477105478105479105480105481105482105483105484105485105486105487105488105489105490105491105492105493105494105495105496105497105498105499105500105501105502105503105504105505105506105507105508105509105510105511105512105513105514105515105516105517105518105519105520105521105522105523105524105525105526105527105528105529105530105531105532105533105534105535105536105537105538105539105540105541105542105543105544105545105546105547105548105549105550105551105552105553105554105555105556105557105558105559105560105561105562105563105564105565105566105567105568105569105570105571105572105573105574105575105576105577105578105579105580105581105582105583105584105585105586105587105588105589105590105591105592105593105594105595105596105597105598105599105600105601105602105603105604105605105606105607105608105609105610105611105612105613105614105615105616105617105618105619105620105621105622105623105624105625105626105627105628105629105630105631105632105633105634105635105636105637105638105639105640105641105642105643105644105645105646105647105648105649105650105651105652105653105654105655105656105657105658105659105660105661105662105663105664105665105666105667105668105669105670105671105672105673105674105675105676105677105678105679105680105681105682105683105684105685105686105687105688105689105690105691105692105693105694105695105696105697105698105699105700105701105702105703105704105705105706105707105708105709105710105711105712105713105714105715105716105717105718105719105720105721105722105723105724105725105726105727105728105729105730105731105732105733105734105735105736105737105738105739105740105741105742105743105744105745105746105747105748105749105750105751105752105753105754105755105756105757105758105759105760105761105762105763105764105765105766105767105768105769105770105771105772105773105774105775105776105777105778105779105780105781105782105783105784105785105786105787105788105789105790105791105792105793105794105795105796105797105798105799105800105801105802105803105804105805105806105807105808105809105810105811105812105813105814105815105816105817105818105819105820105821105822105823105824105825105826105827105828105829105830105831105832105833105834105835105836105837105838105839105840105841105842105843105844105845105846105847105848105849105850105851105852105853105854105855105856105857105858105859105860105861105862105863105864105865105866105867105868105869105870105871105872105873105874105875105876105877105878105879105880105881105882105883105884105885105886105887105888105889105890105891105892105893105894105895105896105897105898105899105900105901105902105903105904105905105906105907105908105909105910105911105912105913105914105915105916105917105918105919105920105921105922105923105924105925105926105927105928105929105930105931105932105933105934105935105936105937105938105939105940105941105942105943105944105945105946105947105948105949105950105951105952105953105954105955105956105957105958105959105960105961105962105963105964105965105966105967105968105969105970105971105972105973105974105975105976105977105978105979105980105981105982105983105984105985105986105987105988105989105990105991105992105993105994105995105996105997105998105999106000106001106002106003106004106005106006106007106008106009106010106011106012106013106014106015106016106017106018106019106020106021106022106023106024106025106026106027106028106029106030106031106032106033106034106035106036106037106038106039106040106041106042106043106044106045106046106047106048106049106050106051106052106053106054106055106056106057106058106059106060106061106062106063106064106065106066106067106068106069106070106071106072106073106074106075106076106077106078106079106080106081106082106083106084106085106086106087106088106089106090106091106092106093106094106095106096106097106098106099106100106101106102106103106104106105106106106107106108106109106110106111106112106113106114106115106116106117106118106119106120106121106122106123106124106125106126106127106128106129106130106131106132106133106134106135106136106137106138106139106140106141106142106143106144106145106146106147106148106149106150106151106152106153106154106155106156106157106158106159106160106161106162106163106164106165106166106167106168106169106170106171106172106173106174106175106176106177106178106179106180106181106182106183106184106185106186106187106188106189106190106191106192106193106194106195106196106197106198106199106200106201106202106203106204106205106206106207106208106209106210106211106212106213106214106215106216106217106218106219106220106221106222106223106224106225106226106227106228106229106230106231106232106233106234106235106236106237106238106239106240106241106242106243106244106245106246106247106248106249106250106251106252106253106254106255106256106257106258106259106260106261106262106263106264106265106266106267106268106269106270106271106272106273106274106275106276106277106278106279106280106281106282106283106284106285106286106287106288106289106290106291106292106293106294106295106296106297106298106299106300106301106302106303106304106305106306106307106308106309106310106311106312106313106314106315106316106317106318106319106320106321106322106323106324106325106326106327106328106329106330106331106332106333106334106335106336106337106338106339106340106341106342106343106344106345106346106347106348106349106350106351106352106353106354106355106356106357106358106359106360106361106362106363106364106365106366106367106368106369106370106371106372106373106374106375106376106377106378106379106380106381106382106383106384106385106386106387106388106389106390106391106392106393106394106395106396106397106398106399106400106401106402106403106404106405106406106407106408106409106410106411106412106413106414106415106416106417106418106419106420106421106422106423106424106425106426106427106428106429106430106431106432106433106434106435106436106437106438106439106440106441106442106443106444106445106446106447106448106449106450106451106452106453106454106455106456106457106458106459106460106461106462106463106464106465106466106467106468106469106470106471106472106473106474106475106476106477106478106479106480106481106482106483106484106485106486106487106488106489106490106491106492106493106494106495106496106497106498106499106500106501106502106503106504106505106506106507106508106509106510106511106512106513106514106515106516106517106518106519106520106521106522106523106524106525106526106527106528106529106530106531106532106533106534106535106536106537106538106539106540106541106542106543106544106545106546106547106548106549106550106551106552106553106554106555106556106557106558106559106560106561106562106563106564106565106566106567106568106569106570106571106572106573106574106575106576106577106578106579106580106581106582106583106584106585106586106587106588106589106590106591106592106593106594106595106596106597106598106599106600106601106602106603106604106605106606106607106608106609106610106611106612106613106614106615106616106617106618106619106620106621106622106623106624106625106626106627106628106629106630106631106632106633106634106635106636106637106638106639106640106641106642106643106644106645106646106647106648106649106650106651106652106653106654106655106656106657106658106659106660106661106662106663106664106665106666106667106668106669106670106671106672106673106674106675106676106677106678106679106680106681106682106683106684106685106686106687106688106689106690106691106692106693106694106695106696106697106698106699106700106701106702106703106704106705106706106707106708106709106710106711106712106713106714106715106716106717106718106719106720106721106722106723106724106725106726106727106728106729106730106731106732106733106734106735106736106737106738106739106740106741106742106743106744106745106746106747106748106749106750106751106752106753106754106755106756106757106758106759106760106761106762106763106764106765106766106767106768106769106770106771106772106773106774106775106776106777106778106779106780106781106782106783106784106785106786106787106788106789106790106791106792106793106794106795106796106797106798106799106800106801106802106803106804106805106806106807106808106809106810106811106812106813106814106815106816106817106818106819106820106821106822106823106824106825106826106827106828106829106830106831106832106833106834106835106836106837106838106839106840106841106842106843106844106845106846106847106848106849106850106851106852106853106854106855106856106857106858106859106860106861106862106863106864106865106866106867106868106869106870106871106872106873106874106875106876106877106878106879106880106881106882106883106884106885106886106887106888106889106890106891106892106893106894106895106896106897106898106899106900106901106902106903106904106905106906106907106908106909106910106911106912106913106914106915106916106917106918106919106920106921106922106923106924106925106926106927106928106929106930106931106932106933106934106935106936106937106938106939106940106941106942106943106944106945106946106947106948106949106950106951106952106953106954106955106956106957106958106959106960106961106962106963106964106965106966106967106968106969106970106971106972106973106974106975106976106977106978106979106980106981106982106983106984106985106986106987106988106989106990106991106992106993106994106995106996106997106998106999107000107001107002107003107004107005107006107007107008107009107010107011107012107013107014107015107016107017107018107019107020107021107022107023107024107025107026107027107028107029107030107031107032107033107034107035107036107037107038107039107040107041107042107043107044107045107046107047107048107049107050107051107052107053107054107055107056107057107058107059107060107061107062107063107064107065107066107067107068107069107070107071107072107073107074107075107076107077107078107079107080107081107082107083107084107085107086107087107088107089107090107091107092107093107094107095107096107097107098107099107100107101107102107103107104107105107106107107107108107109107110107111107112107113107114107115107116107117107118107119107120107121107122107123107124107125107126107127107128107129107130107131107132107133107134107135107136107137107138107139107140107141107142107143107144107145107146107147107148107149107150107151107152107153107154107155107156107157107158107159107160107161107162107163107164107165107166107167107168107169107170107171107172107173107174107175107176107177107178107179107180107181107182107183107184107185107186107187107188107189107190107191107192107193107194107195107196107197107198107199107200107201107202107203107204107205107206107207107208107209107210107211107212107213107214107215107216107217107218107219107220107221107222107223107224107225107226107227107228107229107230107231107232107233107234107235107236107237107238107239107240107241107242107243107244107245107246107247107248107249107250107251107252107253107254107255107256107257107258107259107260107261107262107263107264107265107266107267107268107269107270107271107272107273107274107275107276107277107278107279107280107281107282107283107284107285107286107287107288107289107290107291107292107293107294107295107296107297107298107299107300107301107302107303107304107305107306107307107308107309107310107311107312107313107314107315107316107317107318107319107320107321107322107323107324107325107326107327107328107329107330107331107332107333107334107335107336107337107338107339107340107341107342107343107344107345107346107347107348107349107350107351107352107353107354107355107356107357107358107359107360107361107362107363107364107365107366107367107368107369107370107371107372107373107374107375107376107377107378107379107380107381107382107383107384107385107386107387107388107389107390107391107392107393107394107395107396107397107398107399107400107401107402107403107404107405107406107407107408107409107410107411107412107413107414107415107416107417107418107419107420107421107422107423107424107425107426107427107428107429107430107431107432107433107434107435107436107437107438107439107440107441107442107443107444107445107446107447107448107449107450107451107452107453107454107455107456107457107458107459107460107461107462107463107464107465107466107467107468107469107470107471107472107473107474107475107476107477107478107479107480107481107482107483107484107485107486107487107488107489107490107491107492107493107494107495107496107497107498107499107500107501107502107503107504107505107506107507107508107509107510107511107512107513107514107515107516107517107518107519107520107521107522107523107524107525107526107527107528107529107530107531107532107533107534107535107536107537107538107539107540107541107542107543107544107545107546107547107548107549107550107551107552107553107554107555107556107557107558107559107560107561107562107563107564107565107566107567107568107569107570107571107572107573107574107575107576107577107578107579107580107581107582107583107584107585107586107587107588107589107590107591107592107593107594107595107596107597107598107599107600107601107602107603107604107605107606107607107608107609107610107611107612107613107614107615107616107617107618107619107620107621107622107623107624107625107626107627107628107629107630107631107632107633107634107635107636107637107638107639107640107641107642107643107644107645107646107647107648107649107650107651107652107653107654107655107656107657107658107659107660107661107662107663107664107665107666107667107668107669107670107671107672107673107674107675107676107677107678107679107680107681107682107683107684107685107686107687107688107689107690107691107692107693107694107695107696107697107698107699107700107701107702107703107704107705107706107707107708107709107710107711107712107713107714107715107716107717107718107719107720107721107722107723107724107725107726107727107728107729107730107731107732107733107734107735107736107737107738107739107740107741107742107743107744107745107746107747107748107749107750107751107752107753107754107755107756107757107758107759107760107761107762107763107764107765107766107767107768107769107770107771107772107773107774107775107776107777107778107779107780107781107782107783107784107785107786107787107788107789107790107791107792107793107794107795107796107797107798107799107800107801107802107803107804107805107806107807107808107809107810107811107812107813107814107815107816107817107818107819107820107821107822107823107824107825107826107827107828107829107830107831107832107833107834107835107836107837107838107839107840107841107842107843107844107845107846107847107848107849107850107851107852107853107854107855107856107857107858107859107860107861107862107863107864107865107866107867107868107869107870107871107872107873107874107875107876107877107878107879107880107881107882107883107884107885107886107887107888107889107890107891107892107893107894107895107896107897107898107899107900107901107902107903107904107905107906107907107908107909107910107911107912107913107914107915107916107917107918107919107920107921107922107923107924107925107926107927107928107929107930107931107932107933107934107935107936107937107938107939107940107941107942107943107944107945107946107947107948107949107950107951107952107953107954107955107956107957107958107959107960107961107962107963107964107965107966107967107968107969107970107971107972107973107974107975107976107977107978107979107980107981107982107983107984107985107986107987107988107989107990107991107992107993107994107995107996107997107998107999108000108001108002108003108004108005108006108007108008108009108010108011108012108013108014108015108016108017108018108019108020108021108022108023108024108025108026108027108028108029108030108031108032108033108034108035108036108037108038108039108040108041108042108043108044108045108046108047108048108049108050108051108052108053108054108055108056108057108058108059108060108061108062108063108064108065108066108067108068108069108070108071108072108073108074108075108076108077108078108079108080108081108082108083108084108085108086108087108088108089108090108091108092108093108094108095108096108097108098108099108100108101108102108103108104108105108106108107108108108109108110108111108112108113108114108115108116108117108118108119108120108121108122108123108124108125108126108127108128108129108130108131108132108133108134108135108136108137108138108139108140108141108142108143108144108145108146108147108148108149108150108151108152108153108154108155108156108157108158108159108160108161108162108163108164108165108166108167108168108169108170108171108172108173108174108175108176108177108178108179108180108181108182108183108184108185108186108187108188108189108190108191108192108193108194108195108196108197108198108199108200108201108202108203108204108205108206108207108208108209108210108211108212108213108214108215108216108217108218108219108220108221108222108223108224108225108226108227108228108229108230108231108232108233108234108235108236108237108238108239108240108241108242108243108244108245108246108247108248108249108250108251108252108253108254108255108256108257108258108259108260108261108262108263108264108265108266108267108268108269108270108271108272108273108274108275108276108277108278108279108280108281108282108283108284108285108286108287108288108289108290108291108292108293108294108295108296108297108298108299108300108301108302108303108304108305108306108307108308108309108310108311108312108313108314108315108316108317108318108319108320108321108322108323108324108325108326108327108328108329108330108331108332108333108334108335108336108337108338108339108340108341108342108343108344108345108346108347108348108349108350108351108352108353108354108355108356108357108358108359108360108361108362108363108364108365108366108367108368108369108370108371108372108373108374108375108376108377108378108379108380108381108382108383108384108385108386108387108388108389108390108391108392108393108394108395108396108397108398108399108400108401108402108403108404108405108406108407108408108409108410108411108412108413108414108415108416108417108418108419108420108421108422108423108424108425108426108427108428108429108430108431108432108433108434108435108436108437108438108439108440108441108442108443108444108445108446108447108448108449108450108451108452108453108454108455108456108457108458108459108460108461108462108463108464108465108466108467108468108469108470108471108472108473108474108475108476108477108478108479108480108481108482108483108484108485108486108487108488108489108490108491108492108493108494108495108496108497108498108499108500108501108502108503108504108505108506108507108508108509108510108511108512108513108514108515108516108517108518108519108520108521108522108523108524108525108526108527108528108529108530108531108532108533108534108535108536108537108538108539108540108541108542108543108544108545108546108547108548108549108550108551108552108553108554108555108556108557108558108559108560108561108562108563108564108565108566108567108568108569108570108571108572108573108574108575108576108577108578108579108580108581108582108583108584108585108586108587108588108589108590108591108592108593108594108595108596108597108598108599108600108601108602108603108604108605108606108607108608108609108610108611108612108613108614108615108616108617108618108619108620108621108622108623108624108625108626108627108628108629108630108631108632108633108634108635108636108637108638108639108640108641108642108643108644108645108646108647108648108649108650108651108652108653108654108655108656108657108658108659108660108661108662108663108664108665108666108667108668108669108670108671108672108673108674108675108676108677108678108679108680108681108682108683108684108685108686108687108688108689108690108691108692108693108694108695108696108697108698108699108700108701108702108703108704108705108706108707108708108709108710108711108712108713108714108715108716108717108718108719108720108721108722108723108724108725108726108727108728108729108730108731108732108733108734108735108736108737108738108739108740108741108742108743108744108745108746108747108748108749108750108751108752108753108754108755108756108757108758108759108760108761108762108763108764108765108766108767108768108769108770108771108772108773108774108775108776108777108778108779108780108781108782108783108784108785108786108787108788108789108790108791108792108793108794108795108796108797108798108799108800108801108802108803108804108805108806108807108808108809108810108811108812108813108814108815108816108817108818108819108820108821108822108823108824108825108826108827108828108829108830108831108832108833108834108835108836108837108838108839108840108841108842108843108844108845108846108847108848108849108850108851108852108853108854108855108856108857108858108859108860108861108862108863108864108865108866108867108868108869108870108871108872108873108874108875108876108877108878108879108880108881108882108883108884108885108886108887108888108889108890108891108892108893108894108895108896108897108898108899108900108901108902108903108904108905108906108907108908108909108910108911108912108913108914108915108916108917108918108919108920108921108922108923108924108925108926108927108928108929108930108931108932108933108934108935108936108937108938108939108940108941108942108943108944108945108946108947108948108949108950108951108952108953108954108955108956108957108958108959108960108961108962108963108964108965108966108967108968108969108970108971108972108973108974108975108976108977108978108979108980108981108982108983108984108985108986108987108988108989108990108991108992108993108994108995108996108997108998108999109000109001109002109003109004109005109006109007109008109009109010109011109012109013109014109015109016109017109018109019109020109021109022109023109024109025109026109027109028109029109030109031109032109033109034109035109036109037109038109039109040109041109042109043109044109045109046109047109048109049109050109051109052109053109054109055109056109057109058109059109060109061109062109063109064109065109066109067109068109069109070109071109072109073109074109075109076109077109078109079109080109081109082109083109084109085109086109087109088109089109090109091109092109093109094109095109096109097109098109099109100109101109102109103109104109105109106109107109108109109109110109111109112109113109114109115109116109117109118109119109120109121109122109123109124109125109126109127109128109129109130109131109132109133109134109135109136109137109138109139109140109141109142109143109144109145109146109147109148109149109150109151109152109153109154109155109156109157109158109159109160109161109162109163109164109165109166109167109168109169109170109171109172109173109174109175109176109177109178109179109180109181109182109183109184109185109186109187109188109189109190109191109192109193109194109195109196109197109198109199109200109201109202109203109204109205109206109207109208109209109210109211109212109213109214109215109216109217109218109219109220109221109222109223109224109225109226109227109228109229109230109231109232109233109234109235109236109237109238109239109240109241109242109243109244109245109246109247109248109249109250109251109252109253109254109255109256109257109258109259109260109261109262109263109264109265109266109267109268109269109270109271109272109273109274109275109276109277109278109279109280109281109282109283109284109285109286109287109288109289109290109291109292109293109294109295109296109297109298109299109300109301109302109303109304109305109306109307109308109309109310109311109312109313109314109315109316109317109318109319109320109321109322109323109324109325109326109327109328109329109330109331109332109333109334109335109336109337109338109339109340109341109342109343109344109345109346109347109348109349109350109351109352109353109354109355109356109357109358109359109360109361109362109363109364109365109366109367109368109369109370109371109372109373109374109375109376109377109378109379109380109381109382109383109384109385109386109387109388109389109390109391109392109393109394109395109396109397109398109399109400109401109402109403109404109405109406109407109408109409109410109411109412109413109414109415109416109417109418109419109420109421109422109423109424109425109426109427109428109429109430109431109432109433109434109435109436109437109438109439109440109441109442109443109444109445109446109447109448109449109450109451109452109453109454109455109456109457109458109459109460109461109462109463109464109465109466109467109468109469109470109471109472109473109474109475109476109477109478109479109480109481109482109483109484109485109486109487109488109489109490109491109492109493109494109495109496109497109498109499109500109501109502109503109504109505109506109507109508109509109510109511109512109513109514109515109516109517109518109519109520109521109522109523109524109525109526109527109528109529109530109531109532109533109534109535109536109537109538109539109540109541109542109543109544109545109546109547109548109549109550109551109552109553109554109555109556109557109558109559109560109561109562109563109564109565109566109567109568109569109570109571109572109573109574109575109576109577109578109579109580109581109582109583109584109585109586109587109588109589109590109591109592109593109594109595109596109597109598109599109600109601109602109603109604109605109606109607109608109609109610109611109612109613109614109615109616109617109618109619109620109621109622109623109624109625109626109627109628109629109630109631109632109633109634109635109636109637109638109639109640109641109642109643109644109645109646109647109648109649109650109651109652109653109654109655109656109657109658109659109660109661109662109663109664109665109666109667109668109669109670109671109672109673109674109675109676109677109678109679109680109681109682109683109684109685109686109687109688109689109690109691109692109693109694109695109696109697109698109699109700109701109702109703109704109705109706109707109708109709109710109711109712109713109714109715109716109717109718109719109720109721109722109723109724109725109726109727109728109729109730109731109732109733109734109735109736109737109738109739109740109741109742109743109744109745109746109747109748109749109750109751109752109753109754109755109756109757109758109759109760109761109762109763109764109765109766109767109768109769109770109771109772109773109774109775109776109777109778109779109780109781109782109783109784109785109786109787109788109789109790109791109792109793109794109795109796109797109798109799109800109801109802109803109804109805109806109807109808109809109810109811109812109813109814109815109816109817109818109819109820109821109822109823109824109825109826109827109828109829109830109831109832109833109834109835109836109837109838109839109840109841109842109843109844109845109846109847109848109849109850109851109852109853109854109855109856109857109858109859109860109861109862109863109864109865109866109867109868109869109870109871109872109873109874109875109876109877109878109879109880109881109882109883109884109885109886109887109888109889109890109891109892109893109894109895109896109897109898109899109900109901109902109903109904109905109906109907109908109909109910109911109912109913109914109915109916109917109918109919109920109921109922109923109924109925109926109927109928109929109930109931109932109933109934109935109936109937109938109939109940109941109942109943109944109945109946109947109948109949109950109951109952109953109954109955109956109957109958109959109960109961109962109963109964109965109966109967109968109969109970109971109972109973109974109975109976109977109978109979109980109981109982109983109984109985109986109987109988109989109990109991109992109993109994109995109996109997109998109999110000110001110002110003110004110005110006110007110008110009110010110011110012110013110014110015110016110017110018110019110020110021110022110023110024110025110026110027110028110029110030110031110032110033110034110035110036110037110038110039110040110041110042110043110044110045110046110047110048110049110050110051110052110053110054110055110056110057110058110059110060110061110062110063110064110065110066110067110068110069110070110071110072110073110074110075110076110077110078110079110080110081110082110083110084110085110086110087110088110089110090110091110092110093110094110095110096110097110098110099110100110101110102110103110104110105110106110107110108110109110110110111110112110113110114110115110116110117110118110119110120110121110122110123110124110125110126110127110128110129110130110131110132110133110134110135110136110137110138110139110140110141110142110143110144110145110146110147110148110149110150110151110152110153110154110155110156110157110158110159110160110161110162110163110164110165110166110167110168110169110170110171110172110173110174110175110176110177110178110179110180110181110182110183110184110185110186110187110188110189110190110191110192110193110194110195110196110197110198110199110200110201110202110203110204110205110206110207110208110209110210110211110212110213110214110215110216110217110218110219110220110221110222110223110224110225110226110227110228110229110230110231110232110233110234110235110236110237110238110239110240110241110242110243110244110245110246110247110248110249110250110251110252110253110254110255110256110257110258110259110260110261110262110263110264110265110266110267110268110269110270110271110272110273110274110275110276110277110278110279110280110281110282110283110284110285110286110287110288110289110290110291110292110293110294110295110296110297110298110299110300110301110302110303110304110305110306110307110308110309110310110311110312110313110314110315110316110317110318110319110320110321110322110323110324110325110326110327110328110329110330110331110332110333110334110335110336110337110338110339110340110341110342110343110344110345110346110347110348110349110350110351110352110353110354110355110356110357110358110359110360110361110362110363110364110365110366110367110368110369110370110371110372110373110374110375110376110377110378110379110380110381110382110383110384110385110386110387110388110389110390110391110392110393110394110395110396110397110398110399110400110401110402110403110404110405110406110407110408110409110410110411110412110413110414110415110416110417110418110419110420110421110422110423110424110425110426110427110428110429110430110431110432110433110434110435110436110437110438110439110440110441110442110443110444110445110446110447110448110449110450110451110452110453110454110455110456110457110458110459110460110461110462110463110464110465110466110467110468110469110470110471110472110473110474110475110476110477110478110479110480110481110482110483110484110485110486110487110488110489110490110491
  1. diff -Nur linux-3.16.2/arch/arm/boot/dts/bcm2708.dtsi linux-3.16-rpi/arch/arm/boot/dts/bcm2708.dtsi
  2. --- linux-3.16.2/arch/arm/boot/dts/bcm2708.dtsi 1970-01-01 01:00:00.000000000 +0100
  3. +++ linux-3.16-rpi/arch/arm/boot/dts/bcm2708.dtsi 2014-09-14 19:03:04.000000000 +0200
  4. @@ -0,0 +1,88 @@
  5. +/include/ "skeleton.dtsi"
  6. +
  7. +/ {
  8. + compatible = "brcm,bcm2708";
  9. + model = "BCM2708";
  10. +
  11. + interrupt-parent = <&intc>;
  12. +
  13. + chosen {
  14. + /*
  15. + bootargs must be 1024 characters long because the
  16. + VC bootloader can't expand it
  17. + */
  18. + bootargs = "console=ttyAMA0 ";
  19. + };
  20. +
  21. + soc {
  22. + compatible = "simple-bus";
  23. + #address-cells = <1>;
  24. + #size-cells = <1>;
  25. + ranges = <0x7e000000 0x20000000 0x02000000>;
  26. +
  27. + intc: interrupt-controller {
  28. + compatible = "brcm,bcm2708-armctrl-ic";
  29. + reg = <0x7e00b200 0x200>;
  30. + interrupt-controller;
  31. + #interrupt-cells = <2>;
  32. + };
  33. +
  34. + gpio: gpio {
  35. + compatible = "brcm,bcm2708-pinctrl";
  36. + reg = <0x7e200000 0xb4>;
  37. + gpio-controller;
  38. + #gpio-cells = <2>;
  39. + };
  40. +
  41. + spi0: spi@7e204000 {
  42. + compatible = "brcm,bcm2708-spi";
  43. + reg = <0x7e204000 0x1000>;
  44. + interrupts = <2 22>;
  45. + clocks = <&clk_spi>;
  46. + #address-cells = <1>;
  47. + #size-cells = <0>;
  48. + status = "disabled";
  49. + };
  50. +
  51. + i2c0: i2c@7e205000 {
  52. + compatible = "brcm,bcm2708-i2c";
  53. + reg = <0x7e205000 0x1000>;
  54. + interrupts = <2 21>;
  55. + clocks = <&clk_i2c>;
  56. + #address-cells = <1>;
  57. + #size-cells = <0>;
  58. + status = "disabled";
  59. + };
  60. +
  61. + i2c1: i2c@7e804000 {
  62. + compatible = "brcm,bcm2708-i2c";
  63. + reg = <0x7e804000 0x1000>;
  64. + interrupts = <2 21>;
  65. + clocks = <&clk_i2c>;
  66. + #address-cells = <1>;
  67. + #size-cells = <0>;
  68. + status = "disabled";
  69. + };
  70. + };
  71. +
  72. + clocks {
  73. + compatible = "simple-bus";
  74. + #address-cells = <1>;
  75. + #size-cells = <0>;
  76. +
  77. + clk_i2c: i2c {
  78. + compatible = "fixed-clock";
  79. + reg = <1>;
  80. + #clock-cells = <0>;
  81. + clock-frequency = <250000000>;
  82. + };
  83. +
  84. + clk_spi: clock@2 {
  85. + compatible = "fixed-clock";
  86. + reg = <2>;
  87. + #clock-cells = <0>;
  88. + clock-output-names = "spi";
  89. + clock-frequency = <250000000>;
  90. + };
  91. + };
  92. +};
  93. diff -Nur linux-3.16.2/arch/arm/boot/dts/bcm2708-rpi-b.dts linux-3.16-rpi/arch/arm/boot/dts/bcm2708-rpi-b.dts
  94. --- linux-3.16.2/arch/arm/boot/dts/bcm2708-rpi-b.dts 1970-01-01 01:00:00.000000000 +0100
  95. +++ linux-3.16-rpi/arch/arm/boot/dts/bcm2708-rpi-b.dts 2014-09-14 19:03:04.000000000 +0200
  96. @@ -0,0 +1,64 @@
  97. +/dts-v1/;
  98. +
  99. +/include/ "bcm2708.dtsi"
  100. +
  101. +/ {
  102. + compatible = "brcm,bcm2708";
  103. + model = "Raspberry Pi";
  104. +
  105. + aliases {
  106. + spi0 = &spi0;
  107. + i2c0 = &i2c0;
  108. + i2c1 = &i2c1;
  109. + };
  110. +};
  111. +
  112. +&gpio {
  113. + spi0_pins: spi0_pins {
  114. + brcm,pins = <7 8 9 10 11>;
  115. + brcm,function = <4>; /* alt0 */
  116. + };
  117. +
  118. + i2c0_pins: i2c0 {
  119. + brcm,pins = <0 1>;
  120. + brcm,function = <4>;
  121. + };
  122. +
  123. + i2c1_pins: i2c1 {
  124. + brcm,pins = <2 3>;
  125. + brcm,function = <4>;
  126. + };
  127. +};
  128. +
  129. +&spi0 {
  130. + pinctrl-names = "default";
  131. + pinctrl-0 = <&spi0_pins>;
  132. +
  133. + spidev@0{
  134. + compatible = "spidev";
  135. + reg = <0>; /* CE0 */
  136. + #address-cells = <1>;
  137. + #size-cells = <0>;
  138. + spi-max-frequency = <500000>;
  139. + };
  140. +
  141. + spidev@1{
  142. + compatible = "spidev";
  143. + reg = <1>; /* CE1 */
  144. + #address-cells = <1>;
  145. + #size-cells = <0>;
  146. + spi-max-frequency = <500000>;
  147. + };
  148. +};
  149. +
  150. +&i2c0 {
  151. + pinctrl-names = "default";
  152. + pinctrl-0 = <&i2c0_pins>;
  153. + clock-frequency = <100000>;
  154. +};
  155. +
  156. +&i2c1 {
  157. + pinctrl-names = "default";
  158. + pinctrl-0 = <&i2c1_pins>;
  159. + clock-frequency = <100000>;
  160. +};
  161. diff -Nur linux-3.16.2/arch/arm/boot/dts/Makefile linux-3.16-rpi/arch/arm/boot/dts/Makefile
  162. --- linux-3.16.2/arch/arm/boot/dts/Makefile 2014-09-06 01:37:11.000000000 +0200
  163. +++ linux-3.16-rpi/arch/arm/boot/dts/Makefile 2014-09-14 19:03:03.000000000 +0200
  164. @@ -51,6 +51,7 @@
  165. dtb-$(CONFIG_ARCH_ATLAS6) += atlas6-evb.dtb
  166. dtb-$(CONFIG_ARCH_AXXIA) += axm5516-amarillo.dtb
  167. +dtb-$(CONFIG_BCM2708_DT) += bcm2708-rpi-b.dtb
  168. dtb-$(CONFIG_ARCH_BCM2835) += bcm2835-rpi-b.dtb
  169. dtb-$(CONFIG_ARCH_BCM_5301X) += bcm4708-netgear-r6250.dtb
  170. dtb-$(CONFIG_ARCH_BCM_MOBILE) += bcm28155-ap.dtb \
  171. diff -Nur linux-3.16.2/arch/arm/configs/bcmrpi_cutdown_defconfig linux-3.16-rpi/arch/arm/configs/bcmrpi_cutdown_defconfig
  172. --- linux-3.16.2/arch/arm/configs/bcmrpi_cutdown_defconfig 1970-01-01 01:00:00.000000000 +0100
  173. +++ linux-3.16-rpi/arch/arm/configs/bcmrpi_cutdown_defconfig 2014-09-14 19:03:04.000000000 +0200
  174. @@ -0,0 +1,503 @@
  175. +CONFIG_EXPERIMENTAL=y
  176. +# CONFIG_LOCALVERSION_AUTO is not set
  177. +CONFIG_SYSVIPC=y
  178. +CONFIG_POSIX_MQUEUE=y
  179. +CONFIG_IKCONFIG=y
  180. +CONFIG_IKCONFIG_PROC=y
  181. +# CONFIG_UID16 is not set
  182. +# CONFIG_KALLSYMS is not set
  183. +CONFIG_EMBEDDED=y
  184. +# CONFIG_VM_EVENT_COUNTERS is not set
  185. +# CONFIG_COMPAT_BRK is not set
  186. +CONFIG_SLAB=y
  187. +CONFIG_MODULES=y
  188. +CONFIG_MODULE_UNLOAD=y
  189. +CONFIG_MODVERSIONS=y
  190. +CONFIG_MODULE_SRCVERSION_ALL=y
  191. +# CONFIG_BLK_DEV_BSG is not set
  192. +CONFIG_ARCH_BCM2708=y
  193. +CONFIG_NO_HZ=y
  194. +CONFIG_HIGH_RES_TIMERS=y
  195. +CONFIG_AEABI=y
  196. +CONFIG_ZBOOT_ROM_TEXT=0x0
  197. +CONFIG_ZBOOT_ROM_BSS=0x0
  198. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext3 rootwait"
  199. +CONFIG_CPU_IDLE=y
  200. +CONFIG_VFP=y
  201. +CONFIG_BINFMT_MISC=m
  202. +CONFIG_NET=y
  203. +CONFIG_PACKET=y
  204. +CONFIG_UNIX=y
  205. +CONFIG_XFRM_USER=y
  206. +CONFIG_NET_KEY=m
  207. +CONFIG_INET=y
  208. +CONFIG_IP_MULTICAST=y
  209. +CONFIG_IP_PNP=y
  210. +CONFIG_IP_PNP_DHCP=y
  211. +CONFIG_IP_PNP_RARP=y
  212. +CONFIG_SYN_COOKIES=y
  213. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  214. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  215. +# CONFIG_INET_XFRM_MODE_BEET is not set
  216. +# CONFIG_INET_LRO is not set
  217. +# CONFIG_INET_DIAG is not set
  218. +# CONFIG_IPV6 is not set
  219. +CONFIG_NET_PKTGEN=m
  220. +CONFIG_IRDA=m
  221. +CONFIG_IRLAN=m
  222. +CONFIG_IRCOMM=m
  223. +CONFIG_IRDA_ULTRA=y
  224. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  225. +CONFIG_IRDA_FAST_RR=y
  226. +CONFIG_IRTTY_SIR=m
  227. +CONFIG_KINGSUN_DONGLE=m
  228. +CONFIG_KSDAZZLE_DONGLE=m
  229. +CONFIG_KS959_DONGLE=m
  230. +CONFIG_USB_IRDA=m
  231. +CONFIG_SIGMATEL_FIR=m
  232. +CONFIG_MCS_FIR=m
  233. +CONFIG_BT=m
  234. +CONFIG_BT_L2CAP=y
  235. +CONFIG_BT_SCO=y
  236. +CONFIG_BT_RFCOMM=m
  237. +CONFIG_BT_RFCOMM_TTY=y
  238. +CONFIG_BT_BNEP=m
  239. +CONFIG_BT_BNEP_MC_FILTER=y
  240. +CONFIG_BT_BNEP_PROTO_FILTER=y
  241. +CONFIG_BT_HIDP=m
  242. +CONFIG_BT_HCIBTUSB=m
  243. +CONFIG_BT_HCIBCM203X=m
  244. +CONFIG_BT_HCIBPA10X=m
  245. +CONFIG_BT_HCIBFUSB=m
  246. +CONFIG_BT_HCIVHCI=m
  247. +CONFIG_BT_MRVL=m
  248. +CONFIG_BT_MRVL_SDIO=m
  249. +CONFIG_BT_ATH3K=m
  250. +CONFIG_CFG80211=m
  251. +CONFIG_MAC80211=m
  252. +CONFIG_MAC80211_RC_PID=y
  253. +CONFIG_MAC80211_MESH=y
  254. +CONFIG_WIMAX=m
  255. +CONFIG_NET_9P=m
  256. +CONFIG_NFC=m
  257. +CONFIG_NFC_PN533=m
  258. +CONFIG_DEVTMPFS=y
  259. +CONFIG_BLK_DEV_LOOP=y
  260. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  261. +CONFIG_BLK_DEV_NBD=m
  262. +CONFIG_BLK_DEV_RAM=y
  263. +CONFIG_CDROM_PKTCDVD=m
  264. +CONFIG_MISC_DEVICES=y
  265. +CONFIG_SCSI=y
  266. +# CONFIG_SCSI_PROC_FS is not set
  267. +CONFIG_BLK_DEV_SD=m
  268. +CONFIG_BLK_DEV_SR=m
  269. +CONFIG_SCSI_MULTI_LUN=y
  270. +# CONFIG_SCSI_LOWLEVEL is not set
  271. +CONFIG_NETDEVICES=y
  272. +CONFIG_TUN=m
  273. +CONFIG_PHYLIB=m
  274. +CONFIG_MDIO_BITBANG=m
  275. +CONFIG_NET_ETHERNET=y
  276. +# CONFIG_NETDEV_1000 is not set
  277. +# CONFIG_NETDEV_10000 is not set
  278. +CONFIG_LIBERTAS_THINFIRM=m
  279. +CONFIG_LIBERTAS_THINFIRM_USB=m
  280. +CONFIG_AT76C50X_USB=m
  281. +CONFIG_USB_ZD1201=m
  282. +CONFIG_USB_NET_RNDIS_WLAN=m
  283. +CONFIG_RTL8187=m
  284. +CONFIG_MAC80211_HWSIM=m
  285. +CONFIG_ATH_COMMON=m
  286. +CONFIG_ATH9K=m
  287. +CONFIG_ATH9K_HTC=m
  288. +CONFIG_CARL9170=m
  289. +CONFIG_B43=m
  290. +CONFIG_B43LEGACY=m
  291. +CONFIG_HOSTAP=m
  292. +CONFIG_IWM=m
  293. +CONFIG_LIBERTAS=m
  294. +CONFIG_LIBERTAS_USB=m
  295. +CONFIG_LIBERTAS_SDIO=m
  296. +CONFIG_P54_COMMON=m
  297. +CONFIG_P54_USB=m
  298. +CONFIG_RT2X00=m
  299. +CONFIG_RT2500USB=m
  300. +CONFIG_RT73USB=m
  301. +CONFIG_RT2800USB=m
  302. +CONFIG_RT2800USB_RT53XX=y
  303. +CONFIG_RTL8192CU=m
  304. +CONFIG_WL1251=m
  305. +CONFIG_WL12XX_MENU=m
  306. +CONFIG_ZD1211RW=m
  307. +CONFIG_MWIFIEX=m
  308. +CONFIG_MWIFIEX_SDIO=m
  309. +CONFIG_WIMAX_I2400M_USB=m
  310. +CONFIG_USB_CATC=m
  311. +CONFIG_USB_KAWETH=m
  312. +CONFIG_USB_PEGASUS=m
  313. +CONFIG_USB_RTL8150=m
  314. +CONFIG_USB_USBNET=y
  315. +CONFIG_USB_NET_AX8817X=m
  316. +CONFIG_USB_NET_CDCETHER=m
  317. +CONFIG_USB_NET_CDC_EEM=m
  318. +CONFIG_USB_NET_DM9601=m
  319. +CONFIG_USB_NET_SMSC75XX=m
  320. +CONFIG_USB_NET_SMSC95XX=y
  321. +CONFIG_USB_NET_GL620A=m
  322. +CONFIG_USB_NET_NET1080=m
  323. +CONFIG_USB_NET_PLUSB=m
  324. +CONFIG_USB_NET_MCS7830=m
  325. +CONFIG_USB_NET_CDC_SUBSET=m
  326. +CONFIG_USB_ALI_M5632=y
  327. +CONFIG_USB_AN2720=y
  328. +CONFIG_USB_KC2190=y
  329. +# CONFIG_USB_NET_ZAURUS is not set
  330. +CONFIG_USB_NET_CX82310_ETH=m
  331. +CONFIG_USB_NET_KALMIA=m
  332. +CONFIG_USB_NET_INT51X1=m
  333. +CONFIG_USB_IPHETH=m
  334. +CONFIG_USB_SIERRA_NET=m
  335. +CONFIG_USB_VL600=m
  336. +CONFIG_PPP=m
  337. +CONFIG_PPP_ASYNC=m
  338. +CONFIG_PPP_SYNC_TTY=m
  339. +CONFIG_PPP_DEFLATE=m
  340. +CONFIG_PPP_BSDCOMP=m
  341. +CONFIG_SLIP=m
  342. +CONFIG_SLIP_COMPRESSED=y
  343. +CONFIG_NETCONSOLE=m
  344. +CONFIG_INPUT_POLLDEV=m
  345. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  346. +CONFIG_INPUT_JOYDEV=m
  347. +CONFIG_INPUT_EVDEV=m
  348. +# CONFIG_INPUT_KEYBOARD is not set
  349. +# CONFIG_INPUT_MOUSE is not set
  350. +CONFIG_INPUT_MISC=y
  351. +CONFIG_INPUT_AD714X=m
  352. +CONFIG_INPUT_ATI_REMOTE=m
  353. +CONFIG_INPUT_ATI_REMOTE2=m
  354. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  355. +CONFIG_INPUT_POWERMATE=m
  356. +CONFIG_INPUT_YEALINK=m
  357. +CONFIG_INPUT_CM109=m
  358. +CONFIG_INPUT_UINPUT=m
  359. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  360. +CONFIG_INPUT_ADXL34X=m
  361. +CONFIG_INPUT_CMA3000=m
  362. +CONFIG_SERIO=m
  363. +CONFIG_SERIO_RAW=m
  364. +CONFIG_GAMEPORT=m
  365. +CONFIG_GAMEPORT_NS558=m
  366. +CONFIG_GAMEPORT_L4=m
  367. +CONFIG_VT_HW_CONSOLE_BINDING=y
  368. +# CONFIG_LEGACY_PTYS is not set
  369. +# CONFIG_DEVKMEM is not set
  370. +CONFIG_SERIAL_AMBA_PL011=y
  371. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  372. +# CONFIG_HW_RANDOM is not set
  373. +CONFIG_RAW_DRIVER=y
  374. +CONFIG_GPIO_SYSFS=y
  375. +# CONFIG_HWMON is not set
  376. +CONFIG_WATCHDOG=y
  377. +CONFIG_BCM2708_WDT=m
  378. +# CONFIG_MFD_SUPPORT is not set
  379. +CONFIG_FB=y
  380. +CONFIG_FB_BCM2708=y
  381. +CONFIG_FRAMEBUFFER_CONSOLE=y
  382. +CONFIG_LOGO=y
  383. +# CONFIG_LOGO_LINUX_MONO is not set
  384. +# CONFIG_LOGO_LINUX_VGA16 is not set
  385. +CONFIG_SOUND=y
  386. +CONFIG_SND=m
  387. +CONFIG_SND_SEQUENCER=m
  388. +CONFIG_SND_SEQ_DUMMY=m
  389. +CONFIG_SND_MIXER_OSS=m
  390. +CONFIG_SND_PCM_OSS=m
  391. +CONFIG_SND_SEQUENCER_OSS=y
  392. +CONFIG_SND_HRTIMER=m
  393. +CONFIG_SND_DUMMY=m
  394. +CONFIG_SND_ALOOP=m
  395. +CONFIG_SND_VIRMIDI=m
  396. +CONFIG_SND_MTPAV=m
  397. +CONFIG_SND_SERIAL_U16550=m
  398. +CONFIG_SND_MPU401=m
  399. +CONFIG_SND_BCM2835=m
  400. +CONFIG_SND_USB_AUDIO=m
  401. +CONFIG_SND_USB_UA101=m
  402. +CONFIG_SND_USB_CAIAQ=m
  403. +CONFIG_SND_USB_6FIRE=m
  404. +CONFIG_SOUND_PRIME=m
  405. +CONFIG_HID_PID=y
  406. +CONFIG_USB_HIDDEV=y
  407. +CONFIG_HID_A4TECH=m
  408. +CONFIG_HID_ACRUX=m
  409. +CONFIG_HID_APPLE=m
  410. +CONFIG_HID_BELKIN=m
  411. +CONFIG_HID_CHERRY=m
  412. +CONFIG_HID_CHICONY=m
  413. +CONFIG_HID_CYPRESS=m
  414. +CONFIG_HID_DRAGONRISE=m
  415. +CONFIG_HID_EMS_FF=m
  416. +CONFIG_HID_ELECOM=m
  417. +CONFIG_HID_EZKEY=m
  418. +CONFIG_HID_HOLTEK=m
  419. +CONFIG_HID_KEYTOUCH=m
  420. +CONFIG_HID_KYE=m
  421. +CONFIG_HID_UCLOGIC=m
  422. +CONFIG_HID_WALTOP=m
  423. +CONFIG_HID_GYRATION=m
  424. +CONFIG_HID_TWINHAN=m
  425. +CONFIG_HID_KENSINGTON=m
  426. +CONFIG_HID_LCPOWER=m
  427. +CONFIG_HID_LOGITECH=m
  428. +CONFIG_HID_MAGICMOUSE=m
  429. +CONFIG_HID_MICROSOFT=m
  430. +CONFIG_HID_MONTEREY=m
  431. +CONFIG_HID_MULTITOUCH=m
  432. +CONFIG_HID_NTRIG=m
  433. +CONFIG_HID_ORTEK=m
  434. +CONFIG_HID_PANTHERLORD=m
  435. +CONFIG_HID_PETALYNX=m
  436. +CONFIG_HID_PICOLCD=m
  437. +CONFIG_HID_QUANTA=m
  438. +CONFIG_HID_ROCCAT=m
  439. +CONFIG_HID_SAMSUNG=m
  440. +CONFIG_HID_SONY=m
  441. +CONFIG_HID_SPEEDLINK=m
  442. +CONFIG_HID_SUNPLUS=m
  443. +CONFIG_HID_GREENASIA=m
  444. +CONFIG_HID_SMARTJOYPLUS=m
  445. +CONFIG_HID_TOPSEED=m
  446. +CONFIG_HID_THRUSTMASTER=m
  447. +CONFIG_HID_WACOM=m
  448. +CONFIG_HID_WIIMOTE=m
  449. +CONFIG_HID_ZEROPLUS=m
  450. +CONFIG_HID_ZYDACRON=m
  451. +CONFIG_USB=y
  452. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  453. +CONFIG_USB_MON=m
  454. +CONFIG_USB_DWCOTG=y
  455. +CONFIG_USB_STORAGE=y
  456. +CONFIG_USB_STORAGE_REALTEK=m
  457. +CONFIG_USB_STORAGE_DATAFAB=m
  458. +CONFIG_USB_STORAGE_FREECOM=m
  459. +CONFIG_USB_STORAGE_ISD200=m
  460. +CONFIG_USB_STORAGE_USBAT=m
  461. +CONFIG_USB_STORAGE_SDDR09=m
  462. +CONFIG_USB_STORAGE_SDDR55=m
  463. +CONFIG_USB_STORAGE_JUMPSHOT=m
  464. +CONFIG_USB_STORAGE_ALAUDA=m
  465. +CONFIG_USB_STORAGE_ONETOUCH=m
  466. +CONFIG_USB_STORAGE_KARMA=m
  467. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  468. +CONFIG_USB_STORAGE_ENE_UB6250=m
  469. +CONFIG_USB_UAS=m
  470. +CONFIG_USB_LIBUSUAL=y
  471. +CONFIG_USB_MDC800=m
  472. +CONFIG_USB_MICROTEK=m
  473. +CONFIG_USB_SERIAL=m
  474. +CONFIG_USB_SERIAL_GENERIC=y
  475. +CONFIG_USB_SERIAL_AIRCABLE=m
  476. +CONFIG_USB_SERIAL_ARK3116=m
  477. +CONFIG_USB_SERIAL_BELKIN=m
  478. +CONFIG_USB_SERIAL_CH341=m
  479. +CONFIG_USB_SERIAL_WHITEHEAT=m
  480. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  481. +CONFIG_USB_SERIAL_CP210X=m
  482. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  483. +CONFIG_USB_SERIAL_EMPEG=m
  484. +CONFIG_USB_SERIAL_FTDI_SIO=m
  485. +CONFIG_USB_SERIAL_FUNSOFT=m
  486. +CONFIG_USB_SERIAL_VISOR=m
  487. +CONFIG_USB_SERIAL_IPAQ=m
  488. +CONFIG_USB_SERIAL_IR=m
  489. +CONFIG_USB_SERIAL_EDGEPORT=m
  490. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  491. +CONFIG_USB_SERIAL_GARMIN=m
  492. +CONFIG_USB_SERIAL_IPW=m
  493. +CONFIG_USB_SERIAL_IUU=m
  494. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  495. +CONFIG_USB_SERIAL_KEYSPAN=m
  496. +CONFIG_USB_SERIAL_KLSI=m
  497. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  498. +CONFIG_USB_SERIAL_MCT_U232=m
  499. +CONFIG_USB_SERIAL_MOS7720=m
  500. +CONFIG_USB_SERIAL_MOS7840=m
  501. +CONFIG_USB_SERIAL_MOTOROLA=m
  502. +CONFIG_USB_SERIAL_NAVMAN=m
  503. +CONFIG_USB_SERIAL_PL2303=m
  504. +CONFIG_USB_SERIAL_OTI6858=m
  505. +CONFIG_USB_SERIAL_QCAUX=m
  506. +CONFIG_USB_SERIAL_QUALCOMM=m
  507. +CONFIG_USB_SERIAL_SPCP8X5=m
  508. +CONFIG_USB_SERIAL_HP4X=m
  509. +CONFIG_USB_SERIAL_SAFE=m
  510. +CONFIG_USB_SERIAL_SIEMENS_MPI=m
  511. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  512. +CONFIG_USB_SERIAL_SYMBOL=m
  513. +CONFIG_USB_SERIAL_TI=m
  514. +CONFIG_USB_SERIAL_CYBERJACK=m
  515. +CONFIG_USB_SERIAL_XIRCOM=m
  516. +CONFIG_USB_SERIAL_OPTION=m
  517. +CONFIG_USB_SERIAL_OMNINET=m
  518. +CONFIG_USB_SERIAL_OPTICON=m
  519. +CONFIG_USB_SERIAL_VIVOPAY_SERIAL=m
  520. +CONFIG_USB_SERIAL_ZIO=m
  521. +CONFIG_USB_SERIAL_SSU100=m
  522. +CONFIG_USB_SERIAL_DEBUG=m
  523. +CONFIG_USB_EMI62=m
  524. +CONFIG_USB_EMI26=m
  525. +CONFIG_USB_ADUTUX=m
  526. +CONFIG_USB_SEVSEG=m
  527. +CONFIG_USB_RIO500=m
  528. +CONFIG_USB_LEGOTOWER=m
  529. +CONFIG_USB_LCD=m
  530. +CONFIG_USB_LED=m
  531. +CONFIG_USB_CYPRESS_CY7C63=m
  532. +CONFIG_USB_CYTHERM=m
  533. +CONFIG_USB_IDMOUSE=m
  534. +CONFIG_USB_FTDI_ELAN=m
  535. +CONFIG_USB_APPLEDISPLAY=m
  536. +CONFIG_USB_LD=m
  537. +CONFIG_USB_TRANCEVIBRATOR=m
  538. +CONFIG_USB_IOWARRIOR=m
  539. +CONFIG_USB_TEST=m
  540. +CONFIG_USB_ISIGHTFW=m
  541. +CONFIG_USB_YUREX=m
  542. +CONFIG_MMC=y
  543. +CONFIG_MMC_SDHCI=y
  544. +CONFIG_MMC_SDHCI_PLTFM=y
  545. +CONFIG_MMC_SDHCI_BCM2708=y
  546. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  547. +CONFIG_LEDS_GPIO=y
  548. +CONFIG_LEDS_TRIGGER_TIMER=m
  549. +CONFIG_LEDS_TRIGGER_HEARTBEAT=m
  550. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=m
  551. +CONFIG_UIO=m
  552. +CONFIG_UIO_PDRV=m
  553. +CONFIG_UIO_PDRV_GENIRQ=m
  554. +# CONFIG_IOMMU_SUPPORT is not set
  555. +CONFIG_EXT4_FS=y
  556. +CONFIG_EXT4_FS_POSIX_ACL=y
  557. +CONFIG_EXT4_FS_SECURITY=y
  558. +CONFIG_REISERFS_FS=m
  559. +CONFIG_REISERFS_FS_XATTR=y
  560. +CONFIG_REISERFS_FS_POSIX_ACL=y
  561. +CONFIG_REISERFS_FS_SECURITY=y
  562. +CONFIG_JFS_FS=m
  563. +CONFIG_JFS_POSIX_ACL=y
  564. +CONFIG_JFS_SECURITY=y
  565. +CONFIG_XFS_FS=m
  566. +CONFIG_XFS_QUOTA=y
  567. +CONFIG_XFS_POSIX_ACL=y
  568. +CONFIG_XFS_RT=y
  569. +CONFIG_GFS2_FS=m
  570. +CONFIG_OCFS2_FS=m
  571. +CONFIG_BTRFS_FS=m
  572. +CONFIG_BTRFS_FS_POSIX_ACL=y
  573. +CONFIG_NILFS2_FS=m
  574. +CONFIG_AUTOFS4_FS=y
  575. +CONFIG_FUSE_FS=m
  576. +CONFIG_CUSE=m
  577. +CONFIG_FSCACHE=y
  578. +CONFIG_CACHEFILES=y
  579. +CONFIG_ISO9660_FS=m
  580. +CONFIG_JOLIET=y
  581. +CONFIG_ZISOFS=y
  582. +CONFIG_UDF_FS=m
  583. +CONFIG_MSDOS_FS=y
  584. +CONFIG_VFAT_FS=y
  585. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  586. +CONFIG_NTFS_FS=m
  587. +CONFIG_TMPFS=y
  588. +CONFIG_TMPFS_POSIX_ACL=y
  589. +CONFIG_CONFIGFS_FS=y
  590. +CONFIG_SQUASHFS=m
  591. +CONFIG_SQUASHFS_XATTR=y
  592. +CONFIG_SQUASHFS_LZO=y
  593. +CONFIG_SQUASHFS_XZ=y
  594. +CONFIG_NFS_FS=y
  595. +CONFIG_NFS_V3=y
  596. +CONFIG_NFS_V3_ACL=y
  597. +CONFIG_NFS_V4=y
  598. +CONFIG_ROOT_NFS=y
  599. +CONFIG_NFS_FSCACHE=y
  600. +CONFIG_CIFS=m
  601. +CONFIG_CIFS_WEAK_PW_HASH=y
  602. +CONFIG_CIFS_XATTR=y
  603. +CONFIG_CIFS_POSIX=y
  604. +CONFIG_9P_FS=m
  605. +CONFIG_PARTITION_ADVANCED=y
  606. +CONFIG_MAC_PARTITION=y
  607. +CONFIG_EFI_PARTITION=y
  608. +CONFIG_NLS_DEFAULT="utf8"
  609. +CONFIG_NLS_CODEPAGE_437=y
  610. +CONFIG_NLS_CODEPAGE_737=m
  611. +CONFIG_NLS_CODEPAGE_775=m
  612. +CONFIG_NLS_CODEPAGE_850=m
  613. +CONFIG_NLS_CODEPAGE_852=m
  614. +CONFIG_NLS_CODEPAGE_855=m
  615. +CONFIG_NLS_CODEPAGE_857=m
  616. +CONFIG_NLS_CODEPAGE_860=m
  617. +CONFIG_NLS_CODEPAGE_861=m
  618. +CONFIG_NLS_CODEPAGE_862=m
  619. +CONFIG_NLS_CODEPAGE_863=m
  620. +CONFIG_NLS_CODEPAGE_864=m
  621. +CONFIG_NLS_CODEPAGE_865=m
  622. +CONFIG_NLS_CODEPAGE_866=m
  623. +CONFIG_NLS_CODEPAGE_869=m
  624. +CONFIG_NLS_CODEPAGE_936=m
  625. +CONFIG_NLS_CODEPAGE_950=m
  626. +CONFIG_NLS_CODEPAGE_932=m
  627. +CONFIG_NLS_CODEPAGE_949=m
  628. +CONFIG_NLS_CODEPAGE_874=m
  629. +CONFIG_NLS_ISO8859_8=m
  630. +CONFIG_NLS_CODEPAGE_1250=m
  631. +CONFIG_NLS_CODEPAGE_1251=m
  632. +CONFIG_NLS_ASCII=y
  633. +CONFIG_NLS_ISO8859_1=m
  634. +CONFIG_NLS_ISO8859_2=m
  635. +CONFIG_NLS_ISO8859_3=m
  636. +CONFIG_NLS_ISO8859_4=m
  637. +CONFIG_NLS_ISO8859_5=m
  638. +CONFIG_NLS_ISO8859_6=m
  639. +CONFIG_NLS_ISO8859_7=m
  640. +CONFIG_NLS_ISO8859_9=m
  641. +CONFIG_NLS_ISO8859_13=m
  642. +CONFIG_NLS_ISO8859_14=m
  643. +CONFIG_NLS_ISO8859_15=m
  644. +CONFIG_NLS_KOI8_R=m
  645. +CONFIG_NLS_KOI8_U=m
  646. +CONFIG_NLS_UTF8=m
  647. +# CONFIG_SCHED_DEBUG is not set
  648. +# CONFIG_DEBUG_BUGVERBOSE is not set
  649. +# CONFIG_FTRACE is not set
  650. +# CONFIG_ARM_UNWIND is not set
  651. +CONFIG_CRYPTO_AUTHENC=m
  652. +CONFIG_CRYPTO_SEQIV=m
  653. +CONFIG_CRYPTO_CBC=y
  654. +CONFIG_CRYPTO_HMAC=y
  655. +CONFIG_CRYPTO_XCBC=m
  656. +CONFIG_CRYPTO_MD5=y
  657. +CONFIG_CRYPTO_SHA1=y
  658. +CONFIG_CRYPTO_SHA256=m
  659. +CONFIG_CRYPTO_SHA512=m
  660. +CONFIG_CRYPTO_TGR192=m
  661. +CONFIG_CRYPTO_WP512=m
  662. +CONFIG_CRYPTO_CAST5=m
  663. +CONFIG_CRYPTO_DES=y
  664. +CONFIG_CRYPTO_DEFLATE=m
  665. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  666. +# CONFIG_CRYPTO_HW is not set
  667. +CONFIG_CRC_ITU_T=y
  668. +CONFIG_LIBCRC32C=y
  669. +CONFIG_I2C=y
  670. +CONFIG_I2C_BOARDINFO=y
  671. +CONFIG_I2C_COMPAT=y
  672. +CONFIG_I2C_CHARDEV=m
  673. +CONFIG_I2C_HELPER_AUTO=y
  674. +CONFIG_I2C_BCM2708=m
  675. +CONFIG_SPI=y
  676. +CONFIG_SPI_MASTER=y
  677. +CONFIG_SPI_BCM2708=m
  678. diff -Nur linux-3.16.2/arch/arm/configs/bcmrpi_defconfig linux-3.16-rpi/arch/arm/configs/bcmrpi_defconfig
  679. --- linux-3.16.2/arch/arm/configs/bcmrpi_defconfig 1970-01-01 01:00:00.000000000 +0100
  680. +++ linux-3.16-rpi/arch/arm/configs/bcmrpi_defconfig 2014-09-14 19:03:04.000000000 +0200
  681. @@ -0,0 +1,1105 @@
  682. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  683. +# CONFIG_LOCALVERSION_AUTO is not set
  684. +CONFIG_SYSVIPC=y
  685. +CONFIG_POSIX_MQUEUE=y
  686. +CONFIG_FHANDLE=y
  687. +CONFIG_AUDIT=y
  688. +CONFIG_NO_HZ=y
  689. +CONFIG_HIGH_RES_TIMERS=y
  690. +CONFIG_BSD_PROCESS_ACCT=y
  691. +CONFIG_BSD_PROCESS_ACCT_V3=y
  692. +CONFIG_TASKSTATS=y
  693. +CONFIG_TASK_DELAY_ACCT=y
  694. +CONFIG_TASK_XACCT=y
  695. +CONFIG_TASK_IO_ACCOUNTING=y
  696. +CONFIG_IKCONFIG=y
  697. +CONFIG_IKCONFIG_PROC=y
  698. +CONFIG_CGROUP_FREEZER=y
  699. +CONFIG_CGROUP_DEVICE=y
  700. +CONFIG_CGROUP_CPUACCT=y
  701. +CONFIG_RESOURCE_COUNTERS=y
  702. +CONFIG_MEMCG=y
  703. +CONFIG_BLK_CGROUP=y
  704. +CONFIG_NAMESPACES=y
  705. +CONFIG_SCHED_AUTOGROUP=y
  706. +CONFIG_RELAY=y
  707. +CONFIG_BLK_DEV_INITRD=y
  708. +CONFIG_EMBEDDED=y
  709. +# CONFIG_COMPAT_BRK is not set
  710. +CONFIG_PROFILING=y
  711. +CONFIG_OPROFILE=m
  712. +CONFIG_KPROBES=y
  713. +CONFIG_JUMP_LABEL=y
  714. +CONFIG_MODULES=y
  715. +CONFIG_MODULE_UNLOAD=y
  716. +CONFIG_MODVERSIONS=y
  717. +CONFIG_MODULE_SRCVERSION_ALL=y
  718. +CONFIG_BLK_DEV_THROTTLING=y
  719. +CONFIG_PARTITION_ADVANCED=y
  720. +CONFIG_MAC_PARTITION=y
  721. +CONFIG_CFQ_GROUP_IOSCHED=y
  722. +CONFIG_ARCH_BCM2708=y
  723. +CONFIG_PREEMPT=y
  724. +CONFIG_AEABI=y
  725. +CONFIG_CLEANCACHE=y
  726. +CONFIG_FRONTSWAP=y
  727. +CONFIG_CMA=y
  728. +CONFIG_UACCESS_WITH_MEMCPY=y
  729. +CONFIG_SECCOMP=y
  730. +CONFIG_ZBOOT_ROM_TEXT=0x0
  731. +CONFIG_ZBOOT_ROM_BSS=0x0
  732. +CONFIG_CMDLINE="console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  733. +CONFIG_KEXEC=y
  734. +CONFIG_CPU_FREQ=y
  735. +CONFIG_CPU_FREQ_STAT=m
  736. +CONFIG_CPU_FREQ_STAT_DETAILS=y
  737. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  738. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  739. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  740. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  741. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  742. +CONFIG_CPU_IDLE=y
  743. +CONFIG_VFP=y
  744. +CONFIG_BINFMT_MISC=m
  745. +CONFIG_NET=y
  746. +CONFIG_PACKET=y
  747. +CONFIG_UNIX=y
  748. +CONFIG_XFRM_USER=y
  749. +CONFIG_NET_KEY=m
  750. +CONFIG_INET=y
  751. +CONFIG_IP_MULTICAST=y
  752. +CONFIG_IP_ADVANCED_ROUTER=y
  753. +CONFIG_IP_MULTIPLE_TABLES=y
  754. +CONFIG_IP_ROUTE_MULTIPATH=y
  755. +CONFIG_IP_ROUTE_VERBOSE=y
  756. +CONFIG_IP_PNP=y
  757. +CONFIG_IP_PNP_DHCP=y
  758. +CONFIG_IP_PNP_RARP=y
  759. +CONFIG_NET_IPIP=m
  760. +CONFIG_NET_IPGRE_DEMUX=m
  761. +CONFIG_NET_IPGRE=m
  762. +CONFIG_IP_MROUTE=y
  763. +CONFIG_IP_MROUTE_MULTIPLE_TABLES=y
  764. +CONFIG_IP_PIMSM_V1=y
  765. +CONFIG_IP_PIMSM_V2=y
  766. +CONFIG_SYN_COOKIES=y
  767. +CONFIG_INET_AH=m
  768. +CONFIG_INET_ESP=m
  769. +CONFIG_INET_IPCOMP=m
  770. +CONFIG_INET_XFRM_MODE_TRANSPORT=m
  771. +CONFIG_INET_XFRM_MODE_TUNNEL=m
  772. +CONFIG_INET_XFRM_MODE_BEET=m
  773. +CONFIG_INET_LRO=m
  774. +CONFIG_INET_DIAG=m
  775. +CONFIG_INET6_AH=m
  776. +CONFIG_INET6_ESP=m
  777. +CONFIG_INET6_IPCOMP=m
  778. +CONFIG_IPV6_TUNNEL=m
  779. +CONFIG_IPV6_MULTIPLE_TABLES=y
  780. +CONFIG_IPV6_MROUTE=y
  781. +CONFIG_IPV6_MROUTE_MULTIPLE_TABLES=y
  782. +CONFIG_IPV6_PIMSM_V2=y
  783. +CONFIG_NETFILTER=y
  784. +CONFIG_NF_CONNTRACK=m
  785. +CONFIG_NF_CONNTRACK_ZONES=y
  786. +CONFIG_NF_CONNTRACK_EVENTS=y
  787. +CONFIG_NF_CONNTRACK_TIMESTAMP=y
  788. +CONFIG_NF_CT_PROTO_DCCP=m
  789. +CONFIG_NF_CT_PROTO_UDPLITE=m
  790. +CONFIG_NF_CONNTRACK_AMANDA=m
  791. +CONFIG_NF_CONNTRACK_FTP=m
  792. +CONFIG_NF_CONNTRACK_H323=m
  793. +CONFIG_NF_CONNTRACK_IRC=m
  794. +CONFIG_NF_CONNTRACK_NETBIOS_NS=m
  795. +CONFIG_NF_CONNTRACK_SNMP=m
  796. +CONFIG_NF_CONNTRACK_PPTP=m
  797. +CONFIG_NF_CONNTRACK_SANE=m
  798. +CONFIG_NF_CONNTRACK_SIP=m
  799. +CONFIG_NF_CONNTRACK_TFTP=m
  800. +CONFIG_NF_CT_NETLINK=m
  801. +CONFIG_NETFILTER_XT_SET=m
  802. +CONFIG_NETFILTER_XT_TARGET_AUDIT=m
  803. +CONFIG_NETFILTER_XT_TARGET_CHECKSUM=m
  804. +CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
  805. +CONFIG_NETFILTER_XT_TARGET_CONNMARK=m
  806. +CONFIG_NETFILTER_XT_TARGET_DSCP=m
  807. +CONFIG_NETFILTER_XT_TARGET_HMARK=m
  808. +CONFIG_NETFILTER_XT_TARGET_IDLETIMER=m
  809. +CONFIG_NETFILTER_XT_TARGET_LED=m
  810. +CONFIG_NETFILTER_XT_TARGET_LOG=m
  811. +CONFIG_NETFILTER_XT_TARGET_MARK=m
  812. +CONFIG_NETFILTER_XT_TARGET_NFLOG=m
  813. +CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
  814. +CONFIG_NETFILTER_XT_TARGET_NOTRACK=m
  815. +CONFIG_NETFILTER_XT_TARGET_TEE=m
  816. +CONFIG_NETFILTER_XT_TARGET_TPROXY=m
  817. +CONFIG_NETFILTER_XT_TARGET_TRACE=m
  818. +CONFIG_NETFILTER_XT_TARGET_TCPMSS=m
  819. +CONFIG_NETFILTER_XT_TARGET_TCPOPTSTRIP=m
  820. +CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=m
  821. +CONFIG_NETFILTER_XT_MATCH_BPF=m
  822. +CONFIG_NETFILTER_XT_MATCH_CLUSTER=m
  823. +CONFIG_NETFILTER_XT_MATCH_COMMENT=m
  824. +CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
  825. +CONFIG_NETFILTER_XT_MATCH_CONNLABEL=m
  826. +CONFIG_NETFILTER_XT_MATCH_CONNLIMIT=m
  827. +CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
  828. +CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
  829. +CONFIG_NETFILTER_XT_MATCH_CPU=m
  830. +CONFIG_NETFILTER_XT_MATCH_DCCP=m
  831. +CONFIG_NETFILTER_XT_MATCH_DEVGROUP=m
  832. +CONFIG_NETFILTER_XT_MATCH_DSCP=m
  833. +CONFIG_NETFILTER_XT_MATCH_ESP=m
  834. +CONFIG_NETFILTER_XT_MATCH_HASHLIMIT=m
  835. +CONFIG_NETFILTER_XT_MATCH_HELPER=m
  836. +CONFIG_NETFILTER_XT_MATCH_IPRANGE=m
  837. +CONFIG_NETFILTER_XT_MATCH_IPVS=m
  838. +CONFIG_NETFILTER_XT_MATCH_LENGTH=m
  839. +CONFIG_NETFILTER_XT_MATCH_LIMIT=m
  840. +CONFIG_NETFILTER_XT_MATCH_MAC=m
  841. +CONFIG_NETFILTER_XT_MATCH_MARK=m
  842. +CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
  843. +CONFIG_NETFILTER_XT_MATCH_NFACCT=m
  844. +CONFIG_NETFILTER_XT_MATCH_OSF=m
  845. +CONFIG_NETFILTER_XT_MATCH_OWNER=m
  846. +CONFIG_NETFILTER_XT_MATCH_POLICY=m
  847. +CONFIG_NETFILTER_XT_MATCH_PHYSDEV=m
  848. +CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
  849. +CONFIG_NETFILTER_XT_MATCH_QUOTA=m
  850. +CONFIG_NETFILTER_XT_MATCH_RATEEST=m
  851. +CONFIG_NETFILTER_XT_MATCH_REALM=m
  852. +CONFIG_NETFILTER_XT_MATCH_RECENT=m
  853. +CONFIG_NETFILTER_XT_MATCH_SOCKET=m
  854. +CONFIG_NETFILTER_XT_MATCH_STATE=m
  855. +CONFIG_NETFILTER_XT_MATCH_STATISTIC=m
  856. +CONFIG_NETFILTER_XT_MATCH_STRING=m
  857. +CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
  858. +CONFIG_NETFILTER_XT_MATCH_TIME=m
  859. +CONFIG_NETFILTER_XT_MATCH_U32=m
  860. +CONFIG_IP_SET=m
  861. +CONFIG_IP_SET_BITMAP_IP=m
  862. +CONFIG_IP_SET_BITMAP_IPMAC=m
  863. +CONFIG_IP_SET_BITMAP_PORT=m
  864. +CONFIG_IP_SET_HASH_IP=m
  865. +CONFIG_IP_SET_HASH_IPPORT=m
  866. +CONFIG_IP_SET_HASH_IPPORTIP=m
  867. +CONFIG_IP_SET_HASH_IPPORTNET=m
  868. +CONFIG_IP_SET_HASH_NET=m
  869. +CONFIG_IP_SET_HASH_NETPORT=m
  870. +CONFIG_IP_SET_HASH_NETIFACE=m
  871. +CONFIG_IP_SET_LIST_SET=m
  872. +CONFIG_IP_VS=m
  873. +CONFIG_IP_VS_PROTO_TCP=y
  874. +CONFIG_IP_VS_PROTO_UDP=y
  875. +CONFIG_IP_VS_PROTO_ESP=y
  876. +CONFIG_IP_VS_PROTO_AH=y
  877. +CONFIG_IP_VS_PROTO_SCTP=y
  878. +CONFIG_IP_VS_RR=m
  879. +CONFIG_IP_VS_WRR=m
  880. +CONFIG_IP_VS_LC=m
  881. +CONFIG_IP_VS_WLC=m
  882. +CONFIG_IP_VS_LBLC=m
  883. +CONFIG_IP_VS_LBLCR=m
  884. +CONFIG_IP_VS_DH=m
  885. +CONFIG_IP_VS_SH=m
  886. +CONFIG_IP_VS_SED=m
  887. +CONFIG_IP_VS_NQ=m
  888. +CONFIG_IP_VS_FTP=m
  889. +CONFIG_IP_VS_PE_SIP=m
  890. +CONFIG_NF_CONNTRACK_IPV4=m
  891. +CONFIG_IP_NF_IPTABLES=m
  892. +CONFIG_IP_NF_MATCH_AH=m
  893. +CONFIG_IP_NF_MATCH_ECN=m
  894. +CONFIG_IP_NF_MATCH_TTL=m
  895. +CONFIG_IP_NF_FILTER=m
  896. +CONFIG_IP_NF_TARGET_REJECT=m
  897. +CONFIG_IP_NF_TARGET_ULOG=m
  898. +CONFIG_NF_NAT_IPV4=m
  899. +CONFIG_IP_NF_TARGET_MASQUERADE=m
  900. +CONFIG_IP_NF_TARGET_NETMAP=m
  901. +CONFIG_IP_NF_TARGET_REDIRECT=m
  902. +CONFIG_IP_NF_MANGLE=m
  903. +CONFIG_IP_NF_TARGET_ECN=m
  904. +CONFIG_IP_NF_TARGET_TTL=m
  905. +CONFIG_IP_NF_RAW=m
  906. +CONFIG_IP_NF_ARPTABLES=m
  907. +CONFIG_IP_NF_ARPFILTER=m
  908. +CONFIG_IP_NF_ARP_MANGLE=m
  909. +CONFIG_NF_CONNTRACK_IPV6=m
  910. +CONFIG_IP6_NF_IPTABLES=m
  911. +CONFIG_IP6_NF_MATCH_AH=m
  912. +CONFIG_IP6_NF_MATCH_EUI64=m
  913. +CONFIG_IP6_NF_MATCH_FRAG=m
  914. +CONFIG_IP6_NF_MATCH_OPTS=m
  915. +CONFIG_IP6_NF_MATCH_HL=m
  916. +CONFIG_IP6_NF_MATCH_IPV6HEADER=m
  917. +CONFIG_IP6_NF_MATCH_MH=m
  918. +CONFIG_IP6_NF_MATCH_RT=m
  919. +CONFIG_IP6_NF_TARGET_HL=m
  920. +CONFIG_IP6_NF_FILTER=m
  921. +CONFIG_IP6_NF_TARGET_REJECT=m
  922. +CONFIG_IP6_NF_MANGLE=m
  923. +CONFIG_IP6_NF_RAW=m
  924. +CONFIG_NF_NAT_IPV6=m
  925. +CONFIG_IP6_NF_TARGET_MASQUERADE=m
  926. +CONFIG_IP6_NF_TARGET_NPT=m
  927. +CONFIG_BRIDGE_NF_EBTABLES=m
  928. +CONFIG_BRIDGE_EBT_BROUTE=m
  929. +CONFIG_BRIDGE_EBT_T_FILTER=m
  930. +CONFIG_BRIDGE_EBT_T_NAT=m
  931. +CONFIG_BRIDGE_EBT_802_3=m
  932. +CONFIG_BRIDGE_EBT_AMONG=m
  933. +CONFIG_BRIDGE_EBT_ARP=m
  934. +CONFIG_BRIDGE_EBT_IP=m
  935. +CONFIG_BRIDGE_EBT_IP6=m
  936. +CONFIG_BRIDGE_EBT_LIMIT=m
  937. +CONFIG_BRIDGE_EBT_MARK=m
  938. +CONFIG_BRIDGE_EBT_PKTTYPE=m
  939. +CONFIG_BRIDGE_EBT_STP=m
  940. +CONFIG_BRIDGE_EBT_VLAN=m
  941. +CONFIG_BRIDGE_EBT_ARPREPLY=m
  942. +CONFIG_BRIDGE_EBT_DNAT=m
  943. +CONFIG_BRIDGE_EBT_MARK_T=m
  944. +CONFIG_BRIDGE_EBT_REDIRECT=m
  945. +CONFIG_BRIDGE_EBT_SNAT=m
  946. +CONFIG_BRIDGE_EBT_LOG=m
  947. +CONFIG_BRIDGE_EBT_ULOG=m
  948. +CONFIG_BRIDGE_EBT_NFLOG=m
  949. +CONFIG_SCTP_COOKIE_HMAC_SHA1=y
  950. +CONFIG_L2TP=m
  951. +CONFIG_L2TP_V3=y
  952. +CONFIG_L2TP_IP=m
  953. +CONFIG_L2TP_ETH=m
  954. +CONFIG_BRIDGE=m
  955. +CONFIG_VLAN_8021Q=m
  956. +CONFIG_VLAN_8021Q_GVRP=y
  957. +CONFIG_ATALK=m
  958. +CONFIG_NET_SCHED=y
  959. +CONFIG_NET_SCH_CBQ=m
  960. +CONFIG_NET_SCH_HTB=m
  961. +CONFIG_NET_SCH_HFSC=m
  962. +CONFIG_NET_SCH_PRIO=m
  963. +CONFIG_NET_SCH_MULTIQ=m
  964. +CONFIG_NET_SCH_RED=m
  965. +CONFIG_NET_SCH_SFB=m
  966. +CONFIG_NET_SCH_SFQ=m
  967. +CONFIG_NET_SCH_TEQL=m
  968. +CONFIG_NET_SCH_TBF=m
  969. +CONFIG_NET_SCH_GRED=m
  970. +CONFIG_NET_SCH_DSMARK=m
  971. +CONFIG_NET_SCH_NETEM=m
  972. +CONFIG_NET_SCH_DRR=m
  973. +CONFIG_NET_SCH_MQPRIO=m
  974. +CONFIG_NET_SCH_CHOKE=m
  975. +CONFIG_NET_SCH_QFQ=m
  976. +CONFIG_NET_SCH_CODEL=m
  977. +CONFIG_NET_SCH_FQ_CODEL=m
  978. +CONFIG_NET_SCH_INGRESS=m
  979. +CONFIG_NET_SCH_PLUG=m
  980. +CONFIG_NET_CLS_BASIC=m
  981. +CONFIG_NET_CLS_TCINDEX=m
  982. +CONFIG_NET_CLS_ROUTE4=m
  983. +CONFIG_NET_CLS_FW=m
  984. +CONFIG_NET_CLS_U32=m
  985. +CONFIG_CLS_U32_MARK=y
  986. +CONFIG_NET_CLS_RSVP=m
  987. +CONFIG_NET_CLS_RSVP6=m
  988. +CONFIG_NET_CLS_FLOW=m
  989. +CONFIG_NET_CLS_CGROUP=m
  990. +CONFIG_NET_EMATCH=y
  991. +CONFIG_NET_EMATCH_CMP=m
  992. +CONFIG_NET_EMATCH_NBYTE=m
  993. +CONFIG_NET_EMATCH_U32=m
  994. +CONFIG_NET_EMATCH_META=m
  995. +CONFIG_NET_EMATCH_TEXT=m
  996. +CONFIG_NET_EMATCH_IPSET=m
  997. +CONFIG_NET_CLS_ACT=y
  998. +CONFIG_NET_ACT_POLICE=m
  999. +CONFIG_NET_ACT_GACT=m
  1000. +CONFIG_GACT_PROB=y
  1001. +CONFIG_NET_ACT_MIRRED=m
  1002. +CONFIG_NET_ACT_IPT=m
  1003. +CONFIG_NET_ACT_NAT=m
  1004. +CONFIG_NET_ACT_PEDIT=m
  1005. +CONFIG_NET_ACT_SIMP=m
  1006. +CONFIG_NET_ACT_SKBEDIT=m
  1007. +CONFIG_NET_ACT_CSUM=m
  1008. +CONFIG_BATMAN_ADV=m
  1009. +CONFIG_OPENVSWITCH=m
  1010. +CONFIG_NET_PKTGEN=m
  1011. +CONFIG_HAMRADIO=y
  1012. +CONFIG_AX25=m
  1013. +CONFIG_NETROM=m
  1014. +CONFIG_ROSE=m
  1015. +CONFIG_MKISS=m
  1016. +CONFIG_6PACK=m
  1017. +CONFIG_BPQETHER=m
  1018. +CONFIG_BAYCOM_SER_FDX=m
  1019. +CONFIG_BAYCOM_SER_HDX=m
  1020. +CONFIG_YAM=m
  1021. +CONFIG_IRDA=m
  1022. +CONFIG_IRLAN=m
  1023. +CONFIG_IRNET=m
  1024. +CONFIG_IRCOMM=m
  1025. +CONFIG_IRDA_ULTRA=y
  1026. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  1027. +CONFIG_IRDA_FAST_RR=y
  1028. +CONFIG_IRTTY_SIR=m
  1029. +CONFIG_KINGSUN_DONGLE=m
  1030. +CONFIG_KSDAZZLE_DONGLE=m
  1031. +CONFIG_KS959_DONGLE=m
  1032. +CONFIG_USB_IRDA=m
  1033. +CONFIG_SIGMATEL_FIR=m
  1034. +CONFIG_MCS_FIR=m
  1035. +CONFIG_BT=m
  1036. +CONFIG_BT_RFCOMM=m
  1037. +CONFIG_BT_RFCOMM_TTY=y
  1038. +CONFIG_BT_BNEP=m
  1039. +CONFIG_BT_BNEP_MC_FILTER=y
  1040. +CONFIG_BT_BNEP_PROTO_FILTER=y
  1041. +CONFIG_BT_HIDP=m
  1042. +CONFIG_BT_HCIBTUSB=m
  1043. +CONFIG_BT_HCIBCM203X=m
  1044. +CONFIG_BT_HCIBPA10X=m
  1045. +CONFIG_BT_HCIBFUSB=m
  1046. +CONFIG_BT_HCIVHCI=m
  1047. +CONFIG_BT_MRVL=m
  1048. +CONFIG_BT_MRVL_SDIO=m
  1049. +CONFIG_BT_ATH3K=m
  1050. +CONFIG_BT_WILINK=m
  1051. +CONFIG_CFG80211=m
  1052. +CONFIG_CFG80211_WEXT=y
  1053. +CONFIG_MAC80211=m
  1054. +CONFIG_MAC80211_RC_PID=y
  1055. +CONFIG_MAC80211_MESH=y
  1056. +CONFIG_WIMAX=m
  1057. +CONFIG_RFKILL=m
  1058. +CONFIG_RFKILL_INPUT=y
  1059. +CONFIG_NET_9P=m
  1060. +CONFIG_NFC=m
  1061. +CONFIG_NFC_PN533=m
  1062. +CONFIG_DEVTMPFS=y
  1063. +CONFIG_DEVTMPFS_MOUNT=y
  1064. +CONFIG_DMA_CMA=y
  1065. +CONFIG_CMA_SIZE_MBYTES=5
  1066. +CONFIG_BLK_DEV_LOOP=y
  1067. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  1068. +CONFIG_BLK_DEV_DRBD=m
  1069. +CONFIG_BLK_DEV_NBD=m
  1070. +CONFIG_BLK_DEV_RAM=y
  1071. +CONFIG_CDROM_PKTCDVD=m
  1072. +CONFIG_EEPROM_AT24=m
  1073. +CONFIG_SCSI=y
  1074. +# CONFIG_SCSI_PROC_FS is not set
  1075. +CONFIG_BLK_DEV_SD=y
  1076. +CONFIG_CHR_DEV_ST=m
  1077. +CONFIG_CHR_DEV_OSST=m
  1078. +CONFIG_BLK_DEV_SR=m
  1079. +CONFIG_CHR_DEV_SG=m
  1080. +CONFIG_SCSI_MULTI_LUN=y
  1081. +CONFIG_SCSI_ISCSI_ATTRS=y
  1082. +CONFIG_ISCSI_TCP=m
  1083. +CONFIG_ISCSI_BOOT_SYSFS=m
  1084. +CONFIG_MD=y
  1085. +CONFIG_MD_LINEAR=m
  1086. +CONFIG_MD_RAID0=m
  1087. +CONFIG_BLK_DEV_DM=m
  1088. +CONFIG_DM_CRYPT=m
  1089. +CONFIG_DM_SNAPSHOT=m
  1090. +CONFIG_DM_MIRROR=m
  1091. +CONFIG_DM_LOG_USERSPACE=m
  1092. +CONFIG_DM_RAID=m
  1093. +CONFIG_DM_ZERO=m
  1094. +CONFIG_DM_DELAY=m
  1095. +CONFIG_NETDEVICES=y
  1096. +CONFIG_BONDING=m
  1097. +CONFIG_DUMMY=m
  1098. +CONFIG_IFB=m
  1099. +CONFIG_MACVLAN=m
  1100. +CONFIG_NETCONSOLE=m
  1101. +CONFIG_TUN=m
  1102. +CONFIG_VETH=m
  1103. +CONFIG_MDIO_BITBANG=m
  1104. +CONFIG_PPP=m
  1105. +CONFIG_PPP_BSDCOMP=m
  1106. +CONFIG_PPP_DEFLATE=m
  1107. +CONFIG_PPP_FILTER=y
  1108. +CONFIG_PPP_MPPE=m
  1109. +CONFIG_PPP_MULTILINK=y
  1110. +CONFIG_PPPOE=m
  1111. +CONFIG_PPPOL2TP=m
  1112. +CONFIG_PPP_ASYNC=m
  1113. +CONFIG_PPP_SYNC_TTY=m
  1114. +CONFIG_SLIP=m
  1115. +CONFIG_SLIP_COMPRESSED=y
  1116. +CONFIG_SLIP_SMART=y
  1117. +CONFIG_USB_CATC=m
  1118. +CONFIG_USB_KAWETH=m
  1119. +CONFIG_USB_PEGASUS=m
  1120. +CONFIG_USB_RTL8150=m
  1121. +CONFIG_USB_RTL8152=m
  1122. +CONFIG_USB_USBNET=y
  1123. +CONFIG_USB_NET_AX8817X=m
  1124. +CONFIG_USB_NET_AX88179_178A=m
  1125. +CONFIG_USB_NET_CDCETHER=m
  1126. +CONFIG_USB_NET_CDC_EEM=m
  1127. +CONFIG_USB_NET_CDC_NCM=m
  1128. +CONFIG_USB_NET_CDC_MBIM=m
  1129. +CONFIG_USB_NET_DM9601=m
  1130. +CONFIG_USB_NET_SMSC75XX=m
  1131. +CONFIG_USB_NET_SMSC95XX=y
  1132. +CONFIG_USB_NET_GL620A=m
  1133. +CONFIG_USB_NET_NET1080=m
  1134. +CONFIG_USB_NET_PLUSB=m
  1135. +CONFIG_USB_NET_MCS7830=m
  1136. +CONFIG_USB_NET_CDC_SUBSET=m
  1137. +CONFIG_USB_ALI_M5632=y
  1138. +CONFIG_USB_AN2720=y
  1139. +CONFIG_USB_EPSON2888=y
  1140. +CONFIG_USB_KC2190=y
  1141. +CONFIG_USB_NET_ZAURUS=m
  1142. +CONFIG_USB_NET_CX82310_ETH=m
  1143. +CONFIG_USB_NET_KALMIA=m
  1144. +CONFIG_USB_NET_QMI_WWAN=m
  1145. +CONFIG_USB_HSO=m
  1146. +CONFIG_USB_NET_INT51X1=m
  1147. +CONFIG_USB_IPHETH=m
  1148. +CONFIG_USB_SIERRA_NET=m
  1149. +CONFIG_USB_VL600=m
  1150. +CONFIG_LIBERTAS_THINFIRM=m
  1151. +CONFIG_LIBERTAS_THINFIRM_USB=m
  1152. +CONFIG_AT76C50X_USB=m
  1153. +CONFIG_USB_ZD1201=m
  1154. +CONFIG_USB_NET_RNDIS_WLAN=m
  1155. +CONFIG_RTL8187=m
  1156. +CONFIG_MAC80211_HWSIM=m
  1157. +CONFIG_ATH_CARDS=m
  1158. +CONFIG_ATH9K=m
  1159. +CONFIG_ATH9K_HTC=m
  1160. +CONFIG_CARL9170=m
  1161. +CONFIG_ATH6KL=m
  1162. +CONFIG_ATH6KL_USB=m
  1163. +CONFIG_AR5523=m
  1164. +CONFIG_B43=m
  1165. +# CONFIG_B43_PHY_N is not set
  1166. +CONFIG_B43LEGACY=m
  1167. +CONFIG_HOSTAP=m
  1168. +CONFIG_LIBERTAS=m
  1169. +CONFIG_LIBERTAS_USB=m
  1170. +CONFIG_LIBERTAS_SDIO=m
  1171. +CONFIG_P54_COMMON=m
  1172. +CONFIG_P54_USB=m
  1173. +CONFIG_RT2X00=m
  1174. +CONFIG_RT2500USB=m
  1175. +CONFIG_RT73USB=m
  1176. +CONFIG_RT2800USB=m
  1177. +CONFIG_RT2800USB_RT3573=y
  1178. +CONFIG_RT2800USB_RT53XX=y
  1179. +CONFIG_RT2800USB_RT55XX=y
  1180. +CONFIG_RT2800USB_UNKNOWN=y
  1181. +CONFIG_RTL8192CU=m
  1182. +CONFIG_ZD1211RW=m
  1183. +CONFIG_MWIFIEX=m
  1184. +CONFIG_MWIFIEX_SDIO=m
  1185. +CONFIG_WIMAX_I2400M_USB=m
  1186. +CONFIG_INPUT_POLLDEV=m
  1187. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1188. +CONFIG_INPUT_JOYDEV=m
  1189. +CONFIG_INPUT_EVDEV=m
  1190. +# CONFIG_INPUT_KEYBOARD is not set
  1191. +# CONFIG_INPUT_MOUSE is not set
  1192. +CONFIG_INPUT_JOYSTICK=y
  1193. +CONFIG_JOYSTICK_IFORCE=m
  1194. +CONFIG_JOYSTICK_IFORCE_USB=y
  1195. +CONFIG_JOYSTICK_XPAD=m
  1196. +CONFIG_JOYSTICK_XPAD_FF=y
  1197. +CONFIG_INPUT_MISC=y
  1198. +CONFIG_INPUT_AD714X=m
  1199. +CONFIG_INPUT_ATI_REMOTE2=m
  1200. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1201. +CONFIG_INPUT_POWERMATE=m
  1202. +CONFIG_INPUT_YEALINK=m
  1203. +CONFIG_INPUT_CM109=m
  1204. +CONFIG_INPUT_UINPUT=m
  1205. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1206. +CONFIG_INPUT_ADXL34X=m
  1207. +CONFIG_INPUT_CMA3000=m
  1208. +CONFIG_SERIO=m
  1209. +CONFIG_SERIO_RAW=m
  1210. +CONFIG_GAMEPORT=m
  1211. +CONFIG_GAMEPORT_NS558=m
  1212. +CONFIG_GAMEPORT_L4=m
  1213. +CONFIG_DEVPTS_MULTIPLE_INSTANCES=y
  1214. +# CONFIG_LEGACY_PTYS is not set
  1215. +# CONFIG_DEVKMEM is not set
  1216. +CONFIG_SERIAL_AMBA_PL011=y
  1217. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  1218. +CONFIG_TTY_PRINTK=y
  1219. +CONFIG_HW_RANDOM=y
  1220. +CONFIG_HW_RANDOM_BCM2708=m
  1221. +CONFIG_RAW_DRIVER=y
  1222. +CONFIG_BRCM_CHAR_DRIVERS=y
  1223. +CONFIG_BCM_VC_CMA=y
  1224. +CONFIG_I2C=y
  1225. +CONFIG_I2C_CHARDEV=m
  1226. +CONFIG_I2C_BCM2708=m
  1227. +CONFIG_SPI=y
  1228. +CONFIG_SPI_BCM2708=m
  1229. +CONFIG_SPI_SPIDEV=y
  1230. +CONFIG_GPIO_SYSFS=y
  1231. +CONFIG_W1=m
  1232. +CONFIG_W1_MASTER_DS2490=m
  1233. +CONFIG_W1_MASTER_DS2482=m
  1234. +CONFIG_W1_MASTER_DS1WM=m
  1235. +CONFIG_W1_MASTER_GPIO=m
  1236. +CONFIG_W1_SLAVE_THERM=m
  1237. +CONFIG_W1_SLAVE_SMEM=m
  1238. +CONFIG_W1_SLAVE_DS2408=m
  1239. +CONFIG_W1_SLAVE_DS2413=m
  1240. +CONFIG_W1_SLAVE_DS2423=m
  1241. +CONFIG_W1_SLAVE_DS2431=m
  1242. +CONFIG_W1_SLAVE_DS2433=m
  1243. +CONFIG_W1_SLAVE_DS2760=m
  1244. +CONFIG_W1_SLAVE_DS2780=m
  1245. +CONFIG_W1_SLAVE_DS2781=m
  1246. +CONFIG_W1_SLAVE_DS28E04=m
  1247. +CONFIG_W1_SLAVE_BQ27000=m
  1248. +CONFIG_BATTERY_DS2760=m
  1249. +# CONFIG_HWMON is not set
  1250. +CONFIG_THERMAL=y
  1251. +CONFIG_THERMAL_BCM2835=y
  1252. +CONFIG_WATCHDOG=y
  1253. +CONFIG_BCM2708_WDT=m
  1254. +CONFIG_MEDIA_SUPPORT=m
  1255. +CONFIG_MEDIA_CAMERA_SUPPORT=y
  1256. +CONFIG_MEDIA_ANALOG_TV_SUPPORT=y
  1257. +CONFIG_MEDIA_DIGITAL_TV_SUPPORT=y
  1258. +CONFIG_MEDIA_RADIO_SUPPORT=y
  1259. +CONFIG_MEDIA_RC_SUPPORT=y
  1260. +CONFIG_MEDIA_CONTROLLER=y
  1261. +CONFIG_LIRC=m
  1262. +CONFIG_RC_DEVICES=y
  1263. +CONFIG_RC_ATI_REMOTE=m
  1264. +CONFIG_IR_IMON=m
  1265. +CONFIG_IR_MCEUSB=m
  1266. +CONFIG_IR_REDRAT3=m
  1267. +CONFIG_IR_STREAMZAP=m
  1268. +CONFIG_IR_IGUANA=m
  1269. +CONFIG_IR_TTUSBIR=m
  1270. +CONFIG_RC_LOOPBACK=m
  1271. +CONFIG_IR_GPIO_CIR=m
  1272. +CONFIG_MEDIA_USB_SUPPORT=y
  1273. +CONFIG_USB_VIDEO_CLASS=m
  1274. +CONFIG_USB_M5602=m
  1275. +CONFIG_USB_STV06XX=m
  1276. +CONFIG_USB_GL860=m
  1277. +CONFIG_USB_GSPCA_BENQ=m
  1278. +CONFIG_USB_GSPCA_CONEX=m
  1279. +CONFIG_USB_GSPCA_CPIA1=m
  1280. +CONFIG_USB_GSPCA_ETOMS=m
  1281. +CONFIG_USB_GSPCA_FINEPIX=m
  1282. +CONFIG_USB_GSPCA_JEILINJ=m
  1283. +CONFIG_USB_GSPCA_JL2005BCD=m
  1284. +CONFIG_USB_GSPCA_KINECT=m
  1285. +CONFIG_USB_GSPCA_KONICA=m
  1286. +CONFIG_USB_GSPCA_MARS=m
  1287. +CONFIG_USB_GSPCA_MR97310A=m
  1288. +CONFIG_USB_GSPCA_NW80X=m
  1289. +CONFIG_USB_GSPCA_OV519=m
  1290. +CONFIG_USB_GSPCA_OV534=m
  1291. +CONFIG_USB_GSPCA_OV534_9=m
  1292. +CONFIG_USB_GSPCA_PAC207=m
  1293. +CONFIG_USB_GSPCA_PAC7302=m
  1294. +CONFIG_USB_GSPCA_PAC7311=m
  1295. +CONFIG_USB_GSPCA_SE401=m
  1296. +CONFIG_USB_GSPCA_SN9C2028=m
  1297. +CONFIG_USB_GSPCA_SN9C20X=m
  1298. +CONFIG_USB_GSPCA_SONIXB=m
  1299. +CONFIG_USB_GSPCA_SONIXJ=m
  1300. +CONFIG_USB_GSPCA_SPCA500=m
  1301. +CONFIG_USB_GSPCA_SPCA501=m
  1302. +CONFIG_USB_GSPCA_SPCA505=m
  1303. +CONFIG_USB_GSPCA_SPCA506=m
  1304. +CONFIG_USB_GSPCA_SPCA508=m
  1305. +CONFIG_USB_GSPCA_SPCA561=m
  1306. +CONFIG_USB_GSPCA_SPCA1528=m
  1307. +CONFIG_USB_GSPCA_SQ905=m
  1308. +CONFIG_USB_GSPCA_SQ905C=m
  1309. +CONFIG_USB_GSPCA_SQ930X=m
  1310. +CONFIG_USB_GSPCA_STK014=m
  1311. +CONFIG_USB_GSPCA_STV0680=m
  1312. +CONFIG_USB_GSPCA_SUNPLUS=m
  1313. +CONFIG_USB_GSPCA_T613=m
  1314. +CONFIG_USB_GSPCA_TOPRO=m
  1315. +CONFIG_USB_GSPCA_TV8532=m
  1316. +CONFIG_USB_GSPCA_VC032X=m
  1317. +CONFIG_USB_GSPCA_VICAM=m
  1318. +CONFIG_USB_GSPCA_XIRLINK_CIT=m
  1319. +CONFIG_USB_GSPCA_ZC3XX=m
  1320. +CONFIG_USB_PWC=m
  1321. +CONFIG_VIDEO_CPIA2=m
  1322. +CONFIG_USB_ZR364XX=m
  1323. +CONFIG_USB_STKWEBCAM=m
  1324. +CONFIG_USB_S2255=m
  1325. +CONFIG_VIDEO_PVRUSB2=m
  1326. +CONFIG_VIDEO_HDPVR=m
  1327. +CONFIG_VIDEO_TLG2300=m
  1328. +CONFIG_VIDEO_USBVISION=m
  1329. +CONFIG_VIDEO_AU0828=m
  1330. +CONFIG_VIDEO_CX231XX=m
  1331. +CONFIG_VIDEO_CX231XX_ALSA=m
  1332. +CONFIG_VIDEO_CX231XX_DVB=m
  1333. +CONFIG_VIDEO_TM6000=m
  1334. +CONFIG_VIDEO_TM6000_ALSA=m
  1335. +CONFIG_VIDEO_TM6000_DVB=m
  1336. +CONFIG_DVB_USB=m
  1337. +CONFIG_DVB_USB_A800=m
  1338. +CONFIG_DVB_USB_DIBUSB_MB=m
  1339. +CONFIG_DVB_USB_DIBUSB_MB_FAULTY=y
  1340. +CONFIG_DVB_USB_DIBUSB_MC=m
  1341. +CONFIG_DVB_USB_DIB0700=m
  1342. +CONFIG_DVB_USB_UMT_010=m
  1343. +CONFIG_DVB_USB_CXUSB=m
  1344. +CONFIG_DVB_USB_M920X=m
  1345. +CONFIG_DVB_USB_DIGITV=m
  1346. +CONFIG_DVB_USB_VP7045=m
  1347. +CONFIG_DVB_USB_VP702X=m
  1348. +CONFIG_DVB_USB_GP8PSK=m
  1349. +CONFIG_DVB_USB_NOVA_T_USB2=m
  1350. +CONFIG_DVB_USB_TTUSB2=m
  1351. +CONFIG_DVB_USB_DTT200U=m
  1352. +CONFIG_DVB_USB_OPERA1=m
  1353. +CONFIG_DVB_USB_AF9005=m
  1354. +CONFIG_DVB_USB_AF9005_REMOTE=m
  1355. +CONFIG_DVB_USB_PCTV452E=m
  1356. +CONFIG_DVB_USB_DW2102=m
  1357. +CONFIG_DVB_USB_CINERGY_T2=m
  1358. +CONFIG_DVB_USB_DTV5100=m
  1359. +CONFIG_DVB_USB_FRIIO=m
  1360. +CONFIG_DVB_USB_AZ6027=m
  1361. +CONFIG_DVB_USB_TECHNISAT_USB2=m
  1362. +CONFIG_DVB_USB_V2=m
  1363. +CONFIG_DVB_USB_AF9015=m
  1364. +CONFIG_DVB_USB_AF9035=m
  1365. +CONFIG_DVB_USB_ANYSEE=m
  1366. +CONFIG_DVB_USB_AU6610=m
  1367. +CONFIG_DVB_USB_AZ6007=m
  1368. +CONFIG_DVB_USB_CE6230=m
  1369. +CONFIG_DVB_USB_EC168=m
  1370. +CONFIG_DVB_USB_GL861=m
  1371. +CONFIG_DVB_USB_IT913X=m
  1372. +CONFIG_DVB_USB_LME2510=m
  1373. +CONFIG_DVB_USB_MXL111SF=m
  1374. +CONFIG_DVB_USB_RTL28XXU=m
  1375. +CONFIG_SMS_USB_DRV=m
  1376. +CONFIG_DVB_B2C2_FLEXCOP_USB=m
  1377. +CONFIG_VIDEO_EM28XX=m
  1378. +CONFIG_VIDEO_EM28XX_ALSA=m
  1379. +CONFIG_VIDEO_EM28XX_DVB=m
  1380. +CONFIG_V4L_PLATFORM_DRIVERS=y
  1381. +CONFIG_VIDEO_BCM2835=y
  1382. +CONFIG_VIDEO_BCM2835_MMAL=m
  1383. +CONFIG_RADIO_SI470X=y
  1384. +CONFIG_USB_SI470X=m
  1385. +CONFIG_I2C_SI470X=m
  1386. +CONFIG_RADIO_SI4713=m
  1387. +CONFIG_USB_MR800=m
  1388. +CONFIG_USB_DSBR=m
  1389. +CONFIG_RADIO_SHARK=m
  1390. +CONFIG_RADIO_SHARK2=m
  1391. +CONFIG_USB_KEENE=m
  1392. +CONFIG_USB_MA901=m
  1393. +CONFIG_RADIO_TEA5764=m
  1394. +CONFIG_RADIO_SAA7706H=m
  1395. +CONFIG_RADIO_TEF6862=m
  1396. +CONFIG_RADIO_WL1273=m
  1397. +CONFIG_RADIO_WL128X=m
  1398. +CONFIG_FB=y
  1399. +CONFIG_FB_BCM2708=y
  1400. +# CONFIG_BACKLIGHT_GENERIC is not set
  1401. +CONFIG_FRAMEBUFFER_CONSOLE=y
  1402. +CONFIG_LOGO=y
  1403. +# CONFIG_LOGO_LINUX_MONO is not set
  1404. +# CONFIG_LOGO_LINUX_VGA16 is not set
  1405. +CONFIG_SOUND=y
  1406. +CONFIG_SND=m
  1407. +CONFIG_SND_SEQUENCER=m
  1408. +CONFIG_SND_SEQ_DUMMY=m
  1409. +CONFIG_SND_MIXER_OSS=m
  1410. +CONFIG_SND_PCM_OSS=m
  1411. +CONFIG_SND_SEQUENCER_OSS=y
  1412. +CONFIG_SND_HRTIMER=m
  1413. +CONFIG_SND_DUMMY=m
  1414. +CONFIG_SND_ALOOP=m
  1415. +CONFIG_SND_VIRMIDI=m
  1416. +CONFIG_SND_MTPAV=m
  1417. +CONFIG_SND_SERIAL_U16550=m
  1418. +CONFIG_SND_MPU401=m
  1419. +CONFIG_SND_BCM2835=m
  1420. +CONFIG_SND_USB_AUDIO=m
  1421. +CONFIG_SND_USB_UA101=m
  1422. +CONFIG_SND_USB_CAIAQ=m
  1423. +CONFIG_SND_USB_CAIAQ_INPUT=y
  1424. +CONFIG_SND_USB_6FIRE=m
  1425. +CONFIG_SND_SOC=m
  1426. +CONFIG_SND_SOC_DMAENGINE_PCM=y
  1427. +CONFIG_SND_SOC_GENERIC_DMAENGINE_PCM=y
  1428. +CONFIG_SND_SOC_WM8804=m
  1429. +CONFIG_SND_BCM2708_SOC_I2S=m
  1430. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
  1431. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
  1432. +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
  1433. +CONFIG_SND_BCM2708_SOC_RPI_DAC=m
  1434. +CONFIG_SND_SOC_I2C_AND_SPI=m
  1435. +CONFIG_SND_SOC_PCM5102A=m
  1436. +CONFIG_SND_SOC_PCM1794A=m
  1437. +CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC=m
  1438. +CONFIG_SOUND_PRIME=m
  1439. +CONFIG_HIDRAW=y
  1440. +CONFIG_HID_A4TECH=m
  1441. +CONFIG_HID_ACRUX=m
  1442. +CONFIG_HID_APPLE=m
  1443. +CONFIG_HID_BELKIN=m
  1444. +CONFIG_HID_CHERRY=m
  1445. +CONFIG_HID_CHICONY=m
  1446. +CONFIG_HID_CYPRESS=m
  1447. +CONFIG_HID_DRAGONRISE=m
  1448. +CONFIG_HID_EMS_FF=m
  1449. +CONFIG_HID_ELECOM=m
  1450. +CONFIG_HID_EZKEY=m
  1451. +CONFIG_HID_HOLTEK=m
  1452. +CONFIG_HID_KEYTOUCH=m
  1453. +CONFIG_HID_KYE=m
  1454. +CONFIG_HID_UCLOGIC=m
  1455. +CONFIG_HID_WALTOP=m
  1456. +CONFIG_HID_GYRATION=m
  1457. +CONFIG_HID_TWINHAN=m
  1458. +CONFIG_HID_KENSINGTON=m
  1459. +CONFIG_HID_LCPOWER=m
  1460. +CONFIG_HID_LOGITECH=m
  1461. +CONFIG_HID_MAGICMOUSE=m
  1462. +CONFIG_HID_MICROSOFT=m
  1463. +CONFIG_HID_MONTEREY=m
  1464. +CONFIG_HID_MULTITOUCH=m
  1465. +CONFIG_HID_NTRIG=m
  1466. +CONFIG_HID_ORTEK=m
  1467. +CONFIG_HID_PANTHERLORD=m
  1468. +CONFIG_HID_PETALYNX=m
  1469. +CONFIG_HID_PICOLCD=m
  1470. +CONFIG_HID_ROCCAT=m
  1471. +CONFIG_HID_SAMSUNG=m
  1472. +CONFIG_HID_SONY=m
  1473. +CONFIG_HID_SPEEDLINK=m
  1474. +CONFIG_HID_SUNPLUS=m
  1475. +CONFIG_HID_GREENASIA=m
  1476. +CONFIG_HID_SMARTJOYPLUS=m
  1477. +CONFIG_HID_TOPSEED=m
  1478. +CONFIG_HID_THINGM=m
  1479. +CONFIG_HID_THRUSTMASTER=m
  1480. +CONFIG_HID_WACOM=m
  1481. +CONFIG_HID_WIIMOTE=m
  1482. +CONFIG_HID_XINMO=m
  1483. +CONFIG_HID_ZEROPLUS=m
  1484. +CONFIG_HID_ZYDACRON=m
  1485. +CONFIG_HID_PID=y
  1486. +CONFIG_USB_HIDDEV=y
  1487. +CONFIG_USB=y
  1488. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  1489. +CONFIG_USB_MON=m
  1490. +CONFIG_USB_DWCOTG=y
  1491. +CONFIG_USB_PRINTER=m
  1492. +CONFIG_USB_STORAGE=y
  1493. +CONFIG_USB_STORAGE_REALTEK=m
  1494. +CONFIG_USB_STORAGE_DATAFAB=m
  1495. +CONFIG_USB_STORAGE_FREECOM=m
  1496. +CONFIG_USB_STORAGE_ISD200=m
  1497. +CONFIG_USB_STORAGE_USBAT=m
  1498. +CONFIG_USB_STORAGE_SDDR09=m
  1499. +CONFIG_USB_STORAGE_SDDR55=m
  1500. +CONFIG_USB_STORAGE_JUMPSHOT=m
  1501. +CONFIG_USB_STORAGE_ALAUDA=m
  1502. +CONFIG_USB_STORAGE_ONETOUCH=m
  1503. +CONFIG_USB_STORAGE_KARMA=m
  1504. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  1505. +CONFIG_USB_STORAGE_ENE_UB6250=m
  1506. +CONFIG_USB_MDC800=m
  1507. +CONFIG_USB_MICROTEK=m
  1508. +CONFIG_USB_SERIAL=m
  1509. +CONFIG_USB_SERIAL_GENERIC=y
  1510. +CONFIG_USB_SERIAL_AIRCABLE=m
  1511. +CONFIG_USB_SERIAL_ARK3116=m
  1512. +CONFIG_USB_SERIAL_BELKIN=m
  1513. +CONFIG_USB_SERIAL_CH341=m
  1514. +CONFIG_USB_SERIAL_WHITEHEAT=m
  1515. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  1516. +CONFIG_USB_SERIAL_CP210X=m
  1517. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  1518. +CONFIG_USB_SERIAL_EMPEG=m
  1519. +CONFIG_USB_SERIAL_FTDI_SIO=m
  1520. +CONFIG_USB_SERIAL_VISOR=m
  1521. +CONFIG_USB_SERIAL_IPAQ=m
  1522. +CONFIG_USB_SERIAL_IR=m
  1523. +CONFIG_USB_SERIAL_EDGEPORT=m
  1524. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  1525. +CONFIG_USB_SERIAL_F81232=m
  1526. +CONFIG_USB_SERIAL_GARMIN=m
  1527. +CONFIG_USB_SERIAL_IPW=m
  1528. +CONFIG_USB_SERIAL_IUU=m
  1529. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  1530. +CONFIG_USB_SERIAL_KEYSPAN=m
  1531. +CONFIG_USB_SERIAL_KLSI=m
  1532. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  1533. +CONFIG_USB_SERIAL_MCT_U232=m
  1534. +CONFIG_USB_SERIAL_METRO=m
  1535. +CONFIG_USB_SERIAL_MOS7720=m
  1536. +CONFIG_USB_SERIAL_MOS7840=m
  1537. +CONFIG_USB_SERIAL_NAVMAN=m
  1538. +CONFIG_USB_SERIAL_PL2303=m
  1539. +CONFIG_USB_SERIAL_OTI6858=m
  1540. +CONFIG_USB_SERIAL_QCAUX=m
  1541. +CONFIG_USB_SERIAL_QUALCOMM=m
  1542. +CONFIG_USB_SERIAL_SPCP8X5=m
  1543. +CONFIG_USB_SERIAL_SAFE=m
  1544. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  1545. +CONFIG_USB_SERIAL_SYMBOL=m
  1546. +CONFIG_USB_SERIAL_TI=m
  1547. +CONFIG_USB_SERIAL_CYBERJACK=m
  1548. +CONFIG_USB_SERIAL_XIRCOM=m
  1549. +CONFIG_USB_SERIAL_OPTION=m
  1550. +CONFIG_USB_SERIAL_OMNINET=m
  1551. +CONFIG_USB_SERIAL_OPTICON=m
  1552. +CONFIG_USB_SERIAL_XSENS_MT=m
  1553. +CONFIG_USB_SERIAL_WISHBONE=m
  1554. +CONFIG_USB_SERIAL_ZTE=m
  1555. +CONFIG_USB_SERIAL_SSU100=m
  1556. +CONFIG_USB_SERIAL_QT2=m
  1557. +CONFIG_USB_SERIAL_DEBUG=m
  1558. +CONFIG_USB_EMI62=m
  1559. +CONFIG_USB_EMI26=m
  1560. +CONFIG_USB_ADUTUX=m
  1561. +CONFIG_USB_SEVSEG=m
  1562. +CONFIG_USB_RIO500=m
  1563. +CONFIG_USB_LEGOTOWER=m
  1564. +CONFIG_USB_LCD=m
  1565. +CONFIG_USB_LED=m
  1566. +CONFIG_USB_CYPRESS_CY7C63=m
  1567. +CONFIG_USB_CYTHERM=m
  1568. +CONFIG_USB_IDMOUSE=m
  1569. +CONFIG_USB_FTDI_ELAN=m
  1570. +CONFIG_USB_APPLEDISPLAY=m
  1571. +CONFIG_USB_LD=m
  1572. +CONFIG_USB_TRANCEVIBRATOR=m
  1573. +CONFIG_USB_IOWARRIOR=m
  1574. +CONFIG_USB_TEST=m
  1575. +CONFIG_USB_ISIGHTFW=m
  1576. +CONFIG_USB_YUREX=m
  1577. +CONFIG_MMC=y
  1578. +CONFIG_MMC_BLOCK_MINORS=32
  1579. +CONFIG_MMC_BCM2835=y
  1580. +CONFIG_MMC_BCM2835_DMA=y
  1581. +CONFIG_MMC_BCM2835_PIO_DMA_BARRIER=2
  1582. +CONFIG_MMC_SDHCI=y
  1583. +CONFIG_MMC_SDHCI_PLTFM=y
  1584. +CONFIG_MMC_SDHCI_BCM2708=y
  1585. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  1586. +CONFIG_MMC_SPI=m
  1587. +CONFIG_LEDS_GPIO=m
  1588. +CONFIG_LEDS_TRIGGER_TIMER=y
  1589. +CONFIG_LEDS_TRIGGER_ONESHOT=y
  1590. +CONFIG_LEDS_TRIGGER_HEARTBEAT=y
  1591. +CONFIG_LEDS_TRIGGER_BACKLIGHT=y
  1592. +CONFIG_LEDS_TRIGGER_CPU=y
  1593. +CONFIG_LEDS_TRIGGER_GPIO=y
  1594. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=y
  1595. +CONFIG_LEDS_TRIGGER_TRANSIENT=m
  1596. +CONFIG_LEDS_TRIGGER_CAMERA=m
  1597. +CONFIG_RTC_CLASS=y
  1598. +# CONFIG_RTC_HCTOSYS is not set
  1599. +CONFIG_RTC_DRV_DS1307=m
  1600. +CONFIG_RTC_DRV_DS1374=m
  1601. +CONFIG_RTC_DRV_DS1672=m
  1602. +CONFIG_RTC_DRV_DS3232=m
  1603. +CONFIG_RTC_DRV_MAX6900=m
  1604. +CONFIG_RTC_DRV_RS5C372=m
  1605. +CONFIG_RTC_DRV_ISL1208=m
  1606. +CONFIG_RTC_DRV_ISL12022=m
  1607. +CONFIG_RTC_DRV_ISL12057=m
  1608. +CONFIG_RTC_DRV_X1205=m
  1609. +CONFIG_RTC_DRV_PCF2127=m
  1610. +CONFIG_RTC_DRV_PCF8523=m
  1611. +CONFIG_RTC_DRV_PCF8563=m
  1612. +CONFIG_RTC_DRV_PCF8583=m
  1613. +CONFIG_RTC_DRV_M41T80=m
  1614. +CONFIG_RTC_DRV_BQ32K=m
  1615. +CONFIG_RTC_DRV_S35390A=m
  1616. +CONFIG_RTC_DRV_FM3130=m
  1617. +CONFIG_RTC_DRV_RX8581=m
  1618. +CONFIG_RTC_DRV_RX8025=m
  1619. +CONFIG_RTC_DRV_EM3027=m
  1620. +CONFIG_RTC_DRV_RV3029C2=m
  1621. +CONFIG_RTC_DRV_M41T93=m
  1622. +CONFIG_RTC_DRV_M41T94=m
  1623. +CONFIG_RTC_DRV_DS1305=m
  1624. +CONFIG_RTC_DRV_DS1390=m
  1625. +CONFIG_RTC_DRV_MAX6902=m
  1626. +CONFIG_RTC_DRV_R9701=m
  1627. +CONFIG_RTC_DRV_RS5C348=m
  1628. +CONFIG_RTC_DRV_DS3234=m
  1629. +CONFIG_RTC_DRV_PCF2123=m
  1630. +CONFIG_RTC_DRV_RX4581=m
  1631. +CONFIG_DMADEVICES=y
  1632. +CONFIG_DMA_BCM2708=y
  1633. +CONFIG_DMA_ENGINE=y
  1634. +CONFIG_DMA_VIRTUAL_CHANNELS=y
  1635. +CONFIG_UIO=m
  1636. +CONFIG_UIO_PDRV_GENIRQ=m
  1637. +CONFIG_STAGING=y
  1638. +CONFIG_W35UND=m
  1639. +CONFIG_PRISM2_USB=m
  1640. +CONFIG_R8712U=m
  1641. +CONFIG_VT6656=m
  1642. +CONFIG_SPEAKUP=m
  1643. +CONFIG_SPEAKUP_SYNTH_SOFT=m
  1644. +CONFIG_STAGING_MEDIA=y
  1645. +CONFIG_DVB_AS102=m
  1646. +CONFIG_USB_SN9C102=m
  1647. +CONFIG_LIRC_STAGING=y
  1648. +CONFIG_LIRC_IGORPLUGUSB=m
  1649. +CONFIG_LIRC_IMON=m
  1650. +CONFIG_LIRC_RPI=m
  1651. +CONFIG_LIRC_SASEM=m
  1652. +CONFIG_LIRC_SERIAL=m
  1653. +# CONFIG_IOMMU_SUPPORT is not set
  1654. +CONFIG_EXT4_FS=y
  1655. +CONFIG_EXT4_FS_POSIX_ACL=y
  1656. +CONFIG_EXT4_FS_SECURITY=y
  1657. +CONFIG_REISERFS_FS=m
  1658. +CONFIG_REISERFS_FS_XATTR=y
  1659. +CONFIG_REISERFS_FS_POSIX_ACL=y
  1660. +CONFIG_REISERFS_FS_SECURITY=y
  1661. +CONFIG_JFS_FS=m
  1662. +CONFIG_JFS_POSIX_ACL=y
  1663. +CONFIG_JFS_SECURITY=y
  1664. +CONFIG_JFS_STATISTICS=y
  1665. +CONFIG_XFS_FS=m
  1666. +CONFIG_XFS_QUOTA=y
  1667. +CONFIG_XFS_POSIX_ACL=y
  1668. +CONFIG_XFS_RT=y
  1669. +CONFIG_GFS2_FS=m
  1670. +CONFIG_OCFS2_FS=m
  1671. +CONFIG_BTRFS_FS=m
  1672. +CONFIG_BTRFS_FS_POSIX_ACL=y
  1673. +CONFIG_NILFS2_FS=m
  1674. +CONFIG_FANOTIFY=y
  1675. +CONFIG_QFMT_V1=m
  1676. +CONFIG_QFMT_V2=m
  1677. +CONFIG_AUTOFS4_FS=y
  1678. +CONFIG_FUSE_FS=m
  1679. +CONFIG_CUSE=m
  1680. +CONFIG_FSCACHE=y
  1681. +CONFIG_FSCACHE_STATS=y
  1682. +CONFIG_FSCACHE_HISTOGRAM=y
  1683. +CONFIG_CACHEFILES=y
  1684. +CONFIG_ISO9660_FS=m
  1685. +CONFIG_JOLIET=y
  1686. +CONFIG_ZISOFS=y
  1687. +CONFIG_UDF_FS=m
  1688. +CONFIG_MSDOS_FS=y
  1689. +CONFIG_VFAT_FS=y
  1690. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  1691. +CONFIG_NTFS_FS=m
  1692. +CONFIG_NTFS_RW=y
  1693. +CONFIG_TMPFS=y
  1694. +CONFIG_TMPFS_POSIX_ACL=y
  1695. +CONFIG_CONFIGFS_FS=y
  1696. +CONFIG_ECRYPT_FS=m
  1697. +CONFIG_HFS_FS=m
  1698. +CONFIG_HFSPLUS_FS=m
  1699. +CONFIG_SQUASHFS=m
  1700. +CONFIG_SQUASHFS_XATTR=y
  1701. +CONFIG_SQUASHFS_LZO=y
  1702. +CONFIG_SQUASHFS_XZ=y
  1703. +CONFIG_F2FS_FS=y
  1704. +CONFIG_NFS_FS=y
  1705. +CONFIG_NFS_V3_ACL=y
  1706. +CONFIG_NFS_V4=y
  1707. +CONFIG_ROOT_NFS=y
  1708. +CONFIG_NFS_FSCACHE=y
  1709. +CONFIG_NFSD=m
  1710. +CONFIG_NFSD_V3_ACL=y
  1711. +CONFIG_NFSD_V4=y
  1712. +CONFIG_CIFS=m
  1713. +CONFIG_CIFS_WEAK_PW_HASH=y
  1714. +CONFIG_CIFS_XATTR=y
  1715. +CONFIG_CIFS_POSIX=y
  1716. +CONFIG_9P_FS=m
  1717. +CONFIG_9P_FS_POSIX_ACL=y
  1718. +CONFIG_NLS_DEFAULT="utf8"
  1719. +CONFIG_NLS_CODEPAGE_437=y
  1720. +CONFIG_NLS_CODEPAGE_737=m
  1721. +CONFIG_NLS_CODEPAGE_775=m
  1722. +CONFIG_NLS_CODEPAGE_850=m
  1723. +CONFIG_NLS_CODEPAGE_852=m
  1724. +CONFIG_NLS_CODEPAGE_855=m
  1725. +CONFIG_NLS_CODEPAGE_857=m
  1726. +CONFIG_NLS_CODEPAGE_860=m
  1727. +CONFIG_NLS_CODEPAGE_861=m
  1728. +CONFIG_NLS_CODEPAGE_862=m
  1729. +CONFIG_NLS_CODEPAGE_863=m
  1730. +CONFIG_NLS_CODEPAGE_864=m
  1731. +CONFIG_NLS_CODEPAGE_865=m
  1732. +CONFIG_NLS_CODEPAGE_866=m
  1733. +CONFIG_NLS_CODEPAGE_869=m
  1734. +CONFIG_NLS_CODEPAGE_936=m
  1735. +CONFIG_NLS_CODEPAGE_950=m
  1736. +CONFIG_NLS_CODEPAGE_932=m
  1737. +CONFIG_NLS_CODEPAGE_949=m
  1738. +CONFIG_NLS_CODEPAGE_874=m
  1739. +CONFIG_NLS_ISO8859_8=m
  1740. +CONFIG_NLS_CODEPAGE_1250=m
  1741. +CONFIG_NLS_CODEPAGE_1251=m
  1742. +CONFIG_NLS_ASCII=y
  1743. +CONFIG_NLS_ISO8859_1=m
  1744. +CONFIG_NLS_ISO8859_2=m
  1745. +CONFIG_NLS_ISO8859_3=m
  1746. +CONFIG_NLS_ISO8859_4=m
  1747. +CONFIG_NLS_ISO8859_5=m
  1748. +CONFIG_NLS_ISO8859_6=m
  1749. +CONFIG_NLS_ISO8859_7=m
  1750. +CONFIG_NLS_ISO8859_9=m
  1751. +CONFIG_NLS_ISO8859_13=m
  1752. +CONFIG_NLS_ISO8859_14=m
  1753. +CONFIG_NLS_ISO8859_15=m
  1754. +CONFIG_NLS_KOI8_R=m
  1755. +CONFIG_NLS_KOI8_U=m
  1756. +CONFIG_DLM=m
  1757. +CONFIG_PRINTK_TIME=y
  1758. +CONFIG_BOOT_PRINTK_DELAY=y
  1759. +CONFIG_DEBUG_FS=y
  1760. +CONFIG_DEBUG_MEMORY_INIT=y
  1761. +CONFIG_DETECT_HUNG_TASK=y
  1762. +CONFIG_TIMER_STATS=y
  1763. +# CONFIG_DEBUG_PREEMPT is not set
  1764. +CONFIG_LATENCYTOP=y
  1765. +# CONFIG_KPROBE_EVENT is not set
  1766. +CONFIG_KGDB=y
  1767. +CONFIG_KGDB_KDB=y
  1768. +CONFIG_KDB_KEYBOARD=y
  1769. +CONFIG_STRICT_DEVMEM=y
  1770. +CONFIG_CRYPTO_USER=m
  1771. +CONFIG_CRYPTO_NULL=m
  1772. +CONFIG_CRYPTO_CRYPTD=m
  1773. +CONFIG_CRYPTO_CBC=y
  1774. +CONFIG_CRYPTO_XTS=m
  1775. +CONFIG_CRYPTO_XCBC=m
  1776. +CONFIG_CRYPTO_SHA1_ARM=m
  1777. +CONFIG_CRYPTO_SHA512=m
  1778. +CONFIG_CRYPTO_TGR192=m
  1779. +CONFIG_CRYPTO_WP512=m
  1780. +CONFIG_CRYPTO_AES_ARM=m
  1781. +CONFIG_CRYPTO_CAST5=m
  1782. +CONFIG_CRYPTO_DES=y
  1783. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  1784. +# CONFIG_CRYPTO_HW is not set
  1785. +CONFIG_CRC_ITU_T=y
  1786. +CONFIG_LIBCRC32C=y
  1787. diff -Nur linux-3.16.2/arch/arm/configs/bcmrpi_emergency_defconfig linux-3.16-rpi/arch/arm/configs/bcmrpi_emergency_defconfig
  1788. --- linux-3.16.2/arch/arm/configs/bcmrpi_emergency_defconfig 1970-01-01 01:00:00.000000000 +0100
  1789. +++ linux-3.16-rpi/arch/arm/configs/bcmrpi_emergency_defconfig 2014-09-14 19:03:04.000000000 +0200
  1790. @@ -0,0 +1,532 @@
  1791. +CONFIG_EXPERIMENTAL=y
  1792. +# CONFIG_LOCALVERSION_AUTO is not set
  1793. +CONFIG_SYSVIPC=y
  1794. +CONFIG_POSIX_MQUEUE=y
  1795. +CONFIG_BSD_PROCESS_ACCT=y
  1796. +CONFIG_BSD_PROCESS_ACCT_V3=y
  1797. +CONFIG_FHANDLE=y
  1798. +CONFIG_AUDIT=y
  1799. +CONFIG_IKCONFIG=y
  1800. +CONFIG_IKCONFIG_PROC=y
  1801. +CONFIG_BLK_DEV_INITRD=y
  1802. +CONFIG_INITRAMFS_SOURCE="../target_fs"
  1803. +CONFIG_CGROUP_FREEZER=y
  1804. +CONFIG_CGROUP_DEVICE=y
  1805. +CONFIG_CGROUP_CPUACCT=y
  1806. +CONFIG_RESOURCE_COUNTERS=y
  1807. +CONFIG_BLK_CGROUP=y
  1808. +CONFIG_NAMESPACES=y
  1809. +CONFIG_SCHED_AUTOGROUP=y
  1810. +CONFIG_EMBEDDED=y
  1811. +# CONFIG_COMPAT_BRK is not set
  1812. +CONFIG_SLAB=y
  1813. +CONFIG_PROFILING=y
  1814. +CONFIG_OPROFILE=m
  1815. +CONFIG_KPROBES=y
  1816. +CONFIG_MODULES=y
  1817. +CONFIG_MODULE_UNLOAD=y
  1818. +CONFIG_MODVERSIONS=y
  1819. +CONFIG_MODULE_SRCVERSION_ALL=y
  1820. +# CONFIG_BLK_DEV_BSG is not set
  1821. +CONFIG_BLK_DEV_THROTTLING=y
  1822. +CONFIG_CFQ_GROUP_IOSCHED=y
  1823. +CONFIG_ARCH_BCM2708=y
  1824. +CONFIG_NO_HZ=y
  1825. +CONFIG_HIGH_RES_TIMERS=y
  1826. +CONFIG_AEABI=y
  1827. +CONFIG_SECCOMP=y
  1828. +CONFIG_CC_STACKPROTECTOR=y
  1829. +CONFIG_ZBOOT_ROM_TEXT=0x0
  1830. +CONFIG_ZBOOT_ROM_BSS=0x0
  1831. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext3 rootwait"
  1832. +CONFIG_KEXEC=y
  1833. +CONFIG_CPU_IDLE=y
  1834. +CONFIG_VFP=y
  1835. +CONFIG_BINFMT_MISC=m
  1836. +CONFIG_NET=y
  1837. +CONFIG_PACKET=y
  1838. +CONFIG_UNIX=y
  1839. +CONFIG_XFRM_USER=y
  1840. +CONFIG_NET_KEY=m
  1841. +CONFIG_INET=y
  1842. +CONFIG_IP_MULTICAST=y
  1843. +CONFIG_IP_PNP=y
  1844. +CONFIG_IP_PNP_DHCP=y
  1845. +CONFIG_IP_PNP_RARP=y
  1846. +CONFIG_SYN_COOKIES=y
  1847. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  1848. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  1849. +# CONFIG_INET_XFRM_MODE_BEET is not set
  1850. +# CONFIG_INET_LRO is not set
  1851. +# CONFIG_INET_DIAG is not set
  1852. +# CONFIG_IPV6 is not set
  1853. +CONFIG_NET_PKTGEN=m
  1854. +CONFIG_IRDA=m
  1855. +CONFIG_IRLAN=m
  1856. +CONFIG_IRCOMM=m
  1857. +CONFIG_IRDA_ULTRA=y
  1858. +CONFIG_IRDA_CACHE_LAST_LSAP=y
  1859. +CONFIG_IRDA_FAST_RR=y
  1860. +CONFIG_IRTTY_SIR=m
  1861. +CONFIG_KINGSUN_DONGLE=m
  1862. +CONFIG_KSDAZZLE_DONGLE=m
  1863. +CONFIG_KS959_DONGLE=m
  1864. +CONFIG_USB_IRDA=m
  1865. +CONFIG_SIGMATEL_FIR=m
  1866. +CONFIG_MCS_FIR=m
  1867. +CONFIG_BT=m
  1868. +CONFIG_BT_L2CAP=y
  1869. +CONFIG_BT_SCO=y
  1870. +CONFIG_BT_RFCOMM=m
  1871. +CONFIG_BT_RFCOMM_TTY=y
  1872. +CONFIG_BT_BNEP=m
  1873. +CONFIG_BT_BNEP_MC_FILTER=y
  1874. +CONFIG_BT_BNEP_PROTO_FILTER=y
  1875. +CONFIG_BT_HIDP=m
  1876. +CONFIG_BT_HCIBTUSB=m
  1877. +CONFIG_BT_HCIBCM203X=m
  1878. +CONFIG_BT_HCIBPA10X=m
  1879. +CONFIG_BT_HCIBFUSB=m
  1880. +CONFIG_BT_HCIVHCI=m
  1881. +CONFIG_BT_MRVL=m
  1882. +CONFIG_BT_MRVL_SDIO=m
  1883. +CONFIG_BT_ATH3K=m
  1884. +CONFIG_CFG80211=m
  1885. +CONFIG_MAC80211=m
  1886. +CONFIG_MAC80211_RC_PID=y
  1887. +CONFIG_MAC80211_MESH=y
  1888. +CONFIG_WIMAX=m
  1889. +CONFIG_NET_9P=m
  1890. +CONFIG_NFC=m
  1891. +CONFIG_NFC_PN533=m
  1892. +CONFIG_UEVENT_HELPER_PATH="/sbin/hotplug"
  1893. +CONFIG_BLK_DEV_LOOP=y
  1894. +CONFIG_BLK_DEV_CRYPTOLOOP=m
  1895. +CONFIG_BLK_DEV_NBD=m
  1896. +CONFIG_BLK_DEV_RAM=y
  1897. +CONFIG_CDROM_PKTCDVD=m
  1898. +CONFIG_MISC_DEVICES=y
  1899. +CONFIG_SCSI=y
  1900. +# CONFIG_SCSI_PROC_FS is not set
  1901. +CONFIG_BLK_DEV_SD=y
  1902. +CONFIG_BLK_DEV_SR=m
  1903. +CONFIG_SCSI_MULTI_LUN=y
  1904. +# CONFIG_SCSI_LOWLEVEL is not set
  1905. +CONFIG_MD=y
  1906. +CONFIG_NETDEVICES=y
  1907. +CONFIG_TUN=m
  1908. +CONFIG_PHYLIB=m
  1909. +CONFIG_MDIO_BITBANG=m
  1910. +CONFIG_NET_ETHERNET=y
  1911. +# CONFIG_NETDEV_1000 is not set
  1912. +# CONFIG_NETDEV_10000 is not set
  1913. +CONFIG_LIBERTAS_THINFIRM=m
  1914. +CONFIG_LIBERTAS_THINFIRM_USB=m
  1915. +CONFIG_AT76C50X_USB=m
  1916. +CONFIG_USB_ZD1201=m
  1917. +CONFIG_USB_NET_RNDIS_WLAN=m
  1918. +CONFIG_RTL8187=m
  1919. +CONFIG_MAC80211_HWSIM=m
  1920. +CONFIG_ATH_COMMON=m
  1921. +CONFIG_ATH9K=m
  1922. +CONFIG_ATH9K_HTC=m
  1923. +CONFIG_CARL9170=m
  1924. +CONFIG_B43=m
  1925. +CONFIG_B43LEGACY=m
  1926. +CONFIG_HOSTAP=m
  1927. +CONFIG_IWM=m
  1928. +CONFIG_LIBERTAS=m
  1929. +CONFIG_LIBERTAS_USB=m
  1930. +CONFIG_LIBERTAS_SDIO=m
  1931. +CONFIG_P54_COMMON=m
  1932. +CONFIG_P54_USB=m
  1933. +CONFIG_RT2X00=m
  1934. +CONFIG_RT2500USB=m
  1935. +CONFIG_RT73USB=m
  1936. +CONFIG_RT2800USB=m
  1937. +CONFIG_RT2800USB_RT53XX=y
  1938. +CONFIG_RTL8192CU=m
  1939. +CONFIG_WL1251=m
  1940. +CONFIG_WL12XX_MENU=m
  1941. +CONFIG_ZD1211RW=m
  1942. +CONFIG_MWIFIEX=m
  1943. +CONFIG_MWIFIEX_SDIO=m
  1944. +CONFIG_WIMAX_I2400M_USB=m
  1945. +CONFIG_USB_CATC=m
  1946. +CONFIG_USB_KAWETH=m
  1947. +CONFIG_USB_PEGASUS=m
  1948. +CONFIG_USB_RTL8150=m
  1949. +CONFIG_USB_USBNET=y
  1950. +CONFIG_USB_NET_AX8817X=m
  1951. +CONFIG_USB_NET_CDCETHER=m
  1952. +CONFIG_USB_NET_CDC_EEM=m
  1953. +CONFIG_USB_NET_DM9601=m
  1954. +CONFIG_USB_NET_SMSC75XX=m
  1955. +CONFIG_USB_NET_SMSC95XX=y
  1956. +CONFIG_USB_NET_GL620A=m
  1957. +CONFIG_USB_NET_NET1080=m
  1958. +CONFIG_USB_NET_PLUSB=m
  1959. +CONFIG_USB_NET_MCS7830=m
  1960. +CONFIG_USB_NET_CDC_SUBSET=m
  1961. +CONFIG_USB_ALI_M5632=y
  1962. +CONFIG_USB_AN2720=y
  1963. +CONFIG_USB_KC2190=y
  1964. +# CONFIG_USB_NET_ZAURUS is not set
  1965. +CONFIG_USB_NET_CX82310_ETH=m
  1966. +CONFIG_USB_NET_KALMIA=m
  1967. +CONFIG_USB_NET_INT51X1=m
  1968. +CONFIG_USB_IPHETH=m
  1969. +CONFIG_USB_SIERRA_NET=m
  1970. +CONFIG_USB_VL600=m
  1971. +CONFIG_PPP=m
  1972. +CONFIG_PPP_ASYNC=m
  1973. +CONFIG_PPP_SYNC_TTY=m
  1974. +CONFIG_PPP_DEFLATE=m
  1975. +CONFIG_PPP_BSDCOMP=m
  1976. +CONFIG_SLIP=m
  1977. +CONFIG_SLIP_COMPRESSED=y
  1978. +CONFIG_NETCONSOLE=m
  1979. +CONFIG_INPUT_POLLDEV=m
  1980. +# CONFIG_INPUT_MOUSEDEV_PSAUX is not set
  1981. +CONFIG_INPUT_JOYDEV=m
  1982. +CONFIG_INPUT_EVDEV=m
  1983. +# CONFIG_INPUT_KEYBOARD is not set
  1984. +# CONFIG_INPUT_MOUSE is not set
  1985. +CONFIG_INPUT_MISC=y
  1986. +CONFIG_INPUT_AD714X=m
  1987. +CONFIG_INPUT_ATI_REMOTE=m
  1988. +CONFIG_INPUT_ATI_REMOTE2=m
  1989. +CONFIG_INPUT_KEYSPAN_REMOTE=m
  1990. +CONFIG_INPUT_POWERMATE=m
  1991. +CONFIG_INPUT_YEALINK=m
  1992. +CONFIG_INPUT_CM109=m
  1993. +CONFIG_INPUT_UINPUT=m
  1994. +CONFIG_INPUT_GPIO_ROTARY_ENCODER=m
  1995. +CONFIG_INPUT_ADXL34X=m
  1996. +CONFIG_INPUT_CMA3000=m
  1997. +CONFIG_SERIO=m
  1998. +CONFIG_SERIO_RAW=m
  1999. +CONFIG_GAMEPORT=m
  2000. +CONFIG_GAMEPORT_NS558=m
  2001. +CONFIG_GAMEPORT_L4=m
  2002. +CONFIG_VT_HW_CONSOLE_BINDING=y
  2003. +# CONFIG_LEGACY_PTYS is not set
  2004. +# CONFIG_DEVKMEM is not set
  2005. +CONFIG_SERIAL_AMBA_PL011=y
  2006. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  2007. +# CONFIG_HW_RANDOM is not set
  2008. +CONFIG_RAW_DRIVER=y
  2009. +CONFIG_GPIO_SYSFS=y
  2010. +# CONFIG_HWMON is not set
  2011. +CONFIG_WATCHDOG=y
  2012. +CONFIG_BCM2708_WDT=m
  2013. +# CONFIG_MFD_SUPPORT is not set
  2014. +CONFIG_FB=y
  2015. +CONFIG_FB_BCM2708=y
  2016. +CONFIG_FRAMEBUFFER_CONSOLE=y
  2017. +CONFIG_LOGO=y
  2018. +# CONFIG_LOGO_LINUX_MONO is not set
  2019. +# CONFIG_LOGO_LINUX_VGA16 is not set
  2020. +CONFIG_SOUND=y
  2021. +CONFIG_SND=m
  2022. +CONFIG_SND_SEQUENCER=m
  2023. +CONFIG_SND_SEQ_DUMMY=m
  2024. +CONFIG_SND_MIXER_OSS=m
  2025. +CONFIG_SND_PCM_OSS=m
  2026. +CONFIG_SND_SEQUENCER_OSS=y
  2027. +CONFIG_SND_HRTIMER=m
  2028. +CONFIG_SND_DUMMY=m
  2029. +CONFIG_SND_ALOOP=m
  2030. +CONFIG_SND_VIRMIDI=m
  2031. +CONFIG_SND_MTPAV=m
  2032. +CONFIG_SND_SERIAL_U16550=m
  2033. +CONFIG_SND_MPU401=m
  2034. +CONFIG_SND_BCM2835=m
  2035. +CONFIG_SND_USB_AUDIO=m
  2036. +CONFIG_SND_USB_UA101=m
  2037. +CONFIG_SND_USB_CAIAQ=m
  2038. +CONFIG_SND_USB_6FIRE=m
  2039. +CONFIG_SOUND_PRIME=m
  2040. +CONFIG_HID_PID=y
  2041. +CONFIG_USB_HIDDEV=y
  2042. +CONFIG_HID_A4TECH=m
  2043. +CONFIG_HID_ACRUX=m
  2044. +CONFIG_HID_APPLE=m
  2045. +CONFIG_HID_BELKIN=m
  2046. +CONFIG_HID_CHERRY=m
  2047. +CONFIG_HID_CHICONY=m
  2048. +CONFIG_HID_CYPRESS=m
  2049. +CONFIG_HID_DRAGONRISE=m
  2050. +CONFIG_HID_EMS_FF=m
  2051. +CONFIG_HID_ELECOM=m
  2052. +CONFIG_HID_EZKEY=m
  2053. +CONFIG_HID_HOLTEK=m
  2054. +CONFIG_HID_KEYTOUCH=m
  2055. +CONFIG_HID_KYE=m
  2056. +CONFIG_HID_UCLOGIC=m
  2057. +CONFIG_HID_WALTOP=m
  2058. +CONFIG_HID_GYRATION=m
  2059. +CONFIG_HID_TWINHAN=m
  2060. +CONFIG_HID_KENSINGTON=m
  2061. +CONFIG_HID_LCPOWER=m
  2062. +CONFIG_HID_LOGITECH=m
  2063. +CONFIG_HID_MAGICMOUSE=m
  2064. +CONFIG_HID_MICROSOFT=m
  2065. +CONFIG_HID_MONTEREY=m
  2066. +CONFIG_HID_MULTITOUCH=m
  2067. +CONFIG_HID_NTRIG=m
  2068. +CONFIG_HID_ORTEK=m
  2069. +CONFIG_HID_PANTHERLORD=m
  2070. +CONFIG_HID_PETALYNX=m
  2071. +CONFIG_HID_PICOLCD=m
  2072. +CONFIG_HID_QUANTA=m
  2073. +CONFIG_HID_ROCCAT=m
  2074. +CONFIG_HID_SAMSUNG=m
  2075. +CONFIG_HID_SONY=m
  2076. +CONFIG_HID_SPEEDLINK=m
  2077. +CONFIG_HID_SUNPLUS=m
  2078. +CONFIG_HID_GREENASIA=m
  2079. +CONFIG_HID_SMARTJOYPLUS=m
  2080. +CONFIG_HID_TOPSEED=m
  2081. +CONFIG_HID_THRUSTMASTER=m
  2082. +CONFIG_HID_WACOM=m
  2083. +CONFIG_HID_WIIMOTE=m
  2084. +CONFIG_HID_ZEROPLUS=m
  2085. +CONFIG_HID_ZYDACRON=m
  2086. +CONFIG_USB=y
  2087. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  2088. +CONFIG_USB_MON=m
  2089. +CONFIG_USB_DWCOTG=y
  2090. +CONFIG_USB_STORAGE=y
  2091. +CONFIG_USB_STORAGE_REALTEK=m
  2092. +CONFIG_USB_STORAGE_DATAFAB=m
  2093. +CONFIG_USB_STORAGE_FREECOM=m
  2094. +CONFIG_USB_STORAGE_ISD200=m
  2095. +CONFIG_USB_STORAGE_USBAT=m
  2096. +CONFIG_USB_STORAGE_SDDR09=m
  2097. +CONFIG_USB_STORAGE_SDDR55=m
  2098. +CONFIG_USB_STORAGE_JUMPSHOT=m
  2099. +CONFIG_USB_STORAGE_ALAUDA=m
  2100. +CONFIG_USB_STORAGE_ONETOUCH=m
  2101. +CONFIG_USB_STORAGE_KARMA=m
  2102. +CONFIG_USB_STORAGE_CYPRESS_ATACB=m
  2103. +CONFIG_USB_STORAGE_ENE_UB6250=m
  2104. +CONFIG_USB_UAS=y
  2105. +CONFIG_USB_LIBUSUAL=y
  2106. +CONFIG_USB_MDC800=m
  2107. +CONFIG_USB_MICROTEK=m
  2108. +CONFIG_USB_SERIAL=m
  2109. +CONFIG_USB_SERIAL_GENERIC=y
  2110. +CONFIG_USB_SERIAL_AIRCABLE=m
  2111. +CONFIG_USB_SERIAL_ARK3116=m
  2112. +CONFIG_USB_SERIAL_BELKIN=m
  2113. +CONFIG_USB_SERIAL_CH341=m
  2114. +CONFIG_USB_SERIAL_WHITEHEAT=m
  2115. +CONFIG_USB_SERIAL_DIGI_ACCELEPORT=m
  2116. +CONFIG_USB_SERIAL_CP210X=m
  2117. +CONFIG_USB_SERIAL_CYPRESS_M8=m
  2118. +CONFIG_USB_SERIAL_EMPEG=m
  2119. +CONFIG_USB_SERIAL_FTDI_SIO=m
  2120. +CONFIG_USB_SERIAL_FUNSOFT=m
  2121. +CONFIG_USB_SERIAL_VISOR=m
  2122. +CONFIG_USB_SERIAL_IPAQ=m
  2123. +CONFIG_USB_SERIAL_IR=m
  2124. +CONFIG_USB_SERIAL_EDGEPORT=m
  2125. +CONFIG_USB_SERIAL_EDGEPORT_TI=m
  2126. +CONFIG_USB_SERIAL_GARMIN=m
  2127. +CONFIG_USB_SERIAL_IPW=m
  2128. +CONFIG_USB_SERIAL_IUU=m
  2129. +CONFIG_USB_SERIAL_KEYSPAN_PDA=m
  2130. +CONFIG_USB_SERIAL_KEYSPAN=m
  2131. +CONFIG_USB_SERIAL_KLSI=m
  2132. +CONFIG_USB_SERIAL_KOBIL_SCT=m
  2133. +CONFIG_USB_SERIAL_MCT_U232=m
  2134. +CONFIG_USB_SERIAL_MOS7720=m
  2135. +CONFIG_USB_SERIAL_MOS7840=m
  2136. +CONFIG_USB_SERIAL_MOTOROLA=m
  2137. +CONFIG_USB_SERIAL_NAVMAN=m
  2138. +CONFIG_USB_SERIAL_PL2303=m
  2139. +CONFIG_USB_SERIAL_OTI6858=m
  2140. +CONFIG_USB_SERIAL_QCAUX=m
  2141. +CONFIG_USB_SERIAL_QUALCOMM=m
  2142. +CONFIG_USB_SERIAL_SPCP8X5=m
  2143. +CONFIG_USB_SERIAL_HP4X=m
  2144. +CONFIG_USB_SERIAL_SAFE=m
  2145. +CONFIG_USB_SERIAL_SIEMENS_MPI=m
  2146. +CONFIG_USB_SERIAL_SIERRAWIRELESS=m
  2147. +CONFIG_USB_SERIAL_SYMBOL=m
  2148. +CONFIG_USB_SERIAL_TI=m
  2149. +CONFIG_USB_SERIAL_CYBERJACK=m
  2150. +CONFIG_USB_SERIAL_XIRCOM=m
  2151. +CONFIG_USB_SERIAL_OPTION=m
  2152. +CONFIG_USB_SERIAL_OMNINET=m
  2153. +CONFIG_USB_SERIAL_OPTICON=m
  2154. +CONFIG_USB_SERIAL_VIVOPAY_SERIAL=m
  2155. +CONFIG_USB_SERIAL_ZIO=m
  2156. +CONFIG_USB_SERIAL_SSU100=m
  2157. +CONFIG_USB_SERIAL_DEBUG=m
  2158. +CONFIG_USB_EMI62=m
  2159. +CONFIG_USB_EMI26=m
  2160. +CONFIG_USB_ADUTUX=m
  2161. +CONFIG_USB_SEVSEG=m
  2162. +CONFIG_USB_RIO500=m
  2163. +CONFIG_USB_LEGOTOWER=m
  2164. +CONFIG_USB_LCD=m
  2165. +CONFIG_USB_LED=m
  2166. +CONFIG_USB_CYPRESS_CY7C63=m
  2167. +CONFIG_USB_CYTHERM=m
  2168. +CONFIG_USB_IDMOUSE=m
  2169. +CONFIG_USB_FTDI_ELAN=m
  2170. +CONFIG_USB_APPLEDISPLAY=m
  2171. +CONFIG_USB_LD=m
  2172. +CONFIG_USB_TRANCEVIBRATOR=m
  2173. +CONFIG_USB_IOWARRIOR=m
  2174. +CONFIG_USB_TEST=m
  2175. +CONFIG_USB_ISIGHTFW=m
  2176. +CONFIG_USB_YUREX=m
  2177. +CONFIG_MMC=y
  2178. +CONFIG_MMC_SDHCI=y
  2179. +CONFIG_MMC_SDHCI_PLTFM=y
  2180. +CONFIG_MMC_SDHCI_BCM2708=y
  2181. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  2182. +CONFIG_LEDS_GPIO=y
  2183. +CONFIG_LEDS_TRIGGER_TIMER=m
  2184. +CONFIG_LEDS_TRIGGER_HEARTBEAT=m
  2185. +CONFIG_LEDS_TRIGGER_DEFAULT_ON=m
  2186. +CONFIG_UIO=m
  2187. +CONFIG_UIO_PDRV=m
  2188. +CONFIG_UIO_PDRV_GENIRQ=m
  2189. +# CONFIG_IOMMU_SUPPORT is not set
  2190. +CONFIG_EXT4_FS=y
  2191. +CONFIG_EXT4_FS_POSIX_ACL=y
  2192. +CONFIG_EXT4_FS_SECURITY=y
  2193. +CONFIG_REISERFS_FS=m
  2194. +CONFIG_REISERFS_FS_XATTR=y
  2195. +CONFIG_REISERFS_FS_POSIX_ACL=y
  2196. +CONFIG_REISERFS_FS_SECURITY=y
  2197. +CONFIG_JFS_FS=m
  2198. +CONFIG_JFS_POSIX_ACL=y
  2199. +CONFIG_JFS_SECURITY=y
  2200. +CONFIG_JFS_STATISTICS=y
  2201. +CONFIG_XFS_FS=m
  2202. +CONFIG_XFS_QUOTA=y
  2203. +CONFIG_XFS_POSIX_ACL=y
  2204. +CONFIG_XFS_RT=y
  2205. +CONFIG_GFS2_FS=m
  2206. +CONFIG_OCFS2_FS=m
  2207. +CONFIG_BTRFS_FS=m
  2208. +CONFIG_BTRFS_FS_POSIX_ACL=y
  2209. +CONFIG_NILFS2_FS=m
  2210. +CONFIG_FANOTIFY=y
  2211. +CONFIG_AUTOFS4_FS=y
  2212. +CONFIG_FUSE_FS=m
  2213. +CONFIG_CUSE=m
  2214. +CONFIG_FSCACHE=y
  2215. +CONFIG_FSCACHE_STATS=y
  2216. +CONFIG_FSCACHE_HISTOGRAM=y
  2217. +CONFIG_CACHEFILES=y
  2218. +CONFIG_ISO9660_FS=m
  2219. +CONFIG_JOLIET=y
  2220. +CONFIG_ZISOFS=y
  2221. +CONFIG_UDF_FS=m
  2222. +CONFIG_MSDOS_FS=y
  2223. +CONFIG_VFAT_FS=y
  2224. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  2225. +CONFIG_NTFS_FS=m
  2226. +CONFIG_TMPFS=y
  2227. +CONFIG_TMPFS_POSIX_ACL=y
  2228. +CONFIG_CONFIGFS_FS=y
  2229. +CONFIG_SQUASHFS=m
  2230. +CONFIG_SQUASHFS_XATTR=y
  2231. +CONFIG_SQUASHFS_LZO=y
  2232. +CONFIG_SQUASHFS_XZ=y
  2233. +CONFIG_NFS_FS=y
  2234. +CONFIG_NFS_V3=y
  2235. +CONFIG_NFS_V3_ACL=y
  2236. +CONFIG_NFS_V4=y
  2237. +CONFIG_ROOT_NFS=y
  2238. +CONFIG_NFS_FSCACHE=y
  2239. +CONFIG_CIFS=m
  2240. +CONFIG_CIFS_WEAK_PW_HASH=y
  2241. +CONFIG_CIFS_XATTR=y
  2242. +CONFIG_CIFS_POSIX=y
  2243. +CONFIG_9P_FS=m
  2244. +CONFIG_9P_FS_POSIX_ACL=y
  2245. +CONFIG_PARTITION_ADVANCED=y
  2246. +CONFIG_MAC_PARTITION=y
  2247. +CONFIG_EFI_PARTITION=y
  2248. +CONFIG_NLS_DEFAULT="utf8"
  2249. +CONFIG_NLS_CODEPAGE_437=y
  2250. +CONFIG_NLS_CODEPAGE_737=m
  2251. +CONFIG_NLS_CODEPAGE_775=m
  2252. +CONFIG_NLS_CODEPAGE_850=m
  2253. +CONFIG_NLS_CODEPAGE_852=m
  2254. +CONFIG_NLS_CODEPAGE_855=m
  2255. +CONFIG_NLS_CODEPAGE_857=m
  2256. +CONFIG_NLS_CODEPAGE_860=m
  2257. +CONFIG_NLS_CODEPAGE_861=m
  2258. +CONFIG_NLS_CODEPAGE_862=m
  2259. +CONFIG_NLS_CODEPAGE_863=m
  2260. +CONFIG_NLS_CODEPAGE_864=m
  2261. +CONFIG_NLS_CODEPAGE_865=m
  2262. +CONFIG_NLS_CODEPAGE_866=m
  2263. +CONFIG_NLS_CODEPAGE_869=m
  2264. +CONFIG_NLS_CODEPAGE_936=m
  2265. +CONFIG_NLS_CODEPAGE_950=m
  2266. +CONFIG_NLS_CODEPAGE_932=m
  2267. +CONFIG_NLS_CODEPAGE_949=m
  2268. +CONFIG_NLS_CODEPAGE_874=m
  2269. +CONFIG_NLS_ISO8859_8=m
  2270. +CONFIG_NLS_CODEPAGE_1250=m
  2271. +CONFIG_NLS_CODEPAGE_1251=m
  2272. +CONFIG_NLS_ASCII=y
  2273. +CONFIG_NLS_ISO8859_1=m
  2274. +CONFIG_NLS_ISO8859_2=m
  2275. +CONFIG_NLS_ISO8859_3=m
  2276. +CONFIG_NLS_ISO8859_4=m
  2277. +CONFIG_NLS_ISO8859_5=m
  2278. +CONFIG_NLS_ISO8859_6=m
  2279. +CONFIG_NLS_ISO8859_7=m
  2280. +CONFIG_NLS_ISO8859_9=m
  2281. +CONFIG_NLS_ISO8859_13=m
  2282. +CONFIG_NLS_ISO8859_14=m
  2283. +CONFIG_NLS_ISO8859_15=m
  2284. +CONFIG_NLS_KOI8_R=m
  2285. +CONFIG_NLS_KOI8_U=m
  2286. +CONFIG_NLS_UTF8=m
  2287. +CONFIG_PRINTK_TIME=y
  2288. +CONFIG_DETECT_HUNG_TASK=y
  2289. +CONFIG_TIMER_STATS=y
  2290. +CONFIG_DEBUG_STACK_USAGE=y
  2291. +CONFIG_DEBUG_INFO=y
  2292. +CONFIG_DEBUG_MEMORY_INIT=y
  2293. +CONFIG_BOOT_PRINTK_DELAY=y
  2294. +CONFIG_LATENCYTOP=y
  2295. +CONFIG_SYSCTL_SYSCALL_CHECK=y
  2296. +CONFIG_IRQSOFF_TRACER=y
  2297. +CONFIG_SCHED_TRACER=y
  2298. +CONFIG_STACK_TRACER=y
  2299. +CONFIG_BLK_DEV_IO_TRACE=y
  2300. +CONFIG_FUNCTION_PROFILER=y
  2301. +CONFIG_KGDB=y
  2302. +CONFIG_KGDB_KDB=y
  2303. +CONFIG_KDB_KEYBOARD=y
  2304. +CONFIG_STRICT_DEVMEM=y
  2305. +CONFIG_CRYPTO_AUTHENC=m
  2306. +CONFIG_CRYPTO_SEQIV=m
  2307. +CONFIG_CRYPTO_CBC=y
  2308. +CONFIG_CRYPTO_HMAC=y
  2309. +CONFIG_CRYPTO_XCBC=m
  2310. +CONFIG_CRYPTO_MD5=y
  2311. +CONFIG_CRYPTO_SHA1=y
  2312. +CONFIG_CRYPTO_SHA256=m
  2313. +CONFIG_CRYPTO_SHA512=m
  2314. +CONFIG_CRYPTO_TGR192=m
  2315. +CONFIG_CRYPTO_WP512=m
  2316. +CONFIG_CRYPTO_CAST5=m
  2317. +CONFIG_CRYPTO_DES=y
  2318. +CONFIG_CRYPTO_DEFLATE=m
  2319. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2320. +# CONFIG_CRYPTO_HW is not set
  2321. +CONFIG_CRC_ITU_T=y
  2322. +CONFIG_LIBCRC32C=y
  2323. diff -Nur linux-3.16.2/arch/arm/configs/bcmrpi_quick_defconfig linux-3.16-rpi/arch/arm/configs/bcmrpi_quick_defconfig
  2324. --- linux-3.16.2/arch/arm/configs/bcmrpi_quick_defconfig 1970-01-01 01:00:00.000000000 +0100
  2325. +++ linux-3.16-rpi/arch/arm/configs/bcmrpi_quick_defconfig 2014-04-13 17:32:40.000000000 +0200
  2326. @@ -0,0 +1,197 @@
  2327. +# CONFIG_ARM_PATCH_PHYS_VIRT is not set
  2328. +CONFIG_LOCALVERSION="-quick"
  2329. +# CONFIG_LOCALVERSION_AUTO is not set
  2330. +# CONFIG_SWAP is not set
  2331. +CONFIG_SYSVIPC=y
  2332. +CONFIG_POSIX_MQUEUE=y
  2333. +CONFIG_NO_HZ=y
  2334. +CONFIG_HIGH_RES_TIMERS=y
  2335. +CONFIG_IKCONFIG=y
  2336. +CONFIG_IKCONFIG_PROC=y
  2337. +CONFIG_KALLSYMS_ALL=y
  2338. +CONFIG_EMBEDDED=y
  2339. +CONFIG_PERF_EVENTS=y
  2340. +# CONFIG_COMPAT_BRK is not set
  2341. +CONFIG_SLAB=y
  2342. +CONFIG_MODULES=y
  2343. +CONFIG_MODULE_UNLOAD=y
  2344. +CONFIG_MODVERSIONS=y
  2345. +CONFIG_MODULE_SRCVERSION_ALL=y
  2346. +# CONFIG_BLK_DEV_BSG is not set
  2347. +CONFIG_ARCH_BCM2708=y
  2348. +CONFIG_PREEMPT=y
  2349. +CONFIG_AEABI=y
  2350. +CONFIG_UACCESS_WITH_MEMCPY=y
  2351. +CONFIG_ZBOOT_ROM_TEXT=0x0
  2352. +CONFIG_ZBOOT_ROM_BSS=0x0
  2353. +CONFIG_CMDLINE="dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait"
  2354. +CONFIG_CPU_FREQ=y
  2355. +CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE=y
  2356. +CONFIG_CPU_FREQ_GOV_PERFORMANCE=y
  2357. +CONFIG_CPU_FREQ_GOV_USERSPACE=y
  2358. +CONFIG_CPU_FREQ_GOV_ONDEMAND=y
  2359. +CONFIG_CPU_FREQ_GOV_CONSERVATIVE=y
  2360. +CONFIG_CPU_IDLE=y
  2361. +CONFIG_VFP=y
  2362. +CONFIG_BINFMT_MISC=y
  2363. +CONFIG_NET=y
  2364. +CONFIG_PACKET=y
  2365. +CONFIG_UNIX=y
  2366. +CONFIG_INET=y
  2367. +CONFIG_IP_MULTICAST=y
  2368. +CONFIG_IP_PNP=y
  2369. +CONFIG_IP_PNP_DHCP=y
  2370. +CONFIG_IP_PNP_RARP=y
  2371. +CONFIG_SYN_COOKIES=y
  2372. +# CONFIG_INET_XFRM_MODE_TRANSPORT is not set
  2373. +# CONFIG_INET_XFRM_MODE_TUNNEL is not set
  2374. +# CONFIG_INET_XFRM_MODE_BEET is not set
  2375. +# CONFIG_INET_LRO is not set
  2376. +# CONFIG_INET_DIAG is not set
  2377. +# CONFIG_IPV6 is not set
  2378. +# CONFIG_WIRELESS is not set
  2379. +CONFIG_DEVTMPFS=y
  2380. +CONFIG_DEVTMPFS_MOUNT=y
  2381. +CONFIG_BLK_DEV_LOOP=y
  2382. +CONFIG_BLK_DEV_RAM=y
  2383. +CONFIG_SCSI=y
  2384. +# CONFIG_SCSI_PROC_FS is not set
  2385. +# CONFIG_SCSI_LOWLEVEL is not set
  2386. +CONFIG_NETDEVICES=y
  2387. +# CONFIG_NET_VENDOR_BROADCOM is not set
  2388. +# CONFIG_NET_VENDOR_CIRRUS is not set
  2389. +# CONFIG_NET_VENDOR_FARADAY is not set
  2390. +# CONFIG_NET_VENDOR_INTEL is not set
  2391. +# CONFIG_NET_VENDOR_MARVELL is not set
  2392. +# CONFIG_NET_VENDOR_MICREL is not set
  2393. +# CONFIG_NET_VENDOR_NATSEMI is not set
  2394. +# CONFIG_NET_VENDOR_SEEQ is not set
  2395. +# CONFIG_NET_VENDOR_STMICRO is not set
  2396. +# CONFIG_NET_VENDOR_WIZNET is not set
  2397. +CONFIG_USB_USBNET=y
  2398. +# CONFIG_USB_NET_AX8817X is not set
  2399. +# CONFIG_USB_NET_CDCETHER is not set
  2400. +# CONFIG_USB_NET_CDC_NCM is not set
  2401. +CONFIG_USB_NET_SMSC95XX=y
  2402. +# CONFIG_USB_NET_NET1080 is not set
  2403. +# CONFIG_USB_NET_CDC_SUBSET is not set
  2404. +# CONFIG_USB_NET_ZAURUS is not set
  2405. +# CONFIG_WLAN is not set
  2406. +# CONFIG_INPUT_MOUSEDEV is not set
  2407. +CONFIG_INPUT_EVDEV=y
  2408. +# CONFIG_INPUT_KEYBOARD is not set
  2409. +# CONFIG_INPUT_MOUSE is not set
  2410. +# CONFIG_SERIO is not set
  2411. +CONFIG_VT_HW_CONSOLE_BINDING=y
  2412. +# CONFIG_LEGACY_PTYS is not set
  2413. +# CONFIG_DEVKMEM is not set
  2414. +CONFIG_SERIAL_AMBA_PL011=y
  2415. +CONFIG_SERIAL_AMBA_PL011_CONSOLE=y
  2416. +CONFIG_TTY_PRINTK=y
  2417. +CONFIG_HW_RANDOM=y
  2418. +CONFIG_HW_RANDOM_BCM2708=y
  2419. +CONFIG_RAW_DRIVER=y
  2420. +CONFIG_THERMAL=y
  2421. +CONFIG_THERMAL_BCM2835=y
  2422. +CONFIG_WATCHDOG=y
  2423. +CONFIG_BCM2708_WDT=y
  2424. +CONFIG_REGULATOR=y
  2425. +CONFIG_REGULATOR_DEBUG=y
  2426. +CONFIG_REGULATOR_FIXED_VOLTAGE=y
  2427. +CONFIG_REGULATOR_VIRTUAL_CONSUMER=y
  2428. +CONFIG_REGULATOR_USERSPACE_CONSUMER=y
  2429. +CONFIG_FB=y
  2430. +CONFIG_FB_BCM2708=y
  2431. +CONFIG_FRAMEBUFFER_CONSOLE=y
  2432. +CONFIG_LOGO=y
  2433. +# CONFIG_LOGO_LINUX_MONO is not set
  2434. +# CONFIG_LOGO_LINUX_VGA16 is not set
  2435. +CONFIG_SOUND=y
  2436. +CONFIG_SND=y
  2437. +CONFIG_SND_BCM2835=y
  2438. +# CONFIG_SND_USB is not set
  2439. +CONFIG_USB=y
  2440. +CONFIG_USB_ANNOUNCE_NEW_DEVICES=y
  2441. +CONFIG_USB_DWCOTG=y
  2442. +CONFIG_MMC=y
  2443. +CONFIG_MMC_SDHCI=y
  2444. +CONFIG_MMC_SDHCI_PLTFM=y
  2445. +CONFIG_MMC_SDHCI_BCM2708=y
  2446. +CONFIG_MMC_SDHCI_BCM2708_DMA=y
  2447. +CONFIG_NEW_LEDS=y
  2448. +CONFIG_LEDS_CLASS=y
  2449. +CONFIG_LEDS_TRIGGERS=y
  2450. +# CONFIG_IOMMU_SUPPORT is not set
  2451. +CONFIG_EXT4_FS=y
  2452. +CONFIG_EXT4_FS_POSIX_ACL=y
  2453. +CONFIG_EXT4_FS_SECURITY=y
  2454. +CONFIG_AUTOFS4_FS=y
  2455. +CONFIG_FSCACHE=y
  2456. +CONFIG_CACHEFILES=y
  2457. +CONFIG_MSDOS_FS=y
  2458. +CONFIG_VFAT_FS=y
  2459. +CONFIG_FAT_DEFAULT_IOCHARSET="ascii"
  2460. +CONFIG_TMPFS=y
  2461. +CONFIG_TMPFS_POSIX_ACL=y
  2462. +CONFIG_CONFIGFS_FS=y
  2463. +# CONFIG_MISC_FILESYSTEMS is not set
  2464. +CONFIG_NFS_FS=y
  2465. +CONFIG_NFS_V3_ACL=y
  2466. +CONFIG_NFS_V4=y
  2467. +CONFIG_ROOT_NFS=y
  2468. +CONFIG_NFS_FSCACHE=y
  2469. +CONFIG_NLS_DEFAULT="utf8"
  2470. +CONFIG_NLS_CODEPAGE_437=y
  2471. +CONFIG_NLS_CODEPAGE_737=y
  2472. +CONFIG_NLS_CODEPAGE_775=y
  2473. +CONFIG_NLS_CODEPAGE_850=y
  2474. +CONFIG_NLS_CODEPAGE_852=y
  2475. +CONFIG_NLS_CODEPAGE_855=y
  2476. +CONFIG_NLS_CODEPAGE_857=y
  2477. +CONFIG_NLS_CODEPAGE_860=y
  2478. +CONFIG_NLS_CODEPAGE_861=y
  2479. +CONFIG_NLS_CODEPAGE_862=y
  2480. +CONFIG_NLS_CODEPAGE_863=y
  2481. +CONFIG_NLS_CODEPAGE_864=y
  2482. +CONFIG_NLS_CODEPAGE_865=y
  2483. +CONFIG_NLS_CODEPAGE_866=y
  2484. +CONFIG_NLS_CODEPAGE_869=y
  2485. +CONFIG_NLS_CODEPAGE_936=y
  2486. +CONFIG_NLS_CODEPAGE_950=y
  2487. +CONFIG_NLS_CODEPAGE_932=y
  2488. +CONFIG_NLS_CODEPAGE_949=y
  2489. +CONFIG_NLS_CODEPAGE_874=y
  2490. +CONFIG_NLS_ISO8859_8=y
  2491. +CONFIG_NLS_CODEPAGE_1250=y
  2492. +CONFIG_NLS_CODEPAGE_1251=y
  2493. +CONFIG_NLS_ASCII=y
  2494. +CONFIG_NLS_ISO8859_1=y
  2495. +CONFIG_NLS_ISO8859_2=y
  2496. +CONFIG_NLS_ISO8859_3=y
  2497. +CONFIG_NLS_ISO8859_4=y
  2498. +CONFIG_NLS_ISO8859_5=y
  2499. +CONFIG_NLS_ISO8859_6=y
  2500. +CONFIG_NLS_ISO8859_7=y
  2501. +CONFIG_NLS_ISO8859_9=y
  2502. +CONFIG_NLS_ISO8859_13=y
  2503. +CONFIG_NLS_ISO8859_14=y
  2504. +CONFIG_NLS_ISO8859_15=y
  2505. +CONFIG_NLS_UTF8=y
  2506. +CONFIG_PRINTK_TIME=y
  2507. +CONFIG_DEBUG_FS=y
  2508. +CONFIG_DETECT_HUNG_TASK=y
  2509. +# CONFIG_DEBUG_PREEMPT is not set
  2510. +# CONFIG_DEBUG_BUGVERBOSE is not set
  2511. +# CONFIG_FTRACE is not set
  2512. +CONFIG_KGDB=y
  2513. +CONFIG_KGDB_KDB=y
  2514. +# CONFIG_ARM_UNWIND is not set
  2515. +CONFIG_CRYPTO_CBC=y
  2516. +CONFIG_CRYPTO_HMAC=y
  2517. +CONFIG_CRYPTO_MD5=y
  2518. +CONFIG_CRYPTO_SHA1=y
  2519. +CONFIG_CRYPTO_DES=y
  2520. +# CONFIG_CRYPTO_ANSI_CPRNG is not set
  2521. +# CONFIG_CRYPTO_HW is not set
  2522. +CONFIG_CRC_ITU_T=y
  2523. +CONFIG_LIBCRC32C=y
  2524. diff -Nur linux-3.16.2/arch/arm/include/asm/dma-mapping.h linux-3.16-rpi/arch/arm/include/asm/dma-mapping.h
  2525. --- linux-3.16.2/arch/arm/include/asm/dma-mapping.h 2014-09-06 01:37:11.000000000 +0200
  2526. +++ linux-3.16-rpi/arch/arm/include/asm/dma-mapping.h 2014-09-14 19:03:04.000000000 +0200
  2527. @@ -58,37 +58,21 @@
  2528. #ifndef __arch_pfn_to_dma
  2529. static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
  2530. {
  2531. - if (dev)
  2532. - pfn -= dev->dma_pfn_offset;
  2533. return (dma_addr_t)__pfn_to_bus(pfn);
  2534. }
  2535. static inline unsigned long dma_to_pfn(struct device *dev, dma_addr_t addr)
  2536. {
  2537. - unsigned long pfn = __bus_to_pfn(addr);
  2538. -
  2539. - if (dev)
  2540. - pfn += dev->dma_pfn_offset;
  2541. -
  2542. - return pfn;
  2543. + return __bus_to_pfn(addr);
  2544. }
  2545. static inline void *dma_to_virt(struct device *dev, dma_addr_t addr)
  2546. {
  2547. - if (dev) {
  2548. - unsigned long pfn = dma_to_pfn(dev, addr);
  2549. -
  2550. - return phys_to_virt(__pfn_to_phys(pfn));
  2551. - }
  2552. -
  2553. return (void *)__bus_to_virt((unsigned long)addr);
  2554. }
  2555. static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
  2556. {
  2557. - if (dev)
  2558. - return pfn_to_dma(dev, virt_to_pfn(addr));
  2559. -
  2560. return (dma_addr_t)__virt_to_bus((unsigned long)(addr));
  2561. }
  2562. diff -Nur linux-3.16.2/arch/arm/include/asm/irqflags.h linux-3.16-rpi/arch/arm/include/asm/irqflags.h
  2563. --- linux-3.16.2/arch/arm/include/asm/irqflags.h 2014-09-06 01:37:11.000000000 +0200
  2564. +++ linux-3.16-rpi/arch/arm/include/asm/irqflags.h 2014-09-14 19:03:04.000000000 +0200
  2565. @@ -145,12 +145,22 @@
  2566. }
  2567. /*
  2568. - * restore saved IRQ & FIQ state
  2569. + * restore saved IRQ state
  2570. */
  2571. static inline void arch_local_irq_restore(unsigned long flags)
  2572. {
  2573. - asm volatile(
  2574. - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
  2575. + unsigned long temp = 0;
  2576. + flags &= ~(1 << 6);
  2577. + asm volatile (
  2578. + " mrs %0, cpsr"
  2579. + : "=r" (temp)
  2580. + :
  2581. + : "memory", "cc");
  2582. + /* Preserve FIQ bit */
  2583. + temp &= (1 << 6);
  2584. + flags = flags | temp;
  2585. + asm volatile (
  2586. + " msr cpsr_c, %0 @ local_irq_restore"
  2587. :
  2588. : "r" (flags)
  2589. : "memory", "cc");
  2590. diff -Nur linux-3.16.2/arch/arm/Kconfig linux-3.16-rpi/arch/arm/Kconfig
  2591. --- linux-3.16.2/arch/arm/Kconfig 2014-09-06 01:37:11.000000000 +0200
  2592. +++ linux-3.16-rpi/arch/arm/Kconfig 2014-09-14 19:03:03.000000000 +0200
  2593. @@ -374,6 +374,23 @@
  2594. This enables support for systems based on Atmel
  2595. AT91RM9200 and AT91SAM9* processors.
  2596. +config ARCH_BCM2708
  2597. + bool "Broadcom BCM2708 family"
  2598. + select CPU_V6
  2599. + select ARM_AMBA
  2600. + select HAVE_SCHED_CLOCK
  2601. + select NEED_MACH_GPIO_H
  2602. + select NEED_MACH_MEMORY_H
  2603. + select COMMON_CLK
  2604. + select ARCH_HAS_CPUFREQ
  2605. + select GENERIC_CLOCKEVENTS
  2606. + select ARM_ERRATA_411920
  2607. + select MACH_BCM2708
  2608. + select VC4
  2609. + select FIQ
  2610. + help
  2611. + This enables support for Broadcom BCM2708 boards.
  2612. +
  2613. config ARCH_CLPS711X
  2614. bool "Cirrus Logic CLPS711x/EP721x/EP731x-based"
  2615. select ARCH_REQUIRE_GPIOLIB
  2616. @@ -1034,6 +1051,7 @@
  2617. source "arch/arm/mach-vt8500/Kconfig"
  2618. source "arch/arm/mach-w90x900/Kconfig"
  2619. +source "arch/arm/mach-bcm2708/Kconfig"
  2620. source "arch/arm/mach-zynq/Kconfig"
  2621. diff -Nur linux-3.16.2/arch/arm/Kconfig.debug linux-3.16-rpi/arch/arm/Kconfig.debug
  2622. --- linux-3.16.2/arch/arm/Kconfig.debug 2014-09-06 01:37:11.000000000 +0200
  2623. +++ linux-3.16-rpi/arch/arm/Kconfig.debug 2014-09-14 19:03:03.000000000 +0200
  2624. @@ -937,6 +937,14 @@
  2625. options; the platform specific options are deprecated
  2626. and will be soon removed.
  2627. + config DEBUG_BCM2708_UART0
  2628. + bool "Broadcom BCM2708 UART0 (PL011)"
  2629. + depends on MACH_BCM2708
  2630. + help
  2631. + Say Y here if you want the debug print routines to direct
  2632. + their output to UART 0. The port must have been initialised
  2633. + by the boot-loader before use.
  2634. +
  2635. endchoice
  2636. config DEBUG_EXYNOS_UART
  2637. diff -Nur linux-3.16.2/arch/arm/kernel/fiqasm.S linux-3.16-rpi/arch/arm/kernel/fiqasm.S
  2638. --- linux-3.16.2/arch/arm/kernel/fiqasm.S 2014-09-06 01:37:11.000000000 +0200
  2639. +++ linux-3.16-rpi/arch/arm/kernel/fiqasm.S 2014-09-14 19:03:04.000000000 +0200
  2640. @@ -47,3 +47,7 @@
  2641. mov r0, r0 @ avoid hazard prior to ARMv4
  2642. mov pc, lr
  2643. ENDPROC(__get_fiq_regs)
  2644. +
  2645. +ENTRY(__FIQ_Branch)
  2646. + mov pc, r8
  2647. +ENDPROC(__FIQ_Branch)
  2648. diff -Nur linux-3.16.2/arch/arm/kernel/process.c linux-3.16-rpi/arch/arm/kernel/process.c
  2649. --- linux-3.16.2/arch/arm/kernel/process.c 2014-09-06 01:37:11.000000000 +0200
  2650. +++ linux-3.16-rpi/arch/arm/kernel/process.c 2014-09-14 19:03:04.000000000 +0200
  2651. @@ -171,6 +171,16 @@
  2652. }
  2653. #endif
  2654. +char bcm2708_reboot_mode = 'h';
  2655. +
  2656. +int __init reboot_setup(char *str)
  2657. +{
  2658. + bcm2708_reboot_mode = str[0];
  2659. + return 1;
  2660. +}
  2661. +
  2662. +__setup("reboot=", reboot_setup);
  2663. +
  2664. /*
  2665. * Called by kexec, immediately prior to machine_kexec().
  2666. *
  2667. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/armctrl.c linux-3.16-rpi/arch/arm/mach-bcm2708/armctrl.c
  2668. --- linux-3.16.2/arch/arm/mach-bcm2708/armctrl.c 1970-01-01 01:00:00.000000000 +0100
  2669. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/armctrl.c 2014-09-14 19:03:05.000000000 +0200
  2670. @@ -0,0 +1,316 @@
  2671. +/*
  2672. + * linux/arch/arm/mach-bcm2708/armctrl.c
  2673. + *
  2674. + * Copyright (C) 2010 Broadcom
  2675. + *
  2676. + * This program is free software; you can redistribute it and/or modify
  2677. + * it under the terms of the GNU General Public License as published by
  2678. + * the Free Software Foundation; either version 2 of the License, or
  2679. + * (at your option) any later version.
  2680. + *
  2681. + * This program is distributed in the hope that it will be useful,
  2682. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2683. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2684. + * GNU General Public License for more details.
  2685. + *
  2686. + * You should have received a copy of the GNU General Public License
  2687. + * along with this program; if not, write to the Free Software
  2688. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  2689. + */
  2690. +#include <linux/init.h>
  2691. +#include <linux/list.h>
  2692. +#include <linux/io.h>
  2693. +#include <linux/version.h>
  2694. +#include <linux/syscore_ops.h>
  2695. +#include <linux/interrupt.h>
  2696. +#include <linux/irqdomain.h>
  2697. +#include <linux/of.h>
  2698. +
  2699. +#include <asm/mach/irq.h>
  2700. +#include <mach/hardware.h>
  2701. +#include "armctrl.h"
  2702. +
  2703. +/* For support of kernels >= 3.0 assume only one VIC for now*/
  2704. +static unsigned int remap_irqs[(INTERRUPT_ARASANSDIO + 1) - INTERRUPT_JPEG] = {
  2705. + INTERRUPT_VC_JPEG,
  2706. + INTERRUPT_VC_USB,
  2707. + INTERRUPT_VC_3D,
  2708. + INTERRUPT_VC_DMA2,
  2709. + INTERRUPT_VC_DMA3,
  2710. + INTERRUPT_VC_I2C,
  2711. + INTERRUPT_VC_SPI,
  2712. + INTERRUPT_VC_I2SPCM,
  2713. + INTERRUPT_VC_SDIO,
  2714. + INTERRUPT_VC_UART,
  2715. + INTERRUPT_VC_ARASANSDIO
  2716. +};
  2717. +
  2718. +static void armctrl_mask_irq(struct irq_data *d)
  2719. +{
  2720. + static const unsigned int disables[4] = {
  2721. + ARM_IRQ_DIBL1,
  2722. + ARM_IRQ_DIBL2,
  2723. + ARM_IRQ_DIBL3,
  2724. + 0
  2725. + };
  2726. +
  2727. + if (d->irq >= FIQ_START) {
  2728. + writel(0, __io_address(ARM_IRQ_FAST));
  2729. + } else {
  2730. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2731. + writel(1 << (data & 0x1f), __io_address(disables[(data >> 5) & 0x3]));
  2732. + }
  2733. +}
  2734. +
  2735. +static void armctrl_unmask_irq(struct irq_data *d)
  2736. +{
  2737. + static const unsigned int enables[4] = {
  2738. + ARM_IRQ_ENBL1,
  2739. + ARM_IRQ_ENBL2,
  2740. + ARM_IRQ_ENBL3,
  2741. + 0
  2742. + };
  2743. +
  2744. + if (d->irq >= FIQ_START) {
  2745. + unsigned int data =
  2746. + (unsigned int)irq_get_chip_data(d->irq) - FIQ_START;
  2747. + writel(0x80 | data, __io_address(ARM_IRQ_FAST));
  2748. + } else {
  2749. + unsigned int data = (unsigned int)irq_get_chip_data(d->irq);
  2750. + writel(1 << (data & 0x1f), __io_address(enables[(data >> 5) & 0x3]));
  2751. + }
  2752. +}
  2753. +
  2754. +#ifdef CONFIG_OF
  2755. +
  2756. +#define NR_IRQS_BANK0 21
  2757. +#define NR_BANKS 3 + 1 /* bank 3 is used for GPIO interrupts */
  2758. +#define IRQS_PER_BANK 32
  2759. +
  2760. +/* from drivers/irqchip/irq-bcm2835.c */
  2761. +static int armctrl_xlate(struct irq_domain *d, struct device_node *ctrlr,
  2762. + const u32 *intspec, unsigned int intsize,
  2763. + unsigned long *out_hwirq, unsigned int *out_type)
  2764. +{
  2765. + if (WARN_ON(intsize != 2))
  2766. + return -EINVAL;
  2767. +
  2768. + if (WARN_ON(intspec[0] >= NR_BANKS))
  2769. + return -EINVAL;
  2770. +
  2771. + if (WARN_ON(intspec[1] >= IRQS_PER_BANK))
  2772. + return -EINVAL;
  2773. +
  2774. + if (WARN_ON(intspec[0] == 0 && intspec[1] >= NR_IRQS_BANK0))
  2775. + return -EINVAL;
  2776. +
  2777. + if (intspec[0] == 0)
  2778. + *out_hwirq = ARM_IRQ0_BASE + intspec[1];
  2779. + else if (intspec[0] == 1)
  2780. + *out_hwirq = ARM_IRQ1_BASE + intspec[1];
  2781. + else if (intspec[0] == 2)
  2782. + *out_hwirq = ARM_IRQ2_BASE + intspec[1];
  2783. + else
  2784. + *out_hwirq = GPIO_IRQ_START + intspec[1];
  2785. +
  2786. + /* reverse remap_irqs[] */
  2787. + switch (*out_hwirq) {
  2788. + case INTERRUPT_VC_JPEG:
  2789. + *out_hwirq = INTERRUPT_JPEG;
  2790. + break;
  2791. + case INTERRUPT_VC_USB:
  2792. + *out_hwirq = INTERRUPT_USB;
  2793. + break;
  2794. + case INTERRUPT_VC_3D:
  2795. + *out_hwirq = INTERRUPT_3D;
  2796. + break;
  2797. + case INTERRUPT_VC_DMA2:
  2798. + *out_hwirq = INTERRUPT_DMA2;
  2799. + break;
  2800. + case INTERRUPT_VC_DMA3:
  2801. + *out_hwirq = INTERRUPT_DMA3;
  2802. + break;
  2803. + case INTERRUPT_VC_I2C:
  2804. + *out_hwirq = INTERRUPT_I2C;
  2805. + break;
  2806. + case INTERRUPT_VC_SPI:
  2807. + *out_hwirq = INTERRUPT_SPI;
  2808. + break;
  2809. + case INTERRUPT_VC_I2SPCM:
  2810. + *out_hwirq = INTERRUPT_I2SPCM;
  2811. + break;
  2812. + case INTERRUPT_VC_SDIO:
  2813. + *out_hwirq = INTERRUPT_SDIO;
  2814. + break;
  2815. + case INTERRUPT_VC_UART:
  2816. + *out_hwirq = INTERRUPT_UART;
  2817. + break;
  2818. + case INTERRUPT_VC_ARASANSDIO:
  2819. + *out_hwirq = INTERRUPT_ARASANSDIO;
  2820. + break;
  2821. + }
  2822. +
  2823. + *out_type = IRQ_TYPE_NONE;
  2824. + return 0;
  2825. +}
  2826. +
  2827. +static struct irq_domain_ops armctrl_ops = {
  2828. + .xlate = armctrl_xlate
  2829. +};
  2830. +
  2831. +void __init armctrl_dt_init(void)
  2832. +{
  2833. + struct device_node *np;
  2834. + struct irq_domain *domain;
  2835. +
  2836. + np = of_find_compatible_node(NULL, NULL, "brcm,bcm2708-armctrl-ic");
  2837. + if (!np)
  2838. + return;
  2839. +
  2840. + domain = irq_domain_add_legacy(np, NR_IRQS, IRQ_ARMCTRL_START, 0,
  2841. + &armctrl_ops, NULL);
  2842. + WARN_ON(!domain);
  2843. +}
  2844. +#else
  2845. +void __init armctrl_dt_init(void) { }
  2846. +#endif /* CONFIG_OF */
  2847. +
  2848. +#if defined(CONFIG_PM)
  2849. +
  2850. +/* for kernels 3.xx use the new syscore_ops apis but for older kernels use the sys dev class */
  2851. +
  2852. +/* Static defines
  2853. + * struct armctrl_device - VIC PM device (< 3.xx)
  2854. + * @sysdev: The system device which is registered. (< 3.xx)
  2855. + * @irq: The IRQ number for the base of the VIC.
  2856. + * @base: The register base for the VIC.
  2857. + * @resume_sources: A bitmask of interrupts for resume.
  2858. + * @resume_irqs: The IRQs enabled for resume.
  2859. + * @int_select: Save for VIC_INT_SELECT.
  2860. + * @int_enable: Save for VIC_INT_ENABLE.
  2861. + * @soft_int: Save for VIC_INT_SOFT.
  2862. + * @protect: Save for VIC_PROTECT.
  2863. + */
  2864. +struct armctrl_info {
  2865. + void __iomem *base;
  2866. + int irq;
  2867. + u32 resume_sources;
  2868. + u32 resume_irqs;
  2869. + u32 int_select;
  2870. + u32 int_enable;
  2871. + u32 soft_int;
  2872. + u32 protect;
  2873. +} armctrl;
  2874. +
  2875. +static int armctrl_suspend(void)
  2876. +{
  2877. + return 0;
  2878. +}
  2879. +
  2880. +static void armctrl_resume(void)
  2881. +{
  2882. + return;
  2883. +}
  2884. +
  2885. +/**
  2886. + * armctrl_pm_register - Register a VIC for later power management control
  2887. + * @base: The base address of the VIC.
  2888. + * @irq: The base IRQ for the VIC.
  2889. + * @resume_sources: bitmask of interrupts allowed for resume sources.
  2890. + *
  2891. + * For older kernels (< 3.xx) do -
  2892. + * Register the VIC with the system device tree so that it can be notified
  2893. + * of suspend and resume requests and ensure that the correct actions are
  2894. + * taken to re-instate the settings on resume.
  2895. + */
  2896. +static void __init armctrl_pm_register(void __iomem * base, unsigned int irq,
  2897. + u32 resume_sources)
  2898. +{
  2899. + armctrl.base = base;
  2900. + armctrl.resume_sources = resume_sources;
  2901. + armctrl.irq = irq;
  2902. +}
  2903. +
  2904. +static int armctrl_set_wake(struct irq_data *d, unsigned int on)
  2905. +{
  2906. + unsigned int off = d->irq & 31;
  2907. + u32 bit = 1 << off;
  2908. +
  2909. + if (!(bit & armctrl.resume_sources))
  2910. + return -EINVAL;
  2911. +
  2912. + if (on)
  2913. + armctrl.resume_irqs |= bit;
  2914. + else
  2915. + armctrl.resume_irqs &= ~bit;
  2916. +
  2917. + return 0;
  2918. +}
  2919. +
  2920. +#else
  2921. +static inline void armctrl_pm_register(void __iomem * base, unsigned int irq,
  2922. + u32 arg1)
  2923. +{
  2924. +}
  2925. +
  2926. +#define armctrl_suspend NULL
  2927. +#define armctrl_resume NULL
  2928. +#define armctrl_set_wake NULL
  2929. +#endif /* CONFIG_PM */
  2930. +
  2931. +static struct syscore_ops armctrl_syscore_ops = {
  2932. + .suspend = armctrl_suspend,
  2933. + .resume = armctrl_resume,
  2934. +};
  2935. +
  2936. +/**
  2937. + * armctrl_syscore_init - initicall to register VIC pm functions
  2938. + *
  2939. + * This is called via late_initcall() to register
  2940. + * the resources for the VICs due to the early
  2941. + * nature of the VIC's registration.
  2942. +*/
  2943. +static int __init armctrl_syscore_init(void)
  2944. +{
  2945. + register_syscore_ops(&armctrl_syscore_ops);
  2946. + return 0;
  2947. +}
  2948. +
  2949. +late_initcall(armctrl_syscore_init);
  2950. +
  2951. +static struct irq_chip armctrl_chip = {
  2952. + .name = "ARMCTRL",
  2953. + .irq_ack = NULL,
  2954. + .irq_mask = armctrl_mask_irq,
  2955. + .irq_unmask = armctrl_unmask_irq,
  2956. + .irq_set_wake = armctrl_set_wake,
  2957. +};
  2958. +
  2959. +/**
  2960. + * armctrl_init - initialise a vectored interrupt controller
  2961. + * @base: iomem base address
  2962. + * @irq_start: starting interrupt number, must be muliple of 32
  2963. + * @armctrl_sources: bitmask of interrupt sources to allow
  2964. + * @resume_sources: bitmask of interrupt sources to allow for resume
  2965. + */
  2966. +int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  2967. + u32 armctrl_sources, u32 resume_sources)
  2968. +{
  2969. + unsigned int irq;
  2970. +
  2971. + for (irq = 0; irq < NR_IRQS; irq++) {
  2972. + unsigned int data = irq;
  2973. + if (irq >= INTERRUPT_JPEG && irq <= INTERRUPT_ARASANSDIO)
  2974. + data = remap_irqs[irq - INTERRUPT_JPEG];
  2975. +
  2976. + irq_set_chip(irq, &armctrl_chip);
  2977. + irq_set_chip_data(irq, (void *)data);
  2978. + irq_set_handler(irq, handle_level_irq);
  2979. + set_irq_flags(irq, IRQF_VALID | IRQF_PROBE | IRQF_DISABLED);
  2980. + }
  2981. +
  2982. + armctrl_pm_register(base, irq_start, resume_sources);
  2983. + init_FIQ(FIQ_START);
  2984. + armctrl_dt_init();
  2985. + return 0;
  2986. +}
  2987. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/armctrl.h linux-3.16-rpi/arch/arm/mach-bcm2708/armctrl.h
  2988. --- linux-3.16.2/arch/arm/mach-bcm2708/armctrl.h 1970-01-01 01:00:00.000000000 +0100
  2989. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/armctrl.h 2014-04-13 17:32:40.000000000 +0200
  2990. @@ -0,0 +1,27 @@
  2991. +/*
  2992. + * linux/arch/arm/mach-bcm2708/armctrl.h
  2993. + *
  2994. + * Copyright (C) 2010 Broadcom
  2995. + *
  2996. + * This program is free software; you can redistribute it and/or modify
  2997. + * it under the terms of the GNU General Public License as published by
  2998. + * the Free Software Foundation; either version 2 of the License, or
  2999. + * (at your option) any later version.
  3000. + *
  3001. + * This program is distributed in the hope that it will be useful,
  3002. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3003. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3004. + * GNU General Public License for more details.
  3005. + *
  3006. + * You should have received a copy of the GNU General Public License
  3007. + * along with this program; if not, write to the Free Software
  3008. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3009. + */
  3010. +
  3011. +#ifndef __BCM2708_ARMCTRL_H
  3012. +#define __BCM2708_ARMCTRL_H
  3013. +
  3014. +extern int __init armctrl_init(void __iomem * base, unsigned int irq_start,
  3015. + u32 armctrl_sources, u32 resume_sources);
  3016. +
  3017. +#endif
  3018. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/bcm2708.c linux-3.16-rpi/arch/arm/mach-bcm2708/bcm2708.c
  3019. --- linux-3.16.2/arch/arm/mach-bcm2708/bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  3020. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/bcm2708.c 2014-09-14 19:03:05.000000000 +0200
  3021. @@ -0,0 +1,1109 @@
  3022. +/*
  3023. + * linux/arch/arm/mach-bcm2708/bcm2708.c
  3024. + *
  3025. + * Copyright (C) 2010 Broadcom
  3026. + *
  3027. + * This program is free software; you can redistribute it and/or modify
  3028. + * it under the terms of the GNU General Public License as published by
  3029. + * the Free Software Foundation; either version 2 of the License, or
  3030. + * (at your option) any later version.
  3031. + *
  3032. + * This program is distributed in the hope that it will be useful,
  3033. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3034. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3035. + * GNU General Public License for more details.
  3036. + *
  3037. + * You should have received a copy of the GNU General Public License
  3038. + * along with this program; if not, write to the Free Software
  3039. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  3040. + */
  3041. +
  3042. +#include <linux/init.h>
  3043. +#include <linux/device.h>
  3044. +#include <linux/dma-mapping.h>
  3045. +#include <linux/serial_8250.h>
  3046. +#include <linux/platform_device.h>
  3047. +#include <linux/syscore_ops.h>
  3048. +#include <linux/interrupt.h>
  3049. +#include <linux/amba/bus.h>
  3050. +#include <linux/amba/clcd.h>
  3051. +#include <linux/clk-provider.h>
  3052. +#include <linux/clkdev.h>
  3053. +#include <linux/clockchips.h>
  3054. +#include <linux/cnt32_to_63.h>
  3055. +#include <linux/io.h>
  3056. +#include <linux/module.h>
  3057. +#include <linux/of_platform.h>
  3058. +#include <linux/spi/spi.h>
  3059. +#include <linux/w1-gpio.h>
  3060. +
  3061. +#include <linux/version.h>
  3062. +#include <linux/clkdev.h>
  3063. +#include <asm/system_info.h>
  3064. +#include <mach/hardware.h>
  3065. +#include <asm/irq.h>
  3066. +#include <linux/leds.h>
  3067. +#include <asm/mach-types.h>
  3068. +#include <linux/sched_clock.h>
  3069. +
  3070. +#include <asm/mach/arch.h>
  3071. +#include <asm/mach/flash.h>
  3072. +#include <asm/mach/irq.h>
  3073. +#include <asm/mach/time.h>
  3074. +#include <asm/mach/map.h>
  3075. +
  3076. +#include <mach/timex.h>
  3077. +#include <mach/dma.h>
  3078. +#include <mach/vcio.h>
  3079. +#include <mach/system.h>
  3080. +
  3081. +#include <linux/delay.h>
  3082. +
  3083. +#include "bcm2708.h"
  3084. +#include "armctrl.h"
  3085. +
  3086. +#ifdef CONFIG_BCM_VC_CMA
  3087. +#include <linux/broadcom/vc_cma.h>
  3088. +#endif
  3089. +
  3090. +
  3091. +/* Effectively we have an IOMMU (ARM<->VideoCore map) that is set up to
  3092. + * give us IO access only to 64Mbytes of physical memory (26 bits). We could
  3093. + * represent this window by setting our dmamasks to 26 bits but, in fact
  3094. + * we're not going to use addresses outside this range (they're not in real
  3095. + * memory) so we don't bother.
  3096. + *
  3097. + * In the future we might include code to use this IOMMU to remap other
  3098. + * physical addresses onto VideoCore memory then the use of 32-bits would be
  3099. + * more legitimate.
  3100. + */
  3101. +#define DMA_MASK_BITS_COMMON 32
  3102. +
  3103. +// use GPIO 4 for the one-wire GPIO pin, if enabled
  3104. +#define W1_GPIO 4
  3105. +// ensure one-wire GPIO pullup is disabled by default
  3106. +#define W1_PULLUP -1
  3107. +
  3108. +/* command line parameters */
  3109. +static unsigned boardrev, serial;
  3110. +static unsigned uart_clock = UART0_CLOCK;
  3111. +static unsigned disk_led_gpio = 16;
  3112. +static unsigned disk_led_active_low = 1;
  3113. +static unsigned reboot_part = 0;
  3114. +static unsigned w1_gpio_pin = W1_GPIO;
  3115. +static unsigned w1_gpio_pullup = W1_PULLUP;
  3116. +static unsigned bcm2835_mmc = 1;
  3117. +
  3118. +static void __init bcm2708_init_led(void);
  3119. +
  3120. +void __init bcm2708_init_irq(void)
  3121. +{
  3122. + armctrl_init(__io_address(ARMCTRL_IC_BASE), 0, 0, 0);
  3123. +}
  3124. +
  3125. +static struct map_desc bcm2708_io_desc[] __initdata = {
  3126. + {
  3127. + .virtual = IO_ADDRESS(ARMCTRL_BASE),
  3128. + .pfn = __phys_to_pfn(ARMCTRL_BASE),
  3129. + .length = SZ_4K,
  3130. + .type = MT_DEVICE},
  3131. + {
  3132. + .virtual = IO_ADDRESS(UART0_BASE),
  3133. + .pfn = __phys_to_pfn(UART0_BASE),
  3134. + .length = SZ_4K,
  3135. + .type = MT_DEVICE},
  3136. + {
  3137. + .virtual = IO_ADDRESS(UART1_BASE),
  3138. + .pfn = __phys_to_pfn(UART1_BASE),
  3139. + .length = SZ_4K,
  3140. + .type = MT_DEVICE},
  3141. + {
  3142. + .virtual = IO_ADDRESS(DMA_BASE),
  3143. + .pfn = __phys_to_pfn(DMA_BASE),
  3144. + .length = SZ_4K,
  3145. + .type = MT_DEVICE},
  3146. + {
  3147. + .virtual = IO_ADDRESS(MCORE_BASE),
  3148. + .pfn = __phys_to_pfn(MCORE_BASE),
  3149. + .length = SZ_4K,
  3150. + .type = MT_DEVICE},
  3151. + {
  3152. + .virtual = IO_ADDRESS(ST_BASE),
  3153. + .pfn = __phys_to_pfn(ST_BASE),
  3154. + .length = SZ_4K,
  3155. + .type = MT_DEVICE},
  3156. + {
  3157. + .virtual = IO_ADDRESS(USB_BASE),
  3158. + .pfn = __phys_to_pfn(USB_BASE),
  3159. + .length = SZ_128K,
  3160. + .type = MT_DEVICE},
  3161. + {
  3162. + .virtual = IO_ADDRESS(PM_BASE),
  3163. + .pfn = __phys_to_pfn(PM_BASE),
  3164. + .length = SZ_4K,
  3165. + .type = MT_DEVICE},
  3166. + {
  3167. + .virtual = IO_ADDRESS(GPIO_BASE),
  3168. + .pfn = __phys_to_pfn(GPIO_BASE),
  3169. + .length = SZ_4K,
  3170. + .type = MT_DEVICE}
  3171. +};
  3172. +
  3173. +void __init bcm2708_map_io(void)
  3174. +{
  3175. + iotable_init(bcm2708_io_desc, ARRAY_SIZE(bcm2708_io_desc));
  3176. +}
  3177. +
  3178. +/* The STC is a free running counter that increments at the rate of 1MHz */
  3179. +#define STC_FREQ_HZ 1000000
  3180. +
  3181. +static inline uint32_t timer_read(void)
  3182. +{
  3183. + /* STC: a free running counter that increments at the rate of 1MHz */
  3184. + return readl(__io_address(ST_BASE + 0x04));
  3185. +}
  3186. +
  3187. +static unsigned long bcm2708_read_current_timer(void)
  3188. +{
  3189. + return timer_read();
  3190. +}
  3191. +
  3192. +static u64 notrace bcm2708_read_sched_clock(void)
  3193. +{
  3194. + return timer_read();
  3195. +}
  3196. +
  3197. +static cycle_t clksrc_read(struct clocksource *cs)
  3198. +{
  3199. + return timer_read();
  3200. +}
  3201. +
  3202. +static struct clocksource clocksource_stc = {
  3203. + .name = "stc",
  3204. + .rating = 300,
  3205. + .read = clksrc_read,
  3206. + .mask = CLOCKSOURCE_MASK(32),
  3207. + .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  3208. +};
  3209. +
  3210. +unsigned long frc_clock_ticks32(void)
  3211. +{
  3212. + return timer_read();
  3213. +}
  3214. +
  3215. +static void __init bcm2708_clocksource_init(void)
  3216. +{
  3217. + if (clocksource_register_hz(&clocksource_stc, STC_FREQ_HZ)) {
  3218. + printk(KERN_ERR "timer: failed to initialize clock "
  3219. + "source %s\n", clocksource_stc.name);
  3220. + }
  3221. +}
  3222. +
  3223. +struct clk __init *bcm2708_clk_register(const char *name, unsigned long fixed_rate)
  3224. +{
  3225. + struct clk *clk;
  3226. +
  3227. + clk = clk_register_fixed_rate(NULL, name, NULL, CLK_IS_ROOT,
  3228. + fixed_rate);
  3229. + if (IS_ERR(clk))
  3230. + pr_err("%s not registered\n", name);
  3231. +
  3232. + return clk;
  3233. +}
  3234. +
  3235. +void __init bcm2708_register_clkdev(struct clk *clk, const char *name)
  3236. +{
  3237. + int ret;
  3238. +
  3239. + ret = clk_register_clkdev(clk, NULL, name);
  3240. + if (ret)
  3241. + pr_err("%s alias not registered\n", name);
  3242. +}
  3243. +
  3244. +void __init bcm2708_init_clocks(void)
  3245. +{
  3246. + struct clk *clk;
  3247. +
  3248. + clk = bcm2708_clk_register("uart0_clk", uart_clock);
  3249. + bcm2708_register_clkdev(clk, "dev:f1");
  3250. +
  3251. + clk = bcm2708_clk_register("sdhost_clk", 250000000);
  3252. + bcm2708_register_clkdev(clk, "bcm2708_spi.0");
  3253. + bcm2708_register_clkdev(clk, "bcm2708_i2c.0");
  3254. + bcm2708_register_clkdev(clk, "bcm2708_i2c.1");
  3255. +}
  3256. +
  3257. +#define UART0_IRQ { IRQ_UART, 0 /*NO_IRQ*/ }
  3258. +#define UART0_DMA { 15, 14 }
  3259. +
  3260. +AMBA_DEVICE(uart0, "dev:f1", UART0, NULL);
  3261. +
  3262. +static struct amba_device *amba_devs[] __initdata = {
  3263. + &uart0_device,
  3264. +};
  3265. +
  3266. +static struct resource bcm2708_dmaman_resources[] = {
  3267. + {
  3268. + .start = DMA_BASE,
  3269. + .end = DMA_BASE + SZ_4K - 1,
  3270. + .flags = IORESOURCE_MEM,
  3271. + }
  3272. +};
  3273. +
  3274. +static struct platform_device bcm2708_dmaman_device = {
  3275. + .name = BCM_DMAMAN_DRIVER_NAME,
  3276. + .id = 0, /* first bcm2708_dma */
  3277. + .resource = bcm2708_dmaman_resources,
  3278. + .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
  3279. +};
  3280. +
  3281. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  3282. +static struct w1_gpio_platform_data w1_gpio_pdata = {
  3283. + .pin = W1_GPIO,
  3284. + .ext_pullup_enable_pin = W1_PULLUP,
  3285. + .is_open_drain = 0,
  3286. +};
  3287. +
  3288. +static struct platform_device w1_device = {
  3289. + .name = "w1-gpio",
  3290. + .id = -1,
  3291. + .dev.platform_data = &w1_gpio_pdata,
  3292. +};
  3293. +#endif
  3294. +
  3295. +static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3296. +
  3297. +static struct platform_device bcm2708_fb_device = {
  3298. + .name = "bcm2708_fb",
  3299. + .id = -1, /* only one bcm2708_fb */
  3300. + .resource = NULL,
  3301. + .num_resources = 0,
  3302. + .dev = {
  3303. + .dma_mask = &fb_dmamask,
  3304. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3305. + },
  3306. +};
  3307. +
  3308. +static struct plat_serial8250_port bcm2708_uart1_platform_data[] = {
  3309. + {
  3310. + .mapbase = UART1_BASE + 0x40,
  3311. + .irq = IRQ_AUX,
  3312. + .uartclk = 125000000,
  3313. + .regshift = 2,
  3314. + .iotype = UPIO_MEM,
  3315. + .flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_SKIP_TEST,
  3316. + .type = PORT_8250,
  3317. + },
  3318. + {},
  3319. +};
  3320. +
  3321. +static struct platform_device bcm2708_uart1_device = {
  3322. + .name = "serial8250",
  3323. + .id = PLAT8250_DEV_PLATFORM,
  3324. + .dev = {
  3325. + .platform_data = bcm2708_uart1_platform_data,
  3326. + },
  3327. +};
  3328. +
  3329. +static struct resource bcm2708_usb_resources[] = {
  3330. + [0] = {
  3331. + .start = USB_BASE,
  3332. + .end = USB_BASE + SZ_128K - 1,
  3333. + .flags = IORESOURCE_MEM,
  3334. + },
  3335. + [1] = {
  3336. + .start = MPHI_BASE,
  3337. + .end = MPHI_BASE + SZ_4K - 1,
  3338. + .flags = IORESOURCE_MEM,
  3339. + },
  3340. + [2] = {
  3341. + .start = IRQ_HOSTPORT,
  3342. + .end = IRQ_HOSTPORT,
  3343. + .flags = IORESOURCE_IRQ,
  3344. + },
  3345. + [3] = {
  3346. + .start = IRQ_USB,
  3347. + .end = IRQ_USB,
  3348. + .flags = IORESOURCE_IRQ,
  3349. + },
  3350. +};
  3351. +
  3352. +
  3353. +static u64 usb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3354. +
  3355. +static struct platform_device bcm2708_usb_device = {
  3356. + .name = "bcm2708_usb",
  3357. + .id = -1, /* only one bcm2708_usb */
  3358. + .resource = bcm2708_usb_resources,
  3359. + .num_resources = ARRAY_SIZE(bcm2708_usb_resources),
  3360. + .dev = {
  3361. + .dma_mask = &usb_dmamask,
  3362. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3363. + },
  3364. +};
  3365. +
  3366. +static struct resource bcm2708_vcio_resources[] = {
  3367. + [0] = { /* mailbox/semaphore/doorbell access */
  3368. + .start = MCORE_BASE,
  3369. + .end = MCORE_BASE + SZ_4K - 1,
  3370. + .flags = IORESOURCE_MEM,
  3371. + },
  3372. +};
  3373. +
  3374. +static u64 vcio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3375. +
  3376. +static struct platform_device bcm2708_vcio_device = {
  3377. + .name = BCM_VCIO_DRIVER_NAME,
  3378. + .id = -1, /* only one VideoCore I/O area */
  3379. + .resource = bcm2708_vcio_resources,
  3380. + .num_resources = ARRAY_SIZE(bcm2708_vcio_resources),
  3381. + .dev = {
  3382. + .dma_mask = &vcio_dmamask,
  3383. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3384. + },
  3385. +};
  3386. +
  3387. +#ifdef CONFIG_BCM2708_GPIO
  3388. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  3389. +
  3390. +static struct resource bcm2708_gpio_resources[] = {
  3391. + [0] = { /* general purpose I/O */
  3392. + .start = GPIO_BASE,
  3393. + .end = GPIO_BASE + SZ_4K - 1,
  3394. + .flags = IORESOURCE_MEM,
  3395. + },
  3396. +};
  3397. +
  3398. +static u64 gpio_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3399. +
  3400. +static struct platform_device bcm2708_gpio_device = {
  3401. + .name = BCM_GPIO_DRIVER_NAME,
  3402. + .id = -1, /* only one VideoCore I/O area */
  3403. + .resource = bcm2708_gpio_resources,
  3404. + .num_resources = ARRAY_SIZE(bcm2708_gpio_resources),
  3405. + .dev = {
  3406. + .dma_mask = &gpio_dmamask,
  3407. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3408. + },
  3409. +};
  3410. +#endif
  3411. +
  3412. +static struct resource bcm2708_systemtimer_resources[] = {
  3413. + [0] = { /* system timer access */
  3414. + .start = ST_BASE,
  3415. + .end = ST_BASE + SZ_4K - 1,
  3416. + .flags = IORESOURCE_MEM,
  3417. + },
  3418. + {
  3419. + .start = IRQ_TIMER3,
  3420. + .end = IRQ_TIMER3,
  3421. + .flags = IORESOURCE_IRQ,
  3422. + }
  3423. +
  3424. +};
  3425. +
  3426. +static u64 systemtimer_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3427. +
  3428. +static struct platform_device bcm2708_systemtimer_device = {
  3429. + .name = "bcm2708_systemtimer",
  3430. + .id = -1, /* only one VideoCore I/O area */
  3431. + .resource = bcm2708_systemtimer_resources,
  3432. + .num_resources = ARRAY_SIZE(bcm2708_systemtimer_resources),
  3433. + .dev = {
  3434. + .dma_mask = &systemtimer_dmamask,
  3435. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON),
  3436. + },
  3437. +};
  3438. +
  3439. +#ifdef CONFIG_MMC_SDHCI_BCM2708 /* Arasan emmc SD */
  3440. +static struct resource bcm2708_emmc_resources[] = {
  3441. + [0] = {
  3442. + .start = EMMC_BASE,
  3443. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  3444. + /* the memory map actually makes SZ_4K available */
  3445. + .flags = IORESOURCE_MEM,
  3446. + },
  3447. + [1] = {
  3448. + .start = IRQ_ARASANSDIO,
  3449. + .end = IRQ_ARASANSDIO,
  3450. + .flags = IORESOURCE_IRQ,
  3451. + },
  3452. +};
  3453. +
  3454. +static u64 bcm2708_emmc_dmamask = 0xffffffffUL;
  3455. +
  3456. +struct platform_device bcm2708_emmc_device = {
  3457. + .name = "bcm2708_sdhci",
  3458. + .id = 0,
  3459. + .num_resources = ARRAY_SIZE(bcm2708_emmc_resources),
  3460. + .resource = bcm2708_emmc_resources,
  3461. + .dev = {
  3462. + .dma_mask = &bcm2708_emmc_dmamask,
  3463. + .coherent_dma_mask = 0xffffffffUL},
  3464. +};
  3465. +#endif /* CONFIG_MMC_SDHCI_BCM2708 */
  3466. +
  3467. +#ifdef CONFIG_MMC_BCM2835 /* Arasan emmc SD (new) */
  3468. +static struct resource bcm2835_emmc_resources[] = {
  3469. + [0] = {
  3470. + .start = EMMC_BASE,
  3471. + .end = EMMC_BASE + SZ_256 - 1, /* we only need this area */
  3472. + /* the memory map actually makes SZ_4K available */
  3473. + .flags = IORESOURCE_MEM,
  3474. + },
  3475. + [1] = {
  3476. + .start = IRQ_ARASANSDIO,
  3477. + .end = IRQ_ARASANSDIO,
  3478. + .flags = IORESOURCE_IRQ,
  3479. + },
  3480. +};
  3481. +
  3482. +static u64 bcm2835_emmc_dmamask = 0xffffffffUL;
  3483. +
  3484. +struct platform_device bcm2835_emmc_device = {
  3485. + .name = "mmc-bcm2835",
  3486. + .id = 0,
  3487. + .num_resources = ARRAY_SIZE(bcm2835_emmc_resources),
  3488. + .resource = bcm2835_emmc_resources,
  3489. + .dev = {
  3490. + .dma_mask = &bcm2835_emmc_dmamask,
  3491. + .coherent_dma_mask = 0xffffffffUL},
  3492. +};
  3493. +#endif /* CONFIG_MMC_BCM2835 */
  3494. +
  3495. +static struct resource bcm2708_powerman_resources[] = {
  3496. + [0] = {
  3497. + .start = PM_BASE,
  3498. + .end = PM_BASE + SZ_256 - 1,
  3499. + .flags = IORESOURCE_MEM,
  3500. + },
  3501. +};
  3502. +
  3503. +static u64 powerman_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3504. +
  3505. +struct platform_device bcm2708_powerman_device = {
  3506. + .name = "bcm2708_powerman",
  3507. + .id = 0,
  3508. + .num_resources = ARRAY_SIZE(bcm2708_powerman_resources),
  3509. + .resource = bcm2708_powerman_resources,
  3510. + .dev = {
  3511. + .dma_mask = &powerman_dmamask,
  3512. + .coherent_dma_mask = 0xffffffffUL},
  3513. +};
  3514. +
  3515. +
  3516. +static struct platform_device bcm2708_alsa_devices[] = {
  3517. + [0] = {
  3518. + .name = "bcm2835_AUD0",
  3519. + .id = 0, /* first audio device */
  3520. + .resource = 0,
  3521. + .num_resources = 0,
  3522. + },
  3523. + [1] = {
  3524. + .name = "bcm2835_AUD1",
  3525. + .id = 1, /* second audio device */
  3526. + .resource = 0,
  3527. + .num_resources = 0,
  3528. + },
  3529. + [2] = {
  3530. + .name = "bcm2835_AUD2",
  3531. + .id = 2, /* third audio device */
  3532. + .resource = 0,
  3533. + .num_resources = 0,
  3534. + },
  3535. + [3] = {
  3536. + .name = "bcm2835_AUD3",
  3537. + .id = 3, /* forth audio device */
  3538. + .resource = 0,
  3539. + .num_resources = 0,
  3540. + },
  3541. + [4] = {
  3542. + .name = "bcm2835_AUD4",
  3543. + .id = 4, /* fifth audio device */
  3544. + .resource = 0,
  3545. + .num_resources = 0,
  3546. + },
  3547. + [5] = {
  3548. + .name = "bcm2835_AUD5",
  3549. + .id = 5, /* sixth audio device */
  3550. + .resource = 0,
  3551. + .num_resources = 0,
  3552. + },
  3553. + [6] = {
  3554. + .name = "bcm2835_AUD6",
  3555. + .id = 6, /* seventh audio device */
  3556. + .resource = 0,
  3557. + .num_resources = 0,
  3558. + },
  3559. + [7] = {
  3560. + .name = "bcm2835_AUD7",
  3561. + .id = 7, /* eighth audio device */
  3562. + .resource = 0,
  3563. + .num_resources = 0,
  3564. + },
  3565. +};
  3566. +
  3567. +#ifndef CONFIG_OF
  3568. +static struct resource bcm2708_spi_resources[] = {
  3569. + {
  3570. + .start = SPI0_BASE,
  3571. + .end = SPI0_BASE + SZ_256 - 1,
  3572. + .flags = IORESOURCE_MEM,
  3573. + }, {
  3574. + .start = IRQ_SPI,
  3575. + .end = IRQ_SPI,
  3576. + .flags = IORESOURCE_IRQ,
  3577. + }
  3578. +};
  3579. +
  3580. +
  3581. +static u64 bcm2708_spi_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
  3582. +static struct platform_device bcm2708_spi_device = {
  3583. + .name = "bcm2708_spi",
  3584. + .id = 0,
  3585. + .num_resources = ARRAY_SIZE(bcm2708_spi_resources),
  3586. + .resource = bcm2708_spi_resources,
  3587. + .dev = {
  3588. + .dma_mask = &bcm2708_spi_dmamask,
  3589. + .coherent_dma_mask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON)},
  3590. +};
  3591. +#endif
  3592. +
  3593. +#ifdef CONFIG_BCM2708_SPIDEV
  3594. +static struct spi_board_info bcm2708_spi_devices[] = {
  3595. +#ifdef CONFIG_SPI_SPIDEV
  3596. + {
  3597. + .modalias = "spidev",
  3598. + .max_speed_hz = 500000,
  3599. + .bus_num = 0,
  3600. + .chip_select = 0,
  3601. + .mode = SPI_MODE_0,
  3602. + }, {
  3603. + .modalias = "spidev",
  3604. + .max_speed_hz = 500000,
  3605. + .bus_num = 0,
  3606. + .chip_select = 1,
  3607. + .mode = SPI_MODE_0,
  3608. + }
  3609. +#endif
  3610. +};
  3611. +#endif
  3612. +
  3613. +#ifndef CONFIG_OF
  3614. +static struct resource bcm2708_bsc0_resources[] = {
  3615. + {
  3616. + .start = BSC0_BASE,
  3617. + .end = BSC0_BASE + SZ_256 - 1,
  3618. + .flags = IORESOURCE_MEM,
  3619. + }, {
  3620. + .start = INTERRUPT_I2C,
  3621. + .end = INTERRUPT_I2C,
  3622. + .flags = IORESOURCE_IRQ,
  3623. + }
  3624. +};
  3625. +
  3626. +static struct platform_device bcm2708_bsc0_device = {
  3627. + .name = "bcm2708_i2c",
  3628. + .id = 0,
  3629. + .num_resources = ARRAY_SIZE(bcm2708_bsc0_resources),
  3630. + .resource = bcm2708_bsc0_resources,
  3631. +};
  3632. +
  3633. +
  3634. +static struct resource bcm2708_bsc1_resources[] = {
  3635. + {
  3636. + .start = BSC1_BASE,
  3637. + .end = BSC1_BASE + SZ_256 - 1,
  3638. + .flags = IORESOURCE_MEM,
  3639. + }, {
  3640. + .start = INTERRUPT_I2C,
  3641. + .end = INTERRUPT_I2C,
  3642. + .flags = IORESOURCE_IRQ,
  3643. + }
  3644. +};
  3645. +
  3646. +static struct platform_device bcm2708_bsc1_device = {
  3647. + .name = "bcm2708_i2c",
  3648. + .id = 1,
  3649. + .num_resources = ARRAY_SIZE(bcm2708_bsc1_resources),
  3650. + .resource = bcm2708_bsc1_resources,
  3651. +};
  3652. +#endif
  3653. +
  3654. +static struct platform_device bcm2835_hwmon_device = {
  3655. + .name = "bcm2835_hwmon",
  3656. +};
  3657. +
  3658. +static struct platform_device bcm2835_thermal_device = {
  3659. + .name = "bcm2835_thermal",
  3660. +};
  3661. +
  3662. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  3663. +static struct resource bcm2708_i2s_resources[] = {
  3664. + {
  3665. + .start = I2S_BASE,
  3666. + .end = I2S_BASE + 0x20,
  3667. + .flags = IORESOURCE_MEM,
  3668. + },
  3669. + {
  3670. + .start = PCM_CLOCK_BASE,
  3671. + .end = PCM_CLOCK_BASE + 0x02,
  3672. + .flags = IORESOURCE_MEM,
  3673. + }
  3674. +};
  3675. +
  3676. +static struct platform_device bcm2708_i2s_device = {
  3677. + .name = "bcm2708-i2s",
  3678. + .id = 0,
  3679. + .num_resources = ARRAY_SIZE(bcm2708_i2s_resources),
  3680. + .resource = bcm2708_i2s_resources,
  3681. +};
  3682. +#endif
  3683. +
  3684. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  3685. +static struct platform_device snd_hifiberry_dac_device = {
  3686. + .name = "snd-hifiberry-dac",
  3687. + .id = 0,
  3688. + .num_resources = 0,
  3689. +};
  3690. +
  3691. +static struct platform_device snd_pcm5102a_codec_device = {
  3692. + .name = "pcm5102a-codec",
  3693. + .id = -1,
  3694. + .num_resources = 0,
  3695. +};
  3696. +#endif
  3697. +
  3698. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS_MODULE)
  3699. +static struct platform_device snd_rpi_hifiberry_dacplus_device = {
  3700. + .name = "snd-rpi-hifiberry-dacplus",
  3701. + .id = 0,
  3702. + .num_resources = 0,
  3703. +};
  3704. +
  3705. +static struct i2c_board_info __initdata snd_pcm512x_hbdacplus_i2c_devices[] = {
  3706. + {
  3707. + I2C_BOARD_INFO("pcm5122", 0x4d)
  3708. + },
  3709. +};
  3710. +#endif
  3711. +
  3712. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  3713. +static struct platform_device snd_hifiberry_digi_device = {
  3714. + .name = "snd-hifiberry-digi",
  3715. + .id = 0,
  3716. + .num_resources = 0,
  3717. +};
  3718. +
  3719. +static struct i2c_board_info __initdata snd_wm8804_i2c_devices[] = {
  3720. + {
  3721. + I2C_BOARD_INFO("wm8804", 0x3b)
  3722. + },
  3723. +};
  3724. +
  3725. +#endif
  3726. +
  3727. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  3728. +static struct platform_device snd_rpi_dac_device = {
  3729. + .name = "snd-rpi-dac",
  3730. + .id = 0,
  3731. + .num_resources = 0,
  3732. +};
  3733. +
  3734. +static struct platform_device snd_pcm1794a_codec_device = {
  3735. + .name = "pcm1794a-codec",
  3736. + .id = -1,
  3737. + .num_resources = 0,
  3738. +};
  3739. +#endif
  3740. +
  3741. +
  3742. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  3743. +static struct platform_device snd_rpi_iqaudio_dac_device = {
  3744. + .name = "snd-rpi-iqaudio-dac",
  3745. + .id = 0,
  3746. + .num_resources = 0,
  3747. +};
  3748. +
  3749. +// Use the actual device name rather than generic driver name
  3750. +static struct i2c_board_info __initdata snd_pcm512x_i2c_devices[] = {
  3751. + {
  3752. + I2C_BOARD_INFO("pcm5122", 0x4c)
  3753. + },
  3754. +};
  3755. +#endif
  3756. +
  3757. +int __init bcm_register_device(struct platform_device *pdev)
  3758. +{
  3759. + int ret;
  3760. +
  3761. + ret = platform_device_register(pdev);
  3762. + if (ret)
  3763. + pr_debug("Unable to register platform device '%s': %d\n",
  3764. + pdev->name, ret);
  3765. +
  3766. + return ret;
  3767. +}
  3768. +
  3769. +/*
  3770. + * Use this macro for platform devices that are present in the Device Tree.
  3771. + * This way the device is only added on non-DT builds.
  3772. + */
  3773. +#ifdef CONFIG_OF
  3774. +#define bcm_register_device_dt(pdev)
  3775. +#else
  3776. +#define bcm_register_device_dt(pdev) bcm_register_device(pdev)
  3777. +#endif
  3778. +
  3779. +int calc_rsts(int partition)
  3780. +{
  3781. + return PM_PASSWORD |
  3782. + ((partition & (1 << 0)) << 0) |
  3783. + ((partition & (1 << 1)) << 1) |
  3784. + ((partition & (1 << 2)) << 2) |
  3785. + ((partition & (1 << 3)) << 3) |
  3786. + ((partition & (1 << 4)) << 4) |
  3787. + ((partition & (1 << 5)) << 5);
  3788. +}
  3789. +
  3790. +static void bcm2708_restart(enum reboot_mode mode, const char *cmd)
  3791. +{
  3792. + extern char bcm2708_reboot_mode;
  3793. + uint32_t pm_rstc, pm_wdog;
  3794. + uint32_t timeout = 10;
  3795. + uint32_t pm_rsts = 0;
  3796. +
  3797. + if(bcm2708_reboot_mode == 'q')
  3798. + {
  3799. + // NOOBS < 1.3 booting with reboot=q
  3800. + pm_rsts = readl(__io_address(PM_RSTS));
  3801. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRQ_SET;
  3802. + }
  3803. + else if(bcm2708_reboot_mode == 'p')
  3804. + {
  3805. + // NOOBS < 1.3 halting
  3806. + pm_rsts = readl(__io_address(PM_RSTS));
  3807. + pm_rsts = PM_PASSWORD | pm_rsts | PM_RSTS_HADWRH_SET;
  3808. + }
  3809. + else
  3810. + {
  3811. + pm_rsts = calc_rsts(reboot_part);
  3812. + }
  3813. +
  3814. + writel(pm_rsts, __io_address(PM_RSTS));
  3815. +
  3816. + /* Setup watchdog for reset */
  3817. + pm_rstc = readl(__io_address(PM_RSTC));
  3818. +
  3819. + pm_wdog = PM_PASSWORD | (timeout & PM_WDOG_TIME_SET); // watchdog timer = timer clock / 16; need password (31:16) + value (11:0)
  3820. + pm_rstc = PM_PASSWORD | (pm_rstc & PM_RSTC_WRCFG_CLR) | PM_RSTC_WRCFG_FULL_RESET;
  3821. +
  3822. + writel(pm_wdog, __io_address(PM_WDOG));
  3823. + writel(pm_rstc, __io_address(PM_RSTC));
  3824. +}
  3825. +
  3826. +/* We can't really power off, but if we do the normal reset scheme, and indicate to bootcode.bin not to reboot, then most of the chip will be powered off */
  3827. +static void bcm2708_power_off(void)
  3828. +{
  3829. + extern char bcm2708_reboot_mode;
  3830. + if(bcm2708_reboot_mode == 'q')
  3831. + {
  3832. + // NOOBS < v1.3
  3833. + bcm2708_restart('p', "");
  3834. + }
  3835. + else
  3836. + {
  3837. + /* partition 63 is special code for HALT the bootloader knows not to boot*/
  3838. + reboot_part = 63;
  3839. + /* continue with normal reset mechanism */
  3840. + bcm2708_restart(0, "");
  3841. + }
  3842. +}
  3843. +
  3844. +#ifdef CONFIG_OF
  3845. +static void __init bcm2708_dt_init(void)
  3846. +{
  3847. + int ret;
  3848. +
  3849. + of_clk_init(NULL);
  3850. + ret = of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
  3851. + if (ret) {
  3852. + pr_err("of_platform_populate failed: %d\n", ret);
  3853. + BUG();
  3854. + }
  3855. +}
  3856. +#else
  3857. +static void __init bcm2708_dt_init(void) { }
  3858. +#endif /* CONFIG_OF */
  3859. +
  3860. +void __init bcm2708_init(void)
  3861. +{
  3862. + int i;
  3863. +
  3864. +#if defined(CONFIG_BCM_VC_CMA)
  3865. + vc_cma_early_init();
  3866. +#endif
  3867. + printk("bcm2708.uart_clock = %d\n", uart_clock);
  3868. + pm_power_off = bcm2708_power_off;
  3869. +
  3870. + bcm2708_init_clocks();
  3871. + bcm2708_dt_init();
  3872. +
  3873. + bcm_register_device(&bcm2708_dmaman_device);
  3874. + bcm_register_device(&bcm2708_vcio_device);
  3875. +#ifdef CONFIG_BCM2708_GPIO
  3876. + bcm_register_device(&bcm2708_gpio_device);
  3877. +#endif
  3878. +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
  3879. + w1_gpio_pdata.pin = w1_gpio_pin;
  3880. + w1_gpio_pdata.ext_pullup_enable_pin = w1_gpio_pullup;
  3881. + platform_device_register(&w1_device);
  3882. +#endif
  3883. + bcm_register_device(&bcm2708_systemtimer_device);
  3884. + bcm_register_device(&bcm2708_fb_device);
  3885. + bcm_register_device(&bcm2708_usb_device);
  3886. + bcm_register_device(&bcm2708_uart1_device);
  3887. + bcm_register_device(&bcm2708_powerman_device);
  3888. +
  3889. +#ifdef CONFIG_MMC_SDHCI_BCM2708
  3890. + if (!bcm2835_mmc)
  3891. + bcm_register_device(&bcm2708_emmc_device);
  3892. +#endif
  3893. +#ifdef CONFIG_MMC_BCM2835
  3894. + if (bcm2835_mmc)
  3895. + bcm_register_device(&bcm2835_emmc_device);
  3896. +#endif
  3897. + bcm2708_init_led();
  3898. + for (i = 0; i < ARRAY_SIZE(bcm2708_alsa_devices); i++)
  3899. + bcm_register_device(&bcm2708_alsa_devices[i]);
  3900. +
  3901. + bcm_register_device_dt(&bcm2708_spi_device);
  3902. + bcm_register_device_dt(&bcm2708_bsc0_device);
  3903. + bcm_register_device_dt(&bcm2708_bsc1_device);
  3904. +
  3905. + bcm_register_device(&bcm2835_hwmon_device);
  3906. + bcm_register_device(&bcm2835_thermal_device);
  3907. +
  3908. +#ifdef CONFIG_SND_BCM2708_SOC_I2S_MODULE
  3909. + bcm_register_device(&bcm2708_i2s_device);
  3910. +#endif
  3911. +
  3912. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC_MODULE)
  3913. + bcm_register_device(&snd_hifiberry_dac_device);
  3914. + bcm_register_device(&snd_pcm5102a_codec_device);
  3915. +#endif
  3916. +
  3917. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS_MODULE)
  3918. + bcm_register_device(&snd_rpi_hifiberry_dacplus_device);
  3919. + i2c_register_board_info(1, snd_pcm512x_hbdacplus_i2c_devices, ARRAY_SIZE(snd_pcm512x_hbdacplus_i2c_devices));
  3920. +#endif
  3921. +
  3922. +#if defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) || defined(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI_MODULE)
  3923. + bcm_register_device(&snd_hifiberry_digi_device);
  3924. + i2c_register_board_info(1, snd_wm8804_i2c_devices, ARRAY_SIZE(snd_wm8804_i2c_devices));
  3925. +#endif
  3926. +
  3927. +#if defined(CONFIG_SND_BCM2708_SOC_RPI_DAC) || defined(CONFIG_SND_BCM2708_SOC_RPI_DAC_MODULE)
  3928. + bcm_register_device(&snd_rpi_dac_device);
  3929. + bcm_register_device(&snd_pcm1794a_codec_device);
  3930. +#endif
  3931. +
  3932. +#if defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) || defined(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC_MODULE)
  3933. + bcm_register_device(&snd_rpi_iqaudio_dac_device);
  3934. + i2c_register_board_info(1, snd_pcm512x_i2c_devices, ARRAY_SIZE(snd_pcm512x_i2c_devices));
  3935. +#endif
  3936. +
  3937. +
  3938. + for (i = 0; i < ARRAY_SIZE(amba_devs); i++) {
  3939. + struct amba_device *d = amba_devs[i];
  3940. + amba_device_register(d, &iomem_resource);
  3941. + }
  3942. + system_rev = boardrev;
  3943. + system_serial_low = serial;
  3944. +
  3945. +#ifdef CONFIG_BCM2708_SPIDEV
  3946. + spi_register_board_info(bcm2708_spi_devices,
  3947. + ARRAY_SIZE(bcm2708_spi_devices));
  3948. +#endif
  3949. +}
  3950. +
  3951. +static void timer_set_mode(enum clock_event_mode mode,
  3952. + struct clock_event_device *clk)
  3953. +{
  3954. + switch (mode) {
  3955. + case CLOCK_EVT_MODE_ONESHOT: /* Leave the timer disabled, .set_next_event will enable it */
  3956. + case CLOCK_EVT_MODE_SHUTDOWN:
  3957. + break;
  3958. + case CLOCK_EVT_MODE_PERIODIC:
  3959. +
  3960. + case CLOCK_EVT_MODE_UNUSED:
  3961. + case CLOCK_EVT_MODE_RESUME:
  3962. +
  3963. + default:
  3964. + printk(KERN_ERR "timer_set_mode: unhandled mode:%d\n",
  3965. + (int)mode);
  3966. + break;
  3967. + }
  3968. +
  3969. +}
  3970. +
  3971. +static int timer_set_next_event(unsigned long cycles,
  3972. + struct clock_event_device *unused)
  3973. +{
  3974. + unsigned long stc;
  3975. + do {
  3976. + stc = readl(__io_address(ST_BASE + 0x04));
  3977. + /* We could take a FIQ here, which may push ST above STC3 */
  3978. + writel(stc + cycles, __io_address(ST_BASE + 0x18));
  3979. + } while ((signed long) cycles >= 0 &&
  3980. + (signed long) (readl(__io_address(ST_BASE + 0x04)) - stc)
  3981. + >= (signed long) cycles);
  3982. + return 0;
  3983. +}
  3984. +
  3985. +static struct clock_event_device timer0_clockevent = {
  3986. + .name = "timer0",
  3987. + .shift = 32,
  3988. + .features = CLOCK_EVT_FEAT_ONESHOT,
  3989. + .set_mode = timer_set_mode,
  3990. + .set_next_event = timer_set_next_event,
  3991. +};
  3992. +
  3993. +/*
  3994. + * IRQ handler for the timer
  3995. + */
  3996. +static irqreturn_t bcm2708_timer_interrupt(int irq, void *dev_id)
  3997. +{
  3998. + struct clock_event_device *evt = &timer0_clockevent;
  3999. +
  4000. + writel(1 << 3, __io_address(ST_BASE + 0x00)); /* stcs clear timer int */
  4001. +
  4002. + evt->event_handler(evt);
  4003. +
  4004. + return IRQ_HANDLED;
  4005. +}
  4006. +
  4007. +static struct irqaction bcm2708_timer_irq = {
  4008. + .name = "BCM2708 Timer Tick",
  4009. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  4010. + .handler = bcm2708_timer_interrupt,
  4011. +};
  4012. +
  4013. +/*
  4014. + * Set up timer interrupt, and return the current time in seconds.
  4015. + */
  4016. +
  4017. +static struct delay_timer bcm2708_delay_timer = {
  4018. + .read_current_timer = bcm2708_read_current_timer,
  4019. + .freq = STC_FREQ_HZ,
  4020. +};
  4021. +
  4022. +static void __init bcm2708_timer_init(void)
  4023. +{
  4024. + /* init high res timer */
  4025. + bcm2708_clocksource_init();
  4026. +
  4027. + /*
  4028. + * Initialise to a known state (all timers off)
  4029. + */
  4030. + writel(0, __io_address(ARM_T_CONTROL));
  4031. + /*
  4032. + * Make irqs happen for the system timer
  4033. + */
  4034. + setup_irq(IRQ_TIMER3, &bcm2708_timer_irq);
  4035. +
  4036. + sched_clock_register(bcm2708_read_sched_clock, 32, STC_FREQ_HZ);
  4037. +
  4038. + timer0_clockevent.mult =
  4039. + div_sc(STC_FREQ_HZ, NSEC_PER_SEC, timer0_clockevent.shift);
  4040. + timer0_clockevent.max_delta_ns =
  4041. + clockevent_delta2ns(0xffffffff, &timer0_clockevent);
  4042. + timer0_clockevent.min_delta_ns =
  4043. + clockevent_delta2ns(0xf, &timer0_clockevent);
  4044. +
  4045. + timer0_clockevent.cpumask = cpumask_of(0);
  4046. + clockevents_register_device(&timer0_clockevent);
  4047. +
  4048. + register_current_timer_delay(&bcm2708_delay_timer);
  4049. +}
  4050. +
  4051. +#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
  4052. +#include <linux/leds.h>
  4053. +
  4054. +static struct gpio_led bcm2708_leds[] = {
  4055. + [0] = {
  4056. + .gpio = 16,
  4057. + .name = "led0",
  4058. + .default_trigger = "mmc0",
  4059. + .active_low = 1,
  4060. + },
  4061. +};
  4062. +
  4063. +static struct gpio_led_platform_data bcm2708_led_pdata = {
  4064. + .num_leds = ARRAY_SIZE(bcm2708_leds),
  4065. + .leds = bcm2708_leds,
  4066. +};
  4067. +
  4068. +static struct platform_device bcm2708_led_device = {
  4069. + .name = "leds-gpio",
  4070. + .id = -1,
  4071. + .dev = {
  4072. + .platform_data = &bcm2708_led_pdata,
  4073. + },
  4074. +};
  4075. +
  4076. +static void __init bcm2708_init_led(void)
  4077. +{
  4078. + bcm2708_leds[0].gpio = disk_led_gpio;
  4079. + bcm2708_leds[0].active_low = disk_led_active_low;
  4080. + platform_device_register(&bcm2708_led_device);
  4081. +}
  4082. +#else
  4083. +static inline void bcm2708_init_led(void)
  4084. +{
  4085. +}
  4086. +#endif
  4087. +
  4088. +void __init bcm2708_init_early(void)
  4089. +{
  4090. + /*
  4091. + * Some devices allocate their coherent buffers from atomic
  4092. + * context. Increase size of atomic coherent pool to make sure such
  4093. + * the allocations won't fail.
  4094. + */
  4095. + init_dma_coherent_pool_size(SZ_4M);
  4096. +}
  4097. +
  4098. +static void __init board_reserve(void)
  4099. +{
  4100. +#if defined(CONFIG_BCM_VC_CMA)
  4101. + vc_cma_reserve();
  4102. +#endif
  4103. +}
  4104. +
  4105. +static const char * const bcm2708_compat[] = {
  4106. + "brcm,bcm2708",
  4107. + NULL
  4108. +};
  4109. +
  4110. +MACHINE_START(BCM2708, "BCM2708")
  4111. + /* Maintainer: Broadcom Europe Ltd. */
  4112. + .map_io = bcm2708_map_io,
  4113. + .init_irq = bcm2708_init_irq,
  4114. + .init_time = bcm2708_timer_init,
  4115. + .init_machine = bcm2708_init,
  4116. + .init_early = bcm2708_init_early,
  4117. + .reserve = board_reserve,
  4118. + .restart = bcm2708_restart,
  4119. + .dt_compat = bcm2708_compat,
  4120. +MACHINE_END
  4121. +
  4122. +module_param(boardrev, uint, 0644);
  4123. +module_param(serial, uint, 0644);
  4124. +module_param(uart_clock, uint, 0644);
  4125. +module_param(disk_led_gpio, uint, 0644);
  4126. +module_param(disk_led_active_low, uint, 0644);
  4127. +module_param(reboot_part, uint, 0644);
  4128. +module_param(w1_gpio_pin, uint, 0644);
  4129. +module_param(w1_gpio_pullup, uint, 0644);
  4130. +module_param(bcm2835_mmc, uint, 0644);
  4131. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/bcm2708_gpio.c linux-3.16-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c
  4132. --- linux-3.16.2/arch/arm/mach-bcm2708/bcm2708_gpio.c 1970-01-01 01:00:00.000000000 +0100
  4133. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/bcm2708_gpio.c 2014-06-29 11:34:17.000000000 +0200
  4134. @@ -0,0 +1,361 @@
  4135. +/*
  4136. + * linux/arch/arm/mach-bcm2708/bcm2708_gpio.c
  4137. + *
  4138. + * Copyright (C) 2010 Broadcom
  4139. + *
  4140. + * This program is free software; you can redistribute it and/or modify
  4141. + * it under the terms of the GNU General Public License version 2 as
  4142. + * published by the Free Software Foundation.
  4143. + *
  4144. + */
  4145. +
  4146. +#include <linux/spinlock.h>
  4147. +#include <linux/module.h>
  4148. +#include <linux/list.h>
  4149. +#include <linux/io.h>
  4150. +#include <linux/irq.h>
  4151. +#include <linux/interrupt.h>
  4152. +#include <linux/slab.h>
  4153. +#include <mach/gpio.h>
  4154. +#include <linux/gpio.h>
  4155. +#include <linux/platform_device.h>
  4156. +#include <mach/platform.h>
  4157. +
  4158. +#define BCM_GPIO_DRIVER_NAME "bcm2708_gpio"
  4159. +#define DRIVER_NAME BCM_GPIO_DRIVER_NAME
  4160. +#define BCM_GPIO_USE_IRQ 1
  4161. +
  4162. +#define GPIOFSEL(x) (0x00+(x)*4)
  4163. +#define GPIOSET(x) (0x1c+(x)*4)
  4164. +#define GPIOCLR(x) (0x28+(x)*4)
  4165. +#define GPIOLEV(x) (0x34+(x)*4)
  4166. +#define GPIOEDS(x) (0x40+(x)*4)
  4167. +#define GPIOREN(x) (0x4c+(x)*4)
  4168. +#define GPIOFEN(x) (0x58+(x)*4)
  4169. +#define GPIOHEN(x) (0x64+(x)*4)
  4170. +#define GPIOLEN(x) (0x70+(x)*4)
  4171. +#define GPIOAREN(x) (0x7c+(x)*4)
  4172. +#define GPIOAFEN(x) (0x88+(x)*4)
  4173. +#define GPIOUD(x) (0x94+(x)*4)
  4174. +#define GPIOUDCLK(x) (0x98+(x)*4)
  4175. +
  4176. +enum { GPIO_FSEL_INPUT, GPIO_FSEL_OUTPUT,
  4177. + GPIO_FSEL_ALT5, GPIO_FSEL_ALT_4,
  4178. + GPIO_FSEL_ALT0, GPIO_FSEL_ALT1,
  4179. + GPIO_FSEL_ALT2, GPIO_FSEL_ALT3,
  4180. +};
  4181. +
  4182. + /* Each of the two spinlocks protects a different set of hardware
  4183. + * regiters and data structurs. This decouples the code of the IRQ from
  4184. + * the GPIO code. This also makes the case of a GPIO routine call from
  4185. + * the IRQ code simpler.
  4186. + */
  4187. +static DEFINE_SPINLOCK(lock); /* GPIO registers */
  4188. +
  4189. +struct bcm2708_gpio {
  4190. + struct list_head list;
  4191. + void __iomem *base;
  4192. + struct gpio_chip gc;
  4193. + unsigned long rising;
  4194. + unsigned long falling;
  4195. + unsigned long high;
  4196. + unsigned long low;
  4197. +};
  4198. +
  4199. +static int bcm2708_set_function(struct gpio_chip *gc, unsigned offset,
  4200. + int function)
  4201. +{
  4202. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  4203. + unsigned long flags;
  4204. + unsigned gpiodir;
  4205. + unsigned gpio_bank = offset / 10;
  4206. + unsigned gpio_field_offset = (offset - 10 * gpio_bank) * 3;
  4207. +
  4208. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set_function %p (%d,%d)\n", gc, offset, function);
  4209. + if (offset >= BCM2708_NR_GPIOS)
  4210. + return -EINVAL;
  4211. +
  4212. + spin_lock_irqsave(&lock, flags);
  4213. +
  4214. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  4215. + gpiodir &= ~(7 << gpio_field_offset);
  4216. + gpiodir |= function << gpio_field_offset;
  4217. + writel(gpiodir, gpio->base + GPIOFSEL(gpio_bank));
  4218. + spin_unlock_irqrestore(&lock, flags);
  4219. + gpiodir = readl(gpio->base + GPIOFSEL(gpio_bank));
  4220. +
  4221. + return 0;
  4222. +}
  4223. +
  4224. +static int bcm2708_gpio_dir_in(struct gpio_chip *gc, unsigned offset)
  4225. +{
  4226. + return bcm2708_set_function(gc, offset, GPIO_FSEL_INPUT);
  4227. +}
  4228. +
  4229. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value);
  4230. +static int bcm2708_gpio_dir_out(struct gpio_chip *gc, unsigned offset,
  4231. + int value)
  4232. +{
  4233. + int ret;
  4234. + ret = bcm2708_set_function(gc, offset, GPIO_FSEL_OUTPUT);
  4235. + if (ret >= 0)
  4236. + bcm2708_gpio_set(gc, offset, value);
  4237. + return ret;
  4238. +}
  4239. +
  4240. +static int bcm2708_gpio_get(struct gpio_chip *gc, unsigned offset)
  4241. +{
  4242. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  4243. + unsigned gpio_bank = offset / 32;
  4244. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  4245. + unsigned lev;
  4246. +
  4247. + if (offset >= BCM2708_NR_GPIOS)
  4248. + return 0;
  4249. + lev = readl(gpio->base + GPIOLEV(gpio_bank));
  4250. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_get %p (%d)=%d\n", gc, offset, 0x1 & (lev>>gpio_field_offset));
  4251. + return 0x1 & (lev >> gpio_field_offset);
  4252. +}
  4253. +
  4254. +static void bcm2708_gpio_set(struct gpio_chip *gc, unsigned offset, int value)
  4255. +{
  4256. + struct bcm2708_gpio *gpio = container_of(gc, struct bcm2708_gpio, gc);
  4257. + unsigned gpio_bank = offset / 32;
  4258. + unsigned gpio_field_offset = (offset - 32 * gpio_bank);
  4259. +//printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_set %p (%d=%d)\n", gc, offset, value);
  4260. + if (offset >= BCM2708_NR_GPIOS)
  4261. + return;
  4262. + if (value)
  4263. + writel(1 << gpio_field_offset, gpio->base + GPIOSET(gpio_bank));
  4264. + else
  4265. + writel(1 << gpio_field_offset, gpio->base + GPIOCLR(gpio_bank));
  4266. +}
  4267. +
  4268. +/*************************************************************************************************************************
  4269. + * bcm2708 GPIO IRQ
  4270. + */
  4271. +
  4272. +#if BCM_GPIO_USE_IRQ
  4273. +
  4274. +static int bcm2708_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  4275. +{
  4276. + return gpio_to_irq(gpio);
  4277. +}
  4278. +
  4279. +static int bcm2708_gpio_irq_set_type(struct irq_data *d, unsigned type)
  4280. +{
  4281. + unsigned irq = d->irq;
  4282. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  4283. +
  4284. + gpio->rising &= ~(1 << irq_to_gpio(irq));
  4285. + gpio->falling &= ~(1 << irq_to_gpio(irq));
  4286. + gpio->high &= ~(1 << irq_to_gpio(irq));
  4287. + gpio->low &= ~(1 << irq_to_gpio(irq));
  4288. +
  4289. + if (type & ~(IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  4290. + return -EINVAL;
  4291. +
  4292. + if (type & IRQ_TYPE_EDGE_RISING)
  4293. + gpio->rising |= (1 << irq_to_gpio(irq));
  4294. + if (type & IRQ_TYPE_EDGE_FALLING)
  4295. + gpio->falling |= (1 << irq_to_gpio(irq));
  4296. + if (type & IRQ_TYPE_LEVEL_HIGH)
  4297. + gpio->high |= (1 << irq_to_gpio(irq));
  4298. + if (type & IRQ_TYPE_LEVEL_LOW)
  4299. + gpio->low |= (1 << irq_to_gpio(irq));
  4300. + return 0;
  4301. +}
  4302. +
  4303. +static void bcm2708_gpio_irq_mask(struct irq_data *d)
  4304. +{
  4305. + unsigned irq = d->irq;
  4306. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  4307. + unsigned gn = irq_to_gpio(irq);
  4308. + unsigned gb = gn / 32;
  4309. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  4310. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  4311. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  4312. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  4313. +
  4314. + gn = gn % 32;
  4315. +
  4316. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  4317. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  4318. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  4319. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  4320. +}
  4321. +
  4322. +static void bcm2708_gpio_irq_unmask(struct irq_data *d)
  4323. +{
  4324. + unsigned irq = d->irq;
  4325. + struct bcm2708_gpio *gpio = irq_get_chip_data(irq);
  4326. + unsigned gn = irq_to_gpio(irq);
  4327. + unsigned gb = gn / 32;
  4328. + unsigned long rising = readl(gpio->base + GPIOREN(gb));
  4329. + unsigned long falling = readl(gpio->base + GPIOFEN(gb));
  4330. + unsigned long high = readl(gpio->base + GPIOHEN(gb));
  4331. + unsigned long low = readl(gpio->base + GPIOLEN(gb));
  4332. +
  4333. + gn = gn % 32;
  4334. +
  4335. + writel(1 << gn, gpio->base + GPIOEDS(gb));
  4336. +
  4337. + if (gpio->rising & (1 << gn)) {
  4338. + writel(rising | (1 << gn), gpio->base + GPIOREN(gb));
  4339. + } else {
  4340. + writel(rising & ~(1 << gn), gpio->base + GPIOREN(gb));
  4341. + }
  4342. +
  4343. + if (gpio->falling & (1 << gn)) {
  4344. + writel(falling | (1 << gn), gpio->base + GPIOFEN(gb));
  4345. + } else {
  4346. + writel(falling & ~(1 << gn), gpio->base + GPIOFEN(gb));
  4347. + }
  4348. +
  4349. + if (gpio->high & (1 << gn)) {
  4350. + writel(high | (1 << gn), gpio->base + GPIOHEN(gb));
  4351. + } else {
  4352. + writel(high & ~(1 << gn), gpio->base + GPIOHEN(gb));
  4353. + }
  4354. +
  4355. + if (gpio->low & (1 << gn)) {
  4356. + writel(low | (1 << gn), gpio->base + GPIOLEN(gb));
  4357. + } else {
  4358. + writel(low & ~(1 << gn), gpio->base + GPIOLEN(gb));
  4359. + }
  4360. +}
  4361. +
  4362. +static struct irq_chip bcm2708_irqchip = {
  4363. + .name = "GPIO",
  4364. + .irq_enable = bcm2708_gpio_irq_unmask,
  4365. + .irq_disable = bcm2708_gpio_irq_mask,
  4366. + .irq_unmask = bcm2708_gpio_irq_unmask,
  4367. + .irq_mask = bcm2708_gpio_irq_mask,
  4368. + .irq_set_type = bcm2708_gpio_irq_set_type,
  4369. +};
  4370. +
  4371. +static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
  4372. +{
  4373. + unsigned long edsr;
  4374. + unsigned bank;
  4375. + int i;
  4376. + unsigned gpio;
  4377. + for (bank = 0; bank <= 1; bank++) {
  4378. + edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
  4379. + for_each_set_bit(i, &edsr, 32) {
  4380. + gpio = i + bank * 32;
  4381. + generic_handle_irq(gpio_to_irq(gpio));
  4382. + }
  4383. + writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank));
  4384. + }
  4385. + return IRQ_HANDLED;
  4386. +}
  4387. +
  4388. +static struct irqaction bcm2708_gpio_irq = {
  4389. + .name = "BCM2708 GPIO catchall handler",
  4390. + .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  4391. + .handler = bcm2708_gpio_interrupt,
  4392. +};
  4393. +
  4394. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  4395. +{
  4396. + unsigned irq;
  4397. +
  4398. + ucb->gc.to_irq = bcm2708_gpio_to_irq;
  4399. +
  4400. + for (irq = GPIO_IRQ_START; irq < (GPIO_IRQ_START + GPIO_IRQS); irq++) {
  4401. + irq_set_chip_data(irq, ucb);
  4402. + irq_set_chip(irq, &bcm2708_irqchip);
  4403. + set_irq_flags(irq, IRQF_VALID);
  4404. + }
  4405. + setup_irq(IRQ_GPIO3, &bcm2708_gpio_irq);
  4406. +}
  4407. +
  4408. +#else
  4409. +
  4410. +static void bcm2708_gpio_irq_init(struct bcm2708_gpio *ucb)
  4411. +{
  4412. +}
  4413. +
  4414. +#endif /* #if BCM_GPIO_USE_IRQ ***************************************************************************************************************** */
  4415. +
  4416. +static int bcm2708_gpio_probe(struct platform_device *dev)
  4417. +{
  4418. + struct bcm2708_gpio *ucb;
  4419. + struct resource *res;
  4420. + int err = 0;
  4421. +
  4422. + printk(KERN_INFO DRIVER_NAME ": bcm2708_gpio_probe %p\n", dev);
  4423. +
  4424. + ucb = kzalloc(sizeof(*ucb), GFP_KERNEL);
  4425. + if (NULL == ucb) {
  4426. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  4427. + "mailbox memory\n");
  4428. + err = -ENOMEM;
  4429. + goto err;
  4430. + }
  4431. +
  4432. + res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  4433. +
  4434. + platform_set_drvdata(dev, ucb);
  4435. + ucb->base = __io_address(GPIO_BASE);
  4436. +
  4437. + ucb->gc.label = "bcm2708_gpio";
  4438. + ucb->gc.base = 0;
  4439. + ucb->gc.ngpio = BCM2708_NR_GPIOS;
  4440. + ucb->gc.owner = THIS_MODULE;
  4441. +
  4442. + ucb->gc.direction_input = bcm2708_gpio_dir_in;
  4443. + ucb->gc.direction_output = bcm2708_gpio_dir_out;
  4444. + ucb->gc.get = bcm2708_gpio_get;
  4445. + ucb->gc.set = bcm2708_gpio_set;
  4446. + ucb->gc.can_sleep = 0;
  4447. +
  4448. + bcm2708_gpio_irq_init(ucb);
  4449. +
  4450. + err = gpiochip_add(&ucb->gc);
  4451. + if (err)
  4452. + goto err;
  4453. +
  4454. +err:
  4455. + return err;
  4456. +
  4457. +}
  4458. +
  4459. +static int bcm2708_gpio_remove(struct platform_device *dev)
  4460. +{
  4461. + int err = 0;
  4462. + struct bcm2708_gpio *ucb = platform_get_drvdata(dev);
  4463. +
  4464. + printk(KERN_ERR DRIVER_NAME ": bcm2708_gpio_remove %p\n", dev);
  4465. +
  4466. + err = gpiochip_remove(&ucb->gc);
  4467. +
  4468. + platform_set_drvdata(dev, NULL);
  4469. + kfree(ucb);
  4470. +
  4471. + return err;
  4472. +}
  4473. +
  4474. +static struct platform_driver bcm2708_gpio_driver = {
  4475. + .probe = bcm2708_gpio_probe,
  4476. + .remove = bcm2708_gpio_remove,
  4477. + .driver = {
  4478. + .name = "bcm2708_gpio"},
  4479. +};
  4480. +
  4481. +static int __init bcm2708_gpio_init(void)
  4482. +{
  4483. + return platform_driver_register(&bcm2708_gpio_driver);
  4484. +}
  4485. +
  4486. +static void __exit bcm2708_gpio_exit(void)
  4487. +{
  4488. + platform_driver_unregister(&bcm2708_gpio_driver);
  4489. +}
  4490. +
  4491. +module_init(bcm2708_gpio_init);
  4492. +module_exit(bcm2708_gpio_exit);
  4493. +
  4494. +MODULE_DESCRIPTION("Broadcom BCM2708 GPIO driver");
  4495. +MODULE_LICENSE("GPL");
  4496. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/bcm2708.h linux-3.16-rpi/arch/arm/mach-bcm2708/bcm2708.h
  4497. --- linux-3.16.2/arch/arm/mach-bcm2708/bcm2708.h 1970-01-01 01:00:00.000000000 +0100
  4498. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/bcm2708.h 2014-09-14 19:03:05.000000000 +0200
  4499. @@ -0,0 +1,49 @@
  4500. +/*
  4501. + * linux/arch/arm/mach-bcm2708/bcm2708.h
  4502. + *
  4503. + * BCM2708 machine support header
  4504. + *
  4505. + * Copyright (C) 2010 Broadcom
  4506. + *
  4507. + * This program is free software; you can redistribute it and/or modify
  4508. + * it under the terms of the GNU General Public License as published by
  4509. + * the Free Software Foundation; either version 2 of the License, or
  4510. + * (at your option) any later version.
  4511. + *
  4512. + * This program is distributed in the hope that it will be useful,
  4513. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4514. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4515. + * GNU General Public License for more details.
  4516. + *
  4517. + * You should have received a copy of the GNU General Public License
  4518. + * along with this program; if not, write to the Free Software
  4519. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4520. + */
  4521. +
  4522. +#ifndef __BCM2708_BCM2708_H
  4523. +#define __BCM2708_BCM2708_H
  4524. +
  4525. +#include <linux/amba/bus.h>
  4526. +
  4527. +extern void __init bcm2708_init(void);
  4528. +extern void __init bcm2708_init_irq(void);
  4529. +extern void __init bcm2708_map_io(void);
  4530. +extern struct sys_timer bcm2708_timer;
  4531. +extern unsigned int mmc_status(struct device *dev);
  4532. +
  4533. +#define AMBA_DEVICE(name, busid, base, plat) \
  4534. +static struct amba_device name##_device = { \
  4535. + .dev = { \
  4536. + .coherent_dma_mask = ~0, \
  4537. + .init_name = busid, \
  4538. + .platform_data = plat, \
  4539. + }, \
  4540. + .res = { \
  4541. + .start = base##_BASE, \
  4542. + .end = (base##_BASE) + SZ_4K - 1,\
  4543. + .flags = IORESOURCE_MEM, \
  4544. + }, \
  4545. + .irq = base##_IRQ, \
  4546. +}
  4547. +
  4548. +#endif
  4549. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/dma.c linux-3.16-rpi/arch/arm/mach-bcm2708/dma.c
  4550. --- linux-3.16.2/arch/arm/mach-bcm2708/dma.c 1970-01-01 01:00:00.000000000 +0100
  4551. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/dma.c 2014-09-14 19:03:05.000000000 +0200
  4552. @@ -0,0 +1,409 @@
  4553. +/*
  4554. + * linux/arch/arm/mach-bcm2708/dma.c
  4555. + *
  4556. + * Copyright (C) 2010 Broadcom
  4557. + *
  4558. + * This program is free software; you can redistribute it and/or modify
  4559. + * it under the terms of the GNU General Public License version 2 as
  4560. + * published by the Free Software Foundation.
  4561. + */
  4562. +
  4563. +#include <linux/slab.h>
  4564. +#include <linux/device.h>
  4565. +#include <linux/platform_device.h>
  4566. +#include <linux/module.h>
  4567. +#include <linux/scatterlist.h>
  4568. +
  4569. +#include <mach/dma.h>
  4570. +#include <mach/irqs.h>
  4571. +
  4572. +/*****************************************************************************\
  4573. + * *
  4574. + * Configuration *
  4575. + * *
  4576. +\*****************************************************************************/
  4577. +
  4578. +#define CACHE_LINE_MASK 31
  4579. +#define DRIVER_NAME BCM_DMAMAN_DRIVER_NAME
  4580. +#define DEFAULT_DMACHAN_BITMAP 0x10 /* channel 4 only */
  4581. +
  4582. +/* valid only for channels 0 - 14, 15 has its own base address */
  4583. +#define BCM2708_DMA_CHAN(n) ((n)<<8) /* base address */
  4584. +#define BCM2708_DMA_CHANIO(dma_base, n) \
  4585. + ((void __iomem *)((char *)(dma_base)+BCM2708_DMA_CHAN(n)))
  4586. +
  4587. +
  4588. +/*****************************************************************************\
  4589. + * *
  4590. + * DMA Auxilliary Functions *
  4591. + * *
  4592. +\*****************************************************************************/
  4593. +
  4594. +/* A DMA buffer on an arbitrary boundary may separate a cache line into a
  4595. + section inside the DMA buffer and another section outside it.
  4596. + Even if we flush DMA buffers from the cache there is always the chance that
  4597. + during a DMA someone will access the part of a cache line that is outside
  4598. + the DMA buffer - which will then bring in unwelcome data.
  4599. + Without being able to dictate our own buffer pools we must insist that
  4600. + DMA buffers consist of a whole number of cache lines.
  4601. +*/
  4602. +
  4603. +extern int
  4604. +bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len)
  4605. +{
  4606. + int i;
  4607. +
  4608. + for (i = 0; i < sg_len; i++) {
  4609. + if (sg_ptr[i].offset & CACHE_LINE_MASK ||
  4610. + sg_ptr[i].length & CACHE_LINE_MASK)
  4611. + return 0;
  4612. + }
  4613. +
  4614. + return 1;
  4615. +}
  4616. +EXPORT_SYMBOL_GPL(bcm_sg_suitable_for_dma);
  4617. +
  4618. +extern void
  4619. +bcm_dma_start(void __iomem *dma_chan_base, dma_addr_t control_block)
  4620. +{
  4621. + dsb(); /* ARM data synchronization (push) operation */
  4622. +
  4623. + writel(control_block, dma_chan_base + BCM2708_DMA_ADDR);
  4624. + writel(BCM2708_DMA_ACTIVE, dma_chan_base + BCM2708_DMA_CS);
  4625. +}
  4626. +
  4627. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base)
  4628. +{
  4629. + dsb();
  4630. +
  4631. + /* ugly busy wait only option for now */
  4632. + while (readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE)
  4633. + cpu_relax();
  4634. +}
  4635. +
  4636. +EXPORT_SYMBOL_GPL(bcm_dma_start);
  4637. +
  4638. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base)
  4639. +{
  4640. + dsb();
  4641. +
  4642. + return readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_ACTIVE;
  4643. +}
  4644. +EXPORT_SYMBOL_GPL(bcm_dma_is_busy);
  4645. +
  4646. +/* Complete an ongoing DMA (assuming its results are to be ignored)
  4647. + Does nothing if there is no DMA in progress.
  4648. + This routine waits for the current AXI transfer to complete before
  4649. + terminating the current DMA. If the current transfer is hung on a DREQ used
  4650. + by an uncooperative peripheral the AXI transfer may never complete. In this
  4651. + case the routine times out and return a non-zero error code.
  4652. + Use of this routine doesn't guarantee that the ongoing or aborted DMA
  4653. + does not produce an interrupt.
  4654. +*/
  4655. +extern int
  4656. +bcm_dma_abort(void __iomem *dma_chan_base)
  4657. +{
  4658. + unsigned long int cs;
  4659. + int rc = 0;
  4660. +
  4661. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  4662. +
  4663. + if (BCM2708_DMA_ACTIVE & cs) {
  4664. + long int timeout = 10000;
  4665. +
  4666. + /* write 0 to the active bit - pause the DMA */
  4667. + writel(0, dma_chan_base + BCM2708_DMA_CS);
  4668. +
  4669. + /* wait for any current AXI transfer to complete */
  4670. + while (0 != (cs & BCM2708_DMA_ISPAUSED) && --timeout >= 0)
  4671. + cs = readl(dma_chan_base + BCM2708_DMA_CS);
  4672. +
  4673. + if (0 != (cs & BCM2708_DMA_ISPAUSED)) {
  4674. + /* we'll un-pause when we set of our next DMA */
  4675. + rc = -ETIMEDOUT;
  4676. +
  4677. + } else if (BCM2708_DMA_ACTIVE & cs) {
  4678. + /* terminate the control block chain */
  4679. + writel(0, dma_chan_base + BCM2708_DMA_NEXTCB);
  4680. +
  4681. + /* abort the whole DMA */
  4682. + writel(BCM2708_DMA_ABORT | BCM2708_DMA_ACTIVE,
  4683. + dma_chan_base + BCM2708_DMA_CS);
  4684. + }
  4685. + }
  4686. +
  4687. + return rc;
  4688. +}
  4689. +EXPORT_SYMBOL_GPL(bcm_dma_abort);
  4690. +
  4691. +
  4692. +/***************************************************************************** \
  4693. + * *
  4694. + * DMA Manager Device Methods *
  4695. + * *
  4696. +\*****************************************************************************/
  4697. +
  4698. +struct vc_dmaman {
  4699. + void __iomem *dma_base;
  4700. + u32 chan_available; /* bitmap of available channels */
  4701. + u32 has_feature[BCM_DMA_FEATURE_COUNT]; /* bitmap of feature presence */
  4702. +};
  4703. +
  4704. +static void vc_dmaman_init(struct vc_dmaman *dmaman, void __iomem *dma_base,
  4705. + u32 chans_available)
  4706. +{
  4707. + dmaman->dma_base = dma_base;
  4708. + dmaman->chan_available = chans_available;
  4709. + dmaman->has_feature[BCM_DMA_FEATURE_FAST_ORD] = 0x0c; /* chans 2 & 3 */
  4710. + dmaman->has_feature[BCM_DMA_FEATURE_BULK_ORD] = 0x01; /* chan 0 */
  4711. + dmaman->has_feature[BCM_DMA_FEATURE_NORMAL_ORD] = 0xfe; /* chans 1 to 7 */
  4712. + dmaman->has_feature[BCM_DMA_FEATURE_LITE_ORD] = 0x7f00; /* chans 8 to 14 */
  4713. +}
  4714. +
  4715. +static int vc_dmaman_chan_alloc(struct vc_dmaman *dmaman,
  4716. + unsigned preferred_feature_set)
  4717. +{
  4718. + u32 chans;
  4719. + int feature;
  4720. +
  4721. + chans = dmaman->chan_available;
  4722. + for (feature = 0; feature < BCM_DMA_FEATURE_COUNT; feature++)
  4723. + /* select the subset of available channels with the desired
  4724. + feature so long as some of the candidate channels have that
  4725. + feature */
  4726. + if ((preferred_feature_set & (1 << feature)) &&
  4727. + (chans & dmaman->has_feature[feature]))
  4728. + chans &= dmaman->has_feature[feature];
  4729. +
  4730. + if (chans) {
  4731. + int chan = 0;
  4732. + /* return the ordinal of the first channel in the bitmap */
  4733. + while (chans != 0 && (chans & 1) == 0) {
  4734. + chans >>= 1;
  4735. + chan++;
  4736. + }
  4737. + /* claim the channel */
  4738. + dmaman->chan_available &= ~(1 << chan);
  4739. + return chan;
  4740. + } else
  4741. + return -ENOMEM;
  4742. +}
  4743. +
  4744. +static int vc_dmaman_chan_free(struct vc_dmaman *dmaman, int chan)
  4745. +{
  4746. + if (chan < 0)
  4747. + return -EINVAL;
  4748. + else if ((1 << chan) & dmaman->chan_available)
  4749. + return -EIDRM;
  4750. + else {
  4751. + dmaman->chan_available |= (1 << chan);
  4752. + return 0;
  4753. + }
  4754. +}
  4755. +
  4756. +/*****************************************************************************\
  4757. + * *
  4758. + * DMA IRQs *
  4759. + * *
  4760. +\*****************************************************************************/
  4761. +
  4762. +static unsigned char bcm_dma_irqs[] = {
  4763. + IRQ_DMA0,
  4764. + IRQ_DMA1,
  4765. + IRQ_DMA2,
  4766. + IRQ_DMA3,
  4767. + IRQ_DMA4,
  4768. + IRQ_DMA5,
  4769. + IRQ_DMA6,
  4770. + IRQ_DMA7,
  4771. + IRQ_DMA8,
  4772. + IRQ_DMA9,
  4773. + IRQ_DMA10,
  4774. + IRQ_DMA11,
  4775. + IRQ_DMA12
  4776. +};
  4777. +
  4778. +
  4779. +/***************************************************************************** \
  4780. + * *
  4781. + * DMA Manager Monitor *
  4782. + * *
  4783. +\*****************************************************************************/
  4784. +
  4785. +static struct device *dmaman_dev; /* we assume there's only one! */
  4786. +
  4787. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  4788. + void __iomem **out_dma_base, int *out_dma_irq)
  4789. +{
  4790. + if (!dmaman_dev)
  4791. + return -ENODEV;
  4792. + else {
  4793. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  4794. + int rc;
  4795. +
  4796. + device_lock(dmaman_dev);
  4797. + rc = vc_dmaman_chan_alloc(dmaman, preferred_feature_set);
  4798. + if (rc >= 0) {
  4799. + *out_dma_base = BCM2708_DMA_CHANIO(dmaman->dma_base,
  4800. + rc);
  4801. + *out_dma_irq = bcm_dma_irqs[rc];
  4802. + }
  4803. + device_unlock(dmaman_dev);
  4804. +
  4805. + return rc;
  4806. + }
  4807. +}
  4808. +EXPORT_SYMBOL_GPL(bcm_dma_chan_alloc);
  4809. +
  4810. +extern int bcm_dma_chan_free(int channel)
  4811. +{
  4812. + if (dmaman_dev) {
  4813. + struct vc_dmaman *dmaman = dev_get_drvdata(dmaman_dev);
  4814. + int rc;
  4815. +
  4816. + device_lock(dmaman_dev);
  4817. + rc = vc_dmaman_chan_free(dmaman, channel);
  4818. + device_unlock(dmaman_dev);
  4819. +
  4820. + return rc;
  4821. + } else
  4822. + return -ENODEV;
  4823. +}
  4824. +EXPORT_SYMBOL_GPL(bcm_dma_chan_free);
  4825. +
  4826. +static int dev_dmaman_register(const char *dev_name, struct device *dev)
  4827. +{
  4828. + int rc = dmaman_dev ? -EINVAL : 0;
  4829. + dmaman_dev = dev;
  4830. + return rc;
  4831. +}
  4832. +
  4833. +static void dev_dmaman_deregister(const char *dev_name, struct device *dev)
  4834. +{
  4835. + dmaman_dev = NULL;
  4836. +}
  4837. +
  4838. +/*****************************************************************************\
  4839. + * *
  4840. + * DMA Device *
  4841. + * *
  4842. +\*****************************************************************************/
  4843. +
  4844. +static int dmachans = -1; /* module parameter */
  4845. +
  4846. +static int bcm_dmaman_probe(struct platform_device *pdev)
  4847. +{
  4848. + int ret = 0;
  4849. + struct vc_dmaman *dmaman;
  4850. + struct resource *dma_res = NULL;
  4851. + void __iomem *dma_base = NULL;
  4852. + int have_dma_region = 0;
  4853. +
  4854. + dmaman = kzalloc(sizeof(*dmaman), GFP_KERNEL);
  4855. + if (NULL == dmaman) {
  4856. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  4857. + "DMA management memory\n");
  4858. + ret = -ENOMEM;
  4859. + } else {
  4860. +
  4861. + dma_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4862. + if (dma_res == NULL) {
  4863. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  4864. + "resource\n");
  4865. + ret = -ENODEV;
  4866. + } else if (!request_mem_region(dma_res->start,
  4867. + resource_size(dma_res),
  4868. + DRIVER_NAME)) {
  4869. + dev_err(&pdev->dev, "cannot obtain DMA region\n");
  4870. + ret = -EBUSY;
  4871. + } else {
  4872. + have_dma_region = 1;
  4873. + dma_base = ioremap(dma_res->start,
  4874. + resource_size(dma_res));
  4875. + if (!dma_base) {
  4876. + dev_err(&pdev->dev, "cannot map DMA region\n");
  4877. + ret = -ENOMEM;
  4878. + } else {
  4879. + /* use module parameter if one was provided */
  4880. + if (dmachans > 0)
  4881. + vc_dmaman_init(dmaman, dma_base,
  4882. + dmachans);
  4883. + else
  4884. + vc_dmaman_init(dmaman, dma_base,
  4885. + DEFAULT_DMACHAN_BITMAP);
  4886. +
  4887. + platform_set_drvdata(pdev, dmaman);
  4888. + dev_dmaman_register(DRIVER_NAME, &pdev->dev);
  4889. +
  4890. + printk(KERN_INFO DRIVER_NAME ": DMA manager "
  4891. + "at %p\n", dma_base);
  4892. + }
  4893. + }
  4894. + }
  4895. + if (ret != 0) {
  4896. + if (dma_base)
  4897. + iounmap(dma_base);
  4898. + if (dma_res && have_dma_region)
  4899. + release_mem_region(dma_res->start,
  4900. + resource_size(dma_res));
  4901. + if (dmaman)
  4902. + kfree(dmaman);
  4903. + }
  4904. + return ret;
  4905. +}
  4906. +
  4907. +static int bcm_dmaman_remove(struct platform_device *pdev)
  4908. +{
  4909. + struct vc_dmaman *dmaman = platform_get_drvdata(pdev);
  4910. +
  4911. + platform_set_drvdata(pdev, NULL);
  4912. + dev_dmaman_deregister(DRIVER_NAME, &pdev->dev);
  4913. + kfree(dmaman);
  4914. +
  4915. + return 0;
  4916. +}
  4917. +
  4918. +static struct platform_driver bcm_dmaman_driver = {
  4919. + .probe = bcm_dmaman_probe,
  4920. + .remove = bcm_dmaman_remove,
  4921. +
  4922. + .driver = {
  4923. + .name = DRIVER_NAME,
  4924. + .owner = THIS_MODULE,
  4925. + },
  4926. +};
  4927. +
  4928. +/*****************************************************************************\
  4929. + * *
  4930. + * Driver init/exit *
  4931. + * *
  4932. +\*****************************************************************************/
  4933. +
  4934. +static int __init bcm_dmaman_drv_init(void)
  4935. +{
  4936. + int ret;
  4937. +
  4938. + ret = platform_driver_register(&bcm_dmaman_driver);
  4939. + if (ret != 0) {
  4940. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  4941. + "on platform\n");
  4942. + }
  4943. +
  4944. + return ret;
  4945. +}
  4946. +
  4947. +static void __exit bcm_dmaman_drv_exit(void)
  4948. +{
  4949. + platform_driver_unregister(&bcm_dmaman_driver);
  4950. +}
  4951. +
  4952. +module_init(bcm_dmaman_drv_init);
  4953. +module_exit(bcm_dmaman_drv_exit);
  4954. +
  4955. +module_param(dmachans, int, 0644);
  4956. +
  4957. +MODULE_AUTHOR("Gray Girling <grayg@broadcom.com>");
  4958. +MODULE_DESCRIPTION("DMA channel manager driver");
  4959. +MODULE_LICENSE("GPL");
  4960. +
  4961. +MODULE_PARM_DESC(dmachans, "Bitmap of DMA channels available to the ARM");
  4962. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/arm_control.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h
  4963. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/arm_control.h 1970-01-01 01:00:00.000000000 +0100
  4964. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/arm_control.h 2014-04-13 17:32:40.000000000 +0200
  4965. @@ -0,0 +1,419 @@
  4966. +/*
  4967. + * linux/arch/arm/mach-bcm2708/arm_control.h
  4968. + *
  4969. + * Copyright (C) 2010 Broadcom
  4970. + *
  4971. + * This program is free software; you can redistribute it and/or modify
  4972. + * it under the terms of the GNU General Public License as published by
  4973. + * the Free Software Foundation; either version 2 of the License, or
  4974. + * (at your option) any later version.
  4975. + *
  4976. + * This program is distributed in the hope that it will be useful,
  4977. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  4978. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4979. + * GNU General Public License for more details.
  4980. + *
  4981. + * You should have received a copy of the GNU General Public License
  4982. + * along with this program; if not, write to the Free Software
  4983. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  4984. + */
  4985. +
  4986. +#ifndef __BCM2708_ARM_CONTROL_H
  4987. +#define __BCM2708_ARM_CONTROL_H
  4988. +
  4989. +/*
  4990. + * Definitions and addresses for the ARM CONTROL logic
  4991. + * This file is manually generated.
  4992. + */
  4993. +
  4994. +#define ARM_BASE 0x7E00B000
  4995. +
  4996. +/* Basic configuration */
  4997. +#define ARM_CONTROL0 HW_REGISTER_RW(ARM_BASE+0x000)
  4998. +#define ARM_C0_SIZ128M 0x00000000
  4999. +#define ARM_C0_SIZ256M 0x00000001
  5000. +#define ARM_C0_SIZ512M 0x00000002
  5001. +#define ARM_C0_SIZ1G 0x00000003
  5002. +#define ARM_C0_BRESP0 0x00000000
  5003. +#define ARM_C0_BRESP1 0x00000004
  5004. +#define ARM_C0_BRESP2 0x00000008
  5005. +#define ARM_C0_BOOTHI 0x00000010
  5006. +#define ARM_C0_UNUSED05 0x00000020 /* free */
  5007. +#define ARM_C0_FULLPERI 0x00000040
  5008. +#define ARM_C0_UNUSED78 0x00000180 /* free */
  5009. +#define ARM_C0_JTAGMASK 0x00000E00
  5010. +#define ARM_C0_JTAGOFF 0x00000000
  5011. +#define ARM_C0_JTAGBASH 0x00000800 /* Debug on GPIO off */
  5012. +#define ARM_C0_JTAGGPIO 0x00000C00 /* Debug on GPIO on */
  5013. +#define ARM_C0_APROTMSK 0x0000F000
  5014. +#define ARM_C0_DBG0SYNC 0x00010000 /* VPU0 halt sync */
  5015. +#define ARM_C0_DBG1SYNC 0x00020000 /* VPU1 halt sync */
  5016. +#define ARM_C0_SWDBGREQ 0x00040000 /* HW debug request */
  5017. +#define ARM_C0_PASSHALT 0x00080000 /* ARM halt passed to debugger */
  5018. +#define ARM_C0_PRIO_PER 0x00F00000 /* per priority mask */
  5019. +#define ARM_C0_PRIO_L2 0x0F000000
  5020. +#define ARM_C0_PRIO_UC 0xF0000000
  5021. +
  5022. +#define ARM_C0_APROTPASS 0x0000A000 /* Translate 1:1 */
  5023. +#define ARM_C0_APROTUSER 0x00000000 /* Only user mode */
  5024. +#define ARM_C0_APROTSYST 0x0000F000 /* Only system mode */
  5025. +
  5026. +
  5027. +#define ARM_CONTROL1 HW_REGISTER_RW(ARM_BASE+0x440)
  5028. +#define ARM_C1_TIMER 0x00000001 /* re-route timer IRQ to VC */
  5029. +#define ARM_C1_MAIL 0x00000002 /* re-route Mail IRQ to VC */
  5030. +#define ARM_C1_BELL0 0x00000004 /* re-route Doorbell 0 to VC */
  5031. +#define ARM_C1_BELL1 0x00000008 /* re-route Doorbell 1 to VC */
  5032. +#define ARM_C1_PERSON 0x00000100 /* peripherals on */
  5033. +#define ARM_C1_REQSTOP 0x00000200 /* ASYNC bridge request stop */
  5034. +
  5035. +#define ARM_STATUS HW_REGISTER_RW(ARM_BASE+0x444)
  5036. +#define ARM_S_ACKSTOP 0x80000000 /* Bridge stopped */
  5037. +#define ARM_S_READPEND 0x000003FF /* pending reads counter */
  5038. +#define ARM_S_WRITPEND 0x000FFC00 /* pending writes counter */
  5039. +
  5040. +#define ARM_ERRHALT HW_REGISTER_RW(ARM_BASE+0x448)
  5041. +#define ARM_EH_PERIBURST 0x00000001 /* Burst write seen on peri bus */
  5042. +#define ARM_EH_ILLADDRS1 0x00000002 /* Address bits 25-27 error */
  5043. +#define ARM_EH_ILLADDRS2 0x00000004 /* Address bits 31-28 error */
  5044. +#define ARM_EH_VPU0HALT 0x00000008 /* VPU0 halted & in debug mode */
  5045. +#define ARM_EH_VPU1HALT 0x00000010 /* VPU1 halted & in debug mode */
  5046. +#define ARM_EH_ARMHALT 0x00000020 /* ARM in halted debug mode */
  5047. +
  5048. +#define ARM_ID_SECURE HW_REGISTER_RW(ARM_BASE+0x00C)
  5049. +#define ARM_ID HW_REGISTER_RW(ARM_BASE+0x44C)
  5050. +#define ARM_IDVAL 0x364D5241
  5051. +
  5052. +/* Translation memory */
  5053. +#define ARM_TRANSLATE HW_REGISTER_RW(ARM_BASE+0x100)
  5054. +/* 32 locations: 0x100.. 0x17F */
  5055. +/* 32 spare means we CAN go to 64 pages.... */
  5056. +
  5057. +
  5058. +/* Interrupts */
  5059. +#define ARM_IRQ_PEND0 HW_REGISTER_RW(ARM_BASE+0x200) /* Top IRQ bits */
  5060. +#define ARM_I0_TIMER 0x00000001 /* timer IRQ */
  5061. +#define ARM_I0_MAIL 0x00000002 /* Mail IRQ */
  5062. +#define ARM_I0_BELL0 0x00000004 /* Doorbell 0 */
  5063. +#define ARM_I0_BELL1 0x00000008 /* Doorbell 1 */
  5064. +#define ARM_I0_BANK1 0x00000100 /* Bank1 IRQ */
  5065. +#define ARM_I0_BANK2 0x00000200 /* Bank2 IRQ */
  5066. +
  5067. +#define ARM_IRQ_PEND1 HW_REGISTER_RW(ARM_BASE+0x204) /* All bank1 IRQ bits */
  5068. +/* todo: all I1_interrupt sources */
  5069. +#define ARM_IRQ_PEND2 HW_REGISTER_RW(ARM_BASE+0x208) /* All bank2 IRQ bits */
  5070. +/* todo: all I2_interrupt sources */
  5071. +
  5072. +#define ARM_IRQ_FAST HW_REGISTER_RW(ARM_BASE+0x20C) /* FIQ control */
  5073. +#define ARM_IF_INDEX 0x0000007F /* FIQ select */
  5074. +#define ARM_IF_ENABLE 0x00000080 /* FIQ enable */
  5075. +#define ARM_IF_VCMASK 0x0000003F /* FIQ = (index from VC source) */
  5076. +#define ARM_IF_TIMER 0x00000040 /* FIQ = ARM timer */
  5077. +#define ARM_IF_MAIL 0x00000041 /* FIQ = ARM Mail */
  5078. +#define ARM_IF_BELL0 0x00000042 /* FIQ = ARM Doorbell 0 */
  5079. +#define ARM_IF_BELL1 0x00000043 /* FIQ = ARM Doorbell 1 */
  5080. +#define ARM_IF_VP0HALT 0x00000044 /* FIQ = VPU0 Halt seen */
  5081. +#define ARM_IF_VP1HALT 0x00000045 /* FIQ = VPU1 Halt seen */
  5082. +#define ARM_IF_ILLEGAL 0x00000046 /* FIQ = Illegal access seen */
  5083. +
  5084. +#define ARM_IRQ_ENBL1 HW_REGISTER_RW(ARM_BASE+0x210) /* Bank1 enable bits */
  5085. +#define ARM_IRQ_ENBL2 HW_REGISTER_RW(ARM_BASE+0x214) /* Bank2 enable bits */
  5086. +#define ARM_IRQ_ENBL3 HW_REGISTER_RW(ARM_BASE+0x218) /* ARM irqs enable bits */
  5087. +#define ARM_IRQ_DIBL1 HW_REGISTER_RW(ARM_BASE+0x21C) /* Bank1 disable bits */
  5088. +#define ARM_IRQ_DIBL2 HW_REGISTER_RW(ARM_BASE+0x220) /* Bank2 disable bits */
  5089. +#define ARM_IRQ_DIBL3 HW_REGISTER_RW(ARM_BASE+0x224) /* ARM irqs disable bits */
  5090. +#define ARM_IE_TIMER 0x00000001 /* Timer IRQ */
  5091. +#define ARM_IE_MAIL 0x00000002 /* Mail IRQ */
  5092. +#define ARM_IE_BELL0 0x00000004 /* Doorbell 0 */
  5093. +#define ARM_IE_BELL1 0x00000008 /* Doorbell 1 */
  5094. +#define ARM_IE_VP0HALT 0x00000010 /* VPU0 Halt */
  5095. +#define ARM_IE_VP1HALT 0x00000020 /* VPU1 Halt */
  5096. +#define ARM_IE_ILLEGAL 0x00000040 /* Illegal access seen */
  5097. +
  5098. +/* Timer */
  5099. +/* For reg. fields see sp804 spec. */
  5100. +#define ARM_T_LOAD HW_REGISTER_RW(ARM_BASE+0x400)
  5101. +#define ARM_T_VALUE HW_REGISTER_RW(ARM_BASE+0x404)
  5102. +#define ARM_T_CONTROL HW_REGISTER_RW(ARM_BASE+0x408)
  5103. +#define ARM_T_IRQCNTL HW_REGISTER_RW(ARM_BASE+0x40C)
  5104. +#define ARM_T_RAWIRQ HW_REGISTER_RW(ARM_BASE+0x410)
  5105. +#define ARM_T_MSKIRQ HW_REGISTER_RW(ARM_BASE+0x414)
  5106. +#define ARM_T_RELOAD HW_REGISTER_RW(ARM_BASE+0x418)
  5107. +#define ARM_T_PREDIV HW_REGISTER_RW(ARM_BASE+0x41c)
  5108. +#define ARM_T_FREECNT HW_REGISTER_RW(ARM_BASE+0x420)
  5109. +
  5110. +#define TIMER_CTRL_ONESHOT (1 << 0)
  5111. +#define TIMER_CTRL_32BIT (1 << 1)
  5112. +#define TIMER_CTRL_DIV1 (0 << 2)
  5113. +#define TIMER_CTRL_DIV16 (1 << 2)
  5114. +#define TIMER_CTRL_DIV256 (2 << 2)
  5115. +#define TIMER_CTRL_IE (1 << 5)
  5116. +#define TIMER_CTRL_PERIODIC (1 << 6)
  5117. +#define TIMER_CTRL_ENABLE (1 << 7)
  5118. +#define TIMER_CTRL_DBGHALT (1 << 8)
  5119. +#define TIMER_CTRL_ENAFREE (1 << 9)
  5120. +#define TIMER_CTRL_FREEDIV_SHIFT 16)
  5121. +#define TIMER_CTRL_FREEDIV_MASK 0xff
  5122. +
  5123. +/* Semaphores, Doorbells, Mailboxes */
  5124. +#define ARM_SBM_OWN0 (ARM_BASE+0x800)
  5125. +#define ARM_SBM_OWN1 (ARM_BASE+0x900)
  5126. +#define ARM_SBM_OWN2 (ARM_BASE+0xA00)
  5127. +#define ARM_SBM_OWN3 (ARM_BASE+0xB00)
  5128. +
  5129. +/* MAILBOXES
  5130. + * Register flags are common across all
  5131. + * owner registers. See end of this section
  5132. + *
  5133. + * Semaphores, Doorbells, Mailboxes Owner 0
  5134. + *
  5135. + */
  5136. +
  5137. +#define ARM_0_SEMS HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5138. +#define ARM_0_SEM0 HW_REGISTER_RW(ARM_SBM_OWN0+0x00)
  5139. +#define ARM_0_SEM1 HW_REGISTER_RW(ARM_SBM_OWN0+0x04)
  5140. +#define ARM_0_SEM2 HW_REGISTER_RW(ARM_SBM_OWN0+0x08)
  5141. +#define ARM_0_SEM3 HW_REGISTER_RW(ARM_SBM_OWN0+0x0C)
  5142. +#define ARM_0_SEM4 HW_REGISTER_RW(ARM_SBM_OWN0+0x10)
  5143. +#define ARM_0_SEM5 HW_REGISTER_RW(ARM_SBM_OWN0+0x14)
  5144. +#define ARM_0_SEM6 HW_REGISTER_RW(ARM_SBM_OWN0+0x18)
  5145. +#define ARM_0_SEM7 HW_REGISTER_RW(ARM_SBM_OWN0+0x1C)
  5146. +#define ARM_0_BELL0 HW_REGISTER_RW(ARM_SBM_OWN0+0x40)
  5147. +#define ARM_0_BELL1 HW_REGISTER_RW(ARM_SBM_OWN0+0x44)
  5148. +#define ARM_0_BELL2 HW_REGISTER_RW(ARM_SBM_OWN0+0x48)
  5149. +#define ARM_0_BELL3 HW_REGISTER_RW(ARM_SBM_OWN0+0x4C)
  5150. +/* MAILBOX 0 access in Owner 0 area */
  5151. +/* Some addresses should ONLY be used by owner 0 */
  5152. +#define ARM_0_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) */
  5153. +#define ARM_0_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN0+0x80) /* .. 0x8C (4 locations) Normal read */
  5154. +#define ARM_0_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN0+0x90) /* none-pop read */
  5155. +#define ARM_0_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN0+0x94) /* Sender read (only LS 2 bits) */
  5156. +#define ARM_0_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN0+0x98) /* Status read */
  5157. +#define ARM_0_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0x9C) /* Config read/write */
  5158. +/* MAILBOX 1 access in Owner 0 area */
  5159. +/* Owner 0 should only WRITE to this mailbox */
  5160. +#define ARM_0_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) /* .. 0xAC (4 locations) */
  5161. +/*#define ARM_0_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN0+0xA0) */ /* DO NOT USE THIS !!!!! */
  5162. +/*#define ARM_0_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN0+0xB0) */ /* DO NOT USE THIS !!!!! */
  5163. +/*#define ARM_0_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN0+0xB4) */ /* DO NOT USE THIS !!!!! */
  5164. +#define ARM_0_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN0+0xB8) /* Status read */
  5165. +/*#define ARM_0_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN0+0xBC) */ /* DO NOT USE THIS !!!!! */
  5166. +/* General SEM, BELL, MAIL config/status */
  5167. +#define ARM_0_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE0) /* semaphore clear/debug register */
  5168. +#define ARM_0_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN0+0xE4) /* Doorbells clear/debug register */
  5169. +#define ARM_0_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xF8) /* ALL interrupts */
  5170. +#define ARM_0_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN0+0xFC) /* IRQS pending for owner 0 */
  5171. +
  5172. +/* Semaphores, Doorbells, Mailboxes Owner 1 */
  5173. +#define ARM_1_SEMS HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5174. +#define ARM_1_SEM0 HW_REGISTER_RW(ARM_SBM_OWN1+0x00)
  5175. +#define ARM_1_SEM1 HW_REGISTER_RW(ARM_SBM_OWN1+0x04)
  5176. +#define ARM_1_SEM2 HW_REGISTER_RW(ARM_SBM_OWN1+0x08)
  5177. +#define ARM_1_SEM3 HW_REGISTER_RW(ARM_SBM_OWN1+0x0C)
  5178. +#define ARM_1_SEM4 HW_REGISTER_RW(ARM_SBM_OWN1+0x10)
  5179. +#define ARM_1_SEM5 HW_REGISTER_RW(ARM_SBM_OWN1+0x14)
  5180. +#define ARM_1_SEM6 HW_REGISTER_RW(ARM_SBM_OWN1+0x18)
  5181. +#define ARM_1_SEM7 HW_REGISTER_RW(ARM_SBM_OWN1+0x1C)
  5182. +#define ARM_1_BELL0 HW_REGISTER_RW(ARM_SBM_OWN1+0x40)
  5183. +#define ARM_1_BELL1 HW_REGISTER_RW(ARM_SBM_OWN1+0x44)
  5184. +#define ARM_1_BELL2 HW_REGISTER_RW(ARM_SBM_OWN1+0x48)
  5185. +#define ARM_1_BELL3 HW_REGISTER_RW(ARM_SBM_OWN1+0x4C)
  5186. +/* MAILBOX 0 access in Owner 0 area */
  5187. +/* Owner 1 should only WRITE to this mailbox */
  5188. +#define ARM_1_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0x80) /* .. 0x8C (4 locations) */
  5189. +/*#define ARM_1_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN1+0x80) */ /* DO NOT USE THIS !!!!! */
  5190. +/*#define ARM_1_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN1+0x90) */ /* DO NOT USE THIS !!!!! */
  5191. +/*#define ARM_1_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN1+0x94) */ /* DO NOT USE THIS !!!!! */
  5192. +#define ARM_1_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN1+0x98) /* Status read */
  5193. +/*#define ARM_1_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0x9C) */ /* DO NOT USE THIS !!!!! */
  5194. +/* MAILBOX 1 access in Owner 0 area */
  5195. +#define ARM_1_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) */
  5196. +#define ARM_1_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN1+0xA0) /* .. 0xAC (4 locations) Normal read */
  5197. +#define ARM_1_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN1+0xB0) /* none-pop read */
  5198. +#define ARM_1_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN1+0xB4) /* Sender read (only LS 2 bits) */
  5199. +#define ARM_1_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN1+0xB8) /* Status read */
  5200. +#define ARM_1_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN1+0xBC)
  5201. +/* General SEM, BELL, MAIL config/status */
  5202. +#define ARM_1_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE0) /* semaphore clear/debug register */
  5203. +#define ARM_1_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN1+0xE4) /* Doorbells clear/debug register */
  5204. +#define ARM_1_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xFC) /* IRQS pending for owner 1 */
  5205. +#define ARM_1_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN1+0xF8) /* ALL interrupts */
  5206. +
  5207. +/* Semaphores, Doorbells, Mailboxes Owner 2 */
  5208. +#define ARM_2_SEMS HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5209. +#define ARM_2_SEM0 HW_REGISTER_RW(ARM_SBM_OWN2+0x00)
  5210. +#define ARM_2_SEM1 HW_REGISTER_RW(ARM_SBM_OWN2+0x04)
  5211. +#define ARM_2_SEM2 HW_REGISTER_RW(ARM_SBM_OWN2+0x08)
  5212. +#define ARM_2_SEM3 HW_REGISTER_RW(ARM_SBM_OWN2+0x0C)
  5213. +#define ARM_2_SEM4 HW_REGISTER_RW(ARM_SBM_OWN2+0x10)
  5214. +#define ARM_2_SEM5 HW_REGISTER_RW(ARM_SBM_OWN2+0x14)
  5215. +#define ARM_2_SEM6 HW_REGISTER_RW(ARM_SBM_OWN2+0x18)
  5216. +#define ARM_2_SEM7 HW_REGISTER_RW(ARM_SBM_OWN2+0x1C)
  5217. +#define ARM_2_BELL0 HW_REGISTER_RW(ARM_SBM_OWN2+0x40)
  5218. +#define ARM_2_BELL1 HW_REGISTER_RW(ARM_SBM_OWN2+0x44)
  5219. +#define ARM_2_BELL2 HW_REGISTER_RW(ARM_SBM_OWN2+0x48)
  5220. +#define ARM_2_BELL3 HW_REGISTER_RW(ARM_SBM_OWN2+0x4C)
  5221. +/* MAILBOX 0 access in Owner 2 area */
  5222. +/* Owner 2 should only WRITE to this mailbox */
  5223. +#define ARM_2_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0x80) /* .. 0x8C (4 locations) */
  5224. +/*#define ARM_2_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN2+0x80) */ /* DO NOT USE THIS !!!!! */
  5225. +/*#define ARM_2_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN2+0x90) */ /* DO NOT USE THIS !!!!! */
  5226. +/*#define ARM_2_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN2+0x94) */ /* DO NOT USE THIS !!!!! */
  5227. +#define ARM_2_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN2+0x98) /* Status read */
  5228. +/*#define ARM_2_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0x9C) */ /* DO NOT USE THIS !!!!! */
  5229. +/* MAILBOX 1 access in Owner 2 area */
  5230. +/* Owner 2 should only WRITE to this mailbox */
  5231. +#define ARM_2_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) /* .. 0xAC (4 locations) */
  5232. +/*#define ARM_2_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN2+0xA0) */ /* DO NOT USE THIS !!!!! */
  5233. +/*#define ARM_2_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN2+0xB0) */ /* DO NOT USE THIS !!!!! */
  5234. +/*#define ARM_2_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN2+0xB4) */ /* DO NOT USE THIS !!!!! */
  5235. +#define ARM_2_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN2+0xB8) /* Status read */
  5236. +/*#define ARM_2_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN2+0xBC) */ /* DO NOT USE THIS !!!!! */
  5237. +/* General SEM, BELL, MAIL config/status */
  5238. +#define ARM_2_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE0) /* semaphore clear/debug register */
  5239. +#define ARM_2_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN2+0xE4) /* Doorbells clear/debug register */
  5240. +#define ARM_2_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xFC) /* IRQS pending for owner 2 */
  5241. +#define ARM_2_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN2+0xF8) /* ALL interrupts */
  5242. +
  5243. +/* Semaphores, Doorbells, Mailboxes Owner 3 */
  5244. +#define ARM_3_SEMS HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5245. +#define ARM_3_SEM0 HW_REGISTER_RW(ARM_SBM_OWN3+0x00)
  5246. +#define ARM_3_SEM1 HW_REGISTER_RW(ARM_SBM_OWN3+0x04)
  5247. +#define ARM_3_SEM2 HW_REGISTER_RW(ARM_SBM_OWN3+0x08)
  5248. +#define ARM_3_SEM3 HW_REGISTER_RW(ARM_SBM_OWN3+0x0C)
  5249. +#define ARM_3_SEM4 HW_REGISTER_RW(ARM_SBM_OWN3+0x10)
  5250. +#define ARM_3_SEM5 HW_REGISTER_RW(ARM_SBM_OWN3+0x14)
  5251. +#define ARM_3_SEM6 HW_REGISTER_RW(ARM_SBM_OWN3+0x18)
  5252. +#define ARM_3_SEM7 HW_REGISTER_RW(ARM_SBM_OWN3+0x1C)
  5253. +#define ARM_3_BELL0 HW_REGISTER_RW(ARM_SBM_OWN3+0x40)
  5254. +#define ARM_3_BELL1 HW_REGISTER_RW(ARM_SBM_OWN3+0x44)
  5255. +#define ARM_3_BELL2 HW_REGISTER_RW(ARM_SBM_OWN3+0x48)
  5256. +#define ARM_3_BELL3 HW_REGISTER_RW(ARM_SBM_OWN3+0x4C)
  5257. +/* MAILBOX 0 access in Owner 3 area */
  5258. +/* Owner 3 should only WRITE to this mailbox */
  5259. +#define ARM_3_MAIL0_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0x80) /* .. 0x8C (4 locations) */
  5260. +/*#define ARM_3_MAIL0_RD HW_REGISTER_RW(ARM_SBM_OWN3+0x80) */ /* DO NOT USE THIS !!!!! */
  5261. +/*#define ARM_3_MAIL0_POL HW_REGISTER_RW(ARM_SBM_OWN3+0x90) */ /* DO NOT USE THIS !!!!! */
  5262. +/*#define ARM_3_MAIL0_SND HW_REGISTER_RW(ARM_SBM_OWN3+0x94) */ /* DO NOT USE THIS !!!!! */
  5263. +#define ARM_3_MAIL0_STA HW_REGISTER_RW(ARM_SBM_OWN3+0x98) /* Status read */
  5264. +/*#define ARM_3_MAIL0_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0x9C) */ /* DO NOT USE THIS !!!!! */
  5265. +/* MAILBOX 1 access in Owner 3 area */
  5266. +/* Owner 3 should only WRITE to this mailbox */
  5267. +#define ARM_3_MAIL1_WRT HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) /* .. 0xAC (4 locations) */
  5268. +/*#define ARM_3_MAIL1_RD HW_REGISTER_RW(ARM_SBM_OWN3+0xA0) */ /* DO NOT USE THIS !!!!! */
  5269. +/*#define ARM_3_MAIL1_POL HW_REGISTER_RW(ARM_SBM_OWN3+0xB0) */ /* DO NOT USE THIS !!!!! */
  5270. +/*#define ARM_3_MAIL1_SND HW_REGISTER_RW(ARM_SBM_OWN3+0xB4) */ /* DO NOT USE THIS !!!!! */
  5271. +#define ARM_3_MAIL1_STA HW_REGISTER_RW(ARM_SBM_OWN3+0xB8) /* Status read */
  5272. +/*#define ARM_3_MAIL1_CNF HW_REGISTER_RW(ARM_SBM_OWN3+0xBC) */ /* DO NOT USE THIS !!!!! */
  5273. +/* General SEM, BELL, MAIL config/status */
  5274. +#define ARM_3_SEMCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE0) /* semaphore clear/debug register */
  5275. +#define ARM_3_BELLCLRDBG HW_REGISTER_RW(ARM_SBM_OWN3+0xE4) /* Doorbells clear/debug register */
  5276. +#define ARM_3_MY_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xFC) /* IRQS pending for owner 3 */
  5277. +#define ARM_3_ALL_IRQS HW_REGISTER_RW(ARM_SBM_OWN3+0xF8) /* ALL interrupts */
  5278. +
  5279. +
  5280. +
  5281. +/* Mailbox flags. Valid for all owners */
  5282. +
  5283. +/* Mailbox status register (...0x98) */
  5284. +#define ARM_MS_FULL 0x80000000
  5285. +#define ARM_MS_EMPTY 0x40000000
  5286. +#define ARM_MS_LEVEL 0x400000FF /* Max. value depdnds on mailbox depth parameter */
  5287. +
  5288. +/* MAILBOX config/status register (...0x9C) */
  5289. +/* ANY write to this register clears the error bits! */
  5290. +#define ARM_MC_IHAVEDATAIRQEN 0x00000001 /* mailbox irq enable: has data */
  5291. +#define ARM_MC_IHAVESPACEIRQEN 0x00000002 /* mailbox irq enable: has space */
  5292. +#define ARM_MC_OPPISEMPTYIRQEN 0x00000004 /* mailbox irq enable: Opp. is empty */
  5293. +#define ARM_MC_MAIL_CLEAR 0x00000008 /* mailbox clear write 1, then 0 */
  5294. +#define ARM_MC_IHAVEDATAIRQPEND 0x00000010 /* mailbox irq pending: has space */
  5295. +#define ARM_MC_IHAVESPACEIRQPEND 0x00000020 /* mailbox irq pending: Opp. is empty */
  5296. +#define ARM_MC_OPPISEMPTYIRQPEND 0x00000040 /* mailbox irq pending */
  5297. +/* Bit 7 is unused */
  5298. +#define ARM_MC_ERRNOOWN 0x00000100 /* error : none owner read from mailbox */
  5299. +#define ARM_MC_ERROVERFLW 0x00000200 /* error : write to fill mailbox */
  5300. +#define ARM_MC_ERRUNDRFLW 0x00000400 /* error : read from empty mailbox */
  5301. +
  5302. +/* Semaphore clear/debug register (...0xE0) */
  5303. +#define ARM_SD_OWN0 0x00000003 /* Owner of sem 0 */
  5304. +#define ARM_SD_OWN1 0x0000000C /* Owner of sem 1 */
  5305. +#define ARM_SD_OWN2 0x00000030 /* Owner of sem 2 */
  5306. +#define ARM_SD_OWN3 0x000000C0 /* Owner of sem 3 */
  5307. +#define ARM_SD_OWN4 0x00000300 /* Owner of sem 4 */
  5308. +#define ARM_SD_OWN5 0x00000C00 /* Owner of sem 5 */
  5309. +#define ARM_SD_OWN6 0x00003000 /* Owner of sem 6 */
  5310. +#define ARM_SD_OWN7 0x0000C000 /* Owner of sem 7 */
  5311. +#define ARM_SD_SEM0 0x00010000 /* Status of sem 0 */
  5312. +#define ARM_SD_SEM1 0x00020000 /* Status of sem 1 */
  5313. +#define ARM_SD_SEM2 0x00040000 /* Status of sem 2 */
  5314. +#define ARM_SD_SEM3 0x00080000 /* Status of sem 3 */
  5315. +#define ARM_SD_SEM4 0x00100000 /* Status of sem 4 */
  5316. +#define ARM_SD_SEM5 0x00200000 /* Status of sem 5 */
  5317. +#define ARM_SD_SEM6 0x00400000 /* Status of sem 6 */
  5318. +#define ARM_SD_SEM7 0x00800000 /* Status of sem 7 */
  5319. +
  5320. +/* Doorbells clear/debug register (...0xE4) */
  5321. +#define ARM_BD_OWN0 0x00000003 /* Owner of doorbell 0 */
  5322. +#define ARM_BD_OWN1 0x0000000C /* Owner of doorbell 1 */
  5323. +#define ARM_BD_OWN2 0x00000030 /* Owner of doorbell 2 */
  5324. +#define ARM_BD_OWN3 0x000000C0 /* Owner of doorbell 3 */
  5325. +#define ARM_BD_BELL0 0x00000100 /* Status of doorbell 0 */
  5326. +#define ARM_BD_BELL1 0x00000200 /* Status of doorbell 1 */
  5327. +#define ARM_BD_BELL2 0x00000400 /* Status of doorbell 2 */
  5328. +#define ARM_BD_BELL3 0x00000800 /* Status of doorbell 3 */
  5329. +
  5330. +/* MY IRQS register (...0xF8) */
  5331. +#define ARM_MYIRQ_BELL 0x00000001 /* This owner has a doorbell IRQ */
  5332. +#define ARM_MYIRQ_MAIL 0x00000002 /* This owner has a mailbox IRQ */
  5333. +
  5334. +/* ALL IRQS register (...0xF8) */
  5335. +#define ARM_AIS_BELL0 0x00000001 /* Doorbell 0 IRQ pending */
  5336. +#define ARM_AIS_BELL1 0x00000002 /* Doorbell 1 IRQ pending */
  5337. +#define ARM_AIS_BELL2 0x00000004 /* Doorbell 2 IRQ pending */
  5338. +#define ARM_AIS_BELL3 0x00000008 /* Doorbell 3 IRQ pending */
  5339. +#define ARM_AIS0_HAVEDATA 0x00000010 /* MAIL 0 has data IRQ pending */
  5340. +#define ARM_AIS0_HAVESPAC 0x00000020 /* MAIL 0 has space IRQ pending */
  5341. +#define ARM_AIS0_OPPEMPTY 0x00000040 /* MAIL 0 opposite is empty IRQ */
  5342. +#define ARM_AIS1_HAVEDATA 0x00000080 /* MAIL 1 has data IRQ pending */
  5343. +#define ARM_AIS1_HAVESPAC 0x00000100 /* MAIL 1 has space IRQ pending */
  5344. +#define ARM_AIS1_OPPEMPTY 0x00000200 /* MAIL 1 opposite is empty IRQ */
  5345. +/* Note that bell-0, bell-1 and MAIL0 IRQ go only to the ARM */
  5346. +/* Whilst that bell-2, bell-3 and MAIL1 IRQ go only to the VC */
  5347. +/* */
  5348. +/* ARM JTAG BASH */
  5349. +/* */
  5350. +#define AJB_BASE 0x7e2000c0
  5351. +
  5352. +#define AJBCONF HW_REGISTER_RW(AJB_BASE+0x00)
  5353. +#define AJB_BITS0 0x000000
  5354. +#define AJB_BITS4 0x000004
  5355. +#define AJB_BITS8 0x000008
  5356. +#define AJB_BITS12 0x00000C
  5357. +#define AJB_BITS16 0x000010
  5358. +#define AJB_BITS20 0x000014
  5359. +#define AJB_BITS24 0x000018
  5360. +#define AJB_BITS28 0x00001C
  5361. +#define AJB_BITS32 0x000020
  5362. +#define AJB_BITS34 0x000022
  5363. +#define AJB_OUT_MS 0x000040
  5364. +#define AJB_OUT_LS 0x000000
  5365. +#define AJB_INV_CLK 0x000080
  5366. +#define AJB_D0_RISE 0x000100
  5367. +#define AJB_D0_FALL 0x000000
  5368. +#define AJB_D1_RISE 0x000200
  5369. +#define AJB_D1_FALL 0x000000
  5370. +#define AJB_IN_RISE 0x000400
  5371. +#define AJB_IN_FALL 0x000000
  5372. +#define AJB_ENABLE 0x000800
  5373. +#define AJB_HOLD0 0x000000
  5374. +#define AJB_HOLD1 0x001000
  5375. +#define AJB_HOLD2 0x002000
  5376. +#define AJB_HOLD3 0x003000
  5377. +#define AJB_RESETN 0x004000
  5378. +#define AJB_CLKSHFT 16
  5379. +#define AJB_BUSY 0x80000000
  5380. +#define AJBTMS HW_REGISTER_RW(AJB_BASE+0x04)
  5381. +#define AJBTDI HW_REGISTER_RW(AJB_BASE+0x08)
  5382. +#define AJBTDO HW_REGISTER_RW(AJB_BASE+0x0c)
  5383. +
  5384. +#endif
  5385. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/arm_power.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5386. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/arm_power.h 1970-01-01 01:00:00.000000000 +0100
  5387. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/arm_power.h 2014-04-13 17:32:40.000000000 +0200
  5388. @@ -0,0 +1,60 @@
  5389. +/*
  5390. + * linux/arch/arm/mach-bcm2708/include/mach/arm_power.h
  5391. + *
  5392. + * Copyright (C) 2010 Broadcom
  5393. + *
  5394. + * This program is free software; you can redistribute it and/or modify
  5395. + * it under the terms of the GNU General Public License as published by
  5396. + * the Free Software Foundation; either version 2 of the License, or
  5397. + * (at your option) any later version.
  5398. + *
  5399. + * This program is distributed in the hope that it will be useful,
  5400. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5401. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5402. + * GNU General Public License for more details.
  5403. + *
  5404. + * You should have received a copy of the GNU General Public License
  5405. + * along with this program; if not, write to the Free Software
  5406. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5407. + */
  5408. +
  5409. +#ifndef _ARM_POWER_H
  5410. +#define _ARM_POWER_H
  5411. +
  5412. +/* Use meaningful names on each side */
  5413. +#ifdef __VIDEOCORE__
  5414. +#define PREFIX(x) ARM_##x
  5415. +#else
  5416. +#define PREFIX(x) BCM_##x
  5417. +#endif
  5418. +
  5419. +enum {
  5420. + PREFIX(POWER_SDCARD_BIT),
  5421. + PREFIX(POWER_UART_BIT),
  5422. + PREFIX(POWER_MINIUART_BIT),
  5423. + PREFIX(POWER_USB_BIT),
  5424. + PREFIX(POWER_I2C0_BIT),
  5425. + PREFIX(POWER_I2C1_BIT),
  5426. + PREFIX(POWER_I2C2_BIT),
  5427. + PREFIX(POWER_SPI_BIT),
  5428. + PREFIX(POWER_CCP2TX_BIT),
  5429. +
  5430. + PREFIX(POWER_MAX)
  5431. +};
  5432. +
  5433. +enum {
  5434. + PREFIX(POWER_SDCARD) = (1 << PREFIX(POWER_SDCARD_BIT)),
  5435. + PREFIX(POWER_UART) = (1 << PREFIX(POWER_UART_BIT)),
  5436. + PREFIX(POWER_MINIUART) = (1 << PREFIX(POWER_MINIUART_BIT)),
  5437. + PREFIX(POWER_USB) = (1 << PREFIX(POWER_USB_BIT)),
  5438. + PREFIX(POWER_I2C0) = (1 << PREFIX(POWER_I2C0_BIT)),
  5439. + PREFIX(POWER_I2C1_MASK) = (1 << PREFIX(POWER_I2C1_BIT)),
  5440. + PREFIX(POWER_I2C2_MASK) = (1 << PREFIX(POWER_I2C2_BIT)),
  5441. + PREFIX(POWER_SPI_MASK) = (1 << PREFIX(POWER_SPI_BIT)),
  5442. + PREFIX(POWER_CCP2TX_MASK) = (1 << PREFIX(POWER_CCP2TX_BIT)),
  5443. +
  5444. + PREFIX(POWER_MASK) = (1 << PREFIX(POWER_MAX)) - 1,
  5445. + PREFIX(POWER_NONE) = 0
  5446. +};
  5447. +
  5448. +#endif
  5449. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/clkdev.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h
  5450. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/clkdev.h 1970-01-01 01:00:00.000000000 +0100
  5451. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/clkdev.h 2014-04-13 17:32:40.000000000 +0200
  5452. @@ -0,0 +1,7 @@
  5453. +#ifndef __ASM_MACH_CLKDEV_H
  5454. +#define __ASM_MACH_CLKDEV_H
  5455. +
  5456. +#define __clk_get(clk) ({ 1; })
  5457. +#define __clk_put(clk) do { } while (0)
  5458. +
  5459. +#endif
  5460. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/debug-macro.S linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S
  5461. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/debug-macro.S 1970-01-01 01:00:00.000000000 +0100
  5462. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/debug-macro.S 2014-09-14 19:03:05.000000000 +0200
  5463. @@ -0,0 +1,22 @@
  5464. +/* arch/arm/mach-bcm2708/include/mach/debug-macro.S
  5465. + *
  5466. + * Debugging macro include header
  5467. + *
  5468. + * Copyright (C) 2010 Broadcom
  5469. + * Copyright (C) 1994-1999 Russell King
  5470. + * Moved from linux/arch/arm/kernel/debug.S by Ben Dooks
  5471. + *
  5472. + * This program is free software; you can redistribute it and/or modify
  5473. + * it under the terms of the GNU General Public License version 2 as
  5474. + * published by the Free Software Foundation.
  5475. + *
  5476. +*/
  5477. +
  5478. +#include <mach/platform.h>
  5479. +
  5480. + .macro addruart, rp, rv, tmp
  5481. + ldr \rp, =UART0_BASE
  5482. + ldr \rv, =IO_ADDRESS(UART0_BASE)
  5483. + .endm
  5484. +
  5485. +#include <debug/pl01x.S>
  5486. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/dma.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/dma.h
  5487. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/dma.h 1970-01-01 01:00:00.000000000 +0100
  5488. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/dma.h 2014-09-14 19:03:05.000000000 +0200
  5489. @@ -0,0 +1,94 @@
  5490. +/*
  5491. + * linux/arch/arm/mach-bcm2708/include/mach/dma.h
  5492. + *
  5493. + * Copyright (C) 2010 Broadcom
  5494. + *
  5495. + * This program is free software; you can redistribute it and/or modify
  5496. + * it under the terms of the GNU General Public License version 2 as
  5497. + * published by the Free Software Foundation.
  5498. + */
  5499. +
  5500. +
  5501. +#ifndef _MACH_BCM2708_DMA_H
  5502. +#define _MACH_BCM2708_DMA_H
  5503. +
  5504. +#define BCM_DMAMAN_DRIVER_NAME "bcm2708_dma"
  5505. +
  5506. +/* DMA CS Control and Status bits */
  5507. +#define BCM2708_DMA_ACTIVE (1 << 0)
  5508. +#define BCM2708_DMA_INT (1 << 2)
  5509. +#define BCM2708_DMA_ISPAUSED (1 << 4) /* Pause requested or not active */
  5510. +#define BCM2708_DMA_ISHELD (1 << 5) /* Is held by DREQ flow control */
  5511. +#define BCM2708_DMA_ERR (1 << 8)
  5512. +#define BCM2708_DMA_ABORT (1 << 30) /* stop current CB, go to next, WO */
  5513. +#define BCM2708_DMA_RESET (1 << 31) /* WO, self clearing */
  5514. +
  5515. +/* DMA control block "info" field bits */
  5516. +#define BCM2708_DMA_INT_EN (1 << 0)
  5517. +#define BCM2708_DMA_TDMODE (1 << 1)
  5518. +#define BCM2708_DMA_WAIT_RESP (1 << 3)
  5519. +#define BCM2708_DMA_D_INC (1 << 4)
  5520. +#define BCM2708_DMA_D_WIDTH (1 << 5)
  5521. +#define BCM2708_DMA_D_DREQ (1 << 6)
  5522. +#define BCM2708_DMA_S_INC (1 << 8)
  5523. +#define BCM2708_DMA_S_WIDTH (1 << 9)
  5524. +#define BCM2708_DMA_S_DREQ (1 << 10)
  5525. +
  5526. +#define BCM2708_DMA_BURST(x) (((x)&0xf) << 12)
  5527. +#define BCM2708_DMA_PER_MAP(x) ((x) << 16)
  5528. +#define BCM2708_DMA_WAITS(x) (((x)&0x1f) << 21)
  5529. +
  5530. +#define BCM2708_DMA_DREQ_EMMC 11
  5531. +#define BCM2708_DMA_DREQ_SDHOST 13
  5532. +
  5533. +#define BCM2708_DMA_CS 0x00 /* Control and Status */
  5534. +#define BCM2708_DMA_ADDR 0x04
  5535. +/* the current control block appears in the following registers - read only */
  5536. +#define BCM2708_DMA_INFO 0x08
  5537. +#define BCM2708_DMA_SOURCE_AD 0x0c
  5538. +#define BCM2708_DMA_DEST_AD 0x10
  5539. +#define BCM2708_DMA_NEXTCB 0x1C
  5540. +#define BCM2708_DMA_DEBUG 0x20
  5541. +
  5542. +#define BCM2708_DMA4_CS (BCM2708_DMA_CHAN(4)+BCM2708_DMA_CS)
  5543. +#define BCM2708_DMA4_ADDR (BCM2708_DMA_CHAN(4)+BCM2708_DMA_ADDR)
  5544. +
  5545. +#define BCM2708_DMA_TDMODE_LEN(w, h) ((h) << 16 | (w))
  5546. +
  5547. +struct bcm2708_dma_cb {
  5548. + unsigned long info;
  5549. + unsigned long src;
  5550. + unsigned long dst;
  5551. + unsigned long length;
  5552. + unsigned long stride;
  5553. + unsigned long next;
  5554. + unsigned long pad[2];
  5555. +};
  5556. +struct scatterlist;
  5557. +
  5558. +extern int bcm_sg_suitable_for_dma(struct scatterlist *sg_ptr, int sg_len);
  5559. +extern void bcm_dma_start(void __iomem *dma_chan_base,
  5560. + dma_addr_t control_block);
  5561. +extern void bcm_dma_wait_idle(void __iomem *dma_chan_base);
  5562. +extern bool bcm_dma_is_busy(void __iomem *dma_chan_base);
  5563. +extern int /*rc*/ bcm_dma_abort(void __iomem *dma_chan_base);
  5564. +
  5565. +/* When listing features we can ask for when allocating DMA channels give
  5566. + those with higher priority smaller ordinal numbers */
  5567. +#define BCM_DMA_FEATURE_FAST_ORD 0
  5568. +#define BCM_DMA_FEATURE_BULK_ORD 1
  5569. +#define BCM_DMA_FEATURE_NORMAL_ORD 2
  5570. +#define BCM_DMA_FEATURE_LITE_ORD 3
  5571. +#define BCM_DMA_FEATURE_FAST (1<<BCM_DMA_FEATURE_FAST_ORD)
  5572. +#define BCM_DMA_FEATURE_BULK (1<<BCM_DMA_FEATURE_BULK_ORD)
  5573. +#define BCM_DMA_FEATURE_NORMAL (1<<BCM_DMA_FEATURE_NORMAL_ORD)
  5574. +#define BCM_DMA_FEATURE_LITE (1<<BCM_DMA_FEATURE_LITE_ORD)
  5575. +#define BCM_DMA_FEATURE_COUNT 4
  5576. +
  5577. +/* return channel no or -ve error */
  5578. +extern int bcm_dma_chan_alloc(unsigned preferred_feature_set,
  5579. + void __iomem **out_dma_base, int *out_dma_irq);
  5580. +extern int bcm_dma_chan_free(int channel);
  5581. +
  5582. +
  5583. +#endif /* _MACH_BCM2708_DMA_H */
  5584. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/entry-macro.S linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S
  5585. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/entry-macro.S 1970-01-01 01:00:00.000000000 +0100
  5586. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/entry-macro.S 2014-04-13 17:32:40.000000000 +0200
  5587. @@ -0,0 +1,69 @@
  5588. +/*
  5589. + * arch/arm/mach-bcm2708/include/mach/entry-macro.S
  5590. + *
  5591. + * Low-level IRQ helper macros for BCM2708 platforms
  5592. + *
  5593. + * Copyright (C) 2010 Broadcom
  5594. + *
  5595. + * This program is free software; you can redistribute it and/or modify
  5596. + * it under the terms of the GNU General Public License as published by
  5597. + * the Free Software Foundation; either version 2 of the License, or
  5598. + * (at your option) any later version.
  5599. + *
  5600. + * This program is distributed in the hope that it will be useful,
  5601. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5602. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5603. + * GNU General Public License for more details.
  5604. + *
  5605. + * You should have received a copy of the GNU General Public License
  5606. + * along with this program; if not, write to the Free Software
  5607. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5608. + */
  5609. +#include <mach/hardware.h>
  5610. +
  5611. + .macro disable_fiq
  5612. + .endm
  5613. +
  5614. + .macro get_irqnr_preamble, base, tmp
  5615. + ldr \base, =IO_ADDRESS(ARMCTRL_IC_BASE)
  5616. + .endm
  5617. +
  5618. + .macro arch_ret_to_user, tmp1, tmp2
  5619. + .endm
  5620. +
  5621. + .macro get_irqnr_and_base, irqnr, irqstat, base, tmp
  5622. + /* get masked status */
  5623. + ldr \irqstat, [\base, #(ARM_IRQ_PEND0 - ARMCTRL_IC_BASE)]
  5624. + mov \irqnr, #(ARM_IRQ0_BASE + 31)
  5625. + and \tmp, \irqstat, #0x300 @ save bits 8 and 9
  5626. + /* clear bits 8 and 9, and test */
  5627. + bics \irqstat, \irqstat, #0x300
  5628. + bne 1010f
  5629. +
  5630. + tst \tmp, #0x100
  5631. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND1 - ARMCTRL_IC_BASE)]
  5632. + movne \irqnr, #(ARM_IRQ1_BASE + 31)
  5633. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  5634. + bicne \irqstat, #((1<<7) | (1<<9) | (1<<10))
  5635. + bicne \irqstat, #((1<<18) | (1<<19))
  5636. + bne 1010f
  5637. +
  5638. + tst \tmp, #0x200
  5639. + ldrne \irqstat, [\base, #(ARM_IRQ_PEND2 - ARMCTRL_IC_BASE)]
  5640. + movne \irqnr, #(ARM_IRQ2_BASE + 31)
  5641. + @ Mask out the interrupts also present in PEND0 - see SW-5809
  5642. + bicne \irqstat, #((1<<21) | (1<<22) | (1<<23) | (1<<24) | (1<<25))
  5643. + bicne \irqstat, #((1<<30))
  5644. + beq 1020f
  5645. +
  5646. +1010:
  5647. + @ For non-zero x, LSB(x) = 31 - CLZ(x^(x-1))
  5648. + @ N.B. CLZ is an ARM5 instruction.
  5649. + sub \tmp, \irqstat, #1
  5650. + eor \irqstat, \irqstat, \tmp
  5651. + clz \tmp, \irqstat
  5652. + sub \irqnr, \tmp
  5653. +
  5654. +1020: @ EQ will be set if no irqs pending
  5655. +
  5656. + .endm
  5657. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/frc.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/frc.h
  5658. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/frc.h 1970-01-01 01:00:00.000000000 +0100
  5659. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/frc.h 2014-04-13 17:32:40.000000000 +0200
  5660. @@ -0,0 +1,38 @@
  5661. +/*
  5662. + * arch/arm/mach-bcm2708/include/mach/timex.h
  5663. + *
  5664. + * BCM2708 free running counter (timer)
  5665. + *
  5666. + * Copyright (C) 2010 Broadcom
  5667. + *
  5668. + * This program is free software; you can redistribute it and/or modify
  5669. + * it under the terms of the GNU General Public License as published by
  5670. + * the Free Software Foundation; either version 2 of the License, or
  5671. + * (at your option) any later version.
  5672. + *
  5673. + * This program is distributed in the hope that it will be useful,
  5674. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5675. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5676. + * GNU General Public License for more details.
  5677. + *
  5678. + * You should have received a copy of the GNU General Public License
  5679. + * along with this program; if not, write to the Free Software
  5680. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5681. + */
  5682. +
  5683. +#ifndef _MACH_FRC_H
  5684. +#define _MACH_FRC_H
  5685. +
  5686. +#define FRC_TICK_RATE (1000000)
  5687. +
  5688. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  5689. + (slightly faster than frc_clock_ticks63()
  5690. + */
  5691. +extern unsigned long frc_clock_ticks32(void);
  5692. +
  5693. +/*! Free running counter incrementing at the CLOCK_TICK_RATE
  5694. + * Note - top bit should be ignored (see cnt32_to_63)
  5695. + */
  5696. +extern unsigned long long frc_clock_ticks63(void);
  5697. +
  5698. +#endif
  5699. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/gpio.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h
  5700. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/gpio.h 1970-01-01 01:00:00.000000000 +0100
  5701. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/gpio.h 2014-09-14 19:03:05.000000000 +0200
  5702. @@ -0,0 +1,17 @@
  5703. +/*
  5704. + * arch/arm/mach-bcm2708/include/mach/gpio.h
  5705. + *
  5706. + * This file is licensed under the terms of the GNU General Public
  5707. + * License version 2. This program is licensed "as is" without any
  5708. + * warranty of any kind, whether express or implied.
  5709. + */
  5710. +
  5711. +#ifndef __ASM_ARCH_GPIO_H
  5712. +#define __ASM_ARCH_GPIO_H
  5713. +
  5714. +#define BCM2708_NR_GPIOS 54 // number of gpio lines
  5715. +
  5716. +#define gpio_to_irq(x) ((x) + GPIO_IRQ_START)
  5717. +#define irq_to_gpio(x) ((x) - GPIO_IRQ_START)
  5718. +
  5719. +#endif
  5720. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/hardware.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h
  5721. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/hardware.h 1970-01-01 01:00:00.000000000 +0100
  5722. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/hardware.h 2014-04-13 17:32:40.000000000 +0200
  5723. @@ -0,0 +1,28 @@
  5724. +/*
  5725. + * arch/arm/mach-bcm2708/include/mach/hardware.h
  5726. + *
  5727. + * This file contains the hardware definitions of the BCM2708 devices.
  5728. + *
  5729. + * Copyright (C) 2010 Broadcom
  5730. + *
  5731. + * This program is free software; you can redistribute it and/or modify
  5732. + * it under the terms of the GNU General Public License as published by
  5733. + * the Free Software Foundation; either version 2 of the License, or
  5734. + * (at your option) any later version.
  5735. + *
  5736. + * This program is distributed in the hope that it will be useful,
  5737. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5738. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5739. + * GNU General Public License for more details.
  5740. + *
  5741. + * You should have received a copy of the GNU General Public License
  5742. + * along with this program; if not, write to the Free Software
  5743. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5744. + */
  5745. +#ifndef __ASM_ARCH_HARDWARE_H
  5746. +#define __ASM_ARCH_HARDWARE_H
  5747. +
  5748. +#include <asm/sizes.h>
  5749. +#include <mach/platform.h>
  5750. +
  5751. +#endif
  5752. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/io.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/io.h
  5753. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/io.h 1970-01-01 01:00:00.000000000 +0100
  5754. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/io.h 2014-04-13 17:32:40.000000000 +0200
  5755. @@ -0,0 +1,27 @@
  5756. +/*
  5757. + * arch/arm/mach-bcm2708/include/mach/io.h
  5758. + *
  5759. + * Copyright (C) 2003 ARM Limited
  5760. + *
  5761. + * This program is free software; you can redistribute it and/or modify
  5762. + * it under the terms of the GNU General Public License as published by
  5763. + * the Free Software Foundation; either version 2 of the License, or
  5764. + * (at your option) any later version.
  5765. + *
  5766. + * This program is distributed in the hope that it will be useful,
  5767. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5768. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5769. + * GNU General Public License for more details.
  5770. + *
  5771. + * You should have received a copy of the GNU General Public License
  5772. + * along with this program; if not, write to the Free Software
  5773. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5774. + */
  5775. +#ifndef __ASM_ARM_ARCH_IO_H
  5776. +#define __ASM_ARM_ARCH_IO_H
  5777. +
  5778. +#define IO_SPACE_LIMIT 0xffffffff
  5779. +
  5780. +#define __io(a) __typesafe_io(a)
  5781. +
  5782. +#endif
  5783. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/irqs.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h
  5784. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/irqs.h 1970-01-01 01:00:00.000000000 +0100
  5785. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/irqs.h 2014-09-14 19:03:05.000000000 +0200
  5786. @@ -0,0 +1,197 @@
  5787. +/*
  5788. + * arch/arm/mach-bcm2708/include/mach/irqs.h
  5789. + *
  5790. + * Copyright (C) 2010 Broadcom
  5791. + * Copyright (C) 2003 ARM Limited
  5792. + * Copyright (C) 2000 Deep Blue Solutions Ltd.
  5793. + *
  5794. + * This program is free software; you can redistribute it and/or modify
  5795. + * it under the terms of the GNU General Public License as published by
  5796. + * the Free Software Foundation; either version 2 of the License, or
  5797. + * (at your option) any later version.
  5798. + *
  5799. + * This program is distributed in the hope that it will be useful,
  5800. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5801. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5802. + * GNU General Public License for more details.
  5803. + *
  5804. + * You should have received a copy of the GNU General Public License
  5805. + * along with this program; if not, write to the Free Software
  5806. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  5807. + */
  5808. +
  5809. +#ifndef _BCM2708_IRQS_H_
  5810. +#define _BCM2708_IRQS_H_
  5811. +
  5812. +#include <mach/platform.h>
  5813. +
  5814. +/*
  5815. + * IRQ interrupts definitions are the same as the INT definitions
  5816. + * held within platform.h
  5817. + */
  5818. +#define IRQ_ARMCTRL_START 0
  5819. +#define IRQ_TIMER0 (IRQ_ARMCTRL_START + INTERRUPT_TIMER0)
  5820. +#define IRQ_TIMER1 (IRQ_ARMCTRL_START + INTERRUPT_TIMER1)
  5821. +#define IRQ_TIMER2 (IRQ_ARMCTRL_START + INTERRUPT_TIMER2)
  5822. +#define IRQ_TIMER3 (IRQ_ARMCTRL_START + INTERRUPT_TIMER3)
  5823. +#define IRQ_CODEC0 (IRQ_ARMCTRL_START + INTERRUPT_CODEC0)
  5824. +#define IRQ_CODEC1 (IRQ_ARMCTRL_START + INTERRUPT_CODEC1)
  5825. +#define IRQ_CODEC2 (IRQ_ARMCTRL_START + INTERRUPT_CODEC2)
  5826. +#define IRQ_JPEG (IRQ_ARMCTRL_START + INTERRUPT_JPEG)
  5827. +#define IRQ_ISP (IRQ_ARMCTRL_START + INTERRUPT_ISP)
  5828. +#define IRQ_USB (IRQ_ARMCTRL_START + INTERRUPT_USB)
  5829. +#define IRQ_3D (IRQ_ARMCTRL_START + INTERRUPT_3D)
  5830. +#define IRQ_TRANSPOSER (IRQ_ARMCTRL_START + INTERRUPT_TRANSPOSER)
  5831. +#define IRQ_MULTICORESYNC0 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC0)
  5832. +#define IRQ_MULTICORESYNC1 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC1)
  5833. +#define IRQ_MULTICORESYNC2 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC2)
  5834. +#define IRQ_MULTICORESYNC3 (IRQ_ARMCTRL_START + INTERRUPT_MULTICORESYNC3)
  5835. +#define IRQ_DMA0 (IRQ_ARMCTRL_START + INTERRUPT_DMA0)
  5836. +#define IRQ_DMA1 (IRQ_ARMCTRL_START + INTERRUPT_DMA1)
  5837. +#define IRQ_DMA2 (IRQ_ARMCTRL_START + INTERRUPT_DMA2)
  5838. +#define IRQ_DMA3 (IRQ_ARMCTRL_START + INTERRUPT_DMA3)
  5839. +#define IRQ_DMA4 (IRQ_ARMCTRL_START + INTERRUPT_DMA4)
  5840. +#define IRQ_DMA5 (IRQ_ARMCTRL_START + INTERRUPT_DMA5)
  5841. +#define IRQ_DMA6 (IRQ_ARMCTRL_START + INTERRUPT_DMA6)
  5842. +#define IRQ_DMA7 (IRQ_ARMCTRL_START + INTERRUPT_DMA7)
  5843. +#define IRQ_DMA8 (IRQ_ARMCTRL_START + INTERRUPT_DMA8)
  5844. +#define IRQ_DMA9 (IRQ_ARMCTRL_START + INTERRUPT_DMA9)
  5845. +#define IRQ_DMA10 (IRQ_ARMCTRL_START + INTERRUPT_DMA10)
  5846. +#define IRQ_DMA11 (IRQ_ARMCTRL_START + INTERRUPT_DMA11)
  5847. +#define IRQ_DMA12 (IRQ_ARMCTRL_START + INTERRUPT_DMA12)
  5848. +#define IRQ_AUX (IRQ_ARMCTRL_START + INTERRUPT_AUX)
  5849. +#define IRQ_ARM (IRQ_ARMCTRL_START + INTERRUPT_ARM)
  5850. +#define IRQ_VPUDMA (IRQ_ARMCTRL_START + INTERRUPT_VPUDMA)
  5851. +#define IRQ_HOSTPORT (IRQ_ARMCTRL_START + INTERRUPT_HOSTPORT)
  5852. +#define IRQ_VIDEOSCALER (IRQ_ARMCTRL_START + INTERRUPT_VIDEOSCALER)
  5853. +#define IRQ_CCP2TX (IRQ_ARMCTRL_START + INTERRUPT_CCP2TX)
  5854. +#define IRQ_SDC (IRQ_ARMCTRL_START + INTERRUPT_SDC)
  5855. +#define IRQ_DSI0 (IRQ_ARMCTRL_START + INTERRUPT_DSI0)
  5856. +#define IRQ_AVE (IRQ_ARMCTRL_START + INTERRUPT_AVE)
  5857. +#define IRQ_CAM0 (IRQ_ARMCTRL_START + INTERRUPT_CAM0)
  5858. +#define IRQ_CAM1 (IRQ_ARMCTRL_START + INTERRUPT_CAM1)
  5859. +#define IRQ_HDMI0 (IRQ_ARMCTRL_START + INTERRUPT_HDMI0)
  5860. +#define IRQ_HDMI1 (IRQ_ARMCTRL_START + INTERRUPT_HDMI1)
  5861. +#define IRQ_PIXELVALVE1 (IRQ_ARMCTRL_START + INTERRUPT_PIXELVALVE1)
  5862. +#define IRQ_I2CSPISLV (IRQ_ARMCTRL_START + INTERRUPT_I2CSPISLV)
  5863. +#define IRQ_DSI1 (IRQ_ARMCTRL_START + INTERRUPT_DSI1)
  5864. +#define IRQ_PWA0 (IRQ_ARMCTRL_START + INTERRUPT_PWA0)
  5865. +#define IRQ_PWA1 (IRQ_ARMCTRL_START + INTERRUPT_PWA1)
  5866. +#define IRQ_CPR (IRQ_ARMCTRL_START + INTERRUPT_CPR)
  5867. +#define IRQ_SMI (IRQ_ARMCTRL_START + INTERRUPT_SMI)
  5868. +#define IRQ_GPIO0 (IRQ_ARMCTRL_START + INTERRUPT_GPIO0)
  5869. +#define IRQ_GPIO1 (IRQ_ARMCTRL_START + INTERRUPT_GPIO1)
  5870. +#define IRQ_GPIO2 (IRQ_ARMCTRL_START + INTERRUPT_GPIO2)
  5871. +#define IRQ_GPIO3 (IRQ_ARMCTRL_START + INTERRUPT_GPIO3)
  5872. +#define IRQ_I2C (IRQ_ARMCTRL_START + INTERRUPT_I2C)
  5873. +#define IRQ_SPI (IRQ_ARMCTRL_START + INTERRUPT_SPI)
  5874. +#define IRQ_I2SPCM (IRQ_ARMCTRL_START + INTERRUPT_I2SPCM)
  5875. +#define IRQ_SDIO (IRQ_ARMCTRL_START + INTERRUPT_SDIO)
  5876. +#define IRQ_UART (IRQ_ARMCTRL_START + INTERRUPT_UART)
  5877. +#define IRQ_SLIMBUS (IRQ_ARMCTRL_START + INTERRUPT_SLIMBUS)
  5878. +#define IRQ_VEC (IRQ_ARMCTRL_START + INTERRUPT_VEC)
  5879. +#define IRQ_CPG (IRQ_ARMCTRL_START + INTERRUPT_CPG)
  5880. +#define IRQ_RNG (IRQ_ARMCTRL_START + INTERRUPT_RNG)
  5881. +#define IRQ_ARASANSDIO (IRQ_ARMCTRL_START + INTERRUPT_ARASANSDIO)
  5882. +#define IRQ_AVSPMON (IRQ_ARMCTRL_START + INTERRUPT_AVSPMON)
  5883. +
  5884. +#define IRQ_ARM_TIMER (IRQ_ARMCTRL_START + INTERRUPT_ARM_TIMER)
  5885. +#define IRQ_ARM_MAILBOX (IRQ_ARMCTRL_START + INTERRUPT_ARM_MAILBOX)
  5886. +#define IRQ_ARM_DOORBELL_0 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_0)
  5887. +#define IRQ_ARM_DOORBELL_1 (IRQ_ARMCTRL_START + INTERRUPT_ARM_DOORBELL_1)
  5888. +#define IRQ_VPU0_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU0_HALTED)
  5889. +#define IRQ_VPU1_HALTED (IRQ_ARMCTRL_START + INTERRUPT_VPU1_HALTED)
  5890. +#define IRQ_ILLEGAL_TYPE0 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE0)
  5891. +#define IRQ_ILLEGAL_TYPE1 (IRQ_ARMCTRL_START + INTERRUPT_ILLEGAL_TYPE1)
  5892. +#define IRQ_PENDING1 (IRQ_ARMCTRL_START + INTERRUPT_PENDING1)
  5893. +#define IRQ_PENDING2 (IRQ_ARMCTRL_START + INTERRUPT_PENDING2)
  5894. +
  5895. +#define FIQ_START HARD_IRQS
  5896. +
  5897. +/*
  5898. + * FIQ interrupts definitions are the same as the INT definitions.
  5899. + */
  5900. +#define FIQ_TIMER0 (FIQ_START+INTERRUPT_TIMER0)
  5901. +#define FIQ_TIMER1 (FIQ_START+INTERRUPT_TIMER1)
  5902. +#define FIQ_TIMER2 (FIQ_START+INTERRUPT_TIMER2)
  5903. +#define FIQ_TIMER3 (FIQ_START+INTERRUPT_TIMER3)
  5904. +#define FIQ_CODEC0 (FIQ_START+INTERRUPT_CODEC0)
  5905. +#define FIQ_CODEC1 (FIQ_START+INTERRUPT_CODEC1)
  5906. +#define FIQ_CODEC2 (FIQ_START+INTERRUPT_CODEC2)
  5907. +#define FIQ_JPEG (FIQ_START+INTERRUPT_JPEG)
  5908. +#define FIQ_ISP (FIQ_START+INTERRUPT_ISP)
  5909. +#define FIQ_USB (FIQ_START+INTERRUPT_USB)
  5910. +#define FIQ_3D (FIQ_START+INTERRUPT_3D)
  5911. +#define FIQ_TRANSPOSER (FIQ_START+INTERRUPT_TRANSPOSER)
  5912. +#define FIQ_MULTICORESYNC0 (FIQ_START+INTERRUPT_MULTICORESYNC0)
  5913. +#define FIQ_MULTICORESYNC1 (FIQ_START+INTERRUPT_MULTICORESYNC1)
  5914. +#define FIQ_MULTICORESYNC2 (FIQ_START+INTERRUPT_MULTICORESYNC2)
  5915. +#define FIQ_MULTICORESYNC3 (FIQ_START+INTERRUPT_MULTICORESYNC3)
  5916. +#define FIQ_DMA0 (FIQ_START+INTERRUPT_DMA0)
  5917. +#define FIQ_DMA1 (FIQ_START+INTERRUPT_DMA1)
  5918. +#define FIQ_DMA2 (FIQ_START+INTERRUPT_DMA2)
  5919. +#define FIQ_DMA3 (FIQ_START+INTERRUPT_DMA3)
  5920. +#define FIQ_DMA4 (FIQ_START+INTERRUPT_DMA4)
  5921. +#define FIQ_DMA5 (FIQ_START+INTERRUPT_DMA5)
  5922. +#define FIQ_DMA6 (FIQ_START+INTERRUPT_DMA6)
  5923. +#define FIQ_DMA7 (FIQ_START+INTERRUPT_DMA7)
  5924. +#define FIQ_DMA8 (FIQ_START+INTERRUPT_DMA8)
  5925. +#define FIQ_DMA9 (FIQ_START+INTERRUPT_DMA9)
  5926. +#define FIQ_DMA10 (FIQ_START+INTERRUPT_DMA10)
  5927. +#define FIQ_DMA11 (FIQ_START+INTERRUPT_DMA11)
  5928. +#define FIQ_DMA12 (FIQ_START+INTERRUPT_DMA12)
  5929. +#define FIQ_AUX (FIQ_START+INTERRUPT_AUX)
  5930. +#define FIQ_ARM (FIQ_START+INTERRUPT_ARM)
  5931. +#define FIQ_VPUDMA (FIQ_START+INTERRUPT_VPUDMA)
  5932. +#define FIQ_HOSTPORT (FIQ_START+INTERRUPT_HOSTPORT)
  5933. +#define FIQ_VIDEOSCALER (FIQ_START+INTERRUPT_VIDEOSCALER)
  5934. +#define FIQ_CCP2TX (FIQ_START+INTERRUPT_CCP2TX)
  5935. +#define FIQ_SDC (FIQ_START+INTERRUPT_SDC)
  5936. +#define FIQ_DSI0 (FIQ_START+INTERRUPT_DSI0)
  5937. +#define FIQ_AVE (FIQ_START+INTERRUPT_AVE)
  5938. +#define FIQ_CAM0 (FIQ_START+INTERRUPT_CAM0)
  5939. +#define FIQ_CAM1 (FIQ_START+INTERRUPT_CAM1)
  5940. +#define FIQ_HDMI0 (FIQ_START+INTERRUPT_HDMI0)
  5941. +#define FIQ_HDMI1 (FIQ_START+INTERRUPT_HDMI1)
  5942. +#define FIQ_PIXELVALVE1 (FIQ_START+INTERRUPT_PIXELVALVE1)
  5943. +#define FIQ_I2CSPISLV (FIQ_START+INTERRUPT_I2CSPISLV)
  5944. +#define FIQ_DSI1 (FIQ_START+INTERRUPT_DSI1)
  5945. +#define FIQ_PWA0 (FIQ_START+INTERRUPT_PWA0)
  5946. +#define FIQ_PWA1 (FIQ_START+INTERRUPT_PWA1)
  5947. +#define FIQ_CPR (FIQ_START+INTERRUPT_CPR)
  5948. +#define FIQ_SMI (FIQ_START+INTERRUPT_SMI)
  5949. +#define FIQ_GPIO0 (FIQ_START+INTERRUPT_GPIO0)
  5950. +#define FIQ_GPIO1 (FIQ_START+INTERRUPT_GPIO1)
  5951. +#define FIQ_GPIO2 (FIQ_START+INTERRUPT_GPIO2)
  5952. +#define FIQ_GPIO3 (FIQ_START+INTERRUPT_GPIO3)
  5953. +#define FIQ_I2C (FIQ_START+INTERRUPT_I2C)
  5954. +#define FIQ_SPI (FIQ_START+INTERRUPT_SPI)
  5955. +#define FIQ_I2SPCM (FIQ_START+INTERRUPT_I2SPCM)
  5956. +#define FIQ_SDIO (FIQ_START+INTERRUPT_SDIO)
  5957. +#define FIQ_UART (FIQ_START+INTERRUPT_UART)
  5958. +#define FIQ_SLIMBUS (FIQ_START+INTERRUPT_SLIMBUS)
  5959. +#define FIQ_VEC (FIQ_START+INTERRUPT_VEC)
  5960. +#define FIQ_CPG (FIQ_START+INTERRUPT_CPG)
  5961. +#define FIQ_RNG (FIQ_START+INTERRUPT_RNG)
  5962. +#define FIQ_ARASANSDIO (FIQ_START+INTERRUPT_ARASANSDIO)
  5963. +#define FIQ_AVSPMON (FIQ_START+INTERRUPT_AVSPMON)
  5964. +
  5965. +#define FIQ_ARM_TIMER (FIQ_START+INTERRUPT_ARM_TIMER)
  5966. +#define FIQ_ARM_MAILBOX (FIQ_START+INTERRUPT_ARM_MAILBOX)
  5967. +#define FIQ_ARM_DOORBELL_0 (FIQ_START+INTERRUPT_ARM_DOORBELL_0)
  5968. +#define FIQ_ARM_DOORBELL_1 (FIQ_START+INTERRUPT_ARM_DOORBELL_1)
  5969. +#define FIQ_VPU0_HALTED (FIQ_START+INTERRUPT_VPU0_HALTED)
  5970. +#define FIQ_VPU1_HALTED (FIQ_START+INTERRUPT_VPU1_HALTED)
  5971. +#define FIQ_ILLEGAL_TYPE0 (FIQ_START+INTERRUPT_ILLEGAL_TYPE0)
  5972. +#define FIQ_ILLEGAL_TYPE1 (FIQ_START+INTERRUPT_ILLEGAL_TYPE1)
  5973. +#define FIQ_PENDING1 (FIQ_START+INTERRUPT_PENDING1)
  5974. +#define FIQ_PENDING2 (FIQ_START+INTERRUPT_PENDING2)
  5975. +
  5976. +#define HARD_IRQS (64 + 21)
  5977. +#define FIQ_IRQS (64 + 21)
  5978. +#define GPIO_IRQ_START (HARD_IRQS + FIQ_IRQS)
  5979. +#define GPIO_IRQS (32*5)
  5980. +#define SPARE_IRQS (64)
  5981. +#define NR_IRQS (HARD_IRQS+FIQ_IRQS+GPIO_IRQS+SPARE_IRQS)
  5982. +
  5983. +#endif /* _BCM2708_IRQS_H_ */
  5984. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/memory.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/memory.h
  5985. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/memory.h 1970-01-01 01:00:00.000000000 +0100
  5986. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/memory.h 2014-04-13 17:32:40.000000000 +0200
  5987. @@ -0,0 +1,57 @@
  5988. +/*
  5989. + * arch/arm/mach-bcm2708/include/mach/memory.h
  5990. + *
  5991. + * Copyright (C) 2010 Broadcom
  5992. + *
  5993. + * This program is free software; you can redistribute it and/or modify
  5994. + * it under the terms of the GNU General Public License as published by
  5995. + * the Free Software Foundation; either version 2 of the License, or
  5996. + * (at your option) any later version.
  5997. + *
  5998. + * This program is distributed in the hope that it will be useful,
  5999. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6000. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6001. + * GNU General Public License for more details.
  6002. + *
  6003. + * You should have received a copy of the GNU General Public License
  6004. + * along with this program; if not, write to the Free Software
  6005. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6006. + */
  6007. +#ifndef __ASM_ARCH_MEMORY_H
  6008. +#define __ASM_ARCH_MEMORY_H
  6009. +
  6010. +/* Memory overview:
  6011. +
  6012. + [ARMcore] <--virtual addr-->
  6013. + [ARMmmu] <--physical addr-->
  6014. + [GERTmap] <--bus add-->
  6015. + [VCperiph]
  6016. +
  6017. +*/
  6018. +
  6019. +/*
  6020. + * Physical DRAM offset.
  6021. + */
  6022. +#define PLAT_PHYS_OFFSET UL(0x00000000)
  6023. +#define VC_ARMMEM_OFFSET UL(0x00000000) /* offset in VC of ARM memory */
  6024. +
  6025. +#ifdef CONFIG_BCM2708_NOL2CACHE
  6026. + #define _REAL_BUS_OFFSET UL(0xC0000000) /* don't use L1 or L2 caches */
  6027. +#else
  6028. + #define _REAL_BUS_OFFSET UL(0x40000000) /* use L2 cache */
  6029. +#endif
  6030. +
  6031. +/* We're using the memory at 64M in the VideoCore for Linux - this adjustment
  6032. + * will provide the offset into this area as well as setting the bits that
  6033. + * stop the L1 and L2 cache from being used
  6034. + *
  6035. + * WARNING: this only works because the ARM is given memory at a fixed location
  6036. + * (ARMMEM_OFFSET)
  6037. + */
  6038. +#define BUS_OFFSET (VC_ARMMEM_OFFSET + _REAL_BUS_OFFSET)
  6039. +#define __virt_to_bus(x) ((x) + (BUS_OFFSET - PAGE_OFFSET))
  6040. +#define __bus_to_virt(x) ((x) - (BUS_OFFSET - PAGE_OFFSET))
  6041. +#define __pfn_to_bus(x) (__pfn_to_phys(x) + (BUS_OFFSET - PLAT_PHYS_OFFSET))
  6042. +#define __bus_to_pfn(x) __phys_to_pfn((x) - (BUS_OFFSET - PLAT_PHYS_OFFSET))
  6043. +
  6044. +#endif
  6045. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/platform.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/platform.h
  6046. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/platform.h 1970-01-01 01:00:00.000000000 +0100
  6047. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/platform.h 2014-04-13 17:32:40.000000000 +0200
  6048. @@ -0,0 +1,228 @@
  6049. +/*
  6050. + * arch/arm/mach-bcm2708/include/mach/platform.h
  6051. + *
  6052. + * Copyright (C) 2010 Broadcom
  6053. + *
  6054. + * This program is free software; you can redistribute it and/or modify
  6055. + * it under the terms of the GNU General Public License as published by
  6056. + * the Free Software Foundation; either version 2 of the License, or
  6057. + * (at your option) any later version.
  6058. + *
  6059. + * This program is distributed in the hope that it will be useful,
  6060. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6061. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6062. + * GNU General Public License for more details.
  6063. + *
  6064. + * You should have received a copy of the GNU General Public License
  6065. + * along with this program; if not, write to the Free Software
  6066. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6067. + */
  6068. +
  6069. +#ifndef _BCM2708_PLATFORM_H
  6070. +#define _BCM2708_PLATFORM_H
  6071. +
  6072. +
  6073. +/* macros to get at IO space when running virtually */
  6074. +#define IO_ADDRESS(x) (((x) & 0x0fffffff) + (((x) >> 4) & 0x0f000000) + 0xf0000000)
  6075. +
  6076. +#define __io_address(n) IOMEM(IO_ADDRESS(n))
  6077. +
  6078. +
  6079. +/*
  6080. + * SDRAM
  6081. + */
  6082. +#define BCM2708_SDRAM_BASE 0x00000000
  6083. +
  6084. +/*
  6085. + * Logic expansion modules
  6086. + *
  6087. + */
  6088. +
  6089. +
  6090. +/* ------------------------------------------------------------------------
  6091. + * BCM2708 ARMCTRL Registers
  6092. + * ------------------------------------------------------------------------
  6093. + */
  6094. +
  6095. +#define HW_REGISTER_RW(addr) (addr)
  6096. +#define HW_REGISTER_RO(addr) (addr)
  6097. +
  6098. +#include "arm_control.h"
  6099. +#undef ARM_BASE
  6100. +
  6101. +/*
  6102. + * Definitions and addresses for the ARM CONTROL logic
  6103. + * This file is manually generated.
  6104. + */
  6105. +
  6106. +#define BCM2708_PERI_BASE 0x20000000
  6107. +#define IC0_BASE (BCM2708_PERI_BASE + 0x2000)
  6108. +#define ST_BASE (BCM2708_PERI_BASE + 0x3000) /* System Timer */
  6109. +#define MPHI_BASE (BCM2708_PERI_BASE + 0x6000) /* Message -based Parallel Host Interface */
  6110. +#define DMA_BASE (BCM2708_PERI_BASE + 0x7000) /* DMA controller */
  6111. +#define ARM_BASE (BCM2708_PERI_BASE + 0xB000) /* BCM2708 ARM control block */
  6112. +#define PM_BASE (BCM2708_PERI_BASE + 0x100000) /* Power Management, Reset controller and Watchdog registers */
  6113. +#define PCM_CLOCK_BASE (BCM2708_PERI_BASE + 0x101098) /* PCM Clock */
  6114. +#define RNG_BASE (BCM2708_PERI_BASE + 0x104000) /* Hardware RNG */
  6115. +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO */
  6116. +#define UART0_BASE (BCM2708_PERI_BASE + 0x201000) /* Uart 0 */
  6117. +#define MMCI0_BASE (BCM2708_PERI_BASE + 0x202000) /* MMC interface */
  6118. +#define I2S_BASE (BCM2708_PERI_BASE + 0x203000) /* I2S */
  6119. +#define SPI0_BASE (BCM2708_PERI_BASE + 0x204000) /* SPI0 */
  6120. +#define BSC0_BASE (BCM2708_PERI_BASE + 0x205000) /* BSC0 I2C/TWI */
  6121. +#define UART1_BASE (BCM2708_PERI_BASE + 0x215000) /* Uart 1 */
  6122. +#define EMMC_BASE (BCM2708_PERI_BASE + 0x300000) /* eMMC interface */
  6123. +#define SMI_BASE (BCM2708_PERI_BASE + 0x600000) /* SMI */
  6124. +#define BSC1_BASE (BCM2708_PERI_BASE + 0x804000) /* BSC1 I2C/TWI */
  6125. +#define USB_BASE (BCM2708_PERI_BASE + 0x980000) /* DTC_OTG USB controller */
  6126. +#define MCORE_BASE (BCM2708_PERI_BASE + 0x0000) /* Fake frame buffer device (actually the multicore sync block*/
  6127. +
  6128. +#define ARMCTRL_BASE (ARM_BASE + 0x000)
  6129. +#define ARMCTRL_IC_BASE (ARM_BASE + 0x200) /* ARM interrupt controller */
  6130. +#define ARMCTRL_TIMER0_1_BASE (ARM_BASE + 0x400) /* Timer 0 and 1 */
  6131. +#define ARMCTRL_0_SBM_BASE (ARM_BASE + 0x800) /* User 0 (ARM)'s Semaphores Doorbells and Mailboxes */
  6132. +
  6133. +
  6134. +/*
  6135. + * Interrupt assignments
  6136. + */
  6137. +
  6138. +#define ARM_IRQ1_BASE 0
  6139. +#define INTERRUPT_TIMER0 (ARM_IRQ1_BASE + 0)
  6140. +#define INTERRUPT_TIMER1 (ARM_IRQ1_BASE + 1)
  6141. +#define INTERRUPT_TIMER2 (ARM_IRQ1_BASE + 2)
  6142. +#define INTERRUPT_TIMER3 (ARM_IRQ1_BASE + 3)
  6143. +#define INTERRUPT_CODEC0 (ARM_IRQ1_BASE + 4)
  6144. +#define INTERRUPT_CODEC1 (ARM_IRQ1_BASE + 5)
  6145. +#define INTERRUPT_CODEC2 (ARM_IRQ1_BASE + 6)
  6146. +#define INTERRUPT_VC_JPEG (ARM_IRQ1_BASE + 7)
  6147. +#define INTERRUPT_ISP (ARM_IRQ1_BASE + 8)
  6148. +#define INTERRUPT_VC_USB (ARM_IRQ1_BASE + 9)
  6149. +#define INTERRUPT_VC_3D (ARM_IRQ1_BASE + 10)
  6150. +#define INTERRUPT_TRANSPOSER (ARM_IRQ1_BASE + 11)
  6151. +#define INTERRUPT_MULTICORESYNC0 (ARM_IRQ1_BASE + 12)
  6152. +#define INTERRUPT_MULTICORESYNC1 (ARM_IRQ1_BASE + 13)
  6153. +#define INTERRUPT_MULTICORESYNC2 (ARM_IRQ1_BASE + 14)
  6154. +#define INTERRUPT_MULTICORESYNC3 (ARM_IRQ1_BASE + 15)
  6155. +#define INTERRUPT_DMA0 (ARM_IRQ1_BASE + 16)
  6156. +#define INTERRUPT_DMA1 (ARM_IRQ1_BASE + 17)
  6157. +#define INTERRUPT_VC_DMA2 (ARM_IRQ1_BASE + 18)
  6158. +#define INTERRUPT_VC_DMA3 (ARM_IRQ1_BASE + 19)
  6159. +#define INTERRUPT_DMA4 (ARM_IRQ1_BASE + 20)
  6160. +#define INTERRUPT_DMA5 (ARM_IRQ1_BASE + 21)
  6161. +#define INTERRUPT_DMA6 (ARM_IRQ1_BASE + 22)
  6162. +#define INTERRUPT_DMA7 (ARM_IRQ1_BASE + 23)
  6163. +#define INTERRUPT_DMA8 (ARM_IRQ1_BASE + 24)
  6164. +#define INTERRUPT_DMA9 (ARM_IRQ1_BASE + 25)
  6165. +#define INTERRUPT_DMA10 (ARM_IRQ1_BASE + 26)
  6166. +#define INTERRUPT_DMA11 (ARM_IRQ1_BASE + 27)
  6167. +#define INTERRUPT_DMA12 (ARM_IRQ1_BASE + 28)
  6168. +#define INTERRUPT_AUX (ARM_IRQ1_BASE + 29)
  6169. +#define INTERRUPT_ARM (ARM_IRQ1_BASE + 30)
  6170. +#define INTERRUPT_VPUDMA (ARM_IRQ1_BASE + 31)
  6171. +
  6172. +#define ARM_IRQ2_BASE 32
  6173. +#define INTERRUPT_HOSTPORT (ARM_IRQ2_BASE + 0)
  6174. +#define INTERRUPT_VIDEOSCALER (ARM_IRQ2_BASE + 1)
  6175. +#define INTERRUPT_CCP2TX (ARM_IRQ2_BASE + 2)
  6176. +#define INTERRUPT_SDC (ARM_IRQ2_BASE + 3)
  6177. +#define INTERRUPT_DSI0 (ARM_IRQ2_BASE + 4)
  6178. +#define INTERRUPT_AVE (ARM_IRQ2_BASE + 5)
  6179. +#define INTERRUPT_CAM0 (ARM_IRQ2_BASE + 6)
  6180. +#define INTERRUPT_CAM1 (ARM_IRQ2_BASE + 7)
  6181. +#define INTERRUPT_HDMI0 (ARM_IRQ2_BASE + 8)
  6182. +#define INTERRUPT_HDMI1 (ARM_IRQ2_BASE + 9)
  6183. +#define INTERRUPT_PIXELVALVE1 (ARM_IRQ2_BASE + 10)
  6184. +#define INTERRUPT_I2CSPISLV (ARM_IRQ2_BASE + 11)
  6185. +#define INTERRUPT_DSI1 (ARM_IRQ2_BASE + 12)
  6186. +#define INTERRUPT_PWA0 (ARM_IRQ2_BASE + 13)
  6187. +#define INTERRUPT_PWA1 (ARM_IRQ2_BASE + 14)
  6188. +#define INTERRUPT_CPR (ARM_IRQ2_BASE + 15)
  6189. +#define INTERRUPT_SMI (ARM_IRQ2_BASE + 16)
  6190. +#define INTERRUPT_GPIO0 (ARM_IRQ2_BASE + 17)
  6191. +#define INTERRUPT_GPIO1 (ARM_IRQ2_BASE + 18)
  6192. +#define INTERRUPT_GPIO2 (ARM_IRQ2_BASE + 19)
  6193. +#define INTERRUPT_GPIO3 (ARM_IRQ2_BASE + 20)
  6194. +#define INTERRUPT_VC_I2C (ARM_IRQ2_BASE + 21)
  6195. +#define INTERRUPT_VC_SPI (ARM_IRQ2_BASE + 22)
  6196. +#define INTERRUPT_VC_I2SPCM (ARM_IRQ2_BASE + 23)
  6197. +#define INTERRUPT_VC_SDIO (ARM_IRQ2_BASE + 24)
  6198. +#define INTERRUPT_VC_UART (ARM_IRQ2_BASE + 25)
  6199. +#define INTERRUPT_SLIMBUS (ARM_IRQ2_BASE + 26)
  6200. +#define INTERRUPT_VEC (ARM_IRQ2_BASE + 27)
  6201. +#define INTERRUPT_CPG (ARM_IRQ2_BASE + 28)
  6202. +#define INTERRUPT_RNG (ARM_IRQ2_BASE + 29)
  6203. +#define INTERRUPT_VC_ARASANSDIO (ARM_IRQ2_BASE + 30)
  6204. +#define INTERRUPT_AVSPMON (ARM_IRQ2_BASE + 31)
  6205. +
  6206. +#define ARM_IRQ0_BASE 64
  6207. +#define INTERRUPT_ARM_TIMER (ARM_IRQ0_BASE + 0)
  6208. +#define INTERRUPT_ARM_MAILBOX (ARM_IRQ0_BASE + 1)
  6209. +#define INTERRUPT_ARM_DOORBELL_0 (ARM_IRQ0_BASE + 2)
  6210. +#define INTERRUPT_ARM_DOORBELL_1 (ARM_IRQ0_BASE + 3)
  6211. +#define INTERRUPT_VPU0_HALTED (ARM_IRQ0_BASE + 4)
  6212. +#define INTERRUPT_VPU1_HALTED (ARM_IRQ0_BASE + 5)
  6213. +#define INTERRUPT_ILLEGAL_TYPE0 (ARM_IRQ0_BASE + 6)
  6214. +#define INTERRUPT_ILLEGAL_TYPE1 (ARM_IRQ0_BASE + 7)
  6215. +#define INTERRUPT_PENDING1 (ARM_IRQ0_BASE + 8)
  6216. +#define INTERRUPT_PENDING2 (ARM_IRQ0_BASE + 9)
  6217. +#define INTERRUPT_JPEG (ARM_IRQ0_BASE + 10)
  6218. +#define INTERRUPT_USB (ARM_IRQ0_BASE + 11)
  6219. +#define INTERRUPT_3D (ARM_IRQ0_BASE + 12)
  6220. +#define INTERRUPT_DMA2 (ARM_IRQ0_BASE + 13)
  6221. +#define INTERRUPT_DMA3 (ARM_IRQ0_BASE + 14)
  6222. +#define INTERRUPT_I2C (ARM_IRQ0_BASE + 15)
  6223. +#define INTERRUPT_SPI (ARM_IRQ0_BASE + 16)
  6224. +#define INTERRUPT_I2SPCM (ARM_IRQ0_BASE + 17)
  6225. +#define INTERRUPT_SDIO (ARM_IRQ0_BASE + 18)
  6226. +#define INTERRUPT_UART (ARM_IRQ0_BASE + 19)
  6227. +#define INTERRUPT_ARASANSDIO (ARM_IRQ0_BASE + 20)
  6228. +
  6229. +#define MAXIRQNUM (32 + 32 + 20)
  6230. +#define MAXFIQNUM (32 + 32 + 20)
  6231. +
  6232. +#define MAX_TIMER 2
  6233. +#define MAX_PERIOD 699050
  6234. +#define TICKS_PER_uSEC 1
  6235. +
  6236. +/*
  6237. + * These are useconds NOT ticks.
  6238. + *
  6239. + */
  6240. +#define mSEC_1 1000
  6241. +#define mSEC_5 (mSEC_1 * 5)
  6242. +#define mSEC_10 (mSEC_1 * 10)
  6243. +#define mSEC_25 (mSEC_1 * 25)
  6244. +#define SEC_1 (mSEC_1 * 1000)
  6245. +
  6246. +/*
  6247. + * Watchdog
  6248. + */
  6249. +#define PM_RSTC (PM_BASE+0x1c)
  6250. +#define PM_RSTS (PM_BASE+0x20)
  6251. +#define PM_WDOG (PM_BASE+0x24)
  6252. +
  6253. +#define PM_WDOG_RESET 0000000000
  6254. +#define PM_PASSWORD 0x5a000000
  6255. +#define PM_WDOG_TIME_SET 0x000fffff
  6256. +#define PM_RSTC_WRCFG_CLR 0xffffffcf
  6257. +#define PM_RSTC_WRCFG_SET 0x00000030
  6258. +#define PM_RSTC_WRCFG_FULL_RESET 0x00000020
  6259. +#define PM_RSTC_RESET 0x00000102
  6260. +
  6261. +#define PM_RSTS_HADPOR_SET 0x00001000
  6262. +#define PM_RSTS_HADSRH_SET 0x00000400
  6263. +#define PM_RSTS_HADSRF_SET 0x00000200
  6264. +#define PM_RSTS_HADSRQ_SET 0x00000100
  6265. +#define PM_RSTS_HADWRH_SET 0x00000040
  6266. +#define PM_RSTS_HADWRF_SET 0x00000020
  6267. +#define PM_RSTS_HADWRQ_SET 0x00000010
  6268. +#define PM_RSTS_HADDRH_SET 0x00000004
  6269. +#define PM_RSTS_HADDRF_SET 0x00000002
  6270. +#define PM_RSTS_HADDRQ_SET 0x00000001
  6271. +
  6272. +#define UART0_CLOCK 3000000
  6273. +
  6274. +#endif
  6275. +
  6276. +/* END */
  6277. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/power.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/power.h
  6278. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/power.h 1970-01-01 01:00:00.000000000 +0100
  6279. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/power.h 2014-04-13 17:32:40.000000000 +0200
  6280. @@ -0,0 +1,26 @@
  6281. +/*
  6282. + * linux/arch/arm/mach-bcm2708/power.h
  6283. + *
  6284. + * Copyright (C) 2010 Broadcom
  6285. + *
  6286. + * This program is free software; you can redistribute it and/or modify
  6287. + * it under the terms of the GNU General Public License version 2 as
  6288. + * published by the Free Software Foundation.
  6289. + *
  6290. + * This device provides a shared mechanism for controlling the power to
  6291. + * VideoCore subsystems.
  6292. + */
  6293. +
  6294. +#ifndef _MACH_BCM2708_POWER_H
  6295. +#define _MACH_BCM2708_POWER_H
  6296. +
  6297. +#include <linux/types.h>
  6298. +#include <mach/arm_power.h>
  6299. +
  6300. +typedef unsigned int BCM_POWER_HANDLE_T;
  6301. +
  6302. +extern int bcm_power_open(BCM_POWER_HANDLE_T *handle);
  6303. +extern int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request);
  6304. +extern int bcm_power_close(BCM_POWER_HANDLE_T handle);
  6305. +
  6306. +#endif
  6307. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/system.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/system.h
  6308. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/system.h 1970-01-01 01:00:00.000000000 +0100
  6309. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/system.h 2014-04-13 17:32:40.000000000 +0200
  6310. @@ -0,0 +1,38 @@
  6311. +/*
  6312. + * arch/arm/mach-bcm2708/include/mach/system.h
  6313. + *
  6314. + * Copyright (C) 2010 Broadcom
  6315. + * Copyright (C) 2003 ARM Limited
  6316. + * Copyright (C) 2000 Deep Blue Solutions Ltd
  6317. + *
  6318. + * This program is free software; you can redistribute it and/or modify
  6319. + * it under the terms of the GNU General Public License as published by
  6320. + * the Free Software Foundation; either version 2 of the License, or
  6321. + * (at your option) any later version.
  6322. + *
  6323. + * This program is distributed in the hope that it will be useful,
  6324. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6325. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6326. + * GNU General Public License for more details.
  6327. + *
  6328. + * You should have received a copy of the GNU General Public License
  6329. + * along with this program; if not, write to the Free Software
  6330. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6331. + */
  6332. +#ifndef __ASM_ARCH_SYSTEM_H
  6333. +#define __ASM_ARCH_SYSTEM_H
  6334. +
  6335. +#include <linux/io.h>
  6336. +#include <mach/hardware.h>
  6337. +#include <mach/platform.h>
  6338. +
  6339. +static inline void arch_idle(void)
  6340. +{
  6341. + /*
  6342. + * This should do all the clock switching
  6343. + * and wait for interrupt tricks
  6344. + */
  6345. + cpu_do_idle();
  6346. +}
  6347. +
  6348. +#endif
  6349. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/timex.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/timex.h
  6350. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/timex.h 1970-01-01 01:00:00.000000000 +0100
  6351. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/timex.h 2014-04-13 17:32:40.000000000 +0200
  6352. @@ -0,0 +1,23 @@
  6353. +/*
  6354. + * arch/arm/mach-bcm2708/include/mach/timex.h
  6355. + *
  6356. + * BCM2708 sysem clock frequency
  6357. + *
  6358. + * Copyright (C) 2010 Broadcom
  6359. + *
  6360. + * This program is free software; you can redistribute it and/or modify
  6361. + * it under the terms of the GNU General Public License as published by
  6362. + * the Free Software Foundation; either version 2 of the License, or
  6363. + * (at your option) any later version.
  6364. + *
  6365. + * This program is distributed in the hope that it will be useful,
  6366. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6367. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6368. + * GNU General Public License for more details.
  6369. + *
  6370. + * You should have received a copy of the GNU General Public License
  6371. + * along with this program; if not, write to the Free Software
  6372. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6373. + */
  6374. +
  6375. +#define CLOCK_TICK_RATE (1000000)
  6376. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/uncompress.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h
  6377. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/uncompress.h 1970-01-01 01:00:00.000000000 +0100
  6378. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/uncompress.h 2014-09-14 19:03:05.000000000 +0200
  6379. @@ -0,0 +1,84 @@
  6380. +/*
  6381. + * arch/arm/mach-bcn2708/include/mach/uncompress.h
  6382. + *
  6383. + * Copyright (C) 2010 Broadcom
  6384. + * Copyright (C) 2003 ARM Limited
  6385. + *
  6386. + * This program is free software; you can redistribute it and/or modify
  6387. + * it under the terms of the GNU General Public License as published by
  6388. + * the Free Software Foundation; either version 2 of the License, or
  6389. + * (at your option) any later version.
  6390. + *
  6391. + * This program is distributed in the hope that it will be useful,
  6392. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6393. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6394. + * GNU General Public License for more details.
  6395. + *
  6396. + * You should have received a copy of the GNU General Public License
  6397. + * along with this program; if not, write to the Free Software
  6398. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6399. + */
  6400. +
  6401. +#include <linux/io.h>
  6402. +#include <linux/amba/serial.h>
  6403. +#include <mach/hardware.h>
  6404. +
  6405. +#define UART_BAUD 115200
  6406. +
  6407. +#define BCM2708_UART_DR __io(UART0_BASE + UART01x_DR)
  6408. +#define BCM2708_UART_FR __io(UART0_BASE + UART01x_FR)
  6409. +#define BCM2708_UART_IBRD __io(UART0_BASE + UART011_IBRD)
  6410. +#define BCM2708_UART_FBRD __io(UART0_BASE + UART011_FBRD)
  6411. +#define BCM2708_UART_LCRH __io(UART0_BASE + UART011_LCRH)
  6412. +#define BCM2708_UART_CR __io(UART0_BASE + UART011_CR)
  6413. +
  6414. +/*
  6415. + * This does not append a newline
  6416. + */
  6417. +static inline void putc(int c)
  6418. +{
  6419. + while (__raw_readl(BCM2708_UART_FR) & UART01x_FR_TXFF)
  6420. + barrier();
  6421. +
  6422. + __raw_writel(c, BCM2708_UART_DR);
  6423. +}
  6424. +
  6425. +static inline void flush(void)
  6426. +{
  6427. + int fr;
  6428. +
  6429. + do {
  6430. + fr = __raw_readl(BCM2708_UART_FR);
  6431. + barrier();
  6432. + } while ((fr & (UART011_FR_TXFE | UART01x_FR_BUSY)) != UART011_FR_TXFE);
  6433. +}
  6434. +
  6435. +static inline void arch_decomp_setup(void)
  6436. +{
  6437. + int temp, div, rem, frac;
  6438. +
  6439. + temp = 16 * UART_BAUD;
  6440. + div = UART0_CLOCK / temp;
  6441. + rem = UART0_CLOCK % temp;
  6442. + temp = (8 * rem) / UART_BAUD;
  6443. + frac = (temp >> 1) + (temp & 1);
  6444. +
  6445. + /* Make sure the UART is disabled before we start */
  6446. + __raw_writel(0, BCM2708_UART_CR);
  6447. +
  6448. + /* Set the baud rate */
  6449. + __raw_writel(div, BCM2708_UART_IBRD);
  6450. + __raw_writel(frac, BCM2708_UART_FBRD);
  6451. +
  6452. + /* Set the UART to 8n1, FIFO enabled */
  6453. + __raw_writel(UART01x_LCRH_WLEN_8 | UART01x_LCRH_FEN, BCM2708_UART_LCRH);
  6454. +
  6455. + /* Enable the UART */
  6456. + __raw_writel(UART01x_CR_UARTEN | UART011_CR_TXE | UART011_CR_RXE,
  6457. + BCM2708_UART_CR);
  6458. +}
  6459. +
  6460. +/*
  6461. + * nothing to do
  6462. + */
  6463. +#define arch_decomp_wdog()
  6464. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vcio.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h
  6465. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vcio.h 1970-01-01 01:00:00.000000000 +0100
  6466. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vcio.h 2014-09-14 19:03:05.000000000 +0200
  6467. @@ -0,0 +1,148 @@
  6468. +/*
  6469. + * arch/arm/mach-bcm2708/include/mach/vcio.h
  6470. + *
  6471. + * Copyright (C) 2010 Broadcom
  6472. + *
  6473. + * This program is free software; you can redistribute it and/or modify
  6474. + * it under the terms of the GNU General Public License as published by
  6475. + * the Free Software Foundation; either version 2 of the License, or
  6476. + * (at your option) any later version.
  6477. + *
  6478. + * This program is distributed in the hope that it will be useful,
  6479. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6480. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6481. + * GNU General Public License for more details.
  6482. + *
  6483. + * You should have received a copy of the GNU General Public License
  6484. + * along with this program; if not, write to the Free Software
  6485. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  6486. + */
  6487. +#ifndef _MACH_BCM2708_VCIO_H
  6488. +#define _MACH_BCM2708_VCIO_H
  6489. +
  6490. +/* Routines to handle I/O via the VideoCore "ARM control" registers
  6491. + * (semaphores, doorbells, mailboxes)
  6492. + */
  6493. +
  6494. +#define BCM_VCIO_DRIVER_NAME "bcm2708_vcio"
  6495. +
  6496. +/* Constants shared with the ARM identifying separate mailbox channels */
  6497. +#define MBOX_CHAN_POWER 0 /* for use by the power management interface */
  6498. +#define MBOX_CHAN_FB 1 /* for use by the frame buffer */
  6499. +#define MBOX_CHAN_VCHIQ 3 /* for use by the VCHIQ interface */
  6500. +#define MBOX_CHAN_PROPERTY 8 /* for use by the property channel */
  6501. +#define MBOX_CHAN_COUNT 9
  6502. +
  6503. +enum {
  6504. + VCMSG_PROCESS_REQUEST = 0x00000000
  6505. +};
  6506. +enum {
  6507. + VCMSG_REQUEST_SUCCESSFUL = 0x80000000,
  6508. + VCMSG_REQUEST_FAILED = 0x80000001
  6509. +};
  6510. +/* Mailbox property tags */
  6511. +enum {
  6512. + VCMSG_PROPERTY_END = 0x00000000,
  6513. + VCMSG_GET_FIRMWARE_REVISION = 0x00000001,
  6514. + VCMSG_GET_BOARD_MODEL = 0x00010001,
  6515. + VCMSG_GET_BOARD_REVISION = 0x00020002,
  6516. + VCMSG_GET_BOARD_MAC_ADDRESS = 0x00020003,
  6517. + VCMSG_GET_BOARD_SERIAL = 0x00020004,
  6518. + VCMSG_GET_ARM_MEMORY = 0x00020005,
  6519. + VCMSG_GET_VC_MEMORY = 0x00020006,
  6520. + VCMSG_GET_CLOCKS = 0x00020007,
  6521. + VCMSG_GET_COMMAND_LINE = 0x00050001,
  6522. + VCMSG_GET_DMA_CHANNELS = 0x00060001,
  6523. + VCMSG_GET_POWER_STATE = 0x00020001,
  6524. + VCMSG_GET_TIMING = 0x00020002,
  6525. + VCMSG_SET_POWER_STATE = 0x00028001,
  6526. + VCMSG_GET_CLOCK_STATE = 0x00030001,
  6527. + VCMSG_SET_CLOCK_STATE = 0x00038001,
  6528. + VCMSG_GET_CLOCK_RATE = 0x00030002,
  6529. + VCMSG_SET_CLOCK_RATE = 0x00038002,
  6530. + VCMSG_GET_VOLTAGE = 0x00030003,
  6531. + VCMSG_SET_VOLTAGE = 0x00038003,
  6532. + VCMSG_GET_MAX_CLOCK = 0x00030004,
  6533. + VCMSG_GET_MAX_VOLTAGE = 0x00030005,
  6534. + VCMSG_GET_TEMPERATURE = 0x00030006,
  6535. + VCMSG_GET_MIN_CLOCK = 0x00030007,
  6536. + VCMSG_GET_MIN_VOLTAGE = 0x00030008,
  6537. + VCMSG_GET_TURBO = 0x00030009,
  6538. + VCMSG_SET_TURBO = 0x00038009,
  6539. + VCMSG_SET_ALLOCATE_BUFFER = 0x00040001,
  6540. + VCMSG_SET_RELEASE_BUFFER = 0x00048001,
  6541. + VCMSG_SET_BLANK_SCREEN = 0x00040002,
  6542. + VCMSG_TST_BLANK_SCREEN = 0x00044002,
  6543. + VCMSG_GET_PHYSICAL_WIDTH_HEIGHT = 0x00040003,
  6544. + VCMSG_TST_PHYSICAL_WIDTH_HEIGHT = 0x00044003,
  6545. + VCMSG_SET_PHYSICAL_WIDTH_HEIGHT = 0x00048003,
  6546. + VCMSG_GET_VIRTUAL_WIDTH_HEIGHT = 0x00040004,
  6547. + VCMSG_TST_VIRTUAL_WIDTH_HEIGHT = 0x00044004,
  6548. + VCMSG_SET_VIRTUAL_WIDTH_HEIGHT = 0x00048004,
  6549. + VCMSG_GET_DEPTH = 0x00040005,
  6550. + VCMSG_TST_DEPTH = 0x00044005,
  6551. + VCMSG_SET_DEPTH = 0x00048005,
  6552. + VCMSG_GET_PIXEL_ORDER = 0x00040006,
  6553. + VCMSG_TST_PIXEL_ORDER = 0x00044006,
  6554. + VCMSG_SET_PIXEL_ORDER = 0x00048006,
  6555. + VCMSG_GET_ALPHA_MODE = 0x00040007,
  6556. + VCMSG_TST_ALPHA_MODE = 0x00044007,
  6557. + VCMSG_SET_ALPHA_MODE = 0x00048007,
  6558. + VCMSG_GET_PITCH = 0x00040008,
  6559. + VCMSG_TST_PITCH = 0x00044008,
  6560. + VCMSG_SET_PITCH = 0x00048008,
  6561. + VCMSG_GET_VIRTUAL_OFFSET = 0x00040009,
  6562. + VCMSG_TST_VIRTUAL_OFFSET = 0x00044009,
  6563. + VCMSG_SET_VIRTUAL_OFFSET = 0x00048009,
  6564. + VCMSG_GET_OVERSCAN = 0x0004000a,
  6565. + VCMSG_TST_OVERSCAN = 0x0004400a,
  6566. + VCMSG_SET_OVERSCAN = 0x0004800a,
  6567. + VCMSG_GET_PALETTE = 0x0004000b,
  6568. + VCMSG_TST_PALETTE = 0x0004400b,
  6569. + VCMSG_SET_PALETTE = 0x0004800b,
  6570. + VCMSG_GET_LAYER = 0x0004000c,
  6571. + VCMSG_TST_LAYER = 0x0004400c,
  6572. + VCMSG_SET_LAYER = 0x0004800c,
  6573. + VCMSG_GET_TRANSFORM = 0x0004000d,
  6574. + VCMSG_TST_TRANSFORM = 0x0004400d,
  6575. + VCMSG_SET_TRANSFORM = 0x0004800d,
  6576. +};
  6577. +
  6578. +extern int /*rc*/ bcm_mailbox_read(unsigned chan, uint32_t *data28);
  6579. +extern int /*rc*/ bcm_mailbox_write(unsigned chan, uint32_t data28);
  6580. +extern int /*rc*/ bcm_mailbox_property(void *data, int size);
  6581. +
  6582. +#include <linux/ioctl.h>
  6583. +
  6584. +/*
  6585. + * The major device number. We can't rely on dynamic
  6586. + * registration any more, because ioctls need to know
  6587. + * it.
  6588. + */
  6589. +#define MAJOR_NUM 100
  6590. +
  6591. +/*
  6592. + * Set the message of the device driver
  6593. + */
  6594. +#define IOCTL_MBOX_PROPERTY _IOWR(MAJOR_NUM, 0, char *)
  6595. +/*
  6596. + * _IOWR means that we're creating an ioctl command
  6597. + * number for passing information from a user process
  6598. + * to the kernel module and from the kernel module to user process
  6599. + *
  6600. + * The first arguments, MAJOR_NUM, is the major device
  6601. + * number we're using.
  6602. + *
  6603. + * The second argument is the number of the command
  6604. + * (there could be several with different meanings).
  6605. + *
  6606. + * The third argument is the type we want to get from
  6607. + * the process to the kernel.
  6608. + */
  6609. +
  6610. +/*
  6611. + * The name of the device file
  6612. + */
  6613. +#define DEVICE_FILE_NAME "vcio"
  6614. +
  6615. +#endif
  6616. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_mem.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h
  6617. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_mem.h 1970-01-01 01:00:00.000000000 +0100
  6618. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_mem.h 2014-09-14 19:03:05.000000000 +0200
  6619. @@ -0,0 +1,35 @@
  6620. +/*****************************************************************************
  6621. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  6622. +*
  6623. +* Unless you and Broadcom execute a separate written software license
  6624. +* agreement governing use of this software, this software is licensed to you
  6625. +* under the terms of the GNU General Public License version 2, available at
  6626. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  6627. +*
  6628. +* Notwithstanding the above, under no circumstances may you combine this
  6629. +* software in any way with any other Broadcom software provided under a
  6630. +* license other than the GPL, without Broadcom's express prior written
  6631. +* consent.
  6632. +*****************************************************************************/
  6633. +
  6634. +#if !defined( VC_MEM_H )
  6635. +#define VC_MEM_H
  6636. +
  6637. +#include <linux/ioctl.h>
  6638. +
  6639. +#define VC_MEM_IOC_MAGIC 'v'
  6640. +
  6641. +#define VC_MEM_IOC_MEM_PHYS_ADDR _IOR( VC_MEM_IOC_MAGIC, 0, unsigned long )
  6642. +#define VC_MEM_IOC_MEM_SIZE _IOR( VC_MEM_IOC_MAGIC, 1, unsigned int )
  6643. +#define VC_MEM_IOC_MEM_BASE _IOR( VC_MEM_IOC_MAGIC, 2, unsigned int )
  6644. +#define VC_MEM_IOC_MEM_LOAD _IOR( VC_MEM_IOC_MAGIC, 3, unsigned int )
  6645. +
  6646. +#if defined( __KERNEL__ )
  6647. +#define VC_MEM_TO_ARM_ADDR_MASK 0x3FFFFFFF
  6648. +
  6649. +extern unsigned long mm_vc_mem_phys_addr;
  6650. +extern unsigned int mm_vc_mem_size;
  6651. +extern int vc_mem_get_current_size( void );
  6652. +#endif
  6653. +
  6654. +#endif /* VC_MEM_H */
  6655. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h
  6656. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h 1970-01-01 01:00:00.000000000 +0100
  6657. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_defs.h 2014-09-14 19:03:05.000000000 +0200
  6658. @@ -0,0 +1,181 @@
  6659. +/*****************************************************************************
  6660. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  6661. +*
  6662. +* Unless you and Broadcom execute a separate written software license
  6663. +* agreement governing use of this software, this software is licensed to you
  6664. +* under the terms of the GNU General Public License version 2, available at
  6665. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  6666. +*
  6667. +* Notwithstanding the above, under no circumstances may you combine this
  6668. +* software in any way with any other Broadcom software provided under a
  6669. +* license other than the GPL, without Broadcom's express prior written
  6670. +* consent.
  6671. +*****************************************************************************/
  6672. +
  6673. +#ifndef __VC_SM_DEFS_H__INCLUDED__
  6674. +#define __VC_SM_DEFS_H__INCLUDED__
  6675. +
  6676. +/* FourCC code used for VCHI connection */
  6677. +#define VC_SM_SERVER_NAME MAKE_FOURCC("SMEM")
  6678. +
  6679. +/* Maximum message length */
  6680. +#define VC_SM_MAX_MSG_LEN (sizeof(VC_SM_MSG_UNION_T) + \
  6681. + sizeof(VC_SM_MSG_HDR_T))
  6682. +#define VC_SM_MAX_RSP_LEN (sizeof(VC_SM_MSG_UNION_T))
  6683. +
  6684. +/* Resource name maximum size */
  6685. +#define VC_SM_RESOURCE_NAME 32
  6686. +
  6687. +/* All message types supported for HOST->VC direction */
  6688. +typedef enum {
  6689. + /* Allocate shared memory block */
  6690. + VC_SM_MSG_TYPE_ALLOC,
  6691. + /* Lock allocated shared memory block */
  6692. + VC_SM_MSG_TYPE_LOCK,
  6693. + /* Unlock allocated shared memory block */
  6694. + VC_SM_MSG_TYPE_UNLOCK,
  6695. + /* Unlock allocated shared memory block, do not answer command */
  6696. + VC_SM_MSG_TYPE_UNLOCK_NOANS,
  6697. + /* Free shared memory block */
  6698. + VC_SM_MSG_TYPE_FREE,
  6699. + /* Resize a shared memory block */
  6700. + VC_SM_MSG_TYPE_RESIZE,
  6701. + /* Walk the allocated shared memory block(s) */
  6702. + VC_SM_MSG_TYPE_WALK_ALLOC,
  6703. +
  6704. + /* A previously applied action will need to be reverted */
  6705. + VC_SM_MSG_TYPE_ACTION_CLEAN,
  6706. + VC_SM_MSG_TYPE_MAX
  6707. +} VC_SM_MSG_TYPE;
  6708. +
  6709. +/* Type of memory to be allocated */
  6710. +typedef enum {
  6711. + VC_SM_ALLOC_CACHED,
  6712. + VC_SM_ALLOC_NON_CACHED,
  6713. +
  6714. +} VC_SM_ALLOC_TYPE_T;
  6715. +
  6716. +/* Message header for all messages in HOST->VC direction */
  6717. +typedef struct {
  6718. + int32_t type;
  6719. + uint32_t trans_id;
  6720. + uint8_t body[0];
  6721. +
  6722. +} VC_SM_MSG_HDR_T;
  6723. +
  6724. +/* Request to allocate memory (HOST->VC) */
  6725. +typedef struct {
  6726. + /* type of memory to allocate */
  6727. + VC_SM_ALLOC_TYPE_T type;
  6728. + /* byte amount of data to allocate per unit */
  6729. + uint32_t base_unit;
  6730. + /* number of unit to allocate */
  6731. + uint32_t num_unit;
  6732. + /* alignement to be applied on allocation */
  6733. + uint32_t alignement;
  6734. + /* identity of who allocated this block */
  6735. + uint32_t allocator;
  6736. + /* resource name (for easier tracking on vc side) */
  6737. + char name[VC_SM_RESOURCE_NAME];
  6738. +
  6739. +} VC_SM_ALLOC_T;
  6740. +
  6741. +/* Result of a requested memory allocation (VC->HOST) */
  6742. +typedef struct {
  6743. + /* Transaction identifier */
  6744. + uint32_t trans_id;
  6745. +
  6746. + /* Resource handle */
  6747. + uint32_t res_handle;
  6748. + /* Pointer to resource buffer */
  6749. + void *res_mem;
  6750. + /* Resource base size (bytes) */
  6751. + uint32_t res_base_size;
  6752. + /* Resource number */
  6753. + uint32_t res_num;
  6754. +
  6755. +} VC_SM_ALLOC_RESULT_T;
  6756. +
  6757. +/* Request to free a previously allocated memory (HOST->VC) */
  6758. +typedef struct {
  6759. + /* Resource handle (returned from alloc) */
  6760. + uint32_t res_handle;
  6761. + /* Resource buffer (returned from alloc) */
  6762. + void *res_mem;
  6763. +
  6764. +} VC_SM_FREE_T;
  6765. +
  6766. +/* Request to lock a previously allocated memory (HOST->VC) */
  6767. +typedef struct {
  6768. + /* Resource handle (returned from alloc) */
  6769. + uint32_t res_handle;
  6770. + /* Resource buffer (returned from alloc) */
  6771. + void *res_mem;
  6772. +
  6773. +} VC_SM_LOCK_UNLOCK_T;
  6774. +
  6775. +/* Request to resize a previously allocated memory (HOST->VC) */
  6776. +typedef struct {
  6777. + /* Resource handle (returned from alloc) */
  6778. + uint32_t res_handle;
  6779. + /* Resource buffer (returned from alloc) */
  6780. + void *res_mem;
  6781. + /* Resource *new* size requested (bytes) */
  6782. + uint32_t res_new_size;
  6783. +
  6784. +} VC_SM_RESIZE_T;
  6785. +
  6786. +/* Result of a requested memory lock (VC->HOST) */
  6787. +typedef struct {
  6788. + /* Transaction identifier */
  6789. + uint32_t trans_id;
  6790. +
  6791. + /* Resource handle */
  6792. + uint32_t res_handle;
  6793. + /* Pointer to resource buffer */
  6794. + void *res_mem;
  6795. + /* Pointer to former resource buffer if the memory
  6796. + * was reallocated */
  6797. + void *res_old_mem;
  6798. +
  6799. +} VC_SM_LOCK_RESULT_T;
  6800. +
  6801. +/* Generic result for a request (VC->HOST) */
  6802. +typedef struct {
  6803. + /* Transaction identifier */
  6804. + uint32_t trans_id;
  6805. +
  6806. + int32_t success;
  6807. +
  6808. +} VC_SM_RESULT_T;
  6809. +
  6810. +/* Request to revert a previously applied action (HOST->VC) */
  6811. +typedef struct {
  6812. + /* Action of interest */
  6813. + VC_SM_MSG_TYPE res_action;
  6814. + /* Transaction identifier for the action of interest */
  6815. + uint32_t action_trans_id;
  6816. +
  6817. +} VC_SM_ACTION_CLEAN_T;
  6818. +
  6819. +/* Request to remove all data associated with a given allocator (HOST->VC) */
  6820. +typedef struct {
  6821. + /* Allocator identifier */
  6822. + uint32_t allocator;
  6823. +
  6824. +} VC_SM_FREE_ALL_T;
  6825. +
  6826. +/* Union of ALL messages */
  6827. +typedef union {
  6828. + VC_SM_ALLOC_T alloc;
  6829. + VC_SM_ALLOC_RESULT_T alloc_result;
  6830. + VC_SM_FREE_T free;
  6831. + VC_SM_ACTION_CLEAN_T action_clean;
  6832. + VC_SM_RESIZE_T resize;
  6833. + VC_SM_LOCK_RESULT_T lock_result;
  6834. + VC_SM_RESULT_T result;
  6835. + VC_SM_FREE_ALL_T free_all;
  6836. +
  6837. +} VC_SM_MSG_UNION_T;
  6838. +
  6839. +#endif /* __VC_SM_DEFS_H__INCLUDED__ */
  6840. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h
  6841. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h 1970-01-01 01:00:00.000000000 +0100
  6842. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_sm_knl.h 2014-09-14 19:03:05.000000000 +0200
  6843. @@ -0,0 +1,55 @@
  6844. +/*****************************************************************************
  6845. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  6846. +*
  6847. +* Unless you and Broadcom execute a separate written software license
  6848. +* agreement governing use of this software, this software is licensed to you
  6849. +* under the terms of the GNU General Public License version 2, available at
  6850. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  6851. +*
  6852. +* Notwithstanding the above, under no circumstances may you combine this
  6853. +* software in any way with any other Broadcom software provided under a
  6854. +* license other than the GPL, without Broadcom's express prior written
  6855. +* consent.
  6856. +*****************************************************************************/
  6857. +
  6858. +#ifndef __VC_SM_KNL_H__INCLUDED__
  6859. +#define __VC_SM_KNL_H__INCLUDED__
  6860. +
  6861. +#if !defined(__KERNEL__)
  6862. +#error "This interface is for kernel use only..."
  6863. +#endif
  6864. +
  6865. +/* Type of memory to be locked (ie mapped) */
  6866. +typedef enum {
  6867. + VC_SM_LOCK_CACHED,
  6868. + VC_SM_LOCK_NON_CACHED,
  6869. +
  6870. +} VC_SM_LOCK_CACHE_MODE_T;
  6871. +
  6872. +/* Allocate a shared memory handle and block.
  6873. +*/
  6874. +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle);
  6875. +
  6876. +/* Free a previously allocated shared memory handle and block.
  6877. +*/
  6878. +int vc_sm_free(int handle);
  6879. +
  6880. +/* Lock a memory handle for use by kernel.
  6881. +*/
  6882. +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
  6883. + long unsigned int *data);
  6884. +
  6885. +/* Unlock a memory handle in use by kernel.
  6886. +*/
  6887. +int vc_sm_unlock(int handle, int flush, int no_vc_unlock);
  6888. +
  6889. +/* Get an internal resource handle mapped from the external one.
  6890. +*/
  6891. +int vc_sm_int_handle(int handle);
  6892. +
  6893. +/* Map a shared memory region for use by kernel.
  6894. +*/
  6895. +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
  6896. + long unsigned int *data);
  6897. +
  6898. +#endif /* __VC_SM_KNL_H__INCLUDED__ */
  6899. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h
  6900. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h 1970-01-01 01:00:00.000000000 +0100
  6901. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vc_vchi_sm.h 2014-09-14 19:03:05.000000000 +0200
  6902. @@ -0,0 +1,82 @@
  6903. +/*****************************************************************************
  6904. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  6905. +*
  6906. +* Unless you and Broadcom execute a separate written software license
  6907. +* agreement governing use of this software, this software is licensed to you
  6908. +* under the terms of the GNU General Public License version 2, available at
  6909. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  6910. +*
  6911. +* Notwithstanding the above, under no circumstances may you combine this
  6912. +* software in any way with any other Broadcom software provided under a
  6913. +* license other than the GPL, without Broadcom's express prior written
  6914. +* consent.
  6915. +*****************************************************************************/
  6916. +
  6917. +#ifndef __VC_VCHI_SM_H__INCLUDED__
  6918. +#define __VC_VCHI_SM_H__INCLUDED__
  6919. +
  6920. +#include "interface/vchi/vchi.h"
  6921. +
  6922. +#include "vc_sm_defs.h"
  6923. +
  6924. +/* Forward declare.
  6925. +*/
  6926. +typedef struct sm_instance *VC_VCHI_SM_HANDLE_T;
  6927. +
  6928. +/* Initialize the shared memory service, opens up vchi connection to talk to it.
  6929. +*/
  6930. +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
  6931. + VCHI_CONNECTION_T **vchi_connections,
  6932. + uint32_t num_connections);
  6933. +
  6934. +/* Terminates the shared memory service.
  6935. +*/
  6936. +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle);
  6937. +
  6938. +/* Ask the shared memory service to allocate some memory on videocre and
  6939. +** return the result of this allocation (which upon success will be a pointer
  6940. +** to some memory in videocore space).
  6941. +*/
  6942. +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle,
  6943. + VC_SM_ALLOC_T *alloc,
  6944. + VC_SM_ALLOC_RESULT_T *alloc_result, uint32_t *trans_id);
  6945. +
  6946. +/* Ask the shared memory service to free up some memory that was previously
  6947. +** allocated by the vc_vchi_sm_alloc function call.
  6948. +*/
  6949. +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
  6950. + VC_SM_FREE_T *free, uint32_t *trans_id);
  6951. +
  6952. +/* Ask the shared memory service to lock up some memory that was previously
  6953. +** allocated by the vc_vchi_sm_alloc function call.
  6954. +*/
  6955. +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
  6956. + VC_SM_LOCK_UNLOCK_T *lock_unlock,
  6957. + VC_SM_LOCK_RESULT_T *lock_result, uint32_t *trans_id);
  6958. +
  6959. +/* Ask the shared memory service to unlock some memory that was previously
  6960. +** allocated by the vc_vchi_sm_alloc function call.
  6961. +*/
  6962. +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
  6963. + VC_SM_LOCK_UNLOCK_T *lock_unlock,
  6964. + uint32_t *trans_id, uint8_t wait_reply);
  6965. +
  6966. +/* Ask the shared memory service to resize some memory that was previously
  6967. +** allocated by the vc_vchi_sm_alloc function call.
  6968. +*/
  6969. +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle,
  6970. + VC_SM_RESIZE_T *resize, uint32_t *trans_id);
  6971. +
  6972. +/* Walk the allocated resources on the videocore side, the allocation will
  6973. +** show up in the log. This is purely for debug/information and takes no
  6974. +** specific actions.
  6975. +*/
  6976. +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle);
  6977. +
  6978. +/* Clean up following a previously interrupted action which left the system
  6979. +** in a bad state of some sort.
  6980. +*/
  6981. +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle,
  6982. + VC_SM_ACTION_CLEAN_T *action_clean);
  6983. +
  6984. +#endif /* __VC_VCHI_SM_H__INCLUDED__ */
  6985. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vmalloc.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h
  6986. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vmalloc.h 1970-01-01 01:00:00.000000000 +0100
  6987. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vmalloc.h 2014-04-13 17:32:40.000000000 +0200
  6988. @@ -0,0 +1,20 @@
  6989. +/*
  6990. + * arch/arm/mach-bcm2708/include/mach/vmalloc.h
  6991. + *
  6992. + * Copyright (C) 2010 Broadcom
  6993. + *
  6994. + * This program is free software; you can redistribute it and/or modify
  6995. + * it under the terms of the GNU General Public License as published by
  6996. + * the Free Software Foundation; either version 2 of the License, or
  6997. + * (at your option) any later version.
  6998. + *
  6999. + * This program is distributed in the hope that it will be useful,
  7000. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7001. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7002. + * GNU General Public License for more details.
  7003. + *
  7004. + * You should have received a copy of the GNU General Public License
  7005. + * along with this program; if not, write to the Free Software
  7006. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  7007. + */
  7008. +#define VMALLOC_END (0xe8000000)
  7009. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h
  7010. --- linux-3.16.2/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h 1970-01-01 01:00:00.000000000 +0100
  7011. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/include/mach/vmcs_sm_ioctl.h 2014-09-14 19:03:05.000000000 +0200
  7012. @@ -0,0 +1,233 @@
  7013. +/*****************************************************************************
  7014. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  7015. +*
  7016. +* Unless you and Broadcom execute a separate written software license
  7017. +* agreement governing use of this software, this software is licensed to you
  7018. +* under the terms of the GNU General Public License version 2, available at
  7019. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  7020. +*
  7021. +* Notwithstanding the above, under no circumstances may you combine this
  7022. +* software in any way with any other Broadcom software provided under a
  7023. +* license other than the GPL, without Broadcom's express prior written
  7024. +* consent.
  7025. +*
  7026. +*****************************************************************************/
  7027. +
  7028. +#if !defined(__VMCS_SM_IOCTL_H__INCLUDED__)
  7029. +#define __VMCS_SM_IOCTL_H__INCLUDED__
  7030. +
  7031. +/* ---- Include Files ---------------------------------------------------- */
  7032. +
  7033. +#if defined(__KERNEL__)
  7034. +#include <linux/types.h> /* Needed for standard types */
  7035. +#else
  7036. +#include <stdint.h>
  7037. +#endif
  7038. +
  7039. +#include <linux/ioctl.h>
  7040. +
  7041. +/* ---- Constants and Types ---------------------------------------------- */
  7042. +
  7043. +#define VMCS_SM_RESOURCE_NAME 32
  7044. +#define VMCS_SM_RESOURCE_NAME_DEFAULT "sm-host-resource"
  7045. +
  7046. +/* Type define used to create unique IOCTL number */
  7047. +#define VMCS_SM_MAGIC_TYPE 'I'
  7048. +
  7049. +/* IOCTL commands */
  7050. +enum vmcs_sm_cmd_e {
  7051. + VMCS_SM_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
  7052. + VMCS_SM_CMD_ALLOC_SHARE,
  7053. + VMCS_SM_CMD_LOCK,
  7054. + VMCS_SM_CMD_LOCK_CACHE,
  7055. + VMCS_SM_CMD_UNLOCK,
  7056. + VMCS_SM_CMD_RESIZE,
  7057. + VMCS_SM_CMD_UNMAP,
  7058. + VMCS_SM_CMD_FREE,
  7059. + VMCS_SM_CMD_FLUSH,
  7060. + VMCS_SM_CMD_INVALID,
  7061. +
  7062. + VMCS_SM_CMD_SIZE_USR_HANDLE,
  7063. + VMCS_SM_CMD_CHK_USR_HANDLE,
  7064. +
  7065. + VMCS_SM_CMD_MAPPED_USR_HANDLE,
  7066. + VMCS_SM_CMD_MAPPED_USR_ADDRESS,
  7067. + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,
  7068. + VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,
  7069. + VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,
  7070. +
  7071. + VMCS_SM_CMD_VC_WALK_ALLOC,
  7072. + VMCS_SM_CMD_HOST_WALK_MAP,
  7073. + VMCS_SM_CMD_HOST_WALK_PID_ALLOC,
  7074. + VMCS_SM_CMD_HOST_WALK_PID_MAP,
  7075. +
  7076. + VMCS_SM_CMD_LAST /* Do no delete */
  7077. +};
  7078. +
  7079. +/* Cache type supported, conveniently matches the user space definition in
  7080. +** user-vcsm.h.
  7081. +*/
  7082. +enum vmcs_sm_cache_e {
  7083. + VMCS_SM_CACHE_NONE,
  7084. + VMCS_SM_CACHE_HOST,
  7085. + VMCS_SM_CACHE_VC,
  7086. + VMCS_SM_CACHE_BOTH,
  7087. +};
  7088. +
  7089. +/* IOCTL Data structures */
  7090. +struct vmcs_sm_ioctl_alloc {
  7091. + /* user -> kernel */
  7092. + unsigned int size;
  7093. + unsigned int num;
  7094. + enum vmcs_sm_cache_e cached;
  7095. + char name[VMCS_SM_RESOURCE_NAME];
  7096. +
  7097. + /* kernel -> user */
  7098. + unsigned int handle;
  7099. + /* unsigned int base_addr; */
  7100. +};
  7101. +
  7102. +struct vmcs_sm_ioctl_alloc_share {
  7103. + /* user -> kernel */
  7104. + unsigned int handle;
  7105. + unsigned int size;
  7106. +};
  7107. +
  7108. +struct vmcs_sm_ioctl_free {
  7109. + /* user -> kernel */
  7110. + unsigned int handle;
  7111. + /* unsigned int base_addr; */
  7112. +};
  7113. +
  7114. +struct vmcs_sm_ioctl_lock_unlock {
  7115. + /* user -> kernel */
  7116. + unsigned int handle;
  7117. +
  7118. + /* kernel -> user */
  7119. + unsigned int addr;
  7120. +};
  7121. +
  7122. +struct vmcs_sm_ioctl_lock_cache {
  7123. + /* user -> kernel */
  7124. + unsigned int handle;
  7125. + enum vmcs_sm_cache_e cached;
  7126. +};
  7127. +
  7128. +struct vmcs_sm_ioctl_resize {
  7129. + /* user -> kernel */
  7130. + unsigned int handle;
  7131. + unsigned int new_size;
  7132. +
  7133. + /* kernel -> user */
  7134. + unsigned int old_size;
  7135. +};
  7136. +
  7137. +struct vmcs_sm_ioctl_map {
  7138. + /* user -> kernel */
  7139. + /* and kernel -> user */
  7140. + unsigned int pid;
  7141. + unsigned int handle;
  7142. + unsigned int addr;
  7143. +
  7144. + /* kernel -> user */
  7145. + unsigned int size;
  7146. +};
  7147. +
  7148. +struct vmcs_sm_ioctl_walk {
  7149. + /* user -> kernel */
  7150. + unsigned int pid;
  7151. +};
  7152. +
  7153. +struct vmcs_sm_ioctl_chk {
  7154. + /* user -> kernel */
  7155. + unsigned int handle;
  7156. +
  7157. + /* kernel -> user */
  7158. + unsigned int addr;
  7159. + unsigned int size;
  7160. + enum vmcs_sm_cache_e cache;
  7161. +};
  7162. +
  7163. +struct vmcs_sm_ioctl_size {
  7164. + /* user -> kernel */
  7165. + unsigned int handle;
  7166. +
  7167. + /* kernel -> user */
  7168. + unsigned int size;
  7169. +};
  7170. +
  7171. +struct vmcs_sm_ioctl_cache {
  7172. + /* user -> kernel */
  7173. + unsigned int handle;
  7174. + unsigned int addr;
  7175. + unsigned int size;
  7176. +};
  7177. +
  7178. +/* IOCTL numbers */
  7179. +#define VMCS_SM_IOCTL_MEM_ALLOC\
  7180. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC,\
  7181. + struct vmcs_sm_ioctl_alloc)
  7182. +#define VMCS_SM_IOCTL_MEM_ALLOC_SHARE\
  7183. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_ALLOC_SHARE,\
  7184. + struct vmcs_sm_ioctl_alloc_share)
  7185. +#define VMCS_SM_IOCTL_MEM_LOCK\
  7186. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK,\
  7187. + struct vmcs_sm_ioctl_lock_unlock)
  7188. +#define VMCS_SM_IOCTL_MEM_LOCK_CACHE\
  7189. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_LOCK_CACHE,\
  7190. + struct vmcs_sm_ioctl_lock_cache)
  7191. +#define VMCS_SM_IOCTL_MEM_UNLOCK\
  7192. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_UNLOCK,\
  7193. + struct vmcs_sm_ioctl_lock_unlock)
  7194. +#define VMCS_SM_IOCTL_MEM_RESIZE\
  7195. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_RESIZE,\
  7196. + struct vmcs_sm_ioctl_resize)
  7197. +#define VMCS_SM_IOCTL_MEM_FREE\
  7198. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FREE,\
  7199. + struct vmcs_sm_ioctl_free)
  7200. +#define VMCS_SM_IOCTL_MEM_FLUSH\
  7201. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_FLUSH,\
  7202. + struct vmcs_sm_ioctl_cache)
  7203. +#define VMCS_SM_IOCTL_MEM_INVALID\
  7204. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_INVALID,\
  7205. + struct vmcs_sm_ioctl_cache)
  7206. +
  7207. +#define VMCS_SM_IOCTL_SIZE_USR_HDL\
  7208. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_SIZE_USR_HANDLE,\
  7209. + struct vmcs_sm_ioctl_size)
  7210. +#define VMCS_SM_IOCTL_CHK_USR_HDL\
  7211. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_CHK_USR_HANDLE,\
  7212. + struct vmcs_sm_ioctl_chk)
  7213. +
  7214. +#define VMCS_SM_IOCTL_MAP_USR_HDL\
  7215. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_HANDLE,\
  7216. + struct vmcs_sm_ioctl_map)
  7217. +#define VMCS_SM_IOCTL_MAP_USR_ADDRESS\
  7218. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_USR_ADDRESS,\
  7219. + struct vmcs_sm_ioctl_map)
  7220. +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_ADDR\
  7221. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR,\
  7222. + struct vmcs_sm_ioctl_map)
  7223. +#define VMCS_SM_IOCTL_MAP_VC_HDL_FR_HDL\
  7224. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL,\
  7225. + struct vmcs_sm_ioctl_map)
  7226. +#define VMCS_SM_IOCTL_MAP_VC_ADDR_FR_HDL\
  7227. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL,\
  7228. + struct vmcs_sm_ioctl_map)
  7229. +
  7230. +#define VMCS_SM_IOCTL_VC_WALK_ALLOC\
  7231. + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_VC_WALK_ALLOC)
  7232. +#define VMCS_SM_IOCTL_HOST_WALK_MAP\
  7233. + _IO(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_MAP)
  7234. +#define VMCS_SM_IOCTL_HOST_WALK_PID_ALLOC\
  7235. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_ALLOC,\
  7236. + struct vmcs_sm_ioctl_walk)
  7237. +#define VMCS_SM_IOCTL_HOST_WALK_PID_MAP\
  7238. + _IOR(VMCS_SM_MAGIC_TYPE, VMCS_SM_CMD_HOST_WALK_PID_MAP,\
  7239. + struct vmcs_sm_ioctl_walk)
  7240. +
  7241. +/* ---- Variable Externs ------------------------------------------------- */
  7242. +
  7243. +/* ---- Function Prototypes ---------------------------------------------- */
  7244. +
  7245. +#endif /* __VMCS_SM_IOCTL_H__INCLUDED__ */
  7246. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/Kconfig linux-3.16-rpi/arch/arm/mach-bcm2708/Kconfig
  7247. --- linux-3.16.2/arch/arm/mach-bcm2708/Kconfig 1970-01-01 01:00:00.000000000 +0100
  7248. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/Kconfig 2014-09-14 19:03:05.000000000 +0200
  7249. @@ -0,0 +1,52 @@
  7250. +menu "Broadcom BCM2708 Implementations"
  7251. + depends on ARCH_BCM2708
  7252. +
  7253. +config MACH_BCM2708
  7254. + bool "Broadcom BCM2708 Development Platform"
  7255. + select NEED_MACH_MEMORY_H
  7256. + select NEED_MACH_IO_H
  7257. + select CPU_V6
  7258. + help
  7259. + Include support for the Broadcom(R) BCM2708 platform.
  7260. +
  7261. +config BCM2708_DT
  7262. + bool "BCM2708 Device Tree support"
  7263. + depends on MACH_BCM2708
  7264. + default n
  7265. + select USE_OF
  7266. + select PINCTRL
  7267. + select PINCTRL_BCM2708
  7268. + select BCM2708_GPIO
  7269. + help
  7270. + Enable Device Tree support for BCM2708
  7271. +
  7272. +config BCM2708_GPIO
  7273. + bool "BCM2708 gpio support"
  7274. + depends on MACH_BCM2708
  7275. + select ARCH_REQUIRE_GPIOLIB
  7276. + default y
  7277. + help
  7278. + Include support for the Broadcom(R) BCM2708 gpio.
  7279. +
  7280. +config BCM2708_VCMEM
  7281. + bool "Videocore Memory"
  7282. + depends on MACH_BCM2708
  7283. + default y
  7284. + help
  7285. + Helper for videocore memory access and total size allocation.
  7286. +
  7287. +config BCM2708_NOL2CACHE
  7288. + bool "Videocore L2 cache disable"
  7289. + depends on MACH_BCM2708
  7290. + default n
  7291. + help
  7292. + Do not allow ARM to use GPU's L2 cache. Requires disable_l2cache in config.txt.
  7293. +
  7294. +config BCM2708_SPIDEV
  7295. + bool "Bind spidev to SPI0 master"
  7296. + depends on MACH_BCM2708 && !USE_OF
  7297. + depends on SPI
  7298. + default y
  7299. + help
  7300. + Binds spidev driver to the SPI0 master
  7301. +endmenu
  7302. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/Makefile linux-3.16-rpi/arch/arm/mach-bcm2708/Makefile
  7303. --- linux-3.16.2/arch/arm/mach-bcm2708/Makefile 1970-01-01 01:00:00.000000000 +0100
  7304. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/Makefile 2014-09-14 19:03:05.000000000 +0200
  7305. @@ -0,0 +1,7 @@
  7306. +#
  7307. +# Makefile for the linux kernel.
  7308. +#
  7309. +
  7310. +obj-$(CONFIG_MACH_BCM2708) += bcm2708.o armctrl.o vcio.o power.o dma.o
  7311. +obj-$(CONFIG_BCM2708_GPIO) += bcm2708_gpio.o
  7312. +obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
  7313. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/Makefile.boot linux-3.16-rpi/arch/arm/mach-bcm2708/Makefile.boot
  7314. --- linux-3.16.2/arch/arm/mach-bcm2708/Makefile.boot 1970-01-01 01:00:00.000000000 +0100
  7315. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/Makefile.boot 2014-04-13 17:32:40.000000000 +0200
  7316. @@ -0,0 +1,3 @@
  7317. + zreladdr-y := 0x00008000
  7318. +params_phys-y := 0x00000100
  7319. +initrd_phys-y := 0x00800000
  7320. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/power.c linux-3.16-rpi/arch/arm/mach-bcm2708/power.c
  7321. --- linux-3.16.2/arch/arm/mach-bcm2708/power.c 1970-01-01 01:00:00.000000000 +0100
  7322. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/power.c 2014-04-13 17:32:40.000000000 +0200
  7323. @@ -0,0 +1,194 @@
  7324. +/*
  7325. + * linux/arch/arm/mach-bcm2708/power.c
  7326. + *
  7327. + * Copyright (C) 2010 Broadcom
  7328. + *
  7329. + * This program is free software; you can redistribute it and/or modify
  7330. + * it under the terms of the GNU General Public License version 2 as
  7331. + * published by the Free Software Foundation.
  7332. + *
  7333. + * This device provides a shared mechanism for controlling the power to
  7334. + * VideoCore subsystems.
  7335. + */
  7336. +
  7337. +#include <linux/module.h>
  7338. +#include <linux/semaphore.h>
  7339. +#include <linux/bug.h>
  7340. +#include <mach/power.h>
  7341. +#include <mach/vcio.h>
  7342. +#include <mach/arm_power.h>
  7343. +
  7344. +#define DRIVER_NAME "bcm2708_power"
  7345. +
  7346. +#define BCM_POWER_MAXCLIENTS 4
  7347. +#define BCM_POWER_NOCLIENT (1<<31)
  7348. +
  7349. +/* Some drivers expect there devices to be permanently powered */
  7350. +#define BCM_POWER_ALWAYS_ON (BCM_POWER_USB)
  7351. +
  7352. +#if 1
  7353. +#define DPRINTK printk
  7354. +#else
  7355. +#define DPRINTK if (0) printk
  7356. +#endif
  7357. +
  7358. +struct state_struct {
  7359. + uint32_t global_request;
  7360. + uint32_t client_request[BCM_POWER_MAXCLIENTS];
  7361. + struct semaphore client_mutex;
  7362. + struct semaphore mutex;
  7363. +} g_state;
  7364. +
  7365. +int bcm_power_open(BCM_POWER_HANDLE_T *handle)
  7366. +{
  7367. + BCM_POWER_HANDLE_T i;
  7368. + int ret = -EBUSY;
  7369. +
  7370. + down(&g_state.client_mutex);
  7371. +
  7372. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  7373. + if (g_state.client_request[i] == BCM_POWER_NOCLIENT) {
  7374. + g_state.client_request[i] = BCM_POWER_NONE;
  7375. + *handle = i;
  7376. + ret = 0;
  7377. + break;
  7378. + }
  7379. + }
  7380. +
  7381. + up(&g_state.client_mutex);
  7382. +
  7383. + DPRINTK("bcm_power_open() -> %d\n", *handle);
  7384. +
  7385. + return ret;
  7386. +}
  7387. +EXPORT_SYMBOL_GPL(bcm_power_open);
  7388. +
  7389. +int bcm_power_request(BCM_POWER_HANDLE_T handle, uint32_t request)
  7390. +{
  7391. + int rc = 0;
  7392. +
  7393. + DPRINTK("bcm_power_request(%d, %x)\n", handle, request);
  7394. +
  7395. + if ((handle < BCM_POWER_MAXCLIENTS) &&
  7396. + (g_state.client_request[handle] != BCM_POWER_NOCLIENT)) {
  7397. + if (down_interruptible(&g_state.mutex) != 0) {
  7398. + DPRINTK("bcm_power_request -> interrupted\n");
  7399. + return -EINTR;
  7400. + }
  7401. +
  7402. + if (request != g_state.client_request[handle]) {
  7403. + uint32_t others_request = 0;
  7404. + uint32_t global_request;
  7405. + BCM_POWER_HANDLE_T i;
  7406. +
  7407. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++) {
  7408. + if (i != handle)
  7409. + others_request |=
  7410. + g_state.client_request[i];
  7411. + }
  7412. + others_request &= ~BCM_POWER_NOCLIENT;
  7413. +
  7414. + global_request = request | others_request;
  7415. + if (global_request != g_state.global_request) {
  7416. + uint32_t actual;
  7417. +
  7418. + /* Send a request to VideoCore */
  7419. + bcm_mailbox_write(MBOX_CHAN_POWER,
  7420. + global_request << 4);
  7421. +
  7422. + /* Wait for a response during power-up */
  7423. + if (global_request & ~g_state.global_request) {
  7424. + rc = bcm_mailbox_read(MBOX_CHAN_POWER,
  7425. + &actual);
  7426. + DPRINTK
  7427. + ("bcm_mailbox_read -> %08x, %d\n",
  7428. + actual, rc);
  7429. + actual >>= 4;
  7430. + } else {
  7431. + rc = 0;
  7432. + actual = global_request;
  7433. + }
  7434. +
  7435. + if (rc == 0) {
  7436. + if (actual != global_request) {
  7437. + printk(KERN_ERR
  7438. + "%s: prev global %x, new global %x, actual %x, request %x, others_request %x\n",
  7439. + __func__,
  7440. + g_state.global_request,
  7441. + global_request, actual, request, others_request);
  7442. + /* A failure */
  7443. + BUG_ON((others_request & actual)
  7444. + != others_request);
  7445. + request &= actual;
  7446. + rc = -EIO;
  7447. + }
  7448. +
  7449. + g_state.global_request = actual;
  7450. + g_state.client_request[handle] =
  7451. + request;
  7452. + }
  7453. + }
  7454. + }
  7455. + up(&g_state.mutex);
  7456. + } else {
  7457. + rc = -EINVAL;
  7458. + }
  7459. + DPRINTK("bcm_power_request -> %d\n", rc);
  7460. + return rc;
  7461. +}
  7462. +EXPORT_SYMBOL_GPL(bcm_power_request);
  7463. +
  7464. +int bcm_power_close(BCM_POWER_HANDLE_T handle)
  7465. +{
  7466. + int rc;
  7467. +
  7468. + DPRINTK("bcm_power_close(%d)\n", handle);
  7469. +
  7470. + rc = bcm_power_request(handle, BCM_POWER_NONE);
  7471. + if (rc == 0)
  7472. + g_state.client_request[handle] = BCM_POWER_NOCLIENT;
  7473. +
  7474. + return rc;
  7475. +}
  7476. +EXPORT_SYMBOL_GPL(bcm_power_close);
  7477. +
  7478. +static int __init bcm_power_init(void)
  7479. +{
  7480. +#if defined(BCM_POWER_ALWAYS_ON)
  7481. + BCM_POWER_HANDLE_T always_on_handle;
  7482. +#endif
  7483. + int rc = 0;
  7484. + int i;
  7485. +
  7486. + printk(KERN_INFO "bcm_power: Broadcom power driver\n");
  7487. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  7488. +
  7489. + for (i = 0; i < BCM_POWER_MAXCLIENTS; i++)
  7490. + g_state.client_request[i] = BCM_POWER_NOCLIENT;
  7491. +
  7492. + sema_init(&g_state.client_mutex, 1);
  7493. + sema_init(&g_state.mutex, 1);
  7494. +
  7495. + g_state.global_request = 0;
  7496. +
  7497. +#if defined(BCM_POWER_ALWAYS_ON)
  7498. + if (BCM_POWER_ALWAYS_ON) {
  7499. + bcm_power_open(&always_on_handle);
  7500. + bcm_power_request(always_on_handle, BCM_POWER_ALWAYS_ON);
  7501. + }
  7502. +#endif
  7503. +
  7504. + return rc;
  7505. +}
  7506. +
  7507. +static void __exit bcm_power_exit(void)
  7508. +{
  7509. + bcm_mailbox_write(MBOX_CHAN_POWER, 0);
  7510. +}
  7511. +
  7512. +arch_initcall(bcm_power_init); /* Initialize early */
  7513. +module_exit(bcm_power_exit);
  7514. +
  7515. +MODULE_AUTHOR("Phil Elwell");
  7516. +MODULE_DESCRIPTION("Interface to BCM2708 power management");
  7517. +MODULE_LICENSE("GPL");
  7518. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/vcio.c linux-3.16-rpi/arch/arm/mach-bcm2708/vcio.c
  7519. --- linux-3.16.2/arch/arm/mach-bcm2708/vcio.c 1970-01-01 01:00:00.000000000 +0100
  7520. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/vcio.c 2014-09-14 19:03:05.000000000 +0200
  7521. @@ -0,0 +1,474 @@
  7522. +/*
  7523. + * linux/arch/arm/mach-bcm2708/vcio.c
  7524. + *
  7525. + * Copyright (C) 2010 Broadcom
  7526. + *
  7527. + * This program is free software; you can redistribute it and/or modify
  7528. + * it under the terms of the GNU General Public License version 2 as
  7529. + * published by the Free Software Foundation.
  7530. + *
  7531. + * This device provides a shared mechanism for writing to the mailboxes,
  7532. + * semaphores, doorbells etc. that are shared between the ARM and the
  7533. + * VideoCore processor
  7534. + */
  7535. +
  7536. +#if defined(CONFIG_SERIAL_BCM_MBOX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  7537. +#define SUPPORT_SYSRQ
  7538. +#endif
  7539. +
  7540. +#include <linux/module.h>
  7541. +#include <linux/console.h>
  7542. +#include <linux/serial_core.h>
  7543. +#include <linux/serial.h>
  7544. +#include <linux/errno.h>
  7545. +#include <linux/device.h>
  7546. +#include <linux/init.h>
  7547. +#include <linux/mm.h>
  7548. +#include <linux/dma-mapping.h>
  7549. +#include <linux/platform_device.h>
  7550. +#include <linux/sysrq.h>
  7551. +#include <linux/delay.h>
  7552. +#include <linux/slab.h>
  7553. +#include <linux/interrupt.h>
  7554. +#include <linux/irq.h>
  7555. +
  7556. +#include <linux/io.h>
  7557. +
  7558. +#include <mach/vcio.h>
  7559. +#include <mach/platform.h>
  7560. +
  7561. +#include <asm/uaccess.h>
  7562. +
  7563. +
  7564. +#define DRIVER_NAME BCM_VCIO_DRIVER_NAME
  7565. +
  7566. +/* ----------------------------------------------------------------------
  7567. + * Mailbox
  7568. + * -------------------------------------------------------------------- */
  7569. +
  7570. +/* offsets from a mail box base address */
  7571. +#define MAIL_WRT 0x00 /* write - and next 4 words */
  7572. +#define MAIL_RD 0x00 /* read - and next 4 words */
  7573. +#define MAIL_POL 0x10 /* read without popping the fifo */
  7574. +#define MAIL_SND 0x14 /* sender ID (bottom two bits) */
  7575. +#define MAIL_STA 0x18 /* status */
  7576. +#define MAIL_CNF 0x1C /* configuration */
  7577. +
  7578. +#define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
  7579. +#define MBOX_MSG_LSB(chan, data28) (((data28) << 4) | ((chan) & 0xf))
  7580. +#define MBOX_CHAN(msg) ((msg) & 0xf)
  7581. +#define MBOX_DATA28(msg) ((msg) & ~0xf)
  7582. +#define MBOX_DATA28_LSB(msg) (((uint32_t)msg) >> 4)
  7583. +
  7584. +#define MBOX_MAGIC 0xd0d0c0de
  7585. +
  7586. +struct vc_mailbox {
  7587. + struct device *dev; /* parent device */
  7588. + void __iomem *status;
  7589. + void __iomem *config;
  7590. + void __iomem *read;
  7591. + void __iomem *write;
  7592. + uint32_t msg[MBOX_CHAN_COUNT];
  7593. + struct semaphore sema[MBOX_CHAN_COUNT];
  7594. + uint32_t magic;
  7595. +};
  7596. +
  7597. +static void mbox_init(struct vc_mailbox *mbox_out, struct device *dev,
  7598. + uint32_t addr_mbox)
  7599. +{
  7600. + int i;
  7601. +
  7602. + mbox_out->dev = dev;
  7603. + mbox_out->status = __io_address(addr_mbox + MAIL_STA);
  7604. + mbox_out->config = __io_address(addr_mbox + MAIL_CNF);
  7605. + mbox_out->read = __io_address(addr_mbox + MAIL_RD);
  7606. + /* Write to the other mailbox */
  7607. + mbox_out->write =
  7608. + __io_address((addr_mbox ^ ARM_0_MAIL0_WRT ^ ARM_0_MAIL1_WRT) +
  7609. + MAIL_WRT);
  7610. +
  7611. + for (i = 0; i < MBOX_CHAN_COUNT; i++) {
  7612. + mbox_out->msg[i] = 0;
  7613. + sema_init(&mbox_out->sema[i], 0);
  7614. + }
  7615. +
  7616. + /* Enable the interrupt on data reception */
  7617. + writel(ARM_MC_IHAVEDATAIRQEN, mbox_out->config);
  7618. +
  7619. + mbox_out->magic = MBOX_MAGIC;
  7620. +}
  7621. +
  7622. +static int mbox_write(struct vc_mailbox *mbox, unsigned chan, uint32_t data28)
  7623. +{
  7624. + int rc;
  7625. +
  7626. + if (mbox->magic != MBOX_MAGIC)
  7627. + rc = -EINVAL;
  7628. + else {
  7629. + /* wait for the mailbox FIFO to have some space in it */
  7630. + while (0 != (readl(mbox->status) & ARM_MS_FULL))
  7631. + cpu_relax();
  7632. +
  7633. + writel(MBOX_MSG(chan, data28), mbox->write);
  7634. + rc = 0;
  7635. + }
  7636. + return rc;
  7637. +}
  7638. +
  7639. +static int mbox_read(struct vc_mailbox *mbox, unsigned chan, uint32_t *data28)
  7640. +{
  7641. + int rc;
  7642. +
  7643. + if (mbox->magic != MBOX_MAGIC)
  7644. + rc = -EINVAL;
  7645. + else {
  7646. + down(&mbox->sema[chan]);
  7647. + *data28 = MBOX_DATA28(mbox->msg[chan]);
  7648. + mbox->msg[chan] = 0;
  7649. + rc = 0;
  7650. + }
  7651. + return rc;
  7652. +}
  7653. +
  7654. +static irqreturn_t mbox_irq(int irq, void *dev_id)
  7655. +{
  7656. + /* wait for the mailbox FIFO to have some data in it */
  7657. + struct vc_mailbox *mbox = (struct vc_mailbox *) dev_id;
  7658. + int status = readl(mbox->status);
  7659. + int ret = IRQ_NONE;
  7660. +
  7661. + while (!(status & ARM_MS_EMPTY)) {
  7662. + uint32_t msg = readl(mbox->read);
  7663. + int chan = MBOX_CHAN(msg);
  7664. + if (chan < MBOX_CHAN_COUNT) {
  7665. + if (mbox->msg[chan]) {
  7666. + /* Overflow */
  7667. + printk(KERN_ERR DRIVER_NAME
  7668. + ": mbox chan %d overflow - drop %08x\n",
  7669. + chan, msg);
  7670. + } else {
  7671. + mbox->msg[chan] = (msg | 0xf);
  7672. + up(&mbox->sema[chan]);
  7673. + }
  7674. + } else {
  7675. + printk(KERN_ERR DRIVER_NAME
  7676. + ": invalid channel selector (msg %08x)\n", msg);
  7677. + }
  7678. + ret = IRQ_HANDLED;
  7679. + status = readl(mbox->status);
  7680. + }
  7681. + return ret;
  7682. +}
  7683. +
  7684. +static struct irqaction mbox_irqaction = {
  7685. + .name = "ARM Mailbox IRQ",
  7686. + .flags = IRQF_DISABLED | IRQF_IRQPOLL,
  7687. + .handler = mbox_irq,
  7688. +};
  7689. +
  7690. +/* ----------------------------------------------------------------------
  7691. + * Mailbox Methods
  7692. + * -------------------------------------------------------------------- */
  7693. +
  7694. +static struct device *mbox_dev; /* we assume there's only one! */
  7695. +
  7696. +static int dev_mbox_write(struct device *dev, unsigned chan, uint32_t data28)
  7697. +{
  7698. + int rc;
  7699. +
  7700. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  7701. + device_lock(dev);
  7702. + rc = mbox_write(mailbox, chan, data28);
  7703. + device_unlock(dev);
  7704. +
  7705. + return rc;
  7706. +}
  7707. +
  7708. +static int dev_mbox_read(struct device *dev, unsigned chan, uint32_t *data28)
  7709. +{
  7710. + int rc;
  7711. +
  7712. + struct vc_mailbox *mailbox = dev_get_drvdata(dev);
  7713. + device_lock(dev);
  7714. + rc = mbox_read(mailbox, chan, data28);
  7715. + device_unlock(dev);
  7716. +
  7717. + return rc;
  7718. +}
  7719. +
  7720. +extern int bcm_mailbox_write(unsigned chan, uint32_t data28)
  7721. +{
  7722. + if (mbox_dev)
  7723. + return dev_mbox_write(mbox_dev, chan, data28);
  7724. + else
  7725. + return -ENODEV;
  7726. +}
  7727. +EXPORT_SYMBOL_GPL(bcm_mailbox_write);
  7728. +
  7729. +extern int bcm_mailbox_read(unsigned chan, uint32_t *data28)
  7730. +{
  7731. + if (mbox_dev)
  7732. + return dev_mbox_read(mbox_dev, chan, data28);
  7733. + else
  7734. + return -ENODEV;
  7735. +}
  7736. +EXPORT_SYMBOL_GPL(bcm_mailbox_read);
  7737. +
  7738. +static void dev_mbox_register(const char *dev_name, struct device *dev)
  7739. +{
  7740. + mbox_dev = dev;
  7741. +}
  7742. +
  7743. +static int mbox_copy_from_user(void *dst, const void *src, int size)
  7744. +{
  7745. + if ( (uint32_t)src < TASK_SIZE)
  7746. + {
  7747. + return copy_from_user(dst, src, size);
  7748. + }
  7749. + else
  7750. + {
  7751. + memcpy( dst, src, size );
  7752. + return 0;
  7753. + }
  7754. +}
  7755. +
  7756. +static int mbox_copy_to_user(void *dst, const void *src, int size)
  7757. +{
  7758. + if ( (uint32_t)dst < TASK_SIZE)
  7759. + {
  7760. + return copy_to_user(dst, src, size);
  7761. + }
  7762. + else
  7763. + {
  7764. + memcpy( dst, src, size );
  7765. + return 0;
  7766. + }
  7767. +}
  7768. +
  7769. +static DEFINE_MUTEX(mailbox_lock);
  7770. +extern int bcm_mailbox_property(void *data, int size)
  7771. +{
  7772. + uint32_t success;
  7773. + dma_addr_t mem_bus; /* the memory address accessed from videocore */
  7774. + void *mem_kern; /* the memory address accessed from driver */
  7775. + int s = 0;
  7776. +
  7777. + mutex_lock(&mailbox_lock);
  7778. + /* allocate some memory for the messages communicating with GPU */
  7779. + mem_kern = dma_alloc_coherent(NULL, PAGE_ALIGN(size), &mem_bus, GFP_ATOMIC);
  7780. + if (mem_kern) {
  7781. + /* create the message */
  7782. + mbox_copy_from_user(mem_kern, data, size);
  7783. +
  7784. + /* send the message */
  7785. + wmb();
  7786. + s = bcm_mailbox_write(MBOX_CHAN_PROPERTY, (uint32_t)mem_bus);
  7787. + if (s == 0) {
  7788. + s = bcm_mailbox_read(MBOX_CHAN_PROPERTY, &success);
  7789. + }
  7790. + if (s == 0) {
  7791. + /* copy the response */
  7792. + rmb();
  7793. + mbox_copy_to_user(data, mem_kern, size);
  7794. + }
  7795. + dma_free_coherent(NULL, PAGE_ALIGN(size), mem_kern, mem_bus);
  7796. + } else {
  7797. + s = -ENOMEM;
  7798. + }
  7799. + if (s != 0)
  7800. + printk(KERN_ERR DRIVER_NAME ": %s failed (%d)\n", __func__, s);
  7801. +
  7802. + mutex_unlock(&mailbox_lock);
  7803. + return s;
  7804. +}
  7805. +EXPORT_SYMBOL_GPL(bcm_mailbox_property);
  7806. +
  7807. +/* ----------------------------------------------------------------------
  7808. + * Platform Device for Mailbox
  7809. + * -------------------------------------------------------------------- */
  7810. +
  7811. +/*
  7812. + * Is the device open right now? Used to prevent
  7813. + * concurent access into the same device
  7814. + */
  7815. +static int Device_Open = 0;
  7816. +
  7817. +/*
  7818. + * This is called whenever a process attempts to open the device file
  7819. + */
  7820. +static int device_open(struct inode *inode, struct file *file)
  7821. +{
  7822. + /*
  7823. + * We don't want to talk to two processes at the same time
  7824. + */
  7825. + if (Device_Open)
  7826. + return -EBUSY;
  7827. +
  7828. + Device_Open++;
  7829. + /*
  7830. + * Initialize the message
  7831. + */
  7832. + try_module_get(THIS_MODULE);
  7833. + return 0;
  7834. +}
  7835. +
  7836. +static int device_release(struct inode *inode, struct file *file)
  7837. +{
  7838. + /*
  7839. + * We're now ready for our next caller
  7840. + */
  7841. + Device_Open--;
  7842. +
  7843. + module_put(THIS_MODULE);
  7844. + return 0;
  7845. +}
  7846. +
  7847. +/*
  7848. + * This function is called whenever a process tries to do an ioctl on our
  7849. + * device file. We get two extra parameters (additional to the inode and file
  7850. + * structures, which all device functions get): the number of the ioctl called
  7851. + * and the parameter given to the ioctl function.
  7852. + *
  7853. + * If the ioctl is write or read/write (meaning output is returned to the
  7854. + * calling process), the ioctl call returns the output of this function.
  7855. + *
  7856. + */
  7857. +static long device_ioctl(struct file *file, /* see include/linux/fs.h */
  7858. + unsigned int ioctl_num, /* number and param for ioctl */
  7859. + unsigned long ioctl_param)
  7860. +{
  7861. + unsigned size;
  7862. + /*
  7863. + * Switch according to the ioctl called
  7864. + */
  7865. + switch (ioctl_num) {
  7866. + case IOCTL_MBOX_PROPERTY:
  7867. + /*
  7868. + * Receive a pointer to a message (in user space) and set that
  7869. + * to be the device's message. Get the parameter given to
  7870. + * ioctl by the process.
  7871. + */
  7872. + mbox_copy_from_user(&size, (void *)ioctl_param, sizeof size);
  7873. + return bcm_mailbox_property((void *)ioctl_param, size);
  7874. + break;
  7875. + default:
  7876. + printk(KERN_ERR DRIVER_NAME "unknown ioctl: %d\n", ioctl_num);
  7877. + return -EINVAL;
  7878. + }
  7879. +
  7880. + return 0;
  7881. +}
  7882. +
  7883. +/* Module Declarations */
  7884. +
  7885. +/*
  7886. + * This structure will hold the functions to be called
  7887. + * when a process does something to the device we
  7888. + * created. Since a pointer to this structure is kept in
  7889. + * the devices table, it can't be local to
  7890. + * init_module. NULL is for unimplemented functios.
  7891. + */
  7892. +struct file_operations fops = {
  7893. + .unlocked_ioctl = device_ioctl,
  7894. + .open = device_open,
  7895. + .release = device_release, /* a.k.a. close */
  7896. +};
  7897. +
  7898. +static int bcm_vcio_probe(struct platform_device *pdev)
  7899. +{
  7900. + int ret = 0;
  7901. + struct vc_mailbox *mailbox;
  7902. +
  7903. + mailbox = kzalloc(sizeof(*mailbox), GFP_KERNEL);
  7904. + if (NULL == mailbox) {
  7905. + printk(KERN_ERR DRIVER_NAME ": failed to allocate "
  7906. + "mailbox memory\n");
  7907. + ret = -ENOMEM;
  7908. + } else {
  7909. + struct resource *res;
  7910. +
  7911. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  7912. + if (res == NULL) {
  7913. + printk(KERN_ERR DRIVER_NAME ": failed to obtain memory "
  7914. + "resource\n");
  7915. + ret = -ENODEV;
  7916. + kfree(mailbox);
  7917. + } else {
  7918. + /* should be based on the registers from res really */
  7919. + mbox_init(mailbox, &pdev->dev, ARM_0_MAIL0_RD);
  7920. +
  7921. + platform_set_drvdata(pdev, mailbox);
  7922. + dev_mbox_register(DRIVER_NAME, &pdev->dev);
  7923. +
  7924. + mbox_irqaction.dev_id = mailbox;
  7925. + setup_irq(IRQ_ARM_MAILBOX, &mbox_irqaction);
  7926. + printk(KERN_INFO DRIVER_NAME ": mailbox at %p\n",
  7927. + __io_address(ARM_0_MAIL0_RD));
  7928. + }
  7929. + }
  7930. +
  7931. + if (ret == 0) {
  7932. + /*
  7933. + * Register the character device
  7934. + */
  7935. + ret = register_chrdev(MAJOR_NUM, DEVICE_FILE_NAME, &fops);
  7936. +
  7937. + /*
  7938. + * Negative values signify an error
  7939. + */
  7940. + if (ret < 0) {
  7941. + printk(KERN_ERR DRIVER_NAME
  7942. + "Failed registering the character device %d\n", ret);
  7943. + return ret;
  7944. + }
  7945. + }
  7946. + return ret;
  7947. +}
  7948. +
  7949. +static int bcm_vcio_remove(struct platform_device *pdev)
  7950. +{
  7951. + struct vc_mailbox *mailbox = platform_get_drvdata(pdev);
  7952. +
  7953. + platform_set_drvdata(pdev, NULL);
  7954. + kfree(mailbox);
  7955. +
  7956. + return 0;
  7957. +}
  7958. +
  7959. +static struct platform_driver bcm_mbox_driver = {
  7960. + .probe = bcm_vcio_probe,
  7961. + .remove = bcm_vcio_remove,
  7962. +
  7963. + .driver = {
  7964. + .name = DRIVER_NAME,
  7965. + .owner = THIS_MODULE,
  7966. + },
  7967. +};
  7968. +
  7969. +static int __init bcm_mbox_init(void)
  7970. +{
  7971. + int ret;
  7972. +
  7973. + printk(KERN_INFO "mailbox: Broadcom VideoCore Mailbox driver\n");
  7974. +
  7975. + ret = platform_driver_register(&bcm_mbox_driver);
  7976. + if (ret != 0) {
  7977. + printk(KERN_ERR DRIVER_NAME ": failed to register "
  7978. + "on platform\n");
  7979. + }
  7980. +
  7981. + return ret;
  7982. +}
  7983. +
  7984. +static void __exit bcm_mbox_exit(void)
  7985. +{
  7986. + platform_driver_unregister(&bcm_mbox_driver);
  7987. +}
  7988. +
  7989. +arch_initcall(bcm_mbox_init); /* Initialize early */
  7990. +module_exit(bcm_mbox_exit);
  7991. +
  7992. +MODULE_AUTHOR("Gray Girling");
  7993. +MODULE_DESCRIPTION("ARM I/O to VideoCore processor");
  7994. +MODULE_LICENSE("GPL");
  7995. +MODULE_ALIAS("platform:bcm-mbox");
  7996. diff -Nur linux-3.16.2/arch/arm/mach-bcm2708/vc_mem.c linux-3.16-rpi/arch/arm/mach-bcm2708/vc_mem.c
  7997. --- linux-3.16.2/arch/arm/mach-bcm2708/vc_mem.c 1970-01-01 01:00:00.000000000 +0100
  7998. +++ linux-3.16-rpi/arch/arm/mach-bcm2708/vc_mem.c 2014-04-13 17:32:40.000000000 +0200
  7999. @@ -0,0 +1,432 @@
  8000. +/*****************************************************************************
  8001. +* Copyright 2010 - 2011 Broadcom Corporation. All rights reserved.
  8002. +*
  8003. +* Unless you and Broadcom execute a separate written software license
  8004. +* agreement governing use of this software, this software is licensed to you
  8005. +* under the terms of the GNU General Public License version 2, available at
  8006. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  8007. +*
  8008. +* Notwithstanding the above, under no circumstances may you combine this
  8009. +* software in any way with any other Broadcom software provided under a
  8010. +* license other than the GPL, without Broadcom's express prior written
  8011. +* consent.
  8012. +*****************************************************************************/
  8013. +
  8014. +#include <linux/kernel.h>
  8015. +#include <linux/module.h>
  8016. +#include <linux/fs.h>
  8017. +#include <linux/device.h>
  8018. +#include <linux/cdev.h>
  8019. +#include <linux/mm.h>
  8020. +#include <linux/slab.h>
  8021. +#include <linux/debugfs.h>
  8022. +#include <asm/uaccess.h>
  8023. +#include <linux/dma-mapping.h>
  8024. +
  8025. +#ifdef CONFIG_ARCH_KONA
  8026. +#include <chal/chal_ipc.h>
  8027. +#elif CONFIG_ARCH_BCM2708
  8028. +#else
  8029. +#include <csp/chal_ipc.h>
  8030. +#endif
  8031. +
  8032. +#include "mach/vc_mem.h"
  8033. +#include <mach/vcio.h>
  8034. +
  8035. +#define DRIVER_NAME "vc-mem"
  8036. +
  8037. +// Device (/dev) related variables
  8038. +static dev_t vc_mem_devnum = 0;
  8039. +static struct class *vc_mem_class = NULL;
  8040. +static struct cdev vc_mem_cdev;
  8041. +static int vc_mem_inited = 0;
  8042. +
  8043. +#ifdef CONFIG_DEBUG_FS
  8044. +static struct dentry *vc_mem_debugfs_entry;
  8045. +#endif
  8046. +
  8047. +/*
  8048. + * Videocore memory addresses and size
  8049. + *
  8050. + * Drivers that wish to know the videocore memory addresses and sizes should
  8051. + * use these variables instead of the MM_IO_BASE and MM_ADDR_IO defines in
  8052. + * headers. This allows the other drivers to not be tied down to a a certain
  8053. + * address/size at compile time.
  8054. + *
  8055. + * In the future, the goal is to have the videocore memory virtual address and
  8056. + * size be calculated at boot time rather than at compile time. The decision of
  8057. + * where the videocore memory resides and its size would be in the hands of the
  8058. + * bootloader (and/or kernel). When that happens, the values of these variables
  8059. + * would be calculated and assigned in the init function.
  8060. + */
  8061. +// in the 2835 VC in mapped above ARM, but ARM has full access to VC space
  8062. +unsigned long mm_vc_mem_phys_addr = 0x00000000;
  8063. +unsigned int mm_vc_mem_size = 0;
  8064. +unsigned int mm_vc_mem_base = 0;
  8065. +
  8066. +EXPORT_SYMBOL(mm_vc_mem_phys_addr);
  8067. +EXPORT_SYMBOL(mm_vc_mem_size);
  8068. +EXPORT_SYMBOL(mm_vc_mem_base);
  8069. +
  8070. +static uint phys_addr = 0;
  8071. +static uint mem_size = 0;
  8072. +static uint mem_base = 0;
  8073. +
  8074. +
  8075. +/****************************************************************************
  8076. +*
  8077. +* vc_mem_open
  8078. +*
  8079. +***************************************************************************/
  8080. +
  8081. +static int
  8082. +vc_mem_open(struct inode *inode, struct file *file)
  8083. +{
  8084. + (void) inode;
  8085. + (void) file;
  8086. +
  8087. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  8088. +
  8089. + return 0;
  8090. +}
  8091. +
  8092. +/****************************************************************************
  8093. +*
  8094. +* vc_mem_release
  8095. +*
  8096. +***************************************************************************/
  8097. +
  8098. +static int
  8099. +vc_mem_release(struct inode *inode, struct file *file)
  8100. +{
  8101. + (void) inode;
  8102. + (void) file;
  8103. +
  8104. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  8105. +
  8106. + return 0;
  8107. +}
  8108. +
  8109. +/****************************************************************************
  8110. +*
  8111. +* vc_mem_get_size
  8112. +*
  8113. +***************************************************************************/
  8114. +
  8115. +static void
  8116. +vc_mem_get_size(void)
  8117. +{
  8118. +}
  8119. +
  8120. +/****************************************************************************
  8121. +*
  8122. +* vc_mem_get_base
  8123. +*
  8124. +***************************************************************************/
  8125. +
  8126. +static void
  8127. +vc_mem_get_base(void)
  8128. +{
  8129. +}
  8130. +
  8131. +/****************************************************************************
  8132. +*
  8133. +* vc_mem_get_current_size
  8134. +*
  8135. +***************************************************************************/
  8136. +
  8137. +int
  8138. +vc_mem_get_current_size(void)
  8139. +{
  8140. + return mm_vc_mem_size;
  8141. +}
  8142. +
  8143. +EXPORT_SYMBOL_GPL(vc_mem_get_current_size);
  8144. +
  8145. +/****************************************************************************
  8146. +*
  8147. +* vc_mem_ioctl
  8148. +*
  8149. +***************************************************************************/
  8150. +
  8151. +static long
  8152. +vc_mem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  8153. +{
  8154. + int rc = 0;
  8155. +
  8156. + (void) cmd;
  8157. + (void) arg;
  8158. +
  8159. + pr_debug("%s: called file = 0x%p\n", __func__, file);
  8160. +
  8161. + switch (cmd) {
  8162. + case VC_MEM_IOC_MEM_PHYS_ADDR:
  8163. + {
  8164. + pr_debug("%s: VC_MEM_IOC_MEM_PHYS_ADDR=0x%p\n",
  8165. + __func__, (void *) mm_vc_mem_phys_addr);
  8166. +
  8167. + if (copy_to_user((void *) arg, &mm_vc_mem_phys_addr,
  8168. + sizeof (mm_vc_mem_phys_addr)) != 0) {
  8169. + rc = -EFAULT;
  8170. + }
  8171. + break;
  8172. + }
  8173. + case VC_MEM_IOC_MEM_SIZE:
  8174. + {
  8175. + // Get the videocore memory size first
  8176. + vc_mem_get_size();
  8177. +
  8178. + pr_debug("%s: VC_MEM_IOC_MEM_SIZE=%u\n", __func__,
  8179. + mm_vc_mem_size);
  8180. +
  8181. + if (copy_to_user((void *) arg, &mm_vc_mem_size,
  8182. + sizeof (mm_vc_mem_size)) != 0) {
  8183. + rc = -EFAULT;
  8184. + }
  8185. + break;
  8186. + }
  8187. + case VC_MEM_IOC_MEM_BASE:
  8188. + {
  8189. + // Get the videocore memory base
  8190. + vc_mem_get_base();
  8191. +
  8192. + pr_debug("%s: VC_MEM_IOC_MEM_BASE=%u\n", __func__,
  8193. + mm_vc_mem_base);
  8194. +
  8195. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  8196. + sizeof (mm_vc_mem_base)) != 0) {
  8197. + rc = -EFAULT;
  8198. + }
  8199. + break;
  8200. + }
  8201. + case VC_MEM_IOC_MEM_LOAD:
  8202. + {
  8203. + // Get the videocore memory base
  8204. + vc_mem_get_base();
  8205. +
  8206. + pr_debug("%s: VC_MEM_IOC_MEM_LOAD=%u\n", __func__,
  8207. + mm_vc_mem_base);
  8208. +
  8209. + if (copy_to_user((void *) arg, &mm_vc_mem_base,
  8210. + sizeof (mm_vc_mem_base)) != 0) {
  8211. + rc = -EFAULT;
  8212. + }
  8213. + break;
  8214. + }
  8215. + default:
  8216. + {
  8217. + return -ENOTTY;
  8218. + }
  8219. + }
  8220. + pr_debug("%s: file = 0x%p returning %d\n", __func__, file, rc);
  8221. +
  8222. + return rc;
  8223. +}
  8224. +
  8225. +/****************************************************************************
  8226. +*
  8227. +* vc_mem_mmap
  8228. +*
  8229. +***************************************************************************/
  8230. +
  8231. +static int
  8232. +vc_mem_mmap(struct file *filp, struct vm_area_struct *vma)
  8233. +{
  8234. + int rc = 0;
  8235. + unsigned long length = vma->vm_end - vma->vm_start;
  8236. + unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  8237. +
  8238. + pr_debug("%s: vm_start = 0x%08lx vm_end = 0x%08lx vm_pgoff = 0x%08lx\n",
  8239. + __func__, (long) vma->vm_start, (long) vma->vm_end,
  8240. + (long) vma->vm_pgoff);
  8241. +
  8242. + if (offset + length > mm_vc_mem_size) {
  8243. + pr_err("%s: length %ld is too big\n", __func__, length);
  8244. + return -EINVAL;
  8245. + }
  8246. + // Do not cache the memory map
  8247. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  8248. +
  8249. + rc = remap_pfn_range(vma, vma->vm_start,
  8250. + (mm_vc_mem_phys_addr >> PAGE_SHIFT) +
  8251. + vma->vm_pgoff, length, vma->vm_page_prot);
  8252. + if (rc != 0) {
  8253. + pr_err("%s: remap_pfn_range failed (rc=%d)\n", __func__, rc);
  8254. + }
  8255. +
  8256. + return rc;
  8257. +}
  8258. +
  8259. +/****************************************************************************
  8260. +*
  8261. +* File Operations for the driver.
  8262. +*
  8263. +***************************************************************************/
  8264. +
  8265. +static const struct file_operations vc_mem_fops = {
  8266. + .owner = THIS_MODULE,
  8267. + .open = vc_mem_open,
  8268. + .release = vc_mem_release,
  8269. + .unlocked_ioctl = vc_mem_ioctl,
  8270. + .mmap = vc_mem_mmap,
  8271. +};
  8272. +
  8273. +#ifdef CONFIG_DEBUG_FS
  8274. +static void vc_mem_debugfs_deinit(void)
  8275. +{
  8276. + debugfs_remove_recursive(vc_mem_debugfs_entry);
  8277. + vc_mem_debugfs_entry = NULL;
  8278. +}
  8279. +
  8280. +
  8281. +static int vc_mem_debugfs_init(
  8282. + struct device *dev)
  8283. +{
  8284. + vc_mem_debugfs_entry = debugfs_create_dir(DRIVER_NAME, NULL);
  8285. + if (!vc_mem_debugfs_entry) {
  8286. + dev_warn(dev, "could not create debugfs entry\n");
  8287. + return -EFAULT;
  8288. + }
  8289. +
  8290. + if (!debugfs_create_x32("vc_mem_phys_addr",
  8291. + 0444,
  8292. + vc_mem_debugfs_entry,
  8293. + (u32 *)&mm_vc_mem_phys_addr)) {
  8294. + dev_warn(dev, "%s:could not create vc_mem_phys entry\n",
  8295. + __func__);
  8296. + goto fail;
  8297. + }
  8298. +
  8299. + if (!debugfs_create_x32("vc_mem_size",
  8300. + 0444,
  8301. + vc_mem_debugfs_entry,
  8302. + (u32 *)&mm_vc_mem_size)) {
  8303. + dev_warn(dev, "%s:could not create vc_mem_size entry\n",
  8304. + __func__);
  8305. + goto fail;
  8306. + }
  8307. +
  8308. + if (!debugfs_create_x32("vc_mem_base",
  8309. + 0444,
  8310. + vc_mem_debugfs_entry,
  8311. + (u32 *)&mm_vc_mem_base)) {
  8312. + dev_warn(dev, "%s:could not create vc_mem_base entry\n",
  8313. + __func__);
  8314. + goto fail;
  8315. + }
  8316. +
  8317. + return 0;
  8318. +
  8319. +fail:
  8320. + vc_mem_debugfs_deinit();
  8321. + return -EFAULT;
  8322. +}
  8323. +
  8324. +#endif /* CONFIG_DEBUG_FS */
  8325. +
  8326. +
  8327. +/****************************************************************************
  8328. +*
  8329. +* vc_mem_init
  8330. +*
  8331. +***************************************************************************/
  8332. +
  8333. +static int __init
  8334. +vc_mem_init(void)
  8335. +{
  8336. + int rc = -EFAULT;
  8337. + struct device *dev;
  8338. +
  8339. + pr_debug("%s: called\n", __func__);
  8340. +
  8341. + mm_vc_mem_phys_addr = phys_addr;
  8342. + mm_vc_mem_size = mem_size;
  8343. + mm_vc_mem_base = mem_base;
  8344. +
  8345. + vc_mem_get_size();
  8346. +
  8347. + pr_info("vc-mem: phys_addr:0x%08lx mem_base=0x%08x mem_size:0x%08x(%u MiB)\n",
  8348. + mm_vc_mem_phys_addr, mm_vc_mem_base, mm_vc_mem_size, mm_vc_mem_size / (1024 * 1024));
  8349. +
  8350. + if ((rc = alloc_chrdev_region(&vc_mem_devnum, 0, 1, DRIVER_NAME)) < 0) {
  8351. + pr_err("%s: alloc_chrdev_region failed (rc=%d)\n",
  8352. + __func__, rc);
  8353. + goto out_err;
  8354. + }
  8355. +
  8356. + cdev_init(&vc_mem_cdev, &vc_mem_fops);
  8357. + if ((rc = cdev_add(&vc_mem_cdev, vc_mem_devnum, 1)) != 0) {
  8358. + pr_err("%s: cdev_add failed (rc=%d)\n", __func__, rc);
  8359. + goto out_unregister;
  8360. + }
  8361. +
  8362. + vc_mem_class = class_create(THIS_MODULE, DRIVER_NAME);
  8363. + if (IS_ERR(vc_mem_class)) {
  8364. + rc = PTR_ERR(vc_mem_class);
  8365. + pr_err("%s: class_create failed (rc=%d)\n", __func__, rc);
  8366. + goto out_cdev_del;
  8367. + }
  8368. +
  8369. + dev = device_create(vc_mem_class, NULL, vc_mem_devnum, NULL,
  8370. + DRIVER_NAME);
  8371. + if (IS_ERR(dev)) {
  8372. + rc = PTR_ERR(dev);
  8373. + pr_err("%s: device_create failed (rc=%d)\n", __func__, rc);
  8374. + goto out_class_destroy;
  8375. + }
  8376. +
  8377. +#ifdef CONFIG_DEBUG_FS
  8378. + /* don't fail if the debug entries cannot be created */
  8379. + vc_mem_debugfs_init(dev);
  8380. +#endif
  8381. +
  8382. + vc_mem_inited = 1;
  8383. + return 0;
  8384. +
  8385. + device_destroy(vc_mem_class, vc_mem_devnum);
  8386. +
  8387. + out_class_destroy:
  8388. + class_destroy(vc_mem_class);
  8389. + vc_mem_class = NULL;
  8390. +
  8391. + out_cdev_del:
  8392. + cdev_del(&vc_mem_cdev);
  8393. +
  8394. + out_unregister:
  8395. + unregister_chrdev_region(vc_mem_devnum, 1);
  8396. +
  8397. + out_err:
  8398. + return -1;
  8399. +}
  8400. +
  8401. +/****************************************************************************
  8402. +*
  8403. +* vc_mem_exit
  8404. +*
  8405. +***************************************************************************/
  8406. +
  8407. +static void __exit
  8408. +vc_mem_exit(void)
  8409. +{
  8410. + pr_debug("%s: called\n", __func__);
  8411. +
  8412. + if (vc_mem_inited) {
  8413. +#if CONFIG_DEBUG_FS
  8414. + vc_mem_debugfs_deinit();
  8415. +#endif
  8416. + device_destroy(vc_mem_class, vc_mem_devnum);
  8417. + class_destroy(vc_mem_class);
  8418. + cdev_del(&vc_mem_cdev);
  8419. + unregister_chrdev_region(vc_mem_devnum, 1);
  8420. + }
  8421. +}
  8422. +
  8423. +module_init(vc_mem_init);
  8424. +module_exit(vc_mem_exit);
  8425. +MODULE_LICENSE("GPL");
  8426. +MODULE_AUTHOR("Broadcom Corporation");
  8427. +
  8428. +module_param(phys_addr, uint, 0644);
  8429. +module_param(mem_size, uint, 0644);
  8430. +module_param(mem_base, uint, 0644);
  8431. +
  8432. diff -Nur linux-3.16.2/arch/arm/Makefile linux-3.16-rpi/arch/arm/Makefile
  8433. --- linux-3.16.2/arch/arm/Makefile 2014-09-06 01:37:11.000000000 +0200
  8434. +++ linux-3.16-rpi/arch/arm/Makefile 2014-09-14 19:03:03.000000000 +0200
  8435. @@ -145,6 +145,7 @@
  8436. machine-$(CONFIG_ARCH_AT91) += at91
  8437. machine-$(CONFIG_ARCH_AXXIA) += axxia
  8438. machine-$(CONFIG_ARCH_BCM) += bcm
  8439. +machine-$(CONFIG_ARCH_BCM2708) += bcm2708
  8440. machine-$(CONFIG_ARCH_BERLIN) += berlin
  8441. machine-$(CONFIG_ARCH_CLPS711X) += clps711x
  8442. machine-$(CONFIG_ARCH_CNS3XXX) += cns3xxx
  8443. diff -Nur linux-3.16.2/arch/arm/mm/Kconfig linux-3.16-rpi/arch/arm/mm/Kconfig
  8444. --- linux-3.16.2/arch/arm/mm/Kconfig 2014-09-06 01:37:11.000000000 +0200
  8445. +++ linux-3.16-rpi/arch/arm/mm/Kconfig 2014-09-14 19:03:09.000000000 +0200
  8446. @@ -358,7 +358,7 @@
  8447. # ARMv6
  8448. config CPU_V6
  8449. - bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX
  8450. + bool "Support ARM V6 processor" if ARCH_INTEGRATOR || MACH_REALVIEW_EB || MACH_REALVIEW_PBX || MACH_BCM2708
  8451. select CPU_32v6
  8452. select CPU_ABRT_EV6
  8453. select CPU_CACHE_V6
  8454. diff -Nur linux-3.16.2/arch/arm/mm/proc-v6.S linux-3.16-rpi/arch/arm/mm/proc-v6.S
  8455. --- linux-3.16.2/arch/arm/mm/proc-v6.S 2014-09-06 01:37:11.000000000 +0200
  8456. +++ linux-3.16-rpi/arch/arm/mm/proc-v6.S 2014-09-14 19:03:09.000000000 +0200
  8457. @@ -73,10 +73,19 @@
  8458. *
  8459. * IRQs are already disabled.
  8460. */
  8461. +
  8462. +/* See jira SW-5991 for details of this workaround */
  8463. ENTRY(cpu_v6_do_idle)
  8464. - mov r1, #0
  8465. - mcr p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  8466. - mcr p15, 0, r1, c7, c0, 4 @ wait for interrupt
  8467. + .align 5
  8468. + mov r1, #2
  8469. +1: subs r1, #1
  8470. + nop
  8471. + mcreq p15, 0, r1, c7, c10, 4 @ DWB - WFI may enter a low-power mode
  8472. + mcreq p15, 0, r1, c7, c0, 4 @ wait for interrupt
  8473. + nop
  8474. + nop
  8475. + nop
  8476. + bne 1b
  8477. mov pc, lr
  8478. ENTRY(cpu_v6_dcache_clean_area)
  8479. diff -Nur linux-3.16.2/arch/arm/tools/mach-types linux-3.16-rpi/arch/arm/tools/mach-types
  8480. --- linux-3.16.2/arch/arm/tools/mach-types 2014-09-06 01:37:11.000000000 +0200
  8481. +++ linux-3.16-rpi/arch/arm/tools/mach-types 2014-04-13 17:32:41.000000000 +0200
  8482. @@ -522,6 +522,7 @@
  8483. prima2_evb MACH_PRIMA2_EVB PRIMA2_EVB 3103
  8484. paz00 MACH_PAZ00 PAZ00 3128
  8485. acmenetusfoxg20 MACH_ACMENETUSFOXG20 ACMENETUSFOXG20 3129
  8486. +bcm2708 MACH_BCM2708 BCM2708 3138
  8487. ag5evm MACH_AG5EVM AG5EVM 3189
  8488. ics_if_voip MACH_ICS_IF_VOIP ICS_IF_VOIP 3206
  8489. wlf_cragg_6410 MACH_WLF_CRAGG_6410 WLF_CRAGG_6410 3207
  8490. diff -Nur linux-3.16.2/Documentation/video4linux/bcm2835-v4l2.txt linux-3.16-rpi/Documentation/video4linux/bcm2835-v4l2.txt
  8491. --- linux-3.16.2/Documentation/video4linux/bcm2835-v4l2.txt 1970-01-01 01:00:00.000000000 +0100
  8492. +++ linux-3.16-rpi/Documentation/video4linux/bcm2835-v4l2.txt 2014-04-13 17:32:39.000000000 +0200
  8493. @@ -0,0 +1,60 @@
  8494. +
  8495. +BCM2835 (aka Raspberry Pi) V4L2 driver
  8496. +======================================
  8497. +
  8498. +1. Copyright
  8499. +============
  8500. +
  8501. +Copyright © 2013 Raspberry Pi (Trading) Ltd.
  8502. +
  8503. +2. License
  8504. +==========
  8505. +
  8506. +This program is free software; you can redistribute it and/or modify
  8507. +it under the terms of the GNU General Public License as published by
  8508. +the Free Software Foundation; either version 2 of the License, or
  8509. +(at your option) any later version.
  8510. +
  8511. +This program is distributed in the hope that it will be useful,
  8512. +but WITHOUT ANY WARRANTY; without even the implied warranty of
  8513. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8514. +GNU General Public License for more details.
  8515. +
  8516. +You should have received a copy of the GNU General Public License
  8517. +along with this program; if not, write to the Free Software
  8518. +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  8519. +
  8520. +3. Quick Start
  8521. +==============
  8522. +
  8523. +You need a version 1.0 or later of v4l2-ctl, available from:
  8524. + git://git.linuxtv.org/v4l-utils.git
  8525. +
  8526. +$ sudo modprobe bcm2835-v4l2
  8527. +
  8528. +Turn on the overlay:
  8529. +
  8530. +$ v4l2-ctl --overlay=1
  8531. +
  8532. +Turn off the overlay:
  8533. +
  8534. +$ v4l2-ctl --overlay=0
  8535. +
  8536. +Set the capture format for video:
  8537. +
  8538. +$ v4l2-ctl --set-fmt-video=width=1920,height=1088,pixelformat=4
  8539. +
  8540. +(Note: 1088 not 1080).
  8541. +
  8542. +Capture:
  8543. +
  8544. +$ v4l2-ctl --stream-mmap=3 --stream-count=100 --stream-to=somefile.h264
  8545. +
  8546. +Stills capture:
  8547. +
  8548. +$ v4l2-ctl --set-fmt-video=width=2592,height=1944,pixelformat=3
  8549. +$ v4l2-ctl --stream-mmap=3 --stream-count=1 --stream-to=somefile.jpg
  8550. +
  8551. +List of available formats:
  8552. +
  8553. +$ v4l2-ctl --list-formats
  8554. diff -Nur linux-3.16.2/drivers/char/broadcom/Kconfig linux-3.16-rpi/drivers/char/broadcom/Kconfig
  8555. --- linux-3.16.2/drivers/char/broadcom/Kconfig 1970-01-01 01:00:00.000000000 +0100
  8556. +++ linux-3.16-rpi/drivers/char/broadcom/Kconfig 2014-09-14 19:03:14.000000000 +0200
  8557. @@ -0,0 +1,22 @@
  8558. +#
  8559. +# Broadcom char driver config
  8560. +#
  8561. +
  8562. +menuconfig BRCM_CHAR_DRIVERS
  8563. + bool "Broadcom Char Drivers"
  8564. + help
  8565. + Broadcom's char drivers
  8566. +
  8567. +config BCM_VC_CMA
  8568. + bool "Videocore CMA"
  8569. + depends on CMA && BRCM_CHAR_DRIVERS && BCM2708_VCHIQ
  8570. + default n
  8571. + help
  8572. + Helper for videocore CMA access.
  8573. +
  8574. +config BCM_VC_SM
  8575. + tristate "VMCS Shared Memory"
  8576. + default n
  8577. + help
  8578. + Support for the VC shared memory on the Broadcom reference
  8579. + design. Uses the VCHIQ stack.
  8580. diff -Nur linux-3.16.2/drivers/char/broadcom/Makefile linux-3.16-rpi/drivers/char/broadcom/Makefile
  8581. --- linux-3.16.2/drivers/char/broadcom/Makefile 1970-01-01 01:00:00.000000000 +0100
  8582. +++ linux-3.16-rpi/drivers/char/broadcom/Makefile 2014-09-14 19:03:14.000000000 +0200
  8583. @@ -0,0 +1,2 @@
  8584. +obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
  8585. +obj-$(CONFIG_BCM_VC_SM) += vc_sm/
  8586. diff -Nur linux-3.16.2/drivers/char/broadcom/vc_cma/Makefile linux-3.16-rpi/drivers/char/broadcom/vc_cma/Makefile
  8587. --- linux-3.16.2/drivers/char/broadcom/vc_cma/Makefile 1970-01-01 01:00:00.000000000 +0100
  8588. +++ linux-3.16-rpi/drivers/char/broadcom/vc_cma/Makefile 2014-09-14 19:03:14.000000000 +0200
  8589. @@ -0,0 +1,14 @@
  8590. +ccflags-y += -Wall -Wstrict-prototypes -Wno-trigraphs
  8591. +ccflags-y += -Werror
  8592. +ccflags-y += -Iinclude/linux/broadcom
  8593. +ccflags-y += -Idrivers/misc/vc04_services
  8594. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchi
  8595. +ccflags-y += -Idrivers/misc/vc04_services/interface/vchiq_arm
  8596. +
  8597. +ccflags-y += -D__KERNEL__
  8598. +ccflags-y += -D__linux__
  8599. +ccflags-y += -Werror
  8600. +
  8601. +obj-$(CONFIG_BCM_VC_CMA) += vc-cma.o
  8602. +
  8603. +vc-cma-objs := vc_cma.o
  8604. diff -Nur linux-3.16.2/drivers/char/broadcom/vc_cma/vc_cma.c linux-3.16-rpi/drivers/char/broadcom/vc_cma/vc_cma.c
  8605. --- linux-3.16.2/drivers/char/broadcom/vc_cma/vc_cma.c 1970-01-01 01:00:00.000000000 +0100
  8606. +++ linux-3.16-rpi/drivers/char/broadcom/vc_cma/vc_cma.c 2014-09-14 19:03:14.000000000 +0200
  8607. @@ -0,0 +1,1143 @@
  8608. +/**
  8609. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  8610. + *
  8611. + * Redistribution and use in source and binary forms, with or without
  8612. + * modification, are permitted provided that the following conditions
  8613. + * are met:
  8614. + * 1. Redistributions of source code must retain the above copyright
  8615. + * notice, this list of conditions, and the following disclaimer,
  8616. + * without modification.
  8617. + * 2. Redistributions in binary form must reproduce the above copyright
  8618. + * notice, this list of conditions and the following disclaimer in the
  8619. + * documentation and/or other materials provided with the distribution.
  8620. + * 3. The names of the above-listed copyright holders may not be used
  8621. + * to endorse or promote products derived from this software without
  8622. + * specific prior written permission.
  8623. + *
  8624. + * ALTERNATIVELY, this software may be distributed under the terms of the
  8625. + * GNU General Public License ("GPL") version 2, as published by the Free
  8626. + * Software Foundation.
  8627. + *
  8628. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  8629. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  8630. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  8631. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  8632. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  8633. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  8634. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  8635. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  8636. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  8637. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  8638. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8639. + */
  8640. +
  8641. +#include <linux/kernel.h>
  8642. +#include <linux/module.h>
  8643. +#include <linux/kthread.h>
  8644. +#include <linux/fs.h>
  8645. +#include <linux/device.h>
  8646. +#include <linux/cdev.h>
  8647. +#include <linux/mm.h>
  8648. +#include <linux/proc_fs.h>
  8649. +#include <linux/seq_file.h>
  8650. +#include <linux/dma-mapping.h>
  8651. +#include <linux/dma-contiguous.h>
  8652. +#include <linux/platform_device.h>
  8653. +#include <linux/uaccess.h>
  8654. +#include <asm/cacheflush.h>
  8655. +
  8656. +#include "vc_cma.h"
  8657. +
  8658. +#include "vchiq_util.h"
  8659. +#include "vchiq_connected.h"
  8660. +//#include "debug_sym.h"
  8661. +//#include "vc_mem.h"
  8662. +
  8663. +#define DRIVER_NAME "vc-cma"
  8664. +
  8665. +#define LOG_DBG(fmt, ...) \
  8666. + if (vc_cma_debug) \
  8667. + printk(KERN_INFO fmt "\n", ##__VA_ARGS__)
  8668. +#define LOG_ERR(fmt, ...) \
  8669. + printk(KERN_ERR fmt "\n", ##__VA_ARGS__)
  8670. +
  8671. +#define VC_CMA_FOURCC VCHIQ_MAKE_FOURCC('C', 'M', 'A', ' ')
  8672. +#define VC_CMA_VERSION 2
  8673. +
  8674. +#define VC_CMA_CHUNK_ORDER 6 /* 256K */
  8675. +#define VC_CMA_CHUNK_SIZE (4096 << VC_CMA_CHUNK_ORDER)
  8676. +#define VC_CMA_MAX_PARAMS_PER_MSG \
  8677. + ((VCHIQ_MAX_MSG_SIZE - sizeof(unsigned short))/sizeof(unsigned short))
  8678. +#define VC_CMA_RESERVE_COUNT_MAX 16
  8679. +
  8680. +#define PAGES_PER_CHUNK (VC_CMA_CHUNK_SIZE / PAGE_SIZE)
  8681. +
  8682. +#define VCADDR_TO_PHYSADDR(vcaddr) (mm_vc_mem_phys_addr + vcaddr)
  8683. +
  8684. +#define loud_error(...) \
  8685. + LOG_ERR("===== " __VA_ARGS__)
  8686. +
  8687. +enum {
  8688. + VC_CMA_MSG_QUIT,
  8689. + VC_CMA_MSG_OPEN,
  8690. + VC_CMA_MSG_TICK,
  8691. + VC_CMA_MSG_ALLOC, /* chunk count */
  8692. + VC_CMA_MSG_FREE, /* chunk, chunk, ... */
  8693. + VC_CMA_MSG_ALLOCATED, /* chunk, chunk, ... */
  8694. + VC_CMA_MSG_REQUEST_ALLOC, /* chunk count */
  8695. + VC_CMA_MSG_REQUEST_FREE, /* chunk count */
  8696. + VC_CMA_MSG_RESERVE, /* bytes lo, bytes hi */
  8697. + VC_CMA_MSG_UPDATE_RESERVE,
  8698. + VC_CMA_MSG_MAX
  8699. +};
  8700. +
  8701. +struct cma_msg {
  8702. + unsigned short type;
  8703. + unsigned short params[VC_CMA_MAX_PARAMS_PER_MSG];
  8704. +};
  8705. +
  8706. +struct vc_cma_reserve_user {
  8707. + unsigned int pid;
  8708. + unsigned int reserve;
  8709. +};
  8710. +
  8711. +/* Device (/dev) related variables */
  8712. +static dev_t vc_cma_devnum;
  8713. +static struct class *vc_cma_class;
  8714. +static struct cdev vc_cma_cdev;
  8715. +static int vc_cma_inited;
  8716. +static int vc_cma_debug;
  8717. +
  8718. +/* Proc entry */
  8719. +static struct proc_dir_entry *vc_cma_proc_entry;
  8720. +
  8721. +phys_addr_t vc_cma_base;
  8722. +struct page *vc_cma_base_page;
  8723. +unsigned int vc_cma_size;
  8724. +EXPORT_SYMBOL(vc_cma_size);
  8725. +unsigned int vc_cma_initial;
  8726. +unsigned int vc_cma_chunks;
  8727. +unsigned int vc_cma_chunks_used;
  8728. +unsigned int vc_cma_chunks_reserved;
  8729. +
  8730. +static int in_loud_error;
  8731. +
  8732. +unsigned int vc_cma_reserve_total;
  8733. +unsigned int vc_cma_reserve_count;
  8734. +struct vc_cma_reserve_user vc_cma_reserve_users[VC_CMA_RESERVE_COUNT_MAX];
  8735. +static DEFINE_SEMAPHORE(vc_cma_reserve_mutex);
  8736. +static DEFINE_SEMAPHORE(vc_cma_worker_queue_push_mutex);
  8737. +
  8738. +static u64 vc_cma_dma_mask = DMA_BIT_MASK(32);
  8739. +static struct platform_device vc_cma_device = {
  8740. + .name = "vc-cma",
  8741. + .id = 0,
  8742. + .dev = {
  8743. + .dma_mask = &vc_cma_dma_mask,
  8744. + .coherent_dma_mask = DMA_BIT_MASK(32),
  8745. + },
  8746. +};
  8747. +
  8748. +static VCHIQ_INSTANCE_T cma_instance;
  8749. +static VCHIQ_SERVICE_HANDLE_T cma_service;
  8750. +static VCHIU_QUEUE_T cma_msg_queue;
  8751. +static struct task_struct *cma_worker;
  8752. +
  8753. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid);
  8754. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply);
  8755. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  8756. + VCHIQ_HEADER_T * header,
  8757. + VCHIQ_SERVICE_HANDLE_T service,
  8758. + void *bulk_userdata);
  8759. +static void send_vc_msg(unsigned short type,
  8760. + unsigned short param1, unsigned short param2);
  8761. +static bool send_worker_msg(VCHIQ_HEADER_T * msg);
  8762. +
  8763. +static int early_vc_cma_mem(char *p)
  8764. +{
  8765. + unsigned int new_size;
  8766. + printk(KERN_NOTICE "early_vc_cma_mem(%s)", p);
  8767. + vc_cma_size = memparse(p, &p);
  8768. + vc_cma_initial = vc_cma_size;
  8769. + if (*p == '/')
  8770. + vc_cma_size = memparse(p + 1, &p);
  8771. + if (*p == '@')
  8772. + vc_cma_base = memparse(p + 1, &p);
  8773. +
  8774. + new_size = (vc_cma_size - ((-vc_cma_base) & (VC_CMA_CHUNK_SIZE - 1)))
  8775. + & ~(VC_CMA_CHUNK_SIZE - 1);
  8776. + if (new_size > vc_cma_size)
  8777. + vc_cma_size = 0;
  8778. + vc_cma_initial = (vc_cma_initial + VC_CMA_CHUNK_SIZE - 1)
  8779. + & ~(VC_CMA_CHUNK_SIZE - 1);
  8780. + if (vc_cma_initial > vc_cma_size)
  8781. + vc_cma_initial = vc_cma_size;
  8782. + vc_cma_base = (vc_cma_base + VC_CMA_CHUNK_SIZE - 1)
  8783. + & ~(VC_CMA_CHUNK_SIZE - 1);
  8784. +
  8785. + printk(KERN_NOTICE " -> initial %x, size %x, base %x", vc_cma_initial,
  8786. + vc_cma_size, (unsigned int)vc_cma_base);
  8787. +
  8788. + return 0;
  8789. +}
  8790. +
  8791. +early_param("vc-cma-mem", early_vc_cma_mem);
  8792. +
  8793. +void vc_cma_early_init(void)
  8794. +{
  8795. + LOG_DBG("vc_cma_early_init - vc_cma_chunks = %d", vc_cma_chunks);
  8796. + if (vc_cma_size) {
  8797. + int rc = platform_device_register(&vc_cma_device);
  8798. + LOG_DBG("platform_device_register -> %d", rc);
  8799. + }
  8800. +}
  8801. +
  8802. +void vc_cma_reserve(void)
  8803. +{
  8804. + /* if vc_cma_size is set, then declare vc CMA area of the same
  8805. + * size from the end of memory
  8806. + */
  8807. + if (vc_cma_size) {
  8808. + if (dma_declare_contiguous(NULL /*&vc_cma_device.dev*/, vc_cma_size,
  8809. + vc_cma_base, 0) == 0) {
  8810. + } else {
  8811. + LOG_ERR("vc_cma: dma_declare_contiguous(%x,%x) failed",
  8812. + vc_cma_size, (unsigned int)vc_cma_base);
  8813. + vc_cma_size = 0;
  8814. + }
  8815. + }
  8816. + vc_cma_chunks = vc_cma_size / VC_CMA_CHUNK_SIZE;
  8817. +}
  8818. +
  8819. +/****************************************************************************
  8820. +*
  8821. +* vc_cma_open
  8822. +*
  8823. +***************************************************************************/
  8824. +
  8825. +static int vc_cma_open(struct inode *inode, struct file *file)
  8826. +{
  8827. + (void)inode;
  8828. + (void)file;
  8829. +
  8830. + return 0;
  8831. +}
  8832. +
  8833. +/****************************************************************************
  8834. +*
  8835. +* vc_cma_release
  8836. +*
  8837. +***************************************************************************/
  8838. +
  8839. +static int vc_cma_release(struct inode *inode, struct file *file)
  8840. +{
  8841. + (void)inode;
  8842. + (void)file;
  8843. +
  8844. + vc_cma_set_reserve(0, current->tgid);
  8845. +
  8846. + return 0;
  8847. +}
  8848. +
  8849. +/****************************************************************************
  8850. +*
  8851. +* vc_cma_ioctl
  8852. +*
  8853. +***************************************************************************/
  8854. +
  8855. +static long vc_cma_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  8856. +{
  8857. + int rc = 0;
  8858. +
  8859. + (void)cmd;
  8860. + (void)arg;
  8861. +
  8862. + switch (cmd) {
  8863. + case VC_CMA_IOC_RESERVE:
  8864. + rc = vc_cma_set_reserve((unsigned int)arg, current->tgid);
  8865. + if (rc >= 0)
  8866. + rc = 0;
  8867. + break;
  8868. + default:
  8869. + LOG_ERR("vc-cma: Unknown ioctl %x", cmd);
  8870. + return -ENOTTY;
  8871. + }
  8872. +
  8873. + return rc;
  8874. +}
  8875. +
  8876. +/****************************************************************************
  8877. +*
  8878. +* File Operations for the driver.
  8879. +*
  8880. +***************************************************************************/
  8881. +
  8882. +static const struct file_operations vc_cma_fops = {
  8883. + .owner = THIS_MODULE,
  8884. + .open = vc_cma_open,
  8885. + .release = vc_cma_release,
  8886. + .unlocked_ioctl = vc_cma_ioctl,
  8887. +};
  8888. +
  8889. +/****************************************************************************
  8890. +*
  8891. +* vc_cma_proc_open
  8892. +*
  8893. +***************************************************************************/
  8894. +
  8895. +static int vc_cma_show_info(struct seq_file *m, void *v)
  8896. +{
  8897. + int i;
  8898. +
  8899. + seq_printf(m, "Videocore CMA:\n");
  8900. + seq_printf(m, " Base : %08x\n", (unsigned int)vc_cma_base);
  8901. + seq_printf(m, " Length : %08x\n", vc_cma_size);
  8902. + seq_printf(m, " Initial : %08x\n", vc_cma_initial);
  8903. + seq_printf(m, " Chunk size : %08x\n", VC_CMA_CHUNK_SIZE);
  8904. + seq_printf(m, " Chunks : %4d (%d bytes)\n",
  8905. + (int)vc_cma_chunks,
  8906. + (int)(vc_cma_chunks * VC_CMA_CHUNK_SIZE));
  8907. + seq_printf(m, " Used : %4d (%d bytes)\n",
  8908. + (int)vc_cma_chunks_used,
  8909. + (int)(vc_cma_chunks_used * VC_CMA_CHUNK_SIZE));
  8910. + seq_printf(m, " Reserved : %4d (%d bytes)\n",
  8911. + (unsigned int)vc_cma_chunks_reserved,
  8912. + (int)(vc_cma_chunks_reserved * VC_CMA_CHUNK_SIZE));
  8913. +
  8914. + for (i = 0; i < vc_cma_reserve_count; i++) {
  8915. + struct vc_cma_reserve_user *user = &vc_cma_reserve_users[i];
  8916. + seq_printf(m, " PID %5d: %d bytes\n", user->pid,
  8917. + user->reserve);
  8918. + }
  8919. +
  8920. + seq_printf(m, "\n");
  8921. +
  8922. + return 0;
  8923. +}
  8924. +
  8925. +static int vc_cma_proc_open(struct inode *inode, struct file *file)
  8926. +{
  8927. + return single_open(file, vc_cma_show_info, NULL);
  8928. +}
  8929. +
  8930. +/****************************************************************************
  8931. +*
  8932. +* vc_cma_proc_write
  8933. +*
  8934. +***************************************************************************/
  8935. +
  8936. +static int vc_cma_proc_write(struct file *file,
  8937. + const char __user *buffer,
  8938. + size_t size, loff_t *ppos)
  8939. +{
  8940. + int rc = -EFAULT;
  8941. + char input_str[20];
  8942. +
  8943. + memset(input_str, 0, sizeof(input_str));
  8944. +
  8945. + if (size > sizeof(input_str)) {
  8946. + LOG_ERR("%s: input string length too long", __func__);
  8947. + goto out;
  8948. + }
  8949. +
  8950. + if (copy_from_user(input_str, buffer, size - 1)) {
  8951. + LOG_ERR("%s: failed to get input string", __func__);
  8952. + goto out;
  8953. + }
  8954. +#define ALLOC_STR "alloc"
  8955. +#define FREE_STR "free"
  8956. +#define DEBUG_STR "debug"
  8957. +#define RESERVE_STR "reserve"
  8958. + if (strncmp(input_str, ALLOC_STR, strlen(ALLOC_STR)) == 0) {
  8959. + int size;
  8960. + char *p = input_str + strlen(ALLOC_STR);
  8961. +
  8962. + while (*p == ' ')
  8963. + p++;
  8964. + size = memparse(p, NULL);
  8965. + LOG_ERR("/proc/vc-cma: alloc %d", size);
  8966. + if (size)
  8967. + send_vc_msg(VC_CMA_MSG_REQUEST_FREE,
  8968. + size / VC_CMA_CHUNK_SIZE, 0);
  8969. + else
  8970. + LOG_ERR("invalid size '%s'", p);
  8971. + rc = size;
  8972. + } else if (strncmp(input_str, FREE_STR, strlen(FREE_STR)) == 0) {
  8973. + int size;
  8974. + char *p = input_str + strlen(FREE_STR);
  8975. +
  8976. + while (*p == ' ')
  8977. + p++;
  8978. + size = memparse(p, NULL);
  8979. + LOG_ERR("/proc/vc-cma: free %d", size);
  8980. + if (size)
  8981. + send_vc_msg(VC_CMA_MSG_REQUEST_ALLOC,
  8982. + size / VC_CMA_CHUNK_SIZE, 0);
  8983. + else
  8984. + LOG_ERR("invalid size '%s'", p);
  8985. + rc = size;
  8986. + } else if (strncmp(input_str, DEBUG_STR, strlen(DEBUG_STR)) == 0) {
  8987. + char *p = input_str + strlen(DEBUG_STR);
  8988. + while (*p == ' ')
  8989. + p++;
  8990. + if ((strcmp(p, "on") == 0) || (strcmp(p, "1") == 0))
  8991. + vc_cma_debug = 1;
  8992. + else if ((strcmp(p, "off") == 0) || (strcmp(p, "0") == 0))
  8993. + vc_cma_debug = 0;
  8994. + LOG_ERR("/proc/vc-cma: debug %s", vc_cma_debug ? "on" : "off");
  8995. + rc = size;
  8996. + } else if (strncmp(input_str, RESERVE_STR, strlen(RESERVE_STR)) == 0) {
  8997. + int size;
  8998. + int reserved;
  8999. + char *p = input_str + strlen(RESERVE_STR);
  9000. + while (*p == ' ')
  9001. + p++;
  9002. + size = memparse(p, NULL);
  9003. +
  9004. + reserved = vc_cma_set_reserve(size, current->tgid);
  9005. + rc = (reserved >= 0) ? size : reserved;
  9006. + }
  9007. +
  9008. +out:
  9009. + return rc;
  9010. +}
  9011. +
  9012. +/****************************************************************************
  9013. +*
  9014. +* File Operations for /proc interface.
  9015. +*
  9016. +***************************************************************************/
  9017. +
  9018. +static const struct file_operations vc_cma_proc_fops = {
  9019. + .open = vc_cma_proc_open,
  9020. + .read = seq_read,
  9021. + .write = vc_cma_proc_write,
  9022. + .llseek = seq_lseek,
  9023. + .release = single_release
  9024. +};
  9025. +
  9026. +static int vc_cma_set_reserve(unsigned int reserve, unsigned int pid)
  9027. +{
  9028. + struct vc_cma_reserve_user *user = NULL;
  9029. + int delta = 0;
  9030. + int i;
  9031. +
  9032. + if (down_interruptible(&vc_cma_reserve_mutex))
  9033. + return -ERESTARTSYS;
  9034. +
  9035. + for (i = 0; i < vc_cma_reserve_count; i++) {
  9036. + if (pid == vc_cma_reserve_users[i].pid) {
  9037. + user = &vc_cma_reserve_users[i];
  9038. + delta = reserve - user->reserve;
  9039. + if (reserve)
  9040. + user->reserve = reserve;
  9041. + else {
  9042. + /* Remove this entry by copying downwards */
  9043. + while ((i + 1) < vc_cma_reserve_count) {
  9044. + user[0].pid = user[1].pid;
  9045. + user[0].reserve = user[1].reserve;
  9046. + user++;
  9047. + i++;
  9048. + }
  9049. + vc_cma_reserve_count--;
  9050. + user = NULL;
  9051. + }
  9052. + break;
  9053. + }
  9054. + }
  9055. +
  9056. + if (reserve && !user) {
  9057. + if (vc_cma_reserve_count == VC_CMA_RESERVE_COUNT_MAX) {
  9058. + LOG_ERR("vc-cma: Too many reservations - "
  9059. + "increase CMA_RESERVE_COUNT_MAX");
  9060. + up(&vc_cma_reserve_mutex);
  9061. + return -EBUSY;
  9062. + }
  9063. + user = &vc_cma_reserve_users[vc_cma_reserve_count];
  9064. + user->pid = pid;
  9065. + user->reserve = reserve;
  9066. + delta = reserve;
  9067. + vc_cma_reserve_count++;
  9068. + }
  9069. +
  9070. + vc_cma_reserve_total += delta;
  9071. +
  9072. + send_vc_msg(VC_CMA_MSG_RESERVE,
  9073. + vc_cma_reserve_total & 0xffff, vc_cma_reserve_total >> 16);
  9074. +
  9075. + send_worker_msg((VCHIQ_HEADER_T *) VC_CMA_MSG_UPDATE_RESERVE);
  9076. +
  9077. + LOG_DBG("/proc/vc-cma: reserve %d (PID %d) - total %u",
  9078. + reserve, pid, vc_cma_reserve_total);
  9079. +
  9080. + up(&vc_cma_reserve_mutex);
  9081. +
  9082. + return vc_cma_reserve_total;
  9083. +}
  9084. +
  9085. +static VCHIQ_STATUS_T cma_service_callback(VCHIQ_REASON_T reason,
  9086. + VCHIQ_HEADER_T * header,
  9087. + VCHIQ_SERVICE_HANDLE_T service,
  9088. + void *bulk_userdata)
  9089. +{
  9090. + switch (reason) {
  9091. + case VCHIQ_MESSAGE_AVAILABLE:
  9092. + if (!send_worker_msg(header))
  9093. + return VCHIQ_RETRY;
  9094. + break;
  9095. + case VCHIQ_SERVICE_CLOSED:
  9096. + LOG_DBG("CMA service closed");
  9097. + break;
  9098. + default:
  9099. + LOG_ERR("Unexpected CMA callback reason %d", reason);
  9100. + break;
  9101. + }
  9102. + return VCHIQ_SUCCESS;
  9103. +}
  9104. +
  9105. +static void send_vc_msg(unsigned short type,
  9106. + unsigned short param1, unsigned short param2)
  9107. +{
  9108. + unsigned short msg[] = { type, param1, param2 };
  9109. + VCHIQ_ELEMENT_T elem = { &msg, sizeof(msg) };
  9110. + VCHIQ_STATUS_T ret;
  9111. + vchiq_use_service(cma_service);
  9112. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9113. + vchiq_release_service(cma_service);
  9114. + if (ret != VCHIQ_SUCCESS)
  9115. + LOG_ERR("vchiq_queue_message returned %x", ret);
  9116. +}
  9117. +
  9118. +static bool send_worker_msg(VCHIQ_HEADER_T * msg)
  9119. +{
  9120. + if (down_interruptible(&vc_cma_worker_queue_push_mutex))
  9121. + return false;
  9122. + vchiu_queue_push(&cma_msg_queue, msg);
  9123. + up(&vc_cma_worker_queue_push_mutex);
  9124. + return true;
  9125. +}
  9126. +
  9127. +static int vc_cma_alloc_chunks(int num_chunks, struct cma_msg *reply)
  9128. +{
  9129. + int i;
  9130. + for (i = 0; i < num_chunks; i++) {
  9131. + struct page *chunk;
  9132. + unsigned int chunk_num;
  9133. + uint8_t *chunk_addr;
  9134. + size_t chunk_size = PAGES_PER_CHUNK << PAGE_SHIFT;
  9135. +
  9136. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  9137. + PAGES_PER_CHUNK,
  9138. + VC_CMA_CHUNK_ORDER);
  9139. + if (!chunk)
  9140. + break;
  9141. +
  9142. + chunk_addr = page_address(chunk);
  9143. + dmac_flush_range(chunk_addr, chunk_addr + chunk_size);
  9144. + outer_inv_range(__pa(chunk_addr), __pa(chunk_addr) +
  9145. + chunk_size);
  9146. +
  9147. + chunk_num =
  9148. + (page_to_phys(chunk) - vc_cma_base) / VC_CMA_CHUNK_SIZE;
  9149. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  9150. + VC_CMA_CHUNK_SIZE) != 0);
  9151. + if (chunk_num >= vc_cma_chunks) {
  9152. + LOG_ERR("%s: ===============================",
  9153. + __func__);
  9154. + LOG_ERR("%s: chunk phys %x, vc_cma %x-%x - "
  9155. + "bad SPARSEMEM configuration?",
  9156. + __func__, (unsigned int)page_to_phys(chunk),
  9157. + vc_cma_base, vc_cma_base + vc_cma_size - 1);
  9158. + LOG_ERR("%s: dev->cma_area = %p\n", __func__,
  9159. + (void*)0/*vc_cma_device.dev.cma_area*/);
  9160. + LOG_ERR("%s: ===============================",
  9161. + __func__);
  9162. + break;
  9163. + }
  9164. + reply->params[i] = chunk_num;
  9165. + vc_cma_chunks_used++;
  9166. + }
  9167. +
  9168. + if (i < num_chunks) {
  9169. + LOG_ERR("%s: dma_alloc_from_contiguous failed "
  9170. + "for %x bytes (alloc %d of %d, %d free)",
  9171. + __func__, VC_CMA_CHUNK_SIZE, i,
  9172. + num_chunks, vc_cma_chunks - vc_cma_chunks_used);
  9173. + num_chunks = i;
  9174. + }
  9175. +
  9176. + LOG_DBG("CMA allocated %d chunks -> %d used",
  9177. + num_chunks, vc_cma_chunks_used);
  9178. + reply->type = VC_CMA_MSG_ALLOCATED;
  9179. +
  9180. + {
  9181. + VCHIQ_ELEMENT_T elem = {
  9182. + reply,
  9183. + offsetof(struct cma_msg, params[0]) +
  9184. + num_chunks * sizeof(reply->params[0])
  9185. + };
  9186. + VCHIQ_STATUS_T ret;
  9187. + vchiq_use_service(cma_service);
  9188. + ret = vchiq_queue_message(cma_service, &elem, 1);
  9189. + vchiq_release_service(cma_service);
  9190. + if (ret != VCHIQ_SUCCESS)
  9191. + LOG_ERR("vchiq_queue_message return " "%x", ret);
  9192. + }
  9193. +
  9194. + return num_chunks;
  9195. +}
  9196. +
  9197. +static int cma_worker_proc(void *param)
  9198. +{
  9199. + static struct cma_msg reply;
  9200. + (void)param;
  9201. +
  9202. + while (1) {
  9203. + VCHIQ_HEADER_T *msg;
  9204. + static struct cma_msg msg_copy;
  9205. + struct cma_msg *cma_msg = &msg_copy;
  9206. + int type, msg_size;
  9207. +
  9208. + msg = vchiu_queue_pop(&cma_msg_queue);
  9209. + if ((unsigned int)msg >= VC_CMA_MSG_MAX) {
  9210. + msg_size = msg->size;
  9211. + memcpy(&msg_copy, msg->data, msg_size);
  9212. + type = cma_msg->type;
  9213. + vchiq_release_message(cma_service, msg);
  9214. + } else {
  9215. + msg_size = 0;
  9216. + type = (int)msg;
  9217. + if (type == VC_CMA_MSG_QUIT)
  9218. + break;
  9219. + else if (type == VC_CMA_MSG_UPDATE_RESERVE) {
  9220. + msg = NULL;
  9221. + cma_msg = NULL;
  9222. + } else {
  9223. + BUG();
  9224. + continue;
  9225. + }
  9226. + }
  9227. +
  9228. + switch (type) {
  9229. + case VC_CMA_MSG_ALLOC:{
  9230. + int num_chunks, free_chunks;
  9231. + num_chunks = cma_msg->params[0];
  9232. + free_chunks =
  9233. + vc_cma_chunks - vc_cma_chunks_used;
  9234. + LOG_DBG("CMA_MSG_ALLOC(%d chunks)", num_chunks);
  9235. + if (num_chunks > VC_CMA_MAX_PARAMS_PER_MSG) {
  9236. + LOG_ERR
  9237. + ("CMA_MSG_ALLOC - chunk count (%d) "
  9238. + "exceeds VC_CMA_MAX_PARAMS_PER_MSG (%d)",
  9239. + num_chunks,
  9240. + VC_CMA_MAX_PARAMS_PER_MSG);
  9241. + num_chunks = VC_CMA_MAX_PARAMS_PER_MSG;
  9242. + }
  9243. +
  9244. + if (num_chunks > free_chunks) {
  9245. + LOG_ERR
  9246. + ("CMA_MSG_ALLOC - chunk count (%d) "
  9247. + "exceeds free chunks (%d)",
  9248. + num_chunks, free_chunks);
  9249. + num_chunks = free_chunks;
  9250. + }
  9251. +
  9252. + vc_cma_alloc_chunks(num_chunks, &reply);
  9253. + }
  9254. + break;
  9255. +
  9256. + case VC_CMA_MSG_FREE:{
  9257. + int chunk_count =
  9258. + (msg_size -
  9259. + offsetof(struct cma_msg,
  9260. + params)) /
  9261. + sizeof(cma_msg->params[0]);
  9262. + int i;
  9263. + BUG_ON(chunk_count <= 0);
  9264. +
  9265. + LOG_DBG("CMA_MSG_FREE(%d chunks - %x, ...)",
  9266. + chunk_count, cma_msg->params[0]);
  9267. + for (i = 0; i < chunk_count; i++) {
  9268. + int chunk_num = cma_msg->params[i];
  9269. + struct page *page = vc_cma_base_page +
  9270. + chunk_num * PAGES_PER_CHUNK;
  9271. + if (chunk_num >= vc_cma_chunks) {
  9272. + LOG_ERR
  9273. + ("CMA_MSG_FREE - chunk %d of %d"
  9274. + " (value %x) exceeds maximum "
  9275. + "(%x)", i, chunk_count,
  9276. + chunk_num,
  9277. + vc_cma_chunks - 1);
  9278. + break;
  9279. + }
  9280. +
  9281. + if (!dma_release_from_contiguous
  9282. + (NULL /*&vc_cma_device.dev*/, page,
  9283. + PAGES_PER_CHUNK)) {
  9284. + LOG_ERR
  9285. + ("CMA_MSG_FREE - failed to "
  9286. + "release chunk %d (phys %x, "
  9287. + "page %x)", chunk_num,
  9288. + page_to_phys(page),
  9289. + (unsigned int)page);
  9290. + }
  9291. + vc_cma_chunks_used--;
  9292. + }
  9293. + LOG_DBG("CMA released %d chunks -> %d used",
  9294. + i, vc_cma_chunks_used);
  9295. + }
  9296. + break;
  9297. +
  9298. + case VC_CMA_MSG_UPDATE_RESERVE:{
  9299. + int chunks_needed =
  9300. + ((vc_cma_reserve_total + VC_CMA_CHUNK_SIZE -
  9301. + 1)
  9302. + / VC_CMA_CHUNK_SIZE) -
  9303. + vc_cma_chunks_reserved;
  9304. +
  9305. + LOG_DBG
  9306. + ("CMA_MSG_UPDATE_RESERVE(%d chunks needed)",
  9307. + chunks_needed);
  9308. +
  9309. + /* Cap the reservations to what is available */
  9310. + if (chunks_needed > 0) {
  9311. + if (chunks_needed >
  9312. + (vc_cma_chunks -
  9313. + vc_cma_chunks_used))
  9314. + chunks_needed =
  9315. + (vc_cma_chunks -
  9316. + vc_cma_chunks_used);
  9317. +
  9318. + chunks_needed =
  9319. + vc_cma_alloc_chunks(chunks_needed,
  9320. + &reply);
  9321. + }
  9322. +
  9323. + LOG_DBG
  9324. + ("CMA_MSG_UPDATE_RESERVE(%d chunks allocated)",
  9325. + chunks_needed);
  9326. + vc_cma_chunks_reserved += chunks_needed;
  9327. + }
  9328. + break;
  9329. +
  9330. + default:
  9331. + LOG_ERR("unexpected msg type %d", type);
  9332. + break;
  9333. + }
  9334. + }
  9335. +
  9336. + LOG_DBG("quitting...");
  9337. + return 0;
  9338. +}
  9339. +
  9340. +/****************************************************************************
  9341. +*
  9342. +* vc_cma_connected_init
  9343. +*
  9344. +* This function is called once the videocore has been connected.
  9345. +*
  9346. +***************************************************************************/
  9347. +
  9348. +static void vc_cma_connected_init(void)
  9349. +{
  9350. + VCHIQ_SERVICE_PARAMS_T service_params;
  9351. +
  9352. + LOG_DBG("vc_cma_connected_init");
  9353. +
  9354. + if (!vchiu_queue_init(&cma_msg_queue, 16)) {
  9355. + LOG_ERR("could not create CMA msg queue");
  9356. + goto fail_queue;
  9357. + }
  9358. +
  9359. + if (vchiq_initialise(&cma_instance) != VCHIQ_SUCCESS)
  9360. + goto fail_vchiq_init;
  9361. +
  9362. + vchiq_connect(cma_instance);
  9363. +
  9364. + service_params.fourcc = VC_CMA_FOURCC;
  9365. + service_params.callback = cma_service_callback;
  9366. + service_params.userdata = NULL;
  9367. + service_params.version = VC_CMA_VERSION;
  9368. + service_params.version_min = VC_CMA_VERSION;
  9369. +
  9370. + if (vchiq_open_service(cma_instance, &service_params,
  9371. + &cma_service) != VCHIQ_SUCCESS) {
  9372. + LOG_ERR("failed to open service - already in use?");
  9373. + goto fail_vchiq_open;
  9374. + }
  9375. +
  9376. + vchiq_release_service(cma_service);
  9377. +
  9378. + cma_worker = kthread_create(cma_worker_proc, NULL, "cma_worker");
  9379. + if (!cma_worker) {
  9380. + LOG_ERR("could not create CMA worker thread");
  9381. + goto fail_worker;
  9382. + }
  9383. + set_user_nice(cma_worker, -20);
  9384. + wake_up_process(cma_worker);
  9385. +
  9386. + return;
  9387. +
  9388. +fail_worker:
  9389. + vchiq_close_service(cma_service);
  9390. +fail_vchiq_open:
  9391. + vchiq_shutdown(cma_instance);
  9392. +fail_vchiq_init:
  9393. + vchiu_queue_delete(&cma_msg_queue);
  9394. +fail_queue:
  9395. + return;
  9396. +}
  9397. +
  9398. +void
  9399. +loud_error_header(void)
  9400. +{
  9401. + if (in_loud_error)
  9402. + return;
  9403. +
  9404. + LOG_ERR("============================================================"
  9405. + "================");
  9406. + LOG_ERR("============================================================"
  9407. + "================");
  9408. + LOG_ERR("=====");
  9409. +
  9410. + in_loud_error = 1;
  9411. +}
  9412. +
  9413. +void
  9414. +loud_error_footer(void)
  9415. +{
  9416. + if (!in_loud_error)
  9417. + return;
  9418. +
  9419. + LOG_ERR("=====");
  9420. + LOG_ERR("============================================================"
  9421. + "================");
  9422. + LOG_ERR("============================================================"
  9423. + "================");
  9424. +
  9425. + in_loud_error = 0;
  9426. +}
  9427. +
  9428. +#if 1
  9429. +static int check_cma_config(void) { return 1; }
  9430. +#else
  9431. +static int
  9432. +read_vc_debug_var(VC_MEM_ACCESS_HANDLE_T handle,
  9433. + const char *symbol,
  9434. + void *buf, size_t bufsize)
  9435. +{
  9436. + VC_MEM_ADDR_T vcMemAddr;
  9437. + size_t vcMemSize;
  9438. + uint8_t *mapAddr;
  9439. + off_t vcMapAddr;
  9440. +
  9441. + if (!LookupVideoCoreSymbol(handle, symbol,
  9442. + &vcMemAddr,
  9443. + &vcMemSize)) {
  9444. + loud_error_header();
  9445. + loud_error(
  9446. + "failed to find VC symbol \"%s\".",
  9447. + symbol);
  9448. + loud_error_footer();
  9449. + return 0;
  9450. + }
  9451. +
  9452. + if (vcMemSize != bufsize) {
  9453. + loud_error_header();
  9454. + loud_error(
  9455. + "VC symbol \"%s\" is the wrong size.",
  9456. + symbol);
  9457. + loud_error_footer();
  9458. + return 0;
  9459. + }
  9460. +
  9461. + vcMapAddr = (off_t)vcMemAddr & VC_MEM_TO_ARM_ADDR_MASK;
  9462. + vcMapAddr += mm_vc_mem_phys_addr;
  9463. + mapAddr = ioremap_nocache(vcMapAddr, vcMemSize);
  9464. + if (mapAddr == 0) {
  9465. + loud_error_header();
  9466. + loud_error(
  9467. + "failed to ioremap \"%s\" @ 0x%x "
  9468. + "(phys: 0x%x, size: %u).",
  9469. + symbol,
  9470. + (unsigned int)vcMapAddr,
  9471. + (unsigned int)vcMemAddr,
  9472. + (unsigned int)vcMemSize);
  9473. + loud_error_footer();
  9474. + return 0;
  9475. + }
  9476. +
  9477. + memcpy(buf, mapAddr, bufsize);
  9478. + iounmap(mapAddr);
  9479. +
  9480. + return 1;
  9481. +}
  9482. +
  9483. +
  9484. +static int
  9485. +check_cma_config(void)
  9486. +{
  9487. + VC_MEM_ACCESS_HANDLE_T mem_hndl;
  9488. + VC_MEM_ADDR_T mempool_start;
  9489. + VC_MEM_ADDR_T mempool_end;
  9490. + VC_MEM_ADDR_T mempool_offline_start;
  9491. + VC_MEM_ADDR_T mempool_offline_end;
  9492. + VC_MEM_ADDR_T cam_alloc_base;
  9493. + VC_MEM_ADDR_T cam_alloc_size;
  9494. + VC_MEM_ADDR_T cam_alloc_end;
  9495. + int success = 0;
  9496. +
  9497. + if (OpenVideoCoreMemory(&mem_hndl) != 0)
  9498. + goto out;
  9499. +
  9500. + /* Read the relevant VideoCore variables */
  9501. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_START",
  9502. + &mempool_start,
  9503. + sizeof(mempool_start)))
  9504. + goto close;
  9505. +
  9506. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_END",
  9507. + &mempool_end,
  9508. + sizeof(mempool_end)))
  9509. + goto close;
  9510. +
  9511. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_START",
  9512. + &mempool_offline_start,
  9513. + sizeof(mempool_offline_start)))
  9514. + goto close;
  9515. +
  9516. + if (!read_vc_debug_var(mem_hndl, "__MEMPOOL_OFFLINE_END",
  9517. + &mempool_offline_end,
  9518. + sizeof(mempool_offline_end)))
  9519. + goto close;
  9520. +
  9521. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_base",
  9522. + &cam_alloc_base,
  9523. + sizeof(cam_alloc_base)))
  9524. + goto close;
  9525. +
  9526. + if (!read_vc_debug_var(mem_hndl, "cam_alloc_size",
  9527. + &cam_alloc_size,
  9528. + sizeof(cam_alloc_size)))
  9529. + goto close;
  9530. +
  9531. + cam_alloc_end = cam_alloc_base + cam_alloc_size;
  9532. +
  9533. + success = 1;
  9534. +
  9535. + /* Now the sanity checks */
  9536. + if (!mempool_offline_start)
  9537. + mempool_offline_start = mempool_start;
  9538. + if (!mempool_offline_end)
  9539. + mempool_offline_end = mempool_end;
  9540. +
  9541. + if (VCADDR_TO_PHYSADDR(mempool_offline_start) != vc_cma_base) {
  9542. + loud_error_header();
  9543. + loud_error(
  9544. + "__MEMPOOL_OFFLINE_START(%x -> %lx) doesn't match "
  9545. + "vc_cma_base(%x)",
  9546. + mempool_offline_start,
  9547. + VCADDR_TO_PHYSADDR(mempool_offline_start),
  9548. + vc_cma_base);
  9549. + success = 0;
  9550. + }
  9551. +
  9552. + if (VCADDR_TO_PHYSADDR(mempool_offline_end) !=
  9553. + (vc_cma_base + vc_cma_size)) {
  9554. + loud_error_header();
  9555. + loud_error(
  9556. + "__MEMPOOL_OFFLINE_END(%x -> %lx) doesn't match "
  9557. + "vc_cma_base(%x) + vc_cma_size(%x) = %x",
  9558. + mempool_offline_start,
  9559. + VCADDR_TO_PHYSADDR(mempool_offline_end),
  9560. + vc_cma_base, vc_cma_size, vc_cma_base + vc_cma_size);
  9561. + success = 0;
  9562. + }
  9563. +
  9564. + if (mempool_end < mempool_start) {
  9565. + loud_error_header();
  9566. + loud_error(
  9567. + "__MEMPOOL_END(%x) must not be before "
  9568. + "__MEMPOOL_START(%x)",
  9569. + mempool_end,
  9570. + mempool_start);
  9571. + success = 0;
  9572. + }
  9573. +
  9574. + if (mempool_offline_end < mempool_offline_start) {
  9575. + loud_error_header();
  9576. + loud_error(
  9577. + "__MEMPOOL_OFFLINE_END(%x) must not be before "
  9578. + "__MEMPOOL_OFFLINE_START(%x)",
  9579. + mempool_offline_end,
  9580. + mempool_offline_start);
  9581. + success = 0;
  9582. + }
  9583. +
  9584. + if (mempool_offline_start < mempool_start) {
  9585. + loud_error_header();
  9586. + loud_error(
  9587. + "__MEMPOOL_OFFLINE_START(%x) must not be before "
  9588. + "__MEMPOOL_START(%x)",
  9589. + mempool_offline_start,
  9590. + mempool_start);
  9591. + success = 0;
  9592. + }
  9593. +
  9594. + if (mempool_offline_end > mempool_end) {
  9595. + loud_error_header();
  9596. + loud_error(
  9597. + "__MEMPOOL_OFFLINE_END(%x) must not be after "
  9598. + "__MEMPOOL_END(%x)",
  9599. + mempool_offline_end,
  9600. + mempool_end);
  9601. + success = 0;
  9602. + }
  9603. +
  9604. + if ((cam_alloc_base < mempool_end) &&
  9605. + (cam_alloc_end > mempool_start)) {
  9606. + loud_error_header();
  9607. + loud_error(
  9608. + "cam_alloc pool(%x-%x) overlaps "
  9609. + "mempool(%x-%x)",
  9610. + cam_alloc_base, cam_alloc_end,
  9611. + mempool_start, mempool_end);
  9612. + success = 0;
  9613. + }
  9614. +
  9615. + loud_error_footer();
  9616. +
  9617. +close:
  9618. + CloseVideoCoreMemory(mem_hndl);
  9619. +
  9620. +out:
  9621. + return success;
  9622. +}
  9623. +#endif
  9624. +
  9625. +static int vc_cma_init(void)
  9626. +{
  9627. + int rc = -EFAULT;
  9628. + struct device *dev;
  9629. +
  9630. + if (!check_cma_config())
  9631. + goto out_release;
  9632. +
  9633. + printk(KERN_INFO "vc-cma: Videocore CMA driver\n");
  9634. + printk(KERN_INFO "vc-cma: vc_cma_base = 0x%08x\n", vc_cma_base);
  9635. + printk(KERN_INFO "vc-cma: vc_cma_size = 0x%08x (%u MiB)\n",
  9636. + vc_cma_size, vc_cma_size / (1024 * 1024));
  9637. + printk(KERN_INFO "vc-cma: vc_cma_initial = 0x%08x (%u MiB)\n",
  9638. + vc_cma_initial, vc_cma_initial / (1024 * 1024));
  9639. +
  9640. + vc_cma_base_page = phys_to_page(vc_cma_base);
  9641. +
  9642. + if (vc_cma_chunks) {
  9643. + int chunks_needed = vc_cma_initial / VC_CMA_CHUNK_SIZE;
  9644. +
  9645. + for (vc_cma_chunks_used = 0;
  9646. + vc_cma_chunks_used < chunks_needed; vc_cma_chunks_used++) {
  9647. + struct page *chunk;
  9648. + chunk = dma_alloc_from_contiguous(NULL /*&vc_cma_device.dev*/,
  9649. + PAGES_PER_CHUNK,
  9650. + VC_CMA_CHUNK_ORDER);
  9651. + if (!chunk)
  9652. + break;
  9653. + BUG_ON(((page_to_phys(chunk) - vc_cma_base) %
  9654. + VC_CMA_CHUNK_SIZE) != 0);
  9655. + }
  9656. + if (vc_cma_chunks_used != chunks_needed) {
  9657. + LOG_ERR("%s: dma_alloc_from_contiguous failed (%d "
  9658. + "bytes, allocation %d of %d)",
  9659. + __func__, VC_CMA_CHUNK_SIZE,
  9660. + vc_cma_chunks_used, chunks_needed);
  9661. + goto out_release;
  9662. + }
  9663. +
  9664. + vchiq_add_connected_callback(vc_cma_connected_init);
  9665. + }
  9666. +
  9667. + rc = alloc_chrdev_region(&vc_cma_devnum, 0, 1, DRIVER_NAME);
  9668. + if (rc < 0) {
  9669. + LOG_ERR("%s: alloc_chrdev_region failed (rc=%d)", __func__, rc);
  9670. + goto out_release;
  9671. + }
  9672. +
  9673. + cdev_init(&vc_cma_cdev, &vc_cma_fops);
  9674. + rc = cdev_add(&vc_cma_cdev, vc_cma_devnum, 1);
  9675. + if (rc != 0) {
  9676. + LOG_ERR("%s: cdev_add failed (rc=%d)", __func__, rc);
  9677. + goto out_unregister;
  9678. + }
  9679. +
  9680. + vc_cma_class = class_create(THIS_MODULE, DRIVER_NAME);
  9681. + if (IS_ERR(vc_cma_class)) {
  9682. + rc = PTR_ERR(vc_cma_class);
  9683. + LOG_ERR("%s: class_create failed (rc=%d)", __func__, rc);
  9684. + goto out_cdev_del;
  9685. + }
  9686. +
  9687. + dev = device_create(vc_cma_class, NULL, vc_cma_devnum, NULL,
  9688. + DRIVER_NAME);
  9689. + if (IS_ERR(dev)) {
  9690. + rc = PTR_ERR(dev);
  9691. + LOG_ERR("%s: device_create failed (rc=%d)", __func__, rc);
  9692. + goto out_class_destroy;
  9693. + }
  9694. +
  9695. + vc_cma_proc_entry = proc_create(DRIVER_NAME, 0444, NULL, &vc_cma_proc_fops);
  9696. + if (vc_cma_proc_entry == NULL) {
  9697. + rc = -EFAULT;
  9698. + LOG_ERR("%s: proc_create failed", __func__);
  9699. + goto out_device_destroy;
  9700. + }
  9701. +
  9702. + vc_cma_inited = 1;
  9703. + return 0;
  9704. +
  9705. +out_device_destroy:
  9706. + device_destroy(vc_cma_class, vc_cma_devnum);
  9707. +
  9708. +out_class_destroy:
  9709. + class_destroy(vc_cma_class);
  9710. + vc_cma_class = NULL;
  9711. +
  9712. +out_cdev_del:
  9713. + cdev_del(&vc_cma_cdev);
  9714. +
  9715. +out_unregister:
  9716. + unregister_chrdev_region(vc_cma_devnum, 1);
  9717. +
  9718. +out_release:
  9719. + /* It is tempting to try to clean up by calling
  9720. + dma_release_from_contiguous for all allocated chunks, but it isn't
  9721. + a very safe thing to do. If vc_cma_initial is non-zero it is because
  9722. + VideoCore is already using that memory, so giving it back to Linux
  9723. + is likely to be fatal.
  9724. + */
  9725. + return -1;
  9726. +}
  9727. +
  9728. +/****************************************************************************
  9729. +*
  9730. +* vc_cma_exit
  9731. +*
  9732. +***************************************************************************/
  9733. +
  9734. +static void __exit vc_cma_exit(void)
  9735. +{
  9736. + LOG_DBG("%s: called", __func__);
  9737. +
  9738. + if (vc_cma_inited) {
  9739. + remove_proc_entry(DRIVER_NAME, NULL);
  9740. + device_destroy(vc_cma_class, vc_cma_devnum);
  9741. + class_destroy(vc_cma_class);
  9742. + cdev_del(&vc_cma_cdev);
  9743. + unregister_chrdev_region(vc_cma_devnum, 1);
  9744. + }
  9745. +}
  9746. +
  9747. +module_init(vc_cma_init);
  9748. +module_exit(vc_cma_exit);
  9749. +MODULE_LICENSE("GPL");
  9750. +MODULE_AUTHOR("Broadcom Corporation");
  9751. diff -Nur linux-3.16.2/drivers/char/broadcom/vc_sm/Makefile linux-3.16-rpi/drivers/char/broadcom/vc_sm/Makefile
  9752. --- linux-3.16.2/drivers/char/broadcom/vc_sm/Makefile 1970-01-01 01:00:00.000000000 +0100
  9753. +++ linux-3.16-rpi/drivers/char/broadcom/vc_sm/Makefile 2014-09-14 19:03:14.000000000 +0200
  9754. @@ -0,0 +1,21 @@
  9755. +EXTRA_CFLAGS += -Wall -Wstrict-prototypes -Wno-trigraphs -O2
  9756. +
  9757. +EXTRA_CFLAGS += -I"./arch/arm/mach-bcm2708/include/mach"
  9758. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services"
  9759. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchi"
  9760. +EXTRA_CFLAGS += -I"drivers/misc/vc04_services/interface/vchiq_arm"
  9761. +EXTRA_CFLAGS += -I"$(srctree)/fs/"
  9762. +
  9763. +EXTRA_CFLAGS += -DOS_ASSERT_FAILURE
  9764. +EXTRA_CFLAGS += -D__STDC_VERSION=199901L
  9765. +EXTRA_CFLAGS += -D__STDC_VERSION__=199901L
  9766. +EXTRA_CFLAGS += -D__VCCOREVER__=0
  9767. +EXTRA_CFLAGS += -D__KERNEL__
  9768. +EXTRA_CFLAGS += -D__linux__
  9769. +EXTRA_CFLAGS += -Werror
  9770. +
  9771. +obj-$(CONFIG_BCM_VC_SM) := vc-sm.o
  9772. +
  9773. +vc-sm-objs := \
  9774. + vmcs_sm.o \
  9775. + vc_vchi_sm.o
  9776. diff -Nur linux-3.16.2/drivers/char/broadcom/vc_sm/vc_vchi_sm.c linux-3.16-rpi/drivers/char/broadcom/vc_sm/vc_vchi_sm.c
  9777. --- linux-3.16.2/drivers/char/broadcom/vc_sm/vc_vchi_sm.c 1970-01-01 01:00:00.000000000 +0100
  9778. +++ linux-3.16-rpi/drivers/char/broadcom/vc_sm/vc_vchi_sm.c 2014-09-14 19:03:14.000000000 +0200
  9779. @@ -0,0 +1,492 @@
  9780. +/*****************************************************************************
  9781. +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
  9782. +*
  9783. +* Unless you and Broadcom execute a separate written software license
  9784. +* agreement governing use of this software, this software is licensed to you
  9785. +* under the terms of the GNU General Public License version 2, available at
  9786. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  9787. +*
  9788. +* Notwithstanding the above, under no circumstances may you combine this
  9789. +* software in any way with any other Broadcom software provided under a
  9790. +* license other than the GPL, without Broadcom's express prior written
  9791. +* consent.
  9792. +*****************************************************************************/
  9793. +
  9794. +/* ---- Include Files ----------------------------------------------------- */
  9795. +#include <linux/types.h>
  9796. +#include <linux/kernel.h>
  9797. +#include <linux/list.h>
  9798. +#include <linux/semaphore.h>
  9799. +#include <linux/mutex.h>
  9800. +#include <linux/slab.h>
  9801. +#include <linux/kthread.h>
  9802. +
  9803. +#include "vc_vchi_sm.h"
  9804. +
  9805. +#define VC_SM_VER 1
  9806. +#define VC_SM_MIN_VER 0
  9807. +
  9808. +/* ---- Private Constants and Types -------------------------------------- */
  9809. +
  9810. +/* Command blocks come from a pool */
  9811. +#define SM_MAX_NUM_CMD_RSP_BLKS 32
  9812. +
  9813. +struct sm_cmd_rsp_blk {
  9814. + struct list_head head; /* To create lists */
  9815. + struct semaphore sema; /* To be signaled when the response is there */
  9816. +
  9817. + uint16_t id;
  9818. + uint16_t length;
  9819. +
  9820. + uint8_t msg[VC_SM_MAX_MSG_LEN];
  9821. +
  9822. + uint32_t wait:1;
  9823. + uint32_t sent:1;
  9824. + uint32_t alloc:1;
  9825. +
  9826. +};
  9827. +
  9828. +struct sm_instance {
  9829. + uint32_t num_connections;
  9830. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  9831. + struct task_struct *io_thread;
  9832. + struct semaphore io_sema;
  9833. +
  9834. + uint32_t trans_id;
  9835. +
  9836. + struct mutex lock;
  9837. + struct list_head cmd_list;
  9838. + struct list_head rsp_list;
  9839. + struct list_head dead_list;
  9840. +
  9841. + struct sm_cmd_rsp_blk free_blk[SM_MAX_NUM_CMD_RSP_BLKS];
  9842. + struct list_head free_list;
  9843. + struct mutex free_lock;
  9844. + struct semaphore free_sema;
  9845. +
  9846. +};
  9847. +
  9848. +/* ---- Private Variables ------------------------------------------------ */
  9849. +
  9850. +/* ---- Private Function Prototypes -------------------------------------- */
  9851. +
  9852. +/* ---- Private Functions ------------------------------------------------ */
  9853. +static struct
  9854. +sm_cmd_rsp_blk *vc_vchi_cmd_create(struct sm_instance *instance,
  9855. + VC_SM_MSG_TYPE id, void *msg,
  9856. + uint32_t size, int wait)
  9857. +{
  9858. + struct sm_cmd_rsp_blk *blk;
  9859. + VC_SM_MSG_HDR_T *hdr;
  9860. +
  9861. + if (down_interruptible(&instance->free_sema)) {
  9862. + blk = kmalloc(sizeof(*blk), GFP_KERNEL);
  9863. + if (!blk)
  9864. + return NULL;
  9865. +
  9866. + blk->alloc = 1;
  9867. + sema_init(&blk->sema, 0);
  9868. + } else {
  9869. + mutex_lock(&instance->free_lock);
  9870. + blk =
  9871. + list_first_entry(&instance->free_list,
  9872. + struct sm_cmd_rsp_blk, head);
  9873. + list_del(&blk->head);
  9874. + mutex_unlock(&instance->free_lock);
  9875. + }
  9876. +
  9877. + blk->sent = 0;
  9878. + blk->wait = wait;
  9879. + blk->length = sizeof(*hdr) + size;
  9880. +
  9881. + hdr = (VC_SM_MSG_HDR_T *) blk->msg;
  9882. + hdr->type = id;
  9883. + mutex_lock(&instance->lock);
  9884. + hdr->trans_id = blk->id = ++instance->trans_id;
  9885. + mutex_unlock(&instance->lock);
  9886. +
  9887. + if (size)
  9888. + memcpy(hdr->body, msg, size);
  9889. +
  9890. + return blk;
  9891. +}
  9892. +
  9893. +static void
  9894. +vc_vchi_cmd_delete(struct sm_instance *instance, struct sm_cmd_rsp_blk *blk)
  9895. +{
  9896. + if (blk->alloc) {
  9897. + kfree(blk);
  9898. + return;
  9899. + }
  9900. +
  9901. + mutex_lock(&instance->free_lock);
  9902. + list_add(&blk->head, &instance->free_list);
  9903. + mutex_unlock(&instance->free_lock);
  9904. + up(&instance->free_sema);
  9905. +}
  9906. +
  9907. +static int vc_vchi_sm_videocore_io(void *arg)
  9908. +{
  9909. + struct sm_instance *instance = arg;
  9910. + struct sm_cmd_rsp_blk *cmd = NULL, *cmd_tmp;
  9911. + VC_SM_RESULT_T *reply;
  9912. + uint32_t reply_len;
  9913. + int32_t status;
  9914. + int svc_use = 1;
  9915. +
  9916. + while (1) {
  9917. + if (svc_use)
  9918. + vchi_service_release(instance->vchi_handle[0]);
  9919. + svc_use = 0;
  9920. + if (!down_interruptible(&instance->io_sema)) {
  9921. + vchi_service_use(instance->vchi_handle[0]);
  9922. + svc_use = 1;
  9923. +
  9924. + do {
  9925. + unsigned int flags;
  9926. + /*
  9927. + * Get new command and move it to response list
  9928. + */
  9929. + mutex_lock(&instance->lock);
  9930. + if (list_empty(&instance->cmd_list)) {
  9931. + /* no more commands to process */
  9932. + mutex_unlock(&instance->lock);
  9933. + break;
  9934. + }
  9935. + cmd =
  9936. + list_first_entry(&instance->cmd_list,
  9937. + struct sm_cmd_rsp_blk,
  9938. + head);
  9939. + list_move(&cmd->head, &instance->rsp_list);
  9940. + cmd->sent = 1;
  9941. + mutex_unlock(&instance->lock);
  9942. +
  9943. + /* Send the command */
  9944. + flags = VCHI_FLAGS_BLOCK_UNTIL_QUEUED;
  9945. + status = vchi_msg_queue(
  9946. + instance->vchi_handle[0],
  9947. + cmd->msg, cmd->length,
  9948. + flags, NULL);
  9949. + if (status) {
  9950. + pr_err("%s: failed to queue message (%d)",
  9951. + __func__, status);
  9952. + }
  9953. +
  9954. + /* If no reply is needed then we're done */
  9955. + if (!cmd->wait) {
  9956. + mutex_lock(&instance->lock);
  9957. + list_del(&cmd->head);
  9958. + mutex_unlock(&instance->lock);
  9959. + vc_vchi_cmd_delete(instance, cmd);
  9960. + continue;
  9961. + }
  9962. +
  9963. + if (status) {
  9964. + up(&cmd->sema);
  9965. + continue;
  9966. + }
  9967. +
  9968. + } while (1);
  9969. +
  9970. + while (!vchi_msg_peek
  9971. + (instance->vchi_handle[0], (void **)&reply,
  9972. + &reply_len, VCHI_FLAGS_NONE)) {
  9973. + mutex_lock(&instance->lock);
  9974. + list_for_each_entry(cmd, &instance->rsp_list,
  9975. + head) {
  9976. + if (cmd->id == reply->trans_id)
  9977. + break;
  9978. + }
  9979. + mutex_unlock(&instance->lock);
  9980. +
  9981. + if (&cmd->head == &instance->rsp_list) {
  9982. + pr_debug("%s: received response %u, throw away...",
  9983. + __func__, reply->trans_id);
  9984. + } else if (reply_len > sizeof(cmd->msg)) {
  9985. + pr_err("%s: reply too big (%u) %u, throw away...",
  9986. + __func__, reply_len,
  9987. + reply->trans_id);
  9988. + } else {
  9989. + memcpy(cmd->msg, reply, reply_len);
  9990. + up(&cmd->sema);
  9991. + }
  9992. +
  9993. + vchi_msg_remove(instance->vchi_handle[0]);
  9994. + }
  9995. +
  9996. + /* Go through the dead list and free them */
  9997. + mutex_lock(&instance->lock);
  9998. + list_for_each_entry_safe(cmd, cmd_tmp,
  9999. + &instance->dead_list, head) {
  10000. + list_del(&cmd->head);
  10001. + vc_vchi_cmd_delete(instance, cmd);
  10002. + }
  10003. + mutex_unlock(&instance->lock);
  10004. + }
  10005. + }
  10006. +
  10007. + return 0;
  10008. +}
  10009. +
  10010. +static void vc_sm_vchi_callback(void *param,
  10011. + const VCHI_CALLBACK_REASON_T reason,
  10012. + void *msg_handle)
  10013. +{
  10014. + struct sm_instance *instance = param;
  10015. +
  10016. + (void)msg_handle;
  10017. +
  10018. + switch (reason) {
  10019. + case VCHI_CALLBACK_MSG_AVAILABLE:
  10020. + up(&instance->io_sema);
  10021. + break;
  10022. +
  10023. + case VCHI_CALLBACK_SERVICE_CLOSED:
  10024. + pr_info("%s: service CLOSED!!", __func__);
  10025. + default:
  10026. + break;
  10027. + }
  10028. +}
  10029. +
  10030. +VC_VCHI_SM_HANDLE_T vc_vchi_sm_init(VCHI_INSTANCE_T vchi_instance,
  10031. + VCHI_CONNECTION_T **vchi_connections,
  10032. + uint32_t num_connections)
  10033. +{
  10034. + uint32_t i;
  10035. + struct sm_instance *instance;
  10036. + int status;
  10037. +
  10038. + pr_debug("%s: start", __func__);
  10039. +
  10040. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  10041. + pr_err("%s: unsupported number of connections %u (max=%u)",
  10042. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  10043. +
  10044. + goto err_null;
  10045. + }
  10046. + /* Allocate memory for this instance */
  10047. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  10048. +
  10049. + /* Misc initialisations */
  10050. + mutex_init(&instance->lock);
  10051. + sema_init(&instance->io_sema, 0);
  10052. + INIT_LIST_HEAD(&instance->cmd_list);
  10053. + INIT_LIST_HEAD(&instance->rsp_list);
  10054. + INIT_LIST_HEAD(&instance->dead_list);
  10055. + INIT_LIST_HEAD(&instance->free_list);
  10056. + sema_init(&instance->free_sema, SM_MAX_NUM_CMD_RSP_BLKS);
  10057. + mutex_init(&instance->free_lock);
  10058. + for (i = 0; i < SM_MAX_NUM_CMD_RSP_BLKS; i++) {
  10059. + sema_init(&instance->free_blk[i].sema, 0);
  10060. + list_add(&instance->free_blk[i].head, &instance->free_list);
  10061. + }
  10062. +
  10063. + /* Open the VCHI service connections */
  10064. + instance->num_connections = num_connections;
  10065. + for (i = 0; i < num_connections; i++) {
  10066. + SERVICE_CREATION_T params = {
  10067. + VCHI_VERSION_EX(VC_SM_VER, VC_SM_MIN_VER),
  10068. + VC_SM_SERVER_NAME,
  10069. + vchi_connections[i],
  10070. + 0,
  10071. + 0,
  10072. + vc_sm_vchi_callback,
  10073. + instance,
  10074. + 0,
  10075. + 0,
  10076. + 0,
  10077. + };
  10078. +
  10079. + status = vchi_service_open(vchi_instance,
  10080. + &params, &instance->vchi_handle[i]);
  10081. + if (status) {
  10082. + pr_err("%s: failed to open VCHI service (%d)",
  10083. + __func__, status);
  10084. +
  10085. + goto err_close_services;
  10086. + }
  10087. + }
  10088. +
  10089. + /* Create the thread which takes care of all io to/from videoocore. */
  10090. + instance->io_thread = kthread_create(&vc_vchi_sm_videocore_io,
  10091. + (void *)instance, "SMIO");
  10092. + if (instance->io_thread == NULL) {
  10093. + pr_err("%s: failed to create SMIO thread", __func__);
  10094. +
  10095. + goto err_close_services;
  10096. + }
  10097. + set_user_nice(instance->io_thread, -10);
  10098. + wake_up_process(instance->io_thread);
  10099. +
  10100. + pr_debug("%s: success - instance 0x%x", __func__, (unsigned)instance);
  10101. + return instance;
  10102. +
  10103. +err_close_services:
  10104. + for (i = 0; i < instance->num_connections; i++) {
  10105. + if (instance->vchi_handle[i] != NULL)
  10106. + vchi_service_close(instance->vchi_handle[i]);
  10107. + }
  10108. + kfree(instance);
  10109. +err_null:
  10110. + pr_debug("%s: FAILED", __func__);
  10111. + return NULL;
  10112. +}
  10113. +
  10114. +int vc_vchi_sm_stop(VC_VCHI_SM_HANDLE_T *handle)
  10115. +{
  10116. + struct sm_instance *instance;
  10117. + uint32_t i;
  10118. +
  10119. + if (handle == NULL) {
  10120. + pr_err("%s: invalid pointer to handle %p", __func__, handle);
  10121. + goto lock;
  10122. + }
  10123. +
  10124. + if (*handle == NULL) {
  10125. + pr_err("%s: invalid handle %p", __func__, *handle);
  10126. + goto lock;
  10127. + }
  10128. +
  10129. + instance = *handle;
  10130. +
  10131. + /* Close all VCHI service connections */
  10132. + for (i = 0; i < instance->num_connections; i++) {
  10133. + int32_t success;
  10134. + vchi_service_use(instance->vchi_handle[i]);
  10135. +
  10136. + success = vchi_service_close(instance->vchi_handle[i]);
  10137. + }
  10138. +
  10139. + kfree(instance);
  10140. +
  10141. + *handle = NULL;
  10142. + return 0;
  10143. +
  10144. +lock:
  10145. + return -EINVAL;
  10146. +}
  10147. +
  10148. +int vc_vchi_sm_send_msg(VC_VCHI_SM_HANDLE_T handle,
  10149. + VC_SM_MSG_TYPE msg_id,
  10150. + void *msg, uint32_t msg_size,
  10151. + void *result, uint32_t result_size,
  10152. + uint32_t *cur_trans_id, uint8_t wait_reply)
  10153. +{
  10154. + int status = 0;
  10155. + struct sm_instance *instance = handle;
  10156. + struct sm_cmd_rsp_blk *cmd_blk;
  10157. +
  10158. + if (handle == NULL) {
  10159. + pr_err("%s: invalid handle", __func__);
  10160. + return -EINVAL;
  10161. + }
  10162. + if (msg == NULL) {
  10163. + pr_err("%s: invalid msg pointer", __func__);
  10164. + return -EINVAL;
  10165. + }
  10166. +
  10167. + cmd_blk =
  10168. + vc_vchi_cmd_create(instance, msg_id, msg, msg_size, wait_reply);
  10169. + if (cmd_blk == NULL) {
  10170. + pr_err("[%s]: failed to allocate global tracking resource",
  10171. + __func__);
  10172. + return -ENOMEM;
  10173. + }
  10174. +
  10175. + if (cur_trans_id != NULL)
  10176. + *cur_trans_id = cmd_blk->id;
  10177. +
  10178. + mutex_lock(&instance->lock);
  10179. + list_add_tail(&cmd_blk->head, &instance->cmd_list);
  10180. + mutex_unlock(&instance->lock);
  10181. + up(&instance->io_sema);
  10182. +
  10183. + if (!wait_reply)
  10184. + /* We're done */
  10185. + return 0;
  10186. +
  10187. + /* Wait for the response */
  10188. + if (down_interruptible(&cmd_blk->sema)) {
  10189. + mutex_lock(&instance->lock);
  10190. + if (!cmd_blk->sent) {
  10191. + list_del(&cmd_blk->head);
  10192. + mutex_unlock(&instance->lock);
  10193. + vc_vchi_cmd_delete(instance, cmd_blk);
  10194. + return -ENXIO;
  10195. + }
  10196. + mutex_unlock(&instance->lock);
  10197. +
  10198. + mutex_lock(&instance->lock);
  10199. + list_move(&cmd_blk->head, &instance->dead_list);
  10200. + mutex_unlock(&instance->lock);
  10201. + up(&instance->io_sema);
  10202. + return -EINTR; /* We're done */
  10203. + }
  10204. +
  10205. + if (result && result_size) {
  10206. + memcpy(result, cmd_blk->msg, result_size);
  10207. + } else {
  10208. + VC_SM_RESULT_T *res = (VC_SM_RESULT_T *) cmd_blk->msg;
  10209. + status = (res->success == 0) ? 0 : -ENXIO;
  10210. + }
  10211. +
  10212. + mutex_lock(&instance->lock);
  10213. + list_del(&cmd_blk->head);
  10214. + mutex_unlock(&instance->lock);
  10215. + vc_vchi_cmd_delete(instance, cmd_blk);
  10216. + return status;
  10217. +}
  10218. +
  10219. +int vc_vchi_sm_alloc(VC_VCHI_SM_HANDLE_T handle, VC_SM_ALLOC_T *msg,
  10220. + VC_SM_ALLOC_RESULT_T *result, uint32_t *cur_trans_id)
  10221. +{
  10222. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ALLOC,
  10223. + msg, sizeof(*msg), result, sizeof(*result),
  10224. + cur_trans_id, 1);
  10225. +}
  10226. +
  10227. +int vc_vchi_sm_free(VC_VCHI_SM_HANDLE_T handle,
  10228. + VC_SM_FREE_T *msg, uint32_t *cur_trans_id)
  10229. +{
  10230. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_FREE,
  10231. + msg, sizeof(*msg), 0, 0, cur_trans_id, 0);
  10232. +}
  10233. +
  10234. +int vc_vchi_sm_lock(VC_VCHI_SM_HANDLE_T handle,
  10235. + VC_SM_LOCK_UNLOCK_T *msg,
  10236. + VC_SM_LOCK_RESULT_T *result, uint32_t *cur_trans_id)
  10237. +{
  10238. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_LOCK,
  10239. + msg, sizeof(*msg), result, sizeof(*result),
  10240. + cur_trans_id, 1);
  10241. +}
  10242. +
  10243. +int vc_vchi_sm_unlock(VC_VCHI_SM_HANDLE_T handle,
  10244. + VC_SM_LOCK_UNLOCK_T *msg,
  10245. + uint32_t *cur_trans_id, uint8_t wait_reply)
  10246. +{
  10247. + return vc_vchi_sm_send_msg(handle, wait_reply ?
  10248. + VC_SM_MSG_TYPE_UNLOCK :
  10249. + VC_SM_MSG_TYPE_UNLOCK_NOANS, msg,
  10250. + sizeof(*msg), 0, 0, cur_trans_id,
  10251. + wait_reply);
  10252. +}
  10253. +
  10254. +int vc_vchi_sm_resize(VC_VCHI_SM_HANDLE_T handle, VC_SM_RESIZE_T *msg,
  10255. + uint32_t *cur_trans_id)
  10256. +{
  10257. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_RESIZE,
  10258. + msg, sizeof(*msg), 0, 0, cur_trans_id, 1);
  10259. +}
  10260. +
  10261. +int vc_vchi_sm_walk_alloc(VC_VCHI_SM_HANDLE_T handle)
  10262. +{
  10263. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_WALK_ALLOC,
  10264. + 0, 0, 0, 0, 0, 0);
  10265. +}
  10266. +
  10267. +int vc_vchi_sm_clean_up(VC_VCHI_SM_HANDLE_T handle, VC_SM_ACTION_CLEAN_T *msg)
  10268. +{
  10269. + return vc_vchi_sm_send_msg(handle, VC_SM_MSG_TYPE_ACTION_CLEAN,
  10270. + msg, sizeof(*msg), 0, 0, 0, 0);
  10271. +}
  10272. diff -Nur linux-3.16.2/drivers/char/broadcom/vc_sm/vmcs_sm.c linux-3.16-rpi/drivers/char/broadcom/vc_sm/vmcs_sm.c
  10273. --- linux-3.16.2/drivers/char/broadcom/vc_sm/vmcs_sm.c 1970-01-01 01:00:00.000000000 +0100
  10274. +++ linux-3.16-rpi/drivers/char/broadcom/vc_sm/vmcs_sm.c 2014-09-14 19:03:14.000000000 +0200
  10275. @@ -0,0 +1,3163 @@
  10276. +/*****************************************************************************
  10277. +* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
  10278. +*
  10279. +* Unless you and Broadcom execute a separate written software license
  10280. +* agreement governing use of this software, this software is licensed to you
  10281. +* under the terms of the GNU General Public License version 2, available at
  10282. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  10283. +*
  10284. +* Notwithstanding the above, under no circumstances may you combine this
  10285. +* software in any way with any other Broadcom software provided under a
  10286. +* license other than the GPL, without Broadcom's express prior written
  10287. +* consent.
  10288. +*****************************************************************************/
  10289. +
  10290. +/* ---- Include Files ----------------------------------------------------- */
  10291. +
  10292. +#include <linux/cdev.h>
  10293. +#include <linux/device.h>
  10294. +#include <linux/debugfs.h>
  10295. +#include <linux/dma-mapping.h>
  10296. +#include <linux/errno.h>
  10297. +#include <linux/fs.h>
  10298. +#include <linux/hugetlb.h>
  10299. +#include <linux/ioctl.h>
  10300. +#include <linux/kernel.h>
  10301. +#include <linux/list.h>
  10302. +#include <linux/module.h>
  10303. +#include <linux/mm.h>
  10304. +#include <linux/pfn.h>
  10305. +#include <linux/proc_fs.h>
  10306. +#include <linux/pagemap.h>
  10307. +#include <linux/semaphore.h>
  10308. +#include <linux/slab.h>
  10309. +#include <linux/seq_file.h>
  10310. +#include <linux/types.h>
  10311. +#include <asm/cacheflush.h>
  10312. +
  10313. +#include <vc_mem.h>
  10314. +
  10315. +#include "vchiq_connected.h"
  10316. +#include "vc_vchi_sm.h"
  10317. +
  10318. +#include <vmcs_sm_ioctl.h>
  10319. +#include "vc_sm_knl.h"
  10320. +
  10321. +/* ---- Private Constants and Types --------------------------------------- */
  10322. +
  10323. +#define DEVICE_NAME "vcsm"
  10324. +#define DEVICE_MINOR 0
  10325. +
  10326. +#define VC_SM_DIR_ROOT_NAME "vc-smem"
  10327. +#define VC_SM_DIR_ALLOC_NAME "alloc"
  10328. +#define VC_SM_STATE "state"
  10329. +#define VC_SM_STATS "statistics"
  10330. +#define VC_SM_RESOURCES "resources"
  10331. +#define VC_SM_DEBUG "debug"
  10332. +#define VC_SM_WRITE_BUF_SIZE 128
  10333. +
  10334. +/* Statistics tracked per resource and globally.
  10335. +*/
  10336. +enum SM_STATS_T {
  10337. + /* Attempt. */
  10338. + ALLOC,
  10339. + FREE,
  10340. + LOCK,
  10341. + UNLOCK,
  10342. + MAP,
  10343. + FLUSH,
  10344. + INVALID,
  10345. +
  10346. + END_ATTEMPT,
  10347. +
  10348. + /* Failure. */
  10349. + ALLOC_FAIL,
  10350. + FREE_FAIL,
  10351. + LOCK_FAIL,
  10352. + UNLOCK_FAIL,
  10353. + MAP_FAIL,
  10354. + FLUSH_FAIL,
  10355. + INVALID_FAIL,
  10356. +
  10357. + END_ALL,
  10358. +
  10359. +};
  10360. +
  10361. +static const char *const sm_stats_human_read[] = {
  10362. + "Alloc",
  10363. + "Free",
  10364. + "Lock",
  10365. + "Unlock",
  10366. + "Map",
  10367. + "Cache Flush",
  10368. + "Cache Invalidate",
  10369. +};
  10370. +
  10371. +typedef int (*VC_SM_SHOW) (struct seq_file *s, void *v);
  10372. +struct SM_PDE_T {
  10373. + VC_SM_SHOW show; /* Debug fs function hookup. */
  10374. + struct dentry *dir_entry; /* Debug fs directory entry. */
  10375. + void *priv_data; /* Private data */
  10376. +
  10377. +};
  10378. +
  10379. +/* Single resource allocation tracked for all devices.
  10380. +*/
  10381. +struct sm_mmap {
  10382. + struct list_head map_list; /* Linked list of maps. */
  10383. +
  10384. + struct SM_RESOURCE_T *resource; /* Pointer to the resource. */
  10385. +
  10386. + pid_t res_pid; /* PID owning that resource. */
  10387. + unsigned int res_vc_hdl; /* Resource handle (videocore). */
  10388. + unsigned int res_usr_hdl; /* Resource handle (user). */
  10389. +
  10390. + long unsigned int res_addr; /* Mapped virtual address. */
  10391. + struct vm_area_struct *vma; /* VM area for this mapping. */
  10392. + unsigned int ref_count; /* Reference count to this vma. */
  10393. +
  10394. + /* Used to link maps associated with a resource. */
  10395. + struct list_head resource_map_list;
  10396. +};
  10397. +
  10398. +/* Single resource allocation tracked for each opened device.
  10399. +*/
  10400. +struct SM_RESOURCE_T {
  10401. + struct list_head resource_list; /* List of resources. */
  10402. + struct list_head global_resource_list; /* Global list of resources. */
  10403. +
  10404. + pid_t pid; /* PID owning that resource. */
  10405. + uint32_t res_guid; /* Unique identifier. */
  10406. + uint32_t lock_count; /* Lock count for this resource. */
  10407. + uint32_t ref_count; /* Ref count for this resource. */
  10408. +
  10409. + uint32_t res_handle; /* Resource allocation handle. */
  10410. + void *res_base_mem; /* Resource base memory address. */
  10411. + uint32_t res_size; /* Resource size allocated. */
  10412. + enum vmcs_sm_cache_e res_cached; /* Resource cache type. */
  10413. + struct SM_RESOURCE_T *res_shared; /* Shared resource */
  10414. +
  10415. + enum SM_STATS_T res_stats[END_ALL]; /* Resource statistics. */
  10416. +
  10417. + uint8_t map_count; /* Counter of mappings for this resource. */
  10418. + struct list_head map_list; /* Maps associated with a resource. */
  10419. +
  10420. + struct SM_PRIV_DATA_T *private;
  10421. +};
  10422. +
  10423. +/* Private file data associated with each opened device.
  10424. +*/
  10425. +struct SM_PRIV_DATA_T {
  10426. + struct list_head resource_list; /* List of resources. */
  10427. +
  10428. + pid_t pid; /* PID of creator. */
  10429. +
  10430. + struct dentry *dir_pid; /* Debug fs entries root. */
  10431. + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
  10432. + struct SM_PDE_T dir_res; /* Debug fs resource sub-tree. */
  10433. +
  10434. + int restart_sys; /* Tracks restart on interrupt. */
  10435. + VC_SM_MSG_TYPE int_action; /* Interrupted action. */
  10436. + uint32_t int_trans_id; /* Interrupted transaction. */
  10437. +
  10438. +};
  10439. +
  10440. +/* Global state information.
  10441. +*/
  10442. +struct SM_STATE_T {
  10443. + VC_VCHI_SM_HANDLE_T sm_handle; /* Handle for videocore service. */
  10444. + struct dentry *dir_root; /* Debug fs entries root. */
  10445. + struct dentry *dir_alloc; /* Debug fs entries allocations. */
  10446. + struct SM_PDE_T dir_stats; /* Debug fs entries statistics sub-tree. */
  10447. + struct SM_PDE_T dir_state; /* Debug fs entries state sub-tree. */
  10448. + struct dentry *debug; /* Debug fs entries debug. */
  10449. +
  10450. + struct mutex map_lock; /* Global map lock. */
  10451. + struct list_head map_list; /* List of maps. */
  10452. + struct list_head resource_list; /* List of resources. */
  10453. +
  10454. + enum SM_STATS_T deceased[END_ALL]; /* Natural termination stats. */
  10455. + enum SM_STATS_T terminated[END_ALL]; /* Forced termination stats. */
  10456. + uint32_t res_deceased_cnt; /* Natural termination counter. */
  10457. + uint32_t res_terminated_cnt; /* Forced termination counter. */
  10458. +
  10459. + struct cdev sm_cdev; /* Device. */
  10460. + dev_t sm_devid; /* Device identifier. */
  10461. + struct class *sm_class; /* Class. */
  10462. + struct device *sm_dev; /* Device. */
  10463. +
  10464. + struct SM_PRIV_DATA_T *data_knl; /* Kernel internal data tracking. */
  10465. +
  10466. + struct mutex lock; /* Global lock. */
  10467. + uint32_t guid; /* GUID (next) tracker. */
  10468. +
  10469. +};
  10470. +
  10471. +/* ---- Private Variables ----------------------------------------------- */
  10472. +
  10473. +static struct SM_STATE_T *sm_state;
  10474. +static int sm_inited;
  10475. +
  10476. +static const char *const sm_cache_map_vector[] = {
  10477. + "(null)",
  10478. + "host",
  10479. + "videocore",
  10480. + "host+videocore",
  10481. +};
  10482. +
  10483. +/* ---- Private Function Prototypes -------------------------------------- */
  10484. +
  10485. +/* ---- Private Functions ------------------------------------------------ */
  10486. +
  10487. +static inline unsigned vcaddr_to_pfn(unsigned long vc_addr)
  10488. +{
  10489. + unsigned long pfn = vc_addr & 0x3FFFFFFF;
  10490. + pfn += mm_vc_mem_phys_addr;
  10491. + pfn >>= PAGE_SHIFT;
  10492. + return pfn;
  10493. +}
  10494. +
  10495. +/* Carries over to the state statistics the statistics once owned by a deceased
  10496. +** resource.
  10497. +*/
  10498. +static void vc_sm_resource_deceased(struct SM_RESOURCE_T *p_res, int terminated)
  10499. +{
  10500. + if (sm_state != NULL) {
  10501. + if (p_res != NULL) {
  10502. + int ix;
  10503. +
  10504. + if (terminated)
  10505. + sm_state->res_terminated_cnt++;
  10506. + else
  10507. + sm_state->res_deceased_cnt++;
  10508. +
  10509. + for (ix = 0; ix < END_ALL; ix++) {
  10510. + if (terminated)
  10511. + sm_state->terminated[ix] +=
  10512. + p_res->res_stats[ix];
  10513. + else
  10514. + sm_state->deceased[ix] +=
  10515. + p_res->res_stats[ix];
  10516. + }
  10517. + }
  10518. + }
  10519. +}
  10520. +
  10521. +/* Fetch a videocore handle corresponding to a mapping of the pid+address
  10522. +** returns 0 (ie NULL) if no such handle exists in the global map.
  10523. +*/
  10524. +static unsigned int vmcs_sm_vc_handle_from_pid_and_address(unsigned int pid,
  10525. + unsigned int addr)
  10526. +{
  10527. + struct sm_mmap *map = NULL;
  10528. + unsigned int handle = 0;
  10529. +
  10530. + if (!sm_state || addr == 0)
  10531. + goto out;
  10532. +
  10533. + mutex_lock(&(sm_state->map_lock));
  10534. +
  10535. + /* Lookup the resource.
  10536. + */
  10537. + if (!list_empty(&sm_state->map_list)) {
  10538. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  10539. + if (map->res_pid != pid || map->res_addr != addr)
  10540. + continue;
  10541. +
  10542. + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> vc-hdl %x (usr-hdl %x)\n",
  10543. + __func__, map, map->res_pid, map->res_addr,
  10544. + map->res_vc_hdl, map->res_usr_hdl);
  10545. +
  10546. + handle = map->res_vc_hdl;
  10547. + break;
  10548. + }
  10549. + }
  10550. +
  10551. + mutex_unlock(&(sm_state->map_lock));
  10552. +
  10553. +out:
  10554. + /* Use a debug log here as it may be a valid situation that we query
  10555. + ** for something that is not mapped, we do not want a kernel log each
  10556. + ** time around.
  10557. + **
  10558. + ** There are other error log that would pop up accordingly if someone
  10559. + ** subsequently tries to use something invalid after being told not to
  10560. + ** use it...
  10561. + */
  10562. + if (handle == 0) {
  10563. + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
  10564. + __func__, pid, addr);
  10565. + }
  10566. +
  10567. + return handle;
  10568. +}
  10569. +
  10570. +/* Fetch a user handle corresponding to a mapping of the pid+address
  10571. +** returns 0 (ie NULL) if no such handle exists in the global map.
  10572. +*/
  10573. +static unsigned int vmcs_sm_usr_handle_from_pid_and_address(unsigned int pid,
  10574. + unsigned int addr)
  10575. +{
  10576. + struct sm_mmap *map = NULL;
  10577. + unsigned int handle = 0;
  10578. +
  10579. + if (!sm_state || addr == 0)
  10580. + goto out;
  10581. +
  10582. + mutex_lock(&(sm_state->map_lock));
  10583. +
  10584. + /* Lookup the resource.
  10585. + */
  10586. + if (!list_empty(&sm_state->map_list)) {
  10587. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  10588. + if (map->res_pid != pid || map->res_addr != addr)
  10589. + continue;
  10590. +
  10591. + pr_debug("[%s]: global map %p (pid %u, addr %lx) -> usr-hdl %x (vc-hdl %x)\n",
  10592. + __func__, map, map->res_pid, map->res_addr,
  10593. + map->res_usr_hdl, map->res_vc_hdl);
  10594. +
  10595. + handle = map->res_usr_hdl;
  10596. + break;
  10597. + }
  10598. + }
  10599. +
  10600. + mutex_unlock(&(sm_state->map_lock));
  10601. +
  10602. +out:
  10603. + /* Use a debug log here as it may be a valid situation that we query
  10604. + * for something that is not mapped yet.
  10605. + *
  10606. + * There are other error log that would pop up accordingly if someone
  10607. + * subsequently tries to use something invalid after being told not to
  10608. + * use it...
  10609. + */
  10610. + if (handle == 0)
  10611. + pr_debug("[%s]: not a valid map (pid %u, addr %x)\n",
  10612. + __func__, pid, addr);
  10613. +
  10614. + return handle;
  10615. +}
  10616. +
  10617. +#if defined(DO_NOT_USE)
  10618. +/* Fetch an address corresponding to a mapping of the pid+handle
  10619. +** returns 0 (ie NULL) if no such address exists in the global map.
  10620. +*/
  10621. +static unsigned int vmcs_sm_usr_address_from_pid_and_vc_handle(unsigned int pid,
  10622. + unsigned int hdl)
  10623. +{
  10624. + struct sm_mmap *map = NULL;
  10625. + unsigned int addr = 0;
  10626. +
  10627. + if (sm_state == NULL || hdl == 0)
  10628. + goto out;
  10629. +
  10630. + mutex_lock(&(sm_state->map_lock));
  10631. +
  10632. + /* Lookup the resource.
  10633. + */
  10634. + if (!list_empty(&sm_state->map_list)) {
  10635. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  10636. + if (map->res_pid != pid || map->res_vc_hdl != hdl)
  10637. + continue;
  10638. +
  10639. + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
  10640. + __func__, map, map->res_pid, map->res_vc_hdl,
  10641. + map->res_usr_hdl, map->res_addr);
  10642. +
  10643. + addr = map->res_addr;
  10644. + break;
  10645. + }
  10646. + }
  10647. +
  10648. + mutex_unlock(&(sm_state->map_lock));
  10649. +
  10650. +out:
  10651. + /* Use a debug log here as it may be a valid situation that we query
  10652. + ** for something that is not mapped, we do not want a kernel log each
  10653. + ** time around.
  10654. + **
  10655. + ** There are other error log that would pop up accordingly if someone
  10656. + ** subsequently tries to use something invalid after being told not to
  10657. + ** use it...
  10658. + */
  10659. + if (addr == 0)
  10660. + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n",
  10661. + __func__, pid, hdl);
  10662. +
  10663. + return addr;
  10664. +}
  10665. +#endif
  10666. +
  10667. +/* Fetch an address corresponding to a mapping of the pid+handle
  10668. +** returns 0 (ie NULL) if no such address exists in the global map.
  10669. +*/
  10670. +static unsigned int vmcs_sm_usr_address_from_pid_and_usr_handle(unsigned int
  10671. + pid,
  10672. + unsigned int
  10673. + hdl)
  10674. +{
  10675. + struct sm_mmap *map = NULL;
  10676. + unsigned int addr = 0;
  10677. +
  10678. + if (sm_state == NULL || hdl == 0)
  10679. + goto out;
  10680. +
  10681. + mutex_lock(&(sm_state->map_lock));
  10682. +
  10683. + /* Lookup the resource.
  10684. + */
  10685. + if (!list_empty(&sm_state->map_list)) {
  10686. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  10687. + if (map->res_pid != pid || map->res_usr_hdl != hdl)
  10688. + continue;
  10689. +
  10690. + pr_debug("[%s]: global map %p (pid %u, vc-hdl %x, usr-hdl %x) -> addr %lx\n",
  10691. + __func__, map, map->res_pid, map->res_vc_hdl,
  10692. + map->res_usr_hdl, map->res_addr);
  10693. +
  10694. + addr = map->res_addr;
  10695. + break;
  10696. + }
  10697. + }
  10698. +
  10699. + mutex_unlock(&(sm_state->map_lock));
  10700. +
  10701. +out:
  10702. + /* Use a debug log here as it may be a valid situation that we query
  10703. + * for something that is not mapped, we do not want a kernel log each
  10704. + * time around.
  10705. + *
  10706. + * There are other error log that would pop up accordingly if someone
  10707. + * subsequently tries to use something invalid after being told not to
  10708. + * use it...
  10709. + */
  10710. + if (addr == 0)
  10711. + pr_debug("[%s]: not a valid map (pid %u, hdl %x)\n", __func__,
  10712. + pid, hdl);
  10713. +
  10714. + return addr;
  10715. +}
  10716. +
  10717. +/* Adds a resource mapping to the global data list.
  10718. +*/
  10719. +static void vmcs_sm_add_map(struct SM_STATE_T *state,
  10720. + struct SM_RESOURCE_T *resource, struct sm_mmap *map)
  10721. +{
  10722. + mutex_lock(&(state->map_lock));
  10723. +
  10724. + /* Add to the global list of mappings
  10725. + */
  10726. + list_add(&map->map_list, &state->map_list);
  10727. +
  10728. + /* Add to the list of mappings for this resource
  10729. + */
  10730. + list_add(&map->resource_map_list, &resource->map_list);
  10731. + resource->map_count++;
  10732. +
  10733. + mutex_unlock(&(state->map_lock));
  10734. +
  10735. + pr_debug("[%s]: added map %p (pid %u, vc-hdl %x, usr-hdl %x, addr %lx)\n",
  10736. + __func__, map, map->res_pid, map->res_vc_hdl,
  10737. + map->res_usr_hdl, map->res_addr);
  10738. +}
  10739. +
  10740. +/* Removes a resource mapping from the global data list.
  10741. +*/
  10742. +static void vmcs_sm_remove_map(struct SM_STATE_T *state,
  10743. + struct SM_RESOURCE_T *resource,
  10744. + struct sm_mmap *map)
  10745. +{
  10746. + mutex_lock(&(state->map_lock));
  10747. +
  10748. + /* Remove from the global list of mappings
  10749. + */
  10750. + list_del(&map->map_list);
  10751. +
  10752. + /* Remove from the list of mapping for this resource
  10753. + */
  10754. + list_del(&map->resource_map_list);
  10755. + if (resource->map_count > 0)
  10756. + resource->map_count--;
  10757. +
  10758. + mutex_unlock(&(state->map_lock));
  10759. +
  10760. + pr_debug("[%s]: removed map %p (pid %d, vc-hdl %x, usr-hdl %x, addr %lx)\n",
  10761. + __func__, map, map->res_pid, map->res_vc_hdl, map->res_usr_hdl,
  10762. + map->res_addr);
  10763. +
  10764. + kfree(map);
  10765. +}
  10766. +
  10767. +/* Read callback for the global state proc entry.
  10768. +*/
  10769. +static int vc_sm_global_state_show(struct seq_file *s, void *v)
  10770. +{
  10771. + struct sm_mmap *map = NULL;
  10772. + int map_count = 0;
  10773. +
  10774. + if (sm_state == NULL)
  10775. + return 0;
  10776. +
  10777. + seq_printf(s, "\nVC-ServiceHandle 0x%x\n",
  10778. + (unsigned int)sm_state->sm_handle);
  10779. +
  10780. + /* Log all applicable mapping(s).
  10781. + */
  10782. +
  10783. + mutex_lock(&(sm_state->map_lock));
  10784. +
  10785. + if (!list_empty(&sm_state->map_list)) {
  10786. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  10787. + map_count++;
  10788. +
  10789. + seq_printf(s, "\nMapping 0x%x\n",
  10790. + (unsigned int)map);
  10791. + seq_printf(s, " TGID %u\n",
  10792. + map->res_pid);
  10793. + seq_printf(s, " VC-HDL 0x%x\n",
  10794. + map->res_vc_hdl);
  10795. + seq_printf(s, " USR-HDL 0x%x\n",
  10796. + map->res_usr_hdl);
  10797. + seq_printf(s, " USR-ADDR 0x%lx\n",
  10798. + map->res_addr);
  10799. + }
  10800. + }
  10801. +
  10802. + mutex_unlock(&(sm_state->map_lock));
  10803. + seq_printf(s, "\n\nTotal map count: %d\n\n", map_count);
  10804. +
  10805. + return 0;
  10806. +}
  10807. +
  10808. +static int vc_sm_global_statistics_show(struct seq_file *s, void *v)
  10809. +{
  10810. + int ix;
  10811. +
  10812. + /* Global state tracked statistics.
  10813. + */
  10814. + if (sm_state != NULL) {
  10815. + seq_puts(s, "\nDeceased Resources Statistics\n");
  10816. +
  10817. + seq_printf(s, "\nNatural Cause (%u occurences)\n",
  10818. + sm_state->res_deceased_cnt);
  10819. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  10820. + if (sm_state->deceased[ix] > 0) {
  10821. + seq_printf(s, " %u\t%s\n",
  10822. + sm_state->deceased[ix],
  10823. + sm_stats_human_read[ix]);
  10824. + }
  10825. + }
  10826. + seq_puts(s, "\n");
  10827. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  10828. + if (sm_state->deceased[ix + END_ATTEMPT] > 0) {
  10829. + seq_printf(s, " %u\tFAILED %s\n",
  10830. + sm_state->deceased[ix + END_ATTEMPT],
  10831. + sm_stats_human_read[ix]);
  10832. + }
  10833. + }
  10834. +
  10835. + seq_printf(s, "\nForcefull (%u occurences)\n",
  10836. + sm_state->res_terminated_cnt);
  10837. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  10838. + if (sm_state->terminated[ix] > 0) {
  10839. + seq_printf(s, " %u\t%s\n",
  10840. + sm_state->terminated[ix],
  10841. + sm_stats_human_read[ix]);
  10842. + }
  10843. + }
  10844. + seq_puts(s, "\n");
  10845. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  10846. + if (sm_state->terminated[ix + END_ATTEMPT] > 0) {
  10847. + seq_printf(s, " %u\tFAILED %s\n",
  10848. + sm_state->terminated[ix +
  10849. + END_ATTEMPT],
  10850. + sm_stats_human_read[ix]);
  10851. + }
  10852. + }
  10853. + }
  10854. +
  10855. + return 0;
  10856. +}
  10857. +
  10858. +#if 0
  10859. +/* Read callback for the statistics proc entry.
  10860. +*/
  10861. +static int vc_sm_statistics_show(struct seq_file *s, void *v)
  10862. +{
  10863. + int ix;
  10864. + struct SM_PRIV_DATA_T *file_data;
  10865. + struct SM_RESOURCE_T *resource;
  10866. + int res_count = 0;
  10867. + struct SM_PDE_T *p_pde;
  10868. +
  10869. + p_pde = (struct SM_PDE_T *)(s->private);
  10870. + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
  10871. +
  10872. + if (file_data == NULL)
  10873. + return 0;
  10874. +
  10875. + /* Per process statistics.
  10876. + */
  10877. +
  10878. + seq_printf(s, "\nStatistics for TGID %d\n", file_data->pid);
  10879. +
  10880. + mutex_lock(&(sm_state->map_lock));
  10881. +
  10882. + if (!list_empty(&file_data->resource_list)) {
  10883. + list_for_each_entry(resource, &file_data->resource_list,
  10884. + resource_list) {
  10885. + res_count++;
  10886. +
  10887. + seq_printf(s, "\nGUID: 0x%x\n\n",
  10888. + resource->res_guid);
  10889. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  10890. + if (resource->res_stats[ix] > 0) {
  10891. + seq_printf(s,
  10892. + " %u\t%s\n",
  10893. + resource->res_stats[ix],
  10894. + sm_stats_human_read[ix]);
  10895. + }
  10896. + }
  10897. + seq_puts(s, "\n");
  10898. + for (ix = 0; ix < END_ATTEMPT; ix++) {
  10899. + if (resource->res_stats[ix + END_ATTEMPT] > 0) {
  10900. + seq_printf(s,
  10901. + " %u\tFAILED %s\n",
  10902. + resource->res_stats[
  10903. + ix + END_ATTEMPT],
  10904. + sm_stats_human_read[ix]);
  10905. + }
  10906. + }
  10907. + }
  10908. + }
  10909. +
  10910. + mutex_unlock(&(sm_state->map_lock));
  10911. +
  10912. + seq_printf(s, "\nResources Count %d\n", res_count);
  10913. +
  10914. + return 0;
  10915. +}
  10916. +#endif
  10917. +
  10918. +#if 0
  10919. +/* Read callback for the allocation proc entry. */
  10920. +static int vc_sm_alloc_show(struct seq_file *s, void *v)
  10921. +{
  10922. + struct SM_PRIV_DATA_T *file_data;
  10923. + struct SM_RESOURCE_T *resource;
  10924. + int alloc_count = 0;
  10925. + struct SM_PDE_T *p_pde;
  10926. +
  10927. + p_pde = (struct SM_PDE_T *)(s->private);
  10928. + file_data = (struct SM_PRIV_DATA_T *)(p_pde->priv_data);
  10929. +
  10930. + if (!file_data)
  10931. + return 0;
  10932. +
  10933. + /* Per process statistics. */
  10934. + seq_printf(s, "\nAllocation for TGID %d\n", file_data->pid);
  10935. +
  10936. + mutex_lock(&(sm_state->map_lock));
  10937. +
  10938. + if (!list_empty(&file_data->resource_list)) {
  10939. + list_for_each_entry(resource, &file_data->resource_list,
  10940. + resource_list) {
  10941. + alloc_count++;
  10942. +
  10943. + seq_printf(s, "\nGUID: 0x%x\n",
  10944. + resource->res_guid);
  10945. + seq_printf(s, "Lock Count: %u\n",
  10946. + resource->lock_count);
  10947. + seq_printf(s, "Mapped: %s\n",
  10948. + (resource->map_count ? "yes" : "no"));
  10949. + seq_printf(s, "VC-handle: 0x%x\n",
  10950. + resource->res_handle);
  10951. + seq_printf(s, "VC-address: 0x%p\n",
  10952. + resource->res_base_mem);
  10953. + seq_printf(s, "VC-size (bytes): %u\n",
  10954. + resource->res_size);
  10955. + seq_printf(s, "Cache: %s\n",
  10956. + sm_cache_map_vector[resource->res_cached]);
  10957. + }
  10958. + }
  10959. +
  10960. + mutex_unlock(&(sm_state->map_lock));
  10961. +
  10962. + seq_printf(s, "\n\nTotal allocation count: %d\n\n", alloc_count);
  10963. +
  10964. + return 0;
  10965. +}
  10966. +#endif
  10967. +
  10968. +static int vc_sm_seq_file_show(struct seq_file *s, void *v)
  10969. +{
  10970. + struct SM_PDE_T *sm_pde;
  10971. +
  10972. + sm_pde = (struct SM_PDE_T *)(s->private);
  10973. +
  10974. + if (sm_pde && sm_pde->show)
  10975. + sm_pde->show(s, v);
  10976. +
  10977. + return 0;
  10978. +}
  10979. +
  10980. +static int vc_sm_single_open(struct inode *inode, struct file *file)
  10981. +{
  10982. + return single_open(file, vc_sm_seq_file_show, inode->i_private);
  10983. +}
  10984. +
  10985. +static const struct file_operations vc_sm_debug_fs_fops = {
  10986. + .open = vc_sm_single_open,
  10987. + .read = seq_read,
  10988. + .llseek = seq_lseek,
  10989. + .release = single_release,
  10990. +};
  10991. +
  10992. +/* Adds a resource to the private data list which tracks all the allocated
  10993. +** data.
  10994. +*/
  10995. +static void vmcs_sm_add_resource(struct SM_PRIV_DATA_T *privdata,
  10996. + struct SM_RESOURCE_T *resource)
  10997. +{
  10998. + mutex_lock(&(sm_state->map_lock));
  10999. + list_add(&resource->resource_list, &privdata->resource_list);
  11000. + list_add(&resource->global_resource_list, &sm_state->resource_list);
  11001. + mutex_unlock(&(sm_state->map_lock));
  11002. +
  11003. + pr_debug("[%s]: added resource %p (base addr %p, hdl %x, size %u, cache %u)\n",
  11004. + __func__, resource, resource->res_base_mem,
  11005. + resource->res_handle, resource->res_size, resource->res_cached);
  11006. +}
  11007. +
  11008. +/* Locates a resource and acquire a reference on it.
  11009. +** The resource won't be deleted while there is a reference on it.
  11010. +*/
  11011. +static struct SM_RESOURCE_T *vmcs_sm_acquire_resource(struct SM_PRIV_DATA_T
  11012. + *private,
  11013. + unsigned int res_guid)
  11014. +{
  11015. + struct SM_RESOURCE_T *resource, *ret = NULL;
  11016. +
  11017. + mutex_lock(&(sm_state->map_lock));
  11018. +
  11019. + list_for_each_entry(resource, &private->resource_list, resource_list) {
  11020. + if (resource->res_guid != res_guid)
  11021. + continue;
  11022. +
  11023. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  11024. + __func__, resource, resource->res_guid,
  11025. + resource->res_base_mem, resource->res_handle,
  11026. + resource->res_size, resource->res_cached);
  11027. + resource->ref_count++;
  11028. + ret = resource;
  11029. + break;
  11030. + }
  11031. +
  11032. + mutex_unlock(&(sm_state->map_lock));
  11033. +
  11034. + return ret;
  11035. +}
  11036. +
  11037. +/* Locates a resource and acquire a reference on it.
  11038. +** The resource won't be deleted while there is a reference on it.
  11039. +*/
  11040. +static struct SM_RESOURCE_T *vmcs_sm_acquire_first_resource(
  11041. + struct SM_PRIV_DATA_T *private)
  11042. +{
  11043. + struct SM_RESOURCE_T *resource, *ret = NULL;
  11044. +
  11045. + mutex_lock(&(sm_state->map_lock));
  11046. +
  11047. + list_for_each_entry(resource, &private->resource_list, resource_list) {
  11048. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  11049. + __func__, resource, resource->res_guid,
  11050. + resource->res_base_mem, resource->res_handle,
  11051. + resource->res_size, resource->res_cached);
  11052. + resource->ref_count++;
  11053. + ret = resource;
  11054. + break;
  11055. + }
  11056. +
  11057. + mutex_unlock(&(sm_state->map_lock));
  11058. +
  11059. + return ret;
  11060. +}
  11061. +
  11062. +/* Locates a resource and acquire a reference on it.
  11063. +** The resource won't be deleted while there is a reference on it.
  11064. +*/
  11065. +static struct SM_RESOURCE_T *vmcs_sm_acquire_global_resource(unsigned int
  11066. + res_guid)
  11067. +{
  11068. + struct SM_RESOURCE_T *resource, *ret = NULL;
  11069. +
  11070. + mutex_lock(&(sm_state->map_lock));
  11071. +
  11072. + list_for_each_entry(resource, &sm_state->resource_list,
  11073. + global_resource_list) {
  11074. + if (resource->res_guid != res_guid)
  11075. + continue;
  11076. +
  11077. + pr_debug("[%s]: located resource %p (guid: %x, base addr %p, hdl %x, size %u, cache %u)\n",
  11078. + __func__, resource, resource->res_guid,
  11079. + resource->res_base_mem, resource->res_handle,
  11080. + resource->res_size, resource->res_cached);
  11081. + resource->ref_count++;
  11082. + ret = resource;
  11083. + break;
  11084. + }
  11085. +
  11086. + mutex_unlock(&(sm_state->map_lock));
  11087. +
  11088. + return ret;
  11089. +}
  11090. +
  11091. +/* Release a previously acquired resource.
  11092. +** The resource will be deleted when its refcount reaches 0.
  11093. +*/
  11094. +static void vmcs_sm_release_resource(struct SM_RESOURCE_T *resource, int force)
  11095. +{
  11096. + struct SM_PRIV_DATA_T *private = resource->private;
  11097. + struct sm_mmap *map, *map_tmp;
  11098. + struct SM_RESOURCE_T *res_tmp;
  11099. + int ret;
  11100. +
  11101. + mutex_lock(&(sm_state->map_lock));
  11102. +
  11103. + if (--resource->ref_count) {
  11104. + if (force)
  11105. + pr_err("[%s]: resource %p in use\n", __func__, resource);
  11106. +
  11107. + mutex_unlock(&(sm_state->map_lock));
  11108. + return;
  11109. + }
  11110. +
  11111. + /* Time to free the resource. Start by removing it from the list */
  11112. + list_del(&resource->resource_list);
  11113. + list_del(&resource->global_resource_list);
  11114. +
  11115. + /* Walk the global resource list, find out if the resource is used
  11116. + * somewhere else. In which case we don't want to delete it.
  11117. + */
  11118. + list_for_each_entry(res_tmp, &sm_state->resource_list,
  11119. + global_resource_list) {
  11120. + if (res_tmp->res_handle == resource->res_handle) {
  11121. + resource->res_handle = 0;
  11122. + break;
  11123. + }
  11124. + }
  11125. +
  11126. + mutex_unlock(&(sm_state->map_lock));
  11127. +
  11128. + pr_debug("[%s]: freeing data - guid %x, hdl %x, base address %p\n",
  11129. + __func__, resource->res_guid, resource->res_handle,
  11130. + resource->res_base_mem);
  11131. + resource->res_stats[FREE]++;
  11132. +
  11133. + /* Make sure the resource we're removing is unmapped first */
  11134. + if (resource->map_count && !list_empty(&resource->map_list)) {
  11135. + down_write(&current->mm->mmap_sem);
  11136. + list_for_each_entry_safe(map, map_tmp, &resource->map_list,
  11137. + resource_map_list) {
  11138. + ret =
  11139. + do_munmap(current->mm, map->res_addr,
  11140. + resource->res_size);
  11141. + if (ret) {
  11142. + pr_err("[%s]: could not unmap resource %p\n",
  11143. + __func__, resource);
  11144. + }
  11145. + }
  11146. + up_write(&current->mm->mmap_sem);
  11147. + }
  11148. +
  11149. + /* Free up the videocore allocated resource.
  11150. + */
  11151. + if (resource->res_handle) {
  11152. + VC_SM_FREE_T free = {
  11153. + resource->res_handle, resource->res_base_mem
  11154. + };
  11155. + int status = vc_vchi_sm_free(sm_state->sm_handle, &free,
  11156. + &private->int_trans_id);
  11157. + if (status != 0 && status != -EINTR) {
  11158. + pr_err("[%s]: failed to free memory on videocore (status: %u, trans_id: %u)\n",
  11159. + __func__, status, private->int_trans_id);
  11160. + resource->res_stats[FREE_FAIL]++;
  11161. + ret = -EPERM;
  11162. + }
  11163. + }
  11164. +
  11165. + /* Free up the shared resource.
  11166. + */
  11167. + if (resource->res_shared)
  11168. + vmcs_sm_release_resource(resource->res_shared, 0);
  11169. +
  11170. + /* Free up the local resource tracking this allocation.
  11171. + */
  11172. + vc_sm_resource_deceased(resource, force);
  11173. + kfree(resource);
  11174. +}
  11175. +
  11176. +/* Dump the map table for the driver. If process is -1, dumps the whole table,
  11177. +** if process is a valid pid (non -1) dump only the entries associated with the
  11178. +** pid of interest.
  11179. +*/
  11180. +static void vmcs_sm_host_walk_map_per_pid(int pid)
  11181. +{
  11182. + struct sm_mmap *map = NULL;
  11183. +
  11184. + /* Make sure the device was started properly.
  11185. + */
  11186. + if (sm_state == NULL) {
  11187. + pr_err("[%s]: invalid device\n", __func__);
  11188. + return;
  11189. + }
  11190. +
  11191. + mutex_lock(&(sm_state->map_lock));
  11192. +
  11193. + /* Log all applicable mapping(s).
  11194. + */
  11195. + if (!list_empty(&sm_state->map_list)) {
  11196. + list_for_each_entry(map, &sm_state->map_list, map_list) {
  11197. + if (pid == -1 || map->res_pid == pid) {
  11198. + pr_info("[%s]: tgid: %u - vc-hdl: %x, usr-hdl: %x, usr-addr: %lx\n",
  11199. + __func__, map->res_pid, map->res_vc_hdl,
  11200. + map->res_usr_hdl, map->res_addr);
  11201. + }
  11202. + }
  11203. + }
  11204. +
  11205. + mutex_unlock(&(sm_state->map_lock));
  11206. +
  11207. + return;
  11208. +}
  11209. +
  11210. +/* Dump the allocation table from host side point of view. This only dumps the
  11211. +** data allocated for this process/device referenced by the file_data.
  11212. +*/
  11213. +static void vmcs_sm_host_walk_alloc(struct SM_PRIV_DATA_T *file_data)
  11214. +{
  11215. + struct SM_RESOURCE_T *resource = NULL;
  11216. +
  11217. + /* Make sure the device was started properly.
  11218. + */
  11219. + if ((sm_state == NULL) || (file_data == NULL)) {
  11220. + pr_err("[%s]: invalid device\n", __func__);
  11221. + return;
  11222. + }
  11223. +
  11224. + mutex_lock(&(sm_state->map_lock));
  11225. +
  11226. + if (!list_empty(&file_data->resource_list)) {
  11227. + list_for_each_entry(resource, &file_data->resource_list,
  11228. + resource_list) {
  11229. + pr_info("[%s]: guid: %x - hdl: %x, vc-mem: %p, size: %u, cache: %u\n",
  11230. + __func__, resource->res_guid, resource->res_handle,
  11231. + resource->res_base_mem, resource->res_size,
  11232. + resource->res_cached);
  11233. + }
  11234. + }
  11235. +
  11236. + mutex_unlock(&(sm_state->map_lock));
  11237. +
  11238. + return;
  11239. +}
  11240. +
  11241. +/* Create support for private data tracking.
  11242. +*/
  11243. +static struct SM_PRIV_DATA_T *vc_sm_create_priv_data(pid_t id)
  11244. +{
  11245. + char alloc_name[32];
  11246. + struct SM_PRIV_DATA_T *file_data = NULL;
  11247. +
  11248. + /* Allocate private structure. */
  11249. + file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
  11250. +
  11251. + if (!file_data) {
  11252. + pr_err("[%s]: cannot allocate file data\n", __func__);
  11253. + goto out;
  11254. + }
  11255. +
  11256. + snprintf(alloc_name, sizeof(alloc_name), "%d", id);
  11257. +
  11258. + INIT_LIST_HEAD(&file_data->resource_list);
  11259. + file_data->pid = id;
  11260. + file_data->dir_pid = debugfs_create_dir(alloc_name,
  11261. + sm_state->dir_alloc);
  11262. +#if 0
  11263. + /* TODO: fix this to support querying statistics per pid */
  11264. +
  11265. + if (IS_ERR_OR_NULL(file_data->dir_pid)) {
  11266. + file_data->dir_pid = NULL;
  11267. + } else {
  11268. + struct dentry *dir_entry;
  11269. +
  11270. + dir_entry = debugfs_create_file(VC_SM_RESOURCES, S_IRUGO,
  11271. + file_data->dir_pid, file_data,
  11272. + vc_sm_debug_fs_fops);
  11273. +
  11274. + file_data->dir_res.dir_entry = dir_entry;
  11275. + file_data->dir_res.priv_data = file_data;
  11276. + file_data->dir_res.show = &vc_sm_alloc_show;
  11277. +
  11278. + dir_entry = debugfs_create_file(VC_SM_STATS, S_IRUGO,
  11279. + file_data->dir_pid, file_data,
  11280. + vc_sm_debug_fs_fops);
  11281. +
  11282. + file_data->dir_res.dir_entry = dir_entry;
  11283. + file_data->dir_res.priv_data = file_data;
  11284. + file_data->dir_res.show = &vc_sm_statistics_show;
  11285. + }
  11286. + pr_debug("[%s]: private data allocated %p\n", __func__, file_data);
  11287. +
  11288. +#endif
  11289. +out:
  11290. + return file_data;
  11291. +}
  11292. +
  11293. +/* Open the device. Creates a private state to help track all allocation
  11294. +** associated with this device.
  11295. +*/
  11296. +static int vc_sm_open(struct inode *inode, struct file *file)
  11297. +{
  11298. + int ret = 0;
  11299. +
  11300. + /* Make sure the device was started properly.
  11301. + */
  11302. + if (!sm_state) {
  11303. + pr_err("[%s]: invalid device\n", __func__);
  11304. + ret = -EPERM;
  11305. + goto out;
  11306. + }
  11307. +
  11308. + file->private_data = vc_sm_create_priv_data(current->tgid);
  11309. + if (file->private_data == NULL) {
  11310. + pr_err("[%s]: failed to create data tracker\n", __func__);
  11311. +
  11312. + ret = -ENOMEM;
  11313. + goto out;
  11314. + }
  11315. +
  11316. +out:
  11317. + return ret;
  11318. +}
  11319. +
  11320. +/* Close the device. Free up all resources still associated with this device
  11321. +** at the time.
  11322. +*/
  11323. +static int vc_sm_release(struct inode *inode, struct file *file)
  11324. +{
  11325. + struct SM_PRIV_DATA_T *file_data =
  11326. + (struct SM_PRIV_DATA_T *)file->private_data;
  11327. + struct SM_RESOURCE_T *resource;
  11328. + int ret = 0;
  11329. +
  11330. + /* Make sure the device was started properly.
  11331. + */
  11332. + if (sm_state == NULL || file_data == NULL) {
  11333. + pr_err("[%s]: invalid device\n", __func__);
  11334. + ret = -EPERM;
  11335. + goto out;
  11336. + }
  11337. +
  11338. + pr_debug("[%s]: using private data %p\n", __func__, file_data);
  11339. +
  11340. + if (file_data->restart_sys == -EINTR) {
  11341. + VC_SM_ACTION_CLEAN_T action_clean;
  11342. +
  11343. + pr_debug("[%s]: releasing following EINTR on %u (trans_id: %u) (likely due to signal)...\n",
  11344. + __func__, file_data->int_action,
  11345. + file_data->int_trans_id);
  11346. +
  11347. + action_clean.res_action = file_data->int_action;
  11348. + action_clean.action_trans_id = file_data->int_trans_id;
  11349. +
  11350. + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
  11351. + }
  11352. +
  11353. + while ((resource = vmcs_sm_acquire_first_resource(file_data)) != NULL) {
  11354. + vmcs_sm_release_resource(resource, 0);
  11355. + vmcs_sm_release_resource(resource, 1);
  11356. + }
  11357. +
  11358. + /* Remove the corresponding proc entry. */
  11359. + debugfs_remove_recursive(file_data->dir_pid);
  11360. +
  11361. + /* Terminate the private data.
  11362. + */
  11363. + kfree(file_data);
  11364. +
  11365. +out:
  11366. + return ret;
  11367. +}
  11368. +
  11369. +static void vcsm_vma_open(struct vm_area_struct *vma)
  11370. +{
  11371. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  11372. +
  11373. + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
  11374. + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
  11375. + (int)vma->vm_pgoff);
  11376. +
  11377. + map->ref_count++;
  11378. +}
  11379. +
  11380. +static void vcsm_vma_close(struct vm_area_struct *vma)
  11381. +{
  11382. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  11383. +
  11384. + pr_debug("[%s]: virt %lx-%lx, pid %i, pfn %i\n",
  11385. + __func__, vma->vm_start, vma->vm_end, (int)current->tgid,
  11386. + (int)vma->vm_pgoff);
  11387. +
  11388. + map->ref_count--;
  11389. +
  11390. + /* Remove from the map table.
  11391. + */
  11392. + if (map->ref_count == 0)
  11393. + vmcs_sm_remove_map(sm_state, map->resource, map);
  11394. +}
  11395. +
  11396. +static int vcsm_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  11397. +{
  11398. + struct sm_mmap *map = (struct sm_mmap *)vma->vm_private_data;
  11399. + struct SM_RESOURCE_T *resource = map->resource;
  11400. + pgoff_t page_offset;
  11401. + unsigned long pfn;
  11402. + int ret = 0;
  11403. +
  11404. + /* Lock the resource if necessary.
  11405. + */
  11406. + if (!resource->lock_count) {
  11407. + VC_SM_LOCK_UNLOCK_T lock_unlock;
  11408. + VC_SM_LOCK_RESULT_T lock_result;
  11409. + int status;
  11410. +
  11411. + lock_unlock.res_handle = resource->res_handle;
  11412. + lock_unlock.res_mem = resource->res_base_mem;
  11413. +
  11414. + pr_debug("[%s]: attempt to lock data - hdl %x, base address %p\n",
  11415. + __func__, lock_unlock.res_handle, lock_unlock.res_mem);
  11416. +
  11417. + /* Lock the videocore allocated resource.
  11418. + */
  11419. + status = vc_vchi_sm_lock(sm_state->sm_handle,
  11420. + &lock_unlock, &lock_result, 0);
  11421. + if ((status != 0) ||
  11422. + ((status == 0) && (lock_result.res_mem == NULL))) {
  11423. + pr_err("[%s]: failed to lock memory on videocore (status: %u)\n",
  11424. + __func__, status);
  11425. + resource->res_stats[LOCK_FAIL]++;
  11426. + return VM_FAULT_SIGBUS;
  11427. + }
  11428. +
  11429. + pfn = vcaddr_to_pfn((unsigned long)resource->res_base_mem);
  11430. + outer_inv_range(__pfn_to_phys(pfn),
  11431. + __pfn_to_phys(pfn) + resource->res_size);
  11432. +
  11433. + resource->res_stats[LOCK]++;
  11434. + resource->lock_count++;
  11435. +
  11436. + /* Keep track of the new base memory.
  11437. + */
  11438. + if ((lock_result.res_mem != NULL) &&
  11439. + (lock_result.res_old_mem != NULL) &&
  11440. + (lock_result.res_mem != lock_result.res_old_mem)) {
  11441. + resource->res_base_mem = lock_result.res_mem;
  11442. + }
  11443. + }
  11444. +
  11445. + /* We don't use vmf->pgoff since that has the fake offset */
  11446. + page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start);
  11447. + pfn = (uint32_t)resource->res_base_mem & 0x3FFFFFFF;
  11448. + pfn += mm_vc_mem_phys_addr;
  11449. + pfn += page_offset;
  11450. + pfn >>= PAGE_SHIFT;
  11451. +
  11452. + /* Finally, remap it */
  11453. + ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  11454. +
  11455. + switch (ret) {
  11456. + case 0:
  11457. + case -ERESTARTSYS:
  11458. + return VM_FAULT_NOPAGE;
  11459. + case -ENOMEM:
  11460. + case -EAGAIN:
  11461. + return VM_FAULT_OOM;
  11462. + default:
  11463. + return VM_FAULT_SIGBUS;
  11464. + }
  11465. +}
  11466. +
  11467. +static struct vm_operations_struct vcsm_vm_ops = {
  11468. + .open = vcsm_vma_open,
  11469. + .close = vcsm_vma_close,
  11470. + .fault = vcsm_vma_fault,
  11471. +};
  11472. +
  11473. +/* Walks a VMA and clean each valid page from the cache */
  11474. +static void vcsm_vma_cache_clean_page_range(unsigned long addr,
  11475. + unsigned long end)
  11476. +{
  11477. + pgd_t *pgd;
  11478. + pud_t *pud;
  11479. + pmd_t *pmd;
  11480. + pte_t *pte;
  11481. + unsigned long pgd_next, pud_next, pmd_next;
  11482. +
  11483. + if (addr >= end)
  11484. + return;
  11485. +
  11486. + /* Walk PGD */
  11487. + pgd = pgd_offset(current->mm, addr);
  11488. + do {
  11489. + pgd_next = pgd_addr_end(addr, end);
  11490. +
  11491. + if (pgd_none(*pgd) || pgd_bad(*pgd))
  11492. + continue;
  11493. +
  11494. + /* Walk PUD */
  11495. + pud = pud_offset(pgd, addr);
  11496. + do {
  11497. + pud_next = pud_addr_end(addr, pgd_next);
  11498. + if (pud_none(*pud) || pud_bad(*pud))
  11499. + continue;
  11500. +
  11501. + /* Walk PMD */
  11502. + pmd = pmd_offset(pud, addr);
  11503. + do {
  11504. + pmd_next = pmd_addr_end(addr, pud_next);
  11505. + if (pmd_none(*pmd) || pmd_bad(*pmd))
  11506. + continue;
  11507. +
  11508. + /* Walk PTE */
  11509. + pte = pte_offset_map(pmd, addr);
  11510. + do {
  11511. + if (pte_none(*pte)
  11512. + || !pte_present(*pte))
  11513. + continue;
  11514. +
  11515. + /* Clean + invalidate */
  11516. + dmac_flush_range((const void *) addr,
  11517. + (const void *)
  11518. + (addr + PAGE_SIZE));
  11519. +
  11520. + } while (pte++, addr +=
  11521. + PAGE_SIZE, addr != pmd_next);
  11522. + pte_unmap(pte);
  11523. +
  11524. + } while (pmd++, addr = pmd_next, addr != pud_next);
  11525. +
  11526. + } while (pud++, addr = pud_next, addr != pgd_next);
  11527. + } while (pgd++, addr = pgd_next, addr != end);
  11528. +}
  11529. +
  11530. +/* Map an allocated data into something that the user space.
  11531. +*/
  11532. +static int vc_sm_mmap(struct file *file, struct vm_area_struct *vma)
  11533. +{
  11534. + int ret = 0;
  11535. + struct SM_PRIV_DATA_T *file_data =
  11536. + (struct SM_PRIV_DATA_T *)file->private_data;
  11537. + struct SM_RESOURCE_T *resource = NULL;
  11538. + struct sm_mmap *map = NULL;
  11539. +
  11540. + /* Make sure the device was started properly.
  11541. + */
  11542. + if ((sm_state == NULL) || (file_data == NULL)) {
  11543. + pr_err("[%s]: invalid device\n", __func__);
  11544. + return -EPERM;
  11545. + }
  11546. +
  11547. + pr_debug("[%s]: private data %p, guid %x\n", __func__, file_data,
  11548. + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
  11549. +
  11550. + /* We lookup to make sure that the data we are being asked to mmap is
  11551. + ** something that we allocated.
  11552. + **
  11553. + ** We use the offset information as the key to tell us which resource
  11554. + ** we are mapping.
  11555. + */
  11556. + resource = vmcs_sm_acquire_resource(file_data,
  11557. + ((unsigned int)vma->vm_pgoff <<
  11558. + PAGE_SHIFT));
  11559. + if (resource == NULL) {
  11560. + pr_err("[%s]: failed to locate resource for guid %x\n", __func__,
  11561. + ((unsigned int)vma->vm_pgoff << PAGE_SHIFT));
  11562. + return -ENOMEM;
  11563. + }
  11564. +
  11565. + pr_debug("[%s]: guid %x, tgid %u, %u, %u\n",
  11566. + __func__, resource->res_guid, current->tgid, resource->pid,
  11567. + file_data->pid);
  11568. +
  11569. + /* Check permissions.
  11570. + */
  11571. + if (resource->pid && (resource->pid != current->tgid)) {
  11572. + pr_err("[%s]: current tgid %u != %u owner\n",
  11573. + __func__, current->tgid, resource->pid);
  11574. + ret = -EPERM;
  11575. + goto error;
  11576. + }
  11577. +
  11578. + /* Verify that what we are asked to mmap is proper.
  11579. + */
  11580. + if (resource->res_size != (unsigned int)(vma->vm_end - vma->vm_start)) {
  11581. + pr_err("[%s]: size inconsistency (resource: %u - mmap: %u)\n",
  11582. + __func__,
  11583. + resource->res_size,
  11584. + (unsigned int)(vma->vm_end - vma->vm_start));
  11585. +
  11586. + ret = -EINVAL;
  11587. + goto error;
  11588. + }
  11589. +
  11590. + /* Keep track of the tuple in the global resource list such that one
  11591. + * can do a mapping lookup for address/memory handle.
  11592. + */
  11593. + map = kzalloc(sizeof(*map), GFP_KERNEL);
  11594. + if (map == NULL) {
  11595. + pr_err("[%s]: failed to allocate global tracking resource\n",
  11596. + __func__);
  11597. + ret = -ENOMEM;
  11598. + goto error;
  11599. + }
  11600. +
  11601. + map->res_pid = current->tgid;
  11602. + map->res_vc_hdl = resource->res_handle;
  11603. + map->res_usr_hdl = resource->res_guid;
  11604. + map->res_addr = (long unsigned int)vma->vm_start;
  11605. + map->resource = resource;
  11606. + map->vma = vma;
  11607. + vmcs_sm_add_map(sm_state, resource, map);
  11608. +
  11609. + /* We are not actually mapping the pages, we just provide a fault
  11610. + ** handler to allow pages to be mapped when accessed
  11611. + */
  11612. + vma->vm_flags |=
  11613. + VM_IO | VM_PFNMAP | VM_DONTCOPY | VM_DONTEXPAND;
  11614. + vma->vm_ops = &vcsm_vm_ops;
  11615. + vma->vm_private_data = map;
  11616. +
  11617. + /* vm_pgoff is the first PFN of the mapped memory */
  11618. + vma->vm_pgoff = (unsigned long)resource->res_base_mem & 0x3FFFFFFF;
  11619. + vma->vm_pgoff += mm_vc_mem_phys_addr;
  11620. + vma->vm_pgoff >>= PAGE_SHIFT;
  11621. +
  11622. + if ((resource->res_cached == VMCS_SM_CACHE_NONE) ||
  11623. + (resource->res_cached == VMCS_SM_CACHE_VC)) {
  11624. + /* Allocated non host cached memory, honour it.
  11625. + */
  11626. + vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  11627. + }
  11628. +
  11629. + pr_debug("[%s]: resource %p (guid %x) - cnt %u, base address %p, handle %x, size %u (%u), cache %u\n",
  11630. + __func__,
  11631. + resource, resource->res_guid, resource->lock_count,
  11632. + resource->res_base_mem, resource->res_handle,
  11633. + resource->res_size, (unsigned int)(vma->vm_end - vma->vm_start),
  11634. + resource->res_cached);
  11635. +
  11636. + pr_debug("[%s]: resource %p (base address %p, handle %x) - map-count %d, usr-addr %x\n",
  11637. + __func__, resource, resource->res_base_mem,
  11638. + resource->res_handle, resource->map_count,
  11639. + (unsigned int)vma->vm_start);
  11640. +
  11641. + vcsm_vma_open(vma);
  11642. + resource->res_stats[MAP]++;
  11643. + vmcs_sm_release_resource(resource, 0);
  11644. + return 0;
  11645. +
  11646. +error:
  11647. + vmcs_sm_release_resource(resource, 0);
  11648. + resource->res_stats[MAP_FAIL]++;
  11649. + return ret;
  11650. +}
  11651. +
  11652. +/* Allocate a shared memory handle and block.
  11653. +*/
  11654. +int vc_sm_ioctl_alloc(struct SM_PRIV_DATA_T *private,
  11655. + struct vmcs_sm_ioctl_alloc *ioparam)
  11656. +{
  11657. + int ret = 0;
  11658. + int status;
  11659. + struct SM_RESOURCE_T *resource;
  11660. + VC_SM_ALLOC_T alloc = { 0 };
  11661. + VC_SM_ALLOC_RESULT_T result = { 0 };
  11662. +
  11663. + /* Setup our allocation parameters */
  11664. + alloc.type = ((ioparam->cached == VMCS_SM_CACHE_VC)
  11665. + || (ioparam->cached ==
  11666. + VMCS_SM_CACHE_BOTH)) ? VC_SM_ALLOC_CACHED :
  11667. + VC_SM_ALLOC_NON_CACHED;
  11668. + alloc.base_unit = ioparam->size;
  11669. + alloc.num_unit = ioparam->num;
  11670. + alloc.allocator = current->tgid;
  11671. + /* Align to kernel page size */
  11672. + alloc.alignement = 4096;
  11673. + /* Align the size to the kernel page size */
  11674. + alloc.base_unit =
  11675. + (alloc.base_unit + alloc.alignement - 1) & ~(alloc.alignement - 1);
  11676. + if (*ioparam->name) {
  11677. + memcpy(alloc.name, ioparam->name, sizeof(alloc.name) - 1);
  11678. + } else {
  11679. + memcpy(alloc.name, VMCS_SM_RESOURCE_NAME_DEFAULT,
  11680. + sizeof(VMCS_SM_RESOURCE_NAME_DEFAULT));
  11681. + }
  11682. +
  11683. + pr_debug("[%s]: attempt to allocate \"%s\" data - type %u, base %u (%u), num %u, alignement %u\n",
  11684. + __func__, alloc.name, alloc.type, ioparam->size,
  11685. + alloc.base_unit, alloc.num_unit, alloc.alignement);
  11686. +
  11687. + /* Allocate local resource to track this allocation.
  11688. + */
  11689. + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  11690. + if (!resource) {
  11691. + ret = -ENOMEM;
  11692. + goto error;
  11693. + }
  11694. + INIT_LIST_HEAD(&resource->map_list);
  11695. + resource->ref_count++;
  11696. + resource->pid = current->tgid;
  11697. +
  11698. + /* Allocate the videocore resource.
  11699. + */
  11700. + status = vc_vchi_sm_alloc(sm_state->sm_handle, &alloc, &result,
  11701. + &private->int_trans_id);
  11702. + if (status == -EINTR) {
  11703. + pr_debug("[%s]: requesting allocate memory action restart (trans_id: %u)\n",
  11704. + __func__, private->int_trans_id);
  11705. + ret = -ERESTARTSYS;
  11706. + private->restart_sys = -EINTR;
  11707. + private->int_action = VC_SM_MSG_TYPE_ALLOC;
  11708. + goto error;
  11709. + } else if (status != 0 || (status == 0 && result.res_mem == NULL)) {
  11710. + pr_err("[%s]: failed to allocate memory on videocore (status: %u, trans_id: %u)\n",
  11711. + __func__, status, private->int_trans_id);
  11712. + ret = -ENOMEM;
  11713. + resource->res_stats[ALLOC_FAIL]++;
  11714. + goto error;
  11715. + }
  11716. +
  11717. + /* Keep track of the resource we created.
  11718. + */
  11719. + resource->private = private;
  11720. + resource->res_handle = result.res_handle;
  11721. + resource->res_base_mem = result.res_mem;
  11722. + resource->res_size = alloc.base_unit * alloc.num_unit;
  11723. + resource->res_cached = ioparam->cached;
  11724. +
  11725. + /* Kernel/user GUID. This global identifier is used for mmap'ing the
  11726. + * allocated region from user space, it is passed as the mmap'ing
  11727. + * offset, we use it to 'hide' the videocore handle/address.
  11728. + */
  11729. + mutex_lock(&sm_state->lock);
  11730. + resource->res_guid = ++sm_state->guid;
  11731. + mutex_unlock(&sm_state->lock);
  11732. + resource->res_guid <<= PAGE_SHIFT;
  11733. +
  11734. + vmcs_sm_add_resource(private, resource);
  11735. +
  11736. + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
  11737. + __func__, resource->res_guid, resource->res_handle,
  11738. + resource->res_base_mem, resource->res_size,
  11739. + resource->res_cached);
  11740. +
  11741. + /* We're done */
  11742. + resource->res_stats[ALLOC]++;
  11743. + ioparam->handle = resource->res_guid;
  11744. + return 0;
  11745. +
  11746. +error:
  11747. + pr_err("[%s]: failed to allocate \"%s\" data (%i) - type %u, base %u (%u), num %u, alignment %u\n",
  11748. + __func__, alloc.name, ret, alloc.type, ioparam->size,
  11749. + alloc.base_unit, alloc.num_unit, alloc.alignement);
  11750. + if (resource != NULL) {
  11751. + vc_sm_resource_deceased(resource, 1);
  11752. + kfree(resource);
  11753. + }
  11754. + return ret;
  11755. +}
  11756. +
  11757. +/* Share an allocate memory handle and block.
  11758. +*/
  11759. +int vc_sm_ioctl_alloc_share(struct SM_PRIV_DATA_T *private,
  11760. + struct vmcs_sm_ioctl_alloc_share *ioparam)
  11761. +{
  11762. + struct SM_RESOURCE_T *resource, *shared_resource;
  11763. + int ret = 0;
  11764. +
  11765. + pr_debug("[%s]: attempt to share resource %u\n", __func__,
  11766. + ioparam->handle);
  11767. +
  11768. + shared_resource = vmcs_sm_acquire_global_resource(ioparam->handle);
  11769. + if (shared_resource == NULL) {
  11770. + ret = -ENOMEM;
  11771. + goto error;
  11772. + }
  11773. +
  11774. + /* Allocate local resource to track this allocation.
  11775. + */
  11776. + resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  11777. + if (resource == NULL) {
  11778. + pr_err("[%s]: failed to allocate local tracking resource\n",
  11779. + __func__);
  11780. + ret = -ENOMEM;
  11781. + goto error;
  11782. + }
  11783. + INIT_LIST_HEAD(&resource->map_list);
  11784. + resource->ref_count++;
  11785. + resource->pid = current->tgid;
  11786. +
  11787. + /* Keep track of the resource we created.
  11788. + */
  11789. + resource->private = private;
  11790. + resource->res_handle = shared_resource->res_handle;
  11791. + resource->res_base_mem = shared_resource->res_base_mem;
  11792. + resource->res_size = shared_resource->res_size;
  11793. + resource->res_cached = shared_resource->res_cached;
  11794. + resource->res_shared = shared_resource;
  11795. +
  11796. + mutex_lock(&sm_state->lock);
  11797. + resource->res_guid = ++sm_state->guid;
  11798. + mutex_unlock(&sm_state->lock);
  11799. + resource->res_guid <<= PAGE_SHIFT;
  11800. +
  11801. + vmcs_sm_add_resource(private, resource);
  11802. +
  11803. + pr_debug("[%s]: allocated data - guid %x, hdl %x, base address %p, size %d, cache %d\n",
  11804. + __func__, resource->res_guid, resource->res_handle,
  11805. + resource->res_base_mem, resource->res_size,
  11806. + resource->res_cached);
  11807. +
  11808. + /* We're done */
  11809. + resource->res_stats[ALLOC]++;
  11810. + ioparam->handle = resource->res_guid;
  11811. + ioparam->size = resource->res_size;
  11812. + return 0;
  11813. +
  11814. +error:
  11815. + pr_err("[%s]: failed to share %u\n", __func__, ioparam->handle);
  11816. + if (shared_resource != NULL)
  11817. + vmcs_sm_release_resource(shared_resource, 0);
  11818. +
  11819. + return ret;
  11820. +}
  11821. +
  11822. +/* Free a previously allocated shared memory handle and block.
  11823. +*/
  11824. +static int vc_sm_ioctl_free(struct SM_PRIV_DATA_T *private,
  11825. + struct vmcs_sm_ioctl_free *ioparam)
  11826. +{
  11827. + struct SM_RESOURCE_T *resource =
  11828. + vmcs_sm_acquire_resource(private, ioparam->handle);
  11829. +
  11830. + if (resource == NULL) {
  11831. + pr_err("[%s]: resource for guid %u does not exist\n", __func__,
  11832. + ioparam->handle);
  11833. + return -EINVAL;
  11834. + }
  11835. +
  11836. + /* Check permissions.
  11837. + */
  11838. + if (resource->pid && (resource->pid != current->tgid)) {
  11839. + pr_err("[%s]: current tgid %u != %u owner\n",
  11840. + __func__, current->tgid, resource->pid);
  11841. + vmcs_sm_release_resource(resource, 0);
  11842. + return -EPERM;
  11843. + }
  11844. +
  11845. + vmcs_sm_release_resource(resource, 0);
  11846. + vmcs_sm_release_resource(resource, 0);
  11847. + return 0;
  11848. +}
  11849. +
  11850. +/* Resize a previously allocated shared memory handle and block.
  11851. +*/
  11852. +static int vc_sm_ioctl_resize(struct SM_PRIV_DATA_T *private,
  11853. + struct vmcs_sm_ioctl_resize *ioparam)
  11854. +{
  11855. + int ret = 0;
  11856. + int status;
  11857. + VC_SM_RESIZE_T resize;
  11858. + struct SM_RESOURCE_T *resource;
  11859. +
  11860. + /* Locate resource from GUID.
  11861. + */
  11862. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  11863. + if (!resource) {
  11864. + pr_err("[%s]: failed resource - guid %x\n",
  11865. + __func__, ioparam->handle);
  11866. + ret = -EFAULT;
  11867. + goto error;
  11868. + }
  11869. +
  11870. + /* If the resource is locked, its reference count will be not NULL,
  11871. + ** in which case we will not be allowed to resize it anyways, so
  11872. + ** reject the attempt here.
  11873. + */
  11874. + if (resource->lock_count != 0) {
  11875. + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
  11876. + __func__, ioparam->handle, resource->lock_count);
  11877. + ret = -EFAULT;
  11878. + goto error;
  11879. + }
  11880. +
  11881. + /* Check permissions.
  11882. + */
  11883. + if (resource->pid && (resource->pid != current->tgid)) {
  11884. + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
  11885. + current->tgid, resource->pid);
  11886. + ret = -EPERM;
  11887. + goto error;
  11888. + }
  11889. +
  11890. + if (resource->map_count != 0) {
  11891. + pr_err("[%s]: cannot resize - guid %x, ref-cnt %d\n",
  11892. + __func__, ioparam->handle, resource->map_count);
  11893. + ret = -EFAULT;
  11894. + goto error;
  11895. + }
  11896. +
  11897. + resize.res_handle = resource->res_handle;
  11898. + resize.res_mem = resource->res_base_mem;
  11899. + resize.res_new_size = ioparam->new_size;
  11900. +
  11901. + pr_debug("[%s]: attempt to resize data - guid %x, hdl %x, base address %p\n",
  11902. + __func__, ioparam->handle, resize.res_handle, resize.res_mem);
  11903. +
  11904. + /* Resize the videocore allocated resource.
  11905. + */
  11906. + status = vc_vchi_sm_resize(sm_state->sm_handle, &resize,
  11907. + &private->int_trans_id);
  11908. + if (status == -EINTR) {
  11909. + pr_debug("[%s]: requesting resize memory action restart (trans_id: %u)\n",
  11910. + __func__, private->int_trans_id);
  11911. + ret = -ERESTARTSYS;
  11912. + private->restart_sys = -EINTR;
  11913. + private->int_action = VC_SM_MSG_TYPE_RESIZE;
  11914. + goto error;
  11915. + } else if (status != 0) {
  11916. + pr_err("[%s]: failed to resize memory on videocore (status: %u, trans_id: %u)\n",
  11917. + __func__, status, private->int_trans_id);
  11918. + ret = -EPERM;
  11919. + goto error;
  11920. + }
  11921. +
  11922. + pr_debug("[%s]: success to resize data - hdl %x, size %d -> %d\n",
  11923. + __func__, resize.res_handle, resource->res_size,
  11924. + resize.res_new_size);
  11925. +
  11926. + /* Successfully resized, save the information and inform the user.
  11927. + */
  11928. + ioparam->old_size = resource->res_size;
  11929. + resource->res_size = resize.res_new_size;
  11930. +
  11931. +error:
  11932. + if (resource)
  11933. + vmcs_sm_release_resource(resource, 0);
  11934. +
  11935. + return ret;
  11936. +}
  11937. +
  11938. +/* Lock a previously allocated shared memory handle and block.
  11939. +*/
  11940. +static int vc_sm_ioctl_lock(struct SM_PRIV_DATA_T *private,
  11941. + struct vmcs_sm_ioctl_lock_unlock *ioparam,
  11942. + int change_cache, enum vmcs_sm_cache_e cache_type,
  11943. + unsigned int vc_addr)
  11944. +{
  11945. + int status;
  11946. + VC_SM_LOCK_UNLOCK_T lock;
  11947. + VC_SM_LOCK_RESULT_T result;
  11948. + struct SM_RESOURCE_T *resource;
  11949. + int ret = 0;
  11950. + struct sm_mmap *map, *map_tmp;
  11951. + long unsigned int phys_addr;
  11952. +
  11953. + map = NULL;
  11954. +
  11955. + /* Locate resource from GUID.
  11956. + */
  11957. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  11958. + if (resource == NULL) {
  11959. + ret = -EINVAL;
  11960. + goto error;
  11961. + }
  11962. +
  11963. + /* Check permissions.
  11964. + */
  11965. + if (resource->pid && (resource->pid != current->tgid)) {
  11966. + pr_err("[%s]: current tgid %u != %u owner\n", __func__,
  11967. + current->tgid, resource->pid);
  11968. + ret = -EPERM;
  11969. + goto error;
  11970. + }
  11971. +
  11972. + lock.res_handle = resource->res_handle;
  11973. + lock.res_mem = resource->res_base_mem;
  11974. +
  11975. + /* Take the lock and get the address to be mapped.
  11976. + */
  11977. + if (vc_addr == 0) {
  11978. + pr_debug("[%s]: attempt to lock data - guid %x, hdl %x, base address %p\n",
  11979. + __func__, ioparam->handle, lock.res_handle,
  11980. + lock.res_mem);
  11981. +
  11982. + /* Lock the videocore allocated resource.
  11983. + */
  11984. + status = vc_vchi_sm_lock(sm_state->sm_handle, &lock, &result,
  11985. + &private->int_trans_id);
  11986. + if (status == -EINTR) {
  11987. + pr_debug("[%s]: requesting lock memory action restart (trans_id: %u)\n",
  11988. + __func__, private->int_trans_id);
  11989. + ret = -ERESTARTSYS;
  11990. + private->restart_sys = -EINTR;
  11991. + private->int_action = VC_SM_MSG_TYPE_LOCK;
  11992. + goto error;
  11993. + } else if (status != 0 ||
  11994. + (status == 0 && result.res_mem == NULL)) {
  11995. + pr_err("[%s]: failed to lock memory on videocore (status: %u, trans_id: %u)\n",
  11996. + __func__, status, private->int_trans_id);
  11997. + ret = -EPERM;
  11998. + resource->res_stats[LOCK_FAIL]++;
  11999. + goto error;
  12000. + }
  12001. +
  12002. + pr_debug("[%s]: succeed to lock data - hdl %x, base address %p (%p), ref-cnt %d\n",
  12003. + __func__, lock.res_handle, result.res_mem,
  12004. + lock.res_mem, resource->lock_count);
  12005. + }
  12006. + /* Lock assumed taken already, address to be mapped is known.
  12007. + */
  12008. + else
  12009. + resource->res_base_mem = (void *)vc_addr;
  12010. +
  12011. + resource->res_stats[LOCK]++;
  12012. + resource->lock_count++;
  12013. +
  12014. + /* Keep track of the new base memory allocation if it has changed.
  12015. + */
  12016. + if ((vc_addr == 0) &&
  12017. + (result.res_mem != NULL) &&
  12018. + (result.res_old_mem != NULL) &&
  12019. + (result.res_mem != result.res_old_mem)) {
  12020. + resource->res_base_mem = result.res_mem;
  12021. +
  12022. + /* Kernel allocated resources.
  12023. + */
  12024. + if (resource->pid == 0) {
  12025. + if (!list_empty(&resource->map_list)) {
  12026. + list_for_each_entry_safe(map, map_tmp,
  12027. + &resource->map_list,
  12028. + resource_map_list) {
  12029. + if (map->res_addr) {
  12030. + iounmap((void *)map->res_addr);
  12031. + map->res_addr = 0;
  12032. +
  12033. + vmcs_sm_remove_map(sm_state,
  12034. + map->resource,
  12035. + map);
  12036. + break;
  12037. + }
  12038. + }
  12039. + }
  12040. + }
  12041. + }
  12042. +
  12043. + if (change_cache)
  12044. + resource->res_cached = cache_type;
  12045. +
  12046. + if (resource->map_count) {
  12047. + ioparam->addr =
  12048. + vmcs_sm_usr_address_from_pid_and_usr_handle(
  12049. + current->tgid, ioparam->handle);
  12050. +
  12051. + pr_debug("[%s] map_count %d private->pid %d current->tgid %d hnd %x addr %u\n",
  12052. + __func__, resource->map_count, private->pid,
  12053. + current->tgid, ioparam->handle, ioparam->addr);
  12054. + } else {
  12055. + /* Kernel allocated resources.
  12056. + */
  12057. + if (resource->pid == 0) {
  12058. + pr_debug("[%s]: attempt mapping kernel resource - guid %x, hdl %x\n",
  12059. + __func__, ioparam->handle, lock.res_handle);
  12060. +
  12061. + ioparam->addr = 0;
  12062. +
  12063. + map = kzalloc(sizeof(*map), GFP_KERNEL);
  12064. + if (map == NULL) {
  12065. + pr_err("[%s]: failed allocating tracker\n",
  12066. + __func__);
  12067. + ret = -ENOMEM;
  12068. + goto error;
  12069. + } else {
  12070. + phys_addr = (uint32_t)resource->res_base_mem &
  12071. + 0x3FFFFFFF;
  12072. + phys_addr += mm_vc_mem_phys_addr;
  12073. + if (resource->res_cached
  12074. + == VMCS_SM_CACHE_HOST) {
  12075. + ioparam->addr = (long unsigned int)
  12076. + /* TODO - make cached work */
  12077. + ioremap_nocache(phys_addr,
  12078. + resource->res_size);
  12079. +
  12080. + pr_debug("[%s]: mapping kernel - guid %x, hdl %x - cached mapping %u\n",
  12081. + __func__, ioparam->handle,
  12082. + lock.res_handle, ioparam->addr);
  12083. + } else {
  12084. + ioparam->addr = (long unsigned int)
  12085. + ioremap_nocache(phys_addr,
  12086. + resource->res_size);
  12087. +
  12088. + pr_debug("[%s]: mapping kernel- guid %x, hdl %x - non cached mapping %u\n",
  12089. + __func__, ioparam->handle,
  12090. + lock.res_handle, ioparam->addr);
  12091. + }
  12092. +
  12093. + map->res_pid = 0;
  12094. + map->res_vc_hdl = resource->res_handle;
  12095. + map->res_usr_hdl = resource->res_guid;
  12096. + map->res_addr = ioparam->addr;
  12097. + map->resource = resource;
  12098. + map->vma = NULL;
  12099. +
  12100. + vmcs_sm_add_map(sm_state, resource, map);
  12101. + }
  12102. + } else
  12103. + ioparam->addr = 0;
  12104. + }
  12105. +
  12106. +error:
  12107. + if (resource)
  12108. + vmcs_sm_release_resource(resource, 0);
  12109. +
  12110. + return ret;
  12111. +}
  12112. +
  12113. +/* Unlock a previously allocated shared memory handle and block.
  12114. +*/
  12115. +static int vc_sm_ioctl_unlock(struct SM_PRIV_DATA_T *private,
  12116. + struct vmcs_sm_ioctl_lock_unlock *ioparam,
  12117. + int flush, int wait_reply, int no_vc_unlock)
  12118. +{
  12119. + int status;
  12120. + VC_SM_LOCK_UNLOCK_T unlock;
  12121. + struct sm_mmap *map, *map_tmp;
  12122. + struct SM_RESOURCE_T *resource;
  12123. + int ret = 0;
  12124. +
  12125. + map = NULL;
  12126. +
  12127. + /* Locate resource from GUID.
  12128. + */
  12129. + resource = vmcs_sm_acquire_resource(private, ioparam->handle);
  12130. + if (resource == NULL) {
  12131. + ret = -EINVAL;
  12132. + goto error;
  12133. + }
  12134. +
  12135. + /* Check permissions.
  12136. + */
  12137. + if (resource->pid && (resource->pid != current->tgid)) {
  12138. + pr_err("[%s]: current tgid %u != %u owner\n",
  12139. + __func__, current->tgid, resource->pid);
  12140. + ret = -EPERM;
  12141. + goto error;
  12142. + }
  12143. +
  12144. + unlock.res_handle = resource->res_handle;
  12145. + unlock.res_mem = resource->res_base_mem;
  12146. +
  12147. + pr_debug("[%s]: attempt to unlock data - guid %x, hdl %x, base address %p\n",
  12148. + __func__, ioparam->handle, unlock.res_handle, unlock.res_mem);
  12149. +
  12150. + /* User space allocated resources.
  12151. + */
  12152. + if (resource->pid) {
  12153. + /* Flush if requested */
  12154. + if (resource->res_cached && flush) {
  12155. + dma_addr_t phys_addr = 0;
  12156. + resource->res_stats[FLUSH]++;
  12157. +
  12158. + phys_addr =
  12159. + (dma_addr_t)((uint32_t)resource->res_base_mem &
  12160. + 0x3FFFFFFF);
  12161. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  12162. +
  12163. + /* L1 cache flush */
  12164. + down_read(&current->mm->mmap_sem);
  12165. + list_for_each_entry(map, &resource->map_list,
  12166. + resource_map_list) {
  12167. + if (map->vma) {
  12168. + unsigned long start;
  12169. + unsigned long end;
  12170. + start = map->vma->vm_start;
  12171. + end = map->vma->vm_end;
  12172. +
  12173. + vcsm_vma_cache_clean_page_range(
  12174. + start, end);
  12175. + }
  12176. + }
  12177. + up_read(&current->mm->mmap_sem);
  12178. +
  12179. + /* L2 cache flush */
  12180. + outer_clean_range(phys_addr,
  12181. + phys_addr +
  12182. + (size_t) resource->res_size);
  12183. + }
  12184. +
  12185. + /* We need to zap all the vmas associated with this resource */
  12186. + if (resource->lock_count == 1) {
  12187. + down_read(&current->mm->mmap_sem);
  12188. + list_for_each_entry(map, &resource->map_list,
  12189. + resource_map_list) {
  12190. + if (map->vma) {
  12191. + zap_vma_ptes(map->vma,
  12192. + map->vma->vm_start,
  12193. + map->vma->vm_end -
  12194. + map->vma->vm_start);
  12195. + }
  12196. + }
  12197. + up_read(&current->mm->mmap_sem);
  12198. + }
  12199. + }
  12200. + /* Kernel allocated resources. */
  12201. + else {
  12202. + /* Global + Taken in this context */
  12203. + if (resource->ref_count == 2) {
  12204. + if (!list_empty(&resource->map_list)) {
  12205. + list_for_each_entry_safe(map, map_tmp,
  12206. + &resource->map_list,
  12207. + resource_map_list) {
  12208. + if (map->res_addr) {
  12209. + if (flush &&
  12210. + (resource->res_cached ==
  12211. + VMCS_SM_CACHE_HOST)) {
  12212. + long unsigned int
  12213. + phys_addr;
  12214. + phys_addr = (uint32_t)
  12215. + resource->res_base_mem & 0x3FFFFFFF;
  12216. + phys_addr +=
  12217. + mm_vc_mem_phys_addr;
  12218. +
  12219. + /* L1 cache flush */
  12220. + dmac_flush_range((const
  12221. + void
  12222. + *)
  12223. + map->res_addr, (const void *)
  12224. + (map->res_addr + resource->res_size));
  12225. +
  12226. + /* L2 cache flush */
  12227. + outer_clean_range
  12228. + (phys_addr,
  12229. + phys_addr +
  12230. + (size_t)
  12231. + resource->res_size);
  12232. + }
  12233. +
  12234. + iounmap((void *)map->res_addr);
  12235. + map->res_addr = 0;
  12236. +
  12237. + vmcs_sm_remove_map(sm_state,
  12238. + map->resource,
  12239. + map);
  12240. + break;
  12241. + }
  12242. + }
  12243. + }
  12244. + }
  12245. + }
  12246. +
  12247. + if (resource->lock_count) {
  12248. + /* Bypass the videocore unlock.
  12249. + */
  12250. + if (no_vc_unlock)
  12251. + status = 0;
  12252. + /* Unlock the videocore allocated resource.
  12253. + */
  12254. + else {
  12255. + status =
  12256. + vc_vchi_sm_unlock(sm_state->sm_handle, &unlock,
  12257. + &private->int_trans_id,
  12258. + wait_reply);
  12259. + if (status == -EINTR) {
  12260. + pr_debug("[%s]: requesting unlock memory action restart (trans_id: %u)\n",
  12261. + __func__, private->int_trans_id);
  12262. +
  12263. + ret = -ERESTARTSYS;
  12264. + resource->res_stats[UNLOCK]--;
  12265. + private->restart_sys = -EINTR;
  12266. + private->int_action = VC_SM_MSG_TYPE_UNLOCK;
  12267. + goto error;
  12268. + } else if (status != 0) {
  12269. + pr_err("[%s]: failed to unlock vc mem (status: %u, trans_id: %u)\n",
  12270. + __func__, status, private->int_trans_id);
  12271. +
  12272. + ret = -EPERM;
  12273. + resource->res_stats[UNLOCK_FAIL]++;
  12274. + goto error;
  12275. + }
  12276. + }
  12277. +
  12278. + resource->res_stats[UNLOCK]++;
  12279. + resource->lock_count--;
  12280. + }
  12281. +
  12282. + pr_debug("[%s]: success to unlock data - hdl %x, base address %p, ref-cnt %d\n",
  12283. + __func__, unlock.res_handle, unlock.res_mem,
  12284. + resource->lock_count);
  12285. +
  12286. +error:
  12287. + if (resource)
  12288. + vmcs_sm_release_resource(resource, 0);
  12289. +
  12290. + return ret;
  12291. +}
  12292. +
  12293. +/* Handle control from host. */
  12294. +static long vc_sm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  12295. +{
  12296. + int ret = 0;
  12297. + unsigned int cmdnr = _IOC_NR(cmd);
  12298. + struct SM_PRIV_DATA_T *file_data =
  12299. + (struct SM_PRIV_DATA_T *)file->private_data;
  12300. + struct SM_RESOURCE_T *resource = NULL;
  12301. +
  12302. + /* Validate we can work with this device. */
  12303. + if ((sm_state == NULL) || (file_data == NULL)) {
  12304. + pr_err("[%s]: invalid device\n", __func__);
  12305. + ret = -EPERM;
  12306. + goto out;
  12307. + }
  12308. +
  12309. + pr_debug("[%s]: cmd %x tgid %u, owner %u\n", __func__, cmdnr,
  12310. + current->tgid, file_data->pid);
  12311. +
  12312. + /* Action is a re-post of a previously interrupted action? */
  12313. + if (file_data->restart_sys == -EINTR) {
  12314. + VC_SM_ACTION_CLEAN_T action_clean;
  12315. +
  12316. + pr_debug("[%s]: clean up of action %u (trans_id: %u) following EINTR\n",
  12317. + __func__, file_data->int_action,
  12318. + file_data->int_trans_id);
  12319. +
  12320. + action_clean.res_action = file_data->int_action;
  12321. + action_clean.action_trans_id = file_data->int_trans_id;
  12322. +
  12323. + vc_vchi_sm_clean_up(sm_state->sm_handle, &action_clean);
  12324. +
  12325. + file_data->restart_sys = 0;
  12326. + }
  12327. +
  12328. + /* Now process the command.
  12329. + */
  12330. + switch (cmdnr) {
  12331. + /* New memory allocation.
  12332. + */
  12333. + case VMCS_SM_CMD_ALLOC:
  12334. + {
  12335. + struct vmcs_sm_ioctl_alloc ioparam;
  12336. +
  12337. + /* Get the parameter data.
  12338. + */
  12339. + if (copy_from_user
  12340. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  12341. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12342. + __func__, cmdnr);
  12343. + ret = -EFAULT;
  12344. + goto out;
  12345. + }
  12346. +
  12347. + ret = vc_sm_ioctl_alloc(file_data, &ioparam);
  12348. + if (!ret &&
  12349. + (copy_to_user((void *)arg,
  12350. + &ioparam, sizeof(ioparam)) != 0)) {
  12351. + struct vmcs_sm_ioctl_free freeparam = {
  12352. + ioparam.handle
  12353. + };
  12354. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12355. + __func__, cmdnr);
  12356. + vc_sm_ioctl_free(file_data, &freeparam);
  12357. + ret = -EFAULT;
  12358. + }
  12359. +
  12360. + /* Done.
  12361. + */
  12362. + goto out;
  12363. + }
  12364. + break;
  12365. +
  12366. + /* Share existing memory allocation.
  12367. + */
  12368. + case VMCS_SM_CMD_ALLOC_SHARE:
  12369. + {
  12370. + struct vmcs_sm_ioctl_alloc_share ioparam;
  12371. +
  12372. + /* Get the parameter data.
  12373. + */
  12374. + if (copy_from_user
  12375. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  12376. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12377. + __func__, cmdnr);
  12378. + ret = -EFAULT;
  12379. + goto out;
  12380. + }
  12381. +
  12382. + ret = vc_sm_ioctl_alloc_share(file_data, &ioparam);
  12383. +
  12384. + /* Copy result back to user.
  12385. + */
  12386. + if (!ret
  12387. + && copy_to_user((void *)arg, &ioparam,
  12388. + sizeof(ioparam)) != 0) {
  12389. + struct vmcs_sm_ioctl_free freeparam = {
  12390. + ioparam.handle
  12391. + };
  12392. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12393. + __func__, cmdnr);
  12394. + vc_sm_ioctl_free(file_data, &freeparam);
  12395. + ret = -EFAULT;
  12396. + }
  12397. +
  12398. + /* Done.
  12399. + */
  12400. + goto out;
  12401. + }
  12402. + break;
  12403. +
  12404. + /* Lock (attempt to) *and* register a cache behavior change.
  12405. + */
  12406. + case VMCS_SM_CMD_LOCK_CACHE:
  12407. + {
  12408. + struct vmcs_sm_ioctl_lock_cache ioparam;
  12409. + struct vmcs_sm_ioctl_lock_unlock lock;
  12410. +
  12411. + /* Get parameter data.
  12412. + */
  12413. + if (copy_from_user
  12414. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  12415. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12416. + __func__, cmdnr);
  12417. + ret = -EFAULT;
  12418. + goto out;
  12419. + }
  12420. +
  12421. + lock.handle = ioparam.handle;
  12422. + ret =
  12423. + vc_sm_ioctl_lock(file_data, &lock, 1,
  12424. + ioparam.cached, 0);
  12425. +
  12426. + /* Done.
  12427. + */
  12428. + goto out;
  12429. + }
  12430. + break;
  12431. +
  12432. + /* Lock (attempt to) existing memory allocation.
  12433. + */
  12434. + case VMCS_SM_CMD_LOCK:
  12435. + {
  12436. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  12437. +
  12438. + /* Get parameter data.
  12439. + */
  12440. + if (copy_from_user
  12441. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  12442. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12443. + __func__, cmdnr);
  12444. + ret = -EFAULT;
  12445. + goto out;
  12446. + }
  12447. +
  12448. + ret = vc_sm_ioctl_lock(file_data, &ioparam, 0, 0, 0);
  12449. +
  12450. + /* Copy result back to user.
  12451. + */
  12452. + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
  12453. + != 0) {
  12454. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12455. + __func__, cmdnr);
  12456. + ret = -EFAULT;
  12457. + }
  12458. +
  12459. + /* Done.
  12460. + */
  12461. + goto out;
  12462. + }
  12463. + break;
  12464. +
  12465. + /* Unlock (attempt to) existing memory allocation.
  12466. + */
  12467. + case VMCS_SM_CMD_UNLOCK:
  12468. + {
  12469. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  12470. +
  12471. + /* Get parameter data.
  12472. + */
  12473. + if (copy_from_user
  12474. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  12475. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12476. + __func__, cmdnr);
  12477. + ret = -EFAULT;
  12478. + goto out;
  12479. + }
  12480. +
  12481. + ret = vc_sm_ioctl_unlock(file_data, &ioparam, 0, 1, 0);
  12482. +
  12483. + /* Done.
  12484. + */
  12485. + goto out;
  12486. + }
  12487. + break;
  12488. +
  12489. + /* Resize (attempt to) existing memory allocation.
  12490. + */
  12491. + case VMCS_SM_CMD_RESIZE:
  12492. + {
  12493. + struct vmcs_sm_ioctl_resize ioparam;
  12494. +
  12495. + /* Get parameter data.
  12496. + */
  12497. + if (copy_from_user
  12498. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  12499. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12500. + __func__, cmdnr);
  12501. + ret = -EFAULT;
  12502. + goto out;
  12503. + }
  12504. +
  12505. + ret = vc_sm_ioctl_resize(file_data, &ioparam);
  12506. +
  12507. + /* Copy result back to user.
  12508. + */
  12509. + if (copy_to_user((void *)arg, &ioparam, sizeof(ioparam))
  12510. + != 0) {
  12511. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12512. + __func__, cmdnr);
  12513. + ret = -EFAULT;
  12514. + }
  12515. +
  12516. + /* Done.
  12517. + */
  12518. + goto out;
  12519. + }
  12520. + break;
  12521. +
  12522. + /* Terminate existing memory allocation.
  12523. + */
  12524. + case VMCS_SM_CMD_FREE:
  12525. + {
  12526. + struct vmcs_sm_ioctl_free ioparam;
  12527. +
  12528. + /* Get parameter data.
  12529. + */
  12530. + if (copy_from_user
  12531. + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
  12532. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12533. + __func__, cmdnr);
  12534. + ret = -EFAULT;
  12535. + goto out;
  12536. + }
  12537. +
  12538. + ret = vc_sm_ioctl_free(file_data, &ioparam);
  12539. +
  12540. + /* Done.
  12541. + */
  12542. + goto out;
  12543. + }
  12544. + break;
  12545. +
  12546. + /* Walk allocation on videocore, information shows up in the
  12547. + ** videocore log.
  12548. + */
  12549. + case VMCS_SM_CMD_VC_WALK_ALLOC:
  12550. + {
  12551. + pr_debug("[%s]: invoking walk alloc\n", __func__);
  12552. +
  12553. + if (vc_vchi_sm_walk_alloc(sm_state->sm_handle) != 0)
  12554. + pr_err("[%s]: failed to walk-alloc on videocore\n",
  12555. + __func__);
  12556. +
  12557. + /* Done.
  12558. + */
  12559. + goto out;
  12560. + }
  12561. + break;
  12562. +/* Walk mapping table on host, information shows up in the
  12563. + ** kernel log.
  12564. + */
  12565. + case VMCS_SM_CMD_HOST_WALK_MAP:
  12566. + {
  12567. + /* Use pid of -1 to tell to walk the whole map. */
  12568. + vmcs_sm_host_walk_map_per_pid(-1);
  12569. +
  12570. + /* Done. */
  12571. + goto out;
  12572. + }
  12573. + break;
  12574. +
  12575. + /* Walk mapping table per process on host. */
  12576. + case VMCS_SM_CMD_HOST_WALK_PID_ALLOC:
  12577. + {
  12578. + struct vmcs_sm_ioctl_walk ioparam;
  12579. +
  12580. + /* Get parameter data. */
  12581. + if (copy_from_user(&ioparam,
  12582. + (void *)arg, sizeof(ioparam)) != 0) {
  12583. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12584. + __func__, cmdnr);
  12585. + ret = -EFAULT;
  12586. + goto out;
  12587. + }
  12588. +
  12589. + vmcs_sm_host_walk_alloc(file_data);
  12590. +
  12591. + /* Done. */
  12592. + goto out;
  12593. + }
  12594. + break;
  12595. +
  12596. + /* Walk allocation per process on host. */
  12597. + case VMCS_SM_CMD_HOST_WALK_PID_MAP:
  12598. + {
  12599. + struct vmcs_sm_ioctl_walk ioparam;
  12600. +
  12601. + /* Get parameter data. */
  12602. + if (copy_from_user(&ioparam,
  12603. + (void *)arg, sizeof(ioparam)) != 0) {
  12604. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12605. + __func__, cmdnr);
  12606. + ret = -EFAULT;
  12607. + goto out;
  12608. + }
  12609. +
  12610. + vmcs_sm_host_walk_map_per_pid(ioparam.pid);
  12611. +
  12612. + /* Done. */
  12613. + goto out;
  12614. + }
  12615. + break;
  12616. +
  12617. + /* Gets the size of the memory associated with a user handle. */
  12618. + case VMCS_SM_CMD_SIZE_USR_HANDLE:
  12619. + {
  12620. + struct vmcs_sm_ioctl_size ioparam;
  12621. +
  12622. + /* Get parameter data. */
  12623. + if (copy_from_user(&ioparam,
  12624. + (void *)arg, sizeof(ioparam)) != 0) {
  12625. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12626. + __func__, cmdnr);
  12627. + ret = -EFAULT;
  12628. + goto out;
  12629. + }
  12630. +
  12631. + /* Locate resource from GUID. */
  12632. + resource =
  12633. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  12634. + if (resource != NULL) {
  12635. + ioparam.size = resource->res_size;
  12636. + vmcs_sm_release_resource(resource, 0);
  12637. + } else {
  12638. + ioparam.size = 0;
  12639. + }
  12640. +
  12641. + if (copy_to_user((void *)arg,
  12642. + &ioparam, sizeof(ioparam)) != 0) {
  12643. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12644. + __func__, cmdnr);
  12645. + ret = -EFAULT;
  12646. + }
  12647. +
  12648. + /* Done. */
  12649. + goto out;
  12650. + }
  12651. + break;
  12652. +
  12653. + /* Verify we are dealing with a valid resource. */
  12654. + case VMCS_SM_CMD_CHK_USR_HANDLE:
  12655. + {
  12656. + struct vmcs_sm_ioctl_chk ioparam;
  12657. +
  12658. + /* Get parameter data.
  12659. + */
  12660. + if (copy_from_user(&ioparam,
  12661. + (void *)arg, sizeof(ioparam)) != 0) {
  12662. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12663. + __func__, cmdnr);
  12664. +
  12665. + ret = -EFAULT;
  12666. + goto out;
  12667. + }
  12668. +
  12669. + /* Locate resource from GUID. */
  12670. + resource =
  12671. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  12672. + if (resource == NULL)
  12673. + ret = -EINVAL;
  12674. + /* If the resource is cacheable, return additional
  12675. + * information that may be needed to flush the cache.
  12676. + */
  12677. + else if ((resource->res_cached == VMCS_SM_CACHE_HOST) ||
  12678. + (resource->res_cached == VMCS_SM_CACHE_BOTH)) {
  12679. + ioparam.addr =
  12680. + vmcs_sm_usr_address_from_pid_and_usr_handle
  12681. + (current->tgid, ioparam.handle);
  12682. + ioparam.size = resource->res_size;
  12683. + ioparam.cache = resource->res_cached;
  12684. + } else {
  12685. + ioparam.addr = 0;
  12686. + ioparam.size = 0;
  12687. + ioparam.cache = resource->res_cached;
  12688. + }
  12689. +
  12690. + if (resource)
  12691. + vmcs_sm_release_resource(resource, 0);
  12692. +
  12693. + if (copy_to_user((void *)arg,
  12694. + &ioparam, sizeof(ioparam)) != 0) {
  12695. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12696. + __func__, cmdnr);
  12697. + ret = -EFAULT;
  12698. + }
  12699. +
  12700. + /* Done.
  12701. + */
  12702. + goto out;
  12703. + }
  12704. + break;
  12705. +
  12706. + /*
  12707. + * Maps a user handle given the process and the virtual address.
  12708. + */
  12709. + case VMCS_SM_CMD_MAPPED_USR_HANDLE:
  12710. + {
  12711. + struct vmcs_sm_ioctl_map ioparam;
  12712. +
  12713. + /* Get parameter data. */
  12714. + if (copy_from_user(&ioparam,
  12715. + (void *)arg, sizeof(ioparam)) != 0) {
  12716. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12717. + __func__, cmdnr);
  12718. +
  12719. + ret = -EFAULT;
  12720. + goto out;
  12721. + }
  12722. +
  12723. + ioparam.handle =
  12724. + vmcs_sm_usr_handle_from_pid_and_address(
  12725. + ioparam.pid, ioparam.addr);
  12726. +
  12727. + resource =
  12728. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  12729. + if ((resource != NULL)
  12730. + && ((resource->res_cached == VMCS_SM_CACHE_HOST)
  12731. + || (resource->res_cached ==
  12732. + VMCS_SM_CACHE_BOTH))) {
  12733. + ioparam.size = resource->res_size;
  12734. + } else {
  12735. + ioparam.size = 0;
  12736. + }
  12737. +
  12738. + if (resource)
  12739. + vmcs_sm_release_resource(resource, 0);
  12740. +
  12741. + if (copy_to_user((void *)arg,
  12742. + &ioparam, sizeof(ioparam)) != 0) {
  12743. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12744. + __func__, cmdnr);
  12745. + ret = -EFAULT;
  12746. + }
  12747. +
  12748. + /* Done. */
  12749. + goto out;
  12750. + }
  12751. + break;
  12752. +
  12753. + /*
  12754. + * Maps a videocore handle given process and virtual address.
  12755. + */
  12756. + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_ADDR:
  12757. + {
  12758. + struct vmcs_sm_ioctl_map ioparam;
  12759. +
  12760. + /* Get parameter data. */
  12761. + if (copy_from_user(&ioparam,
  12762. + (void *)arg, sizeof(ioparam)) != 0) {
  12763. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12764. + __func__, cmdnr);
  12765. + ret = -EFAULT;
  12766. + goto out;
  12767. + }
  12768. +
  12769. + ioparam.handle = vmcs_sm_vc_handle_from_pid_and_address(
  12770. + ioparam.pid, ioparam.addr);
  12771. +
  12772. + if (copy_to_user((void *)arg,
  12773. + &ioparam, sizeof(ioparam)) != 0) {
  12774. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12775. + __func__, cmdnr);
  12776. +
  12777. + ret = -EFAULT;
  12778. + }
  12779. +
  12780. + /* Done.
  12781. + */
  12782. + goto out;
  12783. + }
  12784. + break;
  12785. +
  12786. + /* Maps a videocore handle given process and user handle. */
  12787. + case VMCS_SM_CMD_MAPPED_VC_HDL_FROM_HDL:
  12788. + {
  12789. + struct vmcs_sm_ioctl_map ioparam;
  12790. +
  12791. + /* Get parameter data. */
  12792. + if (copy_from_user(&ioparam,
  12793. + (void *)arg, sizeof(ioparam)) != 0) {
  12794. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12795. + __func__, cmdnr);
  12796. + ret = -EFAULT;
  12797. + goto out;
  12798. + }
  12799. +
  12800. + /* Locate resource from GUID. */
  12801. + resource =
  12802. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  12803. + if (resource != NULL) {
  12804. + ioparam.handle = resource->res_handle;
  12805. + vmcs_sm_release_resource(resource, 0);
  12806. + } else {
  12807. + ioparam.handle = 0;
  12808. + }
  12809. +
  12810. + if (copy_to_user((void *)arg,
  12811. + &ioparam, sizeof(ioparam)) != 0) {
  12812. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12813. + __func__, cmdnr);
  12814. +
  12815. + ret = -EFAULT;
  12816. + }
  12817. +
  12818. + /* Done. */
  12819. + goto out;
  12820. + }
  12821. + break;
  12822. +
  12823. + /*
  12824. + * Maps a videocore address given process and videocore handle.
  12825. + */
  12826. + case VMCS_SM_CMD_MAPPED_VC_ADDR_FROM_HDL:
  12827. + {
  12828. + struct vmcs_sm_ioctl_map ioparam;
  12829. +
  12830. + /* Get parameter data. */
  12831. + if (copy_from_user(&ioparam,
  12832. + (void *)arg, sizeof(ioparam)) != 0) {
  12833. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12834. + __func__, cmdnr);
  12835. +
  12836. + ret = -EFAULT;
  12837. + goto out;
  12838. + }
  12839. +
  12840. + /* Locate resource from GUID. */
  12841. + resource =
  12842. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  12843. + if (resource != NULL) {
  12844. + ioparam.addr =
  12845. + (unsigned int)resource->res_base_mem;
  12846. + vmcs_sm_release_resource(resource, 0);
  12847. + } else {
  12848. + ioparam.addr = 0;
  12849. + }
  12850. +
  12851. + if (copy_to_user((void *)arg,
  12852. + &ioparam, sizeof(ioparam)) != 0) {
  12853. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12854. + __func__, cmdnr);
  12855. + ret = -EFAULT;
  12856. + }
  12857. +
  12858. + /* Done. */
  12859. + goto out;
  12860. + }
  12861. + break;
  12862. +
  12863. + /* Maps a user address given process and vc handle.
  12864. + */
  12865. + case VMCS_SM_CMD_MAPPED_USR_ADDRESS:
  12866. + {
  12867. + struct vmcs_sm_ioctl_map ioparam;
  12868. +
  12869. + /* Get parameter data. */
  12870. + if (copy_from_user(&ioparam,
  12871. + (void *)arg, sizeof(ioparam)) != 0) {
  12872. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12873. + __func__, cmdnr);
  12874. + ret = -EFAULT;
  12875. + goto out;
  12876. + }
  12877. +
  12878. + /*
  12879. + * Return the address information from the mapping,
  12880. + * 0 (ie NULL) if it cannot locate the actual mapping.
  12881. + */
  12882. + ioparam.addr =
  12883. + vmcs_sm_usr_address_from_pid_and_usr_handle
  12884. + (ioparam.pid, ioparam.handle);
  12885. +
  12886. + if (copy_to_user((void *)arg,
  12887. + &ioparam, sizeof(ioparam)) != 0) {
  12888. + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
  12889. + __func__, cmdnr);
  12890. + ret = -EFAULT;
  12891. + }
  12892. +
  12893. + /* Done. */
  12894. + goto out;
  12895. + }
  12896. + break;
  12897. +
  12898. + /* Flush the cache for a given mapping. */
  12899. + case VMCS_SM_CMD_FLUSH:
  12900. + {
  12901. + struct vmcs_sm_ioctl_cache ioparam;
  12902. +
  12903. + /* Get parameter data. */
  12904. + if (copy_from_user(&ioparam,
  12905. + (void *)arg, sizeof(ioparam)) != 0) {
  12906. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12907. + __func__, cmdnr);
  12908. + ret = -EFAULT;
  12909. + goto out;
  12910. + }
  12911. +
  12912. + /* Locate resource from GUID. */
  12913. + resource =
  12914. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  12915. +
  12916. + if ((resource != NULL) && resource->res_cached) {
  12917. + dma_addr_t phys_addr = 0;
  12918. +
  12919. + resource->res_stats[FLUSH]++;
  12920. +
  12921. + phys_addr =
  12922. + (dma_addr_t)((uint32_t)
  12923. + resource->res_base_mem &
  12924. + 0x3FFFFFFF);
  12925. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  12926. +
  12927. + /* L1 cache flush */
  12928. + down_read(&current->mm->mmap_sem);
  12929. + vcsm_vma_cache_clean_page_range((unsigned long)
  12930. + ioparam.addr,
  12931. + (unsigned long)
  12932. + ioparam.addr +
  12933. + ioparam.size);
  12934. + up_read(&current->mm->mmap_sem);
  12935. +
  12936. + /* L2 cache flush */
  12937. + outer_clean_range(phys_addr,
  12938. + phys_addr +
  12939. + (size_t) ioparam.size);
  12940. + } else if (resource == NULL) {
  12941. + ret = -EINVAL;
  12942. + goto out;
  12943. + }
  12944. +
  12945. + if (resource)
  12946. + vmcs_sm_release_resource(resource, 0);
  12947. +
  12948. + /* Done. */
  12949. + goto out;
  12950. + }
  12951. + break;
  12952. +
  12953. + /* Invalidate the cache for a given mapping. */
  12954. + case VMCS_SM_CMD_INVALID:
  12955. + {
  12956. + struct vmcs_sm_ioctl_cache ioparam;
  12957. +
  12958. + /* Get parameter data. */
  12959. + if (copy_from_user(&ioparam,
  12960. + (void *)arg, sizeof(ioparam)) != 0) {
  12961. + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
  12962. + __func__, cmdnr);
  12963. + ret = -EFAULT;
  12964. + goto out;
  12965. + }
  12966. +
  12967. + /* Locate resource from GUID.
  12968. + */
  12969. + resource =
  12970. + vmcs_sm_acquire_resource(file_data, ioparam.handle);
  12971. +
  12972. + if ((resource != NULL) && resource->res_cached) {
  12973. + dma_addr_t phys_addr = 0;
  12974. +
  12975. + resource->res_stats[INVALID]++;
  12976. +
  12977. + phys_addr =
  12978. + (dma_addr_t)((uint32_t)
  12979. + resource->res_base_mem &
  12980. + 0x3FFFFFFF);
  12981. + phys_addr += (dma_addr_t)mm_vc_mem_phys_addr;
  12982. +
  12983. + /* L2 cache invalidate */
  12984. + outer_inv_range(phys_addr,
  12985. + phys_addr +
  12986. + (size_t) ioparam.size);
  12987. +
  12988. + /* L1 cache invalidate */
  12989. + down_read(&current->mm->mmap_sem);
  12990. + vcsm_vma_cache_clean_page_range((unsigned long)
  12991. + ioparam.addr,
  12992. + (unsigned long)
  12993. + ioparam.addr +
  12994. + ioparam.size);
  12995. + up_read(&current->mm->mmap_sem);
  12996. + } else if (resource == NULL) {
  12997. + ret = -EINVAL;
  12998. + goto out;
  12999. + }
  13000. +
  13001. + if (resource)
  13002. + vmcs_sm_release_resource(resource, 0);
  13003. +
  13004. + /* Done.
  13005. + */
  13006. + goto out;
  13007. + }
  13008. + break;
  13009. +
  13010. + default:
  13011. + {
  13012. + ret = -EINVAL;
  13013. + goto out;
  13014. + }
  13015. + break;
  13016. + }
  13017. +
  13018. +out:
  13019. + return ret;
  13020. +}
  13021. +
  13022. +/* Device operations that we managed in this driver.
  13023. +*/
  13024. +static const struct file_operations vmcs_sm_ops = {
  13025. + .owner = THIS_MODULE,
  13026. + .unlocked_ioctl = vc_sm_ioctl,
  13027. + .open = vc_sm_open,
  13028. + .release = vc_sm_release,
  13029. + .mmap = vc_sm_mmap,
  13030. +};
  13031. +
  13032. +/* Creation of device.
  13033. +*/
  13034. +static int vc_sm_create_sharedmemory(void)
  13035. +{
  13036. + int ret;
  13037. +
  13038. + if (sm_state == NULL) {
  13039. + ret = -ENOMEM;
  13040. + goto out;
  13041. + }
  13042. +
  13043. + /* Create a device class for creating dev nodes.
  13044. + */
  13045. + sm_state->sm_class = class_create(THIS_MODULE, "vc-sm");
  13046. + if (IS_ERR(sm_state->sm_class)) {
  13047. + pr_err("[%s]: unable to create device class\n", __func__);
  13048. + ret = PTR_ERR(sm_state->sm_class);
  13049. + goto out;
  13050. + }
  13051. +
  13052. + /* Create a character driver.
  13053. + */
  13054. + ret = alloc_chrdev_region(&sm_state->sm_devid,
  13055. + DEVICE_MINOR, 1, DEVICE_NAME);
  13056. + if (ret != 0) {
  13057. + pr_err("[%s]: unable to allocate device number\n", __func__);
  13058. + goto out_dev_class_destroy;
  13059. + }
  13060. +
  13061. + cdev_init(&sm_state->sm_cdev, &vmcs_sm_ops);
  13062. + ret = cdev_add(&sm_state->sm_cdev, sm_state->sm_devid, 1);
  13063. + if (ret != 0) {
  13064. + pr_err("[%s]: unable to register device\n", __func__);
  13065. + goto out_chrdev_unreg;
  13066. + }
  13067. +
  13068. + /* Create a device node.
  13069. + */
  13070. + sm_state->sm_dev = device_create(sm_state->sm_class,
  13071. + NULL,
  13072. + MKDEV(MAJOR(sm_state->sm_devid),
  13073. + DEVICE_MINOR), NULL,
  13074. + DEVICE_NAME);
  13075. + if (IS_ERR(sm_state->sm_dev)) {
  13076. + pr_err("[%s]: unable to create device node\n", __func__);
  13077. + ret = PTR_ERR(sm_state->sm_dev);
  13078. + goto out_chrdev_del;
  13079. + }
  13080. +
  13081. + goto out;
  13082. +
  13083. +out_chrdev_del:
  13084. + cdev_del(&sm_state->sm_cdev);
  13085. +out_chrdev_unreg:
  13086. + unregister_chrdev_region(sm_state->sm_devid, 1);
  13087. +out_dev_class_destroy:
  13088. + class_destroy(sm_state->sm_class);
  13089. + sm_state->sm_class = NULL;
  13090. +out:
  13091. + return ret;
  13092. +}
  13093. +
  13094. +/* Termination of the device.
  13095. +*/
  13096. +static int vc_sm_remove_sharedmemory(void)
  13097. +{
  13098. + int ret;
  13099. +
  13100. + if (sm_state == NULL) {
  13101. + /* Nothing to do.
  13102. + */
  13103. + ret = 0;
  13104. + goto out;
  13105. + }
  13106. +
  13107. + /* Remove the sharedmemory character driver.
  13108. + */
  13109. + cdev_del(&sm_state->sm_cdev);
  13110. +
  13111. + /* Unregister region.
  13112. + */
  13113. + unregister_chrdev_region(sm_state->sm_devid, 1);
  13114. +
  13115. + ret = 0;
  13116. + goto out;
  13117. +
  13118. +out:
  13119. + return ret;
  13120. +}
  13121. +
  13122. +/* Videocore connected. */
  13123. +static void vc_sm_connected_init(void)
  13124. +{
  13125. + int ret;
  13126. + VCHI_INSTANCE_T vchi_instance;
  13127. + VCHI_CONNECTION_T *vchi_connection = NULL;
  13128. +
  13129. + pr_info("[%s]: start\n", __func__);
  13130. +
  13131. + /* Allocate memory for the state structure.
  13132. + */
  13133. + sm_state = kzalloc(sizeof(struct SM_STATE_T), GFP_KERNEL);
  13134. + if (sm_state == NULL) {
  13135. + pr_err("[%s]: failed to allocate memory\n", __func__);
  13136. + ret = -ENOMEM;
  13137. + goto out;
  13138. + }
  13139. +
  13140. + mutex_init(&sm_state->lock);
  13141. + mutex_init(&sm_state->map_lock);
  13142. +
  13143. + /* Initialize and create a VCHI connection for the shared memory service
  13144. + ** running on videocore.
  13145. + */
  13146. + ret = vchi_initialise(&vchi_instance);
  13147. + if (ret != 0) {
  13148. + pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
  13149. + __func__, ret);
  13150. +
  13151. + ret = -EIO;
  13152. + goto err_free_mem;
  13153. + }
  13154. +
  13155. + ret = vchi_connect(NULL, 0, vchi_instance);
  13156. + if (ret != 0) {
  13157. + pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
  13158. + __func__, ret);
  13159. +
  13160. + ret = -EIO;
  13161. + goto err_free_mem;
  13162. + }
  13163. +
  13164. + /* Initialize an instance of the shared memory service. */
  13165. + sm_state->sm_handle =
  13166. + vc_vchi_sm_init(vchi_instance, &vchi_connection, 1);
  13167. + if (sm_state->sm_handle == NULL) {
  13168. + pr_err("[%s]: failed to initialize shared memory service\n",
  13169. + __func__);
  13170. +
  13171. + ret = -EPERM;
  13172. + goto err_free_mem;
  13173. + }
  13174. +
  13175. + /* Create a debug fs directory entry (root). */
  13176. + sm_state->dir_root = debugfs_create_dir(VC_SM_DIR_ROOT_NAME, NULL);
  13177. + if (!sm_state->dir_root) {
  13178. + pr_err("[%s]: failed to create \'%s\' directory entry\n",
  13179. + __func__, VC_SM_DIR_ROOT_NAME);
  13180. +
  13181. + ret = -EPERM;
  13182. + goto err_stop_sm_service;
  13183. + }
  13184. +
  13185. + sm_state->dir_state.show = &vc_sm_global_state_show;
  13186. + sm_state->dir_state.dir_entry = debugfs_create_file(VC_SM_STATE,
  13187. + S_IRUGO, sm_state->dir_root, &sm_state->dir_state,
  13188. + &vc_sm_debug_fs_fops);
  13189. +
  13190. + sm_state->dir_stats.show = &vc_sm_global_statistics_show;
  13191. + sm_state->dir_stats.dir_entry = debugfs_create_file(VC_SM_STATS,
  13192. + S_IRUGO, sm_state->dir_root, &sm_state->dir_stats,
  13193. + &vc_sm_debug_fs_fops);
  13194. +
  13195. + /* Create the proc entry children. */
  13196. + sm_state->dir_alloc = debugfs_create_dir(VC_SM_DIR_ALLOC_NAME,
  13197. + sm_state->dir_root);
  13198. +
  13199. + /* Create a shared memory device. */
  13200. + ret = vc_sm_create_sharedmemory();
  13201. + if (ret != 0) {
  13202. + pr_err("[%s]: failed to create shared memory device\n",
  13203. + __func__);
  13204. + goto err_remove_debugfs;
  13205. + }
  13206. +
  13207. + INIT_LIST_HEAD(&sm_state->map_list);
  13208. + INIT_LIST_HEAD(&sm_state->resource_list);
  13209. +
  13210. + sm_state->data_knl = vc_sm_create_priv_data(0);
  13211. + if (sm_state->data_knl == NULL) {
  13212. + pr_err("[%s]: failed to create kernel private data tracker\n",
  13213. + __func__);
  13214. + goto err_remove_shared_memory;
  13215. + }
  13216. +
  13217. + /* Done!
  13218. + */
  13219. + sm_inited = 1;
  13220. + goto out;
  13221. +
  13222. +err_remove_shared_memory:
  13223. + vc_sm_remove_sharedmemory();
  13224. +err_remove_debugfs:
  13225. + debugfs_remove_recursive(sm_state->dir_root);
  13226. +err_stop_sm_service:
  13227. + vc_vchi_sm_stop(&sm_state->sm_handle);
  13228. +err_free_mem:
  13229. + kfree(sm_state);
  13230. +out:
  13231. + pr_info("[%s]: end - returning %d\n", __func__, ret);
  13232. +}
  13233. +
  13234. +/* Driver loading. */
  13235. +static int __init vc_sm_init(void)
  13236. +{
  13237. + pr_info("vc-sm: Videocore shared memory driver\n");
  13238. + vchiq_add_connected_callback(vc_sm_connected_init);
  13239. + return 0;
  13240. +}
  13241. +
  13242. +/* Driver unloading. */
  13243. +static void __exit vc_sm_exit(void)
  13244. +{
  13245. + pr_debug("[%s]: start\n", __func__);
  13246. + if (sm_inited) {
  13247. + /* Remove shared memory device.
  13248. + */
  13249. + vc_sm_remove_sharedmemory();
  13250. +
  13251. + /* Remove all proc entries.
  13252. + */
  13253. + debugfs_remove_recursive(sm_state->dir_root);
  13254. +
  13255. + /* Stop the videocore shared memory service.
  13256. + */
  13257. + vc_vchi_sm_stop(&sm_state->sm_handle);
  13258. +
  13259. + /* Free the memory for the state structure.
  13260. + */
  13261. + mutex_destroy(&(sm_state->map_lock));
  13262. + kfree(sm_state);
  13263. + }
  13264. +
  13265. + pr_debug("[%s]: end\n", __func__);
  13266. +}
  13267. +
  13268. +#if defined(__KERNEL__)
  13269. +/* Allocate a shared memory handle and block. */
  13270. +int vc_sm_alloc(VC_SM_ALLOC_T *alloc, int *handle)
  13271. +{
  13272. + struct vmcs_sm_ioctl_alloc ioparam = { 0 };
  13273. + int ret;
  13274. + struct SM_RESOURCE_T *resource;
  13275. +
  13276. + /* Validate we can work with this device.
  13277. + */
  13278. + if (sm_state == NULL || alloc == NULL || handle == NULL) {
  13279. + pr_err("[%s]: invalid input\n", __func__);
  13280. + return -EPERM;
  13281. + }
  13282. +
  13283. + ioparam.size = alloc->base_unit;
  13284. + ioparam.num = alloc->num_unit;
  13285. + ioparam.cached =
  13286. + alloc->type == VC_SM_ALLOC_CACHED ? VMCS_SM_CACHE_VC : 0;
  13287. +
  13288. + ret = vc_sm_ioctl_alloc(sm_state->data_knl, &ioparam);
  13289. +
  13290. + if (ret == 0) {
  13291. + resource =
  13292. + vmcs_sm_acquire_resource(sm_state->data_knl,
  13293. + ioparam.handle);
  13294. + if (resource) {
  13295. + resource->pid = 0;
  13296. + vmcs_sm_release_resource(resource, 0);
  13297. +
  13298. + /* Assign valid handle at this time.
  13299. + */
  13300. + *handle = ioparam.handle;
  13301. + } else {
  13302. + ret = -ENOMEM;
  13303. + }
  13304. + }
  13305. +
  13306. + return ret;
  13307. +}
  13308. +EXPORT_SYMBOL_GPL(vc_sm_alloc);
  13309. +
  13310. +/* Get an internal resource handle mapped from the external one.
  13311. +*/
  13312. +int vc_sm_int_handle(int handle)
  13313. +{
  13314. + struct SM_RESOURCE_T *resource;
  13315. + int ret = 0;
  13316. +
  13317. + /* Validate we can work with this device.
  13318. + */
  13319. + if (sm_state == NULL || handle == 0) {
  13320. + pr_err("[%s]: invalid input\n", __func__);
  13321. + return 0;
  13322. + }
  13323. +
  13324. + /* Locate resource from GUID.
  13325. + */
  13326. + resource = vmcs_sm_acquire_resource(sm_state->data_knl, handle);
  13327. + if (resource) {
  13328. + ret = resource->res_handle;
  13329. + vmcs_sm_release_resource(resource, 0);
  13330. + }
  13331. +
  13332. + return ret;
  13333. +}
  13334. +EXPORT_SYMBOL_GPL(vc_sm_int_handle);
  13335. +
  13336. +/* Free a previously allocated shared memory handle and block.
  13337. +*/
  13338. +int vc_sm_free(int handle)
  13339. +{
  13340. + struct vmcs_sm_ioctl_free ioparam = { handle };
  13341. +
  13342. + /* Validate we can work with this device.
  13343. + */
  13344. + if (sm_state == NULL || handle == 0) {
  13345. + pr_err("[%s]: invalid input\n", __func__);
  13346. + return -EPERM;
  13347. + }
  13348. +
  13349. + return vc_sm_ioctl_free(sm_state->data_knl, &ioparam);
  13350. +}
  13351. +EXPORT_SYMBOL_GPL(vc_sm_free);
  13352. +
  13353. +/* Lock a memory handle for use by kernel.
  13354. +*/
  13355. +int vc_sm_lock(int handle, VC_SM_LOCK_CACHE_MODE_T mode,
  13356. + long unsigned int *data)
  13357. +{
  13358. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  13359. + int ret;
  13360. +
  13361. + /* Validate we can work with this device.
  13362. + */
  13363. + if (sm_state == NULL || handle == 0 || data == NULL) {
  13364. + pr_err("[%s]: invalid input\n", __func__);
  13365. + return -EPERM;
  13366. + }
  13367. +
  13368. + *data = 0;
  13369. +
  13370. + ioparam.handle = handle;
  13371. + ret = vc_sm_ioctl_lock(sm_state->data_knl,
  13372. + &ioparam,
  13373. + 1,
  13374. + ((mode ==
  13375. + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
  13376. + VMCS_SM_CACHE_NONE), 0);
  13377. +
  13378. + *data = ioparam.addr;
  13379. + return ret;
  13380. +}
  13381. +EXPORT_SYMBOL_GPL(vc_sm_lock);
  13382. +
  13383. +/* Unlock a memory handle in use by kernel.
  13384. +*/
  13385. +int vc_sm_unlock(int handle, int flush, int no_vc_unlock)
  13386. +{
  13387. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  13388. +
  13389. + /* Validate we can work with this device.
  13390. + */
  13391. + if (sm_state == NULL || handle == 0) {
  13392. + pr_err("[%s]: invalid input\n", __func__);
  13393. + return -EPERM;
  13394. + }
  13395. +
  13396. + ioparam.handle = handle;
  13397. + return vc_sm_ioctl_unlock(sm_state->data_knl,
  13398. + &ioparam, flush, 0, no_vc_unlock);
  13399. +}
  13400. +EXPORT_SYMBOL_GPL(vc_sm_unlock);
  13401. +
  13402. +/* Map a shared memory region for use by kernel.
  13403. +*/
  13404. +int vc_sm_map(int handle, unsigned int sm_addr, VC_SM_LOCK_CACHE_MODE_T mode,
  13405. + long unsigned int *data)
  13406. +{
  13407. + struct vmcs_sm_ioctl_lock_unlock ioparam;
  13408. + int ret;
  13409. +
  13410. + /* Validate we can work with this device.
  13411. + */
  13412. + if (sm_state == NULL || handle == 0 || data == NULL || sm_addr == 0) {
  13413. + pr_err("[%s]: invalid input\n", __func__);
  13414. + return -EPERM;
  13415. + }
  13416. +
  13417. + *data = 0;
  13418. +
  13419. + ioparam.handle = handle;
  13420. + ret = vc_sm_ioctl_lock(sm_state->data_knl,
  13421. + &ioparam,
  13422. + 1,
  13423. + ((mode ==
  13424. + VC_SM_LOCK_CACHED) ? VMCS_SM_CACHE_HOST :
  13425. + VMCS_SM_CACHE_NONE), sm_addr);
  13426. +
  13427. + *data = ioparam.addr;
  13428. + return ret;
  13429. +}
  13430. +EXPORT_SYMBOL_GPL(vc_sm_map);
  13431. +#endif
  13432. +
  13433. +late_initcall(vc_sm_init);
  13434. +module_exit(vc_sm_exit);
  13435. +
  13436. +MODULE_AUTHOR("Broadcom");
  13437. +MODULE_DESCRIPTION("VideoCore SharedMemory Driver");
  13438. +MODULE_LICENSE("GPL v2");
  13439. diff -Nur linux-3.16.2/drivers/char/hw_random/bcm2708-rng.c linux-3.16-rpi/drivers/char/hw_random/bcm2708-rng.c
  13440. --- linux-3.16.2/drivers/char/hw_random/bcm2708-rng.c 1970-01-01 01:00:00.000000000 +0100
  13441. +++ linux-3.16-rpi/drivers/char/hw_random/bcm2708-rng.c 2014-09-14 19:03:14.000000000 +0200
  13442. @@ -0,0 +1,118 @@
  13443. +/**
  13444. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  13445. + *
  13446. + * Redistribution and use in source and binary forms, with or without
  13447. + * modification, are permitted provided that the following conditions
  13448. + * are met:
  13449. + * 1. Redistributions of source code must retain the above copyright
  13450. + * notice, this list of conditions, and the following disclaimer,
  13451. + * without modification.
  13452. + * 2. Redistributions in binary form must reproduce the above copyright
  13453. + * notice, this list of conditions and the following disclaimer in the
  13454. + * documentation and/or other materials provided with the distribution.
  13455. + * 3. The names of the above-listed copyright holders may not be used
  13456. + * to endorse or promote products derived from this software without
  13457. + * specific prior written permission.
  13458. + *
  13459. + * ALTERNATIVELY, this software may be distributed under the terms of the
  13460. + * GNU General Public License ("GPL") version 2, as published by the Free
  13461. + * Software Foundation.
  13462. + *
  13463. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  13464. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  13465. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13466. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  13467. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13468. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  13469. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  13470. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  13471. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  13472. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  13473. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  13474. + */
  13475. +
  13476. +#include <linux/kernel.h>
  13477. +#include <linux/module.h>
  13478. +#include <linux/init.h>
  13479. +#include <linux/hw_random.h>
  13480. +#include <linux/printk.h>
  13481. +
  13482. +#include <asm/io.h>
  13483. +#include <mach/hardware.h>
  13484. +#include <mach/platform.h>
  13485. +
  13486. +#define RNG_CTRL (0x0)
  13487. +#define RNG_STATUS (0x4)
  13488. +#define RNG_DATA (0x8)
  13489. +#define RNG_FF_THRESHOLD (0xc)
  13490. +
  13491. +/* enable rng */
  13492. +#define RNG_RBGEN 0x1
  13493. +/* double speed, less random mode */
  13494. +#define RNG_RBG2X 0x2
  13495. +
  13496. +/* the initial numbers generated are "less random" so will be discarded */
  13497. +#define RNG_WARMUP_COUNT 0x40000
  13498. +
  13499. +static int bcm2708_rng_data_read(struct hwrng *rng, u32 *buffer)
  13500. +{
  13501. + void __iomem *rng_base = (void __iomem *)rng->priv;
  13502. + unsigned words;
  13503. + /* wait for a random number to be in fifo */
  13504. + do {
  13505. + words = __raw_readl(rng_base + RNG_STATUS)>>24;
  13506. + }
  13507. + while (words == 0);
  13508. + /* read the random number */
  13509. + *buffer = __raw_readl(rng_base + RNG_DATA);
  13510. + return 4;
  13511. +}
  13512. +
  13513. +static struct hwrng bcm2708_rng_ops = {
  13514. + .name = "bcm2708",
  13515. + .data_read = bcm2708_rng_data_read,
  13516. +};
  13517. +
  13518. +static int __init bcm2708_rng_init(void)
  13519. +{
  13520. + void __iomem *rng_base;
  13521. + int err;
  13522. +
  13523. + /* map peripheral */
  13524. + rng_base = ioremap(RNG_BASE, 0x10);
  13525. + pr_info("bcm2708_rng_init=%p\n", rng_base);
  13526. + if (!rng_base) {
  13527. + pr_err("bcm2708_rng_init failed to ioremap\n");
  13528. + return -ENOMEM;
  13529. + }
  13530. + bcm2708_rng_ops.priv = (unsigned long)rng_base;
  13531. +
  13532. + /* set warm-up count & enable */
  13533. + __raw_writel(RNG_WARMUP_COUNT, rng_base + RNG_STATUS);
  13534. + __raw_writel(RNG_RBGEN, rng_base + RNG_CTRL);
  13535. +
  13536. + /* register driver */
  13537. + err = hwrng_register(&bcm2708_rng_ops);
  13538. + if (err) {
  13539. + pr_err("bcm2708_rng_init hwrng_register()=%d\n", err);
  13540. + iounmap(rng_base);
  13541. + }
  13542. + return err;
  13543. +}
  13544. +
  13545. +static void __exit bcm2708_rng_exit(void)
  13546. +{
  13547. + void __iomem *rng_base = (void __iomem *)bcm2708_rng_ops.priv;
  13548. + pr_info("bcm2708_rng_exit\n");
  13549. + /* disable rng hardware */
  13550. + __raw_writel(0, rng_base + RNG_CTRL);
  13551. + /* unregister driver */
  13552. + hwrng_unregister(&bcm2708_rng_ops);
  13553. + iounmap(rng_base);
  13554. +}
  13555. +
  13556. +module_init(bcm2708_rng_init);
  13557. +module_exit(bcm2708_rng_exit);
  13558. +
  13559. +MODULE_DESCRIPTION("BCM2708 H/W Random Number Generator (RNG) driver");
  13560. +MODULE_LICENSE("GPL and additional rights");
  13561. diff -Nur linux-3.16.2/drivers/char/hw_random/Kconfig linux-3.16-rpi/drivers/char/hw_random/Kconfig
  13562. --- linux-3.16.2/drivers/char/hw_random/Kconfig 2014-09-06 01:37:11.000000000 +0200
  13563. +++ linux-3.16-rpi/drivers/char/hw_random/Kconfig 2014-09-14 19:03:14.000000000 +0200
  13564. @@ -320,6 +320,17 @@
  13565. If unsure, say Y.
  13566. +config HW_RANDOM_BCM2708
  13567. + tristate "BCM2708 generic true random number generator support"
  13568. + depends on HW_RANDOM && ARCH_BCM2708
  13569. + ---help---
  13570. + This driver provides the kernel-side support for the BCM2708 hardware.
  13571. +
  13572. + To compile this driver as a module, choose M here: the
  13573. + module will be called bcm2708-rng.
  13574. +
  13575. + If unsure, say N.
  13576. +
  13577. config HW_RANDOM_MSM
  13578. tristate "Qualcomm SoCs Random Number Generator support"
  13579. depends on HW_RANDOM && ARCH_QCOM
  13580. diff -Nur linux-3.16.2/drivers/char/hw_random/Makefile linux-3.16-rpi/drivers/char/hw_random/Makefile
  13581. --- linux-3.16.2/drivers/char/hw_random/Makefile 2014-09-06 01:37:11.000000000 +0200
  13582. +++ linux-3.16-rpi/drivers/char/hw_random/Makefile 2014-09-14 19:03:14.000000000 +0200
  13583. @@ -28,4 +28,5 @@
  13584. obj-$(CONFIG_HW_RANDOM_EXYNOS) += exynos-rng.o
  13585. obj-$(CONFIG_HW_RANDOM_TPM) += tpm-rng.o
  13586. obj-$(CONFIG_HW_RANDOM_BCM2835) += bcm2835-rng.o
  13587. +obj-$(CONFIG_HW_RANDOM_BCM2708) += bcm2708-rng.o
  13588. obj-$(CONFIG_HW_RANDOM_MSM) += msm-rng.o
  13589. diff -Nur linux-3.16.2/drivers/char/Kconfig linux-3.16-rpi/drivers/char/Kconfig
  13590. --- linux-3.16.2/drivers/char/Kconfig 2014-09-06 01:37:11.000000000 +0200
  13591. +++ linux-3.16-rpi/drivers/char/Kconfig 2014-09-14 19:03:14.000000000 +0200
  13592. @@ -581,6 +581,8 @@
  13593. source "drivers/s390/char/Kconfig"
  13594. +source "drivers/char/broadcom/Kconfig"
  13595. +
  13596. config MSM_SMD_PKT
  13597. bool "Enable device interface for some SMD packet ports"
  13598. default n
  13599. diff -Nur linux-3.16.2/drivers/char/Makefile linux-3.16-rpi/drivers/char/Makefile
  13600. --- linux-3.16.2/drivers/char/Makefile 2014-09-06 01:37:11.000000000 +0200
  13601. +++ linux-3.16-rpi/drivers/char/Makefile 2014-09-14 19:03:14.000000000 +0200
  13602. @@ -61,3 +61,5 @@
  13603. js-rtc-y = rtc.o
  13604. obj-$(CONFIG_TILE_SROM) += tile-srom.o
  13605. +
  13606. +obj-$(CONFIG_BRCM_CHAR_DRIVERS) += broadcom/
  13607. diff -Nur linux-3.16.2/drivers/cpufreq/bcm2835-cpufreq.c linux-3.16-rpi/drivers/cpufreq/bcm2835-cpufreq.c
  13608. --- linux-3.16.2/drivers/cpufreq/bcm2835-cpufreq.c 1970-01-01 01:00:00.000000000 +0100
  13609. +++ linux-3.16-rpi/drivers/cpufreq/bcm2835-cpufreq.c 2014-09-14 19:03:14.000000000 +0200
  13610. @@ -0,0 +1,239 @@
  13611. +/*****************************************************************************
  13612. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  13613. +*
  13614. +* Unless you and Broadcom execute a separate written software license
  13615. +* agreement governing use of this software, this software is licensed to you
  13616. +* under the terms of the GNU General Public License version 2, available at
  13617. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  13618. +*
  13619. +* Notwithstanding the above, under no circumstances may you combine this
  13620. +* software in any way with any other Broadcom software provided under a
  13621. +* license other than the GPL, without Broadcom's express prior written
  13622. +* consent.
  13623. +*****************************************************************************/
  13624. +
  13625. +/*****************************************************************************
  13626. +* FILENAME: bcm2835-cpufreq.h
  13627. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  13628. +* processor. Messages are sent to Videocore either setting or requesting the
  13629. +* frequency of the ARM in order to match an appropiate frequency to the current
  13630. +* usage of the processor. The policy which selects the frequency to use is
  13631. +* defined in the kernel .config file, but can be changed during runtime.
  13632. +*****************************************************************************/
  13633. +
  13634. +/* ---------- INCLUDES ---------- */
  13635. +#include <linux/kernel.h>
  13636. +#include <linux/init.h>
  13637. +#include <linux/module.h>
  13638. +#include <linux/cpufreq.h>
  13639. +#include <mach/vcio.h>
  13640. +
  13641. +/* ---------- DEFINES ---------- */
  13642. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  13643. +#define MODULE_NAME "bcm2835-cpufreq"
  13644. +
  13645. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  13646. +
  13647. +/* debug printk macros */
  13648. +#ifdef CPUFREQ_DEBUG_ENABLE
  13649. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  13650. +#else
  13651. +#define print_debug(fmt,...)
  13652. +#endif
  13653. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  13654. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  13655. +
  13656. +/* tag part of the message */
  13657. +struct vc_msg_tag {
  13658. + uint32_t tag_id; /* the message id */
  13659. + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
  13660. + uint32_t data_size; /* amount of data being sent or received */
  13661. + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
  13662. + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
  13663. +};
  13664. +
  13665. +/* message structure to be sent to videocore */
  13666. +struct vc_msg {
  13667. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  13668. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  13669. + struct vc_msg_tag tag; /* the tag structure above to make */
  13670. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  13671. +};
  13672. +
  13673. +/* ---------- GLOBALS ---------- */
  13674. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  13675. +
  13676. +/*
  13677. + ===============================================
  13678. + clk_rate either gets or sets the clock rates.
  13679. + ===============================================
  13680. +*/
  13681. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  13682. +{
  13683. + int s, actual_rate=0;
  13684. + struct vc_msg msg;
  13685. +
  13686. + /* wipe all previous message data */
  13687. + memset(&msg, 0, sizeof msg);
  13688. +
  13689. + msg.msg_size = sizeof msg;
  13690. +
  13691. + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
  13692. + msg.tag.buffer_size = 8;
  13693. + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
  13694. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  13695. + msg.tag.val = arm_rate * 1000;
  13696. +
  13697. + /* send the message */
  13698. + s = bcm_mailbox_property(&msg, sizeof msg);
  13699. +
  13700. + /* check if it was all ok and return the rate in KHz */
  13701. + if (s == 0 && (msg.request_code & 0x80000000))
  13702. + actual_rate = msg.tag.val/1000;
  13703. +
  13704. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
  13705. + return actual_rate;
  13706. +}
  13707. +
  13708. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  13709. +{
  13710. + int s;
  13711. + int arm_rate = 0;
  13712. + struct vc_msg msg;
  13713. +
  13714. + /* wipe all previous message data */
  13715. + memset(&msg, 0, sizeof msg);
  13716. +
  13717. + msg.msg_size = sizeof msg;
  13718. + msg.tag.tag_id = tag;
  13719. + msg.tag.buffer_size = 8;
  13720. + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
  13721. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  13722. +
  13723. + /* send the message */
  13724. + s = bcm_mailbox_property(&msg, sizeof msg);
  13725. +
  13726. + /* check if it was all ok and return the rate in KHz */
  13727. + if (s == 0 && (msg.request_code & 0x80000000))
  13728. + arm_rate = msg.tag.val/1000;
  13729. +
  13730. + print_debug("%s frequency = %d\n",
  13731. + tag == VCMSG_GET_CLOCK_RATE ? "Current":
  13732. + tag == VCMSG_GET_MIN_CLOCK ? "Min":
  13733. + tag == VCMSG_GET_MAX_CLOCK ? "Max":
  13734. + "Unexpected", arm_rate);
  13735. +
  13736. + return arm_rate;
  13737. +}
  13738. +
  13739. +/*
  13740. + ====================================================
  13741. + Module Initialisation registers the cpufreq driver
  13742. + ====================================================
  13743. +*/
  13744. +static int __init bcm2835_cpufreq_module_init(void)
  13745. +{
  13746. + print_debug("IN\n");
  13747. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  13748. +}
  13749. +
  13750. +/*
  13751. + =============
  13752. + Module exit
  13753. + =============
  13754. +*/
  13755. +static void __exit bcm2835_cpufreq_module_exit(void)
  13756. +{
  13757. + print_debug("IN\n");
  13758. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  13759. + return;
  13760. +}
  13761. +
  13762. +/*
  13763. + ==============================================================
  13764. + Initialisation function sets up the CPU policy for first use
  13765. + ==============================================================
  13766. +*/
  13767. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  13768. +{
  13769. + /* measured value of how long it takes to change frequency */
  13770. + policy->cpuinfo.transition_latency = 355000; /* ns */
  13771. +
  13772. + /* now find out what the maximum and minimum frequencies are */
  13773. + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
  13774. + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
  13775. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  13776. +
  13777. + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
  13778. + return 0;
  13779. +}
  13780. +
  13781. +/*
  13782. + =================================================================================
  13783. + Target function chooses the most appropriate frequency from the table to enable
  13784. + =================================================================================
  13785. +*/
  13786. +
  13787. +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
  13788. +{
  13789. + unsigned int target = target_freq;
  13790. +#ifdef CPUFREQ_DEBUG_ENABLE
  13791. + unsigned int cur = policy->cur;
  13792. +#endif
  13793. + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
  13794. +
  13795. + /* if we are above min and using ondemand, then just use max */
  13796. + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
  13797. + target = policy->max;
  13798. + /* if the frequency is the same, just quit */
  13799. + if (target == policy->cur)
  13800. + return 0;
  13801. +
  13802. + /* otherwise were good to set the clock frequency */
  13803. + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
  13804. +
  13805. + if (!policy->cur)
  13806. + {
  13807. + print_err("Error occurred setting a new frequency (%d)!\n", target);
  13808. + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  13809. + return -EINVAL;
  13810. + }
  13811. + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
  13812. + return 0;
  13813. +}
  13814. +
  13815. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  13816. +{
  13817. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  13818. + print_debug("cpu=%d\n", actual_rate);
  13819. + return actual_rate;
  13820. +}
  13821. +
  13822. +/*
  13823. + =================================================================================
  13824. + Verify ensures that when a policy is changed, it is suitable for the CPU to use
  13825. + =================================================================================
  13826. +*/
  13827. +
  13828. +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
  13829. +{
  13830. + print_info("switching to governor %s\n", policy->governor->name);
  13831. + return 0;
  13832. +}
  13833. +
  13834. +
  13835. +/* the CPUFreq driver */
  13836. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  13837. + .name = "BCM2835 CPUFreq",
  13838. + .init = bcm2835_cpufreq_driver_init,
  13839. + .verify = bcm2835_cpufreq_driver_verify,
  13840. + .target = bcm2835_cpufreq_driver_target,
  13841. + .get = bcm2835_cpufreq_driver_get
  13842. +};
  13843. +
  13844. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  13845. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  13846. +MODULE_LICENSE("GPL");
  13847. +
  13848. +module_init(bcm2835_cpufreq_module_init);
  13849. +module_exit(bcm2835_cpufreq_module_exit);
  13850. diff -Nur linux-3.16.2/drivers/cpufreq/Kconfig.arm linux-3.16-rpi/drivers/cpufreq/Kconfig.arm
  13851. --- linux-3.16.2/drivers/cpufreq/Kconfig.arm 2014-09-06 01:37:11.000000000 +0200
  13852. +++ linux-3.16-rpi/drivers/cpufreq/Kconfig.arm 2014-09-14 19:03:14.000000000 +0200
  13853. @@ -241,6 +241,14 @@
  13854. help
  13855. This adds the CPUFreq driver support for SPEAr SOCs.
  13856. +config ARM_BCM2835_CPUFREQ
  13857. + bool "BCM2835 Driver"
  13858. + default y
  13859. + help
  13860. + This adds the CPUFreq driver for BCM2835
  13861. +
  13862. + If in doubt, say N.
  13863. +
  13864. config ARM_TEGRA_CPUFREQ
  13865. bool "TEGRA CPUFreq support"
  13866. depends on ARCH_TEGRA
  13867. diff -Nur linux-3.16.2/drivers/cpufreq/Makefile linux-3.16-rpi/drivers/cpufreq/Makefile
  13868. --- linux-3.16.2/drivers/cpufreq/Makefile 2014-09-06 01:37:11.000000000 +0200
  13869. +++ linux-3.16-rpi/drivers/cpufreq/Makefile 2014-09-14 19:03:14.000000000 +0200
  13870. @@ -75,6 +75,7 @@
  13871. obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
  13872. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  13873. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  13874. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  13875. obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
  13876. obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
  13877. diff -Nur linux-3.16.2/drivers/dma/bcm2708-dmaengine.c linux-3.16-rpi/drivers/dma/bcm2708-dmaengine.c
  13878. --- linux-3.16.2/drivers/dma/bcm2708-dmaengine.c 1970-01-01 01:00:00.000000000 +0100
  13879. +++ linux-3.16-rpi/drivers/dma/bcm2708-dmaengine.c 2014-09-14 19:03:15.000000000 +0200
  13880. @@ -0,0 +1,1041 @@
  13881. +/*
  13882. + * BCM2835 DMA engine support
  13883. + *
  13884. + * This driver supports cyclic and scatter/gather DMA transfers.
  13885. + *
  13886. + * Author: Florian Meier <florian.meier@koalo.de>
  13887. + * Gellert Weisz <gellert@raspberrypi.org>
  13888. + * Copyright 2013-2014
  13889. + *
  13890. + * Based on
  13891. + * OMAP DMAengine support by Russell King
  13892. + *
  13893. + * BCM2708 DMA Driver
  13894. + * Copyright (C) 2010 Broadcom
  13895. + *
  13896. + * Raspberry Pi PCM I2S ALSA Driver
  13897. + * Copyright (c) by Phil Poole 2013
  13898. + *
  13899. + * MARVELL MMP Peripheral DMA Driver
  13900. + * Copyright 2012 Marvell International Ltd.
  13901. + *
  13902. + * This program is free software; you can redistribute it and/or modify
  13903. + * it under the terms of the GNU General Public License as published by
  13904. + * the Free Software Foundation; either version 2 of the License, or
  13905. + * (at your option) any later version.
  13906. + *
  13907. + * This program is distributed in the hope that it will be useful,
  13908. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13909. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13910. + * GNU General Public License for more details.
  13911. + */
  13912. +
  13913. +#include <linux/dmaengine.h>
  13914. +#include <linux/dma-mapping.h>
  13915. +#include <linux/err.h>
  13916. +#include <linux/init.h>
  13917. +#include <linux/interrupt.h>
  13918. +#include <linux/list.h>
  13919. +#include <linux/module.h>
  13920. +#include <linux/platform_device.h>
  13921. +#include <linux/slab.h>
  13922. +#include <linux/io.h>
  13923. +#include <linux/spinlock.h>
  13924. +
  13925. +#ifndef CONFIG_OF
  13926. +
  13927. +/* dma manager */
  13928. +#include <mach/dma.h>
  13929. +
  13930. +//#define DMA_COMPLETE DMA_SUCCESS
  13931. +
  13932. +#endif
  13933. +
  13934. +#include <linux/of.h>
  13935. +#include <linux/of_dma.h>
  13936. +
  13937. +#include "virt-dma.h"
  13938. +
  13939. +
  13940. +struct bcm2835_dmadev {
  13941. + struct dma_device ddev;
  13942. + spinlock_t lock;
  13943. + void __iomem *base;
  13944. + struct device_dma_parameters dma_parms;
  13945. +};
  13946. +
  13947. +struct bcm2835_dma_cb {
  13948. + uint32_t info;
  13949. + uint32_t src;
  13950. + uint32_t dst;
  13951. + uint32_t length;
  13952. + uint32_t stride;
  13953. + uint32_t next;
  13954. + uint32_t pad[2];
  13955. +};
  13956. +
  13957. +struct bcm2835_chan {
  13958. + struct virt_dma_chan vc;
  13959. + struct list_head node;
  13960. +
  13961. + struct dma_slave_config cfg;
  13962. + bool cyclic;
  13963. +
  13964. + int ch;
  13965. + struct bcm2835_desc *desc;
  13966. +
  13967. + void __iomem *chan_base;
  13968. + int irq_number;
  13969. +
  13970. + unsigned int dreq;
  13971. +};
  13972. +
  13973. +struct bcm2835_desc {
  13974. + struct virt_dma_desc vd;
  13975. + enum dma_transfer_direction dir;
  13976. +
  13977. + unsigned int control_block_size;
  13978. + struct bcm2835_dma_cb *control_block_base;
  13979. + dma_addr_t control_block_base_phys;
  13980. +
  13981. + unsigned int frames;
  13982. + size_t size;
  13983. +};
  13984. +
  13985. +#define BCM2835_DMA_CS 0x00
  13986. +#define BCM2835_DMA_ADDR 0x04
  13987. +#define BCM2835_DMA_SOURCE_AD 0x0c
  13988. +#define BCM2835_DMA_DEST_AD 0x10
  13989. +#define BCM2835_DMA_NEXTCB 0x1C
  13990. +
  13991. +/* DMA CS Control and Status bits */
  13992. +#define BCM2835_DMA_ACTIVE BIT(0)
  13993. +#define BCM2835_DMA_INT BIT(2)
  13994. +#define BCM2835_DMA_ISPAUSED BIT(4) /* Pause requested or not active */
  13995. +#define BCM2835_DMA_ISHELD BIT(5) /* Is held by DREQ flow control */
  13996. +#define BCM2835_DMA_ERR BIT(8)
  13997. +#define BCM2835_DMA_ABORT BIT(30) /* Stop current CB, go to next, WO */
  13998. +#define BCM2835_DMA_RESET BIT(31) /* WO, self clearing */
  13999. +
  14000. +#define BCM2835_DMA_INT_EN BIT(0)
  14001. +#define BCM2835_DMA_WAIT_RESP BIT(3)
  14002. +#define BCM2835_DMA_D_INC BIT(4)
  14003. +#define BCM2835_DMA_D_WIDTH BIT(5)
  14004. +#define BCM2835_DMA_D_DREQ BIT(6)
  14005. +#define BCM2835_DMA_S_INC BIT(8)
  14006. +#define BCM2835_DMA_S_WIDTH BIT(9)
  14007. +#define BCM2835_DMA_S_DREQ BIT(10)
  14008. +
  14009. +#define BCM2835_DMA_PER_MAP(x) ((x) << 16)
  14010. +#define BCM2835_DMA_WAITS(x) (((x)&0x1f) << 21)
  14011. +
  14012. +#define SDHCI_BCM_DMA_WAITS 0 /* delays slowing DMA transfers: 0-31 */
  14013. +
  14014. +#define BCM2835_DMA_DATA_TYPE_S8 1
  14015. +#define BCM2835_DMA_DATA_TYPE_S16 2
  14016. +#define BCM2835_DMA_DATA_TYPE_S32 4
  14017. +#define BCM2835_DMA_DATA_TYPE_S128 16
  14018. +
  14019. +#define BCM2835_DMA_BULK_MASK BIT(0)
  14020. +#define BCM2835_DMA_FIQ_MASK (BIT(2) | BIT(3))
  14021. +
  14022. +
  14023. +/* Valid only for channels 0 - 14, 15 has its own base address */
  14024. +#define BCM2835_DMA_CHAN(n) ((n) << 8) /* Base address */
  14025. +#define BCM2835_DMA_CHANIO(base, n) ((base) + BCM2835_DMA_CHAN(n))
  14026. +
  14027. +#define MAX_LITE_TRANSFER 32768
  14028. +#define MAX_NORMAL_TRANSFER 1073741824
  14029. +
  14030. +static inline struct bcm2835_dmadev *to_bcm2835_dma_dev(struct dma_device *d)
  14031. +{
  14032. + return container_of(d, struct bcm2835_dmadev, ddev);
  14033. +}
  14034. +
  14035. +static inline struct bcm2835_chan *to_bcm2835_dma_chan(struct dma_chan *c)
  14036. +{
  14037. + return container_of(c, struct bcm2835_chan, vc.chan);
  14038. +}
  14039. +
  14040. +static inline struct bcm2835_desc *to_bcm2835_dma_desc(
  14041. + struct dma_async_tx_descriptor *t)
  14042. +{
  14043. + return container_of(t, struct bcm2835_desc, vd.tx);
  14044. +}
  14045. +
  14046. +static void dma_dumpregs(struct bcm2835_chan *c)
  14047. +{
  14048. + pr_debug("-------------DMA DUMPREGS-------------\n");
  14049. + pr_debug("CS= %u\n",
  14050. + readl(c->chan_base + BCM2835_DMA_CS));
  14051. + pr_debug("ADDR= %u\n",
  14052. + readl(c->chan_base + BCM2835_DMA_ADDR));
  14053. + pr_debug("SOURCE_ADDR= %u\n",
  14054. + readl(c->chan_base + BCM2835_DMA_SOURCE_AD));
  14055. + pr_debug("DEST_AD= %u\n",
  14056. + readl(c->chan_base + BCM2835_DMA_DEST_AD));
  14057. + pr_debug("NEXTCB= %u\n",
  14058. + readl(c->chan_base + BCM2835_DMA_NEXTCB));
  14059. + pr_debug("--------------------------------------\n");
  14060. +}
  14061. +
  14062. +static void bcm2835_dma_desc_free(struct virt_dma_desc *vd)
  14063. +{
  14064. + struct bcm2835_desc *desc = container_of(vd, struct bcm2835_desc, vd);
  14065. + dma_free_coherent(desc->vd.tx.chan->device->dev,
  14066. + desc->control_block_size,
  14067. + desc->control_block_base,
  14068. + desc->control_block_base_phys);
  14069. + kfree(desc);
  14070. +}
  14071. +
  14072. +static int bcm2835_dma_abort(void __iomem *chan_base)
  14073. +{
  14074. + unsigned long cs;
  14075. + long int timeout = 10000;
  14076. +
  14077. + cs = readl(chan_base + BCM2835_DMA_CS);
  14078. + if (!(cs & BCM2835_DMA_ACTIVE))
  14079. + return 0;
  14080. +
  14081. + /* Write 0 to the active bit - Pause the DMA */
  14082. + writel(0, chan_base + BCM2835_DMA_CS);
  14083. +
  14084. + /* Wait for any current AXI transfer to complete */
  14085. + while ((cs & BCM2835_DMA_ISPAUSED) && --timeout) {
  14086. + cpu_relax();
  14087. + cs = readl(chan_base + BCM2835_DMA_CS);
  14088. + }
  14089. +
  14090. + /* We'll un-pause when we set of our next DMA */
  14091. + if (!timeout)
  14092. + return -ETIMEDOUT;
  14093. +
  14094. + if (!(cs & BCM2835_DMA_ACTIVE))
  14095. + return 0;
  14096. +
  14097. + /* Terminate the control block chain */
  14098. + writel(0, chan_base + BCM2835_DMA_NEXTCB);
  14099. +
  14100. + /* Abort the whole DMA */
  14101. + writel(BCM2835_DMA_ABORT | BCM2835_DMA_ACTIVE,
  14102. + chan_base + BCM2835_DMA_CS);
  14103. +
  14104. + return 0;
  14105. +}
  14106. +
  14107. +
  14108. +static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
  14109. +{
  14110. + struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
  14111. + struct bcm2835_desc *d;
  14112. +
  14113. + if (!vd) {
  14114. + c->desc = NULL;
  14115. + return;
  14116. + }
  14117. +
  14118. + list_del(&vd->node);
  14119. +
  14120. + c->desc = d = to_bcm2835_dma_desc(&vd->tx);
  14121. +
  14122. + writel(d->control_block_base_phys, c->chan_base + BCM2835_DMA_ADDR);
  14123. + writel(BCM2835_DMA_ACTIVE, c->chan_base + BCM2835_DMA_CS);
  14124. +
  14125. +}
  14126. +
  14127. +static irqreturn_t bcm2835_dma_callback(int irq, void *data)
  14128. +{
  14129. + struct bcm2835_chan *c = data;
  14130. + struct bcm2835_desc *d;
  14131. + unsigned long flags;
  14132. +
  14133. + spin_lock_irqsave(&c->vc.lock, flags);
  14134. +
  14135. + /* Acknowledge interrupt */
  14136. + writel(BCM2835_DMA_INT, c->chan_base + BCM2835_DMA_CS);
  14137. +
  14138. + d = c->desc;
  14139. +
  14140. + if (d) {
  14141. + if (c->cyclic) {
  14142. + vchan_cyclic_callback(&d->vd);
  14143. +
  14144. + /* Keep the DMA engine running */
  14145. + writel(BCM2835_DMA_ACTIVE,
  14146. + c->chan_base + BCM2835_DMA_CS);
  14147. +
  14148. + } else {
  14149. + vchan_cookie_complete(&c->desc->vd);
  14150. + bcm2835_dma_start_desc(c);
  14151. + }
  14152. + }
  14153. +
  14154. + spin_unlock_irqrestore(&c->vc.lock, flags);
  14155. +
  14156. + return IRQ_HANDLED;
  14157. +}
  14158. +
  14159. +static int bcm2835_dma_alloc_chan_resources(struct dma_chan *chan)
  14160. +{
  14161. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14162. + int ret;
  14163. +
  14164. + dev_dbg(c->vc.chan.device->dev,
  14165. + "Allocating DMA channel %d\n", c->ch);
  14166. +
  14167. + ret = request_irq(c->irq_number,
  14168. + bcm2835_dma_callback, 0, "DMA IRQ", c);
  14169. +
  14170. + return ret;
  14171. +}
  14172. +
  14173. +static void bcm2835_dma_free_chan_resources(struct dma_chan *chan)
  14174. +{
  14175. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14176. +
  14177. + vchan_free_chan_resources(&c->vc);
  14178. + free_irq(c->irq_number, c);
  14179. +
  14180. + dev_dbg(c->vc.chan.device->dev, "Freeing DMA channel %u\n", c->ch);
  14181. +}
  14182. +
  14183. +static size_t bcm2835_dma_desc_size(struct bcm2835_desc *d)
  14184. +{
  14185. + return d->size;
  14186. +}
  14187. +
  14188. +static size_t bcm2835_dma_desc_size_pos(struct bcm2835_desc *d, dma_addr_t addr)
  14189. +{
  14190. + unsigned int i;
  14191. + size_t size;
  14192. +
  14193. + for (size = i = 0; i < d->frames; i++) {
  14194. + struct bcm2835_dma_cb *control_block =
  14195. + &d->control_block_base[i];
  14196. + size_t this_size = control_block->length;
  14197. + dma_addr_t dma;
  14198. +
  14199. + if (d->dir == DMA_DEV_TO_MEM)
  14200. + dma = control_block->dst;
  14201. + else
  14202. + dma = control_block->src;
  14203. +
  14204. + if (size)
  14205. + size += this_size;
  14206. + else if (addr >= dma && addr < dma + this_size)
  14207. + size += dma + this_size - addr;
  14208. + }
  14209. +
  14210. + return size;
  14211. +}
  14212. +
  14213. +static enum dma_status bcm2835_dma_tx_status(struct dma_chan *chan,
  14214. + dma_cookie_t cookie, struct dma_tx_state *txstate)
  14215. +{
  14216. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14217. + struct bcm2835_desc *d;
  14218. + struct virt_dma_desc *vd;
  14219. + enum dma_status ret;
  14220. + unsigned long flags;
  14221. + dma_addr_t pos;
  14222. +
  14223. + ret = dma_cookie_status(chan, cookie, txstate);
  14224. + if (ret == DMA_COMPLETE || !txstate)
  14225. + return ret;
  14226. +
  14227. + spin_lock_irqsave(&c->vc.lock, flags);
  14228. + vd = vchan_find_desc(&c->vc, cookie);
  14229. + if (vd) {
  14230. + txstate->residue =
  14231. + bcm2835_dma_desc_size(to_bcm2835_dma_desc(&vd->tx));
  14232. + } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
  14233. + d = c->desc;
  14234. +
  14235. + if (d->dir == DMA_MEM_TO_DEV)
  14236. + pos = readl(c->chan_base + BCM2835_DMA_SOURCE_AD);
  14237. + else if (d->dir == DMA_DEV_TO_MEM)
  14238. + pos = readl(c->chan_base + BCM2835_DMA_DEST_AD);
  14239. + else
  14240. + pos = 0;
  14241. +
  14242. + txstate->residue = bcm2835_dma_desc_size_pos(d, pos);
  14243. + } else {
  14244. + txstate->residue = 0;
  14245. + }
  14246. +
  14247. + spin_unlock_irqrestore(&c->vc.lock, flags);
  14248. +
  14249. + return ret;
  14250. +}
  14251. +
  14252. +static void bcm2835_dma_issue_pending(struct dma_chan *chan)
  14253. +{
  14254. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14255. + unsigned long flags;
  14256. +
  14257. + spin_lock_irqsave(&c->vc.lock, flags);
  14258. + if (vchan_issue_pending(&c->vc) && !c->desc)
  14259. + bcm2835_dma_start_desc(c);
  14260. +
  14261. + spin_unlock_irqrestore(&c->vc.lock, flags);
  14262. +}
  14263. +
  14264. +static struct dma_async_tx_descriptor *bcm2835_dma_prep_dma_cyclic(
  14265. + struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  14266. + size_t period_len, enum dma_transfer_direction direction,
  14267. + unsigned long flags, void *context)
  14268. +{
  14269. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14270. + enum dma_slave_buswidth dev_width;
  14271. + struct bcm2835_desc *d;
  14272. + dma_addr_t dev_addr;
  14273. + unsigned int es, sync_type;
  14274. + unsigned int frame;
  14275. +
  14276. + /* Grab configuration */
  14277. + if (!is_slave_direction(direction)) {
  14278. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  14279. + return NULL;
  14280. + }
  14281. +
  14282. + if (direction == DMA_DEV_TO_MEM) {
  14283. + dev_addr = c->cfg.src_addr;
  14284. + dev_width = c->cfg.src_addr_width;
  14285. + sync_type = BCM2835_DMA_S_DREQ;
  14286. + } else {
  14287. + dev_addr = c->cfg.dst_addr;
  14288. + dev_width = c->cfg.dst_addr_width;
  14289. + sync_type = BCM2835_DMA_D_DREQ;
  14290. + }
  14291. +
  14292. + /* Bus width translates to the element size (ES) */
  14293. + switch (dev_width) {
  14294. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  14295. + es = BCM2835_DMA_DATA_TYPE_S32;
  14296. + break;
  14297. + default:
  14298. + return NULL;
  14299. + }
  14300. +
  14301. + /* Now allocate and setup the descriptor. */
  14302. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  14303. + if (!d)
  14304. + return NULL;
  14305. +
  14306. + d->dir = direction;
  14307. + d->frames = buf_len / period_len;
  14308. +
  14309. + /* Allocate memory for control blocks */
  14310. + d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
  14311. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  14312. + d->control_block_size, &d->control_block_base_phys,
  14313. + GFP_NOWAIT);
  14314. +
  14315. + if (!d->control_block_base) {
  14316. + kfree(d);
  14317. + return NULL;
  14318. + }
  14319. +
  14320. + /*
  14321. + * Iterate over all frames, create a control block
  14322. + * for each frame and link them together.
  14323. + */
  14324. + for (frame = 0; frame < d->frames; frame++) {
  14325. + struct bcm2835_dma_cb *control_block =
  14326. + &d->control_block_base[frame];
  14327. +
  14328. + /* Setup adresses */
  14329. + if (d->dir == DMA_DEV_TO_MEM) {
  14330. + control_block->info = BCM2835_DMA_D_INC;
  14331. + control_block->src = dev_addr;
  14332. + control_block->dst = buf_addr + frame * period_len;
  14333. + } else {
  14334. + control_block->info = BCM2835_DMA_S_INC;
  14335. + control_block->src = buf_addr + frame * period_len;
  14336. + control_block->dst = dev_addr;
  14337. + }
  14338. +
  14339. + /* Enable interrupt */
  14340. + control_block->info |= BCM2835_DMA_INT_EN;
  14341. +
  14342. + /* Setup synchronization */
  14343. + if (sync_type != 0)
  14344. + control_block->info |= sync_type;
  14345. +
  14346. + /* Setup DREQ channel */
  14347. + if (c->cfg.slave_id != 0)
  14348. + control_block->info |=
  14349. + BCM2835_DMA_PER_MAP(c->cfg.slave_id);
  14350. +
  14351. + /* Length of a frame */
  14352. + control_block->length = period_len;
  14353. + d->size += control_block->length;
  14354. +
  14355. + /*
  14356. + * Next block is the next frame.
  14357. + * This function is called on cyclic DMA transfers.
  14358. + * Therefore, wrap around at number of frames.
  14359. + */
  14360. + control_block->next = d->control_block_base_phys +
  14361. + sizeof(struct bcm2835_dma_cb)
  14362. + * ((frame + 1) % d->frames);
  14363. + }
  14364. +
  14365. + c->cyclic = true;
  14366. +
  14367. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  14368. +}
  14369. +
  14370. +
  14371. +static struct dma_async_tx_descriptor *bcm2835_dma_prep_slave_sg(
  14372. + struct dma_chan *chan, struct scatterlist *sgl,
  14373. + unsigned int sg_len, enum dma_transfer_direction direction,
  14374. + unsigned long flags, void *context)
  14375. +{
  14376. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14377. + enum dma_slave_buswidth dev_width;
  14378. + struct bcm2835_desc *d;
  14379. + dma_addr_t dev_addr;
  14380. + struct scatterlist *sgent;
  14381. + unsigned int es, sync_type;
  14382. + unsigned int i, j, splitct, max_size;
  14383. +
  14384. + if (!is_slave_direction(direction)) {
  14385. + dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
  14386. + return NULL;
  14387. + }
  14388. +
  14389. + if (direction == DMA_DEV_TO_MEM) {
  14390. + dev_addr = c->cfg.src_addr;
  14391. + dev_width = c->cfg.src_addr_width;
  14392. + sync_type = BCM2835_DMA_S_DREQ;
  14393. + } else {
  14394. + dev_addr = c->cfg.dst_addr;
  14395. + dev_width = c->cfg.dst_addr_width;
  14396. + sync_type = BCM2835_DMA_D_DREQ;
  14397. + }
  14398. +
  14399. + /* Bus width translates to the element size (ES) */
  14400. + switch (dev_width) {
  14401. + case DMA_SLAVE_BUSWIDTH_4_BYTES:
  14402. + es = BCM2835_DMA_DATA_TYPE_S32;
  14403. + break;
  14404. + default:
  14405. + return NULL;
  14406. + }
  14407. +
  14408. + /* Now allocate and setup the descriptor. */
  14409. + d = kzalloc(sizeof(*d), GFP_NOWAIT);
  14410. + if (!d)
  14411. + return NULL;
  14412. +
  14413. + d->dir = direction;
  14414. +
  14415. + if (c->ch >= 8) /* we have a LITE channel */
  14416. + max_size = MAX_LITE_TRANSFER;
  14417. + else
  14418. + max_size = MAX_NORMAL_TRANSFER;
  14419. +
  14420. + /* We store the length of the SG list in d->frames
  14421. + taking care to account for splitting up transfers
  14422. + too large for a LITE channel */
  14423. +
  14424. + d->frames = 0;
  14425. + for_each_sg(sgl, sgent, sg_len, i) {
  14426. + uint32_t len = sg_dma_len(sgent);
  14427. + d->frames += 1 + len / max_size;
  14428. + }
  14429. +
  14430. + /* Allocate memory for control blocks */
  14431. + d->control_block_size = d->frames * sizeof(struct bcm2835_dma_cb);
  14432. + d->control_block_base = dma_zalloc_coherent(chan->device->dev,
  14433. + d->control_block_size, &d->control_block_base_phys,
  14434. + GFP_NOWAIT);
  14435. +
  14436. + if (!d->control_block_base) {
  14437. + kfree(d);
  14438. + return NULL;
  14439. + }
  14440. +
  14441. + /*
  14442. + * Iterate over all SG entries, create a control block
  14443. + * for each frame and link them together.
  14444. + */
  14445. +
  14446. + /* we count the number of times an SG entry had to be splitct
  14447. + as a result of using a LITE channel */
  14448. + splitct = 0;
  14449. +
  14450. + for_each_sg(sgl, sgent, sg_len, i) {
  14451. + dma_addr_t addr = sg_dma_address(sgent);
  14452. + uint32_t len = sg_dma_len(sgent);
  14453. +
  14454. + for (j = 0; j < len; j += max_size) {
  14455. + struct bcm2835_dma_cb *control_block =
  14456. + &d->control_block_base[i+splitct];
  14457. +
  14458. + /* Setup adresses */
  14459. + if (d->dir == DMA_DEV_TO_MEM) {
  14460. + control_block->info = BCM2835_DMA_D_INC |
  14461. + BCM2835_DMA_D_WIDTH | BCM2835_DMA_S_DREQ;
  14462. + control_block->src = dev_addr;
  14463. + control_block->dst = addr + (dma_addr_t)j;
  14464. + } else {
  14465. + control_block->info = BCM2835_DMA_S_INC |
  14466. + BCM2835_DMA_S_WIDTH | BCM2835_DMA_D_DREQ;
  14467. + control_block->src = addr + (dma_addr_t)j;
  14468. + control_block->dst = dev_addr;
  14469. + }
  14470. +
  14471. + /* Common part */
  14472. + control_block->info |= BCM2835_DMA_WAITS(SDHCI_BCM_DMA_WAITS);
  14473. + control_block->info |= BCM2835_DMA_WAIT_RESP;
  14474. +
  14475. + /* Enable */
  14476. + if (i == sg_len-1 && len-j <= max_size)
  14477. + control_block->info |= BCM2835_DMA_INT_EN;
  14478. +
  14479. + /* Setup synchronization */
  14480. + if (sync_type != 0)
  14481. + control_block->info |= sync_type;
  14482. +
  14483. + /* Setup DREQ channel */
  14484. + c->dreq = c->cfg.slave_id; /* DREQ loaded from config */
  14485. +
  14486. + if (c->dreq != 0)
  14487. + control_block->info |=
  14488. + BCM2835_DMA_PER_MAP(c->dreq);
  14489. +
  14490. + /* Length of a frame */
  14491. + control_block->length = min(len-j, max_size);
  14492. + d->size += control_block->length;
  14493. +
  14494. + /*
  14495. + * Next block is the next frame.
  14496. + */
  14497. + if (i < sg_len-1 || len-j > max_size) {
  14498. + /* next block is the next frame. */
  14499. + control_block->next = d->control_block_base_phys +
  14500. + sizeof(struct bcm2835_dma_cb) * (i + splitct + 1);
  14501. + } else {
  14502. + /* next block is empty. */
  14503. + control_block->next = 0;
  14504. + }
  14505. +
  14506. + if (len-j > max_size)
  14507. + splitct++;
  14508. + }
  14509. + }
  14510. +
  14511. + c->cyclic = false;
  14512. +
  14513. + return vchan_tx_prep(&c->vc, &d->vd, flags);
  14514. +}
  14515. +
  14516. +static int bcm2835_dma_slave_config(struct bcm2835_chan *c,
  14517. + struct dma_slave_config *cfg)
  14518. +{
  14519. + if ((cfg->direction == DMA_DEV_TO_MEM &&
  14520. + cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  14521. + (cfg->direction == DMA_MEM_TO_DEV &&
  14522. + cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
  14523. + !is_slave_direction(cfg->direction)) {
  14524. + return -EINVAL;
  14525. + }
  14526. +
  14527. + c->cfg = *cfg;
  14528. +
  14529. + return 0;
  14530. +}
  14531. +
  14532. +static int bcm2835_dma_terminate_all(struct bcm2835_chan *c)
  14533. +{
  14534. + struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
  14535. + unsigned long flags;
  14536. + int timeout = 10000;
  14537. + LIST_HEAD(head);
  14538. +
  14539. + spin_lock_irqsave(&c->vc.lock, flags);
  14540. +
  14541. + /* Prevent this channel being scheduled */
  14542. + spin_lock(&d->lock);
  14543. + list_del_init(&c->node);
  14544. + spin_unlock(&d->lock);
  14545. +
  14546. + /*
  14547. + * Stop DMA activity: we assume the callback will not be called
  14548. + * after bcm_dma_abort() returns (even if it does, it will see
  14549. + * c->desc is NULL and exit.)
  14550. + */
  14551. + if (c->desc) {
  14552. + c->desc = NULL;
  14553. + bcm2835_dma_abort(c->chan_base);
  14554. +
  14555. + /* Wait for stopping */
  14556. + while (--timeout) {
  14557. + if (!(readl(c->chan_base + BCM2835_DMA_CS) &
  14558. + BCM2835_DMA_ACTIVE))
  14559. + break;
  14560. +
  14561. + cpu_relax();
  14562. + }
  14563. +
  14564. + if (!timeout)
  14565. + dev_err(d->ddev.dev, "DMA transfer could not be terminated\n");
  14566. + }
  14567. +
  14568. + vchan_get_all_descriptors(&c->vc, &head);
  14569. + spin_unlock_irqrestore(&c->vc.lock, flags);
  14570. + vchan_dma_desc_free_list(&c->vc, &head);
  14571. +
  14572. + return 0;
  14573. +}
  14574. +
  14575. +static int bcm2835_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  14576. + unsigned long arg)
  14577. +{
  14578. + struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
  14579. +
  14580. + switch (cmd) {
  14581. + case DMA_SLAVE_CONFIG:
  14582. + return bcm2835_dma_slave_config(c,
  14583. + (struct dma_slave_config *)arg);
  14584. +
  14585. + case DMA_TERMINATE_ALL:
  14586. + return bcm2835_dma_terminate_all(c);
  14587. +
  14588. + default:
  14589. + return -ENXIO;
  14590. + }
  14591. +}
  14592. +
  14593. +#ifdef CONFIG_OF
  14594. +static int bcm2835_dma_chan_init(struct bcm2835_dmadev *d, int chan_id, int irq)
  14595. +{
  14596. + struct bcm2835_chan *c;
  14597. +
  14598. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  14599. + if (!c)
  14600. + return -ENOMEM;
  14601. +
  14602. + c->vc.desc_free = bcm2835_dma_desc_free;
  14603. + vchan_init(&c->vc, &d->ddev);
  14604. + INIT_LIST_HEAD(&c->node);
  14605. +
  14606. + d->ddev.chancnt++;
  14607. +
  14608. + c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
  14609. + c->ch = chan_id;
  14610. + c->irq_number = irq;
  14611. +
  14612. + return 0;
  14613. +}
  14614. +#endif
  14615. +
  14616. +static int bcm2708_dma_chan_init(struct bcm2835_dmadev *d,
  14617. + void __iomem *chan_base, int chan_id, int irq)
  14618. +{
  14619. + struct bcm2835_chan *c;
  14620. +
  14621. + c = devm_kzalloc(d->ddev.dev, sizeof(*c), GFP_KERNEL);
  14622. + if (!c)
  14623. + return -ENOMEM;
  14624. +
  14625. + c->vc.desc_free = bcm2835_dma_desc_free;
  14626. + vchan_init(&c->vc, &d->ddev);
  14627. + INIT_LIST_HEAD(&c->node);
  14628. +
  14629. + d->ddev.chancnt++;
  14630. +
  14631. + c->chan_base = chan_base;
  14632. + c->ch = chan_id;
  14633. + c->irq_number = irq;
  14634. +
  14635. + return 0;
  14636. +}
  14637. +
  14638. +
  14639. +static void bcm2835_dma_free(struct bcm2835_dmadev *od)
  14640. +{
  14641. + struct bcm2835_chan *c, *next;
  14642. +
  14643. + list_for_each_entry_safe(c, next, &od->ddev.channels,
  14644. + vc.chan.device_node) {
  14645. + list_del(&c->vc.chan.device_node);
  14646. + tasklet_kill(&c->vc.task);
  14647. + }
  14648. +}
  14649. +
  14650. +static const struct of_device_id bcm2835_dma_of_match[] = {
  14651. + { .compatible = "brcm,bcm2835-dma", },
  14652. + {},
  14653. +};
  14654. +MODULE_DEVICE_TABLE(of, bcm2835_dma_of_match);
  14655. +
  14656. +#ifdef CONFIG_OF
  14657. +static struct dma_chan *bcm2835_dma_xlate(struct of_phandle_args *spec,
  14658. + struct of_dma *ofdma)
  14659. +{
  14660. + struct bcm2835_dmadev *d = ofdma->of_dma_data;
  14661. + struct dma_chan *chan;
  14662. +
  14663. + chan = dma_get_any_slave_channel(&d->ddev);
  14664. + if (!chan)
  14665. + return NULL;
  14666. +
  14667. + /* Set DREQ from param */
  14668. + to_bcm2835_dma_chan(chan)->dreq = spec->args[0];
  14669. +
  14670. + return chan;
  14671. +}
  14672. +#endif
  14673. +
  14674. +static int bcm2835_dma_device_slave_caps(struct dma_chan *dchan,
  14675. + struct dma_slave_caps *caps)
  14676. +{
  14677. + caps->src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  14678. + caps->dstn_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_4_BYTES);
  14679. + caps->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  14680. + caps->cmd_pause = false;
  14681. + caps->cmd_terminate = true;
  14682. +
  14683. + return 0;
  14684. +}
  14685. +
  14686. +static int bcm2835_dma_probe(struct platform_device *pdev)
  14687. +{
  14688. + struct bcm2835_dmadev *od;
  14689. +#ifdef CONFIG_OF
  14690. + struct resource *res;
  14691. + void __iomem *base;
  14692. + uint32_t chans_available;
  14693. +#endif
  14694. + int rc;
  14695. + int i;
  14696. + int irq;
  14697. +
  14698. +
  14699. + if (!pdev->dev.dma_mask)
  14700. + pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  14701. +
  14702. + /* If CONFIG_OF is selected, device tree is used */
  14703. + /* hence the difference between probing */
  14704. +
  14705. +#ifndef CONFIG_OF
  14706. +
  14707. + rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  14708. + if (rc)
  14709. + return rc;
  14710. + dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  14711. +
  14712. +
  14713. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  14714. + if (!od)
  14715. + return -ENOMEM;
  14716. +
  14717. + pdev->dev.dma_parms = &od->dma_parms;
  14718. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  14719. +
  14720. +
  14721. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  14722. + dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  14723. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  14724. + od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  14725. + od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  14726. + od->ddev.device_tx_status = bcm2835_dma_tx_status;
  14727. + od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  14728. + od->ddev.device_slave_caps = bcm2835_dma_device_slave_caps;
  14729. + od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  14730. + od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  14731. + od->ddev.device_control = bcm2835_dma_control;
  14732. + od->ddev.dev = &pdev->dev;
  14733. + INIT_LIST_HEAD(&od->ddev.channels);
  14734. + spin_lock_init(&od->lock);
  14735. +
  14736. + platform_set_drvdata(pdev, od);
  14737. +
  14738. + for (i = 0; i < 5; i++) {
  14739. + void __iomem *chan_base;
  14740. + int chan_id;
  14741. +
  14742. + chan_id = bcm_dma_chan_alloc(BCM_DMA_FEATURE_LITE,
  14743. + &chan_base,
  14744. + &irq);
  14745. +
  14746. + if (chan_id < 0)
  14747. + break;
  14748. +
  14749. + rc = bcm2708_dma_chan_init(od, chan_base, chan_id, irq);
  14750. + if (rc)
  14751. + goto err_no_dma;
  14752. + }
  14753. +#else
  14754. + rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  14755. + if (rc)
  14756. + return rc;
  14757. +
  14758. +
  14759. + od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
  14760. + if (!od)
  14761. + return -ENOMEM;
  14762. +
  14763. + pdev->dev.dma_parms = &od->dma_parms;
  14764. + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
  14765. +
  14766. +
  14767. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  14768. + base = devm_ioremap_resource(&pdev->dev, res);
  14769. + if (IS_ERR(base))
  14770. + return PTR_ERR(base);
  14771. +
  14772. + od->base = base;
  14773. +
  14774. +
  14775. + dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
  14776. + dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
  14777. + dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
  14778. + od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
  14779. + od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
  14780. + od->ddev.device_tx_status = bcm2835_dma_tx_status;
  14781. + od->ddev.device_issue_pending = bcm2835_dma_issue_pending;
  14782. + od->ddev.device_slave_caps = bcm2835_dma_device_slave_caps;
  14783. + od->ddev.device_prep_dma_cyclic = bcm2835_dma_prep_dma_cyclic;
  14784. + od->ddev.device_prep_slave_sg = bcm2835_dma_prep_slave_sg;
  14785. + od->ddev.device_control = bcm2835_dma_control;
  14786. + od->ddev.dev = &pdev->dev;
  14787. + INIT_LIST_HEAD(&od->ddev.channels);
  14788. + spin_lock_init(&od->lock);
  14789. +
  14790. + platform_set_drvdata(pdev, od);
  14791. +
  14792. +
  14793. + /* Request DMA channel mask from device tree */
  14794. + if (of_property_read_u32(pdev->dev.of_node,
  14795. + "brcm,dma-channel-mask",
  14796. + &chans_available)) {
  14797. + dev_err(&pdev->dev, "Failed to get channel mask\n");
  14798. + rc = -EINVAL;
  14799. + goto err_no_dma;
  14800. + }
  14801. +
  14802. +
  14803. + /*
  14804. + * Do not use the FIQ and BULK channels,
  14805. + * because they are used by the GPU.
  14806. + */
  14807. + chans_available &= ~(BCM2835_DMA_FIQ_MASK | BCM2835_DMA_BULK_MASK);
  14808. +
  14809. +
  14810. + for (i = 0; i < pdev->num_resources; i++) {
  14811. + irq = platform_get_irq(pdev, i);
  14812. + if (irq < 0)
  14813. + break;
  14814. +
  14815. + if (chans_available & (1 << i)) {
  14816. + rc = bcm2835_dma_chan_init(od, i, irq);
  14817. + if (rc)
  14818. + goto err_no_dma;
  14819. + }
  14820. + }
  14821. +
  14822. + dev_dbg(&pdev->dev, "Initialized %i DMA channels\n", i);
  14823. +
  14824. + /* Device-tree DMA controller registration */
  14825. + rc = of_dma_controller_register(pdev->dev.of_node,
  14826. + bcm2835_dma_xlate, od);
  14827. + if (rc) {
  14828. + dev_err(&pdev->dev, "Failed to register DMA controller\n");
  14829. + goto err_no_dma;
  14830. + }
  14831. +#endif
  14832. +
  14833. + rc = dma_async_device_register(&od->ddev);
  14834. + if (rc) {
  14835. + dev_err(&pdev->dev,
  14836. + "Failed to register slave DMA engine device: %d\n", rc);
  14837. + goto err_no_dma;
  14838. + }
  14839. +
  14840. + dev_info(&pdev->dev, "Load BCM2835 DMA engine driver\n");
  14841. +
  14842. + return 0;
  14843. +
  14844. +err_no_dma:
  14845. + bcm2835_dma_free(od);
  14846. + return rc;
  14847. +}
  14848. +
  14849. +static int bcm2835_dma_remove(struct platform_device *pdev)
  14850. +{
  14851. + struct bcm2835_dmadev *od = platform_get_drvdata(pdev);
  14852. +
  14853. + dma_async_device_unregister(&od->ddev);
  14854. + bcm2835_dma_free(od);
  14855. +
  14856. + return 0;
  14857. +}
  14858. +
  14859. +#ifndef CONFIG_OF
  14860. +
  14861. +
  14862. +static struct platform_driver bcm2835_dma_driver = {
  14863. + .probe = bcm2835_dma_probe,
  14864. + .remove = bcm2835_dma_remove,
  14865. + .driver = {
  14866. + .name = "bcm2708-dmaengine",
  14867. + .owner = THIS_MODULE,
  14868. + },
  14869. +};
  14870. +
  14871. +static struct platform_device *pdev;
  14872. +
  14873. +static const struct platform_device_info bcm2835_dma_dev_info = {
  14874. + .name = "bcm2708-dmaengine",
  14875. + .id = -1,
  14876. +};
  14877. +
  14878. +static int bcm2835_dma_init(void)
  14879. +{
  14880. + int rc = platform_driver_register(&bcm2835_dma_driver);
  14881. +
  14882. + if (rc == 0) {
  14883. + pdev = platform_device_register_full(&bcm2835_dma_dev_info);
  14884. + if (IS_ERR(pdev)) {
  14885. + platform_driver_unregister(&bcm2835_dma_driver);
  14886. + rc = PTR_ERR(pdev);
  14887. + }
  14888. + }
  14889. +
  14890. + return rc;
  14891. +}
  14892. +module_init(bcm2835_dma_init); /* preferable to subsys_initcall */
  14893. +
  14894. +static void __exit bcm2835_dma_exit(void)
  14895. +{
  14896. + platform_device_unregister(pdev);
  14897. + platform_driver_unregister(&bcm2835_dma_driver);
  14898. +}
  14899. +module_exit(bcm2835_dma_exit);
  14900. +
  14901. +#else
  14902. +
  14903. +static struct platform_driver bcm2835_dma_driver = {
  14904. + .probe = bcm2835_dma_probe,
  14905. + .remove = bcm2835_dma_remove,
  14906. + .driver = {
  14907. + .name = "bcm2835-dma",
  14908. + .owner = THIS_MODULE,
  14909. + .of_match_table = of_match_ptr(bcm2835_dma_of_match),
  14910. + },
  14911. +};
  14912. +
  14913. +module_platform_driver(bcm2835_dma_driver);
  14914. +
  14915. +#endif
  14916. +
  14917. +MODULE_ALIAS("platform:bcm2835-dma");
  14918. +MODULE_DESCRIPTION("BCM2835 DMA engine driver");
  14919. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  14920. +MODULE_AUTHOR("Gellert Weisz <gellert@raspberrypi.org>");
  14921. +MODULE_LICENSE("GPL v2");
  14922. diff -Nur linux-3.16.2/drivers/dma/Kconfig linux-3.16-rpi/drivers/dma/Kconfig
  14923. --- linux-3.16.2/drivers/dma/Kconfig 2014-09-06 01:37:11.000000000 +0200
  14924. +++ linux-3.16-rpi/drivers/dma/Kconfig 2014-09-14 19:03:15.000000000 +0200
  14925. @@ -312,6 +312,12 @@
  14926. select DMA_ENGINE
  14927. select DMA_VIRTUAL_CHANNELS
  14928. +config DMA_BCM2708
  14929. + tristate "BCM2708 DMA engine support"
  14930. + depends on MACH_BCM2708
  14931. + select DMA_ENGINE
  14932. + select DMA_VIRTUAL_CHANNELS
  14933. +
  14934. config TI_CPPI41
  14935. tristate "AM33xx CPPI41 DMA support"
  14936. depends on ARCH_OMAP
  14937. diff -Nur linux-3.16.2/drivers/dma/Makefile linux-3.16-rpi/drivers/dma/Makefile
  14938. --- linux-3.16.2/drivers/dma/Makefile 2014-09-06 01:37:11.000000000 +0200
  14939. +++ linux-3.16-rpi/drivers/dma/Makefile 2014-09-14 19:03:15.000000000 +0200
  14940. @@ -39,6 +39,7 @@
  14941. obj-$(CONFIG_MMP_TDMA) += mmp_tdma.o
  14942. obj-$(CONFIG_DMA_OMAP) += omap-dma.o
  14943. obj-$(CONFIG_DMA_BCM2835) += bcm2835-dma.o
  14944. +obj-$(CONFIG_DMA_BCM2708) += bcm2708-dmaengine.o
  14945. obj-$(CONFIG_MMP_PDMA) += mmp_pdma.o
  14946. obj-$(CONFIG_DMA_JZ4740) += dma-jz4740.o
  14947. obj-$(CONFIG_TI_CPPI41) += cppi41.o
  14948. diff -Nur linux-3.16.2/drivers/hid/usbhid/hid-core.c linux-3.16-rpi/drivers/hid/usbhid/hid-core.c
  14949. --- linux-3.16.2/drivers/hid/usbhid/hid-core.c 2014-09-06 01:37:11.000000000 +0200
  14950. +++ linux-3.16-rpi/drivers/hid/usbhid/hid-core.c 2014-09-14 19:03:20.000000000 +0200
  14951. @@ -49,7 +49,7 @@
  14952. * Module parameters.
  14953. */
  14954. -static unsigned int hid_mousepoll_interval;
  14955. +static unsigned int hid_mousepoll_interval = ~0;
  14956. module_param_named(mousepoll, hid_mousepoll_interval, uint, 0644);
  14957. MODULE_PARM_DESC(mousepoll, "Polling interval of mice");
  14958. @@ -1090,8 +1090,12 @@
  14959. }
  14960. /* Change the polling interval of mice. */
  14961. - if (hid->collection->usage == HID_GD_MOUSE && hid_mousepoll_interval > 0)
  14962. - interval = hid_mousepoll_interval;
  14963. + if (hid->collection->usage == HID_GD_MOUSE) {
  14964. + if (hid_mousepoll_interval == ~0 && interval < 16)
  14965. + interval = 16;
  14966. + else if (hid_mousepoll_interval != ~0 && hid_mousepoll_interval != 0)
  14967. + interval = hid_mousepoll_interval;
  14968. + }
  14969. ret = -ENOMEM;
  14970. if (usb_endpoint_dir_in(endpoint)) {
  14971. diff -Nur linux-3.16.2/drivers/hwmon/bcm2835-hwmon.c linux-3.16-rpi/drivers/hwmon/bcm2835-hwmon.c
  14972. --- linux-3.16.2/drivers/hwmon/bcm2835-hwmon.c 1970-01-01 01:00:00.000000000 +0100
  14973. +++ linux-3.16-rpi/drivers/hwmon/bcm2835-hwmon.c 2014-04-13 17:32:56.000000000 +0200
  14974. @@ -0,0 +1,219 @@
  14975. +/*****************************************************************************
  14976. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  14977. +*
  14978. +* Unless you and Broadcom execute a separate written software license
  14979. +* agreement governing use of this software, this software is licensed to you
  14980. +* under the terms of the GNU General Public License version 2, available at
  14981. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  14982. +*
  14983. +* Notwithstanding the above, under no circumstances may you combine this
  14984. +* software in any way with any other Broadcom software provided under a
  14985. +* license other than the GPL, without Broadcom's express prior written
  14986. +* consent.
  14987. +*****************************************************************************/
  14988. +
  14989. +#include <linux/kernel.h>
  14990. +#include <linux/module.h>
  14991. +#include <linux/init.h>
  14992. +#include <linux/hwmon.h>
  14993. +#include <linux/hwmon-sysfs.h>
  14994. +#include <linux/platform_device.h>
  14995. +#include <linux/sysfs.h>
  14996. +#include <mach/vcio.h>
  14997. +#include <linux/slab.h>
  14998. +#include <linux/err.h>
  14999. +
  15000. +#define MODULE_NAME "bcm2835_hwmon"
  15001. +
  15002. +/*#define HWMON_DEBUG_ENABLE*/
  15003. +
  15004. +#ifdef HWMON_DEBUG_ENABLE
  15005. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  15006. +#else
  15007. +#define print_debug(fmt,...)
  15008. +#endif
  15009. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  15010. +#define print_info(fmt,...) printk(KERN_INFO "%s: "fmt"\n", MODULE_NAME, ##__VA_ARGS__)
  15011. +
  15012. +#define VC_TAG_GET_TEMP 0x00030006
  15013. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  15014. +
  15015. +/* --- STRUCTS --- */
  15016. +struct bcm2835_hwmon_data {
  15017. + struct device *hwmon_dev;
  15018. +};
  15019. +
  15020. +/* tag part of the message */
  15021. +struct vc_msg_tag {
  15022. + uint32_t tag_id; /* the tag ID for the temperature */
  15023. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  15024. + uint32_t request_code; /* identifies message as a request (should be 0) */
  15025. + uint32_t id; /* extra ID field (should be 0) */
  15026. + uint32_t val; /* returned value of the temperature */
  15027. +};
  15028. +
  15029. +/* message structure to be sent to videocore */
  15030. +struct vc_msg {
  15031. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  15032. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  15033. + struct vc_msg_tag tag; /* the tag structure above to make */
  15034. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  15035. +};
  15036. +
  15037. +typedef enum {
  15038. + TEMP,
  15039. + MAX_TEMP,
  15040. +} temp_type;
  15041. +
  15042. +/* --- PROTOTYPES --- */
  15043. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf);
  15044. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf);
  15045. +
  15046. +/* --- GLOBALS --- */
  15047. +
  15048. +static struct bcm2835_hwmon_data *bcm2835_data;
  15049. +static struct platform_driver bcm2835_hwmon_driver;
  15050. +
  15051. +static SENSOR_DEVICE_ATTR(name, S_IRUGO,bcm2835_get_name,NULL,0);
  15052. +static SENSOR_DEVICE_ATTR(temp1_input,S_IRUGO,bcm2835_get_temp,NULL,TEMP);
  15053. +static SENSOR_DEVICE_ATTR(temp1_max,S_IRUGO,bcm2835_get_temp,NULL,MAX_TEMP);
  15054. +
  15055. +static struct attribute* bcm2835_attributes[] = {
  15056. + &sensor_dev_attr_name.dev_attr.attr,
  15057. + &sensor_dev_attr_temp1_input.dev_attr.attr,
  15058. + &sensor_dev_attr_temp1_max.dev_attr.attr,
  15059. + NULL,
  15060. +};
  15061. +
  15062. +static struct attribute_group bcm2835_attr_group = {
  15063. + .attrs = bcm2835_attributes,
  15064. +};
  15065. +
  15066. +/* --- FUNCTIONS --- */
  15067. +
  15068. +static ssize_t bcm2835_get_name(struct device *dev, struct device_attribute *attr, char *buf)
  15069. +{
  15070. + return sprintf(buf,"bcm2835_hwmon\n");
  15071. +}
  15072. +
  15073. +static ssize_t bcm2835_get_temp(struct device *dev, struct device_attribute *attr, char *buf)
  15074. +{
  15075. + struct vc_msg msg;
  15076. + int result;
  15077. + uint temp = 0;
  15078. + int index = ((struct sensor_device_attribute*)to_sensor_dev_attr(attr))->index;
  15079. +
  15080. + print_debug("IN");
  15081. +
  15082. + /* wipe all previous message data */
  15083. + memset(&msg, 0, sizeof msg);
  15084. +
  15085. + /* determine the message type */
  15086. + if(index == TEMP)
  15087. + msg.tag.tag_id = VC_TAG_GET_TEMP;
  15088. + else if (index == MAX_TEMP)
  15089. + msg.tag.tag_id = VC_TAG_GET_MAX_TEMP;
  15090. + else
  15091. + {
  15092. + print_debug("Unknown temperature message!");
  15093. + return -EINVAL;
  15094. + }
  15095. +
  15096. + msg.msg_size = sizeof msg;
  15097. + msg.tag.buffer_size = 8;
  15098. +
  15099. + /* send the message */
  15100. + result = bcm_mailbox_property(&msg, sizeof msg);
  15101. +
  15102. + /* check if it was all ok and return the rate in milli degrees C */
  15103. + if (result == 0 && (msg.request_code & 0x80000000))
  15104. + temp = (uint)msg.tag.val;
  15105. + #ifdef HWMON_DEBUG_ENABLE
  15106. + else
  15107. + print_debug("Failed to get temperature!");
  15108. + #endif
  15109. + print_debug("Got temperature as %u",temp);
  15110. + print_debug("OUT");
  15111. + return sprintf(buf, "%u\n", temp);
  15112. +}
  15113. +
  15114. +
  15115. +static int bcm2835_hwmon_probe(struct platform_device *pdev)
  15116. +{
  15117. + int err;
  15118. +
  15119. + print_debug("IN");
  15120. + print_debug("HWMON Driver has been probed!");
  15121. +
  15122. + /* check that the device isn't null!*/
  15123. + if(pdev == NULL)
  15124. + {
  15125. + print_debug("Platform device is empty!");
  15126. + return -ENODEV;
  15127. + }
  15128. +
  15129. + /* allocate memory for neccessary data */
  15130. + bcm2835_data = kzalloc(sizeof(struct bcm2835_hwmon_data),GFP_KERNEL);
  15131. + if(!bcm2835_data)
  15132. + {
  15133. + print_debug("Unable to allocate memory for hwmon data!");
  15134. + err = -ENOMEM;
  15135. + goto kzalloc_error;
  15136. + }
  15137. +
  15138. + /* create the sysfs files */
  15139. + if(sysfs_create_group(&pdev->dev.kobj, &bcm2835_attr_group))
  15140. + {
  15141. + print_debug("Unable to create sysfs files!");
  15142. + err = -EFAULT;
  15143. + goto sysfs_error;
  15144. + }
  15145. +
  15146. + /* register the hwmon device */
  15147. + bcm2835_data->hwmon_dev = hwmon_device_register(&pdev->dev);
  15148. + if (IS_ERR(bcm2835_data->hwmon_dev))
  15149. + {
  15150. + err = PTR_ERR(bcm2835_data->hwmon_dev);
  15151. + goto hwmon_error;
  15152. + }
  15153. + print_debug("OUT");
  15154. + return 0;
  15155. +
  15156. + /* error goto's */
  15157. + hwmon_error:
  15158. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  15159. +
  15160. + sysfs_error:
  15161. + kfree(bcm2835_data);
  15162. +
  15163. + kzalloc_error:
  15164. +
  15165. + return err;
  15166. +
  15167. +}
  15168. +
  15169. +static int bcm2835_hwmon_remove(struct platform_device *pdev)
  15170. +{
  15171. + print_debug("IN");
  15172. + hwmon_device_unregister(bcm2835_data->hwmon_dev);
  15173. +
  15174. + sysfs_remove_group(&pdev->dev.kobj, &bcm2835_attr_group);
  15175. + print_debug("OUT");
  15176. + return 0;
  15177. +}
  15178. +
  15179. +/* Hwmon Driver */
  15180. +static struct platform_driver bcm2835_hwmon_driver = {
  15181. + .probe = bcm2835_hwmon_probe,
  15182. + .remove = bcm2835_hwmon_remove,
  15183. + .driver = {
  15184. + .name = "bcm2835_hwmon",
  15185. + .owner = THIS_MODULE,
  15186. + },
  15187. +};
  15188. +
  15189. +MODULE_LICENSE("GPL");
  15190. +MODULE_AUTHOR("Dorian Peake");
  15191. +MODULE_DESCRIPTION("HW Monitor driver for bcm2835 chip");
  15192. +
  15193. +module_platform_driver(bcm2835_hwmon_driver);
  15194. diff -Nur linux-3.16.2/drivers/hwmon/Kconfig linux-3.16-rpi/drivers/hwmon/Kconfig
  15195. --- linux-3.16.2/drivers/hwmon/Kconfig 2014-09-06 01:37:11.000000000 +0200
  15196. +++ linux-3.16-rpi/drivers/hwmon/Kconfig 2014-09-14 19:03:20.000000000 +0200
  15197. @@ -1624,6 +1624,16 @@
  15198. This driver provides support for the Ultra45 workstation environmental
  15199. sensors.
  15200. +config SENSORS_BCM2835
  15201. + depends on THERMAL_BCM2835=n
  15202. + tristate "Broadcom BCM2835 HWMON Driver"
  15203. + help
  15204. + If you say yes here you get support for the hardware
  15205. + monitoring features of the BCM2835 Chip
  15206. +
  15207. + This driver can also be built as a module. If so, the module
  15208. + will be called bcm2835-hwmon.
  15209. +
  15210. if ACPI
  15211. comment "ACPI drivers"
  15212. diff -Nur linux-3.16.2/drivers/hwmon/Makefile linux-3.16-rpi/drivers/hwmon/Makefile
  15213. --- linux-3.16.2/drivers/hwmon/Makefile 2014-09-06 01:37:11.000000000 +0200
  15214. +++ linux-3.16-rpi/drivers/hwmon/Makefile 2014-09-14 19:03:20.000000000 +0200
  15215. @@ -148,6 +148,7 @@
  15216. obj-$(CONFIG_SENSORS_W83L786NG) += w83l786ng.o
  15217. obj-$(CONFIG_SENSORS_WM831X) += wm831x-hwmon.o
  15218. obj-$(CONFIG_SENSORS_WM8350) += wm8350-hwmon.o
  15219. +obj-$(CONFIG_SENSORS_BCM2835) += bcm2835-hwmon.o
  15220. obj-$(CONFIG_PMBUS) += pmbus/
  15221. diff -Nur linux-3.16.2/drivers/i2c/busses/i2c-bcm2708.c linux-3.16-rpi/drivers/i2c/busses/i2c-bcm2708.c
  15222. --- linux-3.16.2/drivers/i2c/busses/i2c-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  15223. +++ linux-3.16-rpi/drivers/i2c/busses/i2c-bcm2708.c 2014-09-14 19:03:20.000000000 +0200
  15224. @@ -0,0 +1,472 @@
  15225. +/*
  15226. + * Driver for Broadcom BCM2708 BSC Controllers
  15227. + *
  15228. + * Copyright (C) 2012 Chris Boot & Frank Buss
  15229. + *
  15230. + * This driver is inspired by:
  15231. + * i2c-ocores.c, by Peter Korsgaard <jacmet@sunsite.dk>
  15232. + *
  15233. + * This program is free software; you can redistribute it and/or modify
  15234. + * it under the terms of the GNU General Public License as published by
  15235. + * the Free Software Foundation; either version 2 of the License, or
  15236. + * (at your option) any later version.
  15237. + *
  15238. + * This program is distributed in the hope that it will be useful,
  15239. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15240. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15241. + * GNU General Public License for more details.
  15242. + *
  15243. + * You should have received a copy of the GNU General Public License
  15244. + * along with this program; if not, write to the Free Software
  15245. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15246. + */
  15247. +
  15248. +#include <linux/kernel.h>
  15249. +#include <linux/module.h>
  15250. +#include <linux/spinlock.h>
  15251. +#include <linux/clk.h>
  15252. +#include <linux/err.h>
  15253. +#include <linux/of.h>
  15254. +#include <linux/platform_device.h>
  15255. +#include <linux/io.h>
  15256. +#include <linux/slab.h>
  15257. +#include <linux/i2c.h>
  15258. +#include <linux/interrupt.h>
  15259. +#include <linux/sched.h>
  15260. +#include <linux/wait.h>
  15261. +
  15262. +/* BSC register offsets */
  15263. +#define BSC_C 0x00
  15264. +#define BSC_S 0x04
  15265. +#define BSC_DLEN 0x08
  15266. +#define BSC_A 0x0c
  15267. +#define BSC_FIFO 0x10
  15268. +#define BSC_DIV 0x14
  15269. +#define BSC_DEL 0x18
  15270. +#define BSC_CLKT 0x1c
  15271. +
  15272. +/* Bitfields in BSC_C */
  15273. +#define BSC_C_I2CEN 0x00008000
  15274. +#define BSC_C_INTR 0x00000400
  15275. +#define BSC_C_INTT 0x00000200
  15276. +#define BSC_C_INTD 0x00000100
  15277. +#define BSC_C_ST 0x00000080
  15278. +#define BSC_C_CLEAR_1 0x00000020
  15279. +#define BSC_C_CLEAR_2 0x00000010
  15280. +#define BSC_C_READ 0x00000001
  15281. +
  15282. +/* Bitfields in BSC_S */
  15283. +#define BSC_S_CLKT 0x00000200
  15284. +#define BSC_S_ERR 0x00000100
  15285. +#define BSC_S_RXF 0x00000080
  15286. +#define BSC_S_TXE 0x00000040
  15287. +#define BSC_S_RXD 0x00000020
  15288. +#define BSC_S_TXD 0x00000010
  15289. +#define BSC_S_RXR 0x00000008
  15290. +#define BSC_S_TXW 0x00000004
  15291. +#define BSC_S_DONE 0x00000002
  15292. +#define BSC_S_TA 0x00000001
  15293. +
  15294. +#define I2C_TIMEOUT_MS 150
  15295. +
  15296. +#define DRV_NAME "bcm2708_i2c"
  15297. +
  15298. +static unsigned int baudrate = CONFIG_I2C_BCM2708_BAUDRATE;
  15299. +module_param(baudrate, uint, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  15300. +MODULE_PARM_DESC(baudrate, "The I2C baudrate");
  15301. +
  15302. +static bool combined = false;
  15303. +module_param(combined, bool, 0644);
  15304. +MODULE_PARM_DESC(combined, "Use combined transactions");
  15305. +
  15306. +struct bcm2708_i2c {
  15307. + struct i2c_adapter adapter;
  15308. +
  15309. + spinlock_t lock;
  15310. + void __iomem *base;
  15311. + int irq;
  15312. + struct clk *clk;
  15313. +
  15314. + struct completion done;
  15315. +
  15316. + struct i2c_msg *msg;
  15317. + int pos;
  15318. + int nmsgs;
  15319. + bool error;
  15320. +};
  15321. +
  15322. +/*
  15323. + * This function sets the ALT mode on the I2C pins so that we can use them with
  15324. + * the BSC hardware.
  15325. + *
  15326. + * FIXME: This is a hack. Use pinmux / pinctrl.
  15327. + */
  15328. +static void bcm2708_i2c_init_pinmode(int id)
  15329. +{
  15330. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  15331. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  15332. +
  15333. + int pin;
  15334. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  15335. +
  15336. + BUG_ON(id != 0 && id != 1);
  15337. + /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
  15338. + for (pin = id*2+0; pin <= id*2+1; pin++) {
  15339. +printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
  15340. + INP_GPIO(pin); /* set mode to GPIO input first */
  15341. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  15342. + }
  15343. +
  15344. + iounmap(gpio);
  15345. +
  15346. +#undef INP_GPIO
  15347. +#undef SET_GPIO_ALT
  15348. +}
  15349. +
  15350. +static inline u32 bcm2708_rd(struct bcm2708_i2c *bi, unsigned reg)
  15351. +{
  15352. + return readl(bi->base + reg);
  15353. +}
  15354. +
  15355. +static inline void bcm2708_wr(struct bcm2708_i2c *bi, unsigned reg, u32 val)
  15356. +{
  15357. + writel(val, bi->base + reg);
  15358. +}
  15359. +
  15360. +static inline void bcm2708_bsc_reset(struct bcm2708_i2c *bi)
  15361. +{
  15362. + bcm2708_wr(bi, BSC_C, 0);
  15363. + bcm2708_wr(bi, BSC_S, BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE);
  15364. +}
  15365. +
  15366. +static inline void bcm2708_bsc_fifo_drain(struct bcm2708_i2c *bi)
  15367. +{
  15368. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_RXD) && (bi->pos < bi->msg->len))
  15369. + bi->msg->buf[bi->pos++] = bcm2708_rd(bi, BSC_FIFO);
  15370. +}
  15371. +
  15372. +static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
  15373. +{
  15374. + while ((bcm2708_rd(bi, BSC_S) & BSC_S_TXD) && (bi->pos < bi->msg->len))
  15375. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  15376. +}
  15377. +
  15378. +static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
  15379. +{
  15380. + unsigned long bus_hz;
  15381. + u32 cdiv, s;
  15382. + u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
  15383. +
  15384. + bus_hz = clk_get_rate(bi->clk);
  15385. + cdiv = bus_hz / baudrate;
  15386. + if (cdiv > 0xffff)
  15387. + cdiv = 0xffff;
  15388. +
  15389. + if (bi->msg->flags & I2C_M_RD)
  15390. + c |= BSC_C_INTR | BSC_C_READ;
  15391. + else
  15392. + c |= BSC_C_INTT;
  15393. +
  15394. + bcm2708_wr(bi, BSC_DIV, cdiv);
  15395. + bcm2708_wr(bi, BSC_A, bi->msg->addr);
  15396. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  15397. + if (combined)
  15398. + {
  15399. + /* Do the next two messages meet combined transaction criteria?
  15400. + - Current message is a write, next message is a read
  15401. + - Both messages to same slave address
  15402. + - Write message can fit inside FIFO (16 bytes or less) */
  15403. + if ( (bi->nmsgs > 1) &&
  15404. + !(bi->msg[0].flags & I2C_M_RD) && (bi->msg[1].flags & I2C_M_RD) &&
  15405. + (bi->msg[0].addr == bi->msg[1].addr) && (bi->msg[0].len <= 16)) {
  15406. + /* Fill FIFO with entire write message (16 byte FIFO) */
  15407. + while (bi->pos < bi->msg->len)
  15408. + bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
  15409. + /* Start write transfer (no interrupts, don't clear FIFO) */
  15410. + bcm2708_wr(bi, BSC_C, BSC_C_I2CEN | BSC_C_ST);
  15411. + /* poll for transfer start bit (should only take 1-20 polls) */
  15412. + do {
  15413. + s = bcm2708_rd(bi, BSC_S);
  15414. + } while (!(s & (BSC_S_TA | BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE)));
  15415. + /* Send next read message before the write transfer finishes. */
  15416. + bi->nmsgs--;
  15417. + bi->msg++;
  15418. + bi->pos = 0;
  15419. + bcm2708_wr(bi, BSC_DLEN, bi->msg->len);
  15420. + c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_INTR | BSC_C_ST | BSC_C_READ;
  15421. + }
  15422. + }
  15423. + bcm2708_wr(bi, BSC_C, c);
  15424. +}
  15425. +
  15426. +static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
  15427. +{
  15428. + struct bcm2708_i2c *bi = dev_id;
  15429. + bool handled = true;
  15430. + u32 s;
  15431. +
  15432. + spin_lock(&bi->lock);
  15433. +
  15434. + /* we may see camera interrupts on the "other" I2C channel
  15435. + Just return if we've not sent anything */
  15436. + if (!bi->nmsgs || !bi->msg )
  15437. + goto early_exit;
  15438. +
  15439. + s = bcm2708_rd(bi, BSC_S);
  15440. +
  15441. + if (s & (BSC_S_CLKT | BSC_S_ERR)) {
  15442. + bcm2708_bsc_reset(bi);
  15443. + bi->error = true;
  15444. +
  15445. + /* wake up our bh */
  15446. + complete(&bi->done);
  15447. + } else if (s & BSC_S_DONE) {
  15448. + bi->nmsgs--;
  15449. +
  15450. + if (bi->msg->flags & I2C_M_RD)
  15451. + bcm2708_bsc_fifo_drain(bi);
  15452. +
  15453. + bcm2708_bsc_reset(bi);
  15454. +
  15455. + if (bi->nmsgs) {
  15456. + /* advance to next message */
  15457. + bi->msg++;
  15458. + bi->pos = 0;
  15459. + bcm2708_bsc_setup(bi);
  15460. + } else {
  15461. + /* wake up our bh */
  15462. + complete(&bi->done);
  15463. + }
  15464. + } else if (s & BSC_S_TXW) {
  15465. + bcm2708_bsc_fifo_fill(bi);
  15466. + } else if (s & BSC_S_RXR) {
  15467. + bcm2708_bsc_fifo_drain(bi);
  15468. + } else {
  15469. + handled = false;
  15470. + }
  15471. +
  15472. +early_exit:
  15473. + spin_unlock(&bi->lock);
  15474. +
  15475. + return handled ? IRQ_HANDLED : IRQ_NONE;
  15476. +}
  15477. +
  15478. +static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
  15479. + struct i2c_msg *msgs, int num)
  15480. +{
  15481. + struct bcm2708_i2c *bi = adap->algo_data;
  15482. + unsigned long flags;
  15483. + int ret;
  15484. +
  15485. + spin_lock_irqsave(&bi->lock, flags);
  15486. +
  15487. + reinit_completion(&bi->done);
  15488. + bi->msg = msgs;
  15489. + bi->pos = 0;
  15490. + bi->nmsgs = num;
  15491. + bi->error = false;
  15492. +
  15493. + spin_unlock_irqrestore(&bi->lock, flags);
  15494. +
  15495. + bcm2708_bsc_setup(bi);
  15496. +
  15497. + ret = wait_for_completion_timeout(&bi->done,
  15498. + msecs_to_jiffies(I2C_TIMEOUT_MS));
  15499. + if (ret == 0) {
  15500. + dev_err(&adap->dev, "transfer timed out\n");
  15501. + spin_lock_irqsave(&bi->lock, flags);
  15502. + bcm2708_bsc_reset(bi);
  15503. + spin_unlock_irqrestore(&bi->lock, flags);
  15504. + return -ETIMEDOUT;
  15505. + }
  15506. +
  15507. + return bi->error ? -EIO : num;
  15508. +}
  15509. +
  15510. +static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
  15511. +{
  15512. + return I2C_FUNC_I2C | /*I2C_FUNC_10BIT_ADDR |*/ I2C_FUNC_SMBUS_EMUL;
  15513. +}
  15514. +
  15515. +static struct i2c_algorithm bcm2708_i2c_algorithm = {
  15516. + .master_xfer = bcm2708_i2c_master_xfer,
  15517. + .functionality = bcm2708_i2c_functionality,
  15518. +};
  15519. +
  15520. +static int bcm2708_i2c_probe(struct platform_device *pdev)
  15521. +{
  15522. + struct resource *regs;
  15523. + int irq, err = -ENOMEM;
  15524. + struct clk *clk;
  15525. + struct bcm2708_i2c *bi;
  15526. + struct i2c_adapter *adap;
  15527. + unsigned long bus_hz;
  15528. + u32 cdiv;
  15529. +
  15530. + if (pdev->dev.of_node) {
  15531. + u32 bus_clk_rate;
  15532. + pdev->id = of_alias_get_id(pdev->dev.of_node, "i2c");
  15533. + if (pdev->id < 0) {
  15534. + dev_err(&pdev->dev, "alias is missing\n");
  15535. + return -EINVAL;
  15536. + }
  15537. + if (!of_property_read_u32(pdev->dev.of_node,
  15538. + "clock-frequency", &bus_clk_rate))
  15539. + baudrate = bus_clk_rate;
  15540. + else
  15541. + dev_warn(&pdev->dev,
  15542. + "Could not read clock-frequency property\n");
  15543. + }
  15544. +
  15545. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  15546. + if (!regs) {
  15547. + dev_err(&pdev->dev, "could not get IO memory\n");
  15548. + return -ENXIO;
  15549. + }
  15550. +
  15551. + irq = platform_get_irq(pdev, 0);
  15552. + if (irq < 0) {
  15553. + dev_err(&pdev->dev, "could not get IRQ\n");
  15554. + return irq;
  15555. + }
  15556. +
  15557. + clk = clk_get(&pdev->dev, NULL);
  15558. + if (IS_ERR(clk)) {
  15559. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  15560. + return PTR_ERR(clk);
  15561. + }
  15562. +
  15563. + bcm2708_i2c_init_pinmode(pdev->id);
  15564. +
  15565. + bi = kzalloc(sizeof(*bi), GFP_KERNEL);
  15566. + if (!bi)
  15567. + goto out_clk_put;
  15568. +
  15569. + platform_set_drvdata(pdev, bi);
  15570. +
  15571. + adap = &bi->adapter;
  15572. + adap->class = I2C_CLASS_HWMON | I2C_CLASS_DDC;
  15573. + adap->algo = &bcm2708_i2c_algorithm;
  15574. + adap->algo_data = bi;
  15575. + adap->dev.parent = &pdev->dev;
  15576. + adap->nr = pdev->id;
  15577. + strlcpy(adap->name, dev_name(&pdev->dev), sizeof(adap->name));
  15578. + adap->dev.of_node = pdev->dev.of_node;
  15579. +
  15580. + switch (pdev->id) {
  15581. + case 0:
  15582. + adap->class = I2C_CLASS_HWMON;
  15583. + break;
  15584. + case 1:
  15585. + adap->class = I2C_CLASS_DDC;
  15586. + break;
  15587. + default:
  15588. + dev_err(&pdev->dev, "can only bind to BSC 0 or 1\n");
  15589. + err = -ENXIO;
  15590. + goto out_free_bi;
  15591. + }
  15592. +
  15593. + spin_lock_init(&bi->lock);
  15594. + init_completion(&bi->done);
  15595. +
  15596. + bi->base = ioremap(regs->start, resource_size(regs));
  15597. + if (!bi->base) {
  15598. + dev_err(&pdev->dev, "could not remap memory\n");
  15599. + goto out_free_bi;
  15600. + }
  15601. +
  15602. + bi->irq = irq;
  15603. + bi->clk = clk;
  15604. +
  15605. + err = request_irq(irq, bcm2708_i2c_interrupt, IRQF_SHARED,
  15606. + dev_name(&pdev->dev), bi);
  15607. + if (err) {
  15608. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  15609. + goto out_iounmap;
  15610. + }
  15611. +
  15612. + bcm2708_bsc_reset(bi);
  15613. +
  15614. + err = i2c_add_numbered_adapter(adap);
  15615. + if (err < 0) {
  15616. + dev_err(&pdev->dev, "could not add I2C adapter: %d\n", err);
  15617. + goto out_free_irq;
  15618. + }
  15619. +
  15620. + bus_hz = clk_get_rate(bi->clk);
  15621. + cdiv = bus_hz / baudrate;
  15622. + if (cdiv > 0xffff) {
  15623. + cdiv = 0xffff;
  15624. + baudrate = bus_hz / cdiv;
  15625. + }
  15626. +
  15627. + dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
  15628. + pdev->id, (unsigned long)regs->start, irq, baudrate);
  15629. +
  15630. + return 0;
  15631. +
  15632. +out_free_irq:
  15633. + free_irq(bi->irq, bi);
  15634. +out_iounmap:
  15635. + iounmap(bi->base);
  15636. +out_free_bi:
  15637. + kfree(bi);
  15638. +out_clk_put:
  15639. + clk_put(clk);
  15640. + return err;
  15641. +}
  15642. +
  15643. +static int bcm2708_i2c_remove(struct platform_device *pdev)
  15644. +{
  15645. + struct bcm2708_i2c *bi = platform_get_drvdata(pdev);
  15646. +
  15647. + platform_set_drvdata(pdev, NULL);
  15648. +
  15649. + i2c_del_adapter(&bi->adapter);
  15650. + free_irq(bi->irq, bi);
  15651. + iounmap(bi->base);
  15652. + clk_disable(bi->clk);
  15653. + clk_put(bi->clk);
  15654. + kfree(bi);
  15655. +
  15656. + return 0;
  15657. +}
  15658. +
  15659. +static const struct of_device_id bcm2708_i2c_of_match[] = {
  15660. + { .compatible = "brcm,bcm2708-i2c" },
  15661. + {},
  15662. +};
  15663. +MODULE_DEVICE_TABLE(of, bcm2708_i2c_of_match);
  15664. +
  15665. +static struct platform_driver bcm2708_i2c_driver = {
  15666. + .driver = {
  15667. + .name = DRV_NAME,
  15668. + .owner = THIS_MODULE,
  15669. + .of_match_table = bcm2708_i2c_of_match,
  15670. + },
  15671. + .probe = bcm2708_i2c_probe,
  15672. + .remove = bcm2708_i2c_remove,
  15673. +};
  15674. +
  15675. +// module_platform_driver(bcm2708_i2c_driver);
  15676. +
  15677. +
  15678. +static int __init bcm2708_i2c_init(void)
  15679. +{
  15680. + return platform_driver_register(&bcm2708_i2c_driver);
  15681. +}
  15682. +
  15683. +static void __exit bcm2708_i2c_exit(void)
  15684. +{
  15685. + platform_driver_unregister(&bcm2708_i2c_driver);
  15686. +}
  15687. +
  15688. +module_init(bcm2708_i2c_init);
  15689. +module_exit(bcm2708_i2c_exit);
  15690. +
  15691. +
  15692. +
  15693. +MODULE_DESCRIPTION("BSC controller driver for Broadcom BCM2708");
  15694. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  15695. +MODULE_LICENSE("GPL v2");
  15696. +MODULE_ALIAS("platform:" DRV_NAME);
  15697. diff -Nur linux-3.16.2/drivers/i2c/busses/Kconfig linux-3.16-rpi/drivers/i2c/busses/Kconfig
  15698. --- linux-3.16.2/drivers/i2c/busses/Kconfig 2014-09-06 01:37:11.000000000 +0200
  15699. +++ linux-3.16-rpi/drivers/i2c/busses/Kconfig 2014-09-14 19:03:20.000000000 +0200
  15700. @@ -338,7 +338,7 @@
  15701. config I2C_BCM2835
  15702. tristate "Broadcom BCM2835 I2C controller"
  15703. - depends on ARCH_BCM2835
  15704. + depends on ARCH_BCM2835 || ARCH_BCM2708
  15705. help
  15706. If you say yes to this option, support will be included for the
  15707. BCM2835 I2C controller.
  15708. @@ -348,6 +348,25 @@
  15709. This support is also available as a module. If so, the module
  15710. will be called i2c-bcm2835.
  15711. +config I2C_BCM2708
  15712. + tristate "BCM2708 BSC"
  15713. + depends on MACH_BCM2708
  15714. + help
  15715. + Enabling this option will add BSC (Broadcom Serial Controller)
  15716. + support for the BCM2708. BSC is a Broadcom proprietary bus compatible
  15717. + with I2C/TWI/SMBus.
  15718. +
  15719. +config I2C_BCM2708_BAUDRATE
  15720. + prompt "BCM2708 I2C baudrate"
  15721. + depends on I2C_BCM2708
  15722. + int
  15723. + default 100000
  15724. + help
  15725. + Set the I2C baudrate. This will alter the default value. A
  15726. + different baudrate can be set by using a module parameter as well. If
  15727. + no parameter is provided when loading, this is the value that will be
  15728. + used.
  15729. +
  15730. config I2C_BCM_KONA
  15731. tristate "BCM Kona I2C adapter"
  15732. depends on ARCH_BCM_MOBILE
  15733. diff -Nur linux-3.16.2/drivers/i2c/busses/Makefile linux-3.16-rpi/drivers/i2c/busses/Makefile
  15734. --- linux-3.16.2/drivers/i2c/busses/Makefile 2014-09-06 01:37:11.000000000 +0200
  15735. +++ linux-3.16-rpi/drivers/i2c/busses/Makefile 2014-09-14 19:03:20.000000000 +0200
  15736. @@ -32,6 +32,7 @@
  15737. obj-$(CONFIG_I2C_AT91) += i2c-at91.o
  15738. obj-$(CONFIG_I2C_AU1550) += i2c-au1550.o
  15739. obj-$(CONFIG_I2C_BCM2835) += i2c-bcm2835.o
  15740. +obj-$(CONFIG_I2C_BCM2708) += i2c-bcm2708.o
  15741. obj-$(CONFIG_I2C_BLACKFIN_TWI) += i2c-bfin-twi.o
  15742. obj-$(CONFIG_I2C_CADENCE) += i2c-cadence.o
  15743. obj-$(CONFIG_I2C_CBUS_GPIO) += i2c-cbus-gpio.o
  15744. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/bcm2835-camera.c linux-3.16-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c
  15745. --- linux-3.16.2/drivers/media/platform/bcm2835/bcm2835-camera.c 1970-01-01 01:00:00.000000000 +0100
  15746. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/bcm2835-camera.c 2014-09-14 19:03:24.000000000 +0200
  15747. @@ -0,0 +1,1827 @@
  15748. +/*
  15749. + * Broadcom BM2835 V4L2 driver
  15750. + *
  15751. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  15752. + *
  15753. + * This file is subject to the terms and conditions of the GNU General Public
  15754. + * License. See the file COPYING in the main directory of this archive
  15755. + * for more details.
  15756. + *
  15757. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  15758. + * Dave Stevenson <dsteve@broadcom.com>
  15759. + * Simon Mellor <simellor@broadcom.com>
  15760. + * Luke Diamand <luked@broadcom.com>
  15761. + */
  15762. +
  15763. +#include <linux/errno.h>
  15764. +#include <linux/kernel.h>
  15765. +#include <linux/module.h>
  15766. +#include <linux/slab.h>
  15767. +#include <media/videobuf2-vmalloc.h>
  15768. +#include <media/videobuf2-dma-contig.h>
  15769. +#include <media/v4l2-device.h>
  15770. +#include <media/v4l2-ioctl.h>
  15771. +#include <media/v4l2-ctrls.h>
  15772. +#include <media/v4l2-fh.h>
  15773. +#include <media/v4l2-event.h>
  15774. +#include <media/v4l2-common.h>
  15775. +#include <linux/delay.h>
  15776. +
  15777. +#include "mmal-common.h"
  15778. +#include "mmal-encodings.h"
  15779. +#include "mmal-vchiq.h"
  15780. +#include "mmal-msg.h"
  15781. +#include "mmal-parameters.h"
  15782. +#include "bcm2835-camera.h"
  15783. +
  15784. +#define BM2835_MMAL_VERSION "0.0.2"
  15785. +#define BM2835_MMAL_MODULE_NAME "bcm2835-v4l2"
  15786. +#define MIN_WIDTH 16
  15787. +#define MIN_HEIGHT 16
  15788. +#define MAX_WIDTH 2592
  15789. +#define MAX_HEIGHT 1944
  15790. +#define MIN_BUFFER_SIZE (80*1024)
  15791. +
  15792. +#define MAX_VIDEO_MODE_WIDTH 1280
  15793. +#define MAX_VIDEO_MODE_HEIGHT 720
  15794. +
  15795. +MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
  15796. +MODULE_AUTHOR("Vincent Sanders");
  15797. +MODULE_LICENSE("GPL");
  15798. +MODULE_VERSION(BM2835_MMAL_VERSION);
  15799. +
  15800. +int bcm2835_v4l2_debug;
  15801. +module_param_named(debug, bcm2835_v4l2_debug, int, 0644);
  15802. +MODULE_PARM_DESC(bcm2835_v4l2_debug, "Debug level 0-2");
  15803. +
  15804. +int max_video_width = MAX_VIDEO_MODE_WIDTH;
  15805. +int max_video_height = MAX_VIDEO_MODE_HEIGHT;
  15806. +module_param(max_video_width, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  15807. +MODULE_PARM_DESC(max_video_width, "Threshold for video mode");
  15808. +module_param(max_video_height, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  15809. +MODULE_PARM_DESC(max_video_height, "Threshold for video mode");
  15810. +
  15811. +/* Gstreamer bug https://bugzilla.gnome.org/show_bug.cgi?id=726521
  15812. + * v4l2src does bad (and actually wrong) things when the vidioc_enum_framesizes
  15813. + * function says type V4L2_FRMSIZE_TYPE_STEPWISE, which we do by default.
  15814. + * It's happier if we just don't say anything at all, when it then
  15815. + * sets up a load of defaults that it thinks might work.
  15816. + * If gst_v4l2src_is_broken is non-zero, then we remove the function from
  15817. + * our function table list (actually switch to an alternate set, but same
  15818. + * result).
  15819. + */
  15820. +int gst_v4l2src_is_broken = 0;
  15821. +module_param(gst_v4l2src_is_broken, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  15822. +MODULE_PARM_DESC(gst_v4l2src_is_broken, "If non-zero, enable workaround for Gstreamer");
  15823. +
  15824. +static struct bm2835_mmal_dev *gdev; /* global device data */
  15825. +
  15826. +#define FPS_MIN 1
  15827. +#define FPS_MAX 90
  15828. +
  15829. +/* timeperframe: min/max and default */
  15830. +static const struct v4l2_fract
  15831. + tpf_min = {.numerator = 1, .denominator = FPS_MAX},
  15832. + tpf_max = {.numerator = 1, .denominator = FPS_MIN},
  15833. + tpf_default = {.numerator = 1000, .denominator = 30000};
  15834. +
  15835. +/* video formats */
  15836. +static struct mmal_fmt formats[] = {
  15837. + {
  15838. + .name = "4:2:0, packed YUV",
  15839. + .fourcc = V4L2_PIX_FMT_YUV420,
  15840. + .flags = 0,
  15841. + .mmal = MMAL_ENCODING_I420,
  15842. + .depth = 12,
  15843. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15844. + },
  15845. + {
  15846. + .name = "4:2:2, packed, YUYV",
  15847. + .fourcc = V4L2_PIX_FMT_YUYV,
  15848. + .flags = 0,
  15849. + .mmal = MMAL_ENCODING_YUYV,
  15850. + .depth = 16,
  15851. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15852. + },
  15853. + {
  15854. + .name = "RGB24 (LE)",
  15855. + .fourcc = V4L2_PIX_FMT_RGB24,
  15856. + .flags = 0,
  15857. + .mmal = MMAL_ENCODING_BGR24,
  15858. + .depth = 24,
  15859. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15860. + },
  15861. + {
  15862. + .name = "JPEG",
  15863. + .fourcc = V4L2_PIX_FMT_JPEG,
  15864. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  15865. + .mmal = MMAL_ENCODING_JPEG,
  15866. + .depth = 8,
  15867. + .mmal_component = MMAL_COMPONENT_IMAGE_ENCODE,
  15868. + },
  15869. + {
  15870. + .name = "H264",
  15871. + .fourcc = V4L2_PIX_FMT_H264,
  15872. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  15873. + .mmal = MMAL_ENCODING_H264,
  15874. + .depth = 8,
  15875. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  15876. + },
  15877. + {
  15878. + .name = "MJPEG",
  15879. + .fourcc = V4L2_PIX_FMT_MJPEG,
  15880. + .flags = V4L2_FMT_FLAG_COMPRESSED,
  15881. + .mmal = MMAL_ENCODING_MJPEG,
  15882. + .depth = 8,
  15883. + .mmal_component = MMAL_COMPONENT_VIDEO_ENCODE,
  15884. + },
  15885. + {
  15886. + .name = "4:2:2, packed, YVYU",
  15887. + .fourcc = V4L2_PIX_FMT_YVYU,
  15888. + .flags = 0,
  15889. + .mmal = MMAL_ENCODING_YVYU,
  15890. + .depth = 16,
  15891. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15892. + },
  15893. + {
  15894. + .name = "4:2:2, packed, VYUY",
  15895. + .fourcc = V4L2_PIX_FMT_VYUY,
  15896. + .flags = 0,
  15897. + .mmal = MMAL_ENCODING_VYUY,
  15898. + .depth = 16,
  15899. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15900. + },
  15901. + {
  15902. + .name = "4:2:2, packed, UYVY",
  15903. + .fourcc = V4L2_PIX_FMT_UYVY,
  15904. + .flags = 0,
  15905. + .mmal = MMAL_ENCODING_UYVY,
  15906. + .depth = 16,
  15907. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15908. + },
  15909. + {
  15910. + .name = "4:2:0, packed, NV12",
  15911. + .fourcc = V4L2_PIX_FMT_NV12,
  15912. + .flags = 0,
  15913. + .mmal = MMAL_ENCODING_NV12,
  15914. + .depth = 12,
  15915. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15916. + },
  15917. + {
  15918. + .name = "RGB24 (BE)",
  15919. + .fourcc = V4L2_PIX_FMT_BGR24,
  15920. + .flags = 0,
  15921. + .mmal = MMAL_ENCODING_RGB24,
  15922. + .depth = 24,
  15923. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15924. + },
  15925. + {
  15926. + .name = "4:2:0, packed YVU",
  15927. + .fourcc = V4L2_PIX_FMT_YVU420,
  15928. + .flags = 0,
  15929. + .mmal = MMAL_ENCODING_YV12,
  15930. + .depth = 12,
  15931. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15932. + },
  15933. + {
  15934. + .name = "4:2:0, packed, NV21",
  15935. + .fourcc = V4L2_PIX_FMT_NV21,
  15936. + .flags = 0,
  15937. + .mmal = MMAL_ENCODING_NV21,
  15938. + .depth = 12,
  15939. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15940. + },
  15941. + {
  15942. + .name = "RGB32 (BE)",
  15943. + .fourcc = V4L2_PIX_FMT_BGR32,
  15944. + .flags = 0,
  15945. + .mmal = MMAL_ENCODING_BGRA,
  15946. + .depth = 32,
  15947. + .mmal_component = MMAL_COMPONENT_CAMERA,
  15948. + },
  15949. +};
  15950. +
  15951. +static struct mmal_fmt *get_format(struct v4l2_format *f)
  15952. +{
  15953. + struct mmal_fmt *fmt;
  15954. + unsigned int k;
  15955. +
  15956. + for (k = 0; k < ARRAY_SIZE(formats); k++) {
  15957. + fmt = &formats[k];
  15958. + if (fmt->fourcc == f->fmt.pix.pixelformat)
  15959. + break;
  15960. + }
  15961. +
  15962. + if (k == ARRAY_SIZE(formats))
  15963. + return NULL;
  15964. +
  15965. + return &formats[k];
  15966. +}
  15967. +
  15968. +/* ------------------------------------------------------------------
  15969. + Videobuf queue operations
  15970. + ------------------------------------------------------------------*/
  15971. +
  15972. +static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  15973. + unsigned int *nbuffers, unsigned int *nplanes,
  15974. + unsigned int sizes[], void *alloc_ctxs[])
  15975. +{
  15976. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  15977. + unsigned long size;
  15978. +
  15979. + /* refuse queue setup if port is not configured */
  15980. + if (dev->capture.port == NULL) {
  15981. + v4l2_err(&dev->v4l2_dev,
  15982. + "%s: capture port not configured\n", __func__);
  15983. + return -EINVAL;
  15984. + }
  15985. +
  15986. + size = dev->capture.port->current_buffer.size;
  15987. + if (size == 0) {
  15988. + v4l2_err(&dev->v4l2_dev,
  15989. + "%s: capture port buffer size is zero\n", __func__);
  15990. + return -EINVAL;
  15991. + }
  15992. +
  15993. + if (*nbuffers < (dev->capture.port->current_buffer.num + 2))
  15994. + *nbuffers = (dev->capture.port->current_buffer.num + 2);
  15995. +
  15996. + *nplanes = 1;
  15997. +
  15998. + sizes[0] = size;
  15999. +
  16000. + /*
  16001. + * videobuf2-vmalloc allocator is context-less so no need to set
  16002. + * alloc_ctxs array.
  16003. + */
  16004. +
  16005. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  16006. + __func__, dev);
  16007. +
  16008. + return 0;
  16009. +}
  16010. +
  16011. +static int buffer_prepare(struct vb2_buffer *vb)
  16012. +{
  16013. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  16014. + unsigned long size;
  16015. +
  16016. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  16017. + __func__, dev);
  16018. +
  16019. + BUG_ON(dev->capture.port == NULL);
  16020. + BUG_ON(dev->capture.fmt == NULL);
  16021. +
  16022. + size = dev->capture.stride * dev->capture.height;
  16023. + if (vb2_plane_size(vb, 0) < size) {
  16024. + v4l2_err(&dev->v4l2_dev,
  16025. + "%s data will not fit into plane (%lu < %lu)\n",
  16026. + __func__, vb2_plane_size(vb, 0), size);
  16027. + return -EINVAL;
  16028. + }
  16029. +
  16030. + return 0;
  16031. +}
  16032. +
  16033. +static inline bool is_capturing(struct bm2835_mmal_dev *dev)
  16034. +{
  16035. + return dev->capture.camera_port ==
  16036. + &dev->
  16037. + component[MMAL_COMPONENT_CAMERA]->output[MMAL_CAMERA_PORT_CAPTURE];
  16038. +}
  16039. +
  16040. +static void buffer_cb(struct vchiq_mmal_instance *instance,
  16041. + struct vchiq_mmal_port *port,
  16042. + int status,
  16043. + struct mmal_buffer *buf,
  16044. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts)
  16045. +{
  16046. + struct bm2835_mmal_dev *dev = port->cb_ctx;
  16047. +
  16048. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16049. + "%s: status:%d, buf:%p, length:%lu, flags %u, pts %lld\n",
  16050. + __func__, status, buf, length, mmal_flags, pts);
  16051. +
  16052. + if (status != 0) {
  16053. + /* error in transfer */
  16054. + if (buf != NULL) {
  16055. + /* there was a buffer with the error so return it */
  16056. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  16057. + }
  16058. + return;
  16059. + } else if (length == 0) {
  16060. + /* stream ended */
  16061. + if (buf != NULL) {
  16062. + /* this should only ever happen if the port is
  16063. + * disabled and there are buffers still queued
  16064. + */
  16065. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  16066. + pr_debug("Empty buffer");
  16067. + } else if (dev->capture.frame_count) {
  16068. + /* grab another frame */
  16069. + if (is_capturing(dev)) {
  16070. + pr_debug("Grab another frame");
  16071. + vchiq_mmal_port_parameter_set(
  16072. + instance,
  16073. + dev->capture.
  16074. + camera_port,
  16075. + MMAL_PARAMETER_CAPTURE,
  16076. + &dev->capture.
  16077. + frame_count,
  16078. + sizeof(dev->capture.frame_count));
  16079. + }
  16080. + } else {
  16081. + /* signal frame completion */
  16082. + complete(&dev->capture.frame_cmplt);
  16083. + }
  16084. + } else {
  16085. + if (dev->capture.frame_count) {
  16086. + if (dev->capture.vc_start_timestamp != -1 &&
  16087. + pts != 0) {
  16088. + s64 runtime_us = pts -
  16089. + dev->capture.vc_start_timestamp;
  16090. + u32 div = 0;
  16091. + u32 rem = 0;
  16092. +
  16093. + div =
  16094. + div_u64_rem(runtime_us, USEC_PER_SEC, &rem);
  16095. + buf->vb.v4l2_buf.timestamp.tv_sec =
  16096. + dev->capture.kernel_start_ts.tv_sec - 1 +
  16097. + div;
  16098. + buf->vb.v4l2_buf.timestamp.tv_usec =
  16099. + dev->capture.kernel_start_ts.tv_usec + rem;
  16100. +
  16101. + if (buf->vb.v4l2_buf.timestamp.tv_usec >=
  16102. + USEC_PER_SEC) {
  16103. + buf->vb.v4l2_buf.timestamp.tv_sec++;
  16104. + buf->vb.v4l2_buf.timestamp.tv_usec -=
  16105. + USEC_PER_SEC;
  16106. + }
  16107. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16108. + "Convert start time %d.%06d and %llu "
  16109. + "with offset %llu to %d.%06d\n",
  16110. + (int)dev->capture.kernel_start_ts.
  16111. + tv_sec,
  16112. + (int)dev->capture.kernel_start_ts.
  16113. + tv_usec,
  16114. + dev->capture.vc_start_timestamp, pts,
  16115. + (int)buf->vb.v4l2_buf.timestamp.tv_sec,
  16116. + (int)buf->vb.v4l2_buf.timestamp.
  16117. + tv_usec);
  16118. + } else {
  16119. + v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  16120. + }
  16121. +
  16122. + vb2_set_plane_payload(&buf->vb, 0, length);
  16123. + vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  16124. +
  16125. + if (mmal_flags & MMAL_BUFFER_HEADER_FLAG_EOS &&
  16126. + is_capturing(dev)) {
  16127. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16128. + "Grab another frame as buffer has EOS");
  16129. + vchiq_mmal_port_parameter_set(
  16130. + instance,
  16131. + dev->capture.
  16132. + camera_port,
  16133. + MMAL_PARAMETER_CAPTURE,
  16134. + &dev->capture.
  16135. + frame_count,
  16136. + sizeof(dev->capture.frame_count));
  16137. + }
  16138. + } else {
  16139. + /* signal frame completion */
  16140. + complete(&dev->capture.frame_cmplt);
  16141. + }
  16142. + }
  16143. +}
  16144. +
  16145. +static int enable_camera(struct bm2835_mmal_dev *dev)
  16146. +{
  16147. + int ret;
  16148. + if (!dev->camera_use_count) {
  16149. + ret = vchiq_mmal_component_enable(
  16150. + dev->instance,
  16151. + dev->component[MMAL_COMPONENT_CAMERA]);
  16152. + if (ret < 0) {
  16153. + v4l2_err(&dev->v4l2_dev,
  16154. + "Failed enabling camera, ret %d\n", ret);
  16155. + return -EINVAL;
  16156. + }
  16157. + }
  16158. + dev->camera_use_count++;
  16159. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16160. + &dev->v4l2_dev, "enabled camera (refcount %d)\n",
  16161. + dev->camera_use_count);
  16162. + return 0;
  16163. +}
  16164. +
  16165. +static int disable_camera(struct bm2835_mmal_dev *dev)
  16166. +{
  16167. + int ret;
  16168. + if (!dev->camera_use_count) {
  16169. + v4l2_err(&dev->v4l2_dev,
  16170. + "Disabled the camera when already disabled\n");
  16171. + return -EINVAL;
  16172. + }
  16173. + dev->camera_use_count--;
  16174. + if (!dev->camera_use_count) {
  16175. + unsigned int i = 0xFFFFFFFF;
  16176. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16177. + "Disabling camera\n");
  16178. + ret =
  16179. + vchiq_mmal_component_disable(
  16180. + dev->instance,
  16181. + dev->component[MMAL_COMPONENT_CAMERA]);
  16182. + if (ret < 0) {
  16183. + v4l2_err(&dev->v4l2_dev,
  16184. + "Failed disabling camera, ret %d\n", ret);
  16185. + return -EINVAL;
  16186. + }
  16187. + vchiq_mmal_port_parameter_set(
  16188. + dev->instance,
  16189. + &dev->component[MMAL_COMPONENT_CAMERA]->control,
  16190. + MMAL_PARAMETER_CAMERA_NUM, &i,
  16191. + sizeof(i));
  16192. + }
  16193. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16194. + "Camera refcount now %d\n", dev->camera_use_count);
  16195. + return 0;
  16196. +}
  16197. +
  16198. +static void buffer_queue(struct vb2_buffer *vb)
  16199. +{
  16200. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  16201. + struct mmal_buffer *buf = container_of(vb, struct mmal_buffer, vb);
  16202. + int ret;
  16203. +
  16204. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16205. + "%s: dev:%p buf:%p\n", __func__, dev, buf);
  16206. +
  16207. + buf->buffer = vb2_plane_vaddr(&buf->vb, 0);
  16208. + buf->buffer_size = vb2_plane_size(&buf->vb, 0);
  16209. +
  16210. + ret = vchiq_mmal_submit_buffer(dev->instance, dev->capture.port, buf);
  16211. + if (ret < 0)
  16212. + v4l2_err(&dev->v4l2_dev, "%s: error submitting buffer\n",
  16213. + __func__);
  16214. +}
  16215. +
  16216. +static int start_streaming(struct vb2_queue *vq, unsigned int count)
  16217. +{
  16218. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  16219. + int ret;
  16220. + int parameter_size;
  16221. +
  16222. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  16223. + __func__, dev);
  16224. +
  16225. + /* ensure a format has actually been set */
  16226. + if (dev->capture.port == NULL)
  16227. + return -EINVAL;
  16228. +
  16229. + if (enable_camera(dev) < 0) {
  16230. + v4l2_err(&dev->v4l2_dev, "Failed to enable camera\n");
  16231. + return -EINVAL;
  16232. + }
  16233. +
  16234. + /*init_completion(&dev->capture.frame_cmplt); */
  16235. +
  16236. + /* enable frame capture */
  16237. + dev->capture.frame_count = 1;
  16238. +
  16239. + /* if the preview is not already running, wait for a few frames for AGC
  16240. + * to settle down.
  16241. + */
  16242. + if (!dev->component[MMAL_COMPONENT_PREVIEW]->enabled)
  16243. + msleep(300);
  16244. +
  16245. + /* enable the connection from camera to encoder (if applicable) */
  16246. + if (dev->capture.camera_port != dev->capture.port
  16247. + && dev->capture.camera_port) {
  16248. + ret = vchiq_mmal_port_enable(dev->instance,
  16249. + dev->capture.camera_port, NULL);
  16250. + if (ret) {
  16251. + v4l2_err(&dev->v4l2_dev,
  16252. + "Failed to enable encode tunnel - error %d\n",
  16253. + ret);
  16254. + return -1;
  16255. + }
  16256. + }
  16257. +
  16258. + /* Get VC timestamp at this point in time */
  16259. + parameter_size = sizeof(dev->capture.vc_start_timestamp);
  16260. + if (vchiq_mmal_port_parameter_get(dev->instance,
  16261. + dev->capture.camera_port,
  16262. + MMAL_PARAMETER_SYSTEM_TIME,
  16263. + &dev->capture.vc_start_timestamp,
  16264. + &parameter_size)) {
  16265. + v4l2_err(&dev->v4l2_dev,
  16266. + "Failed to get VC start time - update your VC f/w\n");
  16267. +
  16268. + /* Flag to indicate just to rely on kernel timestamps */
  16269. + dev->capture.vc_start_timestamp = -1;
  16270. + } else
  16271. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16272. + "Start time %lld size %d\n",
  16273. + dev->capture.vc_start_timestamp, parameter_size);
  16274. +
  16275. + v4l2_get_timestamp(&dev->capture.kernel_start_ts);
  16276. +
  16277. + /* enable the camera port */
  16278. + dev->capture.port->cb_ctx = dev;
  16279. + ret =
  16280. + vchiq_mmal_port_enable(dev->instance, dev->capture.port, buffer_cb);
  16281. + if (ret) {
  16282. + v4l2_err(&dev->v4l2_dev,
  16283. + "Failed to enable capture port - error %d. "
  16284. + "Disabling camera port again\n", ret);
  16285. +
  16286. + vchiq_mmal_port_disable(dev->instance,
  16287. + dev->capture.camera_port);
  16288. + if (disable_camera(dev) < 0) {
  16289. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  16290. + return -EINVAL;
  16291. + }
  16292. + return -1;
  16293. + }
  16294. +
  16295. + /* capture the first frame */
  16296. + vchiq_mmal_port_parameter_set(dev->instance,
  16297. + dev->capture.camera_port,
  16298. + MMAL_PARAMETER_CAPTURE,
  16299. + &dev->capture.frame_count,
  16300. + sizeof(dev->capture.frame_count));
  16301. + return 0;
  16302. +}
  16303. +
  16304. +/* abort streaming and wait for last buffer */
  16305. +static int stop_streaming(struct vb2_queue *vq)
  16306. +{
  16307. + int ret;
  16308. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  16309. +
  16310. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "%s: dev:%p\n",
  16311. + __func__, dev);
  16312. +
  16313. + init_completion(&dev->capture.frame_cmplt);
  16314. + dev->capture.frame_count = 0;
  16315. +
  16316. + /* ensure a format has actually been set */
  16317. + if (dev->capture.port == NULL)
  16318. + return -EINVAL;
  16319. +
  16320. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "stopping capturing\n");
  16321. +
  16322. + /* stop capturing frames */
  16323. + vchiq_mmal_port_parameter_set(dev->instance,
  16324. + dev->capture.camera_port,
  16325. + MMAL_PARAMETER_CAPTURE,
  16326. + &dev->capture.frame_count,
  16327. + sizeof(dev->capture.frame_count));
  16328. +
  16329. + /* wait for last frame to complete */
  16330. + ret = wait_for_completion_timeout(&dev->capture.frame_cmplt, HZ);
  16331. + if (ret <= 0)
  16332. + v4l2_err(&dev->v4l2_dev,
  16333. + "error %d waiting for frame completion\n", ret);
  16334. +
  16335. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16336. + "disabling connection\n");
  16337. +
  16338. + /* disable the connection from camera to encoder */
  16339. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.camera_port);
  16340. + if (!ret && dev->capture.camera_port != dev->capture.port) {
  16341. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16342. + "disabling port\n");
  16343. + ret = vchiq_mmal_port_disable(dev->instance, dev->capture.port);
  16344. + } else if (dev->capture.camera_port != dev->capture.port) {
  16345. + v4l2_err(&dev->v4l2_dev, "port_disable failed, error %d\n",
  16346. + ret);
  16347. + }
  16348. +
  16349. + if (disable_camera(dev) < 0) {
  16350. + v4l2_err(&dev->v4l2_dev, "Failed to disable camera");
  16351. + return -EINVAL;
  16352. + }
  16353. +
  16354. + return ret;
  16355. +}
  16356. +
  16357. +static void bm2835_mmal_lock(struct vb2_queue *vq)
  16358. +{
  16359. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  16360. + mutex_lock(&dev->mutex);
  16361. +}
  16362. +
  16363. +static void bm2835_mmal_unlock(struct vb2_queue *vq)
  16364. +{
  16365. + struct bm2835_mmal_dev *dev = vb2_get_drv_priv(vq);
  16366. + mutex_unlock(&dev->mutex);
  16367. +}
  16368. +
  16369. +static struct vb2_ops bm2835_mmal_video_qops = {
  16370. + .queue_setup = queue_setup,
  16371. + .buf_prepare = buffer_prepare,
  16372. + .buf_queue = buffer_queue,
  16373. + .start_streaming = start_streaming,
  16374. + .stop_streaming = stop_streaming,
  16375. + .wait_prepare = bm2835_mmal_unlock,
  16376. + .wait_finish = bm2835_mmal_lock,
  16377. +};
  16378. +
  16379. +/* ------------------------------------------------------------------
  16380. + IOCTL operations
  16381. + ------------------------------------------------------------------*/
  16382. +
  16383. +/* overlay ioctl */
  16384. +static int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
  16385. + struct v4l2_fmtdesc *f)
  16386. +{
  16387. + struct mmal_fmt *fmt;
  16388. +
  16389. + if (f->index >= ARRAY_SIZE(formats))
  16390. + return -EINVAL;
  16391. +
  16392. + fmt = &formats[f->index];
  16393. +
  16394. + strlcpy(f->description, fmt->name, sizeof(f->description));
  16395. + f->pixelformat = fmt->fourcc;
  16396. + f->flags = fmt->flags;
  16397. +
  16398. + return 0;
  16399. +}
  16400. +
  16401. +static int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
  16402. + struct v4l2_format *f)
  16403. +{
  16404. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16405. +
  16406. + f->fmt.win = dev->overlay;
  16407. +
  16408. + return 0;
  16409. +}
  16410. +
  16411. +static int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
  16412. + struct v4l2_format *f)
  16413. +{
  16414. + /* Only support one format so get the current one. */
  16415. + vidioc_g_fmt_vid_overlay(file, priv, f);
  16416. +
  16417. + /* todo: allow the size and/or offset to be changed. */
  16418. + return 0;
  16419. +}
  16420. +
  16421. +static int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
  16422. + struct v4l2_format *f)
  16423. +{
  16424. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16425. +
  16426. + vidioc_try_fmt_vid_overlay(file, priv, f);
  16427. +
  16428. + dev->overlay = f->fmt.win;
  16429. +
  16430. + /* todo: program the preview port parameters */
  16431. + return 0;
  16432. +}
  16433. +
  16434. +static int vidioc_overlay(struct file *file, void *f, unsigned int on)
  16435. +{
  16436. + int ret;
  16437. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16438. + struct vchiq_mmal_port *src;
  16439. + struct vchiq_mmal_port *dst;
  16440. + struct mmal_parameter_displayregion prev_config = {
  16441. + .set = MMAL_DISPLAY_SET_LAYER | MMAL_DISPLAY_SET_ALPHA |
  16442. + MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN,
  16443. + .layer = PREVIEW_LAYER,
  16444. + .alpha = 255,
  16445. + .fullscreen = 0,
  16446. + .dest_rect = {
  16447. + .x = dev->overlay.w.left,
  16448. + .y = dev->overlay.w.top,
  16449. + .width = dev->overlay.w.width,
  16450. + .height = dev->overlay.w.height,
  16451. + },
  16452. + };
  16453. +
  16454. + if ((on && dev->component[MMAL_COMPONENT_PREVIEW]->enabled) ||
  16455. + (!on && !dev->component[MMAL_COMPONENT_PREVIEW]->enabled))
  16456. + return 0; /* already in requested state */
  16457. +
  16458. + src =
  16459. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16460. + output[MMAL_CAMERA_PORT_PREVIEW];
  16461. +
  16462. + if (!on) {
  16463. + /* disconnect preview ports and disable component */
  16464. + ret = vchiq_mmal_port_disable(dev->instance, src);
  16465. + if (!ret)
  16466. + ret =
  16467. + vchiq_mmal_port_connect_tunnel(dev->instance, src,
  16468. + NULL);
  16469. + if (ret >= 0)
  16470. + ret = vchiq_mmal_component_disable(
  16471. + dev->instance,
  16472. + dev->component[MMAL_COMPONENT_PREVIEW]);
  16473. +
  16474. + disable_camera(dev);
  16475. + return ret;
  16476. + }
  16477. +
  16478. + /* set preview port format and connect it to output */
  16479. + dst = &dev->component[MMAL_COMPONENT_PREVIEW]->input[0];
  16480. +
  16481. + ret = vchiq_mmal_port_set_format(dev->instance, src);
  16482. + if (ret < 0)
  16483. + goto error;
  16484. +
  16485. + ret = vchiq_mmal_port_parameter_set(dev->instance, dst,
  16486. + MMAL_PARAMETER_DISPLAYREGION,
  16487. + &prev_config, sizeof(prev_config));
  16488. + if (ret < 0)
  16489. + goto error;
  16490. +
  16491. + if (enable_camera(dev) < 0)
  16492. + goto error;
  16493. +
  16494. + ret = vchiq_mmal_component_enable(
  16495. + dev->instance,
  16496. + dev->component[MMAL_COMPONENT_PREVIEW]);
  16497. + if (ret < 0)
  16498. + goto error;
  16499. +
  16500. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev, "connecting %p to %p\n",
  16501. + src, dst);
  16502. + ret = vchiq_mmal_port_connect_tunnel(dev->instance, src, dst);
  16503. + if (!ret)
  16504. + ret = vchiq_mmal_port_enable(dev->instance, src, NULL);
  16505. +error:
  16506. + return ret;
  16507. +}
  16508. +
  16509. +static int vidioc_g_fbuf(struct file *file, void *fh,
  16510. + struct v4l2_framebuffer *a)
  16511. +{
  16512. + /* The video overlay must stay within the framebuffer and can't be
  16513. + positioned independently. */
  16514. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16515. + struct vchiq_mmal_port *preview_port =
  16516. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16517. + output[MMAL_CAMERA_PORT_PREVIEW];
  16518. + a->flags = V4L2_FBUF_FLAG_OVERLAY;
  16519. + a->fmt.width = preview_port->es.video.width;
  16520. + a->fmt.height = preview_port->es.video.height;
  16521. + a->fmt.pixelformat = V4L2_PIX_FMT_YUV420;
  16522. + a->fmt.bytesperline = (preview_port->es.video.width * 3)>>1;
  16523. + a->fmt.sizeimage = (preview_port->es.video.width *
  16524. + preview_port->es.video.height * 3)>>1;
  16525. + a->fmt.colorspace = V4L2_COLORSPACE_JPEG;
  16526. +
  16527. + return 0;
  16528. +}
  16529. +
  16530. +/* input ioctls */
  16531. +static int vidioc_enum_input(struct file *file, void *priv,
  16532. + struct v4l2_input *inp)
  16533. +{
  16534. + /* only a single camera input */
  16535. + if (inp->index != 0)
  16536. + return -EINVAL;
  16537. +
  16538. + inp->type = V4L2_INPUT_TYPE_CAMERA;
  16539. + sprintf(inp->name, "Camera %u", inp->index);
  16540. + return 0;
  16541. +}
  16542. +
  16543. +static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  16544. +{
  16545. + *i = 0;
  16546. + return 0;
  16547. +}
  16548. +
  16549. +static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  16550. +{
  16551. + if (i != 0)
  16552. + return -EINVAL;
  16553. +
  16554. + return 0;
  16555. +}
  16556. +
  16557. +/* capture ioctls */
  16558. +static int vidioc_querycap(struct file *file, void *priv,
  16559. + struct v4l2_capability *cap)
  16560. +{
  16561. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16562. + u32 major;
  16563. + u32 minor;
  16564. +
  16565. + vchiq_mmal_version(dev->instance, &major, &minor);
  16566. +
  16567. + strcpy(cap->driver, "bm2835 mmal");
  16568. + snprintf(cap->card, sizeof(cap->card), "mmal service %d.%d",
  16569. + major, minor);
  16570. +
  16571. + snprintf(cap->bus_info, sizeof(cap->bus_info),
  16572. + "platform:%s", dev->v4l2_dev.name);
  16573. + cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
  16574. + V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
  16575. + cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  16576. +
  16577. + return 0;
  16578. +}
  16579. +
  16580. +static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  16581. + struct v4l2_fmtdesc *f)
  16582. +{
  16583. + struct mmal_fmt *fmt;
  16584. +
  16585. + if (f->index >= ARRAY_SIZE(formats))
  16586. + return -EINVAL;
  16587. +
  16588. + fmt = &formats[f->index];
  16589. +
  16590. + strlcpy(f->description, fmt->name, sizeof(f->description));
  16591. + f->pixelformat = fmt->fourcc;
  16592. + f->flags = fmt->flags;
  16593. +
  16594. + return 0;
  16595. +}
  16596. +
  16597. +static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  16598. + struct v4l2_format *f)
  16599. +{
  16600. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16601. +
  16602. + f->fmt.pix.width = dev->capture.width;
  16603. + f->fmt.pix.height = dev->capture.height;
  16604. + f->fmt.pix.field = V4L2_FIELD_NONE;
  16605. + f->fmt.pix.pixelformat = dev->capture.fmt->fourcc;
  16606. + f->fmt.pix.bytesperline = dev->capture.stride;
  16607. + f->fmt.pix.sizeimage = dev->capture.buffersize;
  16608. +
  16609. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
  16610. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  16611. + else
  16612. + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  16613. + f->fmt.pix.priv = 0;
  16614. +
  16615. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  16616. + __func__);
  16617. + return 0;
  16618. +}
  16619. +
  16620. +static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  16621. + struct v4l2_format *f)
  16622. +{
  16623. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16624. + struct mmal_fmt *mfmt;
  16625. +
  16626. + mfmt = get_format(f);
  16627. + if (!mfmt) {
  16628. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16629. + "Fourcc format (0x%08x) unknown.\n",
  16630. + f->fmt.pix.pixelformat);
  16631. + f->fmt.pix.pixelformat = formats[0].fourcc;
  16632. + mfmt = get_format(f);
  16633. + }
  16634. +
  16635. + f->fmt.pix.field = V4L2_FIELD_NONE;
  16636. +
  16637. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16638. + "Clipping/aligning %dx%d format %08X\n",
  16639. + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
  16640. +
  16641. + v4l_bound_align_image(&f->fmt.pix.width, MIN_WIDTH, MAX_WIDTH, 1,
  16642. + &f->fmt.pix.height, MIN_HEIGHT, MAX_HEIGHT, 1, 0);
  16643. + f->fmt.pix.bytesperline = (f->fmt.pix.width * mfmt->depth)>>3;
  16644. +
  16645. + /* Image buffer has to be padded to allow for alignment, even though
  16646. + * we then remove that padding before delivering the buffer.
  16647. + */
  16648. + f->fmt.pix.sizeimage = ((f->fmt.pix.height+15)&~15) *
  16649. + (((f->fmt.pix.width+31)&~31) * mfmt->depth) >> 3;
  16650. +
  16651. + if ((mfmt->flags & V4L2_FMT_FLAG_COMPRESSED) &&
  16652. + f->fmt.pix.sizeimage < MIN_BUFFER_SIZE)
  16653. + f->fmt.pix.sizeimage = MIN_BUFFER_SIZE;
  16654. +
  16655. + if (dev->capture.fmt->fourcc == V4L2_PIX_FMT_RGB24)
  16656. + f->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  16657. + else
  16658. + f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  16659. + f->fmt.pix.priv = 0;
  16660. +
  16661. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16662. + "Now %dx%d format %08X\n",
  16663. + f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.pixelformat);
  16664. +
  16665. + v4l2_dump_pix_format(1, bcm2835_v4l2_debug, &dev->v4l2_dev, &f->fmt.pix,
  16666. + __func__);
  16667. + return 0;
  16668. +}
  16669. +
  16670. +static int mmal_setup_components(struct bm2835_mmal_dev *dev,
  16671. + struct v4l2_format *f)
  16672. +{
  16673. + int ret;
  16674. + struct vchiq_mmal_port *port = NULL, *camera_port = NULL;
  16675. + struct vchiq_mmal_component *encode_component = NULL;
  16676. + struct mmal_fmt *mfmt = get_format(f);
  16677. +
  16678. + BUG_ON(!mfmt);
  16679. +
  16680. + if (dev->capture.encode_component) {
  16681. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16682. + "vid_cap - disconnect previous tunnel\n");
  16683. +
  16684. + /* Disconnect any previous connection */
  16685. + vchiq_mmal_port_connect_tunnel(dev->instance,
  16686. + dev->capture.camera_port, NULL);
  16687. + dev->capture.camera_port = NULL;
  16688. + ret = vchiq_mmal_component_disable(dev->instance,
  16689. + dev->capture.
  16690. + encode_component);
  16691. + if (ret)
  16692. + v4l2_err(&dev->v4l2_dev,
  16693. + "Failed to disable encode component %d\n",
  16694. + ret);
  16695. +
  16696. + dev->capture.encode_component = NULL;
  16697. + }
  16698. + /* format dependant port setup */
  16699. + switch (mfmt->mmal_component) {
  16700. + case MMAL_COMPONENT_CAMERA:
  16701. + /* Make a further decision on port based on resolution */
  16702. + if (f->fmt.pix.width <= max_video_width
  16703. + && f->fmt.pix.height <= max_video_height)
  16704. + camera_port = port =
  16705. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16706. + output[MMAL_CAMERA_PORT_VIDEO];
  16707. + else
  16708. + camera_port = port =
  16709. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16710. + output[MMAL_CAMERA_PORT_CAPTURE];
  16711. + break;
  16712. + case MMAL_COMPONENT_IMAGE_ENCODE:
  16713. + encode_component = dev->component[MMAL_COMPONENT_IMAGE_ENCODE];
  16714. + port = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  16715. + camera_port =
  16716. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16717. + output[MMAL_CAMERA_PORT_CAPTURE];
  16718. + break;
  16719. + case MMAL_COMPONENT_VIDEO_ENCODE:
  16720. + encode_component = dev->component[MMAL_COMPONENT_VIDEO_ENCODE];
  16721. + port = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  16722. + camera_port =
  16723. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16724. + output[MMAL_CAMERA_PORT_VIDEO];
  16725. + break;
  16726. + default:
  16727. + break;
  16728. + }
  16729. +
  16730. + if (!port)
  16731. + return -EINVAL;
  16732. +
  16733. + if (encode_component)
  16734. + camera_port->format.encoding = MMAL_ENCODING_OPAQUE;
  16735. + else
  16736. + camera_port->format.encoding = mfmt->mmal;
  16737. +
  16738. + camera_port->format.encoding_variant = 0;
  16739. + camera_port->es.video.width = f->fmt.pix.width;
  16740. + camera_port->es.video.height = f->fmt.pix.height;
  16741. + camera_port->es.video.crop.x = 0;
  16742. + camera_port->es.video.crop.y = 0;
  16743. + camera_port->es.video.crop.width = f->fmt.pix.width;
  16744. + camera_port->es.video.crop.height = f->fmt.pix.height;
  16745. + camera_port->es.video.frame_rate.num = 0;
  16746. + camera_port->es.video.frame_rate.den = 1;
  16747. + camera_port->es.video.color_space = MMAL_COLOR_SPACE_JPEG_JFIF;
  16748. +
  16749. + ret = vchiq_mmal_port_set_format(dev->instance, camera_port);
  16750. +
  16751. + if (!ret
  16752. + && camera_port ==
  16753. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16754. + output[MMAL_CAMERA_PORT_VIDEO]) {
  16755. + bool overlay_enabled =
  16756. + !!dev->component[MMAL_COMPONENT_PREVIEW]->enabled;
  16757. + struct vchiq_mmal_port *preview_port =
  16758. + &dev->component[MMAL_COMPONENT_CAMERA]->
  16759. + output[MMAL_CAMERA_PORT_PREVIEW];
  16760. + /* Preview and encode ports need to match on resolution */
  16761. + if (overlay_enabled) {
  16762. + /* Need to disable the overlay before we can update
  16763. + * the resolution
  16764. + */
  16765. + ret =
  16766. + vchiq_mmal_port_disable(dev->instance,
  16767. + preview_port);
  16768. + if (!ret)
  16769. + ret =
  16770. + vchiq_mmal_port_connect_tunnel(
  16771. + dev->instance,
  16772. + preview_port,
  16773. + NULL);
  16774. + }
  16775. + preview_port->es.video.width = f->fmt.pix.width;
  16776. + preview_port->es.video.height = f->fmt.pix.height;
  16777. + preview_port->es.video.crop.x = 0;
  16778. + preview_port->es.video.crop.y = 0;
  16779. + preview_port->es.video.crop.width = f->fmt.pix.width;
  16780. + preview_port->es.video.crop.height = f->fmt.pix.height;
  16781. + preview_port->es.video.frame_rate.num =
  16782. + dev->capture.timeperframe.denominator;
  16783. + preview_port->es.video.frame_rate.den =
  16784. + dev->capture.timeperframe.numerator;
  16785. + ret = vchiq_mmal_port_set_format(dev->instance, preview_port);
  16786. + if (overlay_enabled) {
  16787. + ret = vchiq_mmal_port_connect_tunnel(
  16788. + dev->instance,
  16789. + preview_port,
  16790. + &dev->component[MMAL_COMPONENT_PREVIEW]->input[0]);
  16791. + if (!ret)
  16792. + ret = vchiq_mmal_port_enable(dev->instance,
  16793. + preview_port,
  16794. + NULL);
  16795. + }
  16796. + }
  16797. +
  16798. + if (ret) {
  16799. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16800. + "%s failed to set format %dx%d %08X\n", __func__,
  16801. + f->fmt.pix.width, f->fmt.pix.height,
  16802. + f->fmt.pix.pixelformat);
  16803. + /* ensure capture is not going to be tried */
  16804. + dev->capture.port = NULL;
  16805. + } else {
  16806. + if (encode_component) {
  16807. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16808. + "vid_cap - set up encode comp\n");
  16809. +
  16810. + /* configure buffering */
  16811. + camera_port->current_buffer.size =
  16812. + camera_port->recommended_buffer.size;
  16813. + camera_port->current_buffer.num =
  16814. + camera_port->recommended_buffer.num;
  16815. +
  16816. + ret =
  16817. + vchiq_mmal_port_connect_tunnel(
  16818. + dev->instance,
  16819. + camera_port,
  16820. + &encode_component->input[0]);
  16821. + if (ret) {
  16822. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16823. + &dev->v4l2_dev,
  16824. + "%s failed to create connection\n",
  16825. + __func__);
  16826. + /* ensure capture is not going to be tried */
  16827. + dev->capture.port = NULL;
  16828. + } else {
  16829. + port->es.video.width = f->fmt.pix.width;
  16830. + port->es.video.height = f->fmt.pix.height;
  16831. + port->es.video.crop.x = 0;
  16832. + port->es.video.crop.y = 0;
  16833. + port->es.video.crop.width = f->fmt.pix.width;
  16834. + port->es.video.crop.height = f->fmt.pix.height;
  16835. + port->es.video.frame_rate.num =
  16836. + dev->capture.timeperframe.denominator;
  16837. + port->es.video.frame_rate.den =
  16838. + dev->capture.timeperframe.numerator;
  16839. +
  16840. + port->format.encoding = mfmt->mmal;
  16841. + port->format.encoding_variant = 0;
  16842. + /* Set any encoding specific parameters */
  16843. + switch (mfmt->mmal_component) {
  16844. + case MMAL_COMPONENT_VIDEO_ENCODE:
  16845. + port->format.bitrate =
  16846. + dev->capture.encode_bitrate;
  16847. + break;
  16848. + case MMAL_COMPONENT_IMAGE_ENCODE:
  16849. + /* Could set EXIF parameters here */
  16850. + break;
  16851. + default:
  16852. + break;
  16853. + }
  16854. + ret = vchiq_mmal_port_set_format(dev->instance,
  16855. + port);
  16856. + if (ret)
  16857. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16858. + &dev->v4l2_dev,
  16859. + "%s failed to set format %dx%d fmt %08X\n",
  16860. + __func__,
  16861. + f->fmt.pix.width,
  16862. + f->fmt.pix.height,
  16863. + f->fmt.pix.pixelformat
  16864. + );
  16865. + }
  16866. +
  16867. + if (!ret) {
  16868. + ret = vchiq_mmal_component_enable(
  16869. + dev->instance,
  16870. + encode_component);
  16871. + if (ret) {
  16872. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16873. + &dev->v4l2_dev,
  16874. + "%s Failed to enable encode components\n",
  16875. + __func__);
  16876. + }
  16877. + }
  16878. + if (!ret) {
  16879. + /* configure buffering */
  16880. + port->current_buffer.num = 1;
  16881. + port->current_buffer.size =
  16882. + f->fmt.pix.sizeimage;
  16883. + if (port->format.encoding ==
  16884. + MMAL_ENCODING_JPEG) {
  16885. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16886. + &dev->v4l2_dev,
  16887. + "JPG - buf size now %d was %d\n",
  16888. + f->fmt.pix.sizeimage,
  16889. + port->current_buffer.size);
  16890. + port->current_buffer.size =
  16891. + (f->fmt.pix.sizeimage <
  16892. + (100 << 10))
  16893. + ? (100 << 10) : f->fmt.pix.
  16894. + sizeimage;
  16895. + }
  16896. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16897. + &dev->v4l2_dev,
  16898. + "vid_cap - cur_buf.size set to %d\n",
  16899. + f->fmt.pix.sizeimage);
  16900. + port->current_buffer.alignment = 0;
  16901. + }
  16902. + } else {
  16903. + /* configure buffering */
  16904. + camera_port->current_buffer.num = 1;
  16905. + camera_port->current_buffer.size = f->fmt.pix.sizeimage;
  16906. + camera_port->current_buffer.alignment = 0;
  16907. + }
  16908. +
  16909. + if (!ret) {
  16910. + dev->capture.fmt = mfmt;
  16911. + dev->capture.stride = f->fmt.pix.bytesperline;
  16912. + dev->capture.width = camera_port->es.video.crop.width;
  16913. + dev->capture.height = camera_port->es.video.crop.height;
  16914. + dev->capture.buffersize = port->current_buffer.size;
  16915. +
  16916. + /* select port for capture */
  16917. + dev->capture.port = port;
  16918. + dev->capture.camera_port = camera_port;
  16919. + dev->capture.encode_component = encode_component;
  16920. + v4l2_dbg(1, bcm2835_v4l2_debug,
  16921. + &dev->v4l2_dev,
  16922. + "Set dev->capture.fmt %08X, %dx%d, stride %d, size %d",
  16923. + port->format.encoding,
  16924. + dev->capture.width, dev->capture.height,
  16925. + dev->capture.stride, dev->capture.buffersize);
  16926. + }
  16927. + }
  16928. +
  16929. + /* todo: Need to convert the vchiq/mmal error into a v4l2 error. */
  16930. + return ret;
  16931. +}
  16932. +
  16933. +static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  16934. + struct v4l2_format *f)
  16935. +{
  16936. + int ret;
  16937. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  16938. + struct mmal_fmt *mfmt;
  16939. +
  16940. + /* try the format to set valid parameters */
  16941. + ret = vidioc_try_fmt_vid_cap(file, priv, f);
  16942. + if (ret) {
  16943. + v4l2_err(&dev->v4l2_dev,
  16944. + "vid_cap - vidioc_try_fmt_vid_cap failed\n");
  16945. + return ret;
  16946. + }
  16947. +
  16948. + /* if a capture is running refuse to set format */
  16949. + if (vb2_is_busy(&dev->capture.vb_vidq)) {
  16950. + v4l2_info(&dev->v4l2_dev, "%s device busy\n", __func__);
  16951. + return -EBUSY;
  16952. + }
  16953. +
  16954. + /* If the format is unsupported v4l2 says we should switch to
  16955. + * a supported one and not return an error. */
  16956. + mfmt = get_format(f);
  16957. + if (!mfmt) {
  16958. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  16959. + "Fourcc format (0x%08x) unknown.\n",
  16960. + f->fmt.pix.pixelformat);
  16961. + f->fmt.pix.pixelformat = formats[0].fourcc;
  16962. + mfmt = get_format(f);
  16963. + }
  16964. +
  16965. + ret = mmal_setup_components(dev, f);
  16966. + if (ret != 0) {
  16967. + v4l2_err(&dev->v4l2_dev,
  16968. + "%s: failed to setup mmal components: %d\n",
  16969. + __func__, ret);
  16970. + ret = -EINVAL;
  16971. + }
  16972. +
  16973. + return ret;
  16974. +}
  16975. +
  16976. +int vidioc_enum_framesizes(struct file *file, void *fh,
  16977. + struct v4l2_frmsizeenum *fsize)
  16978. +{
  16979. + static const struct v4l2_frmsize_stepwise sizes = {
  16980. + MIN_WIDTH, MAX_WIDTH, 2,
  16981. + MIN_HEIGHT, MAX_HEIGHT, 2
  16982. + };
  16983. + int i;
  16984. +
  16985. + if (fsize->index)
  16986. + return -EINVAL;
  16987. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  16988. + if (formats[i].fourcc == fsize->pixel_format)
  16989. + break;
  16990. + if (i == ARRAY_SIZE(formats))
  16991. + return -EINVAL;
  16992. + fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  16993. + fsize->stepwise = sizes;
  16994. + return 0;
  16995. +}
  16996. +
  16997. +/* timeperframe is arbitrary and continous */
  16998. +static int vidioc_enum_frameintervals(struct file *file, void *priv,
  16999. + struct v4l2_frmivalenum *fival)
  17000. +{
  17001. + int i;
  17002. +
  17003. + if (fival->index)
  17004. + return -EINVAL;
  17005. +
  17006. + for (i = 0; i < ARRAY_SIZE(formats); i++)
  17007. + if (formats[i].fourcc == fival->pixel_format)
  17008. + break;
  17009. + if (i == ARRAY_SIZE(formats))
  17010. + return -EINVAL;
  17011. +
  17012. + /* regarding width & height - we support any within range */
  17013. + if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH ||
  17014. + fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT)
  17015. + return -EINVAL;
  17016. +
  17017. + fival->type = V4L2_FRMIVAL_TYPE_CONTINUOUS;
  17018. +
  17019. + /* fill in stepwise (step=1.0 is requred by V4L2 spec) */
  17020. + fival->stepwise.min = tpf_min;
  17021. + fival->stepwise.max = tpf_max;
  17022. + fival->stepwise.step = (struct v4l2_fract) {1, 1};
  17023. +
  17024. + return 0;
  17025. +}
  17026. +
  17027. +static int vidioc_g_parm(struct file *file, void *priv,
  17028. + struct v4l2_streamparm *parm)
  17029. +{
  17030. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17031. +
  17032. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  17033. + return -EINVAL;
  17034. +
  17035. + parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  17036. + parm->parm.capture.timeperframe = dev->capture.timeperframe;
  17037. + parm->parm.capture.readbuffers = 1;
  17038. + return 0;
  17039. +}
  17040. +
  17041. +#define FRACT_CMP(a, OP, b) \
  17042. + ((u64)(a).numerator * (b).denominator OP \
  17043. + (u64)(b).numerator * (a).denominator)
  17044. +
  17045. +static int vidioc_s_parm(struct file *file, void *priv,
  17046. + struct v4l2_streamparm *parm)
  17047. +{
  17048. + struct bm2835_mmal_dev *dev = video_drvdata(file);
  17049. + struct v4l2_fract tpf;
  17050. + struct mmal_parameter_rational fps_param;
  17051. +
  17052. + if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  17053. + return -EINVAL;
  17054. +
  17055. + tpf = parm->parm.capture.timeperframe;
  17056. +
  17057. + /* tpf: {*, 0} resets timing; clip to [min, max]*/
  17058. + tpf = tpf.denominator ? tpf : tpf_default;
  17059. + tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
  17060. + tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
  17061. +
  17062. + dev->capture.timeperframe = tpf;
  17063. + parm->parm.capture.timeperframe = tpf;
  17064. + parm->parm.capture.readbuffers = 1;
  17065. +
  17066. + fps_param.num = 0; /* Select variable fps, and then use
  17067. + * FPS_RANGE to select the actual limits.
  17068. + */
  17069. + fps_param.den = 1;
  17070. + set_framerate_params(dev);
  17071. +
  17072. + return 0;
  17073. +}
  17074. +
  17075. +static const struct v4l2_ioctl_ops camera0_ioctl_ops = {
  17076. + /* overlay */
  17077. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  17078. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  17079. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  17080. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  17081. + .vidioc_overlay = vidioc_overlay,
  17082. + .vidioc_g_fbuf = vidioc_g_fbuf,
  17083. +
  17084. + /* inputs */
  17085. + .vidioc_enum_input = vidioc_enum_input,
  17086. + .vidioc_g_input = vidioc_g_input,
  17087. + .vidioc_s_input = vidioc_s_input,
  17088. +
  17089. + /* capture */
  17090. + .vidioc_querycap = vidioc_querycap,
  17091. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  17092. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  17093. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  17094. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  17095. +
  17096. + /* buffer management */
  17097. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  17098. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  17099. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  17100. + .vidioc_querybuf = vb2_ioctl_querybuf,
  17101. + .vidioc_qbuf = vb2_ioctl_qbuf,
  17102. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  17103. + .vidioc_enum_framesizes = vidioc_enum_framesizes,
  17104. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  17105. + .vidioc_g_parm = vidioc_g_parm,
  17106. + .vidioc_s_parm = vidioc_s_parm,
  17107. + .vidioc_streamon = vb2_ioctl_streamon,
  17108. + .vidioc_streamoff = vb2_ioctl_streamoff,
  17109. +
  17110. + .vidioc_log_status = v4l2_ctrl_log_status,
  17111. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  17112. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  17113. +};
  17114. +
  17115. +static const struct v4l2_ioctl_ops camera0_ioctl_ops_gstreamer = {
  17116. + /* overlay */
  17117. + .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
  17118. + .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
  17119. + .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
  17120. + .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
  17121. + .vidioc_overlay = vidioc_overlay,
  17122. + .vidioc_g_fbuf = vidioc_g_fbuf,
  17123. +
  17124. + /* inputs */
  17125. + .vidioc_enum_input = vidioc_enum_input,
  17126. + .vidioc_g_input = vidioc_g_input,
  17127. + .vidioc_s_input = vidioc_s_input,
  17128. +
  17129. + /* capture */
  17130. + .vidioc_querycap = vidioc_querycap,
  17131. + .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  17132. + .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  17133. + .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  17134. + .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  17135. +
  17136. + /* buffer management */
  17137. + .vidioc_reqbufs = vb2_ioctl_reqbufs,
  17138. + .vidioc_create_bufs = vb2_ioctl_create_bufs,
  17139. + .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  17140. + .vidioc_querybuf = vb2_ioctl_querybuf,
  17141. + .vidioc_qbuf = vb2_ioctl_qbuf,
  17142. + .vidioc_dqbuf = vb2_ioctl_dqbuf,
  17143. + /* Remove this function ptr to fix gstreamer bug
  17144. + .vidioc_enum_framesizes = vidioc_enum_framesizes, */
  17145. + .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  17146. + .vidioc_g_parm = vidioc_g_parm,
  17147. + .vidioc_s_parm = vidioc_s_parm,
  17148. + .vidioc_streamon = vb2_ioctl_streamon,
  17149. + .vidioc_streamoff = vb2_ioctl_streamoff,
  17150. +
  17151. + .vidioc_log_status = v4l2_ctrl_log_status,
  17152. + .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  17153. + .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  17154. +};
  17155. +
  17156. +/* ------------------------------------------------------------------
  17157. + Driver init/finalise
  17158. + ------------------------------------------------------------------*/
  17159. +
  17160. +static const struct v4l2_file_operations camera0_fops = {
  17161. + .owner = THIS_MODULE,
  17162. + .open = v4l2_fh_open,
  17163. + .release = vb2_fop_release,
  17164. + .read = vb2_fop_read,
  17165. + .poll = vb2_fop_poll,
  17166. + .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
  17167. + .mmap = vb2_fop_mmap,
  17168. +};
  17169. +
  17170. +static struct video_device vdev_template = {
  17171. + .name = "camera0",
  17172. + .fops = &camera0_fops,
  17173. + .ioctl_ops = &camera0_ioctl_ops,
  17174. + .release = video_device_release_empty,
  17175. +};
  17176. +
  17177. +static int set_camera_parameters(struct vchiq_mmal_instance *instance,
  17178. + struct vchiq_mmal_component *camera)
  17179. +{
  17180. + int ret;
  17181. + struct mmal_parameter_camera_config cam_config = {
  17182. + .max_stills_w = MAX_WIDTH,
  17183. + .max_stills_h = MAX_HEIGHT,
  17184. + .stills_yuv422 = 1,
  17185. + .one_shot_stills = 1,
  17186. + .max_preview_video_w = (max_video_width > 1920) ?
  17187. + max_video_width : 1920,
  17188. + .max_preview_video_h = (max_video_height > 1088) ?
  17189. + max_video_height : 1088,
  17190. + .num_preview_video_frames = 3,
  17191. + .stills_capture_circular_buffer_height = 0,
  17192. + .fast_preview_resume = 0,
  17193. + .use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RAW_STC
  17194. + };
  17195. +
  17196. + ret = vchiq_mmal_port_parameter_set(instance, &camera->control,
  17197. + MMAL_PARAMETER_CAMERA_CONFIG,
  17198. + &cam_config, sizeof(cam_config));
  17199. + return ret;
  17200. +}
  17201. +
  17202. +/* MMAL instance and component init */
  17203. +static int __init mmal_init(struct bm2835_mmal_dev *dev)
  17204. +{
  17205. + int ret;
  17206. + struct mmal_es_format *format;
  17207. + u32 bool_true = 1;
  17208. +
  17209. + ret = vchiq_mmal_init(&dev->instance);
  17210. + if (ret < 0)
  17211. + return ret;
  17212. +
  17213. + /* get the camera component ready */
  17214. + ret = vchiq_mmal_component_init(dev->instance, "ril.camera",
  17215. + &dev->component[MMAL_COMPONENT_CAMERA]);
  17216. + if (ret < 0)
  17217. + goto unreg_mmal;
  17218. +
  17219. + if (dev->component[MMAL_COMPONENT_CAMERA]->outputs <
  17220. + MMAL_CAMERA_PORT_COUNT) {
  17221. + ret = -EINVAL;
  17222. + goto unreg_camera;
  17223. + }
  17224. +
  17225. + ret = set_camera_parameters(dev->instance,
  17226. + dev->component[MMAL_COMPONENT_CAMERA]);
  17227. + if (ret < 0)
  17228. + goto unreg_camera;
  17229. +
  17230. + format =
  17231. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17232. + output[MMAL_CAMERA_PORT_PREVIEW].format;
  17233. +
  17234. + format->encoding = MMAL_ENCODING_OPAQUE;
  17235. + format->encoding_variant = MMAL_ENCODING_I420;
  17236. +
  17237. + format->es->video.width = 1024;
  17238. + format->es->video.height = 768;
  17239. + format->es->video.crop.x = 0;
  17240. + format->es->video.crop.y = 0;
  17241. + format->es->video.crop.width = 1024;
  17242. + format->es->video.crop.height = 768;
  17243. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  17244. + format->es->video.frame_rate.den = 1;
  17245. +
  17246. + format =
  17247. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17248. + output[MMAL_CAMERA_PORT_VIDEO].format;
  17249. +
  17250. + format->encoding = MMAL_ENCODING_OPAQUE;
  17251. + format->encoding_variant = MMAL_ENCODING_I420;
  17252. +
  17253. + format->es->video.width = 1024;
  17254. + format->es->video.height = 768;
  17255. + format->es->video.crop.x = 0;
  17256. + format->es->video.crop.y = 0;
  17257. + format->es->video.crop.width = 1024;
  17258. + format->es->video.crop.height = 768;
  17259. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  17260. + format->es->video.frame_rate.den = 1;
  17261. +
  17262. + vchiq_mmal_port_parameter_set(dev->instance,
  17263. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17264. + output[MMAL_CAMERA_PORT_VIDEO],
  17265. + MMAL_PARAMETER_NO_IMAGE_PADDING,
  17266. + &bool_true, sizeof(bool_true));
  17267. +
  17268. + format =
  17269. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17270. + output[MMAL_CAMERA_PORT_CAPTURE].format;
  17271. +
  17272. + format->encoding = MMAL_ENCODING_OPAQUE;
  17273. +
  17274. + format->es->video.width = 2592;
  17275. + format->es->video.height = 1944;
  17276. + format->es->video.crop.x = 0;
  17277. + format->es->video.crop.y = 0;
  17278. + format->es->video.crop.width = 2592;
  17279. + format->es->video.crop.height = 1944;
  17280. + format->es->video.frame_rate.num = 0; /* Rely on fps_range */
  17281. + format->es->video.frame_rate.den = 1;
  17282. +
  17283. + dev->capture.width = format->es->video.width;
  17284. + dev->capture.height = format->es->video.height;
  17285. + dev->capture.fmt = &formats[0];
  17286. + dev->capture.encode_component = NULL;
  17287. + dev->capture.timeperframe = tpf_default;
  17288. + dev->capture.enc_profile = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH;
  17289. + dev->capture.enc_level = V4L2_MPEG_VIDEO_H264_LEVEL_4_0;
  17290. +
  17291. + vchiq_mmal_port_parameter_set(dev->instance,
  17292. + &dev->component[MMAL_COMPONENT_CAMERA]->
  17293. + output[MMAL_CAMERA_PORT_CAPTURE],
  17294. + MMAL_PARAMETER_NO_IMAGE_PADDING,
  17295. + &bool_true, sizeof(bool_true));
  17296. +
  17297. + /* get the preview component ready */
  17298. + ret = vchiq_mmal_component_init(
  17299. + dev->instance, "ril.video_render",
  17300. + &dev->component[MMAL_COMPONENT_PREVIEW]);
  17301. + if (ret < 0)
  17302. + goto unreg_camera;
  17303. +
  17304. + if (dev->component[MMAL_COMPONENT_PREVIEW]->inputs < 1) {
  17305. + ret = -EINVAL;
  17306. + pr_debug("too few input ports %d needed %d\n",
  17307. + dev->component[MMAL_COMPONENT_PREVIEW]->inputs, 1);
  17308. + goto unreg_preview;
  17309. + }
  17310. +
  17311. + /* get the image encoder component ready */
  17312. + ret = vchiq_mmal_component_init(
  17313. + dev->instance, "ril.image_encode",
  17314. + &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  17315. + if (ret < 0)
  17316. + goto unreg_preview;
  17317. +
  17318. + if (dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs < 1) {
  17319. + ret = -EINVAL;
  17320. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  17321. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->inputs,
  17322. + 1);
  17323. + goto unreg_image_encoder;
  17324. + }
  17325. +
  17326. + /* get the video encoder component ready */
  17327. + ret = vchiq_mmal_component_init(dev->instance, "ril.video_encode",
  17328. + &dev->
  17329. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  17330. + if (ret < 0)
  17331. + goto unreg_image_encoder;
  17332. +
  17333. + if (dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs < 1) {
  17334. + ret = -EINVAL;
  17335. + v4l2_err(&dev->v4l2_dev, "too few input ports %d needed %d\n",
  17336. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->inputs,
  17337. + 1);
  17338. + goto unreg_vid_encoder;
  17339. + }
  17340. +
  17341. + {
  17342. + struct vchiq_mmal_port *encoder_port =
  17343. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  17344. + encoder_port->format.encoding = MMAL_ENCODING_H264;
  17345. + ret = vchiq_mmal_port_set_format(dev->instance,
  17346. + encoder_port);
  17347. + }
  17348. +
  17349. + {
  17350. + unsigned int enable = 1;
  17351. + vchiq_mmal_port_parameter_set(
  17352. + dev->instance,
  17353. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  17354. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  17355. + &enable, sizeof(enable));
  17356. +
  17357. + vchiq_mmal_port_parameter_set(dev->instance,
  17358. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->control,
  17359. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  17360. + &enable,
  17361. + sizeof(enable));
  17362. + }
  17363. + ret = bm2835_mmal_set_all_camera_controls(dev);
  17364. + if (ret < 0)
  17365. + goto unreg_vid_encoder;
  17366. +
  17367. + return 0;
  17368. +
  17369. +unreg_vid_encoder:
  17370. + pr_err("Cleanup: Destroy video encoder\n");
  17371. + vchiq_mmal_component_finalise(
  17372. + dev->instance,
  17373. + dev->component[MMAL_COMPONENT_VIDEO_ENCODE]);
  17374. +
  17375. +unreg_image_encoder:
  17376. + pr_err("Cleanup: Destroy image encoder\n");
  17377. + vchiq_mmal_component_finalise(
  17378. + dev->instance,
  17379. + dev->component[MMAL_COMPONENT_IMAGE_ENCODE]);
  17380. +
  17381. +unreg_preview:
  17382. + pr_err("Cleanup: Destroy video render\n");
  17383. + vchiq_mmal_component_finalise(dev->instance,
  17384. + dev->component[MMAL_COMPONENT_PREVIEW]);
  17385. +
  17386. +unreg_camera:
  17387. + pr_err("Cleanup: Destroy camera\n");
  17388. + vchiq_mmal_component_finalise(dev->instance,
  17389. + dev->component[MMAL_COMPONENT_CAMERA]);
  17390. +
  17391. +unreg_mmal:
  17392. + vchiq_mmal_finalise(dev->instance);
  17393. + return ret;
  17394. +}
  17395. +
  17396. +static int __init bm2835_mmal_init_device(struct bm2835_mmal_dev *dev,
  17397. + struct video_device *vfd)
  17398. +{
  17399. + int ret;
  17400. +
  17401. + *vfd = vdev_template;
  17402. + if (gst_v4l2src_is_broken) {
  17403. + v4l2_info(&dev->v4l2_dev,
  17404. + "Work-around for gstreamer issue is active.\n");
  17405. + vfd->ioctl_ops = &camera0_ioctl_ops_gstreamer;
  17406. + }
  17407. +
  17408. + vfd->v4l2_dev = &dev->v4l2_dev;
  17409. +
  17410. + vfd->lock = &dev->mutex;
  17411. +
  17412. + vfd->queue = &dev->capture.vb_vidq;
  17413. +
  17414. + set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  17415. +
  17416. + /* video device needs to be able to access instance data */
  17417. + video_set_drvdata(vfd, dev);
  17418. +
  17419. + ret = video_register_device(vfd, VFL_TYPE_GRABBER, -1);
  17420. + if (ret < 0)
  17421. + return ret;
  17422. +
  17423. + v4l2_info(vfd->v4l2_dev,
  17424. + "V4L2 device registered as %s - stills mode > %dx%d\n",
  17425. + video_device_node_name(vfd), max_video_width, max_video_height);
  17426. +
  17427. + return 0;
  17428. +}
  17429. +
  17430. +static struct v4l2_format default_v4l2_format = {
  17431. + .fmt.pix.pixelformat = V4L2_PIX_FMT_JPEG,
  17432. + .fmt.pix.width = 1024,
  17433. + .fmt.pix.bytesperline = 1024,
  17434. + .fmt.pix.height = 768,
  17435. + .fmt.pix.sizeimage = 1024*768,
  17436. +};
  17437. +
  17438. +static int __init bm2835_mmal_init(void)
  17439. +{
  17440. + int ret;
  17441. + struct bm2835_mmal_dev *dev;
  17442. + struct vb2_queue *q;
  17443. +
  17444. + dev = kzalloc(sizeof(*gdev), GFP_KERNEL);
  17445. + if (!dev)
  17446. + return -ENOMEM;
  17447. +
  17448. + /* setup device defaults */
  17449. + dev->overlay.w.left = 150;
  17450. + dev->overlay.w.top = 50;
  17451. + dev->overlay.w.width = 1024;
  17452. + dev->overlay.w.height = 768;
  17453. + dev->overlay.clipcount = 0;
  17454. + dev->overlay.field = V4L2_FIELD_NONE;
  17455. +
  17456. + dev->capture.fmt = &formats[3]; /* JPEG */
  17457. +
  17458. + /* v4l device registration */
  17459. + snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
  17460. + "%s", BM2835_MMAL_MODULE_NAME);
  17461. + ret = v4l2_device_register(NULL, &dev->v4l2_dev);
  17462. + if (ret)
  17463. + goto free_dev;
  17464. +
  17465. + /* setup v4l controls */
  17466. + ret = bm2835_mmal_init_controls(dev, &dev->ctrl_handler);
  17467. + if (ret < 0)
  17468. + goto unreg_dev;
  17469. + dev->v4l2_dev.ctrl_handler = &dev->ctrl_handler;
  17470. +
  17471. + /* mmal init */
  17472. + ret = mmal_init(dev);
  17473. + if (ret < 0)
  17474. + goto unreg_dev;
  17475. +
  17476. + /* initialize queue */
  17477. + q = &dev->capture.vb_vidq;
  17478. + memset(q, 0, sizeof(*q));
  17479. + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  17480. + q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  17481. + q->drv_priv = dev;
  17482. + q->buf_struct_size = sizeof(struct mmal_buffer);
  17483. + q->ops = &bm2835_mmal_video_qops;
  17484. + q->mem_ops = &vb2_vmalloc_memops;
  17485. + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  17486. + ret = vb2_queue_init(q);
  17487. + if (ret < 0)
  17488. + goto unreg_dev;
  17489. +
  17490. + /* v4l2 core mutex used to protect all fops and v4l2 ioctls. */
  17491. + mutex_init(&dev->mutex);
  17492. +
  17493. + /* initialise video devices */
  17494. + ret = bm2835_mmal_init_device(dev, &dev->vdev);
  17495. + if (ret < 0)
  17496. + goto unreg_dev;
  17497. +
  17498. + /* Really want to call vidioc_s_fmt_vid_cap with the default
  17499. + * format, but currently the APIs don't join up.
  17500. + */
  17501. + ret = mmal_setup_components(dev, &default_v4l2_format);
  17502. + if (ret < 0) {
  17503. + v4l2_err(&dev->v4l2_dev,
  17504. + "%s: could not setup components\n", __func__);
  17505. + goto unreg_dev;
  17506. + }
  17507. +
  17508. + v4l2_info(&dev->v4l2_dev,
  17509. + "Broadcom 2835 MMAL video capture ver %s loaded.\n",
  17510. + BM2835_MMAL_VERSION);
  17511. +
  17512. + gdev = dev;
  17513. + return 0;
  17514. +
  17515. +unreg_dev:
  17516. + v4l2_ctrl_handler_free(&dev->ctrl_handler);
  17517. + v4l2_device_unregister(&dev->v4l2_dev);
  17518. +
  17519. +free_dev:
  17520. + kfree(dev);
  17521. +
  17522. + v4l2_err(&dev->v4l2_dev,
  17523. + "%s: error %d while loading driver\n",
  17524. + BM2835_MMAL_MODULE_NAME, ret);
  17525. +
  17526. + return ret;
  17527. +}
  17528. +
  17529. +static void __exit bm2835_mmal_exit(void)
  17530. +{
  17531. + if (!gdev)
  17532. + return;
  17533. +
  17534. + v4l2_info(&gdev->v4l2_dev, "unregistering %s\n",
  17535. + video_device_node_name(&gdev->vdev));
  17536. +
  17537. + video_unregister_device(&gdev->vdev);
  17538. +
  17539. + if (gdev->capture.encode_component) {
  17540. + v4l2_dbg(1, bcm2835_v4l2_debug, &gdev->v4l2_dev,
  17541. + "mmal_exit - disconnect tunnel\n");
  17542. + vchiq_mmal_port_connect_tunnel(gdev->instance,
  17543. + gdev->capture.camera_port, NULL);
  17544. + vchiq_mmal_component_disable(gdev->instance,
  17545. + gdev->capture.encode_component);
  17546. + }
  17547. + vchiq_mmal_component_disable(gdev->instance,
  17548. + gdev->component[MMAL_COMPONENT_CAMERA]);
  17549. +
  17550. + vchiq_mmal_component_finalise(gdev->instance,
  17551. + gdev->
  17552. + component[MMAL_COMPONENT_VIDEO_ENCODE]);
  17553. +
  17554. + vchiq_mmal_component_finalise(gdev->instance,
  17555. + gdev->
  17556. + component[MMAL_COMPONENT_IMAGE_ENCODE]);
  17557. +
  17558. + vchiq_mmal_component_finalise(gdev->instance,
  17559. + gdev->component[MMAL_COMPONENT_PREVIEW]);
  17560. +
  17561. + vchiq_mmal_component_finalise(gdev->instance,
  17562. + gdev->component[MMAL_COMPONENT_CAMERA]);
  17563. +
  17564. + vchiq_mmal_finalise(gdev->instance);
  17565. +
  17566. + v4l2_ctrl_handler_free(&gdev->ctrl_handler);
  17567. +
  17568. + v4l2_device_unregister(&gdev->v4l2_dev);
  17569. +
  17570. + kfree(gdev);
  17571. +}
  17572. +
  17573. +module_init(bm2835_mmal_init);
  17574. +module_exit(bm2835_mmal_exit);
  17575. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/bcm2835-camera.h linux-3.16-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h
  17576. --- linux-3.16.2/drivers/media/platform/bcm2835/bcm2835-camera.h 1970-01-01 01:00:00.000000000 +0100
  17577. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/bcm2835-camera.h 2014-09-14 19:03:24.000000000 +0200
  17578. @@ -0,0 +1,126 @@
  17579. +/*
  17580. + * Broadcom BM2835 V4L2 driver
  17581. + *
  17582. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  17583. + *
  17584. + * This file is subject to the terms and conditions of the GNU General Public
  17585. + * License. See the file COPYING in the main directory of this archive
  17586. + * for more details.
  17587. + *
  17588. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  17589. + * Dave Stevenson <dsteve@broadcom.com>
  17590. + * Simon Mellor <simellor@broadcom.com>
  17591. + * Luke Diamand <luked@broadcom.com>
  17592. + *
  17593. + * core driver device
  17594. + */
  17595. +
  17596. +#define V4L2_CTRL_COUNT 28 /* number of v4l controls */
  17597. +
  17598. +enum {
  17599. + MMAL_COMPONENT_CAMERA = 0,
  17600. + MMAL_COMPONENT_PREVIEW,
  17601. + MMAL_COMPONENT_IMAGE_ENCODE,
  17602. + MMAL_COMPONENT_VIDEO_ENCODE,
  17603. + MMAL_COMPONENT_COUNT
  17604. +};
  17605. +
  17606. +enum {
  17607. + MMAL_CAMERA_PORT_PREVIEW = 0,
  17608. + MMAL_CAMERA_PORT_VIDEO,
  17609. + MMAL_CAMERA_PORT_CAPTURE,
  17610. + MMAL_CAMERA_PORT_COUNT
  17611. +};
  17612. +
  17613. +#define PREVIEW_LAYER 2
  17614. +
  17615. +extern int bcm2835_v4l2_debug;
  17616. +
  17617. +struct bm2835_mmal_dev {
  17618. + /* v4l2 devices */
  17619. + struct v4l2_device v4l2_dev;
  17620. + struct video_device vdev;
  17621. + struct mutex mutex;
  17622. +
  17623. + /* controls */
  17624. + struct v4l2_ctrl_handler ctrl_handler;
  17625. + struct v4l2_ctrl *ctrls[V4L2_CTRL_COUNT];
  17626. + enum v4l2_scene_mode scene_mode;
  17627. + struct mmal_colourfx colourfx;
  17628. + int hflip;
  17629. + int vflip;
  17630. + int red_gain;
  17631. + int blue_gain;
  17632. + enum mmal_parameter_exposuremode exposure_mode_user;
  17633. + enum v4l2_exposure_auto_type exposure_mode_v4l2_user;
  17634. + /* active exposure mode may differ if selected via a scene mode */
  17635. + enum mmal_parameter_exposuremode exposure_mode_active;
  17636. + enum mmal_parameter_exposuremeteringmode metering_mode;
  17637. + unsigned int manual_shutter_speed;
  17638. + bool exp_auto_priority;
  17639. +
  17640. + /* allocated mmal instance and components */
  17641. + struct vchiq_mmal_instance *instance;
  17642. + struct vchiq_mmal_component *component[MMAL_COMPONENT_COUNT];
  17643. + int camera_use_count;
  17644. +
  17645. + struct v4l2_window overlay;
  17646. +
  17647. + struct {
  17648. + unsigned int width; /* width */
  17649. + unsigned int height; /* height */
  17650. + unsigned int stride; /* stride */
  17651. + unsigned int buffersize; /* buffer size with padding */
  17652. + struct mmal_fmt *fmt;
  17653. + struct v4l2_fract timeperframe;
  17654. +
  17655. + /* H264 encode bitrate */
  17656. + int encode_bitrate;
  17657. + /* H264 bitrate mode. CBR/VBR */
  17658. + int encode_bitrate_mode;
  17659. + /* H264 profile */
  17660. + enum v4l2_mpeg_video_h264_profile enc_profile;
  17661. + /* H264 level */
  17662. + enum v4l2_mpeg_video_h264_level enc_level;
  17663. + /* JPEG Q-factor */
  17664. + int q_factor;
  17665. +
  17666. + struct vb2_queue vb_vidq;
  17667. +
  17668. + /* VC start timestamp for streaming */
  17669. + s64 vc_start_timestamp;
  17670. + /* Kernel start timestamp for streaming */
  17671. + struct timeval kernel_start_ts;
  17672. +
  17673. + struct vchiq_mmal_port *port; /* port being used for capture */
  17674. + /* camera port being used for capture */
  17675. + struct vchiq_mmal_port *camera_port;
  17676. + /* component being used for encode */
  17677. + struct vchiq_mmal_component *encode_component;
  17678. + /* number of frames remaining which driver should capture */
  17679. + unsigned int frame_count;
  17680. + /* last frame completion */
  17681. + struct completion frame_cmplt;
  17682. +
  17683. + } capture;
  17684. +
  17685. +};
  17686. +
  17687. +int bm2835_mmal_init_controls(
  17688. + struct bm2835_mmal_dev *dev,
  17689. + struct v4l2_ctrl_handler *hdl);
  17690. +
  17691. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev);
  17692. +int set_framerate_params(struct bm2835_mmal_dev *dev);
  17693. +
  17694. +/* Debug helpers */
  17695. +
  17696. +#define v4l2_dump_pix_format(level, debug, dev, pix_fmt, desc) \
  17697. +{ \
  17698. + v4l2_dbg(level, debug, dev, \
  17699. +"%s: w %u h %u field %u pfmt 0x%x bpl %u sz_img %u colorspace 0x%x priv %u\n", \
  17700. + desc == NULL ? "" : desc, \
  17701. + (pix_fmt)->width, (pix_fmt)->height, (pix_fmt)->field, \
  17702. + (pix_fmt)->pixelformat, (pix_fmt)->bytesperline, \
  17703. + (pix_fmt)->sizeimage, (pix_fmt)->colorspace, (pix_fmt)->priv); \
  17704. +}
  17705. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/controls.c linux-3.16-rpi/drivers/media/platform/bcm2835/controls.c
  17706. --- linux-3.16.2/drivers/media/platform/bcm2835/controls.c 1970-01-01 01:00:00.000000000 +0100
  17707. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/controls.c 2014-09-14 19:03:24.000000000 +0200
  17708. @@ -0,0 +1,1322 @@
  17709. +/*
  17710. + * Broadcom BM2835 V4L2 driver
  17711. + *
  17712. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  17713. + *
  17714. + * This file is subject to the terms and conditions of the GNU General Public
  17715. + * License. See the file COPYING in the main directory of this archive
  17716. + * for more details.
  17717. + *
  17718. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  17719. + * Dave Stevenson <dsteve@broadcom.com>
  17720. + * Simon Mellor <simellor@broadcom.com>
  17721. + * Luke Diamand <luked@broadcom.com>
  17722. + */
  17723. +
  17724. +#include <linux/errno.h>
  17725. +#include <linux/kernel.h>
  17726. +#include <linux/module.h>
  17727. +#include <linux/slab.h>
  17728. +#include <media/videobuf2-vmalloc.h>
  17729. +#include <media/v4l2-device.h>
  17730. +#include <media/v4l2-ioctl.h>
  17731. +#include <media/v4l2-ctrls.h>
  17732. +#include <media/v4l2-fh.h>
  17733. +#include <media/v4l2-event.h>
  17734. +#include <media/v4l2-common.h>
  17735. +
  17736. +#include "mmal-common.h"
  17737. +#include "mmal-vchiq.h"
  17738. +#include "mmal-parameters.h"
  17739. +#include "bcm2835-camera.h"
  17740. +
  17741. +/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
  17742. + * MMAL values are in 1/6th increments so the MMAL range is -24 to +24.
  17743. + * V4L2 docs say value "is expressed in terms of EV, drivers should interpret
  17744. + * the values as 0.001 EV units, where the value 1000 stands for +1 EV."
  17745. + * V4L2 is limited to a max of 32 values in a menu, so count in 1/3rds from
  17746. + * -4 to +4
  17747. + */
  17748. +static const s64 ev_bias_qmenu[] = {
  17749. + -4000, -3667, -3333,
  17750. + -3000, -2667, -2333,
  17751. + -2000, -1667, -1333,
  17752. + -1000, -667, -333,
  17753. + 0, 333, 667,
  17754. + 1000, 1333, 1667,
  17755. + 2000, 2333, 2667,
  17756. + 3000, 3333, 3667,
  17757. + 4000
  17758. +};
  17759. +
  17760. +/* Supported ISO values
  17761. + * ISOO = auto ISO
  17762. + */
  17763. +static const s64 iso_qmenu[] = {
  17764. + 0, 100, 200, 400, 800,
  17765. +};
  17766. +
  17767. +static const s64 mains_freq_qmenu[] = {
  17768. + V4L2_CID_POWER_LINE_FREQUENCY_DISABLED,
  17769. + V4L2_CID_POWER_LINE_FREQUENCY_50HZ,
  17770. + V4L2_CID_POWER_LINE_FREQUENCY_60HZ,
  17771. + V4L2_CID_POWER_LINE_FREQUENCY_AUTO
  17772. +};
  17773. +
  17774. +/* Supported video encode modes */
  17775. +static const s64 bitrate_mode_qmenu[] = {
  17776. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
  17777. + (s64)V4L2_MPEG_VIDEO_BITRATE_MODE_CBR,
  17778. +};
  17779. +
  17780. +enum bm2835_mmal_ctrl_type {
  17781. + MMAL_CONTROL_TYPE_STD,
  17782. + MMAL_CONTROL_TYPE_STD_MENU,
  17783. + MMAL_CONTROL_TYPE_INT_MENU,
  17784. + MMAL_CONTROL_TYPE_CLUSTER, /* special cluster entry */
  17785. +};
  17786. +
  17787. +struct bm2835_mmal_v4l2_ctrl;
  17788. +
  17789. +typedef int(bm2835_mmal_v4l2_ctrl_cb)(
  17790. + struct bm2835_mmal_dev *dev,
  17791. + struct v4l2_ctrl *ctrl,
  17792. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl);
  17793. +
  17794. +struct bm2835_mmal_v4l2_ctrl {
  17795. + u32 id; /* v4l2 control identifier */
  17796. + enum bm2835_mmal_ctrl_type type;
  17797. + /* control minimum value or
  17798. + * mask for MMAL_CONTROL_TYPE_STD_MENU */
  17799. + s32 min;
  17800. + s32 max; /* maximum value of control */
  17801. + s32 def; /* default value of control */
  17802. + s32 step; /* step size of the control */
  17803. + const s64 *imenu; /* integer menu array */
  17804. + u32 mmal_id; /* mmal parameter id */
  17805. + bm2835_mmal_v4l2_ctrl_cb *setter;
  17806. + bool ignore_errors;
  17807. +};
  17808. +
  17809. +struct v4l2_to_mmal_effects_setting {
  17810. + u32 v4l2_effect;
  17811. + u32 mmal_effect;
  17812. + s32 col_fx_enable;
  17813. + s32 col_fx_fixed_cbcr;
  17814. + u32 u;
  17815. + u32 v;
  17816. + u32 num_effect_params;
  17817. + u32 effect_params[MMAL_MAX_IMAGEFX_PARAMETERS];
  17818. +};
  17819. +
  17820. +static const struct v4l2_to_mmal_effects_setting
  17821. + v4l2_to_mmal_effects_values[] = {
  17822. + { V4L2_COLORFX_NONE, MMAL_PARAM_IMAGEFX_NONE,
  17823. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17824. + { V4L2_COLORFX_BW, MMAL_PARAM_IMAGEFX_NONE,
  17825. + 1, 0, 128, 128, 0, {0, 0, 0, 0, 0} },
  17826. + { V4L2_COLORFX_SEPIA, MMAL_PARAM_IMAGEFX_NONE,
  17827. + 1, 0, 87, 151, 0, {0, 0, 0, 0, 0} },
  17828. + { V4L2_COLORFX_NEGATIVE, MMAL_PARAM_IMAGEFX_NEGATIVE,
  17829. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17830. + { V4L2_COLORFX_EMBOSS, MMAL_PARAM_IMAGEFX_EMBOSS,
  17831. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17832. + { V4L2_COLORFX_SKETCH, MMAL_PARAM_IMAGEFX_SKETCH,
  17833. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17834. + { V4L2_COLORFX_SKY_BLUE, MMAL_PARAM_IMAGEFX_PASTEL,
  17835. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17836. + { V4L2_COLORFX_GRASS_GREEN, MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  17837. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17838. + { V4L2_COLORFX_SKIN_WHITEN, MMAL_PARAM_IMAGEFX_WASHEDOUT,
  17839. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17840. + { V4L2_COLORFX_VIVID, MMAL_PARAM_IMAGEFX_SATURATION,
  17841. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17842. + { V4L2_COLORFX_AQUA, MMAL_PARAM_IMAGEFX_NONE,
  17843. + 1, 0, 171, 121, 0, {0, 0, 0, 0, 0} },
  17844. + { V4L2_COLORFX_ART_FREEZE, MMAL_PARAM_IMAGEFX_HATCH,
  17845. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17846. + { V4L2_COLORFX_SILHOUETTE, MMAL_PARAM_IMAGEFX_FILM,
  17847. + 0, 0, 0, 0, 0, {0, 0, 0, 0, 0} },
  17848. + { V4L2_COLORFX_SOLARIZATION, MMAL_PARAM_IMAGEFX_SOLARIZE,
  17849. + 0, 0, 0, 0, 5, {1, 128, 160, 160, 48} },
  17850. + { V4L2_COLORFX_ANTIQUE, MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  17851. + 0, 0, 0, 0, 3, {108, 274, 238, 0, 0} },
  17852. + { V4L2_COLORFX_SET_CBCR, MMAL_PARAM_IMAGEFX_NONE,
  17853. + 1, 1, 0, 0, 0, {0, 0, 0, 0, 0} }
  17854. +};
  17855. +
  17856. +struct v4l2_mmal_scene_config {
  17857. + enum v4l2_scene_mode v4l2_scene;
  17858. + enum mmal_parameter_exposuremode exposure_mode;
  17859. + enum mmal_parameter_exposuremeteringmode metering_mode;
  17860. +};
  17861. +
  17862. +static const struct v4l2_mmal_scene_config scene_configs[] = {
  17863. + /* V4L2_SCENE_MODE_NONE automatically added */
  17864. + {
  17865. + V4L2_SCENE_MODE_NIGHT,
  17866. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  17867. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  17868. + },
  17869. + {
  17870. + V4L2_SCENE_MODE_SPORTS,
  17871. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  17872. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE
  17873. + },
  17874. +};
  17875. +
  17876. +/* control handlers*/
  17877. +
  17878. +static int ctrl_set_rational(struct bm2835_mmal_dev *dev,
  17879. + struct v4l2_ctrl *ctrl,
  17880. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  17881. +{
  17882. + struct mmal_parameter_rational rational_value;
  17883. + struct vchiq_mmal_port *control;
  17884. +
  17885. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  17886. +
  17887. + rational_value.num = ctrl->val;
  17888. + rational_value.den = 100;
  17889. +
  17890. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  17891. + mmal_ctrl->mmal_id,
  17892. + &rational_value,
  17893. + sizeof(rational_value));
  17894. +}
  17895. +
  17896. +static int ctrl_set_value(struct bm2835_mmal_dev *dev,
  17897. + struct v4l2_ctrl *ctrl,
  17898. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  17899. +{
  17900. + u32 u32_value;
  17901. + struct vchiq_mmal_port *control;
  17902. +
  17903. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  17904. +
  17905. + u32_value = ctrl->val;
  17906. +
  17907. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  17908. + mmal_ctrl->mmal_id,
  17909. + &u32_value, sizeof(u32_value));
  17910. +}
  17911. +
  17912. +static int ctrl_set_value_menu(struct bm2835_mmal_dev *dev,
  17913. + struct v4l2_ctrl *ctrl,
  17914. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  17915. +{
  17916. + u32 u32_value;
  17917. + struct vchiq_mmal_port *control;
  17918. +
  17919. + if (ctrl->val > mmal_ctrl->max || ctrl->val < mmal_ctrl->min)
  17920. + return 1;
  17921. +
  17922. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  17923. +
  17924. + u32_value = mmal_ctrl->imenu[ctrl->val];
  17925. +
  17926. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  17927. + mmal_ctrl->mmal_id,
  17928. + &u32_value, sizeof(u32_value));
  17929. +}
  17930. +
  17931. +static int ctrl_set_value_ev(struct bm2835_mmal_dev *dev,
  17932. + struct v4l2_ctrl *ctrl,
  17933. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  17934. +{
  17935. + s32 s32_value;
  17936. + struct vchiq_mmal_port *control;
  17937. +
  17938. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  17939. +
  17940. + s32_value = (ctrl->val-12)*2; /* Convert from index to 1/6ths */
  17941. +
  17942. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  17943. + mmal_ctrl->mmal_id,
  17944. + &s32_value, sizeof(s32_value));
  17945. +}
  17946. +
  17947. +static int ctrl_set_rotate(struct bm2835_mmal_dev *dev,
  17948. + struct v4l2_ctrl *ctrl,
  17949. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  17950. +{
  17951. + int ret;
  17952. + u32 u32_value;
  17953. + struct vchiq_mmal_component *camera;
  17954. +
  17955. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  17956. +
  17957. + u32_value = ((ctrl->val % 360) / 90) * 90;
  17958. +
  17959. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  17960. + mmal_ctrl->mmal_id,
  17961. + &u32_value, sizeof(u32_value));
  17962. + if (ret < 0)
  17963. + return ret;
  17964. +
  17965. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  17966. + mmal_ctrl->mmal_id,
  17967. + &u32_value, sizeof(u32_value));
  17968. + if (ret < 0)
  17969. + return ret;
  17970. +
  17971. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  17972. + mmal_ctrl->mmal_id,
  17973. + &u32_value, sizeof(u32_value));
  17974. +
  17975. + return ret;
  17976. +}
  17977. +
  17978. +static int ctrl_set_flip(struct bm2835_mmal_dev *dev,
  17979. + struct v4l2_ctrl *ctrl,
  17980. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  17981. +{
  17982. + int ret;
  17983. + u32 u32_value;
  17984. + struct vchiq_mmal_component *camera;
  17985. +
  17986. + if (ctrl->id == V4L2_CID_HFLIP)
  17987. + dev->hflip = ctrl->val;
  17988. + else
  17989. + dev->vflip = ctrl->val;
  17990. +
  17991. + camera = dev->component[MMAL_COMPONENT_CAMERA];
  17992. +
  17993. + if (dev->hflip && dev->vflip)
  17994. + u32_value = MMAL_PARAM_MIRROR_BOTH;
  17995. + else if (dev->hflip)
  17996. + u32_value = MMAL_PARAM_MIRROR_HORIZONTAL;
  17997. + else if (dev->vflip)
  17998. + u32_value = MMAL_PARAM_MIRROR_VERTICAL;
  17999. + else
  18000. + u32_value = MMAL_PARAM_MIRROR_NONE;
  18001. +
  18002. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[0],
  18003. + mmal_ctrl->mmal_id,
  18004. + &u32_value, sizeof(u32_value));
  18005. + if (ret < 0)
  18006. + return ret;
  18007. +
  18008. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[1],
  18009. + mmal_ctrl->mmal_id,
  18010. + &u32_value, sizeof(u32_value));
  18011. + if (ret < 0)
  18012. + return ret;
  18013. +
  18014. + ret = vchiq_mmal_port_parameter_set(dev->instance, &camera->output[2],
  18015. + mmal_ctrl->mmal_id,
  18016. + &u32_value, sizeof(u32_value));
  18017. +
  18018. + return ret;
  18019. +
  18020. +}
  18021. +
  18022. +static int ctrl_set_exposure(struct bm2835_mmal_dev *dev,
  18023. + struct v4l2_ctrl *ctrl,
  18024. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18025. +{
  18026. + enum mmal_parameter_exposuremode exp_mode = dev->exposure_mode_user;
  18027. + u32 shutter_speed = 0;
  18028. + struct vchiq_mmal_port *control;
  18029. + int ret = 0;
  18030. +
  18031. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18032. +
  18033. + if (mmal_ctrl->mmal_id == MMAL_PARAMETER_SHUTTER_SPEED) {
  18034. + /* V4L2 is in 100usec increments.
  18035. + * MMAL is 1usec.
  18036. + */
  18037. + dev->manual_shutter_speed = ctrl->val * 100;
  18038. + } else if (mmal_ctrl->mmal_id == MMAL_PARAMETER_EXPOSURE_MODE) {
  18039. + switch (ctrl->val) {
  18040. + case V4L2_EXPOSURE_AUTO:
  18041. + exp_mode = MMAL_PARAM_EXPOSUREMODE_AUTO;
  18042. + break;
  18043. +
  18044. + case V4L2_EXPOSURE_MANUAL:
  18045. + exp_mode = MMAL_PARAM_EXPOSUREMODE_OFF;
  18046. + break;
  18047. + }
  18048. + dev->exposure_mode_user = exp_mode;
  18049. + dev->exposure_mode_v4l2_user = ctrl->val;
  18050. + } else if (mmal_ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
  18051. + dev->exp_auto_priority = ctrl->val;
  18052. + }
  18053. +
  18054. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  18055. + if (exp_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  18056. + shutter_speed = dev->manual_shutter_speed;
  18057. +
  18058. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  18059. + control,
  18060. + MMAL_PARAMETER_SHUTTER_SPEED,
  18061. + &shutter_speed,
  18062. + sizeof(shutter_speed));
  18063. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  18064. + control,
  18065. + MMAL_PARAMETER_EXPOSURE_MODE,
  18066. + &exp_mode,
  18067. + sizeof(u32));
  18068. + dev->exposure_mode_active = exp_mode;
  18069. + }
  18070. + /* exposure_dynamic_framerate (V4L2_CID_EXPOSURE_AUTO_PRIORITY) should
  18071. + * always apply irrespective of scene mode.
  18072. + */
  18073. + ret += set_framerate_params(dev);
  18074. +
  18075. + return ret;
  18076. +}
  18077. +
  18078. +static int ctrl_set_metering_mode(struct bm2835_mmal_dev *dev,
  18079. + struct v4l2_ctrl *ctrl,
  18080. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18081. +{
  18082. + switch (ctrl->val) {
  18083. + case V4L2_EXPOSURE_METERING_AVERAGE:
  18084. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
  18085. + break;
  18086. +
  18087. + case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
  18088. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT;
  18089. + break;
  18090. +
  18091. + case V4L2_EXPOSURE_METERING_SPOT:
  18092. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT;
  18093. + break;
  18094. +
  18095. + /* todo matrix weighting not added to Linux API till 3.9
  18096. + case V4L2_EXPOSURE_METERING_MATRIX:
  18097. + dev->metering_mode = MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX;
  18098. + break;
  18099. + */
  18100. +
  18101. + }
  18102. +
  18103. + if (dev->scene_mode == V4L2_SCENE_MODE_NONE) {
  18104. + struct vchiq_mmal_port *control;
  18105. + u32 u32_value = dev->metering_mode;
  18106. +
  18107. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18108. +
  18109. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18110. + mmal_ctrl->mmal_id,
  18111. + &u32_value, sizeof(u32_value));
  18112. + } else
  18113. + return 0;
  18114. +}
  18115. +
  18116. +static int ctrl_set_flicker_avoidance(struct bm2835_mmal_dev *dev,
  18117. + struct v4l2_ctrl *ctrl,
  18118. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18119. +{
  18120. + u32 u32_value;
  18121. + struct vchiq_mmal_port *control;
  18122. +
  18123. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18124. +
  18125. + switch (ctrl->val) {
  18126. + case V4L2_CID_POWER_LINE_FREQUENCY_DISABLED:
  18127. + u32_value = MMAL_PARAM_FLICKERAVOID_OFF;
  18128. + break;
  18129. + case V4L2_CID_POWER_LINE_FREQUENCY_50HZ:
  18130. + u32_value = MMAL_PARAM_FLICKERAVOID_50HZ;
  18131. + break;
  18132. + case V4L2_CID_POWER_LINE_FREQUENCY_60HZ:
  18133. + u32_value = MMAL_PARAM_FLICKERAVOID_60HZ;
  18134. + break;
  18135. + case V4L2_CID_POWER_LINE_FREQUENCY_AUTO:
  18136. + u32_value = MMAL_PARAM_FLICKERAVOID_AUTO;
  18137. + break;
  18138. + }
  18139. +
  18140. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18141. + mmal_ctrl->mmal_id,
  18142. + &u32_value, sizeof(u32_value));
  18143. +}
  18144. +
  18145. +static int ctrl_set_awb_mode(struct bm2835_mmal_dev *dev,
  18146. + struct v4l2_ctrl *ctrl,
  18147. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18148. +{
  18149. + u32 u32_value;
  18150. + struct vchiq_mmal_port *control;
  18151. +
  18152. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18153. +
  18154. + switch (ctrl->val) {
  18155. + case V4L2_WHITE_BALANCE_MANUAL:
  18156. + u32_value = MMAL_PARAM_AWBMODE_OFF;
  18157. + break;
  18158. +
  18159. + case V4L2_WHITE_BALANCE_AUTO:
  18160. + u32_value = MMAL_PARAM_AWBMODE_AUTO;
  18161. + break;
  18162. +
  18163. + case V4L2_WHITE_BALANCE_INCANDESCENT:
  18164. + u32_value = MMAL_PARAM_AWBMODE_INCANDESCENT;
  18165. + break;
  18166. +
  18167. + case V4L2_WHITE_BALANCE_FLUORESCENT:
  18168. + u32_value = MMAL_PARAM_AWBMODE_FLUORESCENT;
  18169. + break;
  18170. +
  18171. + case V4L2_WHITE_BALANCE_FLUORESCENT_H:
  18172. + u32_value = MMAL_PARAM_AWBMODE_TUNGSTEN;
  18173. + break;
  18174. +
  18175. + case V4L2_WHITE_BALANCE_HORIZON:
  18176. + u32_value = MMAL_PARAM_AWBMODE_HORIZON;
  18177. + break;
  18178. +
  18179. + case V4L2_WHITE_BALANCE_DAYLIGHT:
  18180. + u32_value = MMAL_PARAM_AWBMODE_SUNLIGHT;
  18181. + break;
  18182. +
  18183. + case V4L2_WHITE_BALANCE_FLASH:
  18184. + u32_value = MMAL_PARAM_AWBMODE_FLASH;
  18185. + break;
  18186. +
  18187. + case V4L2_WHITE_BALANCE_CLOUDY:
  18188. + u32_value = MMAL_PARAM_AWBMODE_CLOUDY;
  18189. + break;
  18190. +
  18191. + case V4L2_WHITE_BALANCE_SHADE:
  18192. + u32_value = MMAL_PARAM_AWBMODE_SHADE;
  18193. + break;
  18194. +
  18195. + }
  18196. +
  18197. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18198. + mmal_ctrl->mmal_id,
  18199. + &u32_value, sizeof(u32_value));
  18200. +}
  18201. +
  18202. +static int ctrl_set_awb_gains(struct bm2835_mmal_dev *dev,
  18203. + struct v4l2_ctrl *ctrl,
  18204. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18205. +{
  18206. + struct vchiq_mmal_port *control;
  18207. + struct mmal_parameter_awbgains gains;
  18208. +
  18209. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18210. +
  18211. + if (ctrl->id == V4L2_CID_RED_BALANCE)
  18212. + dev->red_gain = ctrl->val;
  18213. + else if (ctrl->id == V4L2_CID_BLUE_BALANCE)
  18214. + dev->blue_gain = ctrl->val;
  18215. +
  18216. + gains.r_gain.num = dev->red_gain;
  18217. + gains.b_gain.num = dev->blue_gain;
  18218. + gains.r_gain.den = gains.b_gain.den = 1000;
  18219. +
  18220. + return vchiq_mmal_port_parameter_set(dev->instance, control,
  18221. + mmal_ctrl->mmal_id,
  18222. + &gains, sizeof(gains));
  18223. +}
  18224. +
  18225. +static int ctrl_set_image_effect(struct bm2835_mmal_dev *dev,
  18226. + struct v4l2_ctrl *ctrl,
  18227. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18228. +{
  18229. + int ret = -EINVAL;
  18230. + int i, j;
  18231. + struct vchiq_mmal_port *control;
  18232. + struct mmal_parameter_imagefx_parameters imagefx;
  18233. +
  18234. + for (i = 0; i < ARRAY_SIZE(v4l2_to_mmal_effects_values); i++) {
  18235. + if (ctrl->val == v4l2_to_mmal_effects_values[i].v4l2_effect) {
  18236. +
  18237. + imagefx.effect =
  18238. + v4l2_to_mmal_effects_values[i].mmal_effect;
  18239. + imagefx.num_effect_params =
  18240. + v4l2_to_mmal_effects_values[i].num_effect_params;
  18241. +
  18242. + if (imagefx.num_effect_params > MMAL_MAX_IMAGEFX_PARAMETERS)
  18243. + imagefx.num_effect_params = MMAL_MAX_IMAGEFX_PARAMETERS;
  18244. +
  18245. + for (j = 0; j < imagefx.num_effect_params; j++)
  18246. + imagefx.effect_parameter[j] =
  18247. + v4l2_to_mmal_effects_values[i].effect_params[j];
  18248. +
  18249. + dev->colourfx.enable =
  18250. + v4l2_to_mmal_effects_values[i].col_fx_enable;
  18251. + if (!v4l2_to_mmal_effects_values[i].col_fx_fixed_cbcr) {
  18252. + dev->colourfx.u =
  18253. + v4l2_to_mmal_effects_values[i].u;
  18254. + dev->colourfx.v =
  18255. + v4l2_to_mmal_effects_values[i].v;
  18256. + }
  18257. +
  18258. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18259. +
  18260. + ret = vchiq_mmal_port_parameter_set(
  18261. + dev->instance, control,
  18262. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  18263. + &imagefx, sizeof(imagefx));
  18264. + if (ret)
  18265. + goto exit;
  18266. +
  18267. + ret = vchiq_mmal_port_parameter_set(
  18268. + dev->instance, control,
  18269. + MMAL_PARAMETER_COLOUR_EFFECT,
  18270. + &dev->colourfx, sizeof(dev->colourfx));
  18271. + }
  18272. + }
  18273. +
  18274. +exit:
  18275. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18276. + "mmal_ctrl:%p ctrl id:0x%x ctrl val:%d imagefx:0x%x color_effect:%s u:%d v:%d ret %d(%d)\n",
  18277. + mmal_ctrl, ctrl->id, ctrl->val, imagefx.effect,
  18278. + dev->colourfx.enable ? "true" : "false",
  18279. + dev->colourfx.u, dev->colourfx.v,
  18280. + ret, (ret == 0 ? 0 : -EINVAL));
  18281. + return (ret == 0 ? 0 : EINVAL);
  18282. +}
  18283. +
  18284. +static int ctrl_set_colfx(struct bm2835_mmal_dev *dev,
  18285. + struct v4l2_ctrl *ctrl,
  18286. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18287. +{
  18288. + int ret = -EINVAL;
  18289. + struct vchiq_mmal_port *control;
  18290. +
  18291. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18292. +
  18293. + dev->colourfx.enable = (ctrl->val & 0xff00) >> 8;
  18294. + dev->colourfx.enable = ctrl->val & 0xff;
  18295. +
  18296. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  18297. + MMAL_PARAMETER_COLOUR_EFFECT,
  18298. + &dev->colourfx, sizeof(dev->colourfx));
  18299. +
  18300. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18301. + "%s: After: mmal_ctrl:%p ctrl id:0x%x ctrl val:%d ret %d(%d)\n",
  18302. + __func__, mmal_ctrl, ctrl->id, ctrl->val, ret,
  18303. + (ret == 0 ? 0 : -EINVAL));
  18304. + return (ret == 0 ? 0 : EINVAL);
  18305. +}
  18306. +
  18307. +static int ctrl_set_bitrate(struct bm2835_mmal_dev *dev,
  18308. + struct v4l2_ctrl *ctrl,
  18309. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18310. +{
  18311. + int ret;
  18312. + struct vchiq_mmal_port *encoder_out;
  18313. +
  18314. + dev->capture.encode_bitrate = ctrl->val;
  18315. +
  18316. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  18317. +
  18318. + ret = vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  18319. + mmal_ctrl->mmal_id,
  18320. + &ctrl->val, sizeof(ctrl->val));
  18321. + ret = 0;
  18322. + return ret;
  18323. +}
  18324. +
  18325. +static int ctrl_set_bitrate_mode(struct bm2835_mmal_dev *dev,
  18326. + struct v4l2_ctrl *ctrl,
  18327. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18328. +{
  18329. + u32 bitrate_mode;
  18330. + struct vchiq_mmal_port *encoder_out;
  18331. +
  18332. + encoder_out = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  18333. +
  18334. + dev->capture.encode_bitrate_mode = ctrl->val;
  18335. + switch (ctrl->val) {
  18336. + default:
  18337. + case V4L2_MPEG_VIDEO_BITRATE_MODE_VBR:
  18338. + bitrate_mode = MMAL_VIDEO_RATECONTROL_VARIABLE;
  18339. + break;
  18340. + case V4L2_MPEG_VIDEO_BITRATE_MODE_CBR:
  18341. + bitrate_mode = MMAL_VIDEO_RATECONTROL_CONSTANT;
  18342. + break;
  18343. + }
  18344. +
  18345. + vchiq_mmal_port_parameter_set(dev->instance, encoder_out,
  18346. + mmal_ctrl->mmal_id,
  18347. + &bitrate_mode,
  18348. + sizeof(bitrate_mode));
  18349. + return 0;
  18350. +}
  18351. +
  18352. +static int ctrl_set_image_encode_output(struct bm2835_mmal_dev *dev,
  18353. + struct v4l2_ctrl *ctrl,
  18354. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18355. +{
  18356. + u32 u32_value;
  18357. + struct vchiq_mmal_port *jpeg_out;
  18358. +
  18359. + jpeg_out = &dev->component[MMAL_COMPONENT_IMAGE_ENCODE]->output[0];
  18360. +
  18361. + u32_value = ctrl->val;
  18362. +
  18363. + return vchiq_mmal_port_parameter_set(dev->instance, jpeg_out,
  18364. + mmal_ctrl->mmal_id,
  18365. + &u32_value, sizeof(u32_value));
  18366. +}
  18367. +
  18368. +static int ctrl_set_video_encode_param_output(struct bm2835_mmal_dev *dev,
  18369. + struct v4l2_ctrl *ctrl,
  18370. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18371. +{
  18372. + u32 u32_value;
  18373. + struct vchiq_mmal_port *vid_enc_ctl;
  18374. +
  18375. + vid_enc_ctl = &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0];
  18376. +
  18377. + u32_value = ctrl->val;
  18378. +
  18379. + return vchiq_mmal_port_parameter_set(dev->instance, vid_enc_ctl,
  18380. + mmal_ctrl->mmal_id,
  18381. + &u32_value, sizeof(u32_value));
  18382. +}
  18383. +
  18384. +static int ctrl_set_video_encode_profile_level(struct bm2835_mmal_dev *dev,
  18385. + struct v4l2_ctrl *ctrl,
  18386. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18387. +{
  18388. + struct mmal_parameter_video_profile param;
  18389. + int ret = 0;
  18390. +
  18391. + if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_PROFILE) {
  18392. + switch (ctrl->val) {
  18393. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  18394. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  18395. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  18396. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  18397. + dev->capture.enc_profile = ctrl->val;
  18398. + break;
  18399. + default:
  18400. + ret = -EINVAL;
  18401. + break;
  18402. + }
  18403. + } else if (ctrl->id == V4L2_CID_MPEG_VIDEO_H264_LEVEL) {
  18404. + switch (ctrl->val) {
  18405. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  18406. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  18407. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  18408. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  18409. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  18410. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  18411. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  18412. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  18413. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  18414. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  18415. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  18416. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  18417. + dev->capture.enc_level = ctrl->val;
  18418. + break;
  18419. + default:
  18420. + ret = -EINVAL;
  18421. + break;
  18422. + }
  18423. + }
  18424. +
  18425. + if (!ret) {
  18426. + switch (dev->capture.enc_profile) {
  18427. + case V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE:
  18428. + param.profile = MMAL_VIDEO_PROFILE_H264_BASELINE;
  18429. + break;
  18430. + case V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE:
  18431. + param.profile =
  18432. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE;
  18433. + break;
  18434. + case V4L2_MPEG_VIDEO_H264_PROFILE_MAIN:
  18435. + param.profile = MMAL_VIDEO_PROFILE_H264_MAIN;
  18436. + break;
  18437. + case V4L2_MPEG_VIDEO_H264_PROFILE_HIGH:
  18438. + param.profile = MMAL_VIDEO_PROFILE_H264_HIGH;
  18439. + break;
  18440. + default:
  18441. + /* Should never get here */
  18442. + break;
  18443. + }
  18444. +
  18445. + switch (dev->capture.enc_level) {
  18446. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_0:
  18447. + param.level = MMAL_VIDEO_LEVEL_H264_1;
  18448. + break;
  18449. + case V4L2_MPEG_VIDEO_H264_LEVEL_1B:
  18450. + param.level = MMAL_VIDEO_LEVEL_H264_1b;
  18451. + break;
  18452. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_1:
  18453. + param.level = MMAL_VIDEO_LEVEL_H264_11;
  18454. + break;
  18455. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_2:
  18456. + param.level = MMAL_VIDEO_LEVEL_H264_12;
  18457. + break;
  18458. + case V4L2_MPEG_VIDEO_H264_LEVEL_1_3:
  18459. + param.level = MMAL_VIDEO_LEVEL_H264_13;
  18460. + break;
  18461. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_0:
  18462. + param.level = MMAL_VIDEO_LEVEL_H264_2;
  18463. + break;
  18464. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_1:
  18465. + param.level = MMAL_VIDEO_LEVEL_H264_21;
  18466. + break;
  18467. + case V4L2_MPEG_VIDEO_H264_LEVEL_2_2:
  18468. + param.level = MMAL_VIDEO_LEVEL_H264_22;
  18469. + break;
  18470. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_0:
  18471. + param.level = MMAL_VIDEO_LEVEL_H264_3;
  18472. + break;
  18473. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_1:
  18474. + param.level = MMAL_VIDEO_LEVEL_H264_31;
  18475. + break;
  18476. + case V4L2_MPEG_VIDEO_H264_LEVEL_3_2:
  18477. + param.level = MMAL_VIDEO_LEVEL_H264_32;
  18478. + break;
  18479. + case V4L2_MPEG_VIDEO_H264_LEVEL_4_0:
  18480. + param.level = MMAL_VIDEO_LEVEL_H264_4;
  18481. + break;
  18482. + default:
  18483. + /* Should never get here */
  18484. + break;
  18485. + }
  18486. +
  18487. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  18488. + &dev->component[MMAL_COMPONENT_VIDEO_ENCODE]->output[0],
  18489. + mmal_ctrl->mmal_id,
  18490. + &param, sizeof(param));
  18491. + }
  18492. + return ret;
  18493. +}
  18494. +
  18495. +static int ctrl_set_scene_mode(struct bm2835_mmal_dev *dev,
  18496. + struct v4l2_ctrl *ctrl,
  18497. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl)
  18498. +{
  18499. + int ret = 0;
  18500. + int shutter_speed;
  18501. + struct vchiq_mmal_port *control;
  18502. +
  18503. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18504. + "scene mode selected %d, was %d\n", ctrl->val,
  18505. + dev->scene_mode);
  18506. + control = &dev->component[MMAL_COMPONENT_CAMERA]->control;
  18507. +
  18508. + if (ctrl->val == dev->scene_mode)
  18509. + return 0;
  18510. +
  18511. + if (ctrl->val == V4L2_SCENE_MODE_NONE) {
  18512. + /* Restore all user selections */
  18513. + dev->scene_mode = V4L2_SCENE_MODE_NONE;
  18514. +
  18515. + if (dev->exposure_mode_user == MMAL_PARAM_EXPOSUREMODE_OFF)
  18516. + shutter_speed = dev->manual_shutter_speed;
  18517. + else
  18518. + shutter_speed = 0;
  18519. +
  18520. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18521. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  18522. + __func__, shutter_speed, dev->exposure_mode_user,
  18523. + dev->metering_mode);
  18524. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  18525. + control,
  18526. + MMAL_PARAMETER_SHUTTER_SPEED,
  18527. + &shutter_speed,
  18528. + sizeof(shutter_speed));
  18529. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  18530. + control,
  18531. + MMAL_PARAMETER_EXPOSURE_MODE,
  18532. + &dev->exposure_mode_user,
  18533. + sizeof(u32));
  18534. + dev->exposure_mode_active = dev->exposure_mode_user;
  18535. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  18536. + control,
  18537. + MMAL_PARAMETER_EXP_METERING_MODE,
  18538. + &dev->metering_mode,
  18539. + sizeof(u32));
  18540. + ret += set_framerate_params(dev);
  18541. + } else {
  18542. + /* Set up scene mode */
  18543. + int i;
  18544. + const struct v4l2_mmal_scene_config *scene = NULL;
  18545. + int shutter_speed;
  18546. + enum mmal_parameter_exposuremode exposure_mode;
  18547. + enum mmal_parameter_exposuremeteringmode metering_mode;
  18548. +
  18549. + for (i = 0; i < ARRAY_SIZE(scene_configs); i++) {
  18550. + if (scene_configs[i].v4l2_scene ==
  18551. + ctrl->val) {
  18552. + scene = &scene_configs[i];
  18553. + break;
  18554. + }
  18555. + }
  18556. + if (i >= ARRAY_SIZE(scene_configs))
  18557. + return -EINVAL;
  18558. +
  18559. + /* Set all the values */
  18560. + dev->scene_mode = ctrl->val;
  18561. +
  18562. + if (scene->exposure_mode == MMAL_PARAM_EXPOSUREMODE_OFF)
  18563. + shutter_speed = dev->manual_shutter_speed;
  18564. + else
  18565. + shutter_speed = 0;
  18566. + exposure_mode = scene->exposure_mode;
  18567. + metering_mode = scene->metering_mode;
  18568. +
  18569. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18570. + "%s: scene mode none: shut_speed %d, exp_mode %d, metering %d\n",
  18571. + __func__, shutter_speed, exposure_mode, metering_mode);
  18572. +
  18573. + ret = vchiq_mmal_port_parameter_set(dev->instance, control,
  18574. + MMAL_PARAMETER_SHUTTER_SPEED,
  18575. + &shutter_speed,
  18576. + sizeof(shutter_speed));
  18577. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  18578. + control,
  18579. + MMAL_PARAMETER_EXPOSURE_MODE,
  18580. + &exposure_mode,
  18581. + sizeof(u32));
  18582. + dev->exposure_mode_active = exposure_mode;
  18583. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  18584. + MMAL_PARAMETER_EXPOSURE_MODE,
  18585. + &exposure_mode,
  18586. + sizeof(u32));
  18587. + ret += vchiq_mmal_port_parameter_set(dev->instance, control,
  18588. + MMAL_PARAMETER_EXP_METERING_MODE,
  18589. + &metering_mode,
  18590. + sizeof(u32));
  18591. + ret += set_framerate_params(dev);
  18592. + }
  18593. + if (ret) {
  18594. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18595. + "%s: Setting scene to %d, ret=%d\n",
  18596. + __func__, ctrl->val, ret);
  18597. + ret = -EINVAL;
  18598. + }
  18599. + return 0;
  18600. +}
  18601. +
  18602. +static int bm2835_mmal_s_ctrl(struct v4l2_ctrl *ctrl)
  18603. +{
  18604. + struct bm2835_mmal_dev *dev =
  18605. + container_of(ctrl->handler, struct bm2835_mmal_dev,
  18606. + ctrl_handler);
  18607. + const struct bm2835_mmal_v4l2_ctrl *mmal_ctrl = ctrl->priv;
  18608. + int ret;
  18609. +
  18610. + if ((mmal_ctrl == NULL) ||
  18611. + (mmal_ctrl->id != ctrl->id) ||
  18612. + (mmal_ctrl->setter == NULL)) {
  18613. + pr_warn("mmal_ctrl:%p ctrl id:%d\n", mmal_ctrl, ctrl->id);
  18614. + return -EINVAL;
  18615. + }
  18616. +
  18617. + ret = mmal_ctrl->setter(dev, ctrl, mmal_ctrl);
  18618. + if (ret)
  18619. + pr_warn("ctrl id:%d/MMAL param %08X- returned ret %d\n",
  18620. + ctrl->id, mmal_ctrl->mmal_id, ret);
  18621. + if (mmal_ctrl->ignore_errors)
  18622. + ret = 0;
  18623. + return ret;
  18624. +}
  18625. +
  18626. +static const struct v4l2_ctrl_ops bm2835_mmal_ctrl_ops = {
  18627. + .s_ctrl = bm2835_mmal_s_ctrl,
  18628. +};
  18629. +
  18630. +
  18631. +
  18632. +static const struct bm2835_mmal_v4l2_ctrl v4l2_ctrls[V4L2_CTRL_COUNT] = {
  18633. + {
  18634. + V4L2_CID_SATURATION, MMAL_CONTROL_TYPE_STD,
  18635. + -100, 100, 0, 1, NULL,
  18636. + MMAL_PARAMETER_SATURATION,
  18637. + &ctrl_set_rational,
  18638. + false
  18639. + },
  18640. + {
  18641. + V4L2_CID_SHARPNESS, MMAL_CONTROL_TYPE_STD,
  18642. + -100, 100, 0, 1, NULL,
  18643. + MMAL_PARAMETER_SHARPNESS,
  18644. + &ctrl_set_rational,
  18645. + false
  18646. + },
  18647. + {
  18648. + V4L2_CID_CONTRAST, MMAL_CONTROL_TYPE_STD,
  18649. + -100, 100, 0, 1, NULL,
  18650. + MMAL_PARAMETER_CONTRAST,
  18651. + &ctrl_set_rational,
  18652. + false
  18653. + },
  18654. + {
  18655. + V4L2_CID_BRIGHTNESS, MMAL_CONTROL_TYPE_STD,
  18656. + 0, 100, 50, 1, NULL,
  18657. + MMAL_PARAMETER_BRIGHTNESS,
  18658. + &ctrl_set_rational,
  18659. + false
  18660. + },
  18661. + {
  18662. + V4L2_CID_ISO_SENSITIVITY, MMAL_CONTROL_TYPE_INT_MENU,
  18663. + 0, ARRAY_SIZE(iso_qmenu) - 1, 0, 1, iso_qmenu,
  18664. + MMAL_PARAMETER_ISO,
  18665. + &ctrl_set_value_menu,
  18666. + false
  18667. + },
  18668. + {
  18669. + V4L2_CID_IMAGE_STABILIZATION, MMAL_CONTROL_TYPE_STD,
  18670. + 0, 1, 0, 1, NULL,
  18671. + MMAL_PARAMETER_VIDEO_STABILISATION,
  18672. + &ctrl_set_value,
  18673. + false
  18674. + },
  18675. +/* {
  18676. + 0, MMAL_CONTROL_TYPE_CLUSTER, 3, 1, 0, NULL, 0, NULL
  18677. + }, */
  18678. + {
  18679. + V4L2_CID_EXPOSURE_AUTO, MMAL_CONTROL_TYPE_STD_MENU,
  18680. + ~0x03, 3, V4L2_EXPOSURE_AUTO, 0, NULL,
  18681. + MMAL_PARAMETER_EXPOSURE_MODE,
  18682. + &ctrl_set_exposure,
  18683. + false
  18684. + },
  18685. +/* todo this needs mixing in with set exposure
  18686. + {
  18687. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  18688. + },
  18689. + */
  18690. + {
  18691. + V4L2_CID_EXPOSURE_ABSOLUTE, MMAL_CONTROL_TYPE_STD,
  18692. + /* Units of 100usecs */
  18693. + 1, 1*1000*10, 100*10, 1, NULL,
  18694. + MMAL_PARAMETER_SHUTTER_SPEED,
  18695. + &ctrl_set_exposure,
  18696. + false
  18697. + },
  18698. + {
  18699. + V4L2_CID_AUTO_EXPOSURE_BIAS, MMAL_CONTROL_TYPE_INT_MENU,
  18700. + 0, ARRAY_SIZE(ev_bias_qmenu) - 1,
  18701. + (ARRAY_SIZE(ev_bias_qmenu)+1)/2 - 1, 0, ev_bias_qmenu,
  18702. + MMAL_PARAMETER_EXPOSURE_COMP,
  18703. + &ctrl_set_value_ev,
  18704. + false
  18705. + },
  18706. + {
  18707. + V4L2_CID_EXPOSURE_AUTO_PRIORITY, MMAL_CONTROL_TYPE_STD,
  18708. + 0, 1,
  18709. + 0, 1, NULL,
  18710. + 0, /* Dummy MMAL ID as it gets mapped into FPS range*/
  18711. + &ctrl_set_exposure,
  18712. + false
  18713. + },
  18714. + {
  18715. + V4L2_CID_EXPOSURE_METERING,
  18716. + MMAL_CONTROL_TYPE_STD_MENU,
  18717. + ~0x7, 2, V4L2_EXPOSURE_METERING_AVERAGE, 0, NULL,
  18718. + MMAL_PARAMETER_EXP_METERING_MODE,
  18719. + &ctrl_set_metering_mode,
  18720. + false
  18721. + },
  18722. + {
  18723. + V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE,
  18724. + MMAL_CONTROL_TYPE_STD_MENU,
  18725. + ~0x3ff, 9, V4L2_WHITE_BALANCE_AUTO, 0, NULL,
  18726. + MMAL_PARAMETER_AWB_MODE,
  18727. + &ctrl_set_awb_mode,
  18728. + false
  18729. + },
  18730. + {
  18731. + V4L2_CID_RED_BALANCE, MMAL_CONTROL_TYPE_STD,
  18732. + 1, 7999, 1000, 1, NULL,
  18733. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  18734. + &ctrl_set_awb_gains,
  18735. + false
  18736. + },
  18737. + {
  18738. + V4L2_CID_BLUE_BALANCE, MMAL_CONTROL_TYPE_STD,
  18739. + 1, 7999, 1000, 1, NULL,
  18740. + MMAL_PARAMETER_CUSTOM_AWB_GAINS,
  18741. + &ctrl_set_awb_gains,
  18742. + false
  18743. + },
  18744. + {
  18745. + V4L2_CID_COLORFX, MMAL_CONTROL_TYPE_STD_MENU,
  18746. + 0, 15, V4L2_COLORFX_NONE, 0, NULL,
  18747. + MMAL_PARAMETER_IMAGE_EFFECT,
  18748. + &ctrl_set_image_effect,
  18749. + false
  18750. + },
  18751. + {
  18752. + V4L2_CID_COLORFX_CBCR, MMAL_CONTROL_TYPE_STD,
  18753. + 0, 0xffff, 0x8080, 1, NULL,
  18754. + MMAL_PARAMETER_COLOUR_EFFECT,
  18755. + &ctrl_set_colfx,
  18756. + false
  18757. + },
  18758. + {
  18759. + V4L2_CID_ROTATE, MMAL_CONTROL_TYPE_STD,
  18760. + 0, 360, 0, 90, NULL,
  18761. + MMAL_PARAMETER_ROTATION,
  18762. + &ctrl_set_rotate,
  18763. + false
  18764. + },
  18765. + {
  18766. + V4L2_CID_HFLIP, MMAL_CONTROL_TYPE_STD,
  18767. + 0, 1, 0, 1, NULL,
  18768. + MMAL_PARAMETER_MIRROR,
  18769. + &ctrl_set_flip,
  18770. + false
  18771. + },
  18772. + {
  18773. + V4L2_CID_VFLIP, MMAL_CONTROL_TYPE_STD,
  18774. + 0, 1, 0, 1, NULL,
  18775. + MMAL_PARAMETER_MIRROR,
  18776. + &ctrl_set_flip,
  18777. + false
  18778. + },
  18779. + {
  18780. + V4L2_CID_MPEG_VIDEO_BITRATE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  18781. + 0, ARRAY_SIZE(bitrate_mode_qmenu) - 1,
  18782. + 0, 0, bitrate_mode_qmenu,
  18783. + MMAL_PARAMETER_RATECONTROL,
  18784. + &ctrl_set_bitrate_mode,
  18785. + false
  18786. + },
  18787. + {
  18788. + V4L2_CID_MPEG_VIDEO_BITRATE, MMAL_CONTROL_TYPE_STD,
  18789. + 25*1000, 25*1000*1000, 10*1000*1000, 25*1000, NULL,
  18790. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  18791. + &ctrl_set_bitrate,
  18792. + false
  18793. + },
  18794. + {
  18795. + V4L2_CID_JPEG_COMPRESSION_QUALITY, MMAL_CONTROL_TYPE_STD,
  18796. + 1, 100,
  18797. + 30, 1, NULL,
  18798. + MMAL_PARAMETER_JPEG_Q_FACTOR,
  18799. + &ctrl_set_image_encode_output,
  18800. + false
  18801. + },
  18802. + {
  18803. + V4L2_CID_POWER_LINE_FREQUENCY, MMAL_CONTROL_TYPE_STD_MENU,
  18804. + 0, ARRAY_SIZE(mains_freq_qmenu) - 1,
  18805. + 1, 1, NULL,
  18806. + MMAL_PARAMETER_FLICKER_AVOID,
  18807. + &ctrl_set_flicker_avoidance,
  18808. + false
  18809. + },
  18810. + {
  18811. + V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, MMAL_CONTROL_TYPE_STD,
  18812. + 0, 1,
  18813. + 0, 1, NULL,
  18814. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER,
  18815. + &ctrl_set_video_encode_param_output,
  18816. + true /* Errors ignored as requires latest firmware to work */
  18817. + },
  18818. + {
  18819. + V4L2_CID_MPEG_VIDEO_H264_PROFILE,
  18820. + MMAL_CONTROL_TYPE_STD_MENU,
  18821. + ~((1<<V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
  18822. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_CONSTRAINED_BASELINE) |
  18823. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) |
  18824. + (1<<V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)),
  18825. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
  18826. + V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 1, NULL,
  18827. + MMAL_PARAMETER_PROFILE,
  18828. + &ctrl_set_video_encode_profile_level,
  18829. + false
  18830. + },
  18831. + {
  18832. + V4L2_CID_MPEG_VIDEO_H264_LEVEL, MMAL_CONTROL_TYPE_STD_MENU,
  18833. + ~((1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_0) |
  18834. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1B) |
  18835. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_1) |
  18836. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_2) |
  18837. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_1_3) |
  18838. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_0) |
  18839. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_1) |
  18840. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_2_2) |
  18841. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_0) |
  18842. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_1) |
  18843. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_3_2) |
  18844. + (1<<V4L2_MPEG_VIDEO_H264_LEVEL_4_0)),
  18845. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0,
  18846. + V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 1, NULL,
  18847. + MMAL_PARAMETER_PROFILE,
  18848. + &ctrl_set_video_encode_profile_level,
  18849. + false
  18850. + },
  18851. + {
  18852. + V4L2_CID_SCENE_MODE, MMAL_CONTROL_TYPE_STD_MENU,
  18853. + -1, /* Min is computed at runtime */
  18854. + V4L2_SCENE_MODE_TEXT,
  18855. + V4L2_SCENE_MODE_NONE, 1, NULL,
  18856. + MMAL_PARAMETER_PROFILE,
  18857. + &ctrl_set_scene_mode,
  18858. + false
  18859. + },
  18860. + {
  18861. + V4L2_CID_MPEG_VIDEO_H264_I_PERIOD, MMAL_CONTROL_TYPE_STD,
  18862. + 0, 0x7FFFFFFF, 60, 1, NULL,
  18863. + MMAL_PARAMETER_INTRAPERIOD,
  18864. + &ctrl_set_video_encode_param_output,
  18865. + false
  18866. + },
  18867. +};
  18868. +
  18869. +int bm2835_mmal_set_all_camera_controls(struct bm2835_mmal_dev *dev)
  18870. +{
  18871. + int c;
  18872. + int ret = 0;
  18873. +
  18874. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  18875. + if ((dev->ctrls[c]) && (v4l2_ctrls[c].setter)) {
  18876. + ret = v4l2_ctrls[c].setter(dev, dev->ctrls[c],
  18877. + &v4l2_ctrls[c]);
  18878. + if (!v4l2_ctrls[c].ignore_errors && ret) {
  18879. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18880. + "Failed when setting default values for ctrl %d\n",
  18881. + c);
  18882. + break;
  18883. + }
  18884. + }
  18885. + }
  18886. + return ret;
  18887. +}
  18888. +
  18889. +int set_framerate_params(struct bm2835_mmal_dev *dev)
  18890. +{
  18891. + struct mmal_parameter_fps_range fps_range;
  18892. + int ret;
  18893. +
  18894. + if ((dev->exposure_mode_active != MMAL_PARAM_EXPOSUREMODE_OFF) &&
  18895. + (dev->exp_auto_priority)) {
  18896. + /* Variable FPS. Define min FPS as 1fps.
  18897. + * Max as max defined FPS.
  18898. + */
  18899. + fps_range.fps_low.num = 1;
  18900. + fps_range.fps_low.den = 1;
  18901. + fps_range.fps_high.num = dev->capture.timeperframe.denominator;
  18902. + fps_range.fps_high.den = dev->capture.timeperframe.numerator;
  18903. + } else {
  18904. + /* Fixed FPS - set min and max to be the same */
  18905. + fps_range.fps_low.num = fps_range.fps_high.num =
  18906. + dev->capture.timeperframe.denominator;
  18907. + fps_range.fps_low.den = fps_range.fps_high.den =
  18908. + dev->capture.timeperframe.numerator;
  18909. + }
  18910. +
  18911. + v4l2_dbg(1, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18912. + "Set fps range to %d/%d to %d/%d\n",
  18913. + fps_range.fps_low.num,
  18914. + fps_range.fps_low.den,
  18915. + fps_range.fps_high.num,
  18916. + fps_range.fps_high.den
  18917. + );
  18918. +
  18919. + ret = vchiq_mmal_port_parameter_set(dev->instance,
  18920. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18921. + output[MMAL_CAMERA_PORT_PREVIEW],
  18922. + MMAL_PARAMETER_FPS_RANGE,
  18923. + &fps_range, sizeof(fps_range));
  18924. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  18925. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18926. + output[MMAL_CAMERA_PORT_VIDEO],
  18927. + MMAL_PARAMETER_FPS_RANGE,
  18928. + &fps_range, sizeof(fps_range));
  18929. + ret += vchiq_mmal_port_parameter_set(dev->instance,
  18930. + &dev->component[MMAL_COMPONENT_CAMERA]->
  18931. + output[MMAL_CAMERA_PORT_CAPTURE],
  18932. + MMAL_PARAMETER_FPS_RANGE,
  18933. + &fps_range, sizeof(fps_range));
  18934. + if (ret)
  18935. + v4l2_dbg(0, bcm2835_v4l2_debug, &dev->v4l2_dev,
  18936. + "Failed to set fps ret %d\n",
  18937. + ret);
  18938. +
  18939. + return ret;
  18940. +
  18941. +}
  18942. +
  18943. +int bm2835_mmal_init_controls(struct bm2835_mmal_dev *dev,
  18944. + struct v4l2_ctrl_handler *hdl)
  18945. +{
  18946. + int c;
  18947. + const struct bm2835_mmal_v4l2_ctrl *ctrl;
  18948. +
  18949. + v4l2_ctrl_handler_init(hdl, V4L2_CTRL_COUNT);
  18950. +
  18951. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  18952. + ctrl = &v4l2_ctrls[c];
  18953. +
  18954. + switch (ctrl->type) {
  18955. + case MMAL_CONTROL_TYPE_STD:
  18956. + dev->ctrls[c] = v4l2_ctrl_new_std(hdl,
  18957. + &bm2835_mmal_ctrl_ops, ctrl->id,
  18958. + ctrl->min, ctrl->max, ctrl->step, ctrl->def);
  18959. + break;
  18960. +
  18961. + case MMAL_CONTROL_TYPE_STD_MENU:
  18962. + {
  18963. + int mask = ctrl->min;
  18964. +
  18965. + if (ctrl->id == V4L2_CID_SCENE_MODE) {
  18966. + /* Special handling to work out the mask
  18967. + * value based on the scene_configs array
  18968. + * at runtime. Reduces the chance of
  18969. + * mismatches.
  18970. + */
  18971. + int i;
  18972. + mask = 1<<V4L2_SCENE_MODE_NONE;
  18973. + for (i = 0;
  18974. + i < ARRAY_SIZE(scene_configs);
  18975. + i++) {
  18976. + mask |= 1<<scene_configs[i].v4l2_scene;
  18977. + }
  18978. + mask = ~mask;
  18979. + }
  18980. +
  18981. + dev->ctrls[c] = v4l2_ctrl_new_std_menu(hdl,
  18982. + &bm2835_mmal_ctrl_ops, ctrl->id,
  18983. + ctrl->max, mask, ctrl->def);
  18984. + break;
  18985. + }
  18986. +
  18987. + case MMAL_CONTROL_TYPE_INT_MENU:
  18988. + dev->ctrls[c] = v4l2_ctrl_new_int_menu(hdl,
  18989. + &bm2835_mmal_ctrl_ops, ctrl->id,
  18990. + ctrl->max, ctrl->def, ctrl->imenu);
  18991. + break;
  18992. +
  18993. + case MMAL_CONTROL_TYPE_CLUSTER:
  18994. + /* skip this entry when constructing controls */
  18995. + continue;
  18996. + }
  18997. +
  18998. + if (hdl->error)
  18999. + break;
  19000. +
  19001. + dev->ctrls[c]->priv = (void *)ctrl;
  19002. + }
  19003. +
  19004. + if (hdl->error) {
  19005. + pr_err("error adding control %d/%d id 0x%x\n", c,
  19006. + V4L2_CTRL_COUNT, ctrl->id);
  19007. + return hdl->error;
  19008. + }
  19009. +
  19010. + for (c = 0; c < V4L2_CTRL_COUNT; c++) {
  19011. + ctrl = &v4l2_ctrls[c];
  19012. +
  19013. + switch (ctrl->type) {
  19014. + case MMAL_CONTROL_TYPE_CLUSTER:
  19015. + v4l2_ctrl_auto_cluster(ctrl->min,
  19016. + &dev->ctrls[c+1],
  19017. + ctrl->max,
  19018. + ctrl->def);
  19019. + break;
  19020. +
  19021. + case MMAL_CONTROL_TYPE_STD:
  19022. + case MMAL_CONTROL_TYPE_STD_MENU:
  19023. + case MMAL_CONTROL_TYPE_INT_MENU:
  19024. + break;
  19025. + }
  19026. +
  19027. + }
  19028. +
  19029. + return 0;
  19030. +}
  19031. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/Kconfig linux-3.16-rpi/drivers/media/platform/bcm2835/Kconfig
  19032. --- linux-3.16.2/drivers/media/platform/bcm2835/Kconfig 1970-01-01 01:00:00.000000000 +0100
  19033. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/Kconfig 2014-04-13 17:32:57.000000000 +0200
  19034. @@ -0,0 +1,25 @@
  19035. +# Broadcom VideoCore IV v4l2 camera support
  19036. +
  19037. +config VIDEO_BCM2835
  19038. + bool "Broadcom BCM2835 camera interface driver"
  19039. + depends on VIDEO_V4L2 && ARCH_BCM2708
  19040. + ---help---
  19041. + Say Y here to enable camera host interface devices for
  19042. + Broadcom BCM2835 SoC. This operates over the VCHIQ interface
  19043. + to a service running on VideoCore.
  19044. +
  19045. +
  19046. +if VIDEO_BCM2835
  19047. +
  19048. +config VIDEO_BCM2835_MMAL
  19049. + tristate "Broadcom BM2835 MMAL camera interface driver"
  19050. + depends on BCM2708_VCHIQ
  19051. + select VIDEOBUF2_VMALLOC
  19052. + ---help---
  19053. + This is a V4L2 driver for the Broadcom BCM2835 MMAL camera host interface
  19054. +
  19055. + To compile this driver as a module, choose M here: the
  19056. + module will be called bcm2835-v4l2.o
  19057. +
  19058. +
  19059. +endif # VIDEO_BM2835
  19060. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/Makefile linux-3.16-rpi/drivers/media/platform/bcm2835/Makefile
  19061. --- linux-3.16.2/drivers/media/platform/bcm2835/Makefile 1970-01-01 01:00:00.000000000 +0100
  19062. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/Makefile 2014-04-13 17:32:57.000000000 +0200
  19063. @@ -0,0 +1,5 @@
  19064. +bcm2835-v4l2-objs := bcm2835-camera.o controls.o mmal-vchiq.o
  19065. +
  19066. +obj-$(CONFIG_VIDEO_BCM2835_MMAL) += bcm2835-v4l2.o
  19067. +
  19068. +ccflags-$(CONFIG_VIDEO_BCM2835) += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  19069. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-common.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-common.h
  19070. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-common.h 1970-01-01 01:00:00.000000000 +0100
  19071. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-common.h 2014-04-13 17:32:57.000000000 +0200
  19072. @@ -0,0 +1,53 @@
  19073. +/*
  19074. + * Broadcom BM2835 V4L2 driver
  19075. + *
  19076. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19077. + *
  19078. + * This file is subject to the terms and conditions of the GNU General Public
  19079. + * License. See the file COPYING in the main directory of this archive
  19080. + * for more details.
  19081. + *
  19082. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19083. + * Dave Stevenson <dsteve@broadcom.com>
  19084. + * Simon Mellor <simellor@broadcom.com>
  19085. + * Luke Diamand <luked@broadcom.com>
  19086. + *
  19087. + * MMAL structures
  19088. + *
  19089. + */
  19090. +
  19091. +#define MMAL_FOURCC(a, b, c, d) ((a) | (b << 8) | (c << 16) | (d << 24))
  19092. +#define MMAL_MAGIC MMAL_FOURCC('m', 'm', 'a', 'l')
  19093. +
  19094. +/** Special value signalling that time is not known */
  19095. +#define MMAL_TIME_UNKNOWN (1LL<<63)
  19096. +
  19097. +/* mapping between v4l and mmal video modes */
  19098. +struct mmal_fmt {
  19099. + char *name;
  19100. + u32 fourcc; /* v4l2 format id */
  19101. + int flags; /* v4l2 flags field */
  19102. + u32 mmal;
  19103. + int depth;
  19104. + u32 mmal_component; /* MMAL component index to be used to encode */
  19105. +};
  19106. +
  19107. +/* buffer for one video frame */
  19108. +struct mmal_buffer {
  19109. + /* v4l buffer data -- must be first */
  19110. + struct vb2_buffer vb;
  19111. +
  19112. + /* list of buffers available */
  19113. + struct list_head list;
  19114. +
  19115. + void *buffer; /* buffer pointer */
  19116. + unsigned long buffer_size; /* size of allocated buffer */
  19117. +};
  19118. +
  19119. +/* */
  19120. +struct mmal_colourfx {
  19121. + s32 enable;
  19122. + u32 u;
  19123. + u32 v;
  19124. +};
  19125. +
  19126. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-encodings.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-encodings.h
  19127. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-encodings.h 1970-01-01 01:00:00.000000000 +0100
  19128. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-encodings.h 2014-09-14 19:03:24.000000000 +0200
  19129. @@ -0,0 +1,127 @@
  19130. +/*
  19131. + * Broadcom BM2835 V4L2 driver
  19132. + *
  19133. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19134. + *
  19135. + * This file is subject to the terms and conditions of the GNU General Public
  19136. + * License. See the file COPYING in the main directory of this archive
  19137. + * for more details.
  19138. + *
  19139. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19140. + * Dave Stevenson <dsteve@broadcom.com>
  19141. + * Simon Mellor <simellor@broadcom.com>
  19142. + * Luke Diamand <luked@broadcom.com>
  19143. + */
  19144. +#ifndef MMAL_ENCODINGS_H
  19145. +#define MMAL_ENCODINGS_H
  19146. +
  19147. +#define MMAL_ENCODING_H264 MMAL_FOURCC('H', '2', '6', '4')
  19148. +#define MMAL_ENCODING_H263 MMAL_FOURCC('H', '2', '6', '3')
  19149. +#define MMAL_ENCODING_MP4V MMAL_FOURCC('M', 'P', '4', 'V')
  19150. +#define MMAL_ENCODING_MP2V MMAL_FOURCC('M', 'P', '2', 'V')
  19151. +#define MMAL_ENCODING_MP1V MMAL_FOURCC('M', 'P', '1', 'V')
  19152. +#define MMAL_ENCODING_WMV3 MMAL_FOURCC('W', 'M', 'V', '3')
  19153. +#define MMAL_ENCODING_WMV2 MMAL_FOURCC('W', 'M', 'V', '2')
  19154. +#define MMAL_ENCODING_WMV1 MMAL_FOURCC('W', 'M', 'V', '1')
  19155. +#define MMAL_ENCODING_WVC1 MMAL_FOURCC('W', 'V', 'C', '1')
  19156. +#define MMAL_ENCODING_VP8 MMAL_FOURCC('V', 'P', '8', ' ')
  19157. +#define MMAL_ENCODING_VP7 MMAL_FOURCC('V', 'P', '7', ' ')
  19158. +#define MMAL_ENCODING_VP6 MMAL_FOURCC('V', 'P', '6', ' ')
  19159. +#define MMAL_ENCODING_THEORA MMAL_FOURCC('T', 'H', 'E', 'O')
  19160. +#define MMAL_ENCODING_SPARK MMAL_FOURCC('S', 'P', 'R', 'K')
  19161. +#define MMAL_ENCODING_MJPEG MMAL_FOURCC('M', 'J', 'P', 'G')
  19162. +
  19163. +#define MMAL_ENCODING_JPEG MMAL_FOURCC('J', 'P', 'E', 'G')
  19164. +#define MMAL_ENCODING_GIF MMAL_FOURCC('G', 'I', 'F', ' ')
  19165. +#define MMAL_ENCODING_PNG MMAL_FOURCC('P', 'N', 'G', ' ')
  19166. +#define MMAL_ENCODING_PPM MMAL_FOURCC('P', 'P', 'M', ' ')
  19167. +#define MMAL_ENCODING_TGA MMAL_FOURCC('T', 'G', 'A', ' ')
  19168. +#define MMAL_ENCODING_BMP MMAL_FOURCC('B', 'M', 'P', ' ')
  19169. +
  19170. +#define MMAL_ENCODING_I420 MMAL_FOURCC('I', '4', '2', '0')
  19171. +#define MMAL_ENCODING_I420_SLICE MMAL_FOURCC('S', '4', '2', '0')
  19172. +#define MMAL_ENCODING_YV12 MMAL_FOURCC('Y', 'V', '1', '2')
  19173. +#define MMAL_ENCODING_I422 MMAL_FOURCC('I', '4', '2', '2')
  19174. +#define MMAL_ENCODING_I422_SLICE MMAL_FOURCC('S', '4', '2', '2')
  19175. +#define MMAL_ENCODING_YUYV MMAL_FOURCC('Y', 'U', 'Y', 'V')
  19176. +#define MMAL_ENCODING_YVYU MMAL_FOURCC('Y', 'V', 'Y', 'U')
  19177. +#define MMAL_ENCODING_UYVY MMAL_FOURCC('U', 'Y', 'V', 'Y')
  19178. +#define MMAL_ENCODING_VYUY MMAL_FOURCC('V', 'Y', 'U', 'Y')
  19179. +#define MMAL_ENCODING_NV12 MMAL_FOURCC('N', 'V', '1', '2')
  19180. +#define MMAL_ENCODING_NV21 MMAL_FOURCC('N', 'V', '2', '1')
  19181. +#define MMAL_ENCODING_ARGB MMAL_FOURCC('A', 'R', 'G', 'B')
  19182. +#define MMAL_ENCODING_RGBA MMAL_FOURCC('R', 'G', 'B', 'A')
  19183. +#define MMAL_ENCODING_ABGR MMAL_FOURCC('A', 'B', 'G', 'R')
  19184. +#define MMAL_ENCODING_BGRA MMAL_FOURCC('B', 'G', 'R', 'A')
  19185. +#define MMAL_ENCODING_RGB16 MMAL_FOURCC('R', 'G', 'B', '2')
  19186. +#define MMAL_ENCODING_RGB24 MMAL_FOURCC('R', 'G', 'B', '3')
  19187. +#define MMAL_ENCODING_RGB32 MMAL_FOURCC('R', 'G', 'B', '4')
  19188. +#define MMAL_ENCODING_BGR16 MMAL_FOURCC('B', 'G', 'R', '2')
  19189. +#define MMAL_ENCODING_BGR24 MMAL_FOURCC('B', 'G', 'R', '3')
  19190. +#define MMAL_ENCODING_BGR32 MMAL_FOURCC('B', 'G', 'R', '4')
  19191. +
  19192. +/** SAND Video (YUVUV128) format, native format understood by VideoCore.
  19193. + * This format is *not* opaque - if requested you will receive full frames
  19194. + * of YUV_UV video.
  19195. + */
  19196. +#define MMAL_ENCODING_YUVUV128 MMAL_FOURCC('S', 'A', 'N', 'D')
  19197. +
  19198. +/** VideoCore opaque image format, image handles are returned to
  19199. + * the host but not the actual image data.
  19200. + */
  19201. +#define MMAL_ENCODING_OPAQUE MMAL_FOURCC('O', 'P', 'Q', 'V')
  19202. +
  19203. +/** An EGL image handle
  19204. + */
  19205. +#define MMAL_ENCODING_EGL_IMAGE MMAL_FOURCC('E', 'G', 'L', 'I')
  19206. +
  19207. +/* }@ */
  19208. +
  19209. +/** \name Pre-defined audio encodings */
  19210. +/* @{ */
  19211. +#define MMAL_ENCODING_PCM_UNSIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'U')
  19212. +#define MMAL_ENCODING_PCM_UNSIGNED_LE MMAL_FOURCC('p', 'c', 'm', 'u')
  19213. +#define MMAL_ENCODING_PCM_SIGNED_BE MMAL_FOURCC('P', 'C', 'M', 'S')
  19214. +#define MMAL_ENCODING_PCM_SIGNED_LE MMAL_FOURCC('p', 'c', 'm', 's')
  19215. +#define MMAL_ENCODING_PCM_FLOAT_BE MMAL_FOURCC('P', 'C', 'M', 'F')
  19216. +#define MMAL_ENCODING_PCM_FLOAT_LE MMAL_FOURCC('p', 'c', 'm', 'f')
  19217. +
  19218. +/* Pre-defined H264 encoding variants */
  19219. +
  19220. +/** ISO 14496-10 Annex B byte stream format */
  19221. +#define MMAL_ENCODING_VARIANT_H264_DEFAULT 0
  19222. +/** ISO 14496-15 AVC stream format */
  19223. +#define MMAL_ENCODING_VARIANT_H264_AVC1 MMAL_FOURCC('A', 'V', 'C', '1')
  19224. +/** Implicitly delineated NAL units without emulation prevention */
  19225. +#define MMAL_ENCODING_VARIANT_H264_RAW MMAL_FOURCC('R', 'A', 'W', ' ')
  19226. +
  19227. +
  19228. +/** \defgroup MmalColorSpace List of pre-defined video color spaces
  19229. + * This defines a list of common color spaces. This list isn't exhaustive and
  19230. + * is only provided as a convenience to avoid clients having to use FourCC
  19231. + * codes directly. However components are allowed to define and use their own
  19232. + * FourCC codes.
  19233. + */
  19234. +/* @{ */
  19235. +
  19236. +/** Unknown color space */
  19237. +#define MMAL_COLOR_SPACE_UNKNOWN 0
  19238. +/** ITU-R BT.601-5 [SDTV] */
  19239. +#define MMAL_COLOR_SPACE_ITUR_BT601 MMAL_FOURCC('Y', '6', '0', '1')
  19240. +/** ITU-R BT.709-3 [HDTV] */
  19241. +#define MMAL_COLOR_SPACE_ITUR_BT709 MMAL_FOURCC('Y', '7', '0', '9')
  19242. +/** JPEG JFIF */
  19243. +#define MMAL_COLOR_SPACE_JPEG_JFIF MMAL_FOURCC('Y', 'J', 'F', 'I')
  19244. +/** Title 47 Code of Federal Regulations (2003) 73.682 (a) (20) */
  19245. +#define MMAL_COLOR_SPACE_FCC MMAL_FOURCC('Y', 'F', 'C', 'C')
  19246. +/** Society of Motion Picture and Television Engineers 240M (1999) */
  19247. +#define MMAL_COLOR_SPACE_SMPTE240M MMAL_FOURCC('Y', '2', '4', '0')
  19248. +/** ITU-R BT.470-2 System M */
  19249. +#define MMAL_COLOR_SPACE_BT470_2_M MMAL_FOURCC('Y', '_', '_', 'M')
  19250. +/** ITU-R BT.470-2 System BG */
  19251. +#define MMAL_COLOR_SPACE_BT470_2_BG MMAL_FOURCC('Y', '_', 'B', 'G')
  19252. +/** JPEG JFIF, but with 16..255 luma */
  19253. +#define MMAL_COLOR_SPACE_JFIF_Y16_255 MMAL_FOURCC('Y', 'Y', '1', '6')
  19254. +/* @} MmalColorSpace List */
  19255. +
  19256. +#endif /* MMAL_ENCODINGS_H */
  19257. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg-common.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h
  19258. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg-common.h 1970-01-01 01:00:00.000000000 +0100
  19259. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg-common.h 2014-04-13 17:32:57.000000000 +0200
  19260. @@ -0,0 +1,50 @@
  19261. +/*
  19262. + * Broadcom BM2835 V4L2 driver
  19263. + *
  19264. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19265. + *
  19266. + * This file is subject to the terms and conditions of the GNU General Public
  19267. + * License. See the file COPYING in the main directory of this archive
  19268. + * for more details.
  19269. + *
  19270. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19271. + * Dave Stevenson <dsteve@broadcom.com>
  19272. + * Simon Mellor <simellor@broadcom.com>
  19273. + * Luke Diamand <luked@broadcom.com>
  19274. + */
  19275. +
  19276. +#ifndef MMAL_MSG_COMMON_H
  19277. +#define MMAL_MSG_COMMON_H
  19278. +
  19279. +enum mmal_msg_status {
  19280. + MMAL_MSG_STATUS_SUCCESS = 0, /**< Success */
  19281. + MMAL_MSG_STATUS_ENOMEM, /**< Out of memory */
  19282. + MMAL_MSG_STATUS_ENOSPC, /**< Out of resources other than memory */
  19283. + MMAL_MSG_STATUS_EINVAL, /**< Argument is invalid */
  19284. + MMAL_MSG_STATUS_ENOSYS, /**< Function not implemented */
  19285. + MMAL_MSG_STATUS_ENOENT, /**< No such file or directory */
  19286. + MMAL_MSG_STATUS_ENXIO, /**< No such device or address */
  19287. + MMAL_MSG_STATUS_EIO, /**< I/O error */
  19288. + MMAL_MSG_STATUS_ESPIPE, /**< Illegal seek */
  19289. + MMAL_MSG_STATUS_ECORRUPT, /**< Data is corrupt \attention */
  19290. + MMAL_MSG_STATUS_ENOTREADY, /**< Component is not ready */
  19291. + MMAL_MSG_STATUS_ECONFIG, /**< Component is not configured */
  19292. + MMAL_MSG_STATUS_EISCONN, /**< Port is already connected */
  19293. + MMAL_MSG_STATUS_ENOTCONN, /**< Port is disconnected */
  19294. + MMAL_MSG_STATUS_EAGAIN, /**< Resource temporarily unavailable. */
  19295. + MMAL_MSG_STATUS_EFAULT, /**< Bad address */
  19296. +};
  19297. +
  19298. +struct mmal_rect {
  19299. + s32 x; /**< x coordinate (from left) */
  19300. + s32 y; /**< y coordinate (from top) */
  19301. + s32 width; /**< width */
  19302. + s32 height; /**< height */
  19303. +};
  19304. +
  19305. +struct mmal_rational {
  19306. + s32 num; /**< Numerator */
  19307. + s32 den; /**< Denominator */
  19308. +};
  19309. +
  19310. +#endif /* MMAL_MSG_COMMON_H */
  19311. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg-format.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h
  19312. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg-format.h 1970-01-01 01:00:00.000000000 +0100
  19313. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg-format.h 2014-04-13 17:32:57.000000000 +0200
  19314. @@ -0,0 +1,81 @@
  19315. +/*
  19316. + * Broadcom BM2835 V4L2 driver
  19317. + *
  19318. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19319. + *
  19320. + * This file is subject to the terms and conditions of the GNU General Public
  19321. + * License. See the file COPYING in the main directory of this archive
  19322. + * for more details.
  19323. + *
  19324. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19325. + * Dave Stevenson <dsteve@broadcom.com>
  19326. + * Simon Mellor <simellor@broadcom.com>
  19327. + * Luke Diamand <luked@broadcom.com>
  19328. + */
  19329. +
  19330. +#ifndef MMAL_MSG_FORMAT_H
  19331. +#define MMAL_MSG_FORMAT_H
  19332. +
  19333. +#include "mmal-msg-common.h"
  19334. +
  19335. +/* MMAL_ES_FORMAT_T */
  19336. +
  19337. +
  19338. +struct mmal_audio_format {
  19339. + u32 channels; /**< Number of audio channels */
  19340. + u32 sample_rate; /**< Sample rate */
  19341. +
  19342. + u32 bits_per_sample; /**< Bits per sample */
  19343. + u32 block_align; /**< Size of a block of data */
  19344. +};
  19345. +
  19346. +struct mmal_video_format {
  19347. + u32 width; /**< Width of frame in pixels */
  19348. + u32 height; /**< Height of frame in rows of pixels */
  19349. + struct mmal_rect crop; /**< Visible region of the frame */
  19350. + struct mmal_rational frame_rate; /**< Frame rate */
  19351. + struct mmal_rational par; /**< Pixel aspect ratio */
  19352. +
  19353. + /* FourCC specifying the color space of the video stream. See the
  19354. + * \ref MmalColorSpace "pre-defined color spaces" for some examples.
  19355. + */
  19356. + u32 color_space;
  19357. +};
  19358. +
  19359. +struct mmal_subpicture_format {
  19360. + u32 x_offset;
  19361. + u32 y_offset;
  19362. +};
  19363. +
  19364. +union mmal_es_specific_format {
  19365. + struct mmal_audio_format audio;
  19366. + struct mmal_video_format video;
  19367. + struct mmal_subpicture_format subpicture;
  19368. +};
  19369. +
  19370. +/** Definition of an elementary stream format (MMAL_ES_FORMAT_T) */
  19371. +struct mmal_es_format {
  19372. + u32 type; /* enum mmal_es_type */
  19373. +
  19374. + u32 encoding; /* FourCC specifying encoding of the elementary stream.*/
  19375. + u32 encoding_variant; /* FourCC specifying the specific
  19376. + * encoding variant of the elementary
  19377. + * stream.
  19378. + */
  19379. +
  19380. + union mmal_es_specific_format *es; /* TODO: pointers in
  19381. + * message serialisation?!?
  19382. + */
  19383. + /* Type specific
  19384. + * information for the
  19385. + * elementary stream
  19386. + */
  19387. +
  19388. + u32 bitrate; /**< Bitrate in bits per second */
  19389. + u32 flags; /**< Flags describing properties of the elementary stream. */
  19390. +
  19391. + u32 extradata_size; /**< Size of the codec specific data */
  19392. + u8 *extradata; /**< Codec specific data */
  19393. +};
  19394. +
  19395. +#endif /* MMAL_MSG_FORMAT_H */
  19396. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg.h
  19397. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg.h 1970-01-01 01:00:00.000000000 +0100
  19398. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg.h 2014-04-13 17:32:57.000000000 +0200
  19399. @@ -0,0 +1,404 @@
  19400. +/*
  19401. + * Broadcom BM2835 V4L2 driver
  19402. + *
  19403. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19404. + *
  19405. + * This file is subject to the terms and conditions of the GNU General Public
  19406. + * License. See the file COPYING in the main directory of this archive
  19407. + * for more details.
  19408. + *
  19409. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19410. + * Dave Stevenson <dsteve@broadcom.com>
  19411. + * Simon Mellor <simellor@broadcom.com>
  19412. + * Luke Diamand <luked@broadcom.com>
  19413. + */
  19414. +
  19415. +/* all the data structures which serialise the MMAL protocol. note
  19416. + * these are directly mapped onto the recived message data.
  19417. + *
  19418. + * BEWARE: They seem to *assume* pointers are u32 and that there is no
  19419. + * structure padding!
  19420. + *
  19421. + * NOTE: this implementation uses kernel types to ensure sizes. Rather
  19422. + * than assigning values to enums to force their size the
  19423. + * implementation uses fixed size types and not the enums (though the
  19424. + * comments have the actual enum type
  19425. + */
  19426. +
  19427. +#define VC_MMAL_VER 15
  19428. +#define VC_MMAL_MIN_VER 10
  19429. +#define VC_MMAL_SERVER_NAME MAKE_FOURCC("mmal")
  19430. +
  19431. +/* max total message size is 512 bytes */
  19432. +#define MMAL_MSG_MAX_SIZE 512
  19433. +/* with six 32bit header elements max payload is therefore 488 bytes */
  19434. +#define MMAL_MSG_MAX_PAYLOAD 488
  19435. +
  19436. +#include "mmal-msg-common.h"
  19437. +#include "mmal-msg-format.h"
  19438. +#include "mmal-msg-port.h"
  19439. +
  19440. +enum mmal_msg_type {
  19441. + MMAL_MSG_TYPE_QUIT = 1,
  19442. + MMAL_MSG_TYPE_SERVICE_CLOSED,
  19443. + MMAL_MSG_TYPE_GET_VERSION,
  19444. + MMAL_MSG_TYPE_COMPONENT_CREATE,
  19445. + MMAL_MSG_TYPE_COMPONENT_DESTROY, /* 5 */
  19446. + MMAL_MSG_TYPE_COMPONENT_ENABLE,
  19447. + MMAL_MSG_TYPE_COMPONENT_DISABLE,
  19448. + MMAL_MSG_TYPE_PORT_INFO_GET,
  19449. + MMAL_MSG_TYPE_PORT_INFO_SET,
  19450. + MMAL_MSG_TYPE_PORT_ACTION, /* 10 */
  19451. + MMAL_MSG_TYPE_BUFFER_FROM_HOST,
  19452. + MMAL_MSG_TYPE_BUFFER_TO_HOST,
  19453. + MMAL_MSG_TYPE_GET_STATS,
  19454. + MMAL_MSG_TYPE_PORT_PARAMETER_SET,
  19455. + MMAL_MSG_TYPE_PORT_PARAMETER_GET, /* 15 */
  19456. + MMAL_MSG_TYPE_EVENT_TO_HOST,
  19457. + MMAL_MSG_TYPE_GET_CORE_STATS_FOR_PORT,
  19458. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR,
  19459. + MMAL_MSG_TYPE_CONSUME_MEM,
  19460. + MMAL_MSG_TYPE_LMK, /* 20 */
  19461. + MMAL_MSG_TYPE_OPAQUE_ALLOCATOR_DESC,
  19462. + MMAL_MSG_TYPE_DRM_GET_LHS32,
  19463. + MMAL_MSG_TYPE_DRM_GET_TIME,
  19464. + MMAL_MSG_TYPE_BUFFER_FROM_HOST_ZEROLEN,
  19465. + MMAL_MSG_TYPE_PORT_FLUSH, /* 25 */
  19466. + MMAL_MSG_TYPE_HOST_LOG,
  19467. + MMAL_MSG_TYPE_MSG_LAST
  19468. +};
  19469. +
  19470. +/* port action request messages differ depending on the action type */
  19471. +enum mmal_msg_port_action_type {
  19472. + MMAL_MSG_PORT_ACTION_TYPE_UNKNOWN = 0, /* Unkown action */
  19473. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE, /* Enable a port */
  19474. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE, /* Disable a port */
  19475. + MMAL_MSG_PORT_ACTION_TYPE_FLUSH, /* Flush a port */
  19476. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT, /* Connect ports */
  19477. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT, /* Disconnect ports */
  19478. + MMAL_MSG_PORT_ACTION_TYPE_SET_REQUIREMENTS, /* Set buffer requirements*/
  19479. +};
  19480. +
  19481. +struct mmal_msg_header {
  19482. + u32 magic;
  19483. + u32 type; /** enum mmal_msg_type */
  19484. +
  19485. + /* Opaque handle to the control service */
  19486. + struct mmal_control_service *control_service;
  19487. +
  19488. + struct mmal_msg_context *context; /** a u32 per message context */
  19489. + u32 status; /** The status of the vchiq operation */
  19490. + u32 padding;
  19491. +};
  19492. +
  19493. +/* Send from VC to host to report version */
  19494. +struct mmal_msg_version {
  19495. + u32 flags;
  19496. + u32 major;
  19497. + u32 minor;
  19498. + u32 minimum;
  19499. +};
  19500. +
  19501. +/* request to VC to create component */
  19502. +struct mmal_msg_component_create {
  19503. + void *client_component; /* component context */
  19504. + char name[128];
  19505. + u32 pid; /* For debug */
  19506. +};
  19507. +
  19508. +/* reply from VC to component creation request */
  19509. +struct mmal_msg_component_create_reply {
  19510. + u32 status; /** enum mmal_msg_status - how does this differ to
  19511. + * the one in the header?
  19512. + */
  19513. + u32 component_handle; /* VideoCore handle for component */
  19514. + u32 input_num; /* Number of input ports */
  19515. + u32 output_num; /* Number of output ports */
  19516. + u32 clock_num; /* Number of clock ports */
  19517. +};
  19518. +
  19519. +/* request to VC to destroy a component */
  19520. +struct mmal_msg_component_destroy {
  19521. + u32 component_handle;
  19522. +};
  19523. +
  19524. +struct mmal_msg_component_destroy_reply {
  19525. + u32 status; /** The component destruction status */
  19526. +};
  19527. +
  19528. +
  19529. +/* request and reply to VC to enable a component */
  19530. +struct mmal_msg_component_enable {
  19531. + u32 component_handle;
  19532. +};
  19533. +
  19534. +struct mmal_msg_component_enable_reply {
  19535. + u32 status; /** The component enable status */
  19536. +};
  19537. +
  19538. +
  19539. +/* request and reply to VC to disable a component */
  19540. +struct mmal_msg_component_disable {
  19541. + u32 component_handle;
  19542. +};
  19543. +
  19544. +struct mmal_msg_component_disable_reply {
  19545. + u32 status; /** The component disable status */
  19546. +};
  19547. +
  19548. +/* request to VC to get port information */
  19549. +struct mmal_msg_port_info_get {
  19550. + u32 component_handle; /* component handle port is associated with */
  19551. + u32 port_type; /* enum mmal_msg_port_type */
  19552. + u32 index; /* port index to query */
  19553. +};
  19554. +
  19555. +/* reply from VC to get port info request */
  19556. +struct mmal_msg_port_info_get_reply {
  19557. + u32 status; /** enum mmal_msg_status */
  19558. + u32 component_handle; /* component handle port is associated with */
  19559. + u32 port_type; /* enum mmal_msg_port_type */
  19560. + u32 port_index; /* port indexed in query */
  19561. + s32 found; /* unused */
  19562. + u32 port_handle; /**< Handle to use for this port */
  19563. + struct mmal_port port;
  19564. + struct mmal_es_format format; /* elementry stream format */
  19565. + union mmal_es_specific_format es; /* es type specific data */
  19566. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE]; /* es extra data */
  19567. +};
  19568. +
  19569. +/* request to VC to set port information */
  19570. +struct mmal_msg_port_info_set {
  19571. + u32 component_handle;
  19572. + u32 port_type; /* enum mmal_msg_port_type */
  19573. + u32 port_index; /* port indexed in query */
  19574. + struct mmal_port port;
  19575. + struct mmal_es_format format;
  19576. + union mmal_es_specific_format es;
  19577. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  19578. +};
  19579. +
  19580. +/* reply from VC to port info set request */
  19581. +struct mmal_msg_port_info_set_reply {
  19582. + u32 status;
  19583. + u32 component_handle; /* component handle port is associated with */
  19584. + u32 port_type; /* enum mmal_msg_port_type */
  19585. + u32 index; /* port indexed in query */
  19586. + s32 found; /* unused */
  19587. + u32 port_handle; /**< Handle to use for this port */
  19588. + struct mmal_port port;
  19589. + struct mmal_es_format format;
  19590. + union mmal_es_specific_format es;
  19591. + u8 extradata[MMAL_FORMAT_EXTRADATA_MAX_SIZE];
  19592. +};
  19593. +
  19594. +
  19595. +/* port action requests that take a mmal_port as a parameter */
  19596. +struct mmal_msg_port_action_port {
  19597. + u32 component_handle;
  19598. + u32 port_handle;
  19599. + u32 action; /* enum mmal_msg_port_action_type */
  19600. + struct mmal_port port;
  19601. +};
  19602. +
  19603. +/* port action requests that take handles as a parameter */
  19604. +struct mmal_msg_port_action_handle {
  19605. + u32 component_handle;
  19606. + u32 port_handle;
  19607. + u32 action; /* enum mmal_msg_port_action_type */
  19608. + u32 connect_component_handle;
  19609. + u32 connect_port_handle;
  19610. +};
  19611. +
  19612. +struct mmal_msg_port_action_reply {
  19613. + u32 status; /** The port action operation status */
  19614. +};
  19615. +
  19616. +
  19617. +
  19618. +
  19619. +/* MMAL buffer transfer */
  19620. +
  19621. +/** Size of space reserved in a buffer message for short messages. */
  19622. +#define MMAL_VC_SHORT_DATA 128
  19623. +
  19624. +/** Signals that the current payload is the end of the stream of data */
  19625. +#define MMAL_BUFFER_HEADER_FLAG_EOS (1<<0)
  19626. +/** Signals that the start of the current payload starts a frame */
  19627. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_START (1<<1)
  19628. +/** Signals that the end of the current payload ends a frame */
  19629. +#define MMAL_BUFFER_HEADER_FLAG_FRAME_END (1<<2)
  19630. +/** Signals that the current payload contains only complete frames (>1) */
  19631. +#define MMAL_BUFFER_HEADER_FLAG_FRAME \
  19632. + (MMAL_BUFFER_HEADER_FLAG_FRAME_START|MMAL_BUFFER_HEADER_FLAG_FRAME_END)
  19633. +/** Signals that the current payload is a keyframe (i.e. self decodable) */
  19634. +#define MMAL_BUFFER_HEADER_FLAG_KEYFRAME (1<<3)
  19635. +/** Signals a discontinuity in the stream of data (e.g. after a seek).
  19636. + * Can be used for instance by a decoder to reset its state */
  19637. +#define MMAL_BUFFER_HEADER_FLAG_DISCONTINUITY (1<<4)
  19638. +/** Signals a buffer containing some kind of config data for the component
  19639. + * (e.g. codec config data) */
  19640. +#define MMAL_BUFFER_HEADER_FLAG_CONFIG (1<<5)
  19641. +/** Signals an encrypted payload */
  19642. +#define MMAL_BUFFER_HEADER_FLAG_ENCRYPTED (1<<6)
  19643. +/** Signals a buffer containing side information */
  19644. +#define MMAL_BUFFER_HEADER_FLAG_CODECSIDEINFO (1<<7)
  19645. +/** Signals a buffer which is the snapshot/postview image from a stills
  19646. + * capture
  19647. + */
  19648. +#define MMAL_BUFFER_HEADER_FLAGS_SNAPSHOT (1<<8)
  19649. +/** Signals a buffer which contains data known to be corrupted */
  19650. +#define MMAL_BUFFER_HEADER_FLAG_CORRUPTED (1<<9)
  19651. +/** Signals that a buffer failed to be transmitted */
  19652. +#define MMAL_BUFFER_HEADER_FLAG_TRANSMISSION_FAILED (1<<10)
  19653. +
  19654. +struct mmal_driver_buffer {
  19655. + u32 magic;
  19656. + u32 component_handle;
  19657. + u32 port_handle;
  19658. + void *client_context;
  19659. +};
  19660. +
  19661. +/* buffer header */
  19662. +struct mmal_buffer_header {
  19663. + struct mmal_buffer_header *next; /* next header */
  19664. + void *priv; /* framework private data */
  19665. + u32 cmd;
  19666. + void *data;
  19667. + u32 alloc_size;
  19668. + u32 length;
  19669. + u32 offset;
  19670. + u32 flags;
  19671. + s64 pts;
  19672. + s64 dts;
  19673. + void *type;
  19674. + void *user_data;
  19675. +};
  19676. +
  19677. +struct mmal_buffer_header_type_specific {
  19678. + union {
  19679. + struct {
  19680. + u32 planes;
  19681. + u32 offset[4];
  19682. + u32 pitch[4];
  19683. + u32 flags;
  19684. + } video;
  19685. + } u;
  19686. +};
  19687. +
  19688. +struct mmal_msg_buffer_from_host {
  19689. + /* The front 32 bytes of the buffer header are copied
  19690. + * back to us in the reply to allow for context. This
  19691. + * area is used to store two mmal_driver_buffer structures to
  19692. + * allow for multiple concurrent service users.
  19693. + */
  19694. + /* control data */
  19695. + struct mmal_driver_buffer drvbuf;
  19696. +
  19697. + /* referenced control data for passthrough buffer management */
  19698. + struct mmal_driver_buffer drvbuf_ref;
  19699. + struct mmal_buffer_header buffer_header; /* buffer header itself */
  19700. + struct mmal_buffer_header_type_specific buffer_header_type_specific;
  19701. + s32 is_zero_copy;
  19702. + s32 has_reference;
  19703. +
  19704. + /** allows short data to be xfered in control message */
  19705. + u32 payload_in_message;
  19706. + u8 short_data[MMAL_VC_SHORT_DATA];
  19707. +};
  19708. +
  19709. +
  19710. +/* port parameter setting */
  19711. +
  19712. +#define MMAL_WORKER_PORT_PARAMETER_SPACE 96
  19713. +
  19714. +struct mmal_msg_port_parameter_set {
  19715. + u32 component_handle; /* component */
  19716. + u32 port_handle; /* port */
  19717. + u32 id; /* Parameter ID */
  19718. + u32 size; /* Parameter size */
  19719. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  19720. +};
  19721. +
  19722. +struct mmal_msg_port_parameter_set_reply {
  19723. + u32 status; /** enum mmal_msg_status todo: how does this
  19724. + * differ to the one in the header?
  19725. + */
  19726. +};
  19727. +
  19728. +/* port parameter getting */
  19729. +
  19730. +struct mmal_msg_port_parameter_get {
  19731. + u32 component_handle; /* component */
  19732. + u32 port_handle; /* port */
  19733. + u32 id; /* Parameter ID */
  19734. + u32 size; /* Parameter size */
  19735. +};
  19736. +
  19737. +struct mmal_msg_port_parameter_get_reply {
  19738. + u32 status; /* Status of mmal_port_parameter_get call */
  19739. + u32 id; /* Parameter ID */
  19740. + u32 size; /* Parameter size */
  19741. + uint32_t value[MMAL_WORKER_PORT_PARAMETER_SPACE];
  19742. +};
  19743. +
  19744. +/* event messages */
  19745. +#define MMAL_WORKER_EVENT_SPACE 256
  19746. +
  19747. +struct mmal_msg_event_to_host {
  19748. + void *client_component; /* component context */
  19749. +
  19750. + u32 port_type;
  19751. + u32 port_num;
  19752. +
  19753. + u32 cmd;
  19754. + u32 length;
  19755. + u8 data[MMAL_WORKER_EVENT_SPACE];
  19756. + struct mmal_buffer_header *delayed_buffer;
  19757. +};
  19758. +
  19759. +/* all mmal messages are serialised through this structure */
  19760. +struct mmal_msg {
  19761. + /* header */
  19762. + struct mmal_msg_header h;
  19763. + /* payload */
  19764. + union {
  19765. + struct mmal_msg_version version;
  19766. +
  19767. + struct mmal_msg_component_create component_create;
  19768. + struct mmal_msg_component_create_reply component_create_reply;
  19769. +
  19770. + struct mmal_msg_component_destroy component_destroy;
  19771. + struct mmal_msg_component_destroy_reply component_destroy_reply;
  19772. +
  19773. + struct mmal_msg_component_enable component_enable;
  19774. + struct mmal_msg_component_enable_reply component_enable_reply;
  19775. +
  19776. + struct mmal_msg_component_disable component_disable;
  19777. + struct mmal_msg_component_disable_reply component_disable_reply;
  19778. +
  19779. + struct mmal_msg_port_info_get port_info_get;
  19780. + struct mmal_msg_port_info_get_reply port_info_get_reply;
  19781. +
  19782. + struct mmal_msg_port_info_set port_info_set;
  19783. + struct mmal_msg_port_info_set_reply port_info_set_reply;
  19784. +
  19785. + struct mmal_msg_port_action_port port_action_port;
  19786. + struct mmal_msg_port_action_handle port_action_handle;
  19787. + struct mmal_msg_port_action_reply port_action_reply;
  19788. +
  19789. + struct mmal_msg_buffer_from_host buffer_from_host;
  19790. +
  19791. + struct mmal_msg_port_parameter_set port_parameter_set;
  19792. + struct mmal_msg_port_parameter_set_reply
  19793. + port_parameter_set_reply;
  19794. + struct mmal_msg_port_parameter_get
  19795. + port_parameter_get;
  19796. + struct mmal_msg_port_parameter_get_reply
  19797. + port_parameter_get_reply;
  19798. +
  19799. + struct mmal_msg_event_to_host event_to_host;
  19800. +
  19801. + u8 payload[MMAL_MSG_MAX_PAYLOAD];
  19802. + } u;
  19803. +};
  19804. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg-port.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h
  19805. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-msg-port.h 1970-01-01 01:00:00.000000000 +0100
  19806. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-msg-port.h 2014-04-13 17:32:57.000000000 +0200
  19807. @@ -0,0 +1,107 @@
  19808. +/*
  19809. + * Broadcom BM2835 V4L2 driver
  19810. + *
  19811. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19812. + *
  19813. + * This file is subject to the terms and conditions of the GNU General Public
  19814. + * License. See the file COPYING in the main directory of this archive
  19815. + * for more details.
  19816. + *
  19817. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19818. + * Dave Stevenson <dsteve@broadcom.com>
  19819. + * Simon Mellor <simellor@broadcom.com>
  19820. + * Luke Diamand <luked@broadcom.com>
  19821. + */
  19822. +
  19823. +/* MMAL_PORT_TYPE_T */
  19824. +enum mmal_port_type {
  19825. + MMAL_PORT_TYPE_UNKNOWN = 0, /**< Unknown port type */
  19826. + MMAL_PORT_TYPE_CONTROL, /**< Control port */
  19827. + MMAL_PORT_TYPE_INPUT, /**< Input port */
  19828. + MMAL_PORT_TYPE_OUTPUT, /**< Output port */
  19829. + MMAL_PORT_TYPE_CLOCK, /**< Clock port */
  19830. +};
  19831. +
  19832. +/** The port is pass-through and doesn't need buffer headers allocated */
  19833. +#define MMAL_PORT_CAPABILITY_PASSTHROUGH 0x01
  19834. +/** The port wants to allocate the buffer payloads.
  19835. + * This signals a preference that payload allocation should be done
  19836. + * on this port for efficiency reasons. */
  19837. +#define MMAL_PORT_CAPABILITY_ALLOCATION 0x02
  19838. +/** The port supports format change events.
  19839. + * This applies to input ports and is used to let the client know
  19840. + * whether the port supports being reconfigured via a format
  19841. + * change event (i.e. without having to disable the port). */
  19842. +#define MMAL_PORT_CAPABILITY_SUPPORTS_EVENT_FORMAT_CHANGE 0x04
  19843. +
  19844. +/* mmal port structure (MMAL_PORT_T)
  19845. + *
  19846. + * most elements are informational only, the pointer values for
  19847. + * interogation messages are generally provided as additional
  19848. + * strucures within the message. When used to set values only teh
  19849. + * buffer_num, buffer_size and userdata parameters are writable.
  19850. + */
  19851. +struct mmal_port {
  19852. + void *priv; /* Private member used by the framework */
  19853. + const char *name; /* Port name. Used for debugging purposes (RO) */
  19854. +
  19855. + u32 type; /* Type of the port (RO) enum mmal_port_type */
  19856. + u16 index; /* Index of the port in its type list (RO) */
  19857. + u16 index_all; /* Index of the port in the list of all ports (RO) */
  19858. +
  19859. + u32 is_enabled; /* Indicates whether the port is enabled or not (RO) */
  19860. + struct mmal_es_format *format; /* Format of the elementary stream */
  19861. +
  19862. + u32 buffer_num_min; /* Minimum number of buffers the port
  19863. + * requires (RO). This is set by the
  19864. + * component.
  19865. + */
  19866. +
  19867. + u32 buffer_size_min; /* Minimum size of buffers the port
  19868. + * requires (RO). This is set by the
  19869. + * component.
  19870. + */
  19871. +
  19872. + u32 buffer_alignment_min; /* Minimum alignment requirement for
  19873. + * the buffers (RO). A value of
  19874. + * zero means no special alignment
  19875. + * requirements. This is set by the
  19876. + * component.
  19877. + */
  19878. +
  19879. + u32 buffer_num_recommended; /* Number of buffers the port
  19880. + * recommends for optimal
  19881. + * performance (RO). A value of
  19882. + * zero means no special
  19883. + * recommendation. This is set
  19884. + * by the component.
  19885. + */
  19886. +
  19887. + u32 buffer_size_recommended; /* Size of buffers the port
  19888. + * recommends for optimal
  19889. + * performance (RO). A value of
  19890. + * zero means no special
  19891. + * recommendation. This is set
  19892. + * by the component.
  19893. + */
  19894. +
  19895. + u32 buffer_num; /* Actual number of buffers the port will use.
  19896. + * This is set by the client.
  19897. + */
  19898. +
  19899. + u32 buffer_size; /* Actual maximum size of the buffers that
  19900. + * will be sent to the port. This is set by
  19901. + * the client.
  19902. + */
  19903. +
  19904. + void *component; /* Component this port belongs to (Read Only) */
  19905. +
  19906. + void *userdata; /* Field reserved for use by the client */
  19907. +
  19908. + u32 capabilities; /* Flags describing the capabilities of a
  19909. + * port (RO). Bitwise combination of \ref
  19910. + * portcapabilities "Port capabilities"
  19911. + * values.
  19912. + */
  19913. +
  19914. +};
  19915. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-parameters.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-parameters.h
  19916. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-parameters.h 1970-01-01 01:00:00.000000000 +0100
  19917. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-parameters.h 2014-09-14 19:03:24.000000000 +0200
  19918. @@ -0,0 +1,656 @@
  19919. +/*
  19920. + * Broadcom BM2835 V4L2 driver
  19921. + *
  19922. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  19923. + *
  19924. + * This file is subject to the terms and conditions of the GNU General Public
  19925. + * License. See the file COPYING in the main directory of this archive
  19926. + * for more details.
  19927. + *
  19928. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  19929. + * Dave Stevenson <dsteve@broadcom.com>
  19930. + * Simon Mellor <simellor@broadcom.com>
  19931. + * Luke Diamand <luked@broadcom.com>
  19932. + */
  19933. +
  19934. +/* common parameters */
  19935. +
  19936. +/** @name Parameter groups
  19937. + * Parameters are divided into groups, and then allocated sequentially within
  19938. + * a group using an enum.
  19939. + * @{
  19940. + */
  19941. +
  19942. +/** Common parameter ID group, used with many types of component. */
  19943. +#define MMAL_PARAMETER_GROUP_COMMON (0<<16)
  19944. +/** Camera-specific parameter ID group. */
  19945. +#define MMAL_PARAMETER_GROUP_CAMERA (1<<16)
  19946. +/** Video-specific parameter ID group. */
  19947. +#define MMAL_PARAMETER_GROUP_VIDEO (2<<16)
  19948. +/** Audio-specific parameter ID group. */
  19949. +#define MMAL_PARAMETER_GROUP_AUDIO (3<<16)
  19950. +/** Clock-specific parameter ID group. */
  19951. +#define MMAL_PARAMETER_GROUP_CLOCK (4<<16)
  19952. +/** Miracast-specific parameter ID group. */
  19953. +#define MMAL_PARAMETER_GROUP_MIRACAST (5<<16)
  19954. +
  19955. +/* Common parameters */
  19956. +enum mmal_parameter_common_type {
  19957. + MMAL_PARAMETER_UNUSED /**< Never a valid parameter ID */
  19958. + = MMAL_PARAMETER_GROUP_COMMON,
  19959. + MMAL_PARAMETER_SUPPORTED_ENCODINGS, /**< MMAL_PARAMETER_ENCODING_T */
  19960. + MMAL_PARAMETER_URI, /**< MMAL_PARAMETER_URI_T */
  19961. +
  19962. + /** MMAL_PARAMETER_CHANGE_EVENT_REQUEST_T */
  19963. + MMAL_PARAMETER_CHANGE_EVENT_REQUEST,
  19964. +
  19965. + /** MMAL_PARAMETER_BOOLEAN_T */
  19966. + MMAL_PARAMETER_ZERO_COPY,
  19967. +
  19968. + /**< MMAL_PARAMETER_BUFFER_REQUIREMENTS_T */
  19969. + MMAL_PARAMETER_BUFFER_REQUIREMENTS,
  19970. +
  19971. + MMAL_PARAMETER_STATISTICS, /**< MMAL_PARAMETER_STATISTICS_T */
  19972. + MMAL_PARAMETER_CORE_STATISTICS, /**< MMAL_PARAMETER_CORE_STATISTICS_T */
  19973. + MMAL_PARAMETER_MEM_USAGE, /**< MMAL_PARAMETER_MEM_USAGE_T */
  19974. + MMAL_PARAMETER_BUFFER_FLAG_FILTER, /**< MMAL_PARAMETER_UINT32_T */
  19975. + MMAL_PARAMETER_SEEK, /**< MMAL_PARAMETER_SEEK_T */
  19976. + MMAL_PARAMETER_POWERMON_ENABLE, /**< MMAL_PARAMETER_BOOLEAN_T */
  19977. + MMAL_PARAMETER_LOGGING, /**< MMAL_PARAMETER_LOGGING_T */
  19978. + MMAL_PARAMETER_SYSTEM_TIME, /**< MMAL_PARAMETER_UINT64_T */
  19979. + MMAL_PARAMETER_NO_IMAGE_PADDING /**< MMAL_PARAMETER_BOOLEAN_T */
  19980. +};
  19981. +
  19982. +/* camera parameters */
  19983. +
  19984. +enum mmal_parameter_camera_type {
  19985. + /* 0 */
  19986. + /** @ref MMAL_PARAMETER_THUMBNAIL_CONFIG_T */
  19987. + MMAL_PARAMETER_THUMBNAIL_CONFIGURATION
  19988. + = MMAL_PARAMETER_GROUP_CAMERA,
  19989. + MMAL_PARAMETER_CAPTURE_QUALITY, /**< Unused? */
  19990. + MMAL_PARAMETER_ROTATION, /**< @ref MMAL_PARAMETER_INT32_T */
  19991. + MMAL_PARAMETER_EXIF_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  19992. + MMAL_PARAMETER_EXIF, /**< @ref MMAL_PARAMETER_EXIF_T */
  19993. + MMAL_PARAMETER_AWB_MODE, /**< @ref MMAL_PARAM_AWBMODE_T */
  19994. + MMAL_PARAMETER_IMAGE_EFFECT, /**< @ref MMAL_PARAMETER_IMAGEFX_T */
  19995. + MMAL_PARAMETER_COLOUR_EFFECT, /**< @ref MMAL_PARAMETER_COLOURFX_T */
  19996. + MMAL_PARAMETER_FLICKER_AVOID, /**< @ref MMAL_PARAMETER_FLICKERAVOID_T */
  19997. + MMAL_PARAMETER_FLASH, /**< @ref MMAL_PARAMETER_FLASH_T */
  19998. + MMAL_PARAMETER_REDEYE, /**< @ref MMAL_PARAMETER_REDEYE_T */
  19999. + MMAL_PARAMETER_FOCUS, /**< @ref MMAL_PARAMETER_FOCUS_T */
  20000. + MMAL_PARAMETER_FOCAL_LENGTHS, /**< Unused? */
  20001. + MMAL_PARAMETER_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  20002. + MMAL_PARAMETER_ZOOM, /**< @ref MMAL_PARAMETER_SCALEFACTOR_T */
  20003. + MMAL_PARAMETER_MIRROR, /**< @ref MMAL_PARAMETER_MIRROR_T */
  20004. +
  20005. + /* 0x10 */
  20006. + MMAL_PARAMETER_CAMERA_NUM, /**< @ref MMAL_PARAMETER_UINT32_T */
  20007. + MMAL_PARAMETER_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20008. + MMAL_PARAMETER_EXPOSURE_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMODE_T */
  20009. + MMAL_PARAMETER_EXP_METERING_MODE, /**< @ref MMAL_PARAMETER_EXPOSUREMETERINGMODE_T */
  20010. + MMAL_PARAMETER_FOCUS_STATUS, /**< @ref MMAL_PARAMETER_FOCUS_STATUS_T */
  20011. + MMAL_PARAMETER_CAMERA_CONFIG, /**< @ref MMAL_PARAMETER_CAMERA_CONFIG_T */
  20012. + MMAL_PARAMETER_CAPTURE_STATUS, /**< @ref MMAL_PARAMETER_CAPTURE_STATUS_T */
  20013. + MMAL_PARAMETER_FACE_TRACK, /**< @ref MMAL_PARAMETER_FACE_TRACK_T */
  20014. + MMAL_PARAMETER_DRAW_BOX_FACES_AND_FOCUS, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20015. + MMAL_PARAMETER_JPEG_Q_FACTOR, /**< @ref MMAL_PARAMETER_UINT32_T */
  20016. + MMAL_PARAMETER_FRAME_RATE, /**< @ref MMAL_PARAMETER_FRAME_RATE_T */
  20017. + MMAL_PARAMETER_USE_STC, /**< @ref MMAL_PARAMETER_CAMERA_STC_MODE_T */
  20018. + MMAL_PARAMETER_CAMERA_INFO, /**< @ref MMAL_PARAMETER_CAMERA_INFO_T */
  20019. + MMAL_PARAMETER_VIDEO_STABILISATION, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20020. + MMAL_PARAMETER_FACE_TRACK_RESULTS, /**< @ref MMAL_PARAMETER_FACE_TRACK_RESULTS_T */
  20021. + MMAL_PARAMETER_ENABLE_RAW_CAPTURE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20022. +
  20023. + /* 0x20 */
  20024. + MMAL_PARAMETER_DPF_FILE, /**< @ref MMAL_PARAMETER_URI_T */
  20025. + MMAL_PARAMETER_ENABLE_DPF_FILE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20026. + MMAL_PARAMETER_DPF_FAIL_IS_FATAL, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20027. + MMAL_PARAMETER_CAPTURE_MODE, /**< @ref MMAL_PARAMETER_CAPTUREMODE_T */
  20028. + MMAL_PARAMETER_FOCUS_REGIONS, /**< @ref MMAL_PARAMETER_FOCUS_REGIONS_T */
  20029. + MMAL_PARAMETER_INPUT_CROP, /**< @ref MMAL_PARAMETER_INPUT_CROP_T */
  20030. + MMAL_PARAMETER_SENSOR_INFORMATION, /**< @ref MMAL_PARAMETER_SENSOR_INFORMATION_T */
  20031. + MMAL_PARAMETER_FLASH_SELECT, /**< @ref MMAL_PARAMETER_FLASH_SELECT_T */
  20032. + MMAL_PARAMETER_FIELD_OF_VIEW, /**< @ref MMAL_PARAMETER_FIELD_OF_VIEW_T */
  20033. + MMAL_PARAMETER_HIGH_DYNAMIC_RANGE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20034. + MMAL_PARAMETER_DYNAMIC_RANGE_COMPRESSION, /**< @ref MMAL_PARAMETER_DRC_T */
  20035. + MMAL_PARAMETER_ALGORITHM_CONTROL, /**< @ref MMAL_PARAMETER_ALGORITHM_CONTROL_T */
  20036. + MMAL_PARAMETER_SHARPNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20037. + MMAL_PARAMETER_CONTRAST, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20038. + MMAL_PARAMETER_BRIGHTNESS, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20039. + MMAL_PARAMETER_SATURATION, /**< @ref MMAL_PARAMETER_RATIONAL_T */
  20040. +
  20041. + /* 0x30 */
  20042. + MMAL_PARAMETER_ISO, /**< @ref MMAL_PARAMETER_UINT32_T */
  20043. + MMAL_PARAMETER_ANTISHAKE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20044. +
  20045. + /** @ref MMAL_PARAMETER_IMAGEFX_PARAMETERS_T */
  20046. + MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,
  20047. +
  20048. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20049. + MMAL_PARAMETER_CAMERA_BURST_CAPTURE,
  20050. +
  20051. + /** @ref MMAL_PARAMETER_UINT32_T */
  20052. + MMAL_PARAMETER_CAMERA_MIN_ISO,
  20053. +
  20054. + /** @ref MMAL_PARAMETER_CAMERA_USE_CASE_T */
  20055. + MMAL_PARAMETER_CAMERA_USE_CASE,
  20056. +
  20057. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20058. + MMAL_PARAMETER_CAPTURE_STATS_PASS,
  20059. +
  20060. + /** @ref MMAL_PARAMETER_UINT32_T */
  20061. + MMAL_PARAMETER_CAMERA_CUSTOM_SENSOR_CONFIG,
  20062. +
  20063. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20064. + MMAL_PARAMETER_ENABLE_REGISTER_FILE,
  20065. +
  20066. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20067. + MMAL_PARAMETER_REGISTER_FAIL_IS_FATAL,
  20068. +
  20069. + /** @ref MMAL_PARAMETER_CONFIGFILE_T */
  20070. + MMAL_PARAMETER_CONFIGFILE_REGISTERS,
  20071. +
  20072. + /** @ref MMAL_PARAMETER_CONFIGFILE_CHUNK_T */
  20073. + MMAL_PARAMETER_CONFIGFILE_CHUNK_REGISTERS,
  20074. + MMAL_PARAMETER_JPEG_ATTACH_LOG, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20075. + MMAL_PARAMETER_ZERO_SHUTTER_LAG, /**< @ref MMAL_PARAMETER_ZEROSHUTTERLAG_T */
  20076. + MMAL_PARAMETER_FPS_RANGE, /**< @ref MMAL_PARAMETER_FPS_RANGE_T */
  20077. + MMAL_PARAMETER_CAPTURE_EXPOSURE_COMP, /**< @ref MMAL_PARAMETER_INT32_T */
  20078. +
  20079. + /* 0x40 */
  20080. + MMAL_PARAMETER_SW_SHARPEN_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20081. + MMAL_PARAMETER_FLASH_REQUIRED, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20082. + MMAL_PARAMETER_SW_SATURATION_DISABLE, /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20083. + MMAL_PARAMETER_SHUTTER_SPEED, /**< Takes a @ref MMAL_PARAMETER_UINT32_T */
  20084. + MMAL_PARAMETER_CUSTOM_AWB_GAINS, /**< Takes a @ref MMAL_PARAMETER_AWB_GAINS_T */
  20085. +};
  20086. +
  20087. +struct mmal_parameter_rational {
  20088. + s32 num; /**< Numerator */
  20089. + s32 den; /**< Denominator */
  20090. +};
  20091. +
  20092. +enum mmal_parameter_camera_config_timestamp_mode {
  20093. + MMAL_PARAM_TIMESTAMP_MODE_ZERO = 0, /* Always timestamp frames as 0 */
  20094. + MMAL_PARAM_TIMESTAMP_MODE_RAW_STC, /* Use the raw STC value
  20095. + * for the frame timestamp
  20096. + */
  20097. + MMAL_PARAM_TIMESTAMP_MODE_RESET_STC, /* Use the STC timestamp
  20098. + * but subtract the
  20099. + * timestamp of the first
  20100. + * frame sent to give a
  20101. + * zero based timestamp.
  20102. + */
  20103. +};
  20104. +
  20105. +struct mmal_parameter_fps_range {
  20106. + /**< Low end of the permitted framerate range */
  20107. + struct mmal_parameter_rational fps_low;
  20108. + /**< High end of the permitted framerate range */
  20109. + struct mmal_parameter_rational fps_high;
  20110. +};
  20111. +
  20112. +
  20113. +/* camera configuration parameter */
  20114. +struct mmal_parameter_camera_config {
  20115. + /* Parameters for setting up the image pools */
  20116. + u32 max_stills_w; /* Max size of stills capture */
  20117. + u32 max_stills_h;
  20118. + u32 stills_yuv422; /* Allow YUV422 stills capture */
  20119. + u32 one_shot_stills; /* Continuous or one shot stills captures. */
  20120. +
  20121. + u32 max_preview_video_w; /* Max size of the preview or video
  20122. + * capture frames
  20123. + */
  20124. + u32 max_preview_video_h;
  20125. + u32 num_preview_video_frames;
  20126. +
  20127. + /** Sets the height of the circular buffer for stills capture. */
  20128. + u32 stills_capture_circular_buffer_height;
  20129. +
  20130. + /** Allows preview/encode to resume as fast as possible after the stills
  20131. + * input frame has been received, and then processes the still frame in
  20132. + * the background whilst preview/encode has resumed.
  20133. + * Actual mode is controlled by MMAL_PARAMETER_CAPTURE_MODE.
  20134. + */
  20135. + u32 fast_preview_resume;
  20136. +
  20137. + /** Selects algorithm for timestamping frames if
  20138. + * there is no clock component connected.
  20139. + * enum mmal_parameter_camera_config_timestamp_mode
  20140. + */
  20141. + s32 use_stc_timestamp;
  20142. +};
  20143. +
  20144. +
  20145. +enum mmal_parameter_exposuremode {
  20146. + MMAL_PARAM_EXPOSUREMODE_OFF,
  20147. + MMAL_PARAM_EXPOSUREMODE_AUTO,
  20148. + MMAL_PARAM_EXPOSUREMODE_NIGHT,
  20149. + MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
  20150. + MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
  20151. + MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
  20152. + MMAL_PARAM_EXPOSUREMODE_SPORTS,
  20153. + MMAL_PARAM_EXPOSUREMODE_SNOW,
  20154. + MMAL_PARAM_EXPOSUREMODE_BEACH,
  20155. + MMAL_PARAM_EXPOSUREMODE_VERYLONG,
  20156. + MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
  20157. + MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
  20158. + MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
  20159. +};
  20160. +
  20161. +enum mmal_parameter_exposuremeteringmode {
  20162. + MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
  20163. + MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
  20164. + MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
  20165. + MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX,
  20166. +};
  20167. +
  20168. +enum mmal_parameter_awbmode {
  20169. + MMAL_PARAM_AWBMODE_OFF,
  20170. + MMAL_PARAM_AWBMODE_AUTO,
  20171. + MMAL_PARAM_AWBMODE_SUNLIGHT,
  20172. + MMAL_PARAM_AWBMODE_CLOUDY,
  20173. + MMAL_PARAM_AWBMODE_SHADE,
  20174. + MMAL_PARAM_AWBMODE_TUNGSTEN,
  20175. + MMAL_PARAM_AWBMODE_FLUORESCENT,
  20176. + MMAL_PARAM_AWBMODE_INCANDESCENT,
  20177. + MMAL_PARAM_AWBMODE_FLASH,
  20178. + MMAL_PARAM_AWBMODE_HORIZON,
  20179. +};
  20180. +
  20181. +enum mmal_parameter_imagefx {
  20182. + MMAL_PARAM_IMAGEFX_NONE,
  20183. + MMAL_PARAM_IMAGEFX_NEGATIVE,
  20184. + MMAL_PARAM_IMAGEFX_SOLARIZE,
  20185. + MMAL_PARAM_IMAGEFX_POSTERIZE,
  20186. + MMAL_PARAM_IMAGEFX_WHITEBOARD,
  20187. + MMAL_PARAM_IMAGEFX_BLACKBOARD,
  20188. + MMAL_PARAM_IMAGEFX_SKETCH,
  20189. + MMAL_PARAM_IMAGEFX_DENOISE,
  20190. + MMAL_PARAM_IMAGEFX_EMBOSS,
  20191. + MMAL_PARAM_IMAGEFX_OILPAINT,
  20192. + MMAL_PARAM_IMAGEFX_HATCH,
  20193. + MMAL_PARAM_IMAGEFX_GPEN,
  20194. + MMAL_PARAM_IMAGEFX_PASTEL,
  20195. + MMAL_PARAM_IMAGEFX_WATERCOLOUR,
  20196. + MMAL_PARAM_IMAGEFX_FILM,
  20197. + MMAL_PARAM_IMAGEFX_BLUR,
  20198. + MMAL_PARAM_IMAGEFX_SATURATION,
  20199. + MMAL_PARAM_IMAGEFX_COLOURSWAP,
  20200. + MMAL_PARAM_IMAGEFX_WASHEDOUT,
  20201. + MMAL_PARAM_IMAGEFX_POSTERISE,
  20202. + MMAL_PARAM_IMAGEFX_COLOURPOINT,
  20203. + MMAL_PARAM_IMAGEFX_COLOURBALANCE,
  20204. + MMAL_PARAM_IMAGEFX_CARTOON,
  20205. +};
  20206. +
  20207. +enum MMAL_PARAM_FLICKERAVOID_T {
  20208. + MMAL_PARAM_FLICKERAVOID_OFF,
  20209. + MMAL_PARAM_FLICKERAVOID_AUTO,
  20210. + MMAL_PARAM_FLICKERAVOID_50HZ,
  20211. + MMAL_PARAM_FLICKERAVOID_60HZ,
  20212. + MMAL_PARAM_FLICKERAVOID_MAX = 0x7FFFFFFF
  20213. +};
  20214. +
  20215. +struct mmal_parameter_awbgains {
  20216. + struct mmal_parameter_rational r_gain; /**< Red gain */
  20217. + struct mmal_parameter_rational b_gain; /**< Blue gain */
  20218. +};
  20219. +
  20220. +/** Manner of video rate control */
  20221. +enum mmal_parameter_rate_control_mode {
  20222. + MMAL_VIDEO_RATECONTROL_DEFAULT,
  20223. + MMAL_VIDEO_RATECONTROL_VARIABLE,
  20224. + MMAL_VIDEO_RATECONTROL_CONSTANT,
  20225. + MMAL_VIDEO_RATECONTROL_VARIABLE_SKIP_FRAMES,
  20226. + MMAL_VIDEO_RATECONTROL_CONSTANT_SKIP_FRAMES
  20227. +};
  20228. +
  20229. +enum mmal_video_profile {
  20230. + MMAL_VIDEO_PROFILE_H263_BASELINE,
  20231. + MMAL_VIDEO_PROFILE_H263_H320CODING,
  20232. + MMAL_VIDEO_PROFILE_H263_BACKWARDCOMPATIBLE,
  20233. + MMAL_VIDEO_PROFILE_H263_ISWV2,
  20234. + MMAL_VIDEO_PROFILE_H263_ISWV3,
  20235. + MMAL_VIDEO_PROFILE_H263_HIGHCOMPRESSION,
  20236. + MMAL_VIDEO_PROFILE_H263_INTERNET,
  20237. + MMAL_VIDEO_PROFILE_H263_INTERLACE,
  20238. + MMAL_VIDEO_PROFILE_H263_HIGHLATENCY,
  20239. + MMAL_VIDEO_PROFILE_MP4V_SIMPLE,
  20240. + MMAL_VIDEO_PROFILE_MP4V_SIMPLESCALABLE,
  20241. + MMAL_VIDEO_PROFILE_MP4V_CORE,
  20242. + MMAL_VIDEO_PROFILE_MP4V_MAIN,
  20243. + MMAL_VIDEO_PROFILE_MP4V_NBIT,
  20244. + MMAL_VIDEO_PROFILE_MP4V_SCALABLETEXTURE,
  20245. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFACE,
  20246. + MMAL_VIDEO_PROFILE_MP4V_SIMPLEFBA,
  20247. + MMAL_VIDEO_PROFILE_MP4V_BASICANIMATED,
  20248. + MMAL_VIDEO_PROFILE_MP4V_HYBRID,
  20249. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDREALTIME,
  20250. + MMAL_VIDEO_PROFILE_MP4V_CORESCALABLE,
  20251. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCODING,
  20252. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDCORE,
  20253. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSCALABLE,
  20254. + MMAL_VIDEO_PROFILE_MP4V_ADVANCEDSIMPLE,
  20255. + MMAL_VIDEO_PROFILE_H264_BASELINE,
  20256. + MMAL_VIDEO_PROFILE_H264_MAIN,
  20257. + MMAL_VIDEO_PROFILE_H264_EXTENDED,
  20258. + MMAL_VIDEO_PROFILE_H264_HIGH,
  20259. + MMAL_VIDEO_PROFILE_H264_HIGH10,
  20260. + MMAL_VIDEO_PROFILE_H264_HIGH422,
  20261. + MMAL_VIDEO_PROFILE_H264_HIGH444,
  20262. + MMAL_VIDEO_PROFILE_H264_CONSTRAINED_BASELINE,
  20263. + MMAL_VIDEO_PROFILE_DUMMY = 0x7FFFFFFF
  20264. +};
  20265. +
  20266. +enum mmal_video_level {
  20267. + MMAL_VIDEO_LEVEL_H263_10,
  20268. + MMAL_VIDEO_LEVEL_H263_20,
  20269. + MMAL_VIDEO_LEVEL_H263_30,
  20270. + MMAL_VIDEO_LEVEL_H263_40,
  20271. + MMAL_VIDEO_LEVEL_H263_45,
  20272. + MMAL_VIDEO_LEVEL_H263_50,
  20273. + MMAL_VIDEO_LEVEL_H263_60,
  20274. + MMAL_VIDEO_LEVEL_H263_70,
  20275. + MMAL_VIDEO_LEVEL_MP4V_0,
  20276. + MMAL_VIDEO_LEVEL_MP4V_0b,
  20277. + MMAL_VIDEO_LEVEL_MP4V_1,
  20278. + MMAL_VIDEO_LEVEL_MP4V_2,
  20279. + MMAL_VIDEO_LEVEL_MP4V_3,
  20280. + MMAL_VIDEO_LEVEL_MP4V_4,
  20281. + MMAL_VIDEO_LEVEL_MP4V_4a,
  20282. + MMAL_VIDEO_LEVEL_MP4V_5,
  20283. + MMAL_VIDEO_LEVEL_MP4V_6,
  20284. + MMAL_VIDEO_LEVEL_H264_1,
  20285. + MMAL_VIDEO_LEVEL_H264_1b,
  20286. + MMAL_VIDEO_LEVEL_H264_11,
  20287. + MMAL_VIDEO_LEVEL_H264_12,
  20288. + MMAL_VIDEO_LEVEL_H264_13,
  20289. + MMAL_VIDEO_LEVEL_H264_2,
  20290. + MMAL_VIDEO_LEVEL_H264_21,
  20291. + MMAL_VIDEO_LEVEL_H264_22,
  20292. + MMAL_VIDEO_LEVEL_H264_3,
  20293. + MMAL_VIDEO_LEVEL_H264_31,
  20294. + MMAL_VIDEO_LEVEL_H264_32,
  20295. + MMAL_VIDEO_LEVEL_H264_4,
  20296. + MMAL_VIDEO_LEVEL_H264_41,
  20297. + MMAL_VIDEO_LEVEL_H264_42,
  20298. + MMAL_VIDEO_LEVEL_H264_5,
  20299. + MMAL_VIDEO_LEVEL_H264_51,
  20300. + MMAL_VIDEO_LEVEL_DUMMY = 0x7FFFFFFF
  20301. +};
  20302. +
  20303. +struct mmal_parameter_video_profile {
  20304. + enum mmal_video_profile profile;
  20305. + enum mmal_video_level level;
  20306. +};
  20307. +
  20308. +/* video parameters */
  20309. +
  20310. +enum mmal_parameter_video_type {
  20311. + /** @ref MMAL_DISPLAYREGION_T */
  20312. + MMAL_PARAMETER_DISPLAYREGION = MMAL_PARAMETER_GROUP_VIDEO,
  20313. +
  20314. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  20315. + MMAL_PARAMETER_SUPPORTED_PROFILES,
  20316. +
  20317. + /** @ref MMAL_PARAMETER_VIDEO_PROFILE_T */
  20318. + MMAL_PARAMETER_PROFILE,
  20319. +
  20320. + /** @ref MMAL_PARAMETER_UINT32_T */
  20321. + MMAL_PARAMETER_INTRAPERIOD,
  20322. +
  20323. + /** @ref MMAL_PARAMETER_VIDEO_RATECONTROL_T */
  20324. + MMAL_PARAMETER_RATECONTROL,
  20325. +
  20326. + /** @ref MMAL_PARAMETER_VIDEO_NALUNITFORMAT_T */
  20327. + MMAL_PARAMETER_NALUNITFORMAT,
  20328. +
  20329. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20330. + MMAL_PARAMETER_MINIMISE_FRAGMENTATION,
  20331. +
  20332. + /** @ref MMAL_PARAMETER_UINT32_T.
  20333. + * Setting the value to zero resets to the default (one slice per frame).
  20334. + */
  20335. + MMAL_PARAMETER_MB_ROWS_PER_SLICE,
  20336. +
  20337. + /** @ref MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION_T */
  20338. + MMAL_PARAMETER_VIDEO_LEVEL_EXTENSION,
  20339. +
  20340. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_ENABLE_T */
  20341. + MMAL_PARAMETER_VIDEO_EEDE_ENABLE,
  20342. +
  20343. + /** @ref MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE_T */
  20344. + MMAL_PARAMETER_VIDEO_EEDE_LOSSRATE,
  20345. +
  20346. + /** @ref MMAL_PARAMETER_BOOLEAN_T. Request an I-frame. */
  20347. + MMAL_PARAMETER_VIDEO_REQUEST_I_FRAME,
  20348. + /** @ref MMAL_PARAMETER_VIDEO_INTRA_REFRESH_T */
  20349. + MMAL_PARAMETER_VIDEO_INTRA_REFRESH,
  20350. +
  20351. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  20352. + MMAL_PARAMETER_VIDEO_IMMUTABLE_INPUT,
  20353. +
  20354. + /** @ref MMAL_PARAMETER_UINT32_T. Run-time bit rate control */
  20355. + MMAL_PARAMETER_VIDEO_BIT_RATE,
  20356. +
  20357. + /** @ref MMAL_PARAMETER_FRAME_RATE_T */
  20358. + MMAL_PARAMETER_VIDEO_FRAME_RATE,
  20359. +
  20360. + /** @ref MMAL_PARAMETER_UINT32_T. */
  20361. + MMAL_PARAMETER_VIDEO_ENCODE_MIN_QUANT,
  20362. +
  20363. + /** @ref MMAL_PARAMETER_UINT32_T. */
  20364. + MMAL_PARAMETER_VIDEO_ENCODE_MAX_QUANT,
  20365. +
  20366. + /** @ref MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL_T. */
  20367. + MMAL_PARAMETER_VIDEO_ENCODE_RC_MODEL,
  20368. +
  20369. + MMAL_PARAMETER_EXTRA_BUFFERS, /**< @ref MMAL_PARAMETER_UINT32_T. */
  20370. + /** @ref MMAL_PARAMETER_UINT32_T.
  20371. + * Changing this parameter from the default can reduce frame rate
  20372. + * because image buffers need to be re-pitched.
  20373. + */
  20374. + MMAL_PARAMETER_VIDEO_ALIGN_HORIZ,
  20375. +
  20376. + /** @ref MMAL_PARAMETER_UINT32_T.
  20377. + * Changing this parameter from the default can reduce frame rate
  20378. + * because image buffers need to be re-pitched.
  20379. + */
  20380. + MMAL_PARAMETER_VIDEO_ALIGN_VERT,
  20381. +
  20382. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  20383. + MMAL_PARAMETER_VIDEO_DROPPABLE_PFRAMES,
  20384. +
  20385. + /** @ref MMAL_PARAMETER_UINT32_T. */
  20386. + MMAL_PARAMETER_VIDEO_ENCODE_INITIAL_QUANT,
  20387. +
  20388. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  20389. + MMAL_PARAMETER_VIDEO_ENCODE_QP_P,
  20390. +
  20391. + /**< @ref MMAL_PARAMETER_UINT32_T. */
  20392. + MMAL_PARAMETER_VIDEO_ENCODE_RC_SLICE_DQUANT,
  20393. +
  20394. + /** @ref MMAL_PARAMETER_UINT32_T */
  20395. + MMAL_PARAMETER_VIDEO_ENCODE_FRAME_LIMIT_BITS,
  20396. +
  20397. + /** @ref MMAL_PARAMETER_UINT32_T. */
  20398. + MMAL_PARAMETER_VIDEO_ENCODE_PEAK_RATE,
  20399. +
  20400. + /* H264 specific parameters */
  20401. +
  20402. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  20403. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DISABLE_CABAC,
  20404. +
  20405. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  20406. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_LATENCY,
  20407. +
  20408. + /** @ref MMAL_PARAMETER_BOOLEAN_T. */
  20409. + MMAL_PARAMETER_VIDEO_ENCODE_H264_AU_DELIMITERS,
  20410. +
  20411. + /** @ref MMAL_PARAMETER_UINT32_T. */
  20412. + MMAL_PARAMETER_VIDEO_ENCODE_H264_DEBLOCK_IDC,
  20413. +
  20414. + /** @ref MMAL_PARAMETER_VIDEO_ENCODER_H264_MB_INTRA_MODES_T. */
  20415. + MMAL_PARAMETER_VIDEO_ENCODE_H264_MB_INTRA_MODE,
  20416. +
  20417. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20418. + MMAL_PARAMETER_VIDEO_ENCODE_HEADER_ON_OPEN,
  20419. +
  20420. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20421. + MMAL_PARAMETER_VIDEO_ENCODE_PRECODE_FOR_QP,
  20422. +
  20423. + /** @ref MMAL_PARAMETER_VIDEO_DRM_INIT_INFO_T. */
  20424. + MMAL_PARAMETER_VIDEO_DRM_INIT_INFO,
  20425. +
  20426. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20427. + MMAL_PARAMETER_VIDEO_TIMESTAMP_FIFO,
  20428. +
  20429. + /** @ref MMAL_PARAMETER_BOOLEAN_T */
  20430. + MMAL_PARAMETER_VIDEO_DECODE_ERROR_CONCEALMENT,
  20431. +
  20432. + /** @ref MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER_T. */
  20433. + MMAL_PARAMETER_VIDEO_DRM_PROTECT_BUFFER,
  20434. +
  20435. + /** @ref MMAL_PARAMETER_BYTES_T */
  20436. + MMAL_PARAMETER_VIDEO_DECODE_CONFIG_VD3,
  20437. +
  20438. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20439. + MMAL_PARAMETER_VIDEO_ENCODE_H264_VCL_HRD_PARAMETERS,
  20440. +
  20441. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20442. + MMAL_PARAMETER_VIDEO_ENCODE_H264_LOW_DELAY_HRD_FLAG,
  20443. +
  20444. + /**< @ref MMAL_PARAMETER_BOOLEAN_T */
  20445. + MMAL_PARAMETER_VIDEO_ENCODE_INLINE_HEADER
  20446. +};
  20447. +
  20448. +/** Valid mirror modes */
  20449. +enum mmal_parameter_mirror {
  20450. + MMAL_PARAM_MIRROR_NONE,
  20451. + MMAL_PARAM_MIRROR_VERTICAL,
  20452. + MMAL_PARAM_MIRROR_HORIZONTAL,
  20453. + MMAL_PARAM_MIRROR_BOTH,
  20454. +};
  20455. +
  20456. +enum mmal_parameter_displaytransform {
  20457. + MMAL_DISPLAY_ROT0 = 0,
  20458. + MMAL_DISPLAY_MIRROR_ROT0 = 1,
  20459. + MMAL_DISPLAY_MIRROR_ROT180 = 2,
  20460. + MMAL_DISPLAY_ROT180 = 3,
  20461. + MMAL_DISPLAY_MIRROR_ROT90 = 4,
  20462. + MMAL_DISPLAY_ROT270 = 5,
  20463. + MMAL_DISPLAY_ROT90 = 6,
  20464. + MMAL_DISPLAY_MIRROR_ROT270 = 7,
  20465. +};
  20466. +
  20467. +enum mmal_parameter_displaymode {
  20468. + MMAL_DISPLAY_MODE_FILL = 0,
  20469. + MMAL_DISPLAY_MODE_LETTERBOX = 1,
  20470. +};
  20471. +
  20472. +enum mmal_parameter_displayset {
  20473. + MMAL_DISPLAY_SET_NONE = 0,
  20474. + MMAL_DISPLAY_SET_NUM = 1,
  20475. + MMAL_DISPLAY_SET_FULLSCREEN = 2,
  20476. + MMAL_DISPLAY_SET_TRANSFORM = 4,
  20477. + MMAL_DISPLAY_SET_DEST_RECT = 8,
  20478. + MMAL_DISPLAY_SET_SRC_RECT = 0x10,
  20479. + MMAL_DISPLAY_SET_MODE = 0x20,
  20480. + MMAL_DISPLAY_SET_PIXEL = 0x40,
  20481. + MMAL_DISPLAY_SET_NOASPECT = 0x80,
  20482. + MMAL_DISPLAY_SET_LAYER = 0x100,
  20483. + MMAL_DISPLAY_SET_COPYPROTECT = 0x200,
  20484. + MMAL_DISPLAY_SET_ALPHA = 0x400,
  20485. +};
  20486. +
  20487. +struct mmal_parameter_displayregion {
  20488. + /** Bitfield that indicates which fields are set and should be
  20489. + * used. All other fields will maintain their current value.
  20490. + * \ref MMAL_DISPLAYSET_T defines the bits that can be
  20491. + * combined.
  20492. + */
  20493. + u32 set;
  20494. +
  20495. + /** Describes the display output device, with 0 typically
  20496. + * being a directly connected LCD display. The actual values
  20497. + * will depend on the hardware. Code using hard-wired numbers
  20498. + * (e.g. 2) is certain to fail.
  20499. + */
  20500. +
  20501. + u32 display_num;
  20502. + /** Indicates that we are using the full device screen area,
  20503. + * rather than a window of the display. If zero, then
  20504. + * dest_rect is used to specify a region of the display to
  20505. + * use.
  20506. + */
  20507. +
  20508. + s32 fullscreen;
  20509. + /** Indicates any rotation or flipping used to map frames onto
  20510. + * the natural display orientation.
  20511. + */
  20512. + u32 transform; /* enum mmal_parameter_displaytransform */
  20513. +
  20514. + /** Where to display the frame within the screen, if
  20515. + * fullscreen is zero.
  20516. + */
  20517. + struct vchiq_mmal_rect dest_rect;
  20518. +
  20519. + /** Indicates which area of the frame to display. If all
  20520. + * values are zero, the whole frame will be used.
  20521. + */
  20522. + struct vchiq_mmal_rect src_rect;
  20523. +
  20524. + /** If set to non-zero, indicates that any display scaling
  20525. + * should disregard the aspect ratio of the frame region being
  20526. + * displayed.
  20527. + */
  20528. + s32 noaspect;
  20529. +
  20530. + /** Indicates how the image should be scaled to fit the
  20531. + * display. \code MMAL_DISPLAY_MODE_FILL \endcode indicates
  20532. + * that the image should fill the screen by potentially
  20533. + * cropping the frames. Setting \code mode \endcode to \code
  20534. + * MMAL_DISPLAY_MODE_LETTERBOX \endcode indicates that all the
  20535. + * source region should be displayed and black bars added if
  20536. + * necessary.
  20537. + */
  20538. + u32 mode; /* enum mmal_parameter_displaymode */
  20539. +
  20540. + /** If non-zero, defines the width of a source pixel relative
  20541. + * to \code pixel_y \endcode. If zero, then pixels default to
  20542. + * being square.
  20543. + */
  20544. + u32 pixel_x;
  20545. +
  20546. + /** If non-zero, defines the height of a source pixel relative
  20547. + * to \code pixel_x \endcode. If zero, then pixels default to
  20548. + * being square.
  20549. + */
  20550. + u32 pixel_y;
  20551. +
  20552. + /** Sets the relative depth of the images, with greater values
  20553. + * being in front of smaller values.
  20554. + */
  20555. + u32 layer;
  20556. +
  20557. + /** Set to non-zero to ensure copy protection is used on
  20558. + * output.
  20559. + */
  20560. + s32 copyprotect_required;
  20561. +
  20562. + /** Level of opacity of the layer, where zero is fully
  20563. + * transparent and 255 is fully opaque.
  20564. + */
  20565. + u32 alpha;
  20566. +};
  20567. +
  20568. +#define MMAL_MAX_IMAGEFX_PARAMETERS 5
  20569. +
  20570. +struct mmal_parameter_imagefx_parameters {
  20571. + enum mmal_parameter_imagefx effect;
  20572. + u32 num_effect_params;
  20573. + u32 effect_parameter[MMAL_MAX_IMAGEFX_PARAMETERS];
  20574. +};
  20575. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-vchiq.c linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c
  20576. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  20577. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-vchiq.c 2014-04-13 17:32:57.000000000 +0200
  20578. @@ -0,0 +1,1916 @@
  20579. +/*
  20580. + * Broadcom BM2835 V4L2 driver
  20581. + *
  20582. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  20583. + *
  20584. + * This file is subject to the terms and conditions of the GNU General Public
  20585. + * License. See the file COPYING in the main directory of this archive
  20586. + * for more details.
  20587. + *
  20588. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  20589. + * Dave Stevenson <dsteve@broadcom.com>
  20590. + * Simon Mellor <simellor@broadcom.com>
  20591. + * Luke Diamand <luked@broadcom.com>
  20592. + *
  20593. + * V4L2 driver MMAL vchiq interface code
  20594. + */
  20595. +
  20596. +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20597. +
  20598. +#include <linux/errno.h>
  20599. +#include <linux/kernel.h>
  20600. +#include <linux/mutex.h>
  20601. +#include <linux/mm.h>
  20602. +#include <linux/slab.h>
  20603. +#include <linux/completion.h>
  20604. +#include <linux/vmalloc.h>
  20605. +#include <asm/cacheflush.h>
  20606. +#include <media/videobuf2-vmalloc.h>
  20607. +
  20608. +#include "mmal-common.h"
  20609. +#include "mmal-vchiq.h"
  20610. +#include "mmal-msg.h"
  20611. +
  20612. +#define USE_VCHIQ_ARM
  20613. +#include "interface/vchi/vchi.h"
  20614. +
  20615. +/* maximum number of components supported */
  20616. +#define VCHIQ_MMAL_MAX_COMPONENTS 4
  20617. +
  20618. +/*#define FULL_MSG_DUMP 1*/
  20619. +
  20620. +#ifdef DEBUG
  20621. +static const char *const msg_type_names[] = {
  20622. + "UNKNOWN",
  20623. + "QUIT",
  20624. + "SERVICE_CLOSED",
  20625. + "GET_VERSION",
  20626. + "COMPONENT_CREATE",
  20627. + "COMPONENT_DESTROY",
  20628. + "COMPONENT_ENABLE",
  20629. + "COMPONENT_DISABLE",
  20630. + "PORT_INFO_GET",
  20631. + "PORT_INFO_SET",
  20632. + "PORT_ACTION",
  20633. + "BUFFER_FROM_HOST",
  20634. + "BUFFER_TO_HOST",
  20635. + "GET_STATS",
  20636. + "PORT_PARAMETER_SET",
  20637. + "PORT_PARAMETER_GET",
  20638. + "EVENT_TO_HOST",
  20639. + "GET_CORE_STATS_FOR_PORT",
  20640. + "OPAQUE_ALLOCATOR",
  20641. + "CONSUME_MEM",
  20642. + "LMK",
  20643. + "OPAQUE_ALLOCATOR_DESC",
  20644. + "DRM_GET_LHS32",
  20645. + "DRM_GET_TIME",
  20646. + "BUFFER_FROM_HOST_ZEROLEN",
  20647. + "PORT_FLUSH",
  20648. + "HOST_LOG",
  20649. +};
  20650. +#endif
  20651. +
  20652. +static const char *const port_action_type_names[] = {
  20653. + "UNKNOWN",
  20654. + "ENABLE",
  20655. + "DISABLE",
  20656. + "FLUSH",
  20657. + "CONNECT",
  20658. + "DISCONNECT",
  20659. + "SET_REQUIREMENTS",
  20660. +};
  20661. +
  20662. +#if defined(DEBUG)
  20663. +#if defined(FULL_MSG_DUMP)
  20664. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  20665. + do { \
  20666. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  20667. + msg_type_names[(MSG)->h.type], \
  20668. + (MSG)->h.type, (MSG_LEN)); \
  20669. + print_hex_dump(KERN_DEBUG, "<<h: ", DUMP_PREFIX_OFFSET, \
  20670. + 16, 4, (MSG), \
  20671. + sizeof(struct mmal_msg_header), 1); \
  20672. + print_hex_dump(KERN_DEBUG, "<<p: ", DUMP_PREFIX_OFFSET, \
  20673. + 16, 4, \
  20674. + ((u8 *)(MSG)) + sizeof(struct mmal_msg_header),\
  20675. + (MSG_LEN) - sizeof(struct mmal_msg_header), 1); \
  20676. + } while (0)
  20677. +#else
  20678. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE) \
  20679. + { \
  20680. + pr_debug(TITLE" type:%s(%d) length:%d\n", \
  20681. + msg_type_names[(MSG)->h.type], \
  20682. + (MSG)->h.type, (MSG_LEN)); \
  20683. + }
  20684. +#endif
  20685. +#else
  20686. +#define DBG_DUMP_MSG(MSG, MSG_LEN, TITLE)
  20687. +#endif
  20688. +
  20689. +/* normal message context */
  20690. +struct mmal_msg_context {
  20691. + union {
  20692. + struct {
  20693. + /* work struct for defered callback - must come first */
  20694. + struct work_struct work;
  20695. + /* mmal instance */
  20696. + struct vchiq_mmal_instance *instance;
  20697. + /* mmal port */
  20698. + struct vchiq_mmal_port *port;
  20699. + /* actual buffer used to store bulk reply */
  20700. + struct mmal_buffer *buffer;
  20701. + /* amount of buffer used */
  20702. + unsigned long buffer_used;
  20703. + /* MMAL buffer flags */
  20704. + u32 mmal_flags;
  20705. + /* Presentation and Decode timestamps */
  20706. + s64 pts;
  20707. + s64 dts;
  20708. +
  20709. + int status; /* context status */
  20710. +
  20711. + } bulk; /* bulk data */
  20712. +
  20713. + struct {
  20714. + /* message handle to release */
  20715. + VCHI_HELD_MSG_T msg_handle;
  20716. + /* pointer to received message */
  20717. + struct mmal_msg *msg;
  20718. + /* received message length */
  20719. + u32 msg_len;
  20720. + /* completion upon reply */
  20721. + struct completion cmplt;
  20722. + } sync; /* synchronous response */
  20723. + } u;
  20724. +
  20725. +};
  20726. +
  20727. +struct vchiq_mmal_instance {
  20728. + VCHI_SERVICE_HANDLE_T handle;
  20729. +
  20730. + /* ensure serialised access to service */
  20731. + struct mutex vchiq_mutex;
  20732. +
  20733. + /* ensure serialised access to bulk operations */
  20734. + struct mutex bulk_mutex;
  20735. +
  20736. + /* vmalloc page to receive scratch bulk xfers into */
  20737. + void *bulk_scratch;
  20738. +
  20739. + /* component to use next */
  20740. + int component_idx;
  20741. + struct vchiq_mmal_component component[VCHIQ_MMAL_MAX_COMPONENTS];
  20742. +};
  20743. +
  20744. +static struct mmal_msg_context *get_msg_context(struct vchiq_mmal_instance
  20745. + *instance)
  20746. +{
  20747. + struct mmal_msg_context *msg_context;
  20748. +
  20749. + /* todo: should this be allocated from a pool to avoid kmalloc */
  20750. + msg_context = kmalloc(sizeof(*msg_context), GFP_KERNEL);
  20751. + memset(msg_context, 0, sizeof(*msg_context));
  20752. +
  20753. + return msg_context;
  20754. +}
  20755. +
  20756. +static void release_msg_context(struct mmal_msg_context *msg_context)
  20757. +{
  20758. + kfree(msg_context);
  20759. +}
  20760. +
  20761. +/* deals with receipt of event to host message */
  20762. +static void event_to_host_cb(struct vchiq_mmal_instance *instance,
  20763. + struct mmal_msg *msg, u32 msg_len)
  20764. +{
  20765. + pr_debug("unhandled event\n");
  20766. + pr_debug("component:%p port type:%d num:%d cmd:0x%x length:%d\n",
  20767. + msg->u.event_to_host.client_component,
  20768. + msg->u.event_to_host.port_type,
  20769. + msg->u.event_to_host.port_num,
  20770. + msg->u.event_to_host.cmd, msg->u.event_to_host.length);
  20771. +}
  20772. +
  20773. +/* workqueue scheduled callback
  20774. + *
  20775. + * we do this because it is important we do not call any other vchiq
  20776. + * sync calls from witin the message delivery thread
  20777. + */
  20778. +static void buffer_work_cb(struct work_struct *work)
  20779. +{
  20780. + struct mmal_msg_context *msg_context = (struct mmal_msg_context *)work;
  20781. +
  20782. + msg_context->u.bulk.port->buffer_cb(msg_context->u.bulk.instance,
  20783. + msg_context->u.bulk.port,
  20784. + msg_context->u.bulk.status,
  20785. + msg_context->u.bulk.buffer,
  20786. + msg_context->u.bulk.buffer_used,
  20787. + msg_context->u.bulk.mmal_flags,
  20788. + msg_context->u.bulk.dts,
  20789. + msg_context->u.bulk.pts);
  20790. +
  20791. + /* release message context */
  20792. + release_msg_context(msg_context);
  20793. +}
  20794. +
  20795. +/* enqueue a bulk receive for a given message context */
  20796. +static int bulk_receive(struct vchiq_mmal_instance *instance,
  20797. + struct mmal_msg *msg,
  20798. + struct mmal_msg_context *msg_context)
  20799. +{
  20800. + unsigned long rd_len;
  20801. + unsigned long flags = 0;
  20802. + int ret;
  20803. +
  20804. + /* bulk mutex stops other bulk operations while we have a
  20805. + * receive in progress - released in callback
  20806. + */
  20807. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  20808. + if (ret != 0)
  20809. + return ret;
  20810. +
  20811. + rd_len = msg->u.buffer_from_host.buffer_header.length;
  20812. +
  20813. + /* take buffer from queue */
  20814. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  20815. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  20816. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  20817. + pr_err("buffer list empty trying to submit bulk receive\n");
  20818. +
  20819. + /* todo: this is a serious error, we should never have
  20820. + * commited a buffer_to_host operation to the mmal
  20821. + * port without the buffer to back it up (underflow
  20822. + * handling) and there is no obvious way to deal with
  20823. + * this - how is the mmal servie going to react when
  20824. + * we fail to do the xfer and reschedule a buffer when
  20825. + * it arrives? perhaps a starved flag to indicate a
  20826. + * waiting bulk receive?
  20827. + */
  20828. +
  20829. + mutex_unlock(&instance->bulk_mutex);
  20830. +
  20831. + return -EINVAL;
  20832. + }
  20833. +
  20834. + msg_context->u.bulk.buffer =
  20835. + list_entry(msg_context->u.bulk.port->buffers.next,
  20836. + struct mmal_buffer, list);
  20837. + list_del(&msg_context->u.bulk.buffer->list);
  20838. +
  20839. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  20840. +
  20841. + /* ensure we do not overrun the available buffer */
  20842. + if (rd_len > msg_context->u.bulk.buffer->buffer_size) {
  20843. + rd_len = msg_context->u.bulk.buffer->buffer_size;
  20844. + pr_warn("short read as not enough receive buffer space\n");
  20845. + /* todo: is this the correct response, what happens to
  20846. + * the rest of the message data?
  20847. + */
  20848. + }
  20849. +
  20850. + /* store length */
  20851. + msg_context->u.bulk.buffer_used = rd_len;
  20852. + msg_context->u.bulk.mmal_flags =
  20853. + msg->u.buffer_from_host.buffer_header.flags;
  20854. + msg_context->u.bulk.dts = msg->u.buffer_from_host.buffer_header.dts;
  20855. + msg_context->u.bulk.pts = msg->u.buffer_from_host.buffer_header.pts;
  20856. +
  20857. + // only need to flush L1 cache here, as VCHIQ takes care of the L2
  20858. + // cache.
  20859. + __cpuc_flush_dcache_area(msg_context->u.bulk.buffer->buffer, rd_len);
  20860. +
  20861. + /* queue the bulk submission */
  20862. + vchi_service_use(instance->handle);
  20863. + ret = vchi_bulk_queue_receive(instance->handle,
  20864. + msg_context->u.bulk.buffer->buffer,
  20865. + /* Actual receive needs to be a multiple
  20866. + * of 4 bytes
  20867. + */
  20868. + (rd_len + 3) & ~3,
  20869. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  20870. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  20871. + msg_context);
  20872. +
  20873. + vchi_service_release(instance->handle);
  20874. +
  20875. + if (ret != 0) {
  20876. + /* callback will not be clearing the mutex */
  20877. + mutex_unlock(&instance->bulk_mutex);
  20878. + }
  20879. +
  20880. + return ret;
  20881. +}
  20882. +
  20883. +/* enque a dummy bulk receive for a given message context */
  20884. +static int dummy_bulk_receive(struct vchiq_mmal_instance *instance,
  20885. + struct mmal_msg_context *msg_context)
  20886. +{
  20887. + int ret;
  20888. +
  20889. + /* bulk mutex stops other bulk operations while we have a
  20890. + * receive in progress - released in callback
  20891. + */
  20892. + ret = mutex_lock_interruptible(&instance->bulk_mutex);
  20893. + if (ret != 0)
  20894. + return ret;
  20895. +
  20896. + /* zero length indicates this was a dummy transfer */
  20897. + msg_context->u.bulk.buffer_used = 0;
  20898. +
  20899. + /* queue the bulk submission */
  20900. + vchi_service_use(instance->handle);
  20901. +
  20902. + ret = vchi_bulk_queue_receive(instance->handle,
  20903. + instance->bulk_scratch,
  20904. + 8,
  20905. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE |
  20906. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
  20907. + msg_context);
  20908. +
  20909. + vchi_service_release(instance->handle);
  20910. +
  20911. + if (ret != 0) {
  20912. + /* callback will not be clearing the mutex */
  20913. + mutex_unlock(&instance->bulk_mutex);
  20914. + }
  20915. +
  20916. + return ret;
  20917. +}
  20918. +
  20919. +/* data in message, memcpy from packet into output buffer */
  20920. +static int inline_receive(struct vchiq_mmal_instance *instance,
  20921. + struct mmal_msg *msg,
  20922. + struct mmal_msg_context *msg_context)
  20923. +{
  20924. + unsigned long flags = 0;
  20925. +
  20926. + /* take buffer from queue */
  20927. + spin_lock_irqsave(&msg_context->u.bulk.port->slock, flags);
  20928. + if (list_empty(&msg_context->u.bulk.port->buffers)) {
  20929. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  20930. + pr_err("buffer list empty trying to receive inline\n");
  20931. +
  20932. + /* todo: this is a serious error, we should never have
  20933. + * commited a buffer_to_host operation to the mmal
  20934. + * port without the buffer to back it up (with
  20935. + * underflow handling) and there is no obvious way to
  20936. + * deal with this. Less bad than the bulk case as we
  20937. + * can just drop this on the floor but...unhelpful
  20938. + */
  20939. + return -EINVAL;
  20940. + }
  20941. +
  20942. + msg_context->u.bulk.buffer =
  20943. + list_entry(msg_context->u.bulk.port->buffers.next,
  20944. + struct mmal_buffer, list);
  20945. + list_del(&msg_context->u.bulk.buffer->list);
  20946. +
  20947. + spin_unlock_irqrestore(&msg_context->u.bulk.port->slock, flags);
  20948. +
  20949. + memcpy(msg_context->u.bulk.buffer->buffer,
  20950. + msg->u.buffer_from_host.short_data,
  20951. + msg->u.buffer_from_host.payload_in_message);
  20952. +
  20953. + msg_context->u.bulk.buffer_used =
  20954. + msg->u.buffer_from_host.payload_in_message;
  20955. +
  20956. + return 0;
  20957. +}
  20958. +
  20959. +/* queue the buffer availability with MMAL_MSG_TYPE_BUFFER_FROM_HOST */
  20960. +static int
  20961. +buffer_from_host(struct vchiq_mmal_instance *instance,
  20962. + struct vchiq_mmal_port *port, struct mmal_buffer *buf)
  20963. +{
  20964. + struct mmal_msg_context *msg_context;
  20965. + struct mmal_msg m;
  20966. + int ret;
  20967. +
  20968. + pr_debug("instance:%p buffer:%p\n", instance->handle, buf);
  20969. +
  20970. + /* bulk mutex stops other bulk operations while we
  20971. + * have a receive in progress
  20972. + */
  20973. + if (mutex_lock_interruptible(&instance->bulk_mutex))
  20974. + return -EINTR;
  20975. +
  20976. + /* get context */
  20977. + msg_context = get_msg_context(instance);
  20978. + if (msg_context == NULL)
  20979. + return -ENOMEM;
  20980. +
  20981. + /* store bulk message context for when data arrives */
  20982. + msg_context->u.bulk.instance = instance;
  20983. + msg_context->u.bulk.port = port;
  20984. + msg_context->u.bulk.buffer = NULL; /* not valid until bulk xfer */
  20985. + msg_context->u.bulk.buffer_used = 0;
  20986. +
  20987. + /* initialise work structure ready to schedule callback */
  20988. + INIT_WORK(&msg_context->u.bulk.work, buffer_work_cb);
  20989. +
  20990. + /* prep the buffer from host message */
  20991. + memset(&m, 0xbc, sizeof(m)); /* just to make debug clearer */
  20992. +
  20993. + m.h.type = MMAL_MSG_TYPE_BUFFER_FROM_HOST;
  20994. + m.h.magic = MMAL_MAGIC;
  20995. + m.h.context = msg_context;
  20996. + m.h.status = 0;
  20997. +
  20998. + /* drvbuf is our private data passed back */
  20999. + m.u.buffer_from_host.drvbuf.magic = MMAL_MAGIC;
  21000. + m.u.buffer_from_host.drvbuf.component_handle = port->component->handle;
  21001. + m.u.buffer_from_host.drvbuf.port_handle = port->handle;
  21002. + m.u.buffer_from_host.drvbuf.client_context = msg_context;
  21003. +
  21004. + /* buffer header */
  21005. + m.u.buffer_from_host.buffer_header.cmd = 0;
  21006. + m.u.buffer_from_host.buffer_header.data = buf->buffer;
  21007. + m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
  21008. + m.u.buffer_from_host.buffer_header.length = 0; /* nothing used yet */
  21009. + m.u.buffer_from_host.buffer_header.offset = 0; /* no offset */
  21010. + m.u.buffer_from_host.buffer_header.flags = 0; /* no flags */
  21011. + m.u.buffer_from_host.buffer_header.pts = MMAL_TIME_UNKNOWN;
  21012. + m.u.buffer_from_host.buffer_header.dts = MMAL_TIME_UNKNOWN;
  21013. +
  21014. + /* clear buffer type sepecific data */
  21015. + memset(&m.u.buffer_from_host.buffer_header_type_specific, 0,
  21016. + sizeof(m.u.buffer_from_host.buffer_header_type_specific));
  21017. +
  21018. + /* no payload in message */
  21019. + m.u.buffer_from_host.payload_in_message = 0;
  21020. +
  21021. + vchi_service_use(instance->handle);
  21022. +
  21023. + ret = vchi_msg_queue(instance->handle, &m,
  21024. + sizeof(struct mmal_msg_header) +
  21025. + sizeof(m.u.buffer_from_host),
  21026. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  21027. +
  21028. + if (ret != 0) {
  21029. + release_msg_context(msg_context);
  21030. + /* todo: is this correct error value? */
  21031. + }
  21032. +
  21033. + vchi_service_release(instance->handle);
  21034. +
  21035. + mutex_unlock(&instance->bulk_mutex);
  21036. +
  21037. + return ret;
  21038. +}
  21039. +
  21040. +/* submit a buffer to the mmal sevice
  21041. + *
  21042. + * the buffer_from_host uses size data from the ports next available
  21043. + * mmal_buffer and deals with there being no buffer available by
  21044. + * incrementing the underflow for later
  21045. + */
  21046. +static int port_buffer_from_host(struct vchiq_mmal_instance *instance,
  21047. + struct vchiq_mmal_port *port)
  21048. +{
  21049. + int ret;
  21050. + struct mmal_buffer *buf;
  21051. + unsigned long flags = 0;
  21052. +
  21053. + if (!port->enabled)
  21054. + return -EINVAL;
  21055. +
  21056. + /* peek buffer from queue */
  21057. + spin_lock_irqsave(&port->slock, flags);
  21058. + if (list_empty(&port->buffers)) {
  21059. + port->buffer_underflow++;
  21060. + spin_unlock_irqrestore(&port->slock, flags);
  21061. + return -ENOSPC;
  21062. + }
  21063. +
  21064. + buf = list_entry(port->buffers.next, struct mmal_buffer, list);
  21065. +
  21066. + spin_unlock_irqrestore(&port->slock, flags);
  21067. +
  21068. + /* issue buffer to mmal service */
  21069. + ret = buffer_from_host(instance, port, buf);
  21070. + if (ret) {
  21071. + pr_err("adding buffer header failed\n");
  21072. + /* todo: how should this be dealt with */
  21073. + }
  21074. +
  21075. + return ret;
  21076. +}
  21077. +
  21078. +/* deals with receipt of buffer to host message */
  21079. +static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
  21080. + struct mmal_msg *msg, u32 msg_len)
  21081. +{
  21082. + struct mmal_msg_context *msg_context;
  21083. +
  21084. + pr_debug("buffer_to_host_cb: instance:%p msg:%p msg_len:%d\n",
  21085. + instance, msg, msg_len);
  21086. +
  21087. + if (msg->u.buffer_from_host.drvbuf.magic == MMAL_MAGIC) {
  21088. + msg_context = msg->u.buffer_from_host.drvbuf.client_context;
  21089. + } else {
  21090. + pr_err("MMAL_MSG_TYPE_BUFFER_TO_HOST with bad magic\n");
  21091. + return;
  21092. + }
  21093. +
  21094. + if (msg->h.status != MMAL_MSG_STATUS_SUCCESS) {
  21095. + /* message reception had an error */
  21096. + pr_warn("error %d in reply\n", msg->h.status);
  21097. +
  21098. + msg_context->u.bulk.status = msg->h.status;
  21099. +
  21100. + } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
  21101. + /* empty buffer */
  21102. + if (msg->u.buffer_from_host.buffer_header.flags &
  21103. + MMAL_BUFFER_HEADER_FLAG_EOS) {
  21104. + msg_context->u.bulk.status =
  21105. + dummy_bulk_receive(instance, msg_context);
  21106. + if (msg_context->u.bulk.status == 0)
  21107. + return; /* successful bulk submission, bulk
  21108. + * completion will trigger callback
  21109. + */
  21110. + } else {
  21111. + /* do callback with empty buffer - not EOS though */
  21112. + msg_context->u.bulk.status = 0;
  21113. + msg_context->u.bulk.buffer_used = 0;
  21114. + }
  21115. + } else if (msg->u.buffer_from_host.payload_in_message == 0) {
  21116. + /* data is not in message, queue a bulk receive */
  21117. + msg_context->u.bulk.status =
  21118. + bulk_receive(instance, msg, msg_context);
  21119. + if (msg_context->u.bulk.status == 0)
  21120. + return; /* successful bulk submission, bulk
  21121. + * completion will trigger callback
  21122. + */
  21123. +
  21124. + /* failed to submit buffer, this will end badly */
  21125. + pr_err("error %d on bulk submission\n",
  21126. + msg_context->u.bulk.status);
  21127. +
  21128. + } else if (msg->u.buffer_from_host.payload_in_message <=
  21129. + MMAL_VC_SHORT_DATA) {
  21130. + /* data payload within message */
  21131. + msg_context->u.bulk.status = inline_receive(instance, msg,
  21132. + msg_context);
  21133. + } else {
  21134. + pr_err("message with invalid short payload\n");
  21135. +
  21136. + /* signal error */
  21137. + msg_context->u.bulk.status = -EINVAL;
  21138. + msg_context->u.bulk.buffer_used =
  21139. + msg->u.buffer_from_host.payload_in_message;
  21140. + }
  21141. +
  21142. + /* replace the buffer header */
  21143. + port_buffer_from_host(instance, msg_context->u.bulk.port);
  21144. +
  21145. + /* schedule the port callback */
  21146. + schedule_work(&msg_context->u.bulk.work);
  21147. +}
  21148. +
  21149. +static void bulk_receive_cb(struct vchiq_mmal_instance *instance,
  21150. + struct mmal_msg_context *msg_context)
  21151. +{
  21152. + /* bulk receive operation complete */
  21153. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  21154. +
  21155. + /* replace the buffer header */
  21156. + port_buffer_from_host(msg_context->u.bulk.instance,
  21157. + msg_context->u.bulk.port);
  21158. +
  21159. + msg_context->u.bulk.status = 0;
  21160. +
  21161. + /* schedule the port callback */
  21162. + schedule_work(&msg_context->u.bulk.work);
  21163. +}
  21164. +
  21165. +static void bulk_abort_cb(struct vchiq_mmal_instance *instance,
  21166. + struct mmal_msg_context *msg_context)
  21167. +{
  21168. + pr_err("%s: bulk ABORTED msg_context:%p\n", __func__, msg_context);
  21169. +
  21170. + /* bulk receive operation complete */
  21171. + mutex_unlock(&msg_context->u.bulk.instance->bulk_mutex);
  21172. +
  21173. + /* replace the buffer header */
  21174. + port_buffer_from_host(msg_context->u.bulk.instance,
  21175. + msg_context->u.bulk.port);
  21176. +
  21177. + msg_context->u.bulk.status = -EINTR;
  21178. +
  21179. + schedule_work(&msg_context->u.bulk.work);
  21180. +}
  21181. +
  21182. +/* incoming event service callback */
  21183. +static void service_callback(void *param,
  21184. + const VCHI_CALLBACK_REASON_T reason,
  21185. + void *bulk_ctx)
  21186. +{
  21187. + struct vchiq_mmal_instance *instance = param;
  21188. + int status;
  21189. + u32 msg_len;
  21190. + struct mmal_msg *msg;
  21191. + VCHI_HELD_MSG_T msg_handle;
  21192. +
  21193. + if (!instance) {
  21194. + pr_err("Message callback passed NULL instance\n");
  21195. + return;
  21196. + }
  21197. +
  21198. + switch (reason) {
  21199. + case VCHI_CALLBACK_MSG_AVAILABLE:
  21200. + status = vchi_msg_hold(instance->handle, (void **)&msg,
  21201. + &msg_len, VCHI_FLAGS_NONE, &msg_handle);
  21202. + if (status) {
  21203. + pr_err("Unable to dequeue a message (%d)\n", status);
  21204. + break;
  21205. + }
  21206. +
  21207. + DBG_DUMP_MSG(msg, msg_len, "<<< reply message");
  21208. +
  21209. + /* handling is different for buffer messages */
  21210. + switch (msg->h.type) {
  21211. +
  21212. + case MMAL_MSG_TYPE_BUFFER_FROM_HOST:
  21213. + vchi_held_msg_release(&msg_handle);
  21214. + break;
  21215. +
  21216. + case MMAL_MSG_TYPE_EVENT_TO_HOST:
  21217. + event_to_host_cb(instance, msg, msg_len);
  21218. + vchi_held_msg_release(&msg_handle);
  21219. +
  21220. + break;
  21221. +
  21222. + case MMAL_MSG_TYPE_BUFFER_TO_HOST:
  21223. + buffer_to_host_cb(instance, msg, msg_len);
  21224. + vchi_held_msg_release(&msg_handle);
  21225. + break;
  21226. +
  21227. + default:
  21228. + /* messages dependant on header context to complete */
  21229. +
  21230. + /* todo: the msg.context really ought to be sanity
  21231. + * checked before we just use it, afaict it comes back
  21232. + * and is used raw from the videocore. Perhaps it
  21233. + * should be verified the address lies in the kernel
  21234. + * address space.
  21235. + */
  21236. + if (msg->h.context == NULL) {
  21237. + pr_err("received message context was null!\n");
  21238. + vchi_held_msg_release(&msg_handle);
  21239. + break;
  21240. + }
  21241. +
  21242. + /* fill in context values */
  21243. + msg->h.context->u.sync.msg_handle = msg_handle;
  21244. + msg->h.context->u.sync.msg = msg;
  21245. + msg->h.context->u.sync.msg_len = msg_len;
  21246. +
  21247. + /* todo: should this check (completion_done()
  21248. + * == 1) for no one waiting? or do we need a
  21249. + * flag to tell us the completion has been
  21250. + * interrupted so we can free the message and
  21251. + * its context. This probably also solves the
  21252. + * message arriving after interruption todo
  21253. + * below
  21254. + */
  21255. +
  21256. + /* complete message so caller knows it happened */
  21257. + complete(&msg->h.context->u.sync.cmplt);
  21258. + break;
  21259. + }
  21260. +
  21261. + break;
  21262. +
  21263. + case VCHI_CALLBACK_BULK_RECEIVED:
  21264. + bulk_receive_cb(instance, bulk_ctx);
  21265. + break;
  21266. +
  21267. + case VCHI_CALLBACK_BULK_RECEIVE_ABORTED:
  21268. + bulk_abort_cb(instance, bulk_ctx);
  21269. + break;
  21270. +
  21271. + case VCHI_CALLBACK_SERVICE_CLOSED:
  21272. + /* TODO: consider if this requires action if received when
  21273. + * driver is not explicitly closing the service
  21274. + */
  21275. + break;
  21276. +
  21277. + default:
  21278. + pr_err("Received unhandled message reason %d\n", reason);
  21279. + break;
  21280. + }
  21281. +}
  21282. +
  21283. +static int send_synchronous_mmal_msg(struct vchiq_mmal_instance *instance,
  21284. + struct mmal_msg *msg,
  21285. + unsigned int payload_len,
  21286. + struct mmal_msg **msg_out,
  21287. + VCHI_HELD_MSG_T *msg_handle_out)
  21288. +{
  21289. + struct mmal_msg_context msg_context;
  21290. + int ret;
  21291. +
  21292. + /* payload size must not cause message to exceed max size */
  21293. + if (payload_len >
  21294. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header))) {
  21295. + pr_err("payload length %d exceeds max:%d\n", payload_len,
  21296. + (MMAL_MSG_MAX_SIZE - sizeof(struct mmal_msg_header)));
  21297. + return -EINVAL;
  21298. + }
  21299. +
  21300. + init_completion(&msg_context.u.sync.cmplt);
  21301. +
  21302. + msg->h.magic = MMAL_MAGIC;
  21303. + msg->h.context = &msg_context;
  21304. + msg->h.status = 0;
  21305. +
  21306. + DBG_DUMP_MSG(msg, (sizeof(struct mmal_msg_header) + payload_len),
  21307. + ">>> sync message");
  21308. +
  21309. + vchi_service_use(instance->handle);
  21310. +
  21311. + ret = vchi_msg_queue(instance->handle,
  21312. + msg,
  21313. + sizeof(struct mmal_msg_header) + payload_len,
  21314. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  21315. +
  21316. + vchi_service_release(instance->handle);
  21317. +
  21318. + if (ret) {
  21319. + pr_err("error %d queuing message\n", ret);
  21320. + return ret;
  21321. + }
  21322. +
  21323. + ret = wait_for_completion_timeout(&msg_context.u.sync.cmplt, 3*HZ);
  21324. + if (ret <= 0) {
  21325. + pr_err("error %d waiting for sync completion\n", ret);
  21326. + if (ret == 0)
  21327. + ret = -ETIME;
  21328. + /* todo: what happens if the message arrives after aborting */
  21329. + return ret;
  21330. + }
  21331. +
  21332. + *msg_out = msg_context.u.sync.msg;
  21333. + *msg_handle_out = msg_context.u.sync.msg_handle;
  21334. +
  21335. + return 0;
  21336. +}
  21337. +
  21338. +static void dump_port_info(struct vchiq_mmal_port *port)
  21339. +{
  21340. + pr_debug("port handle:0x%x enabled:%d\n", port->handle, port->enabled);
  21341. +
  21342. + pr_debug("buffer minimum num:%d size:%d align:%d\n",
  21343. + port->minimum_buffer.num,
  21344. + port->minimum_buffer.size, port->minimum_buffer.alignment);
  21345. +
  21346. + pr_debug("buffer recommended num:%d size:%d align:%d\n",
  21347. + port->recommended_buffer.num,
  21348. + port->recommended_buffer.size,
  21349. + port->recommended_buffer.alignment);
  21350. +
  21351. + pr_debug("buffer current values num:%d size:%d align:%d\n",
  21352. + port->current_buffer.num,
  21353. + port->current_buffer.size, port->current_buffer.alignment);
  21354. +
  21355. + pr_debug("elementry stream: type:%d encoding:0x%x varient:0x%x\n",
  21356. + port->format.type,
  21357. + port->format.encoding, port->format.encoding_variant);
  21358. +
  21359. + pr_debug(" bitrate:%d flags:0x%x\n",
  21360. + port->format.bitrate, port->format.flags);
  21361. +
  21362. + if (port->format.type == MMAL_ES_TYPE_VIDEO) {
  21363. + pr_debug
  21364. + ("es video format: width:%d height:%d colourspace:0x%x\n",
  21365. + port->es.video.width, port->es.video.height,
  21366. + port->es.video.color_space);
  21367. +
  21368. + pr_debug(" : crop xywh %d,%d,%d,%d\n",
  21369. + port->es.video.crop.x,
  21370. + port->es.video.crop.y,
  21371. + port->es.video.crop.width, port->es.video.crop.height);
  21372. + pr_debug(" : framerate %d/%d aspect %d/%d\n",
  21373. + port->es.video.frame_rate.num,
  21374. + port->es.video.frame_rate.den,
  21375. + port->es.video.par.num, port->es.video.par.den);
  21376. + }
  21377. +}
  21378. +
  21379. +static void port_to_mmal_msg(struct vchiq_mmal_port *port, struct mmal_port *p)
  21380. +{
  21381. +
  21382. + /* todo do readonly fields need setting at all? */
  21383. + p->type = port->type;
  21384. + p->index = port->index;
  21385. + p->index_all = 0;
  21386. + p->is_enabled = port->enabled;
  21387. + p->buffer_num_min = port->minimum_buffer.num;
  21388. + p->buffer_size_min = port->minimum_buffer.size;
  21389. + p->buffer_alignment_min = port->minimum_buffer.alignment;
  21390. + p->buffer_num_recommended = port->recommended_buffer.num;
  21391. + p->buffer_size_recommended = port->recommended_buffer.size;
  21392. +
  21393. + /* only three writable fields in a port */
  21394. + p->buffer_num = port->current_buffer.num;
  21395. + p->buffer_size = port->current_buffer.size;
  21396. + p->userdata = port;
  21397. +}
  21398. +
  21399. +static int port_info_set(struct vchiq_mmal_instance *instance,
  21400. + struct vchiq_mmal_port *port)
  21401. +{
  21402. + int ret;
  21403. + struct mmal_msg m;
  21404. + struct mmal_msg *rmsg;
  21405. + VCHI_HELD_MSG_T rmsg_handle;
  21406. +
  21407. + pr_debug("setting port info port %p\n", port);
  21408. + if (!port)
  21409. + return -1;
  21410. + dump_port_info(port);
  21411. +
  21412. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_SET;
  21413. +
  21414. + m.u.port_info_set.component_handle = port->component->handle;
  21415. + m.u.port_info_set.port_type = port->type;
  21416. + m.u.port_info_set.port_index = port->index;
  21417. +
  21418. + port_to_mmal_msg(port, &m.u.port_info_set.port);
  21419. +
  21420. + /* elementry stream format setup */
  21421. + m.u.port_info_set.format.type = port->format.type;
  21422. + m.u.port_info_set.format.encoding = port->format.encoding;
  21423. + m.u.port_info_set.format.encoding_variant =
  21424. + port->format.encoding_variant;
  21425. + m.u.port_info_set.format.bitrate = port->format.bitrate;
  21426. + m.u.port_info_set.format.flags = port->format.flags;
  21427. +
  21428. + memcpy(&m.u.port_info_set.es, &port->es,
  21429. + sizeof(union mmal_es_specific_format));
  21430. +
  21431. + m.u.port_info_set.format.extradata_size = port->format.extradata_size;
  21432. + memcpy(rmsg->u.port_info_set.extradata, port->format.extradata,
  21433. + port->format.extradata_size);
  21434. +
  21435. + ret = send_synchronous_mmal_msg(instance, &m,
  21436. + sizeof(m.u.port_info_set),
  21437. + &rmsg, &rmsg_handle);
  21438. + if (ret)
  21439. + return ret;
  21440. +
  21441. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_SET) {
  21442. + /* got an unexpected message type in reply */
  21443. + ret = -EINVAL;
  21444. + goto release_msg;
  21445. + }
  21446. +
  21447. + /* return operation status */
  21448. + ret = -rmsg->u.port_info_get_reply.status;
  21449. +
  21450. + pr_debug("%s:result:%d component:0x%x port:%d\n", __func__, ret,
  21451. + port->component->handle, port->handle);
  21452. +
  21453. +release_msg:
  21454. + vchi_held_msg_release(&rmsg_handle);
  21455. +
  21456. + return ret;
  21457. +
  21458. +}
  21459. +
  21460. +/* use port info get message to retrive port information */
  21461. +static int port_info_get(struct vchiq_mmal_instance *instance,
  21462. + struct vchiq_mmal_port *port)
  21463. +{
  21464. + int ret;
  21465. + struct mmal_msg m;
  21466. + struct mmal_msg *rmsg;
  21467. + VCHI_HELD_MSG_T rmsg_handle;
  21468. +
  21469. + /* port info time */
  21470. + m.h.type = MMAL_MSG_TYPE_PORT_INFO_GET;
  21471. + m.u.port_info_get.component_handle = port->component->handle;
  21472. + m.u.port_info_get.port_type = port->type;
  21473. + m.u.port_info_get.index = port->index;
  21474. +
  21475. + ret = send_synchronous_mmal_msg(instance, &m,
  21476. + sizeof(m.u.port_info_get),
  21477. + &rmsg, &rmsg_handle);
  21478. + if (ret)
  21479. + return ret;
  21480. +
  21481. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_INFO_GET) {
  21482. + /* got an unexpected message type in reply */
  21483. + ret = -EINVAL;
  21484. + goto release_msg;
  21485. + }
  21486. +
  21487. + /* return operation status */
  21488. + ret = -rmsg->u.port_info_get_reply.status;
  21489. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  21490. + goto release_msg;
  21491. +
  21492. + if (rmsg->u.port_info_get_reply.port.is_enabled == 0)
  21493. + port->enabled = false;
  21494. + else
  21495. + port->enabled = true;
  21496. +
  21497. + /* copy the values out of the message */
  21498. + port->handle = rmsg->u.port_info_get_reply.port_handle;
  21499. +
  21500. + /* port type and index cached to use on port info set becuase
  21501. + * it does not use a port handle
  21502. + */
  21503. + port->type = rmsg->u.port_info_get_reply.port_type;
  21504. + port->index = rmsg->u.port_info_get_reply.port_index;
  21505. +
  21506. + port->minimum_buffer.num =
  21507. + rmsg->u.port_info_get_reply.port.buffer_num_min;
  21508. + port->minimum_buffer.size =
  21509. + rmsg->u.port_info_get_reply.port.buffer_size_min;
  21510. + port->minimum_buffer.alignment =
  21511. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  21512. +
  21513. + port->recommended_buffer.alignment =
  21514. + rmsg->u.port_info_get_reply.port.buffer_alignment_min;
  21515. + port->recommended_buffer.num =
  21516. + rmsg->u.port_info_get_reply.port.buffer_num_recommended;
  21517. +
  21518. + port->current_buffer.num = rmsg->u.port_info_get_reply.port.buffer_num;
  21519. + port->current_buffer.size =
  21520. + rmsg->u.port_info_get_reply.port.buffer_size;
  21521. +
  21522. + /* stream format */
  21523. + port->format.type = rmsg->u.port_info_get_reply.format.type;
  21524. + port->format.encoding = rmsg->u.port_info_get_reply.format.encoding;
  21525. + port->format.encoding_variant =
  21526. + rmsg->u.port_info_get_reply.format.encoding_variant;
  21527. + port->format.bitrate = rmsg->u.port_info_get_reply.format.bitrate;
  21528. + port->format.flags = rmsg->u.port_info_get_reply.format.flags;
  21529. +
  21530. + /* elementry stream format */
  21531. + memcpy(&port->es,
  21532. + &rmsg->u.port_info_get_reply.es,
  21533. + sizeof(union mmal_es_specific_format));
  21534. + port->format.es = &port->es;
  21535. +
  21536. + port->format.extradata_size =
  21537. + rmsg->u.port_info_get_reply.format.extradata_size;
  21538. + memcpy(port->format.extradata,
  21539. + rmsg->u.port_info_get_reply.extradata,
  21540. + port->format.extradata_size);
  21541. +
  21542. + pr_debug("received port info\n");
  21543. + dump_port_info(port);
  21544. +
  21545. +release_msg:
  21546. +
  21547. + pr_debug("%s:result:%d component:0x%x port:%d\n",
  21548. + __func__, ret, port->component->handle, port->handle);
  21549. +
  21550. + vchi_held_msg_release(&rmsg_handle);
  21551. +
  21552. + return ret;
  21553. +}
  21554. +
  21555. +/* create comonent on vc */
  21556. +static int create_component(struct vchiq_mmal_instance *instance,
  21557. + struct vchiq_mmal_component *component,
  21558. + const char *name)
  21559. +{
  21560. + int ret;
  21561. + struct mmal_msg m;
  21562. + struct mmal_msg *rmsg;
  21563. + VCHI_HELD_MSG_T rmsg_handle;
  21564. +
  21565. + /* build component create message */
  21566. + m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
  21567. + m.u.component_create.client_component = component;
  21568. + strncpy(m.u.component_create.name, name,
  21569. + sizeof(m.u.component_create.name));
  21570. +
  21571. + ret = send_synchronous_mmal_msg(instance, &m,
  21572. + sizeof(m.u.component_create),
  21573. + &rmsg, &rmsg_handle);
  21574. + if (ret)
  21575. + return ret;
  21576. +
  21577. + if (rmsg->h.type != m.h.type) {
  21578. + /* got an unexpected message type in reply */
  21579. + ret = -EINVAL;
  21580. + goto release_msg;
  21581. + }
  21582. +
  21583. + ret = -rmsg->u.component_create_reply.status;
  21584. + if (ret != MMAL_MSG_STATUS_SUCCESS)
  21585. + goto release_msg;
  21586. +
  21587. + /* a valid component response received */
  21588. + component->handle = rmsg->u.component_create_reply.component_handle;
  21589. + component->inputs = rmsg->u.component_create_reply.input_num;
  21590. + component->outputs = rmsg->u.component_create_reply.output_num;
  21591. + component->clocks = rmsg->u.component_create_reply.clock_num;
  21592. +
  21593. + pr_debug("Component handle:0x%x in:%d out:%d clock:%d\n",
  21594. + component->handle,
  21595. + component->inputs, component->outputs, component->clocks);
  21596. +
  21597. +release_msg:
  21598. + vchi_held_msg_release(&rmsg_handle);
  21599. +
  21600. + return ret;
  21601. +}
  21602. +
  21603. +/* destroys a component on vc */
  21604. +static int destroy_component(struct vchiq_mmal_instance *instance,
  21605. + struct vchiq_mmal_component *component)
  21606. +{
  21607. + int ret;
  21608. + struct mmal_msg m;
  21609. + struct mmal_msg *rmsg;
  21610. + VCHI_HELD_MSG_T rmsg_handle;
  21611. +
  21612. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DESTROY;
  21613. + m.u.component_destroy.component_handle = component->handle;
  21614. +
  21615. + ret = send_synchronous_mmal_msg(instance, &m,
  21616. + sizeof(m.u.component_destroy),
  21617. + &rmsg, &rmsg_handle);
  21618. + if (ret)
  21619. + return ret;
  21620. +
  21621. + if (rmsg->h.type != m.h.type) {
  21622. + /* got an unexpected message type in reply */
  21623. + ret = -EINVAL;
  21624. + goto release_msg;
  21625. + }
  21626. +
  21627. + ret = -rmsg->u.component_destroy_reply.status;
  21628. +
  21629. +release_msg:
  21630. +
  21631. + vchi_held_msg_release(&rmsg_handle);
  21632. +
  21633. + return ret;
  21634. +}
  21635. +
  21636. +/* enable a component on vc */
  21637. +static int enable_component(struct vchiq_mmal_instance *instance,
  21638. + struct vchiq_mmal_component *component)
  21639. +{
  21640. + int ret;
  21641. + struct mmal_msg m;
  21642. + struct mmal_msg *rmsg;
  21643. + VCHI_HELD_MSG_T rmsg_handle;
  21644. +
  21645. + m.h.type = MMAL_MSG_TYPE_COMPONENT_ENABLE;
  21646. + m.u.component_enable.component_handle = component->handle;
  21647. +
  21648. + ret = send_synchronous_mmal_msg(instance, &m,
  21649. + sizeof(m.u.component_enable),
  21650. + &rmsg, &rmsg_handle);
  21651. + if (ret)
  21652. + return ret;
  21653. +
  21654. + if (rmsg->h.type != m.h.type) {
  21655. + /* got an unexpected message type in reply */
  21656. + ret = -EINVAL;
  21657. + goto release_msg;
  21658. + }
  21659. +
  21660. + ret = -rmsg->u.component_enable_reply.status;
  21661. +
  21662. +release_msg:
  21663. + vchi_held_msg_release(&rmsg_handle);
  21664. +
  21665. + return ret;
  21666. +}
  21667. +
  21668. +/* disable a component on vc */
  21669. +static int disable_component(struct vchiq_mmal_instance *instance,
  21670. + struct vchiq_mmal_component *component)
  21671. +{
  21672. + int ret;
  21673. + struct mmal_msg m;
  21674. + struct mmal_msg *rmsg;
  21675. + VCHI_HELD_MSG_T rmsg_handle;
  21676. +
  21677. + m.h.type = MMAL_MSG_TYPE_COMPONENT_DISABLE;
  21678. + m.u.component_disable.component_handle = component->handle;
  21679. +
  21680. + ret = send_synchronous_mmal_msg(instance, &m,
  21681. + sizeof(m.u.component_disable),
  21682. + &rmsg, &rmsg_handle);
  21683. + if (ret)
  21684. + return ret;
  21685. +
  21686. + if (rmsg->h.type != m.h.type) {
  21687. + /* got an unexpected message type in reply */
  21688. + ret = -EINVAL;
  21689. + goto release_msg;
  21690. + }
  21691. +
  21692. + ret = -rmsg->u.component_disable_reply.status;
  21693. +
  21694. +release_msg:
  21695. +
  21696. + vchi_held_msg_release(&rmsg_handle);
  21697. +
  21698. + return ret;
  21699. +}
  21700. +
  21701. +/* get version of mmal implementation */
  21702. +static int get_version(struct vchiq_mmal_instance *instance,
  21703. + u32 *major_out, u32 *minor_out)
  21704. +{
  21705. + int ret;
  21706. + struct mmal_msg m;
  21707. + struct mmal_msg *rmsg;
  21708. + VCHI_HELD_MSG_T rmsg_handle;
  21709. +
  21710. + m.h.type = MMAL_MSG_TYPE_GET_VERSION;
  21711. +
  21712. + ret = send_synchronous_mmal_msg(instance, &m,
  21713. + sizeof(m.u.version),
  21714. + &rmsg, &rmsg_handle);
  21715. + if (ret)
  21716. + return ret;
  21717. +
  21718. + if (rmsg->h.type != m.h.type) {
  21719. + /* got an unexpected message type in reply */
  21720. + ret = -EINVAL;
  21721. + goto release_msg;
  21722. + }
  21723. +
  21724. + *major_out = rmsg->u.version.major;
  21725. + *minor_out = rmsg->u.version.minor;
  21726. +
  21727. +release_msg:
  21728. + vchi_held_msg_release(&rmsg_handle);
  21729. +
  21730. + return ret;
  21731. +}
  21732. +
  21733. +/* do a port action with a port as a parameter */
  21734. +static int port_action_port(struct vchiq_mmal_instance *instance,
  21735. + struct vchiq_mmal_port *port,
  21736. + enum mmal_msg_port_action_type action_type)
  21737. +{
  21738. + int ret;
  21739. + struct mmal_msg m;
  21740. + struct mmal_msg *rmsg;
  21741. + VCHI_HELD_MSG_T rmsg_handle;
  21742. +
  21743. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  21744. + m.u.port_action_port.component_handle = port->component->handle;
  21745. + m.u.port_action_port.port_handle = port->handle;
  21746. + m.u.port_action_port.action = action_type;
  21747. +
  21748. + port_to_mmal_msg(port, &m.u.port_action_port.port);
  21749. +
  21750. + ret = send_synchronous_mmal_msg(instance, &m,
  21751. + sizeof(m.u.port_action_port),
  21752. + &rmsg, &rmsg_handle);
  21753. + if (ret)
  21754. + return ret;
  21755. +
  21756. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  21757. + /* got an unexpected message type in reply */
  21758. + ret = -EINVAL;
  21759. + goto release_msg;
  21760. + }
  21761. +
  21762. + ret = -rmsg->u.port_action_reply.status;
  21763. +
  21764. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)\n",
  21765. + __func__,
  21766. + ret, port->component->handle, port->handle,
  21767. + port_action_type_names[action_type], action_type);
  21768. +
  21769. +release_msg:
  21770. + vchi_held_msg_release(&rmsg_handle);
  21771. +
  21772. + return ret;
  21773. +}
  21774. +
  21775. +/* do a port action with handles as parameters */
  21776. +static int port_action_handle(struct vchiq_mmal_instance *instance,
  21777. + struct vchiq_mmal_port *port,
  21778. + enum mmal_msg_port_action_type action_type,
  21779. + u32 connect_component_handle,
  21780. + u32 connect_port_handle)
  21781. +{
  21782. + int ret;
  21783. + struct mmal_msg m;
  21784. + struct mmal_msg *rmsg;
  21785. + VCHI_HELD_MSG_T rmsg_handle;
  21786. +
  21787. + m.h.type = MMAL_MSG_TYPE_PORT_ACTION;
  21788. +
  21789. + m.u.port_action_handle.component_handle = port->component->handle;
  21790. + m.u.port_action_handle.port_handle = port->handle;
  21791. + m.u.port_action_handle.action = action_type;
  21792. +
  21793. + m.u.port_action_handle.connect_component_handle =
  21794. + connect_component_handle;
  21795. + m.u.port_action_handle.connect_port_handle = connect_port_handle;
  21796. +
  21797. + ret = send_synchronous_mmal_msg(instance, &m,
  21798. + sizeof(m.u.port_action_handle),
  21799. + &rmsg, &rmsg_handle);
  21800. + if (ret)
  21801. + return ret;
  21802. +
  21803. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_ACTION) {
  21804. + /* got an unexpected message type in reply */
  21805. + ret = -EINVAL;
  21806. + goto release_msg;
  21807. + }
  21808. +
  21809. + ret = -rmsg->u.port_action_reply.status;
  21810. +
  21811. + pr_debug("%s:result:%d component:0x%x port:%d action:%s(%d)" \
  21812. + " connect component:0x%x connect port:%d\n",
  21813. + __func__,
  21814. + ret, port->component->handle, port->handle,
  21815. + port_action_type_names[action_type],
  21816. + action_type, connect_component_handle, connect_port_handle);
  21817. +
  21818. +release_msg:
  21819. + vchi_held_msg_release(&rmsg_handle);
  21820. +
  21821. + return ret;
  21822. +}
  21823. +
  21824. +static int port_parameter_set(struct vchiq_mmal_instance *instance,
  21825. + struct vchiq_mmal_port *port,
  21826. + u32 parameter_id, void *value, u32 value_size)
  21827. +{
  21828. + int ret;
  21829. + struct mmal_msg m;
  21830. + struct mmal_msg *rmsg;
  21831. + VCHI_HELD_MSG_T rmsg_handle;
  21832. +
  21833. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_SET;
  21834. +
  21835. + m.u.port_parameter_set.component_handle = port->component->handle;
  21836. + m.u.port_parameter_set.port_handle = port->handle;
  21837. + m.u.port_parameter_set.id = parameter_id;
  21838. + m.u.port_parameter_set.size = (2 * sizeof(u32)) + value_size;
  21839. + memcpy(&m.u.port_parameter_set.value, value, value_size);
  21840. +
  21841. + ret = send_synchronous_mmal_msg(instance, &m,
  21842. + (4 * sizeof(u32)) + value_size,
  21843. + &rmsg, &rmsg_handle);
  21844. + if (ret)
  21845. + return ret;
  21846. +
  21847. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_SET) {
  21848. + /* got an unexpected message type in reply */
  21849. + ret = -EINVAL;
  21850. + goto release_msg;
  21851. + }
  21852. +
  21853. + ret = -rmsg->u.port_parameter_set_reply.status;
  21854. +
  21855. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n",
  21856. + __func__,
  21857. + ret, port->component->handle, port->handle, parameter_id);
  21858. +
  21859. +release_msg:
  21860. + vchi_held_msg_release(&rmsg_handle);
  21861. +
  21862. + return ret;
  21863. +}
  21864. +
  21865. +static int port_parameter_get(struct vchiq_mmal_instance *instance,
  21866. + struct vchiq_mmal_port *port,
  21867. + u32 parameter_id, void *value, u32 *value_size)
  21868. +{
  21869. + int ret;
  21870. + struct mmal_msg m;
  21871. + struct mmal_msg *rmsg;
  21872. + VCHI_HELD_MSG_T rmsg_handle;
  21873. +
  21874. + m.h.type = MMAL_MSG_TYPE_PORT_PARAMETER_GET;
  21875. +
  21876. + m.u.port_parameter_get.component_handle = port->component->handle;
  21877. + m.u.port_parameter_get.port_handle = port->handle;
  21878. + m.u.port_parameter_get.id = parameter_id;
  21879. + m.u.port_parameter_get.size = (2 * sizeof(u32)) + *value_size;
  21880. +
  21881. + ret = send_synchronous_mmal_msg(instance, &m,
  21882. + sizeof(struct
  21883. + mmal_msg_port_parameter_get),
  21884. + &rmsg, &rmsg_handle);
  21885. + if (ret)
  21886. + return ret;
  21887. +
  21888. + if (rmsg->h.type != MMAL_MSG_TYPE_PORT_PARAMETER_GET) {
  21889. + /* got an unexpected message type in reply */
  21890. + pr_err("Incorrect reply type %d\n", rmsg->h.type);
  21891. + ret = -EINVAL;
  21892. + goto release_msg;
  21893. + }
  21894. +
  21895. + ret = -rmsg->u.port_parameter_get_reply.status;
  21896. + if (ret) {
  21897. + /* Copy only as much as we have space for
  21898. + * but report true size of parameter
  21899. + */
  21900. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  21901. + *value_size);
  21902. + *value_size = rmsg->u.port_parameter_get_reply.size;
  21903. + } else
  21904. + memcpy(value, &rmsg->u.port_parameter_get_reply.value,
  21905. + rmsg->u.port_parameter_get_reply.size);
  21906. +
  21907. + pr_debug("%s:result:%d component:0x%x port:%d parameter:%d\n", __func__,
  21908. + ret, port->component->handle, port->handle, parameter_id);
  21909. +
  21910. +release_msg:
  21911. + vchi_held_msg_release(&rmsg_handle);
  21912. +
  21913. + return ret;
  21914. +}
  21915. +
  21916. +/* disables a port and drains buffers from it */
  21917. +static int port_disable(struct vchiq_mmal_instance *instance,
  21918. + struct vchiq_mmal_port *port)
  21919. +{
  21920. + int ret;
  21921. + struct list_head *q, *buf_head;
  21922. + unsigned long flags = 0;
  21923. +
  21924. + if (!port->enabled)
  21925. + return 0;
  21926. +
  21927. + port->enabled = false;
  21928. +
  21929. + ret = port_action_port(instance, port,
  21930. + MMAL_MSG_PORT_ACTION_TYPE_DISABLE);
  21931. + if (ret == 0) {
  21932. +
  21933. + /* drain all queued buffers on port */
  21934. + spin_lock_irqsave(&port->slock, flags);
  21935. +
  21936. + list_for_each_safe(buf_head, q, &port->buffers) {
  21937. + struct mmal_buffer *mmalbuf;
  21938. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  21939. + list);
  21940. + list_del(buf_head);
  21941. + if (port->buffer_cb)
  21942. + port->buffer_cb(instance,
  21943. + port, 0, mmalbuf, 0, 0,
  21944. + MMAL_TIME_UNKNOWN,
  21945. + MMAL_TIME_UNKNOWN);
  21946. + }
  21947. +
  21948. + spin_unlock_irqrestore(&port->slock, flags);
  21949. +
  21950. + ret = port_info_get(instance, port);
  21951. + }
  21952. +
  21953. + return ret;
  21954. +}
  21955. +
  21956. +/* enable a port */
  21957. +static int port_enable(struct vchiq_mmal_instance *instance,
  21958. + struct vchiq_mmal_port *port)
  21959. +{
  21960. + unsigned int hdr_count;
  21961. + struct list_head *buf_head;
  21962. + int ret;
  21963. +
  21964. + if (port->enabled)
  21965. + return 0;
  21966. +
  21967. + /* ensure there are enough buffers queued to cover the buffer headers */
  21968. + if (port->buffer_cb != NULL) {
  21969. + hdr_count = 0;
  21970. + list_for_each(buf_head, &port->buffers) {
  21971. + hdr_count++;
  21972. + }
  21973. + if (hdr_count < port->current_buffer.num)
  21974. + return -ENOSPC;
  21975. + }
  21976. +
  21977. + ret = port_action_port(instance, port,
  21978. + MMAL_MSG_PORT_ACTION_TYPE_ENABLE);
  21979. + if (ret)
  21980. + goto done;
  21981. +
  21982. + port->enabled = true;
  21983. +
  21984. + if (port->buffer_cb) {
  21985. + /* send buffer headers to videocore */
  21986. + hdr_count = 1;
  21987. + list_for_each(buf_head, &port->buffers) {
  21988. + struct mmal_buffer *mmalbuf;
  21989. + mmalbuf = list_entry(buf_head, struct mmal_buffer,
  21990. + list);
  21991. + ret = buffer_from_host(instance, port, mmalbuf);
  21992. + if (ret)
  21993. + goto done;
  21994. +
  21995. + hdr_count++;
  21996. + if (hdr_count > port->current_buffer.num)
  21997. + break;
  21998. + }
  21999. + }
  22000. +
  22001. + ret = port_info_get(instance, port);
  22002. +
  22003. +done:
  22004. + return ret;
  22005. +}
  22006. +
  22007. +/* ------------------------------------------------------------------
  22008. + * Exported API
  22009. + *------------------------------------------------------------------*/
  22010. +
  22011. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  22012. + struct vchiq_mmal_port *port)
  22013. +{
  22014. + int ret;
  22015. +
  22016. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22017. + return -EINTR;
  22018. +
  22019. + ret = port_info_set(instance, port);
  22020. + if (ret)
  22021. + goto release_unlock;
  22022. +
  22023. + /* read what has actually been set */
  22024. + ret = port_info_get(instance, port);
  22025. +
  22026. +release_unlock:
  22027. + mutex_unlock(&instance->vchiq_mutex);
  22028. +
  22029. + return ret;
  22030. +
  22031. +}
  22032. +
  22033. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  22034. + struct vchiq_mmal_port *port,
  22035. + u32 parameter, void *value, u32 value_size)
  22036. +{
  22037. + int ret;
  22038. +
  22039. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22040. + return -EINTR;
  22041. +
  22042. + ret = port_parameter_set(instance, port, parameter, value, value_size);
  22043. +
  22044. + mutex_unlock(&instance->vchiq_mutex);
  22045. +
  22046. + return ret;
  22047. +}
  22048. +
  22049. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  22050. + struct vchiq_mmal_port *port,
  22051. + u32 parameter, void *value, u32 *value_size)
  22052. +{
  22053. + int ret;
  22054. +
  22055. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22056. + return -EINTR;
  22057. +
  22058. + ret = port_parameter_get(instance, port, parameter, value, value_size);
  22059. +
  22060. + mutex_unlock(&instance->vchiq_mutex);
  22061. +
  22062. + return ret;
  22063. +}
  22064. +
  22065. +/* enable a port
  22066. + *
  22067. + * enables a port and queues buffers for satisfying callbacks if we
  22068. + * provide a callback handler
  22069. + */
  22070. +int vchiq_mmal_port_enable(struct vchiq_mmal_instance *instance,
  22071. + struct vchiq_mmal_port *port,
  22072. + vchiq_mmal_buffer_cb buffer_cb)
  22073. +{
  22074. + int ret;
  22075. +
  22076. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22077. + return -EINTR;
  22078. +
  22079. + /* already enabled - noop */
  22080. + if (port->enabled) {
  22081. + ret = 0;
  22082. + goto unlock;
  22083. + }
  22084. +
  22085. + port->buffer_cb = buffer_cb;
  22086. +
  22087. + ret = port_enable(instance, port);
  22088. +
  22089. +unlock:
  22090. + mutex_unlock(&instance->vchiq_mutex);
  22091. +
  22092. + return ret;
  22093. +}
  22094. +
  22095. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  22096. + struct vchiq_mmal_port *port)
  22097. +{
  22098. + int ret;
  22099. +
  22100. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22101. + return -EINTR;
  22102. +
  22103. + if (!port->enabled) {
  22104. + mutex_unlock(&instance->vchiq_mutex);
  22105. + return 0;
  22106. + }
  22107. +
  22108. + ret = port_disable(instance, port);
  22109. +
  22110. + mutex_unlock(&instance->vchiq_mutex);
  22111. +
  22112. + return ret;
  22113. +}
  22114. +
  22115. +/* ports will be connected in a tunneled manner so data buffers
  22116. + * are not handled by client.
  22117. + */
  22118. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  22119. + struct vchiq_mmal_port *src,
  22120. + struct vchiq_mmal_port *dst)
  22121. +{
  22122. + int ret;
  22123. +
  22124. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22125. + return -EINTR;
  22126. +
  22127. + /* disconnect ports if connected */
  22128. + if (src->connected != NULL) {
  22129. + ret = port_disable(instance, src);
  22130. + if (ret) {
  22131. + pr_err("failed disabling src port(%d)\n", ret);
  22132. + goto release_unlock;
  22133. + }
  22134. +
  22135. + /* do not need to disable the destination port as they
  22136. + * are connected and it is done automatically
  22137. + */
  22138. +
  22139. + ret = port_action_handle(instance, src,
  22140. + MMAL_MSG_PORT_ACTION_TYPE_DISCONNECT,
  22141. + src->connected->component->handle,
  22142. + src->connected->handle);
  22143. + if (ret < 0) {
  22144. + pr_err("failed disconnecting src port\n");
  22145. + goto release_unlock;
  22146. + }
  22147. + src->connected->enabled = false;
  22148. + src->connected = NULL;
  22149. + }
  22150. +
  22151. + if (dst == NULL) {
  22152. + /* do not make new connection */
  22153. + ret = 0;
  22154. + pr_debug("not making new connection\n");
  22155. + goto release_unlock;
  22156. + }
  22157. +
  22158. + /* copy src port format to dst */
  22159. + dst->format.encoding = src->format.encoding;
  22160. + dst->es.video.width = src->es.video.width;
  22161. + dst->es.video.height = src->es.video.height;
  22162. + dst->es.video.crop.x = src->es.video.crop.x;
  22163. + dst->es.video.crop.y = src->es.video.crop.y;
  22164. + dst->es.video.crop.width = src->es.video.crop.width;
  22165. + dst->es.video.crop.height = src->es.video.crop.height;
  22166. + dst->es.video.frame_rate.num = src->es.video.frame_rate.num;
  22167. + dst->es.video.frame_rate.den = src->es.video.frame_rate.den;
  22168. +
  22169. + /* set new format */
  22170. + ret = port_info_set(instance, dst);
  22171. + if (ret) {
  22172. + pr_debug("setting port info failed\n");
  22173. + goto release_unlock;
  22174. + }
  22175. +
  22176. + /* read what has actually been set */
  22177. + ret = port_info_get(instance, dst);
  22178. + if (ret) {
  22179. + pr_debug("read back port info failed\n");
  22180. + goto release_unlock;
  22181. + }
  22182. +
  22183. + /* connect two ports together */
  22184. + ret = port_action_handle(instance, src,
  22185. + MMAL_MSG_PORT_ACTION_TYPE_CONNECT,
  22186. + dst->component->handle, dst->handle);
  22187. + if (ret < 0) {
  22188. + pr_debug("connecting port %d:%d to %d:%d failed\n",
  22189. + src->component->handle, src->handle,
  22190. + dst->component->handle, dst->handle);
  22191. + goto release_unlock;
  22192. + }
  22193. + src->connected = dst;
  22194. +
  22195. +release_unlock:
  22196. +
  22197. + mutex_unlock(&instance->vchiq_mutex);
  22198. +
  22199. + return ret;
  22200. +}
  22201. +
  22202. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  22203. + struct vchiq_mmal_port *port,
  22204. + struct mmal_buffer *buffer)
  22205. +{
  22206. + unsigned long flags = 0;
  22207. +
  22208. + spin_lock_irqsave(&port->slock, flags);
  22209. + list_add_tail(&buffer->list, &port->buffers);
  22210. + spin_unlock_irqrestore(&port->slock, flags);
  22211. +
  22212. + /* the port previously underflowed because it was missing a
  22213. + * mmal_buffer which has just been added, submit that buffer
  22214. + * to the mmal service.
  22215. + */
  22216. + if (port->buffer_underflow) {
  22217. + port_buffer_from_host(instance, port);
  22218. + port->buffer_underflow--;
  22219. + }
  22220. +
  22221. + return 0;
  22222. +}
  22223. +
  22224. +/* Initialise a mmal component and its ports
  22225. + *
  22226. + */
  22227. +int vchiq_mmal_component_init(struct vchiq_mmal_instance *instance,
  22228. + const char *name,
  22229. + struct vchiq_mmal_component **component_out)
  22230. +{
  22231. + int ret;
  22232. + int idx; /* port index */
  22233. + struct vchiq_mmal_component *component;
  22234. +
  22235. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22236. + return -EINTR;
  22237. +
  22238. + if (instance->component_idx == VCHIQ_MMAL_MAX_COMPONENTS) {
  22239. + ret = -EINVAL; /* todo is this correct error? */
  22240. + goto unlock;
  22241. + }
  22242. +
  22243. + component = &instance->component[instance->component_idx];
  22244. +
  22245. + ret = create_component(instance, component, name);
  22246. + if (ret < 0)
  22247. + goto unlock;
  22248. +
  22249. + /* ports info needs gathering */
  22250. + component->control.type = MMAL_PORT_TYPE_CONTROL;
  22251. + component->control.index = 0;
  22252. + component->control.component = component;
  22253. + spin_lock_init(&component->control.slock);
  22254. + INIT_LIST_HEAD(&component->control.buffers);
  22255. + ret = port_info_get(instance, &component->control);
  22256. + if (ret < 0)
  22257. + goto release_component;
  22258. +
  22259. + for (idx = 0; idx < component->inputs; idx++) {
  22260. + component->input[idx].type = MMAL_PORT_TYPE_INPUT;
  22261. + component->input[idx].index = idx;
  22262. + component->input[idx].component = component;
  22263. + spin_lock_init(&component->input[idx].slock);
  22264. + INIT_LIST_HEAD(&component->input[idx].buffers);
  22265. + ret = port_info_get(instance, &component->input[idx]);
  22266. + if (ret < 0)
  22267. + goto release_component;
  22268. + }
  22269. +
  22270. + for (idx = 0; idx < component->outputs; idx++) {
  22271. + component->output[idx].type = MMAL_PORT_TYPE_OUTPUT;
  22272. + component->output[idx].index = idx;
  22273. + component->output[idx].component = component;
  22274. + spin_lock_init(&component->output[idx].slock);
  22275. + INIT_LIST_HEAD(&component->output[idx].buffers);
  22276. + ret = port_info_get(instance, &component->output[idx]);
  22277. + if (ret < 0)
  22278. + goto release_component;
  22279. + }
  22280. +
  22281. + for (idx = 0; idx < component->clocks; idx++) {
  22282. + component->clock[idx].type = MMAL_PORT_TYPE_CLOCK;
  22283. + component->clock[idx].index = idx;
  22284. + component->clock[idx].component = component;
  22285. + spin_lock_init(&component->clock[idx].slock);
  22286. + INIT_LIST_HEAD(&component->clock[idx].buffers);
  22287. + ret = port_info_get(instance, &component->clock[idx]);
  22288. + if (ret < 0)
  22289. + goto release_component;
  22290. + }
  22291. +
  22292. + instance->component_idx++;
  22293. +
  22294. + *component_out = component;
  22295. +
  22296. + mutex_unlock(&instance->vchiq_mutex);
  22297. +
  22298. + return 0;
  22299. +
  22300. +release_component:
  22301. + destroy_component(instance, component);
  22302. +unlock:
  22303. + mutex_unlock(&instance->vchiq_mutex);
  22304. +
  22305. + return ret;
  22306. +}
  22307. +
  22308. +/*
  22309. + * cause a mmal component to be destroyed
  22310. + */
  22311. +int vchiq_mmal_component_finalise(struct vchiq_mmal_instance *instance,
  22312. + struct vchiq_mmal_component *component)
  22313. +{
  22314. + int ret;
  22315. +
  22316. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22317. + return -EINTR;
  22318. +
  22319. + if (component->enabled)
  22320. + ret = disable_component(instance, component);
  22321. +
  22322. + ret = destroy_component(instance, component);
  22323. +
  22324. + mutex_unlock(&instance->vchiq_mutex);
  22325. +
  22326. + return ret;
  22327. +}
  22328. +
  22329. +/*
  22330. + * cause a mmal component to be enabled
  22331. + */
  22332. +int vchiq_mmal_component_enable(struct vchiq_mmal_instance *instance,
  22333. + struct vchiq_mmal_component *component)
  22334. +{
  22335. + int ret;
  22336. +
  22337. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22338. + return -EINTR;
  22339. +
  22340. + if (component->enabled) {
  22341. + mutex_unlock(&instance->vchiq_mutex);
  22342. + return 0;
  22343. + }
  22344. +
  22345. + ret = enable_component(instance, component);
  22346. + if (ret == 0)
  22347. + component->enabled = true;
  22348. +
  22349. + mutex_unlock(&instance->vchiq_mutex);
  22350. +
  22351. + return ret;
  22352. +}
  22353. +
  22354. +/*
  22355. + * cause a mmal component to be enabled
  22356. + */
  22357. +int vchiq_mmal_component_disable(struct vchiq_mmal_instance *instance,
  22358. + struct vchiq_mmal_component *component)
  22359. +{
  22360. + int ret;
  22361. +
  22362. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22363. + return -EINTR;
  22364. +
  22365. + if (!component->enabled) {
  22366. + mutex_unlock(&instance->vchiq_mutex);
  22367. + return 0;
  22368. + }
  22369. +
  22370. + ret = disable_component(instance, component);
  22371. + if (ret == 0)
  22372. + component->enabled = false;
  22373. +
  22374. + mutex_unlock(&instance->vchiq_mutex);
  22375. +
  22376. + return ret;
  22377. +}
  22378. +
  22379. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  22380. + u32 *major_out, u32 *minor_out)
  22381. +{
  22382. + int ret;
  22383. +
  22384. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22385. + return -EINTR;
  22386. +
  22387. + ret = get_version(instance, major_out, minor_out);
  22388. +
  22389. + mutex_unlock(&instance->vchiq_mutex);
  22390. +
  22391. + return ret;
  22392. +}
  22393. +
  22394. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance)
  22395. +{
  22396. + int status = 0;
  22397. +
  22398. + if (instance == NULL)
  22399. + return -EINVAL;
  22400. +
  22401. + if (mutex_lock_interruptible(&instance->vchiq_mutex))
  22402. + return -EINTR;
  22403. +
  22404. + vchi_service_use(instance->handle);
  22405. +
  22406. + status = vchi_service_close(instance->handle);
  22407. + if (status != 0)
  22408. + pr_err("mmal-vchiq: VCHIQ close failed");
  22409. +
  22410. + mutex_unlock(&instance->vchiq_mutex);
  22411. +
  22412. + vfree(instance->bulk_scratch);
  22413. +
  22414. + kfree(instance);
  22415. +
  22416. + return status;
  22417. +}
  22418. +
  22419. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance)
  22420. +{
  22421. + int status;
  22422. + struct vchiq_mmal_instance *instance;
  22423. + static VCHI_CONNECTION_T *vchi_connection;
  22424. + static VCHI_INSTANCE_T vchi_instance;
  22425. + SERVICE_CREATION_T params = {
  22426. + VCHI_VERSION_EX(VC_MMAL_VER, VC_MMAL_MIN_VER),
  22427. + VC_MMAL_SERVER_NAME,
  22428. + vchi_connection,
  22429. + 0, /* rx fifo size (unused) */
  22430. + 0, /* tx fifo size (unused) */
  22431. + service_callback,
  22432. + NULL, /* service callback parameter */
  22433. + 1, /* unaligned bulk receives */
  22434. + 1, /* unaligned bulk transmits */
  22435. + 0 /* want crc check on bulk transfers */
  22436. + };
  22437. +
  22438. + /* compile time checks to ensure structure size as they are
  22439. + * directly (de)serialised from memory.
  22440. + */
  22441. +
  22442. + /* ensure the header structure has packed to the correct size */
  22443. + BUILD_BUG_ON(sizeof(struct mmal_msg_header) != 24);
  22444. +
  22445. + /* ensure message structure does not exceed maximum length */
  22446. + BUILD_BUG_ON(sizeof(struct mmal_msg) > MMAL_MSG_MAX_SIZE);
  22447. +
  22448. + /* mmal port struct is correct size */
  22449. + BUILD_BUG_ON(sizeof(struct mmal_port) != 64);
  22450. +
  22451. + /* create a vchi instance */
  22452. + status = vchi_initialise(&vchi_instance);
  22453. + if (status) {
  22454. + pr_err("Failed to initialise VCHI instance (status=%d)\n",
  22455. + status);
  22456. + return -EIO;
  22457. + }
  22458. +
  22459. + status = vchi_connect(NULL, 0, vchi_instance);
  22460. + if (status) {
  22461. + pr_err("Failed to connect VCHI instance (status=%d)\n", status);
  22462. + return -EIO;
  22463. + }
  22464. +
  22465. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  22466. + memset(instance, 0, sizeof(*instance));
  22467. +
  22468. + mutex_init(&instance->vchiq_mutex);
  22469. + mutex_init(&instance->bulk_mutex);
  22470. +
  22471. + instance->bulk_scratch = vmalloc(PAGE_SIZE);
  22472. +
  22473. + params.callback_param = instance;
  22474. +
  22475. + status = vchi_service_open(vchi_instance, &params, &instance->handle);
  22476. + if (status) {
  22477. + pr_err("Failed to open VCHI service connection (status=%d)\n",
  22478. + status);
  22479. + goto err_close_services;
  22480. + }
  22481. +
  22482. + vchi_service_release(instance->handle);
  22483. +
  22484. + *out_instance = instance;
  22485. +
  22486. + return 0;
  22487. +
  22488. +err_close_services:
  22489. +
  22490. + vchi_service_close(instance->handle);
  22491. + vfree(instance->bulk_scratch);
  22492. + kfree(instance);
  22493. + return -ENODEV;
  22494. +}
  22495. diff -Nur linux-3.16.2/drivers/media/platform/bcm2835/mmal-vchiq.h linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h
  22496. --- linux-3.16.2/drivers/media/platform/bcm2835/mmal-vchiq.h 1970-01-01 01:00:00.000000000 +0100
  22497. +++ linux-3.16-rpi/drivers/media/platform/bcm2835/mmal-vchiq.h 2014-04-13 17:32:57.000000000 +0200
  22498. @@ -0,0 +1,178 @@
  22499. +/*
  22500. + * Broadcom BM2835 V4L2 driver
  22501. + *
  22502. + * Copyright © 2013 Raspberry Pi (Trading) Ltd.
  22503. + *
  22504. + * This file is subject to the terms and conditions of the GNU General Public
  22505. + * License. See the file COPYING in the main directory of this archive
  22506. + * for more details.
  22507. + *
  22508. + * Authors: Vincent Sanders <vincent.sanders@collabora.co.uk>
  22509. + * Dave Stevenson <dsteve@broadcom.com>
  22510. + * Simon Mellor <simellor@broadcom.com>
  22511. + * Luke Diamand <luked@broadcom.com>
  22512. + *
  22513. + * MMAL interface to VCHIQ message passing
  22514. + */
  22515. +
  22516. +#ifndef MMAL_VCHIQ_H
  22517. +#define MMAL_VCHIQ_H
  22518. +
  22519. +#include "mmal-msg-format.h"
  22520. +
  22521. +#define MAX_PORT_COUNT 4
  22522. +
  22523. +/* Maximum size of the format extradata. */
  22524. +#define MMAL_FORMAT_EXTRADATA_MAX_SIZE 128
  22525. +
  22526. +struct vchiq_mmal_instance;
  22527. +
  22528. +enum vchiq_mmal_es_type {
  22529. + MMAL_ES_TYPE_UNKNOWN, /**< Unknown elementary stream type */
  22530. + MMAL_ES_TYPE_CONTROL, /**< Elementary stream of control commands */
  22531. + MMAL_ES_TYPE_AUDIO, /**< Audio elementary stream */
  22532. + MMAL_ES_TYPE_VIDEO, /**< Video elementary stream */
  22533. + MMAL_ES_TYPE_SUBPICTURE /**< Sub-picture elementary stream */
  22534. +};
  22535. +
  22536. +/* rectangle, used lots so it gets its own struct */
  22537. +struct vchiq_mmal_rect {
  22538. + s32 x;
  22539. + s32 y;
  22540. + s32 width;
  22541. + s32 height;
  22542. +};
  22543. +
  22544. +struct vchiq_mmal_port_buffer {
  22545. + unsigned int num; /* number of buffers */
  22546. + u32 size; /* size of buffers */
  22547. + u32 alignment; /* alignment of buffers */
  22548. +};
  22549. +
  22550. +struct vchiq_mmal_port;
  22551. +
  22552. +typedef void (*vchiq_mmal_buffer_cb)(
  22553. + struct vchiq_mmal_instance *instance,
  22554. + struct vchiq_mmal_port *port,
  22555. + int status, struct mmal_buffer *buffer,
  22556. + unsigned long length, u32 mmal_flags, s64 dts, s64 pts);
  22557. +
  22558. +struct vchiq_mmal_port {
  22559. + bool enabled;
  22560. + u32 handle;
  22561. + u32 type; /* port type, cached to use on port info set */
  22562. + u32 index; /* port index, cached to use on port info set */
  22563. +
  22564. + /* component port belongs to, allows simple deref */
  22565. + struct vchiq_mmal_component *component;
  22566. +
  22567. + struct vchiq_mmal_port *connected; /* port conencted to */
  22568. +
  22569. + /* buffer info */
  22570. + struct vchiq_mmal_port_buffer minimum_buffer;
  22571. + struct vchiq_mmal_port_buffer recommended_buffer;
  22572. + struct vchiq_mmal_port_buffer current_buffer;
  22573. +
  22574. + /* stream format */
  22575. + struct mmal_es_format format;
  22576. + /* elementry stream format */
  22577. + union mmal_es_specific_format es;
  22578. +
  22579. + /* data buffers to fill */
  22580. + struct list_head buffers;
  22581. + /* lock to serialise adding and removing buffers from list */
  22582. + spinlock_t slock;
  22583. + /* count of how many buffer header refils have failed because
  22584. + * there was no buffer to satisfy them
  22585. + */
  22586. + int buffer_underflow;
  22587. + /* callback on buffer completion */
  22588. + vchiq_mmal_buffer_cb buffer_cb;
  22589. + /* callback context */
  22590. + void *cb_ctx;
  22591. +};
  22592. +
  22593. +struct vchiq_mmal_component {
  22594. + bool enabled;
  22595. + u32 handle; /* VideoCore handle for component */
  22596. + u32 inputs; /* Number of input ports */
  22597. + u32 outputs; /* Number of output ports */
  22598. + u32 clocks; /* Number of clock ports */
  22599. + struct vchiq_mmal_port control; /* control port */
  22600. + struct vchiq_mmal_port input[MAX_PORT_COUNT]; /* input ports */
  22601. + struct vchiq_mmal_port output[MAX_PORT_COUNT]; /* output ports */
  22602. + struct vchiq_mmal_port clock[MAX_PORT_COUNT]; /* clock ports */
  22603. +};
  22604. +
  22605. +
  22606. +int vchiq_mmal_init(struct vchiq_mmal_instance **out_instance);
  22607. +int vchiq_mmal_finalise(struct vchiq_mmal_instance *instance);
  22608. +
  22609. +/* Initialise a mmal component and its ports
  22610. +*
  22611. +*/
  22612. +int vchiq_mmal_component_init(
  22613. + struct vchiq_mmal_instance *instance,
  22614. + const char *name,
  22615. + struct vchiq_mmal_component **component_out);
  22616. +
  22617. +int vchiq_mmal_component_finalise(
  22618. + struct vchiq_mmal_instance *instance,
  22619. + struct vchiq_mmal_component *component);
  22620. +
  22621. +int vchiq_mmal_component_enable(
  22622. + struct vchiq_mmal_instance *instance,
  22623. + struct vchiq_mmal_component *component);
  22624. +
  22625. +int vchiq_mmal_component_disable(
  22626. + struct vchiq_mmal_instance *instance,
  22627. + struct vchiq_mmal_component *component);
  22628. +
  22629. +
  22630. +
  22631. +/* enable a mmal port
  22632. + *
  22633. + * enables a port and if a buffer callback provided enque buffer
  22634. + * headers as apropriate for the port.
  22635. + */
  22636. +int vchiq_mmal_port_enable(
  22637. + struct vchiq_mmal_instance *instance,
  22638. + struct vchiq_mmal_port *port,
  22639. + vchiq_mmal_buffer_cb buffer_cb);
  22640. +
  22641. +/* disable a port
  22642. + *
  22643. + * disable a port will dequeue any pending buffers
  22644. + */
  22645. +int vchiq_mmal_port_disable(struct vchiq_mmal_instance *instance,
  22646. + struct vchiq_mmal_port *port);
  22647. +
  22648. +
  22649. +int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
  22650. + struct vchiq_mmal_port *port,
  22651. + u32 parameter,
  22652. + void *value,
  22653. + u32 value_size);
  22654. +
  22655. +int vchiq_mmal_port_parameter_get(struct vchiq_mmal_instance *instance,
  22656. + struct vchiq_mmal_port *port,
  22657. + u32 parameter,
  22658. + void *value,
  22659. + u32 *value_size);
  22660. +
  22661. +int vchiq_mmal_port_set_format(struct vchiq_mmal_instance *instance,
  22662. + struct vchiq_mmal_port *port);
  22663. +
  22664. +int vchiq_mmal_port_connect_tunnel(struct vchiq_mmal_instance *instance,
  22665. + struct vchiq_mmal_port *src,
  22666. + struct vchiq_mmal_port *dst);
  22667. +
  22668. +int vchiq_mmal_version(struct vchiq_mmal_instance *instance,
  22669. + u32 *major_out,
  22670. + u32 *minor_out);
  22671. +
  22672. +int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
  22673. + struct vchiq_mmal_port *port,
  22674. + struct mmal_buffer *buf);
  22675. +
  22676. +#endif /* MMAL_VCHIQ_H */
  22677. diff -Nur linux-3.16.2/drivers/media/platform/Kconfig linux-3.16-rpi/drivers/media/platform/Kconfig
  22678. --- linux-3.16.2/drivers/media/platform/Kconfig 2014-09-06 01:37:11.000000000 +0200
  22679. +++ linux-3.16-rpi/drivers/media/platform/Kconfig 2014-09-14 19:03:24.000000000 +0200
  22680. @@ -120,6 +120,7 @@
  22681. source "drivers/media/platform/soc_camera/Kconfig"
  22682. source "drivers/media/platform/exynos4-is/Kconfig"
  22683. source "drivers/media/platform/s5p-tv/Kconfig"
  22684. +source "drivers/media/platform/bcm2835/Kconfig"
  22685. endif # V4L_PLATFORM_DRIVERS
  22686. diff -Nur linux-3.16.2/drivers/media/platform/Makefile linux-3.16-rpi/drivers/media/platform/Makefile
  22687. --- linux-3.16.2/drivers/media/platform/Makefile 2014-09-06 01:37:11.000000000 +0200
  22688. +++ linux-3.16-rpi/drivers/media/platform/Makefile 2014-09-14 19:03:24.000000000 +0200
  22689. @@ -51,4 +51,6 @@
  22690. obj-$(CONFIG_ARCH_OMAP) += omap/
  22691. +obj-$(CONFIG_VIDEO_BCM2835) += bcm2835/
  22692. +
  22693. ccflags-y += -I$(srctree)/drivers/media/i2c
  22694. diff -Nur linux-3.16.2/drivers/media/usb/dvb-usb-v2/rtl28xxu.c linux-3.16-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c
  22695. --- linux-3.16.2/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-09-06 01:37:11.000000000 +0200
  22696. +++ linux-3.16-rpi/drivers/media/usb/dvb-usb-v2/rtl28xxu.c 2014-09-14 19:03:24.000000000 +0200
  22697. @@ -1531,6 +1531,10 @@
  22698. &rtl2832u_props, "Compro VideoMate U620F", NULL) },
  22699. { DVB_USB_DEVICE(USB_VID_KWORLD_2, 0xd394,
  22700. &rtl2832u_props, "MaxMedia HU394-T", NULL) },
  22701. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xb803 /*USB_PID_AUGUST_DVBT205*/,
  22702. + &rtl2832u_props, "August DVB-T 205", NULL) },
  22703. + { DVB_USB_DEVICE(USB_VID_GTEK, 0xa803 /*USB_PID_AUGUST_DVBT205*/,
  22704. + &rtl2832u_props, "August DVB-T 205", NULL) },
  22705. { DVB_USB_DEVICE(USB_VID_LEADTEK, 0x6a03,
  22706. &rtl2832u_props, "Leadtek WinFast DTV Dongle mini", NULL) },
  22707. { DVB_USB_DEVICE(USB_VID_GTEK, USB_PID_CPYTO_REDI_PC50A,
  22708. diff -Nur linux-3.16.2/drivers/misc/Kconfig linux-3.16-rpi/drivers/misc/Kconfig
  22709. --- linux-3.16.2/drivers/misc/Kconfig 2014-09-06 01:37:11.000000000 +0200
  22710. +++ linux-3.16-rpi/drivers/misc/Kconfig 2014-09-14 19:03:25.000000000 +0200
  22711. @@ -534,6 +534,7 @@
  22712. source "drivers/misc/altera-stapl/Kconfig"
  22713. source "drivers/misc/mei/Kconfig"
  22714. source "drivers/misc/vmw_vmci/Kconfig"
  22715. +source "drivers/misc/vc04_services/Kconfig"
  22716. source "drivers/misc/mic/Kconfig"
  22717. source "drivers/misc/genwqe/Kconfig"
  22718. source "drivers/misc/echo/Kconfig"
  22719. diff -Nur linux-3.16.2/drivers/misc/Makefile linux-3.16-rpi/drivers/misc/Makefile
  22720. --- linux-3.16.2/drivers/misc/Makefile 2014-09-06 01:37:11.000000000 +0200
  22721. +++ linux-3.16-rpi/drivers/misc/Makefile 2014-09-14 19:03:25.000000000 +0200
  22722. @@ -52,6 +52,7 @@
  22723. obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
  22724. obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
  22725. obj-$(CONFIG_SRAM) += sram.o
  22726. +obj-$(CONFIG_BCM2708_VCHIQ) += vc04_services/
  22727. obj-y += mic/
  22728. obj-$(CONFIG_GENWQE) += genwqe/
  22729. obj-$(CONFIG_ECHO) += echo/
  22730. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchi/connections/connection.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h
  22731. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchi/connections/connection.h 1970-01-01 01:00:00.000000000 +0100
  22732. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/connections/connection.h 2014-04-13 17:32:57.000000000 +0200
  22733. @@ -0,0 +1,328 @@
  22734. +/**
  22735. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  22736. + *
  22737. + * Redistribution and use in source and binary forms, with or without
  22738. + * modification, are permitted provided that the following conditions
  22739. + * are met:
  22740. + * 1. Redistributions of source code must retain the above copyright
  22741. + * notice, this list of conditions, and the following disclaimer,
  22742. + * without modification.
  22743. + * 2. Redistributions in binary form must reproduce the above copyright
  22744. + * notice, this list of conditions and the following disclaimer in the
  22745. + * documentation and/or other materials provided with the distribution.
  22746. + * 3. The names of the above-listed copyright holders may not be used
  22747. + * to endorse or promote products derived from this software without
  22748. + * specific prior written permission.
  22749. + *
  22750. + * ALTERNATIVELY, this software may be distributed under the terms of the
  22751. + * GNU General Public License ("GPL") version 2, as published by the Free
  22752. + * Software Foundation.
  22753. + *
  22754. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  22755. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22756. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22757. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22758. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22759. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  22760. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22761. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  22762. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22763. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22764. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22765. + */
  22766. +
  22767. +#ifndef CONNECTION_H_
  22768. +#define CONNECTION_H_
  22769. +
  22770. +#include <linux/kernel.h>
  22771. +#include <linux/types.h>
  22772. +#include <linux/semaphore.h>
  22773. +
  22774. +#include "interface/vchi/vchi_cfg_internal.h"
  22775. +#include "interface/vchi/vchi_common.h"
  22776. +#include "interface/vchi/message_drivers/message.h"
  22777. +
  22778. +/******************************************************************************
  22779. + Global defs
  22780. + *****************************************************************************/
  22781. +
  22782. +// Opaque handle for a connection / service pair
  22783. +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
  22784. +
  22785. +// opaque handle to the connection state information
  22786. +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
  22787. +
  22788. +typedef struct vchi_connection_t VCHI_CONNECTION_T;
  22789. +
  22790. +
  22791. +/******************************************************************************
  22792. + API
  22793. + *****************************************************************************/
  22794. +
  22795. +// Routine to init a connection with a particular low level driver
  22796. +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
  22797. + const VCHI_MESSAGE_DRIVER_T * driver );
  22798. +
  22799. +// Routine to control CRC enabling at a connection level
  22800. +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
  22801. + VCHI_CRC_CONTROL_T control );
  22802. +
  22803. +// Routine to create a service
  22804. +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
  22805. + int32_t service_id,
  22806. + uint32_t rx_fifo_size,
  22807. + uint32_t tx_fifo_size,
  22808. + int server,
  22809. + VCHI_CALLBACK_T callback,
  22810. + void *callback_param,
  22811. + int32_t want_crc,
  22812. + int32_t want_unaligned_bulk_rx,
  22813. + int32_t want_unaligned_bulk_tx,
  22814. + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
  22815. +
  22816. +// Routine to close a service
  22817. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
  22818. +
  22819. +// Routine to queue a message
  22820. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22821. + const void *data,
  22822. + uint32_t data_size,
  22823. + VCHI_FLAGS_T flags,
  22824. + void *msg_handle );
  22825. +
  22826. +// scatter-gather (vector) message queueing
  22827. +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22828. + VCHI_MSG_VECTOR_T *vector,
  22829. + uint32_t count,
  22830. + VCHI_FLAGS_T flags,
  22831. + void *msg_handle );
  22832. +
  22833. +// Routine to dequeue a message
  22834. +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22835. + void *data,
  22836. + uint32_t max_data_size_to_read,
  22837. + uint32_t *actual_msg_size,
  22838. + VCHI_FLAGS_T flags );
  22839. +
  22840. +// Routine to peek at a message
  22841. +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22842. + void **data,
  22843. + uint32_t *msg_size,
  22844. + VCHI_FLAGS_T flags );
  22845. +
  22846. +// Routine to hold a message
  22847. +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22848. + void **data,
  22849. + uint32_t *msg_size,
  22850. + VCHI_FLAGS_T flags,
  22851. + void **message_handle );
  22852. +
  22853. +// Routine to initialise a received message iterator
  22854. +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22855. + VCHI_MSG_ITER_T *iter,
  22856. + VCHI_FLAGS_T flags );
  22857. +
  22858. +// Routine to release a held message
  22859. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22860. + void *message_handle );
  22861. +
  22862. +// Routine to get info on a held message
  22863. +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22864. + void *message_handle,
  22865. + void **data,
  22866. + int32_t *msg_size,
  22867. + uint32_t *tx_timestamp,
  22868. + uint32_t *rx_timestamp );
  22869. +
  22870. +// Routine to check whether the iterator has a next message
  22871. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  22872. + const VCHI_MSG_ITER_T *iter );
  22873. +
  22874. +// Routine to advance the iterator
  22875. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  22876. + VCHI_MSG_ITER_T *iter,
  22877. + void **data,
  22878. + uint32_t *msg_size );
  22879. +
  22880. +// Routine to remove the last message returned by the iterator
  22881. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  22882. + VCHI_MSG_ITER_T *iter );
  22883. +
  22884. +// Routine to hold the last message returned by the iterator
  22885. +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
  22886. + VCHI_MSG_ITER_T *iter,
  22887. + void **msg_handle );
  22888. +
  22889. +// Routine to transmit bulk data
  22890. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22891. + const void *data_src,
  22892. + uint32_t data_size,
  22893. + VCHI_FLAGS_T flags,
  22894. + void *bulk_handle );
  22895. +
  22896. +// Routine to receive data
  22897. +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
  22898. + void *data_dst,
  22899. + uint32_t data_size,
  22900. + VCHI_FLAGS_T flags,
  22901. + void *bulk_handle );
  22902. +
  22903. +// Routine to report if a server is available
  22904. +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
  22905. +
  22906. +// Routine to report the number of RX slots available
  22907. +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
  22908. +
  22909. +// Routine to report the RX slot size
  22910. +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
  22911. +
  22912. +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  22913. +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
  22914. + int32_t service,
  22915. + uint32_t length,
  22916. + MESSAGE_TX_CHANNEL_T channel,
  22917. + uint32_t channel_params,
  22918. + uint32_t data_length,
  22919. + uint32_t data_offset);
  22920. +
  22921. +// Callback to inform a service that a Xon or Xoff message has been received
  22922. +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
  22923. +
  22924. +// Callback to inform a service that a server available reply message has been received
  22925. +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
  22926. +
  22927. +// Callback to indicate that bulk auxiliary messages have arrived
  22928. +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
  22929. +
  22930. +// Callback to indicate that bulk auxiliary messages have arrived
  22931. +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
  22932. +
  22933. +// Callback with all the connection info you require
  22934. +typedef void (*VCHI_CONNECTION_INFO)(VCHI_CONNECTION_STATE_T *state, uint32_t protocol_version, uint32_t slot_size, uint32_t num_slots, uint32_t min_bulk_size);
  22935. +
  22936. +// Callback to inform of a disconnect
  22937. +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
  22938. +
  22939. +// Callback to inform of a power control request
  22940. +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
  22941. +
  22942. +// allocate memory suitably aligned for this connection
  22943. +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
  22944. +
  22945. +// free memory allocated by buffer_allocate
  22946. +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
  22947. +
  22948. +
  22949. +/******************************************************************************
  22950. + System driver struct
  22951. + *****************************************************************************/
  22952. +
  22953. +struct opaque_vchi_connection_api_t
  22954. +{
  22955. + // Routine to init the connection
  22956. + VCHI_CONNECTION_INIT_T init;
  22957. +
  22958. + // Connection-level CRC control
  22959. + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
  22960. +
  22961. + // Routine to connect to or create service
  22962. + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
  22963. +
  22964. + // Routine to disconnect from a service
  22965. + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
  22966. +
  22967. + // Routine to queue a message
  22968. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
  22969. +
  22970. + // scatter-gather (vector) message queue
  22971. + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
  22972. +
  22973. + // Routine to dequeue a message
  22974. + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
  22975. +
  22976. + // Routine to peek at a message
  22977. + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
  22978. +
  22979. + // Routine to hold a message
  22980. + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
  22981. +
  22982. + // Routine to initialise a received message iterator
  22983. + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
  22984. +
  22985. + // Routine to release a message
  22986. + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
  22987. +
  22988. + // Routine to get information on a held message
  22989. + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
  22990. +
  22991. + // Routine to check for next message on iterator
  22992. + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
  22993. +
  22994. + // Routine to get next message on iterator
  22995. + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
  22996. +
  22997. + // Routine to remove the last message returned by iterator
  22998. + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
  22999. +
  23000. + // Routine to hold the last message returned by iterator
  23001. + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
  23002. +
  23003. + // Routine to transmit bulk data
  23004. + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
  23005. +
  23006. + // Routine to receive data
  23007. + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
  23008. +
  23009. + // Routine to report the available servers
  23010. + VCHI_CONNECTION_SERVER_PRESENT server_present;
  23011. +
  23012. + // Routine to report the number of RX slots available
  23013. + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
  23014. +
  23015. + // Routine to report the RX slot size
  23016. + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
  23017. +
  23018. + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
  23019. + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
  23020. +
  23021. + // Callback to inform a service that a Xon or Xoff message has been received
  23022. + VCHI_CONNECTION_FLOW_CONTROL flow_control;
  23023. +
  23024. + // Callback to inform a service that a server available reply message has been received
  23025. + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
  23026. +
  23027. + // Callback to indicate that bulk auxiliary messages have arrived
  23028. + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
  23029. +
  23030. + // Callback to indicate that a bulk auxiliary message has been transmitted
  23031. + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
  23032. +
  23033. + // Callback to provide information about the connection
  23034. + VCHI_CONNECTION_INFO connection_info;
  23035. +
  23036. + // Callback to notify that peer has requested disconnect
  23037. + VCHI_CONNECTION_DISCONNECT disconnect;
  23038. +
  23039. + // Callback to notify that peer has requested power change
  23040. + VCHI_CONNECTION_POWER_CONTROL power_control;
  23041. +
  23042. + // allocate memory suitably aligned for this connection
  23043. + VCHI_BUFFER_ALLOCATE buffer_allocate;
  23044. +
  23045. + // free memory allocated by buffer_allocate
  23046. + VCHI_BUFFER_FREE buffer_free;
  23047. +
  23048. +};
  23049. +
  23050. +struct vchi_connection_t {
  23051. + const VCHI_CONNECTION_API_T *api;
  23052. + VCHI_CONNECTION_STATE_T *state;
  23053. +#ifdef VCHI_COARSE_LOCKING
  23054. + struct semaphore sem;
  23055. +#endif
  23056. +};
  23057. +
  23058. +
  23059. +#endif /* CONNECTION_H_ */
  23060. +
  23061. +/****************************** End of file **********************************/
  23062. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
  23063. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 1970-01-01 01:00:00.000000000 +0100
  23064. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h 2014-04-13 17:32:57.000000000 +0200
  23065. @@ -0,0 +1,204 @@
  23066. +/**
  23067. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23068. + *
  23069. + * Redistribution and use in source and binary forms, with or without
  23070. + * modification, are permitted provided that the following conditions
  23071. + * are met:
  23072. + * 1. Redistributions of source code must retain the above copyright
  23073. + * notice, this list of conditions, and the following disclaimer,
  23074. + * without modification.
  23075. + * 2. Redistributions in binary form must reproduce the above copyright
  23076. + * notice, this list of conditions and the following disclaimer in the
  23077. + * documentation and/or other materials provided with the distribution.
  23078. + * 3. The names of the above-listed copyright holders may not be used
  23079. + * to endorse or promote products derived from this software without
  23080. + * specific prior written permission.
  23081. + *
  23082. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23083. + * GNU General Public License ("GPL") version 2, as published by the Free
  23084. + * Software Foundation.
  23085. + *
  23086. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23087. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23088. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23089. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23090. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23091. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23092. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23093. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23094. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23095. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23096. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23097. + */
  23098. +
  23099. +#ifndef _VCHI_MESSAGE_H_
  23100. +#define _VCHI_MESSAGE_H_
  23101. +
  23102. +#include <linux/kernel.h>
  23103. +#include <linux/types.h>
  23104. +#include <linux/semaphore.h>
  23105. +
  23106. +#include "interface/vchi/vchi_cfg_internal.h"
  23107. +#include "interface/vchi/vchi_common.h"
  23108. +
  23109. +
  23110. +typedef enum message_event_type {
  23111. + MESSAGE_EVENT_NONE,
  23112. + MESSAGE_EVENT_NOP,
  23113. + MESSAGE_EVENT_MESSAGE,
  23114. + MESSAGE_EVENT_SLOT_COMPLETE,
  23115. + MESSAGE_EVENT_RX_BULK_PAUSED,
  23116. + MESSAGE_EVENT_RX_BULK_COMPLETE,
  23117. + MESSAGE_EVENT_TX_COMPLETE,
  23118. + MESSAGE_EVENT_MSG_DISCARDED
  23119. +} MESSAGE_EVENT_TYPE_T;
  23120. +
  23121. +typedef enum vchi_msg_flags
  23122. +{
  23123. + VCHI_MSG_FLAGS_NONE = 0x0,
  23124. + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
  23125. +} VCHI_MSG_FLAGS_T;
  23126. +
  23127. +typedef enum message_tx_channel
  23128. +{
  23129. + MESSAGE_TX_CHANNEL_MESSAGE = 0,
  23130. + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  23131. +} MESSAGE_TX_CHANNEL_T;
  23132. +
  23133. +// Macros used for cycling through bulk channels
  23134. +#define MESSAGE_TX_CHANNEL_BULK_PREV(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION-1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  23135. +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
  23136. +
  23137. +typedef enum message_rx_channel
  23138. +{
  23139. + MESSAGE_RX_CHANNEL_MESSAGE = 0,
  23140. + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
  23141. +} MESSAGE_RX_CHANNEL_T;
  23142. +
  23143. +// Message receive slot information
  23144. +typedef struct rx_msg_slot_info {
  23145. +
  23146. + struct rx_msg_slot_info *next;
  23147. + //struct slot_info *prev;
  23148. +#if !defined VCHI_COARSE_LOCKING
  23149. + struct semaphore sem;
  23150. +#endif
  23151. +
  23152. + uint8_t *addr; // base address of slot
  23153. + uint32_t len; // length of slot in bytes
  23154. +
  23155. + uint32_t write_ptr; // hardware causes this to advance
  23156. + uint32_t read_ptr; // this module does the reading
  23157. + int active; // is this slot in the hardware dma fifo?
  23158. + uint32_t msgs_parsed; // count how many messages are in this slot
  23159. + uint32_t msgs_released; // how many messages have been released
  23160. + void *state; // connection state information
  23161. + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
  23162. +} RX_MSG_SLOTINFO_T;
  23163. +
  23164. +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
  23165. +// In particular, it mustn't use addr and len - they're the client buffer, but the message
  23166. +// driver will be tasked with sending the aligned core section.
  23167. +typedef struct rx_bulk_slotinfo_t {
  23168. + struct rx_bulk_slotinfo_t *next;
  23169. +
  23170. + struct semaphore *blocking;
  23171. +
  23172. + // needed by DMA
  23173. + void *addr;
  23174. + uint32_t len;
  23175. +
  23176. + // needed for the callback
  23177. + void *service;
  23178. + void *handle;
  23179. + VCHI_FLAGS_T flags;
  23180. +} RX_BULK_SLOTINFO_T;
  23181. +
  23182. +
  23183. +/* ----------------------------------------------------------------------
  23184. + * each connection driver will have a pool of the following struct.
  23185. + *
  23186. + * the pool will be managed by vchi_qman_*
  23187. + * this means there will be multiple queues (single linked lists)
  23188. + * a given struct message_info will be on exactly one of these queues
  23189. + * at any one time
  23190. + * -------------------------------------------------------------------- */
  23191. +typedef struct rx_message_info {
  23192. +
  23193. + struct message_info *next;
  23194. + //struct message_info *prev;
  23195. +
  23196. + uint8_t *addr;
  23197. + uint32_t len;
  23198. + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
  23199. + uint32_t tx_timestamp;
  23200. + uint32_t rx_timestamp;
  23201. +
  23202. +} RX_MESSAGE_INFO_T;
  23203. +
  23204. +typedef struct {
  23205. + MESSAGE_EVENT_TYPE_T type;
  23206. +
  23207. + struct {
  23208. + // for messages
  23209. + void *addr; // address of message
  23210. + uint16_t slot_delta; // whether this message indicated slot delta
  23211. + uint32_t len; // length of message
  23212. + RX_MSG_SLOTINFO_T *slot; // slot this message is in
  23213. + int32_t service; // service id this message is destined for
  23214. + uint32_t tx_timestamp; // timestamp from the header
  23215. + uint32_t rx_timestamp; // timestamp when we parsed it
  23216. + } message;
  23217. +
  23218. + // FIXME: cleanup slot reporting...
  23219. + RX_MSG_SLOTINFO_T *rx_msg;
  23220. + RX_BULK_SLOTINFO_T *rx_bulk;
  23221. + void *tx_handle;
  23222. + MESSAGE_TX_CHANNEL_T tx_channel;
  23223. +
  23224. +} MESSAGE_EVENT_T;
  23225. +
  23226. +
  23227. +// callbacks
  23228. +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
  23229. +
  23230. +typedef struct {
  23231. + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
  23232. +} VCHI_MESSAGE_DRIVER_OPEN_T;
  23233. +
  23234. +
  23235. +// handle to this instance of message driver (as returned by ->open)
  23236. +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
  23237. +
  23238. +struct opaque_vchi_message_driver_t {
  23239. + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
  23240. + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
  23241. + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
  23242. + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
  23243. + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
  23244. + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
  23245. + int32_t (*send)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, VCHI_MSG_FLAGS_T flags, void *send_handle ); // tx (message & bulk)
  23246. + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
  23247. + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
  23248. + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
  23249. + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
  23250. +
  23251. + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
  23252. + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
  23253. + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
  23254. + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
  23255. + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  23256. + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
  23257. +
  23258. + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  23259. + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  23260. + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
  23261. + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
  23262. + void (*form_bulk_aux)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, uint32_t chunk_size, const void **aux_data, int32_t *aux_len );
  23263. + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
  23264. +};
  23265. +
  23266. +
  23267. +#endif // _VCHI_MESSAGE_H_
  23268. +
  23269. +/****************************** End of file ***********************************/
  23270. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
  23271. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 1970-01-01 01:00:00.000000000 +0100
  23272. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h 2014-04-13 17:32:57.000000000 +0200
  23273. @@ -0,0 +1,224 @@
  23274. +/**
  23275. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23276. + *
  23277. + * Redistribution and use in source and binary forms, with or without
  23278. + * modification, are permitted provided that the following conditions
  23279. + * are met:
  23280. + * 1. Redistributions of source code must retain the above copyright
  23281. + * notice, this list of conditions, and the following disclaimer,
  23282. + * without modification.
  23283. + * 2. Redistributions in binary form must reproduce the above copyright
  23284. + * notice, this list of conditions and the following disclaimer in the
  23285. + * documentation and/or other materials provided with the distribution.
  23286. + * 3. The names of the above-listed copyright holders may not be used
  23287. + * to endorse or promote products derived from this software without
  23288. + * specific prior written permission.
  23289. + *
  23290. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23291. + * GNU General Public License ("GPL") version 2, as published by the Free
  23292. + * Software Foundation.
  23293. + *
  23294. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23295. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23296. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23297. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23298. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23299. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23300. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23301. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23302. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23303. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23304. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23305. + */
  23306. +
  23307. +#ifndef VCHI_CFG_H_
  23308. +#define VCHI_CFG_H_
  23309. +
  23310. +/****************************************************************************************
  23311. + * Defines in this first section are part of the VCHI API and may be examined by VCHI
  23312. + * services.
  23313. + ***************************************************************************************/
  23314. +
  23315. +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
  23316. +/* Really determined by the message driver, and should be available from a run-time call. */
  23317. +#ifndef VCHI_BULK_ALIGN
  23318. +# if __VCCOREVER__ >= 0x04000000
  23319. +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
  23320. +# else
  23321. +# define VCHI_BULK_ALIGN 16
  23322. +# endif
  23323. +#endif
  23324. +
  23325. +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
  23326. +/* May be less than or greater than VCHI_BULK_ALIGN */
  23327. +/* Really determined by the message driver, and should be available from a run-time call. */
  23328. +#ifndef VCHI_BULK_GRANULARITY
  23329. +# if __VCCOREVER__ >= 0x04000000
  23330. +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
  23331. +# else
  23332. +# define VCHI_BULK_GRANULARITY 16
  23333. +# endif
  23334. +#endif
  23335. +
  23336. +/* The largest possible message to be queued with vchi_msg_queue. */
  23337. +#ifndef VCHI_MAX_MSG_SIZE
  23338. +# if defined VCHI_LOCAL_HOST_PORT
  23339. +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
  23340. +# else
  23341. +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
  23342. +# endif
  23343. +#endif
  23344. +
  23345. +/******************************************************************************************
  23346. + * Defines below are system configuration options, and should not be used by VCHI services.
  23347. + *****************************************************************************************/
  23348. +
  23349. +/* How many connections can we support? A localhost implementation uses 2 connections,
  23350. + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
  23351. + * driver. */
  23352. +#ifndef VCHI_MAX_NUM_CONNECTIONS
  23353. +# define VCHI_MAX_NUM_CONNECTIONS 3
  23354. +#endif
  23355. +
  23356. +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
  23357. + * amount of static memory. */
  23358. +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
  23359. +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
  23360. +#endif
  23361. +
  23362. +/* Adjust if using a message driver that supports more logical TX channels */
  23363. +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
  23364. +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
  23365. +#endif
  23366. +
  23367. +/* Adjust if using a message driver that supports more logical RX channels */
  23368. +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
  23369. +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
  23370. +#endif
  23371. +
  23372. +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
  23373. + * receive queue space, less message headers. */
  23374. +#ifndef VCHI_NUM_READ_SLOTS
  23375. +# if defined(VCHI_LOCAL_HOST_PORT)
  23376. +# define VCHI_NUM_READ_SLOTS 4
  23377. +# else
  23378. +# define VCHI_NUM_READ_SLOTS 48
  23379. +# endif
  23380. +#endif
  23381. +
  23382. +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
  23383. + * performance. Only define on VideoCore end, talking to host.
  23384. + */
  23385. +//#define VCHI_MSG_RX_OVERRUN
  23386. +
  23387. +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
  23388. + * underneath VCHI will usually have its own buffering. */
  23389. +#ifndef VCHI_NUM_WRITE_SLOTS
  23390. +# define VCHI_NUM_WRITE_SLOTS 4
  23391. +#endif
  23392. +
  23393. +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
  23394. + * then it's taking up too much buffer space, and the peer service will be told to stop
  23395. + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
  23396. + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
  23397. + * is too high. */
  23398. +#ifndef VCHI_XOFF_THRESHOLD
  23399. +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
  23400. +#endif
  23401. +
  23402. +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
  23403. + * service has dequeued/released enough messages that it's now occupying
  23404. + * VCHI_XON_THRESHOLD slots or fewer. */
  23405. +#ifndef VCHI_XON_THRESHOLD
  23406. +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
  23407. +#endif
  23408. +
  23409. +/* A size below which a bulk transfer omits the handshake completely and always goes
  23410. + * via the message channel, if bulk auxiliary is being sent on that service. (The user
  23411. + * can guarantee this by enabling unaligned transmits).
  23412. + * Not API. */
  23413. +#ifndef VCHI_MIN_BULK_SIZE
  23414. +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
  23415. +#endif
  23416. +
  23417. +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
  23418. + * speed and latency; the smaller the chunk size the better change of messages and other
  23419. + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
  23420. + * break transmissions into chunks.
  23421. + */
  23422. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
  23423. +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
  23424. +#endif
  23425. +
  23426. +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
  23427. + * with multiple-line frames. Only use if the receiver can cope. */
  23428. +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
  23429. +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
  23430. +#endif
  23431. +
  23432. +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
  23433. + * vchi_msg_queue will be blocked. */
  23434. +#ifndef VCHI_TX_MSG_QUEUE_SIZE
  23435. +# define VCHI_TX_MSG_QUEUE_SIZE 256
  23436. +#endif
  23437. +
  23438. +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
  23439. + * will be suspended until older messages are dequeued/released. */
  23440. +#ifndef VCHI_RX_MSG_QUEUE_SIZE
  23441. +# define VCHI_RX_MSG_QUEUE_SIZE 256
  23442. +#endif
  23443. +
  23444. +/* Really should be able to cope if we run out of received message descriptors, by
  23445. + * suspending parsing as the comment above says, but we don't. This sweeps the issue
  23446. + * under the carpet. */
  23447. +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  23448. +# undef VCHI_RX_MSG_QUEUE_SIZE
  23449. +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
  23450. +#endif
  23451. +
  23452. +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
  23453. + * will be blocked. */
  23454. +#ifndef VCHI_TX_BULK_QUEUE_SIZE
  23455. +# define VCHI_TX_BULK_QUEUE_SIZE 64
  23456. +#endif
  23457. +
  23458. +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
  23459. + * will be blocked. */
  23460. +#ifndef VCHI_RX_BULK_QUEUE_SIZE
  23461. +# define VCHI_RX_BULK_QUEUE_SIZE 64
  23462. +#endif
  23463. +
  23464. +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
  23465. + * the peer asks for more than this, VCHI will fail and assert. The number is determined
  23466. + * by the peer's hardware - it's the number of outstanding requests that can be queued
  23467. + * on all bulk channels. VC3's MPHI peripheral allows 16. */
  23468. +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
  23469. +# define VCHI_MAX_PEER_BULK_REQUESTS 32
  23470. +#endif
  23471. +
  23472. +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
  23473. + * transmitter on and off.
  23474. + */
  23475. +/*#define VCHI_CCP2TX_MANUAL_POWER*/
  23476. +
  23477. +#ifndef VCHI_CCP2TX_MANUAL_POWER
  23478. +
  23479. +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
  23480. + * negative for no IDLE.
  23481. + */
  23482. +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
  23483. +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
  23484. +# endif
  23485. +
  23486. +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
  23487. + * negative for no OFF.
  23488. + */
  23489. +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
  23490. +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
  23491. +# endif
  23492. +
  23493. +#endif /* VCHI_CCP2TX_MANUAL_POWER */
  23494. +
  23495. +#endif /* VCHI_CFG_H_ */
  23496. +
  23497. +/****************************** End of file **********************************/
  23498. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
  23499. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 1970-01-01 01:00:00.000000000 +0100
  23500. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h 2014-04-13 17:32:57.000000000 +0200
  23501. @@ -0,0 +1,71 @@
  23502. +/**
  23503. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23504. + *
  23505. + * Redistribution and use in source and binary forms, with or without
  23506. + * modification, are permitted provided that the following conditions
  23507. + * are met:
  23508. + * 1. Redistributions of source code must retain the above copyright
  23509. + * notice, this list of conditions, and the following disclaimer,
  23510. + * without modification.
  23511. + * 2. Redistributions in binary form must reproduce the above copyright
  23512. + * notice, this list of conditions and the following disclaimer in the
  23513. + * documentation and/or other materials provided with the distribution.
  23514. + * 3. The names of the above-listed copyright holders may not be used
  23515. + * to endorse or promote products derived from this software without
  23516. + * specific prior written permission.
  23517. + *
  23518. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23519. + * GNU General Public License ("GPL") version 2, as published by the Free
  23520. + * Software Foundation.
  23521. + *
  23522. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23523. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23524. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23525. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23526. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23527. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23528. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23529. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23530. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23531. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23532. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23533. + */
  23534. +
  23535. +#ifndef VCHI_CFG_INTERNAL_H_
  23536. +#define VCHI_CFG_INTERNAL_H_
  23537. +
  23538. +/****************************************************************************************
  23539. + * Control optimisation attempts.
  23540. + ***************************************************************************************/
  23541. +
  23542. +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
  23543. +#define VCHI_COARSE_LOCKING
  23544. +
  23545. +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
  23546. +// (only relevant if VCHI_COARSE_LOCKING)
  23547. +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
  23548. +
  23549. +// Avoid lock on non-blocking peek
  23550. +// (only relevant if VCHI_COARSE_LOCKING)
  23551. +#define VCHI_AVOID_PEEK_LOCK
  23552. +
  23553. +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
  23554. +#define VCHI_MULTIPLE_HANDLER_THREADS
  23555. +
  23556. +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
  23557. +// our way through the pool of descriptors.
  23558. +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
  23559. +
  23560. +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
  23561. +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
  23562. +
  23563. +// Don't use message descriptors for TX messages that don't need them
  23564. +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
  23565. +
  23566. +// Nano-locks for multiqueue
  23567. +//#define VCHI_MQUEUE_NANOLOCKS
  23568. +
  23569. +// Lock-free(er) dequeuing
  23570. +//#define VCHI_RX_NANOLOCKS
  23571. +
  23572. +#endif /*VCHI_CFG_INTERNAL_H_*/
  23573. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_common.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h
  23574. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_common.h 1970-01-01 01:00:00.000000000 +0100
  23575. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_common.h 2014-04-13 17:32:57.000000000 +0200
  23576. @@ -0,0 +1,163 @@
  23577. +/**
  23578. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23579. + *
  23580. + * Redistribution and use in source and binary forms, with or without
  23581. + * modification, are permitted provided that the following conditions
  23582. + * are met:
  23583. + * 1. Redistributions of source code must retain the above copyright
  23584. + * notice, this list of conditions, and the following disclaimer,
  23585. + * without modification.
  23586. + * 2. Redistributions in binary form must reproduce the above copyright
  23587. + * notice, this list of conditions and the following disclaimer in the
  23588. + * documentation and/or other materials provided with the distribution.
  23589. + * 3. The names of the above-listed copyright holders may not be used
  23590. + * to endorse or promote products derived from this software without
  23591. + * specific prior written permission.
  23592. + *
  23593. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23594. + * GNU General Public License ("GPL") version 2, as published by the Free
  23595. + * Software Foundation.
  23596. + *
  23597. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23598. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23599. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23600. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23601. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23602. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23603. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23604. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23605. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23606. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23607. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23608. + */
  23609. +
  23610. +#ifndef VCHI_COMMON_H_
  23611. +#define VCHI_COMMON_H_
  23612. +
  23613. +
  23614. +//flags used when sending messages (must be bitmapped)
  23615. +typedef enum
  23616. +{
  23617. + VCHI_FLAGS_NONE = 0x0,
  23618. + VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE = 0x1, // waits for message to be received, or sent (NB. not the same as being seen on other side)
  23619. + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
  23620. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
  23621. + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
  23622. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
  23623. + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
  23624. +
  23625. + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
  23626. + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
  23627. + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
  23628. + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
  23629. + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
  23630. + VCHI_FLAGS_INTERNAL = 0xFF0000
  23631. +} VCHI_FLAGS_T;
  23632. +
  23633. +// constants for vchi_crc_control()
  23634. +typedef enum {
  23635. + VCHI_CRC_NOTHING = -1,
  23636. + VCHI_CRC_PER_SERVICE = 0,
  23637. + VCHI_CRC_EVERYTHING = 1,
  23638. +} VCHI_CRC_CONTROL_T;
  23639. +
  23640. +//callback reasons when an event occurs on a service
  23641. +typedef enum
  23642. +{
  23643. + VCHI_CALLBACK_REASON_MIN,
  23644. +
  23645. + //This indicates that there is data available
  23646. + //handle is the msg id that was transmitted with the data
  23647. + // When a message is received and there was no FULL message available previously, send callback
  23648. + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
  23649. + VCHI_CALLBACK_MSG_AVAILABLE,
  23650. + VCHI_CALLBACK_MSG_SENT,
  23651. + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
  23652. +
  23653. + // This indicates that a transfer from the other side has completed
  23654. + VCHI_CALLBACK_BULK_RECEIVED,
  23655. + //This indicates that data queued up to be sent has now gone
  23656. + //handle is the msg id that was used when sending the data
  23657. + VCHI_CALLBACK_BULK_SENT,
  23658. + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
  23659. + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
  23660. +
  23661. + VCHI_CALLBACK_SERVICE_CLOSED,
  23662. +
  23663. + // this side has sent XOFF to peer due to lack of data consumption by service
  23664. + // (suggests the service may need to take some recovery action if it has
  23665. + // been deliberately holding off consuming data)
  23666. + VCHI_CALLBACK_SENT_XOFF,
  23667. + VCHI_CALLBACK_SENT_XON,
  23668. +
  23669. + // indicates that a bulk transfer has finished reading the source buffer
  23670. + VCHI_CALLBACK_BULK_DATA_READ,
  23671. +
  23672. + // power notification events (currently host side only)
  23673. + VCHI_CALLBACK_PEER_OFF,
  23674. + VCHI_CALLBACK_PEER_SUSPENDED,
  23675. + VCHI_CALLBACK_PEER_ON,
  23676. + VCHI_CALLBACK_PEER_RESUMED,
  23677. + VCHI_CALLBACK_FORCED_POWER_OFF,
  23678. +
  23679. +#ifdef USE_VCHIQ_ARM
  23680. + // some extra notifications provided by vchiq_arm
  23681. + VCHI_CALLBACK_SERVICE_OPENED,
  23682. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  23683. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  23684. +#endif
  23685. +
  23686. + VCHI_CALLBACK_REASON_MAX
  23687. +} VCHI_CALLBACK_REASON_T;
  23688. +
  23689. +//Calback used by all services / bulk transfers
  23690. +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
  23691. + VCHI_CALLBACK_REASON_T reason,
  23692. + void *handle ); //for transmitting msg's only
  23693. +
  23694. +
  23695. +
  23696. +/*
  23697. + * Define vector struct for scatter-gather (vector) operations
  23698. + * Vectors can be nested - if a vector element has negative length, then
  23699. + * the data pointer is treated as pointing to another vector array, with
  23700. + * '-vec_len' elements. Thus to append a header onto an existing vector,
  23701. + * you can do this:
  23702. + *
  23703. + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
  23704. + * {
  23705. + * VCHI_MSG_VECTOR_T nv[2];
  23706. + * nv[0].vec_base = my_header;
  23707. + * nv[0].vec_len = sizeof my_header;
  23708. + * nv[1].vec_base = v;
  23709. + * nv[1].vec_len = -n;
  23710. + * ...
  23711. + *
  23712. + */
  23713. +typedef struct vchi_msg_vector {
  23714. + const void *vec_base;
  23715. + int32_t vec_len;
  23716. +} VCHI_MSG_VECTOR_T;
  23717. +
  23718. +// Opaque type for a connection API
  23719. +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
  23720. +
  23721. +// Opaque type for a message driver
  23722. +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
  23723. +
  23724. +
  23725. +// Iterator structure for reading ahead through received message queue. Allocated by client,
  23726. +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
  23727. +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
  23728. +// will not proceed to messages received since. Behaviour is undefined if an iterator
  23729. +// is used again after messages for that service are removed/dequeued by any
  23730. +// means other than vchi_msg_iter_... calls on the iterator itself.
  23731. +typedef struct {
  23732. + struct opaque_vchi_service_t *service;
  23733. + void *last;
  23734. + void *next;
  23735. + void *remove;
  23736. +} VCHI_MSG_ITER_T;
  23737. +
  23738. +
  23739. +#endif // VCHI_COMMON_H_
  23740. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h
  23741. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi.h 1970-01-01 01:00:00.000000000 +0100
  23742. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi.h 2014-04-13 17:32:57.000000000 +0200
  23743. @@ -0,0 +1,373 @@
  23744. +/**
  23745. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  23746. + *
  23747. + * Redistribution and use in source and binary forms, with or without
  23748. + * modification, are permitted provided that the following conditions
  23749. + * are met:
  23750. + * 1. Redistributions of source code must retain the above copyright
  23751. + * notice, this list of conditions, and the following disclaimer,
  23752. + * without modification.
  23753. + * 2. Redistributions in binary form must reproduce the above copyright
  23754. + * notice, this list of conditions and the following disclaimer in the
  23755. + * documentation and/or other materials provided with the distribution.
  23756. + * 3. The names of the above-listed copyright holders may not be used
  23757. + * to endorse or promote products derived from this software without
  23758. + * specific prior written permission.
  23759. + *
  23760. + * ALTERNATIVELY, this software may be distributed under the terms of the
  23761. + * GNU General Public License ("GPL") version 2, as published by the Free
  23762. + * Software Foundation.
  23763. + *
  23764. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  23765. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23766. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23767. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23768. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23769. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23770. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  23771. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23772. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23773. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23774. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23775. + */
  23776. +
  23777. +#ifndef VCHI_H_
  23778. +#define VCHI_H_
  23779. +
  23780. +#include "interface/vchi/vchi_cfg.h"
  23781. +#include "interface/vchi/vchi_common.h"
  23782. +#include "interface/vchi/connections/connection.h"
  23783. +#include "vchi_mh.h"
  23784. +
  23785. +
  23786. +/******************************************************************************
  23787. + Global defs
  23788. + *****************************************************************************/
  23789. +
  23790. +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
  23791. +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
  23792. +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
  23793. +
  23794. +#ifdef USE_VCHIQ_ARM
  23795. +#define VCHI_BULK_ALIGNED(x) 1
  23796. +#else
  23797. +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
  23798. +#endif
  23799. +
  23800. +struct vchi_version {
  23801. + uint32_t version;
  23802. + uint32_t version_min;
  23803. +};
  23804. +#define VCHI_VERSION(v_) { v_, v_ }
  23805. +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
  23806. +
  23807. +typedef enum
  23808. +{
  23809. + VCHI_VEC_POINTER,
  23810. + VCHI_VEC_HANDLE,
  23811. + VCHI_VEC_LIST
  23812. +} VCHI_MSG_VECTOR_TYPE_T;
  23813. +
  23814. +typedef struct vchi_msg_vector_ex {
  23815. +
  23816. + VCHI_MSG_VECTOR_TYPE_T type;
  23817. + union
  23818. + {
  23819. + // a memory handle
  23820. + struct
  23821. + {
  23822. + VCHI_MEM_HANDLE_T handle;
  23823. + uint32_t offset;
  23824. + int32_t vec_len;
  23825. + } handle;
  23826. +
  23827. + // an ordinary data pointer
  23828. + struct
  23829. + {
  23830. + const void *vec_base;
  23831. + int32_t vec_len;
  23832. + } ptr;
  23833. +
  23834. + // a nested vector list
  23835. + struct
  23836. + {
  23837. + struct vchi_msg_vector_ex *vec;
  23838. + uint32_t vec_len;
  23839. + } list;
  23840. + } u;
  23841. +} VCHI_MSG_VECTOR_EX_T;
  23842. +
  23843. +
  23844. +// Construct an entry in a msg vector for a pointer (p) of length (l)
  23845. +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
  23846. +
  23847. +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
  23848. +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
  23849. +
  23850. +// Macros to manipulate 'FOURCC' values
  23851. +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
  23852. +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
  23853. +
  23854. +
  23855. +// Opaque service information
  23856. +struct opaque_vchi_service_t;
  23857. +
  23858. +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
  23859. +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
  23860. +typedef struct
  23861. +{
  23862. + struct opaque_vchi_service_t *service;
  23863. + void *message;
  23864. +} VCHI_HELD_MSG_T;
  23865. +
  23866. +
  23867. +
  23868. +// structure used to provide the information needed to open a server or a client
  23869. +typedef struct {
  23870. + struct vchi_version version;
  23871. + int32_t service_id;
  23872. + VCHI_CONNECTION_T *connection;
  23873. + uint32_t rx_fifo_size;
  23874. + uint32_t tx_fifo_size;
  23875. + VCHI_CALLBACK_T callback;
  23876. + void *callback_param;
  23877. + /* client intends to receive bulk transfers of
  23878. + odd lengths or into unaligned buffers */
  23879. + int32_t want_unaligned_bulk_rx;
  23880. + /* client intends to transmit bulk transfers of
  23881. + odd lengths or out of unaligned buffers */
  23882. + int32_t want_unaligned_bulk_tx;
  23883. + /* client wants to check CRCs on (bulk) xfers.
  23884. + Only needs to be set at 1 end - will do both directions. */
  23885. + int32_t want_crc;
  23886. +} SERVICE_CREATION_T;
  23887. +
  23888. +// Opaque handle for a VCHI instance
  23889. +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
  23890. +
  23891. +// Opaque handle for a server or client
  23892. +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
  23893. +
  23894. +// Service registration & startup
  23895. +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
  23896. +
  23897. +typedef struct service_info_tag {
  23898. + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
  23899. + VCHI_SERVICE_INIT init; /* Service initialisation function */
  23900. + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
  23901. +} SERVICE_INFO_T;
  23902. +
  23903. +/******************************************************************************
  23904. + Global funcs - implementation is specific to which side you are on (local / remote)
  23905. + *****************************************************************************/
  23906. +
  23907. +#ifdef __cplusplus
  23908. +extern "C" {
  23909. +#endif
  23910. +
  23911. +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
  23912. + const VCHI_MESSAGE_DRIVER_T * low_level);
  23913. +
  23914. +
  23915. +// Routine used to initialise the vchi on both local + remote connections
  23916. +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
  23917. +
  23918. +extern int32_t vchi_exit( void );
  23919. +
  23920. +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
  23921. + const uint32_t num_connections,
  23922. + VCHI_INSTANCE_T instance_handle );
  23923. +
  23924. +//When this is called, ensure that all services have no data pending.
  23925. +//Bulk transfers can remain 'queued'
  23926. +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
  23927. +
  23928. +// Global control over bulk CRC checking
  23929. +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
  23930. + VCHI_CRC_CONTROL_T control );
  23931. +
  23932. +// helper functions
  23933. +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
  23934. +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
  23935. +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
  23936. +
  23937. +
  23938. +/******************************************************************************
  23939. + Global service API
  23940. + *****************************************************************************/
  23941. +// Routine to create a named service
  23942. +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
  23943. + SERVICE_CREATION_T *setup,
  23944. + VCHI_SERVICE_HANDLE_T *handle );
  23945. +
  23946. +// Routine to destory a service
  23947. +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
  23948. +
  23949. +// Routine to open a named service
  23950. +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
  23951. + SERVICE_CREATION_T *setup,
  23952. + VCHI_SERVICE_HANDLE_T *handle);
  23953. +
  23954. +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
  23955. + short *peer_version );
  23956. +
  23957. +// Routine to close a named service
  23958. +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
  23959. +
  23960. +// Routine to increment ref count on a named service
  23961. +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
  23962. +
  23963. +// Routine to decrement ref count on a named service
  23964. +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
  23965. +
  23966. +// Routine to send a message accross a service
  23967. +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
  23968. + const void *data,
  23969. + uint32_t data_size,
  23970. + VCHI_FLAGS_T flags,
  23971. + void *msg_handle );
  23972. +
  23973. +// scatter-gather (vector) and send message
  23974. +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
  23975. + VCHI_MSG_VECTOR_EX_T *vector,
  23976. + uint32_t count,
  23977. + VCHI_FLAGS_T flags,
  23978. + void *msg_handle );
  23979. +
  23980. +// legacy scatter-gather (vector) and send message, only handles pointers
  23981. +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
  23982. + VCHI_MSG_VECTOR_T *vector,
  23983. + uint32_t count,
  23984. + VCHI_FLAGS_T flags,
  23985. + void *msg_handle );
  23986. +
  23987. +// Routine to receive a msg from a service
  23988. +// Dequeue is equivalent to hold, copy into client buffer, release
  23989. +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
  23990. + void *data,
  23991. + uint32_t max_data_size_to_read,
  23992. + uint32_t *actual_msg_size,
  23993. + VCHI_FLAGS_T flags );
  23994. +
  23995. +// Routine to look at a message in place.
  23996. +// The message is not dequeued, so a subsequent call to peek or dequeue
  23997. +// will return the same message.
  23998. +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
  23999. + void **data,
  24000. + uint32_t *msg_size,
  24001. + VCHI_FLAGS_T flags );
  24002. +
  24003. +// Routine to remove a message after it has been read in place with peek
  24004. +// The first message on the queue is dequeued.
  24005. +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
  24006. +
  24007. +// Routine to look at a message in place.
  24008. +// The message is dequeued, so the caller is left holding it; the descriptor is
  24009. +// filled in and must be released when the user has finished with the message.
  24010. +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
  24011. + void **data, // } may be NULL, as info can be
  24012. + uint32_t *msg_size, // } obtained from HELD_MSG_T
  24013. + VCHI_FLAGS_T flags,
  24014. + VCHI_HELD_MSG_T *message_descriptor );
  24015. +
  24016. +// Initialise an iterator to look through messages in place
  24017. +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
  24018. + VCHI_MSG_ITER_T *iter,
  24019. + VCHI_FLAGS_T flags );
  24020. +
  24021. +/******************************************************************************
  24022. + Global service support API - operations on held messages and message iterators
  24023. + *****************************************************************************/
  24024. +
  24025. +// Routine to get the address of a held message
  24026. +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
  24027. +
  24028. +// Routine to get the size of a held message
  24029. +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
  24030. +
  24031. +// Routine to get the transmit timestamp as written into the header by the peer
  24032. +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
  24033. +
  24034. +// Routine to get the reception timestamp, written as we parsed the header
  24035. +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
  24036. +
  24037. +// Routine to release a held message after it has been processed
  24038. +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
  24039. +
  24040. +// Indicates whether the iterator has a next message.
  24041. +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
  24042. +
  24043. +// Return the pointer and length for the next message and advance the iterator.
  24044. +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
  24045. + void **data,
  24046. + uint32_t *msg_size );
  24047. +
  24048. +// Remove the last message returned by vchi_msg_iter_next.
  24049. +// Can only be called once after each call to vchi_msg_iter_next.
  24050. +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
  24051. +
  24052. +// Hold the last message returned by vchi_msg_iter_next.
  24053. +// Can only be called once after each call to vchi_msg_iter_next.
  24054. +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
  24055. + VCHI_HELD_MSG_T *message );
  24056. +
  24057. +// Return information for the next message, and hold it, advancing the iterator.
  24058. +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
  24059. + void **data, // } may be NULL
  24060. + uint32_t *msg_size, // }
  24061. + VCHI_HELD_MSG_T *message );
  24062. +
  24063. +
  24064. +/******************************************************************************
  24065. + Global bulk API
  24066. + *****************************************************************************/
  24067. +
  24068. +// Routine to prepare interface for a transfer from the other side
  24069. +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
  24070. + void *data_dst,
  24071. + uint32_t data_size,
  24072. + VCHI_FLAGS_T flags,
  24073. + void *transfer_handle );
  24074. +
  24075. +
  24076. +// Prepare interface for a transfer from the other side into relocatable memory.
  24077. +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
  24078. + VCHI_MEM_HANDLE_T h_dst,
  24079. + uint32_t offset,
  24080. + uint32_t data_size,
  24081. + const VCHI_FLAGS_T flags,
  24082. + void * const bulk_handle );
  24083. +
  24084. +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
  24085. +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
  24086. + const void *data_src,
  24087. + uint32_t data_size,
  24088. + VCHI_FLAGS_T flags,
  24089. + void *transfer_handle );
  24090. +
  24091. +
  24092. +/******************************************************************************
  24093. + Configuration plumbing
  24094. + *****************************************************************************/
  24095. +
  24096. +// function prototypes for the different mid layers (the state info gives the different physical connections)
  24097. +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
  24098. +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
  24099. +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
  24100. +
  24101. +// declare all message drivers here
  24102. +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
  24103. +
  24104. +#ifdef __cplusplus
  24105. +}
  24106. +#endif
  24107. +
  24108. +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
  24109. + VCHI_MEM_HANDLE_T h_src,
  24110. + uint32_t offset,
  24111. + uint32_t data_size,
  24112. + VCHI_FLAGS_T flags,
  24113. + void *transfer_handle );
  24114. +#endif /* VCHI_H_ */
  24115. +
  24116. +/****************************** End of file **********************************/
  24117. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_mh.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
  24118. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 1970-01-01 01:00:00.000000000 +0100
  24119. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchi/vchi_mh.h 2014-04-13 17:32:57.000000000 +0200
  24120. @@ -0,0 +1,42 @@
  24121. +/**
  24122. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24123. + *
  24124. + * Redistribution and use in source and binary forms, with or without
  24125. + * modification, are permitted provided that the following conditions
  24126. + * are met:
  24127. + * 1. Redistributions of source code must retain the above copyright
  24128. + * notice, this list of conditions, and the following disclaimer,
  24129. + * without modification.
  24130. + * 2. Redistributions in binary form must reproduce the above copyright
  24131. + * notice, this list of conditions and the following disclaimer in the
  24132. + * documentation and/or other materials provided with the distribution.
  24133. + * 3. The names of the above-listed copyright holders may not be used
  24134. + * to endorse or promote products derived from this software without
  24135. + * specific prior written permission.
  24136. + *
  24137. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24138. + * GNU General Public License ("GPL") version 2, as published by the Free
  24139. + * Software Foundation.
  24140. + *
  24141. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24142. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24143. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24144. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24145. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24146. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24147. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24148. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24149. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24150. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24151. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24152. + */
  24153. +
  24154. +#ifndef VCHI_MH_H_
  24155. +#define VCHI_MH_H_
  24156. +
  24157. +#include <linux/types.h>
  24158. +
  24159. +typedef int32_t VCHI_MEM_HANDLE_T;
  24160. +#define VCHI_MEM_HANDLE_INVALID 0
  24161. +
  24162. +#endif
  24163. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
  24164. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 1970-01-01 01:00:00.000000000 +0100
  24165. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c 2014-09-14 19:03:25.000000000 +0200
  24166. @@ -0,0 +1,562 @@
  24167. +/**
  24168. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24169. + *
  24170. + * Redistribution and use in source and binary forms, with or without
  24171. + * modification, are permitted provided that the following conditions
  24172. + * are met:
  24173. + * 1. Redistributions of source code must retain the above copyright
  24174. + * notice, this list of conditions, and the following disclaimer,
  24175. + * without modification.
  24176. + * 2. Redistributions in binary form must reproduce the above copyright
  24177. + * notice, this list of conditions and the following disclaimer in the
  24178. + * documentation and/or other materials provided with the distribution.
  24179. + * 3. The names of the above-listed copyright holders may not be used
  24180. + * to endorse or promote products derived from this software without
  24181. + * specific prior written permission.
  24182. + *
  24183. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24184. + * GNU General Public License ("GPL") version 2, as published by the Free
  24185. + * Software Foundation.
  24186. + *
  24187. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24188. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24189. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24190. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24191. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24192. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24193. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24194. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24195. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24196. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24197. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24198. + */
  24199. +
  24200. +#include <linux/kernel.h>
  24201. +#include <linux/types.h>
  24202. +#include <linux/errno.h>
  24203. +#include <linux/interrupt.h>
  24204. +#include <linux/irq.h>
  24205. +#include <linux/pagemap.h>
  24206. +#include <linux/dma-mapping.h>
  24207. +#include <linux/version.h>
  24208. +#include <linux/io.h>
  24209. +#include <linux/uaccess.h>
  24210. +#include <asm/pgtable.h>
  24211. +
  24212. +#include <mach/irqs.h>
  24213. +
  24214. +#include <mach/platform.h>
  24215. +#include <mach/vcio.h>
  24216. +
  24217. +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
  24218. +
  24219. +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
  24220. +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
  24221. +
  24222. +#include "vchiq_arm.h"
  24223. +#include "vchiq_2835.h"
  24224. +#include "vchiq_connected.h"
  24225. +#include "vchiq_killable.h"
  24226. +
  24227. +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
  24228. +
  24229. +typedef struct vchiq_2835_state_struct {
  24230. + int inited;
  24231. + VCHIQ_ARM_STATE_T arm_state;
  24232. +} VCHIQ_2835_ARM_STATE_T;
  24233. +
  24234. +static char *g_slot_mem;
  24235. +static int g_slot_mem_size;
  24236. +dma_addr_t g_slot_phys;
  24237. +static FRAGMENTS_T *g_fragments_base;
  24238. +static FRAGMENTS_T *g_free_fragments;
  24239. +struct semaphore g_free_fragments_sema;
  24240. +
  24241. +extern int vchiq_arm_log_level;
  24242. +
  24243. +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
  24244. +
  24245. +static irqreturn_t
  24246. +vchiq_doorbell_irq(int irq, void *dev_id);
  24247. +
  24248. +static int
  24249. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  24250. + struct task_struct *task, PAGELIST_T ** ppagelist);
  24251. +
  24252. +static void
  24253. +free_pagelist(PAGELIST_T *pagelist, int actual);
  24254. +
  24255. +int __init
  24256. +vchiq_platform_init(VCHIQ_STATE_T *state)
  24257. +{
  24258. + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
  24259. + int frag_mem_size;
  24260. + int err;
  24261. + int i;
  24262. +
  24263. + /* Allocate space for the channels in coherent memory */
  24264. + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
  24265. + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
  24266. +
  24267. + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
  24268. + &g_slot_phys, GFP_ATOMIC);
  24269. +
  24270. + if (!g_slot_mem) {
  24271. + vchiq_log_error(vchiq_arm_log_level,
  24272. + "Unable to allocate channel memory");
  24273. + err = -ENOMEM;
  24274. + goto failed_alloc;
  24275. + }
  24276. +
  24277. + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
  24278. +
  24279. + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
  24280. + if (!vchiq_slot_zero) {
  24281. + err = -EINVAL;
  24282. + goto failed_init_slots;
  24283. + }
  24284. +
  24285. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
  24286. + (int)g_slot_phys + g_slot_mem_size;
  24287. + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
  24288. + MAX_FRAGMENTS;
  24289. +
  24290. + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
  24291. + g_slot_mem_size += frag_mem_size;
  24292. +
  24293. + g_free_fragments = g_fragments_base;
  24294. + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
  24295. + *(FRAGMENTS_T **)&g_fragments_base[i] =
  24296. + &g_fragments_base[i + 1];
  24297. + }
  24298. + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
  24299. + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
  24300. +
  24301. + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
  24302. + VCHIQ_SUCCESS) {
  24303. + err = -EINVAL;
  24304. + goto failed_vchiq_init;
  24305. + }
  24306. +
  24307. + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
  24308. + IRQF_IRQPOLL, "VCHIQ doorbell",
  24309. + state);
  24310. + if (err < 0) {
  24311. + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
  24312. + "irq=%d err=%d", __func__,
  24313. + VCHIQ_DOORBELL_IRQ, err);
  24314. + goto failed_request_irq;
  24315. + }
  24316. +
  24317. + /* Send the base address of the slots to VideoCore */
  24318. +
  24319. + dsb(); /* Ensure all writes have completed */
  24320. +
  24321. + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
  24322. +
  24323. + vchiq_log_info(vchiq_arm_log_level,
  24324. + "vchiq_init - done (slots %x, phys %x)",
  24325. + (unsigned int)vchiq_slot_zero, g_slot_phys);
  24326. +
  24327. + vchiq_call_connected_callbacks();
  24328. +
  24329. + return 0;
  24330. +
  24331. +failed_request_irq:
  24332. +failed_vchiq_init:
  24333. +failed_init_slots:
  24334. + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
  24335. +
  24336. +failed_alloc:
  24337. + return err;
  24338. +}
  24339. +
  24340. +void __exit
  24341. +vchiq_platform_exit(VCHIQ_STATE_T *state)
  24342. +{
  24343. + free_irq(VCHIQ_DOORBELL_IRQ, state);
  24344. + dma_free_coherent(NULL, g_slot_mem_size,
  24345. + g_slot_mem, g_slot_phys);
  24346. +}
  24347. +
  24348. +
  24349. +VCHIQ_STATUS_T
  24350. +vchiq_platform_init_state(VCHIQ_STATE_T *state)
  24351. +{
  24352. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  24353. + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
  24354. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
  24355. + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
  24356. + if(status != VCHIQ_SUCCESS)
  24357. + {
  24358. + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
  24359. + }
  24360. + return status;
  24361. +}
  24362. +
  24363. +VCHIQ_ARM_STATE_T*
  24364. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
  24365. +{
  24366. + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
  24367. + {
  24368. + BUG();
  24369. + }
  24370. + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
  24371. +}
  24372. +
  24373. +void
  24374. +remote_event_signal(REMOTE_EVENT_T *event)
  24375. +{
  24376. + wmb();
  24377. +
  24378. + event->fired = 1;
  24379. +
  24380. + dsb(); /* data barrier operation */
  24381. +
  24382. + if (event->armed) {
  24383. + /* trigger vc interrupt */
  24384. +
  24385. + writel(0, __io_address(ARM_0_BELL2));
  24386. + }
  24387. +}
  24388. +
  24389. +int
  24390. +vchiq_copy_from_user(void *dst, const void *src, int size)
  24391. +{
  24392. + if ((uint32_t)src < TASK_SIZE) {
  24393. + return copy_from_user(dst, src, size);
  24394. + } else {
  24395. + memcpy(dst, src, size);
  24396. + return 0;
  24397. + }
  24398. +}
  24399. +
  24400. +VCHIQ_STATUS_T
  24401. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
  24402. + void *offset, int size, int dir)
  24403. +{
  24404. + PAGELIST_T *pagelist;
  24405. + int ret;
  24406. +
  24407. + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
  24408. +
  24409. + ret = create_pagelist((char __user *)offset, size,
  24410. + (dir == VCHIQ_BULK_RECEIVE)
  24411. + ? PAGELIST_READ
  24412. + : PAGELIST_WRITE,
  24413. + current,
  24414. + &pagelist);
  24415. + if (ret != 0)
  24416. + return VCHIQ_ERROR;
  24417. +
  24418. + bulk->handle = memhandle;
  24419. + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
  24420. +
  24421. + /* Store the pagelist address in remote_data, which isn't used by the
  24422. + slave. */
  24423. + bulk->remote_data = pagelist;
  24424. +
  24425. + return VCHIQ_SUCCESS;
  24426. +}
  24427. +
  24428. +void
  24429. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
  24430. +{
  24431. + if (bulk && bulk->remote_data && bulk->actual)
  24432. + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
  24433. +}
  24434. +
  24435. +void
  24436. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
  24437. +{
  24438. + /*
  24439. + * This should only be called on the master (VideoCore) side, but
  24440. + * provide an implementation to avoid the need for ifdefery.
  24441. + */
  24442. + BUG();
  24443. +}
  24444. +
  24445. +void
  24446. +vchiq_dump_platform_state(void *dump_context)
  24447. +{
  24448. + char buf[80];
  24449. + int len;
  24450. + len = snprintf(buf, sizeof(buf),
  24451. + " Platform: 2835 (VC master)");
  24452. + vchiq_dump(dump_context, buf, len + 1);
  24453. +}
  24454. +
  24455. +VCHIQ_STATUS_T
  24456. +vchiq_platform_suspend(VCHIQ_STATE_T *state)
  24457. +{
  24458. + return VCHIQ_ERROR;
  24459. +}
  24460. +
  24461. +VCHIQ_STATUS_T
  24462. +vchiq_platform_resume(VCHIQ_STATE_T *state)
  24463. +{
  24464. + return VCHIQ_SUCCESS;
  24465. +}
  24466. +
  24467. +void
  24468. +vchiq_platform_paused(VCHIQ_STATE_T *state)
  24469. +{
  24470. +}
  24471. +
  24472. +void
  24473. +vchiq_platform_resumed(VCHIQ_STATE_T *state)
  24474. +{
  24475. +}
  24476. +
  24477. +int
  24478. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
  24479. +{
  24480. + return 1; // autosuspend not supported - videocore always wanted
  24481. +}
  24482. +
  24483. +int
  24484. +vchiq_platform_use_suspend_timer(void)
  24485. +{
  24486. + return 0;
  24487. +}
  24488. +void
  24489. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
  24490. +{
  24491. + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
  24492. +}
  24493. +void
  24494. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
  24495. +{
  24496. + (void)state;
  24497. +}
  24498. +/*
  24499. + * Local functions
  24500. + */
  24501. +
  24502. +static irqreturn_t
  24503. +vchiq_doorbell_irq(int irq, void *dev_id)
  24504. +{
  24505. + VCHIQ_STATE_T *state = dev_id;
  24506. + irqreturn_t ret = IRQ_NONE;
  24507. + unsigned int status;
  24508. +
  24509. + /* Read (and clear) the doorbell */
  24510. + status = readl(__io_address(ARM_0_BELL0));
  24511. +
  24512. + if (status & 0x4) { /* Was the doorbell rung? */
  24513. + remote_event_pollall(state);
  24514. + ret = IRQ_HANDLED;
  24515. + }
  24516. +
  24517. + return ret;
  24518. +}
  24519. +
  24520. +/* There is a potential problem with partial cache lines (pages?)
  24521. +** at the ends of the block when reading. If the CPU accessed anything in
  24522. +** the same line (page?) then it may have pulled old data into the cache,
  24523. +** obscuring the new data underneath. We can solve this by transferring the
  24524. +** partial cache lines separately, and allowing the ARM to copy into the
  24525. +** cached area.
  24526. +
  24527. +** N.B. This implementation plays slightly fast and loose with the Linux
  24528. +** driver programming rules, e.g. its use of __virt_to_bus instead of
  24529. +** dma_map_single, but it isn't a multi-platform driver and it benefits
  24530. +** from increased speed as a result.
  24531. +*/
  24532. +
  24533. +static int
  24534. +create_pagelist(char __user *buf, size_t count, unsigned short type,
  24535. + struct task_struct *task, PAGELIST_T ** ppagelist)
  24536. +{
  24537. + PAGELIST_T *pagelist;
  24538. + struct page **pages;
  24539. + struct page *page;
  24540. + unsigned long *addrs;
  24541. + unsigned int num_pages, offset, i;
  24542. + char *addr, *base_addr, *next_addr;
  24543. + int run, addridx, actual_pages;
  24544. + unsigned long *need_release;
  24545. +
  24546. + offset = (unsigned int)buf & (PAGE_SIZE - 1);
  24547. + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
  24548. +
  24549. + *ppagelist = NULL;
  24550. +
  24551. + /* Allocate enough storage to hold the page pointers and the page
  24552. + ** list
  24553. + */
  24554. + pagelist = kmalloc(sizeof(PAGELIST_T) +
  24555. + (num_pages * sizeof(unsigned long)) +
  24556. + sizeof(unsigned long) +
  24557. + (num_pages * sizeof(pages[0])),
  24558. + GFP_KERNEL);
  24559. +
  24560. + vchiq_log_trace(vchiq_arm_log_level,
  24561. + "create_pagelist - %x", (unsigned int)pagelist);
  24562. + if (!pagelist)
  24563. + return -ENOMEM;
  24564. +
  24565. + addrs = pagelist->addrs;
  24566. + need_release = (unsigned long *)(addrs + num_pages);
  24567. + pages = (struct page **)(addrs + num_pages + 1);
  24568. +
  24569. + if (is_vmalloc_addr(buf)) {
  24570. + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
  24571. + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
  24572. + }
  24573. + *need_release = 0; /* do not try and release vmalloc pages */
  24574. + } else {
  24575. + down_read(&task->mm->mmap_sem);
  24576. + actual_pages = get_user_pages(task, task->mm,
  24577. + (unsigned long)buf & ~(PAGE_SIZE - 1),
  24578. + num_pages,
  24579. + (type == PAGELIST_READ) /*Write */ ,
  24580. + 0 /*Force */ ,
  24581. + pages,
  24582. + NULL /*vmas */);
  24583. + up_read(&task->mm->mmap_sem);
  24584. +
  24585. + if (actual_pages != num_pages) {
  24586. + vchiq_log_info(vchiq_arm_log_level,
  24587. + "create_pagelist - only %d/%d pages locked",
  24588. + actual_pages,
  24589. + num_pages);
  24590. +
  24591. + /* This is probably due to the process being killed */
  24592. + while (actual_pages > 0)
  24593. + {
  24594. + actual_pages--;
  24595. + page_cache_release(pages[actual_pages]);
  24596. + }
  24597. + kfree(pagelist);
  24598. + if (actual_pages == 0)
  24599. + actual_pages = -ENOMEM;
  24600. + return actual_pages;
  24601. + }
  24602. + *need_release = 1; /* release user pages */
  24603. + }
  24604. +
  24605. + pagelist->length = count;
  24606. + pagelist->type = type;
  24607. + pagelist->offset = offset;
  24608. +
  24609. + /* Group the pages into runs of contiguous pages */
  24610. +
  24611. + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
  24612. + next_addr = base_addr + PAGE_SIZE;
  24613. + addridx = 0;
  24614. + run = 0;
  24615. +
  24616. + for (i = 1; i < num_pages; i++) {
  24617. + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
  24618. + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
  24619. + next_addr += PAGE_SIZE;
  24620. + run++;
  24621. + } else {
  24622. + addrs[addridx] = (unsigned long)base_addr + run;
  24623. + addridx++;
  24624. + base_addr = addr;
  24625. + next_addr = addr + PAGE_SIZE;
  24626. + run = 0;
  24627. + }
  24628. + }
  24629. +
  24630. + addrs[addridx] = (unsigned long)base_addr + run;
  24631. + addridx++;
  24632. +
  24633. + /* Partial cache lines (fragments) require special measures */
  24634. + if ((type == PAGELIST_READ) &&
  24635. + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
  24636. + ((pagelist->offset + pagelist->length) &
  24637. + (CACHE_LINE_SIZE - 1)))) {
  24638. + FRAGMENTS_T *fragments;
  24639. +
  24640. + if (down_interruptible(&g_free_fragments_sema) != 0) {
  24641. + kfree(pagelist);
  24642. + return -EINTR;
  24643. + }
  24644. +
  24645. + WARN_ON(g_free_fragments == NULL);
  24646. +
  24647. + down(&g_free_fragments_mutex);
  24648. + fragments = (FRAGMENTS_T *) g_free_fragments;
  24649. + WARN_ON(fragments == NULL);
  24650. + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
  24651. + up(&g_free_fragments_mutex);
  24652. + pagelist->type =
  24653. + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
  24654. + g_fragments_base);
  24655. + }
  24656. +
  24657. + for (page = virt_to_page(pagelist);
  24658. + page <= virt_to_page(addrs + num_pages - 1); page++) {
  24659. + flush_dcache_page(page);
  24660. + }
  24661. +
  24662. + *ppagelist = pagelist;
  24663. +
  24664. + return 0;
  24665. +}
  24666. +
  24667. +static void
  24668. +free_pagelist(PAGELIST_T *pagelist, int actual)
  24669. +{
  24670. + unsigned long *need_release;
  24671. + struct page **pages;
  24672. + unsigned int num_pages, i;
  24673. +
  24674. + vchiq_log_trace(vchiq_arm_log_level,
  24675. + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
  24676. +
  24677. + num_pages =
  24678. + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
  24679. + PAGE_SIZE;
  24680. +
  24681. + need_release = (unsigned long *)(pagelist->addrs + num_pages);
  24682. + pages = (struct page **)(pagelist->addrs + num_pages + 1);
  24683. +
  24684. + /* Deal with any partial cache lines (fragments) */
  24685. + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
  24686. + FRAGMENTS_T *fragments = g_fragments_base +
  24687. + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
  24688. + int head_bytes, tail_bytes;
  24689. + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
  24690. + (CACHE_LINE_SIZE - 1);
  24691. + tail_bytes = (pagelist->offset + actual) &
  24692. + (CACHE_LINE_SIZE - 1);
  24693. +
  24694. + if ((actual >= 0) && (head_bytes != 0)) {
  24695. + if (head_bytes > actual)
  24696. + head_bytes = actual;
  24697. +
  24698. + memcpy((char *)page_address(pages[0]) +
  24699. + pagelist->offset,
  24700. + fragments->headbuf,
  24701. + head_bytes);
  24702. + }
  24703. + if ((actual >= 0) && (head_bytes < actual) &&
  24704. + (tail_bytes != 0)) {
  24705. + memcpy((char *)page_address(pages[num_pages - 1]) +
  24706. + ((pagelist->offset + actual) &
  24707. + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
  24708. + fragments->tailbuf, tail_bytes);
  24709. + }
  24710. +
  24711. + down(&g_free_fragments_mutex);
  24712. + *(FRAGMENTS_T **) fragments = g_free_fragments;
  24713. + g_free_fragments = fragments;
  24714. + up(&g_free_fragments_mutex);
  24715. + up(&g_free_fragments_sema);
  24716. + }
  24717. +
  24718. + if (*need_release) {
  24719. + for (i = 0; i < num_pages; i++) {
  24720. + if (pagelist->type != PAGELIST_WRITE)
  24721. + set_page_dirty(pages[i]);
  24722. +
  24723. + page_cache_release(pages[i]);
  24724. + }
  24725. + }
  24726. +
  24727. + kfree(pagelist);
  24728. +}
  24729. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
  24730. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 1970-01-01 01:00:00.000000000 +0100
  24731. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h 2014-04-13 17:32:57.000000000 +0200
  24732. @@ -0,0 +1,42 @@
  24733. +/**
  24734. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24735. + *
  24736. + * Redistribution and use in source and binary forms, with or without
  24737. + * modification, are permitted provided that the following conditions
  24738. + * are met:
  24739. + * 1. Redistributions of source code must retain the above copyright
  24740. + * notice, this list of conditions, and the following disclaimer,
  24741. + * without modification.
  24742. + * 2. Redistributions in binary form must reproduce the above copyright
  24743. + * notice, this list of conditions and the following disclaimer in the
  24744. + * documentation and/or other materials provided with the distribution.
  24745. + * 3. The names of the above-listed copyright holders may not be used
  24746. + * to endorse or promote products derived from this software without
  24747. + * specific prior written permission.
  24748. + *
  24749. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24750. + * GNU General Public License ("GPL") version 2, as published by the Free
  24751. + * Software Foundation.
  24752. + *
  24753. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24754. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24755. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24756. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24757. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24758. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24759. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24760. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24761. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24762. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24763. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24764. + */
  24765. +
  24766. +#ifndef VCHIQ_2835_H
  24767. +#define VCHIQ_2835_H
  24768. +
  24769. +#include "vchiq_pagelist.h"
  24770. +
  24771. +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
  24772. +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
  24773. +
  24774. +#endif /* VCHIQ_2835_H */
  24775. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
  24776. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 1970-01-01 01:00:00.000000000 +0100
  24777. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c 2014-09-14 19:03:25.000000000 +0200
  24778. @@ -0,0 +1,2908 @@
  24779. +/**
  24780. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  24781. + *
  24782. + * Redistribution and use in source and binary forms, with or without
  24783. + * modification, are permitted provided that the following conditions
  24784. + * are met:
  24785. + * 1. Redistributions of source code must retain the above copyright
  24786. + * notice, this list of conditions, and the following disclaimer,
  24787. + * without modification.
  24788. + * 2. Redistributions in binary form must reproduce the above copyright
  24789. + * notice, this list of conditions and the following disclaimer in the
  24790. + * documentation and/or other materials provided with the distribution.
  24791. + * 3. The names of the above-listed copyright holders may not be used
  24792. + * to endorse or promote products derived from this software without
  24793. + * specific prior written permission.
  24794. + *
  24795. + * ALTERNATIVELY, this software may be distributed under the terms of the
  24796. + * GNU General Public License ("GPL") version 2, as published by the Free
  24797. + * Software Foundation.
  24798. + *
  24799. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24800. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24801. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24802. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24803. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24804. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24805. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24806. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24807. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24808. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24809. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24810. + */
  24811. +
  24812. +#include <linux/kernel.h>
  24813. +#include <linux/module.h>
  24814. +#include <linux/types.h>
  24815. +#include <linux/errno.h>
  24816. +#include <linux/cdev.h>
  24817. +#include <linux/fs.h>
  24818. +#include <linux/device.h>
  24819. +#include <linux/mm.h>
  24820. +#include <linux/highmem.h>
  24821. +#include <linux/pagemap.h>
  24822. +#include <linux/bug.h>
  24823. +#include <linux/semaphore.h>
  24824. +#include <linux/list.h>
  24825. +#include <linux/proc_fs.h>
  24826. +
  24827. +#include "vchiq_core.h"
  24828. +#include "vchiq_ioctl.h"
  24829. +#include "vchiq_arm.h"
  24830. +#include "vchiq_killable.h"
  24831. +
  24832. +#define DEVICE_NAME "vchiq"
  24833. +
  24834. +/* Override the default prefix, which would be vchiq_arm (from the filename) */
  24835. +#undef MODULE_PARAM_PREFIX
  24836. +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
  24837. +
  24838. +#define VCHIQ_MINOR 0
  24839. +
  24840. +/* Some per-instance constants */
  24841. +#define MAX_COMPLETIONS 16
  24842. +#define MAX_SERVICES 64
  24843. +#define MAX_ELEMENTS 8
  24844. +#define MSG_QUEUE_SIZE 64
  24845. +
  24846. +#define KEEPALIVE_VER 1
  24847. +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
  24848. +
  24849. +/* Run time control of log level, based on KERN_XXX level. */
  24850. +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
  24851. +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
  24852. +
  24853. +#define SUSPEND_TIMER_TIMEOUT_MS 100
  24854. +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
  24855. +
  24856. +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
  24857. +static const char *const suspend_state_names[] = {
  24858. + "VC_SUSPEND_FORCE_CANCELED",
  24859. + "VC_SUSPEND_REJECTED",
  24860. + "VC_SUSPEND_FAILED",
  24861. + "VC_SUSPEND_IDLE",
  24862. + "VC_SUSPEND_REQUESTED",
  24863. + "VC_SUSPEND_IN_PROGRESS",
  24864. + "VC_SUSPEND_SUSPENDED"
  24865. +};
  24866. +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
  24867. +static const char *const resume_state_names[] = {
  24868. + "VC_RESUME_FAILED",
  24869. + "VC_RESUME_IDLE",
  24870. + "VC_RESUME_REQUESTED",
  24871. + "VC_RESUME_IN_PROGRESS",
  24872. + "VC_RESUME_RESUMED"
  24873. +};
  24874. +/* The number of times we allow force suspend to timeout before actually
  24875. +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
  24876. +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
  24877. +*/
  24878. +#define FORCE_SUSPEND_FAIL_MAX 8
  24879. +
  24880. +/* The time in ms allowed for videocore to go idle when force suspend has been
  24881. + * requested */
  24882. +#define FORCE_SUSPEND_TIMEOUT_MS 200
  24883. +
  24884. +
  24885. +static void suspend_timer_callback(unsigned long context);
  24886. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance);
  24887. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance);
  24888. +
  24889. +
  24890. +typedef struct user_service_struct {
  24891. + VCHIQ_SERVICE_T *service;
  24892. + void *userdata;
  24893. + VCHIQ_INSTANCE_T instance;
  24894. + char is_vchi;
  24895. + char dequeue_pending;
  24896. + char close_pending;
  24897. + int message_available_pos;
  24898. + int msg_insert;
  24899. + int msg_remove;
  24900. + struct semaphore insert_event;
  24901. + struct semaphore remove_event;
  24902. + struct semaphore close_event;
  24903. + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
  24904. +} USER_SERVICE_T;
  24905. +
  24906. +struct bulk_waiter_node {
  24907. + struct bulk_waiter bulk_waiter;
  24908. + int pid;
  24909. + struct list_head list;
  24910. +};
  24911. +
  24912. +struct vchiq_instance_struct {
  24913. + VCHIQ_STATE_T *state;
  24914. + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
  24915. + int completion_insert;
  24916. + int completion_remove;
  24917. + struct semaphore insert_event;
  24918. + struct semaphore remove_event;
  24919. + struct mutex completion_mutex;
  24920. +
  24921. + int connected;
  24922. + int closing;
  24923. + int pid;
  24924. + int mark;
  24925. + int use_close_delivered;
  24926. +
  24927. + struct list_head bulk_waiter_list;
  24928. + struct mutex bulk_waiter_list_mutex;
  24929. +
  24930. + struct proc_dir_entry *proc_entry;
  24931. +};
  24932. +
  24933. +typedef struct dump_context_struct {
  24934. + char __user *buf;
  24935. + size_t actual;
  24936. + size_t space;
  24937. + loff_t offset;
  24938. +} DUMP_CONTEXT_T;
  24939. +
  24940. +static struct cdev vchiq_cdev;
  24941. +static dev_t vchiq_devid;
  24942. +static VCHIQ_STATE_T g_state;
  24943. +static struct class *vchiq_class;
  24944. +static struct device *vchiq_dev;
  24945. +static DEFINE_SPINLOCK(msg_queue_spinlock);
  24946. +
  24947. +static const char *const ioctl_names[] = {
  24948. + "CONNECT",
  24949. + "SHUTDOWN",
  24950. + "CREATE_SERVICE",
  24951. + "REMOVE_SERVICE",
  24952. + "QUEUE_MESSAGE",
  24953. + "QUEUE_BULK_TRANSMIT",
  24954. + "QUEUE_BULK_RECEIVE",
  24955. + "AWAIT_COMPLETION",
  24956. + "DEQUEUE_MESSAGE",
  24957. + "GET_CLIENT_ID",
  24958. + "GET_CONFIG",
  24959. + "CLOSE_SERVICE",
  24960. + "USE_SERVICE",
  24961. + "RELEASE_SERVICE",
  24962. + "SET_SERVICE_OPTION",
  24963. + "DUMP_PHYS_MEM",
  24964. + "LIB_VERSION",
  24965. + "CLOSE_DELIVERED"
  24966. +};
  24967. +
  24968. +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
  24969. + (VCHIQ_IOC_MAX + 1));
  24970. +
  24971. +static void
  24972. +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
  24973. +
  24974. +/****************************************************************************
  24975. +*
  24976. +* add_completion
  24977. +*
  24978. +***************************************************************************/
  24979. +
  24980. +static VCHIQ_STATUS_T
  24981. +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
  24982. + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
  24983. + void *bulk_userdata)
  24984. +{
  24985. + VCHIQ_COMPLETION_DATA_T *completion;
  24986. + DEBUG_INITIALISE(g_state.local)
  24987. +
  24988. + while (instance->completion_insert ==
  24989. + (instance->completion_remove + MAX_COMPLETIONS)) {
  24990. + /* Out of space - wait for the client */
  24991. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  24992. + vchiq_log_trace(vchiq_arm_log_level,
  24993. + "add_completion - completion queue full");
  24994. + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
  24995. + if (down_interruptible(&instance->remove_event) != 0) {
  24996. + vchiq_log_info(vchiq_arm_log_level,
  24997. + "service_callback interrupted");
  24998. + return VCHIQ_RETRY;
  24999. + } else if (instance->closing) {
  25000. + vchiq_log_info(vchiq_arm_log_level,
  25001. + "service_callback closing");
  25002. + return VCHIQ_ERROR;
  25003. + }
  25004. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25005. + }
  25006. +
  25007. + completion =
  25008. + &instance->completions[instance->completion_insert &
  25009. + (MAX_COMPLETIONS - 1)];
  25010. +
  25011. + completion->header = header;
  25012. + completion->reason = reason;
  25013. + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
  25014. + completion->service_userdata = user_service->service;
  25015. + completion->bulk_userdata = bulk_userdata;
  25016. +
  25017. + if (reason == VCHIQ_SERVICE_CLOSED) {
  25018. + /* Take an extra reference, to be held until
  25019. + this CLOSED notification is delivered. */
  25020. + lock_service(user_service->service);
  25021. + if (instance->use_close_delivered)
  25022. + user_service->close_pending = 1;
  25023. + }
  25024. +
  25025. + /* A write barrier is needed here to ensure that the entire completion
  25026. + record is written out before the insert point. */
  25027. + wmb();
  25028. +
  25029. + if (reason == VCHIQ_MESSAGE_AVAILABLE)
  25030. + user_service->message_available_pos =
  25031. + instance->completion_insert;
  25032. + instance->completion_insert++;
  25033. +
  25034. + up(&instance->insert_event);
  25035. +
  25036. + return VCHIQ_SUCCESS;
  25037. +}
  25038. +
  25039. +/****************************************************************************
  25040. +*
  25041. +* service_callback
  25042. +*
  25043. +***************************************************************************/
  25044. +
  25045. +static VCHIQ_STATUS_T
  25046. +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
  25047. + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
  25048. +{
  25049. + /* How do we ensure the callback goes to the right client?
  25050. + ** The service_user data points to a USER_SERVICE_T record containing
  25051. + ** the original callback and the user state structure, which contains a
  25052. + ** circular buffer for completion records.
  25053. + */
  25054. + USER_SERVICE_T *user_service;
  25055. + VCHIQ_SERVICE_T *service;
  25056. + VCHIQ_INSTANCE_T instance;
  25057. + DEBUG_INITIALISE(g_state.local)
  25058. +
  25059. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25060. +
  25061. + service = handle_to_service(handle);
  25062. + BUG_ON(!service);
  25063. + user_service = (USER_SERVICE_T *)service->base.userdata;
  25064. + instance = user_service->instance;
  25065. +
  25066. + if (!instance || instance->closing)
  25067. + return VCHIQ_SUCCESS;
  25068. +
  25069. + vchiq_log_trace(vchiq_arm_log_level,
  25070. + "service_callback - service %lx(%d,%p), reason %d, header %lx, "
  25071. + "instance %lx, bulk_userdata %lx",
  25072. + (unsigned long)user_service,
  25073. + service->localport, user_service->userdata,
  25074. + reason, (unsigned long)header,
  25075. + (unsigned long)instance, (unsigned long)bulk_userdata);
  25076. +
  25077. + if (header && user_service->is_vchi) {
  25078. + spin_lock(&msg_queue_spinlock);
  25079. + while (user_service->msg_insert ==
  25080. + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
  25081. + spin_unlock(&msg_queue_spinlock);
  25082. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25083. + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
  25084. + vchiq_log_trace(vchiq_arm_log_level,
  25085. + "service_callback - msg queue full");
  25086. + /* If there is no MESSAGE_AVAILABLE in the completion
  25087. + ** queue, add one
  25088. + */
  25089. + if ((user_service->message_available_pos -
  25090. + instance->completion_remove) < 0) {
  25091. + VCHIQ_STATUS_T status;
  25092. + vchiq_log_info(vchiq_arm_log_level,
  25093. + "Inserting extra MESSAGE_AVAILABLE");
  25094. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25095. + status = add_completion(instance, reason,
  25096. + NULL, user_service, bulk_userdata);
  25097. + if (status != VCHIQ_SUCCESS) {
  25098. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25099. + return status;
  25100. + }
  25101. + }
  25102. +
  25103. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25104. + if (down_interruptible(&user_service->remove_event)
  25105. + != 0) {
  25106. + vchiq_log_info(vchiq_arm_log_level,
  25107. + "service_callback interrupted");
  25108. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25109. + return VCHIQ_RETRY;
  25110. + } else if (instance->closing) {
  25111. + vchiq_log_info(vchiq_arm_log_level,
  25112. + "service_callback closing");
  25113. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25114. + return VCHIQ_ERROR;
  25115. + }
  25116. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25117. + spin_lock(&msg_queue_spinlock);
  25118. + }
  25119. +
  25120. + user_service->msg_queue[user_service->msg_insert &
  25121. + (MSG_QUEUE_SIZE - 1)] = header;
  25122. + user_service->msg_insert++;
  25123. + spin_unlock(&msg_queue_spinlock);
  25124. +
  25125. + up(&user_service->insert_event);
  25126. +
  25127. + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
  25128. + ** there is a MESSAGE_AVAILABLE in the completion queue then
  25129. + ** bypass the completion queue.
  25130. + */
  25131. + if (((user_service->message_available_pos -
  25132. + instance->completion_remove) >= 0) ||
  25133. + user_service->dequeue_pending) {
  25134. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25135. + user_service->dequeue_pending = 0;
  25136. + return VCHIQ_SUCCESS;
  25137. + }
  25138. +
  25139. + header = NULL;
  25140. + }
  25141. + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
  25142. +
  25143. + return add_completion(instance, reason, header, user_service,
  25144. + bulk_userdata);
  25145. +}
  25146. +
  25147. +/****************************************************************************
  25148. +*
  25149. +* user_service_free
  25150. +*
  25151. +***************************************************************************/
  25152. +static void
  25153. +user_service_free(void *userdata)
  25154. +{
  25155. + kfree(userdata);
  25156. +}
  25157. +
  25158. +/****************************************************************************
  25159. +*
  25160. +* close_delivered
  25161. +*
  25162. +***************************************************************************/
  25163. +static void close_delivered(USER_SERVICE_T *user_service)
  25164. +{
  25165. + vchiq_log_info(vchiq_arm_log_level,
  25166. + "close_delivered(handle=%x)",
  25167. + user_service->service->handle);
  25168. +
  25169. + WARN_ON(user_service->close_pending == 0);
  25170. +
  25171. + /* Allow the underlying service to be culled */
  25172. + unlock_service(user_service->service);
  25173. +
  25174. + /* Wake the user-thread blocked in close_ or remove_service */
  25175. + up(&user_service->close_event);
  25176. +
  25177. + user_service->close_pending = 0;
  25178. +}
  25179. +
  25180. +/****************************************************************************
  25181. +*
  25182. +* vchiq_ioctl
  25183. +*
  25184. +***************************************************************************/
  25185. +static long
  25186. +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  25187. +{
  25188. + VCHIQ_INSTANCE_T instance = file->private_data;
  25189. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  25190. + VCHIQ_SERVICE_T *service = NULL;
  25191. + long ret = 0;
  25192. + int i, rc;
  25193. + DEBUG_INITIALISE(g_state.local)
  25194. +
  25195. + vchiq_log_trace(vchiq_arm_log_level,
  25196. + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
  25197. + (unsigned int)instance,
  25198. + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
  25199. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
  25200. + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
  25201. +
  25202. + switch (cmd) {
  25203. + case VCHIQ_IOC_SHUTDOWN:
  25204. + if (!instance->connected)
  25205. + break;
  25206. +
  25207. + /* Remove all services */
  25208. + i = 0;
  25209. + while ((service = next_service_by_instance(instance->state,
  25210. + instance, &i)) != NULL) {
  25211. + status = vchiq_remove_service(service->handle);
  25212. + unlock_service(service);
  25213. + if (status != VCHIQ_SUCCESS)
  25214. + break;
  25215. + }
  25216. + service = NULL;
  25217. +
  25218. + if (status == VCHIQ_SUCCESS) {
  25219. + /* Wake the completion thread and ask it to exit */
  25220. + instance->closing = 1;
  25221. + up(&instance->insert_event);
  25222. + }
  25223. +
  25224. + break;
  25225. +
  25226. + case VCHIQ_IOC_CONNECT:
  25227. + if (instance->connected) {
  25228. + ret = -EINVAL;
  25229. + break;
  25230. + }
  25231. + rc = mutex_lock_interruptible(&instance->state->mutex);
  25232. + if (rc != 0) {
  25233. + vchiq_log_error(vchiq_arm_log_level,
  25234. + "vchiq: connect: could not lock mutex for "
  25235. + "state %d: %d",
  25236. + instance->state->id, rc);
  25237. + ret = -EINTR;
  25238. + break;
  25239. + }
  25240. + status = vchiq_connect_internal(instance->state, instance);
  25241. + mutex_unlock(&instance->state->mutex);
  25242. +
  25243. + if (status == VCHIQ_SUCCESS)
  25244. + instance->connected = 1;
  25245. + else
  25246. + vchiq_log_error(vchiq_arm_log_level,
  25247. + "vchiq: could not connect: %d", status);
  25248. + break;
  25249. +
  25250. + case VCHIQ_IOC_CREATE_SERVICE: {
  25251. + VCHIQ_CREATE_SERVICE_T args;
  25252. + USER_SERVICE_T *user_service = NULL;
  25253. + void *userdata;
  25254. + int srvstate;
  25255. +
  25256. + if (copy_from_user
  25257. + (&args, (const void __user *)arg,
  25258. + sizeof(args)) != 0) {
  25259. + ret = -EFAULT;
  25260. + break;
  25261. + }
  25262. +
  25263. + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
  25264. + if (!user_service) {
  25265. + ret = -ENOMEM;
  25266. + break;
  25267. + }
  25268. +
  25269. + if (args.is_open) {
  25270. + if (!instance->connected) {
  25271. + ret = -ENOTCONN;
  25272. + kfree(user_service);
  25273. + break;
  25274. + }
  25275. + srvstate = VCHIQ_SRVSTATE_OPENING;
  25276. + } else {
  25277. + srvstate =
  25278. + instance->connected ?
  25279. + VCHIQ_SRVSTATE_LISTENING :
  25280. + VCHIQ_SRVSTATE_HIDDEN;
  25281. + }
  25282. +
  25283. + userdata = args.params.userdata;
  25284. + args.params.callback = service_callback;
  25285. + args.params.userdata = user_service;
  25286. + service = vchiq_add_service_internal(
  25287. + instance->state,
  25288. + &args.params, srvstate,
  25289. + instance, user_service_free);
  25290. +
  25291. + if (service != NULL) {
  25292. + user_service->service = service;
  25293. + user_service->userdata = userdata;
  25294. + user_service->instance = instance;
  25295. + user_service->is_vchi = (args.is_vchi != 0);
  25296. + user_service->dequeue_pending = 0;
  25297. + user_service->close_pending = 0;
  25298. + user_service->message_available_pos =
  25299. + instance->completion_remove - 1;
  25300. + user_service->msg_insert = 0;
  25301. + user_service->msg_remove = 0;
  25302. + sema_init(&user_service->insert_event, 0);
  25303. + sema_init(&user_service->remove_event, 0);
  25304. + sema_init(&user_service->close_event, 0);
  25305. +
  25306. + if (args.is_open) {
  25307. + status = vchiq_open_service_internal
  25308. + (service, instance->pid);
  25309. + if (status != VCHIQ_SUCCESS) {
  25310. + vchiq_remove_service(service->handle);
  25311. + service = NULL;
  25312. + ret = (status == VCHIQ_RETRY) ?
  25313. + -EINTR : -EIO;
  25314. + break;
  25315. + }
  25316. + }
  25317. +
  25318. + if (copy_to_user((void __user *)
  25319. + &(((VCHIQ_CREATE_SERVICE_T __user *)
  25320. + arg)->handle),
  25321. + (const void *)&service->handle,
  25322. + sizeof(service->handle)) != 0) {
  25323. + ret = -EFAULT;
  25324. + vchiq_remove_service(service->handle);
  25325. + }
  25326. +
  25327. + service = NULL;
  25328. + } else {
  25329. + ret = -EEXIST;
  25330. + kfree(user_service);
  25331. + }
  25332. + } break;
  25333. +
  25334. + case VCHIQ_IOC_CLOSE_SERVICE: {
  25335. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  25336. +
  25337. + service = find_service_for_instance(instance, handle);
  25338. + if (service != NULL) {
  25339. + USER_SERVICE_T *user_service =
  25340. + (USER_SERVICE_T *)service->base.userdata;
  25341. + /* close_pending is false on first entry, and when the
  25342. + wait in vchiq_close_service has been interrupted. */
  25343. + if (!user_service->close_pending) {
  25344. + status = vchiq_close_service(service->handle);
  25345. + if (status != VCHIQ_SUCCESS)
  25346. + break;
  25347. + }
  25348. +
  25349. + /* close_pending is true once the underlying service
  25350. + has been closed until the client library calls the
  25351. + CLOSE_DELIVERED ioctl, signalling close_event. */
  25352. + if (user_service->close_pending &&
  25353. + down_interruptible(&user_service->close_event))
  25354. + status = VCHIQ_RETRY;
  25355. + }
  25356. + else
  25357. + ret = -EINVAL;
  25358. + } break;
  25359. +
  25360. + case VCHIQ_IOC_REMOVE_SERVICE: {
  25361. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  25362. +
  25363. + service = find_service_for_instance(instance, handle);
  25364. + if (service != NULL) {
  25365. + USER_SERVICE_T *user_service =
  25366. + (USER_SERVICE_T *)service->base.userdata;
  25367. + /* close_pending is false on first entry, and when the
  25368. + wait in vchiq_close_service has been interrupted. */
  25369. + if (!user_service->close_pending) {
  25370. + status = vchiq_remove_service(service->handle);
  25371. + if (status != VCHIQ_SUCCESS)
  25372. + break;
  25373. + }
  25374. +
  25375. + /* close_pending is true once the underlying service
  25376. + has been closed until the client library calls the
  25377. + CLOSE_DELIVERED ioctl, signalling close_event. */
  25378. + if (user_service->close_pending &&
  25379. + down_interruptible(&user_service->close_event))
  25380. + status = VCHIQ_RETRY;
  25381. + }
  25382. + else
  25383. + ret = -EINVAL;
  25384. + } break;
  25385. +
  25386. + case VCHIQ_IOC_USE_SERVICE:
  25387. + case VCHIQ_IOC_RELEASE_SERVICE: {
  25388. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  25389. +
  25390. + service = find_service_for_instance(instance, handle);
  25391. + if (service != NULL) {
  25392. + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
  25393. + vchiq_use_service_internal(service) :
  25394. + vchiq_release_service_internal(service);
  25395. + if (status != VCHIQ_SUCCESS) {
  25396. + vchiq_log_error(vchiq_susp_log_level,
  25397. + "%s: cmd %s returned error %d for "
  25398. + "service %c%c%c%c:%03d",
  25399. + __func__,
  25400. + (cmd == VCHIQ_IOC_USE_SERVICE) ?
  25401. + "VCHIQ_IOC_USE_SERVICE" :
  25402. + "VCHIQ_IOC_RELEASE_SERVICE",
  25403. + status,
  25404. + VCHIQ_FOURCC_AS_4CHARS(
  25405. + service->base.fourcc),
  25406. + service->client_id);
  25407. + ret = -EINVAL;
  25408. + }
  25409. + } else
  25410. + ret = -EINVAL;
  25411. + } break;
  25412. +
  25413. + case VCHIQ_IOC_QUEUE_MESSAGE: {
  25414. + VCHIQ_QUEUE_MESSAGE_T args;
  25415. + if (copy_from_user
  25416. + (&args, (const void __user *)arg,
  25417. + sizeof(args)) != 0) {
  25418. + ret = -EFAULT;
  25419. + break;
  25420. + }
  25421. +
  25422. + service = find_service_for_instance(instance, args.handle);
  25423. +
  25424. + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
  25425. + /* Copy elements into kernel space */
  25426. + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
  25427. + if (copy_from_user(elements, args.elements,
  25428. + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
  25429. + status = vchiq_queue_message
  25430. + (args.handle,
  25431. + elements, args.count);
  25432. + else
  25433. + ret = -EFAULT;
  25434. + } else {
  25435. + ret = -EINVAL;
  25436. + }
  25437. + } break;
  25438. +
  25439. + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
  25440. + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
  25441. + VCHIQ_QUEUE_BULK_TRANSFER_T args;
  25442. + struct bulk_waiter_node *waiter = NULL;
  25443. + VCHIQ_BULK_DIR_T dir =
  25444. + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
  25445. + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
  25446. +
  25447. + if (copy_from_user
  25448. + (&args, (const void __user *)arg,
  25449. + sizeof(args)) != 0) {
  25450. + ret = -EFAULT;
  25451. + break;
  25452. + }
  25453. +
  25454. + service = find_service_for_instance(instance, args.handle);
  25455. + if (!service) {
  25456. + ret = -EINVAL;
  25457. + break;
  25458. + }
  25459. +
  25460. + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
  25461. + waiter = kzalloc(sizeof(struct bulk_waiter_node),
  25462. + GFP_KERNEL);
  25463. + if (!waiter) {
  25464. + ret = -ENOMEM;
  25465. + break;
  25466. + }
  25467. + args.userdata = &waiter->bulk_waiter;
  25468. + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
  25469. + struct list_head *pos;
  25470. + mutex_lock(&instance->bulk_waiter_list_mutex);
  25471. + list_for_each(pos, &instance->bulk_waiter_list) {
  25472. + if (list_entry(pos, struct bulk_waiter_node,
  25473. + list)->pid == current->pid) {
  25474. + waiter = list_entry(pos,
  25475. + struct bulk_waiter_node,
  25476. + list);
  25477. + list_del(pos);
  25478. + break;
  25479. + }
  25480. +
  25481. + }
  25482. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  25483. + if (!waiter) {
  25484. + vchiq_log_error(vchiq_arm_log_level,
  25485. + "no bulk_waiter found for pid %d",
  25486. + current->pid);
  25487. + ret = -ESRCH;
  25488. + break;
  25489. + }
  25490. + vchiq_log_info(vchiq_arm_log_level,
  25491. + "found bulk_waiter %x for pid %d",
  25492. + (unsigned int)waiter, current->pid);
  25493. + args.userdata = &waiter->bulk_waiter;
  25494. + }
  25495. + status = vchiq_bulk_transfer
  25496. + (args.handle,
  25497. + VCHI_MEM_HANDLE_INVALID,
  25498. + args.data, args.size,
  25499. + args.userdata, args.mode,
  25500. + dir);
  25501. + if (!waiter)
  25502. + break;
  25503. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  25504. + !waiter->bulk_waiter.bulk) {
  25505. + if (waiter->bulk_waiter.bulk) {
  25506. + /* Cancel the signal when the transfer
  25507. + ** completes. */
  25508. + spin_lock(&bulk_waiter_spinlock);
  25509. + waiter->bulk_waiter.bulk->userdata = NULL;
  25510. + spin_unlock(&bulk_waiter_spinlock);
  25511. + }
  25512. + kfree(waiter);
  25513. + } else {
  25514. + const VCHIQ_BULK_MODE_T mode_waiting =
  25515. + VCHIQ_BULK_MODE_WAITING;
  25516. + waiter->pid = current->pid;
  25517. + mutex_lock(&instance->bulk_waiter_list_mutex);
  25518. + list_add(&waiter->list, &instance->bulk_waiter_list);
  25519. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  25520. + vchiq_log_info(vchiq_arm_log_level,
  25521. + "saved bulk_waiter %x for pid %d",
  25522. + (unsigned int)waiter, current->pid);
  25523. +
  25524. + if (copy_to_user((void __user *)
  25525. + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
  25526. + arg)->mode),
  25527. + (const void *)&mode_waiting,
  25528. + sizeof(mode_waiting)) != 0)
  25529. + ret = -EFAULT;
  25530. + }
  25531. + } break;
  25532. +
  25533. + case VCHIQ_IOC_AWAIT_COMPLETION: {
  25534. + VCHIQ_AWAIT_COMPLETION_T args;
  25535. +
  25536. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  25537. + if (!instance->connected) {
  25538. + ret = -ENOTCONN;
  25539. + break;
  25540. + }
  25541. +
  25542. + if (copy_from_user(&args, (const void __user *)arg,
  25543. + sizeof(args)) != 0) {
  25544. + ret = -EFAULT;
  25545. + break;
  25546. + }
  25547. +
  25548. + mutex_lock(&instance->completion_mutex);
  25549. +
  25550. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  25551. + while ((instance->completion_remove ==
  25552. + instance->completion_insert)
  25553. + && !instance->closing) {
  25554. + int rc;
  25555. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  25556. + mutex_unlock(&instance->completion_mutex);
  25557. + rc = down_interruptible(&instance->insert_event);
  25558. + mutex_lock(&instance->completion_mutex);
  25559. + if (rc != 0) {
  25560. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  25561. + vchiq_log_info(vchiq_arm_log_level,
  25562. + "AWAIT_COMPLETION interrupted");
  25563. + ret = -EINTR;
  25564. + break;
  25565. + }
  25566. + }
  25567. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  25568. +
  25569. + /* A read memory barrier is needed to stop prefetch of a stale
  25570. + ** completion record
  25571. + */
  25572. + rmb();
  25573. +
  25574. + if (ret == 0) {
  25575. + int msgbufcount = args.msgbufcount;
  25576. + for (ret = 0; ret < args.count; ret++) {
  25577. + VCHIQ_COMPLETION_DATA_T *completion;
  25578. + VCHIQ_SERVICE_T *service;
  25579. + USER_SERVICE_T *user_service;
  25580. + VCHIQ_HEADER_T *header;
  25581. + if (instance->completion_remove ==
  25582. + instance->completion_insert)
  25583. + break;
  25584. + completion = &instance->completions[
  25585. + instance->completion_remove &
  25586. + (MAX_COMPLETIONS - 1)];
  25587. +
  25588. + service = completion->service_userdata;
  25589. + user_service = service->base.userdata;
  25590. + completion->service_userdata =
  25591. + user_service->userdata;
  25592. +
  25593. + header = completion->header;
  25594. + if (header) {
  25595. + void __user *msgbuf;
  25596. + int msglen;
  25597. +
  25598. + msglen = header->size +
  25599. + sizeof(VCHIQ_HEADER_T);
  25600. + /* This must be a VCHIQ-style service */
  25601. + if (args.msgbufsize < msglen) {
  25602. + vchiq_log_error(
  25603. + vchiq_arm_log_level,
  25604. + "header %x: msgbufsize"
  25605. + " %x < msglen %x",
  25606. + (unsigned int)header,
  25607. + args.msgbufsize,
  25608. + msglen);
  25609. + WARN(1, "invalid message "
  25610. + "size\n");
  25611. + if (ret == 0)
  25612. + ret = -EMSGSIZE;
  25613. + break;
  25614. + }
  25615. + if (msgbufcount <= 0)
  25616. + /* Stall here for lack of a
  25617. + ** buffer for the message. */
  25618. + break;
  25619. + /* Get the pointer from user space */
  25620. + msgbufcount--;
  25621. + if (copy_from_user(&msgbuf,
  25622. + (const void __user *)
  25623. + &args.msgbufs[msgbufcount],
  25624. + sizeof(msgbuf)) != 0) {
  25625. + if (ret == 0)
  25626. + ret = -EFAULT;
  25627. + break;
  25628. + }
  25629. +
  25630. + /* Copy the message to user space */
  25631. + if (copy_to_user(msgbuf, header,
  25632. + msglen) != 0) {
  25633. + if (ret == 0)
  25634. + ret = -EFAULT;
  25635. + break;
  25636. + }
  25637. +
  25638. + /* Now it has been copied, the message
  25639. + ** can be released. */
  25640. + vchiq_release_message(service->handle,
  25641. + header);
  25642. +
  25643. + /* The completion must point to the
  25644. + ** msgbuf. */
  25645. + completion->header = msgbuf;
  25646. + }
  25647. +
  25648. + if ((completion->reason ==
  25649. + VCHIQ_SERVICE_CLOSED) &&
  25650. + !instance->use_close_delivered)
  25651. + unlock_service(service);
  25652. +
  25653. + if (copy_to_user((void __user *)(
  25654. + (size_t)args.buf +
  25655. + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
  25656. + completion,
  25657. + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
  25658. + if (ret == 0)
  25659. + ret = -EFAULT;
  25660. + break;
  25661. + }
  25662. +
  25663. + instance->completion_remove++;
  25664. + }
  25665. +
  25666. + if (msgbufcount != args.msgbufcount) {
  25667. + if (copy_to_user((void __user *)
  25668. + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
  25669. + msgbufcount,
  25670. + &msgbufcount,
  25671. + sizeof(msgbufcount)) != 0) {
  25672. + ret = -EFAULT;
  25673. + }
  25674. + }
  25675. + }
  25676. +
  25677. + if (ret != 0)
  25678. + up(&instance->remove_event);
  25679. + mutex_unlock(&instance->completion_mutex);
  25680. + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
  25681. + } break;
  25682. +
  25683. + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
  25684. + VCHIQ_DEQUEUE_MESSAGE_T args;
  25685. + USER_SERVICE_T *user_service;
  25686. + VCHIQ_HEADER_T *header;
  25687. +
  25688. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  25689. + if (copy_from_user
  25690. + (&args, (const void __user *)arg,
  25691. + sizeof(args)) != 0) {
  25692. + ret = -EFAULT;
  25693. + break;
  25694. + }
  25695. + service = find_service_for_instance(instance, args.handle);
  25696. + if (!service) {
  25697. + ret = -EINVAL;
  25698. + break;
  25699. + }
  25700. + user_service = (USER_SERVICE_T *)service->base.userdata;
  25701. + if (user_service->is_vchi == 0) {
  25702. + ret = -EINVAL;
  25703. + break;
  25704. + }
  25705. +
  25706. + spin_lock(&msg_queue_spinlock);
  25707. + if (user_service->msg_remove == user_service->msg_insert) {
  25708. + if (!args.blocking) {
  25709. + spin_unlock(&msg_queue_spinlock);
  25710. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  25711. + ret = -EWOULDBLOCK;
  25712. + break;
  25713. + }
  25714. + user_service->dequeue_pending = 1;
  25715. + do {
  25716. + spin_unlock(&msg_queue_spinlock);
  25717. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  25718. + if (down_interruptible(
  25719. + &user_service->insert_event) != 0) {
  25720. + vchiq_log_info(vchiq_arm_log_level,
  25721. + "DEQUEUE_MESSAGE interrupted");
  25722. + ret = -EINTR;
  25723. + break;
  25724. + }
  25725. + spin_lock(&msg_queue_spinlock);
  25726. + } while (user_service->msg_remove ==
  25727. + user_service->msg_insert);
  25728. +
  25729. + if (ret)
  25730. + break;
  25731. + }
  25732. +
  25733. + BUG_ON((int)(user_service->msg_insert -
  25734. + user_service->msg_remove) < 0);
  25735. +
  25736. + header = user_service->msg_queue[user_service->msg_remove &
  25737. + (MSG_QUEUE_SIZE - 1)];
  25738. + user_service->msg_remove++;
  25739. + spin_unlock(&msg_queue_spinlock);
  25740. +
  25741. + up(&user_service->remove_event);
  25742. + if (header == NULL)
  25743. + ret = -ENOTCONN;
  25744. + else if (header->size <= args.bufsize) {
  25745. + /* Copy to user space if msgbuf is not NULL */
  25746. + if ((args.buf == NULL) ||
  25747. + (copy_to_user((void __user *)args.buf,
  25748. + header->data,
  25749. + header->size) == 0)) {
  25750. + ret = header->size;
  25751. + vchiq_release_message(
  25752. + service->handle,
  25753. + header);
  25754. + } else
  25755. + ret = -EFAULT;
  25756. + } else {
  25757. + vchiq_log_error(vchiq_arm_log_level,
  25758. + "header %x: bufsize %x < size %x",
  25759. + (unsigned int)header, args.bufsize,
  25760. + header->size);
  25761. + WARN(1, "invalid size\n");
  25762. + ret = -EMSGSIZE;
  25763. + }
  25764. + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
  25765. + } break;
  25766. +
  25767. + case VCHIQ_IOC_GET_CLIENT_ID: {
  25768. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  25769. +
  25770. + ret = vchiq_get_client_id(handle);
  25771. + } break;
  25772. +
  25773. + case VCHIQ_IOC_GET_CONFIG: {
  25774. + VCHIQ_GET_CONFIG_T args;
  25775. + VCHIQ_CONFIG_T config;
  25776. +
  25777. + if (copy_from_user(&args, (const void __user *)arg,
  25778. + sizeof(args)) != 0) {
  25779. + ret = -EFAULT;
  25780. + break;
  25781. + }
  25782. + if (args.config_size > sizeof(config)) {
  25783. + ret = -EINVAL;
  25784. + break;
  25785. + }
  25786. + status = vchiq_get_config(instance, args.config_size, &config);
  25787. + if (status == VCHIQ_SUCCESS) {
  25788. + if (copy_to_user((void __user *)args.pconfig,
  25789. + &config, args.config_size) != 0) {
  25790. + ret = -EFAULT;
  25791. + break;
  25792. + }
  25793. + }
  25794. + } break;
  25795. +
  25796. + case VCHIQ_IOC_SET_SERVICE_OPTION: {
  25797. + VCHIQ_SET_SERVICE_OPTION_T args;
  25798. +
  25799. + if (copy_from_user(
  25800. + &args, (const void __user *)arg,
  25801. + sizeof(args)) != 0) {
  25802. + ret = -EFAULT;
  25803. + break;
  25804. + }
  25805. +
  25806. + service = find_service_for_instance(instance, args.handle);
  25807. + if (!service) {
  25808. + ret = -EINVAL;
  25809. + break;
  25810. + }
  25811. +
  25812. + status = vchiq_set_service_option(
  25813. + args.handle, args.option, args.value);
  25814. + } break;
  25815. +
  25816. + case VCHIQ_IOC_DUMP_PHYS_MEM: {
  25817. + VCHIQ_DUMP_MEM_T args;
  25818. +
  25819. + if (copy_from_user
  25820. + (&args, (const void __user *)arg,
  25821. + sizeof(args)) != 0) {
  25822. + ret = -EFAULT;
  25823. + break;
  25824. + }
  25825. + dump_phys_mem(args.virt_addr, args.num_bytes);
  25826. + } break;
  25827. +
  25828. + case VCHIQ_IOC_LIB_VERSION: {
  25829. + unsigned int lib_version = (unsigned int)arg;
  25830. +
  25831. + if (lib_version < VCHIQ_VERSION_MIN)
  25832. + ret = -EINVAL;
  25833. + else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
  25834. + instance->use_close_delivered = 1;
  25835. + } break;
  25836. +
  25837. + case VCHIQ_IOC_CLOSE_DELIVERED: {
  25838. + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
  25839. +
  25840. + service = find_closed_service_for_instance(instance, handle);
  25841. + if (service != NULL) {
  25842. + USER_SERVICE_T *user_service =
  25843. + (USER_SERVICE_T *)service->base.userdata;
  25844. + close_delivered(user_service);
  25845. + }
  25846. + else
  25847. + ret = -EINVAL;
  25848. + } break;
  25849. +
  25850. + default:
  25851. + ret = -ENOTTY;
  25852. + break;
  25853. + }
  25854. +
  25855. + if (service)
  25856. + unlock_service(service);
  25857. +
  25858. + if (ret == 0) {
  25859. + if (status == VCHIQ_ERROR)
  25860. + ret = -EIO;
  25861. + else if (status == VCHIQ_RETRY)
  25862. + ret = -EINTR;
  25863. + }
  25864. +
  25865. + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
  25866. + (ret != -EWOULDBLOCK))
  25867. + vchiq_log_info(vchiq_arm_log_level,
  25868. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  25869. + (unsigned long)instance,
  25870. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  25871. + ioctl_names[_IOC_NR(cmd)] :
  25872. + "<invalid>",
  25873. + status, ret);
  25874. + else
  25875. + vchiq_log_trace(vchiq_arm_log_level,
  25876. + " ioctl instance %lx, cmd %s -> status %d, %ld",
  25877. + (unsigned long)instance,
  25878. + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
  25879. + ioctl_names[_IOC_NR(cmd)] :
  25880. + "<invalid>",
  25881. + status, ret);
  25882. +
  25883. + return ret;
  25884. +}
  25885. +
  25886. +/****************************************************************************
  25887. +*
  25888. +* vchiq_open
  25889. +*
  25890. +***************************************************************************/
  25891. +
  25892. +static int
  25893. +vchiq_open(struct inode *inode, struct file *file)
  25894. +{
  25895. + int dev = iminor(inode) & 0x0f;
  25896. + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
  25897. + switch (dev) {
  25898. + case VCHIQ_MINOR: {
  25899. + int ret;
  25900. + VCHIQ_STATE_T *state = vchiq_get_state();
  25901. + VCHIQ_INSTANCE_T instance;
  25902. +
  25903. + if (!state) {
  25904. + vchiq_log_error(vchiq_arm_log_level,
  25905. + "vchiq has no connection to VideoCore");
  25906. + return -ENOTCONN;
  25907. + }
  25908. +
  25909. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  25910. + if (!instance)
  25911. + return -ENOMEM;
  25912. +
  25913. + instance->state = state;
  25914. + instance->pid = current->tgid;
  25915. +
  25916. + ret = vchiq_proc_add_instance(instance);
  25917. + if (ret != 0) {
  25918. + kfree(instance);
  25919. + return ret;
  25920. + }
  25921. +
  25922. + sema_init(&instance->insert_event, 0);
  25923. + sema_init(&instance->remove_event, 0);
  25924. + mutex_init(&instance->completion_mutex);
  25925. + mutex_init(&instance->bulk_waiter_list_mutex);
  25926. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  25927. +
  25928. + file->private_data = instance;
  25929. + } break;
  25930. +
  25931. + default:
  25932. + vchiq_log_error(vchiq_arm_log_level,
  25933. + "Unknown minor device: %d", dev);
  25934. + return -ENXIO;
  25935. + }
  25936. +
  25937. + return 0;
  25938. +}
  25939. +
  25940. +/****************************************************************************
  25941. +*
  25942. +* vchiq_release
  25943. +*
  25944. +***************************************************************************/
  25945. +
  25946. +static int
  25947. +vchiq_release(struct inode *inode, struct file *file)
  25948. +{
  25949. + int dev = iminor(inode) & 0x0f;
  25950. + int ret = 0;
  25951. + switch (dev) {
  25952. + case VCHIQ_MINOR: {
  25953. + VCHIQ_INSTANCE_T instance = file->private_data;
  25954. + VCHIQ_STATE_T *state = vchiq_get_state();
  25955. + VCHIQ_SERVICE_T *service;
  25956. + int i;
  25957. +
  25958. + vchiq_log_info(vchiq_arm_log_level,
  25959. + "vchiq_release: instance=%lx",
  25960. + (unsigned long)instance);
  25961. +
  25962. + if (!state) {
  25963. + ret = -EPERM;
  25964. + goto out;
  25965. + }
  25966. +
  25967. + /* Ensure videocore is awake to allow termination. */
  25968. + vchiq_use_internal(instance->state, NULL,
  25969. + USE_TYPE_VCHIQ);
  25970. +
  25971. + mutex_lock(&instance->completion_mutex);
  25972. +
  25973. + /* Wake the completion thread and ask it to exit */
  25974. + instance->closing = 1;
  25975. + up(&instance->insert_event);
  25976. +
  25977. + mutex_unlock(&instance->completion_mutex);
  25978. +
  25979. + /* Wake the slot handler if the completion queue is full. */
  25980. + up(&instance->remove_event);
  25981. +
  25982. + /* Mark all services for termination... */
  25983. + i = 0;
  25984. + while ((service = next_service_by_instance(state, instance,
  25985. + &i)) != NULL) {
  25986. + USER_SERVICE_T *user_service = service->base.userdata;
  25987. +
  25988. + /* Wake the slot handler if the msg queue is full. */
  25989. + up(&user_service->remove_event);
  25990. +
  25991. + vchiq_terminate_service_internal(service);
  25992. + unlock_service(service);
  25993. + }
  25994. +
  25995. + /* ...and wait for them to die */
  25996. + i = 0;
  25997. + while ((service = next_service_by_instance(state, instance, &i))
  25998. + != NULL) {
  25999. + USER_SERVICE_T *user_service = service->base.userdata;
  26000. +
  26001. + down(&service->remove_event);
  26002. +
  26003. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  26004. +
  26005. + spin_lock(&msg_queue_spinlock);
  26006. +
  26007. + while (user_service->msg_remove !=
  26008. + user_service->msg_insert) {
  26009. + VCHIQ_HEADER_T *header = user_service->
  26010. + msg_queue[user_service->msg_remove &
  26011. + (MSG_QUEUE_SIZE - 1)];
  26012. + user_service->msg_remove++;
  26013. + spin_unlock(&msg_queue_spinlock);
  26014. +
  26015. + if (header)
  26016. + vchiq_release_message(
  26017. + service->handle,
  26018. + header);
  26019. + spin_lock(&msg_queue_spinlock);
  26020. + }
  26021. +
  26022. + spin_unlock(&msg_queue_spinlock);
  26023. +
  26024. + unlock_service(service);
  26025. + }
  26026. +
  26027. + /* Release any closed services */
  26028. + while (instance->completion_remove !=
  26029. + instance->completion_insert) {
  26030. + VCHIQ_COMPLETION_DATA_T *completion;
  26031. + VCHIQ_SERVICE_T *service;
  26032. + completion = &instance->completions[
  26033. + instance->completion_remove &
  26034. + (MAX_COMPLETIONS - 1)];
  26035. + service = completion->service_userdata;
  26036. + if (completion->reason == VCHIQ_SERVICE_CLOSED)
  26037. + {
  26038. + USER_SERVICE_T *user_service =
  26039. + service->base.userdata;
  26040. +
  26041. + /* Wake any blocked user-thread */
  26042. + if (instance->use_close_delivered)
  26043. + up(&user_service->close_event);
  26044. + unlock_service(service);
  26045. + }
  26046. + instance->completion_remove++;
  26047. + }
  26048. +
  26049. + /* Release the PEER service count. */
  26050. + vchiq_release_internal(instance->state, NULL);
  26051. +
  26052. + {
  26053. + struct list_head *pos, *next;
  26054. + list_for_each_safe(pos, next,
  26055. + &instance->bulk_waiter_list) {
  26056. + struct bulk_waiter_node *waiter;
  26057. + waiter = list_entry(pos,
  26058. + struct bulk_waiter_node,
  26059. + list);
  26060. + list_del(pos);
  26061. + vchiq_log_info(vchiq_arm_log_level,
  26062. + "bulk_waiter - cleaned up %x "
  26063. + "for pid %d",
  26064. + (unsigned int)waiter, waiter->pid);
  26065. + kfree(waiter);
  26066. + }
  26067. + }
  26068. +
  26069. + vchiq_proc_remove_instance(instance);
  26070. +
  26071. + kfree(instance);
  26072. + file->private_data = NULL;
  26073. + } break;
  26074. +
  26075. + default:
  26076. + vchiq_log_error(vchiq_arm_log_level,
  26077. + "Unknown minor device: %d", dev);
  26078. + ret = -ENXIO;
  26079. + }
  26080. +
  26081. +out:
  26082. + return ret;
  26083. +}
  26084. +
  26085. +/****************************************************************************
  26086. +*
  26087. +* vchiq_dump
  26088. +*
  26089. +***************************************************************************/
  26090. +
  26091. +void
  26092. +vchiq_dump(void *dump_context, const char *str, int len)
  26093. +{
  26094. + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
  26095. +
  26096. + if (context->actual < context->space) {
  26097. + int copy_bytes;
  26098. + if (context->offset > 0) {
  26099. + int skip_bytes = min(len, (int)context->offset);
  26100. + str += skip_bytes;
  26101. + len -= skip_bytes;
  26102. + context->offset -= skip_bytes;
  26103. + if (context->offset > 0)
  26104. + return;
  26105. + }
  26106. + copy_bytes = min(len, (int)(context->space - context->actual));
  26107. + if (copy_bytes == 0)
  26108. + return;
  26109. + if (copy_to_user(context->buf + context->actual, str,
  26110. + copy_bytes))
  26111. + context->actual = -EFAULT;
  26112. + context->actual += copy_bytes;
  26113. + len -= copy_bytes;
  26114. +
  26115. + /* If tne terminating NUL is included in the length, then it
  26116. + ** marks the end of a line and should be replaced with a
  26117. + ** carriage return. */
  26118. + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
  26119. + char cr = '\n';
  26120. + if (copy_to_user(context->buf + context->actual - 1,
  26121. + &cr, 1))
  26122. + context->actual = -EFAULT;
  26123. + }
  26124. + }
  26125. +}
  26126. +
  26127. +/****************************************************************************
  26128. +*
  26129. +* vchiq_dump_platform_instance_state
  26130. +*
  26131. +***************************************************************************/
  26132. +
  26133. +void
  26134. +vchiq_dump_platform_instances(void *dump_context)
  26135. +{
  26136. + VCHIQ_STATE_T *state = vchiq_get_state();
  26137. + char buf[80];
  26138. + int len;
  26139. + int i;
  26140. +
  26141. + /* There is no list of instances, so instead scan all services,
  26142. + marking those that have been dumped. */
  26143. +
  26144. + for (i = 0; i < state->unused_service; i++) {
  26145. + VCHIQ_SERVICE_T *service = state->services[i];
  26146. + VCHIQ_INSTANCE_T instance;
  26147. +
  26148. + if (service && (service->base.callback == service_callback)) {
  26149. + instance = service->instance;
  26150. + if (instance)
  26151. + instance->mark = 0;
  26152. + }
  26153. + }
  26154. +
  26155. + for (i = 0; i < state->unused_service; i++) {
  26156. + VCHIQ_SERVICE_T *service = state->services[i];
  26157. + VCHIQ_INSTANCE_T instance;
  26158. +
  26159. + if (service && (service->base.callback == service_callback)) {
  26160. + instance = service->instance;
  26161. + if (instance && !instance->mark) {
  26162. + len = snprintf(buf, sizeof(buf),
  26163. + "Instance %x: pid %d,%s completions "
  26164. + "%d/%d",
  26165. + (unsigned int)instance, instance->pid,
  26166. + instance->connected ? " connected, " :
  26167. + "",
  26168. + instance->completion_insert -
  26169. + instance->completion_remove,
  26170. + MAX_COMPLETIONS);
  26171. +
  26172. + vchiq_dump(dump_context, buf, len + 1);
  26173. +
  26174. + instance->mark = 1;
  26175. + }
  26176. + }
  26177. + }
  26178. +}
  26179. +
  26180. +/****************************************************************************
  26181. +*
  26182. +* vchiq_dump_platform_service_state
  26183. +*
  26184. +***************************************************************************/
  26185. +
  26186. +void
  26187. +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  26188. +{
  26189. + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
  26190. + char buf[80];
  26191. + int len;
  26192. +
  26193. + len = snprintf(buf, sizeof(buf), " instance %x",
  26194. + (unsigned int)service->instance);
  26195. +
  26196. + if ((service->base.callback == service_callback) &&
  26197. + user_service->is_vchi) {
  26198. + len += snprintf(buf + len, sizeof(buf) - len,
  26199. + ", %d/%d messages",
  26200. + user_service->msg_insert - user_service->msg_remove,
  26201. + MSG_QUEUE_SIZE);
  26202. +
  26203. + if (user_service->dequeue_pending)
  26204. + len += snprintf(buf + len, sizeof(buf) - len,
  26205. + " (dequeue pending)");
  26206. + }
  26207. +
  26208. + vchiq_dump(dump_context, buf, len + 1);
  26209. +}
  26210. +
  26211. +/****************************************************************************
  26212. +*
  26213. +* dump_user_mem
  26214. +*
  26215. +***************************************************************************/
  26216. +
  26217. +static void
  26218. +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
  26219. +{
  26220. + int rc;
  26221. + uint8_t *end_virt_addr = virt_addr + num_bytes;
  26222. + int num_pages;
  26223. + int offset;
  26224. + int end_offset;
  26225. + int page_idx;
  26226. + int prev_idx;
  26227. + struct page *page;
  26228. + struct page **pages;
  26229. + uint8_t *kmapped_virt_ptr;
  26230. +
  26231. + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
  26232. +
  26233. + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
  26234. + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
  26235. + ~0x0fuL);
  26236. +
  26237. + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
  26238. + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
  26239. +
  26240. + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  26241. +
  26242. + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  26243. + if (pages == NULL) {
  26244. + vchiq_log_error(vchiq_arm_log_level,
  26245. + "Unable to allocation memory for %d pages\n",
  26246. + num_pages);
  26247. + return;
  26248. + }
  26249. +
  26250. + down_read(&current->mm->mmap_sem);
  26251. + rc = get_user_pages(current, /* task */
  26252. + current->mm, /* mm */
  26253. + (unsigned long)virt_addr, /* start */
  26254. + num_pages, /* len */
  26255. + 0, /* write */
  26256. + 0, /* force */
  26257. + pages, /* pages (array of page pointers) */
  26258. + NULL); /* vmas */
  26259. + up_read(&current->mm->mmap_sem);
  26260. +
  26261. + prev_idx = -1;
  26262. + page = NULL;
  26263. +
  26264. + while (offset < end_offset) {
  26265. +
  26266. + int page_offset = offset % PAGE_SIZE;
  26267. + page_idx = offset / PAGE_SIZE;
  26268. +
  26269. + if (page_idx != prev_idx) {
  26270. +
  26271. + if (page != NULL)
  26272. + kunmap(page);
  26273. + page = pages[page_idx];
  26274. + kmapped_virt_ptr = kmap(page);
  26275. +
  26276. + prev_idx = page_idx;
  26277. + }
  26278. +
  26279. + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
  26280. + vchiq_log_dump_mem("ph",
  26281. + (uint32_t)(unsigned long)&kmapped_virt_ptr[
  26282. + page_offset],
  26283. + &kmapped_virt_ptr[page_offset], 16);
  26284. +
  26285. + offset += 16;
  26286. + }
  26287. + if (page != NULL)
  26288. + kunmap(page);
  26289. +
  26290. + for (page_idx = 0; page_idx < num_pages; page_idx++)
  26291. + page_cache_release(pages[page_idx]);
  26292. +
  26293. + kfree(pages);
  26294. +}
  26295. +
  26296. +/****************************************************************************
  26297. +*
  26298. +* vchiq_read
  26299. +*
  26300. +***************************************************************************/
  26301. +
  26302. +static ssize_t
  26303. +vchiq_read(struct file *file, char __user *buf,
  26304. + size_t count, loff_t *ppos)
  26305. +{
  26306. + DUMP_CONTEXT_T context;
  26307. + context.buf = buf;
  26308. + context.actual = 0;
  26309. + context.space = count;
  26310. + context.offset = *ppos;
  26311. +
  26312. + vchiq_dump_state(&context, &g_state);
  26313. +
  26314. + *ppos += context.actual;
  26315. +
  26316. + return context.actual;
  26317. +}
  26318. +
  26319. +VCHIQ_STATE_T *
  26320. +vchiq_get_state(void)
  26321. +{
  26322. +
  26323. + if (g_state.remote == NULL)
  26324. + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
  26325. + else if (g_state.remote->initialised != 1)
  26326. + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
  26327. + __func__, g_state.remote->initialised);
  26328. +
  26329. + return ((g_state.remote != NULL) &&
  26330. + (g_state.remote->initialised == 1)) ? &g_state : NULL;
  26331. +}
  26332. +
  26333. +static const struct file_operations
  26334. +vchiq_fops = {
  26335. + .owner = THIS_MODULE,
  26336. + .unlocked_ioctl = vchiq_ioctl,
  26337. + .open = vchiq_open,
  26338. + .release = vchiq_release,
  26339. + .read = vchiq_read
  26340. +};
  26341. +
  26342. +/*
  26343. + * Autosuspend related functionality
  26344. + */
  26345. +
  26346. +int
  26347. +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
  26348. +{
  26349. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26350. + if (!arm_state)
  26351. + /* autosuspend not supported - always return wanted */
  26352. + return 1;
  26353. + else if (arm_state->blocked_count)
  26354. + return 1;
  26355. + else if (!arm_state->videocore_use_count)
  26356. + /* usage count zero - check for override unless we're forcing */
  26357. + if (arm_state->resume_blocked)
  26358. + return 0;
  26359. + else
  26360. + return vchiq_platform_videocore_wanted(state);
  26361. + else
  26362. + /* non-zero usage count - videocore still required */
  26363. + return 1;
  26364. +}
  26365. +
  26366. +static VCHIQ_STATUS_T
  26367. +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
  26368. + VCHIQ_HEADER_T *header,
  26369. + VCHIQ_SERVICE_HANDLE_T service_user,
  26370. + void *bulk_user)
  26371. +{
  26372. + vchiq_log_error(vchiq_susp_log_level,
  26373. + "%s callback reason %d", __func__, reason);
  26374. + return 0;
  26375. +}
  26376. +
  26377. +static int
  26378. +vchiq_keepalive_thread_func(void *v)
  26379. +{
  26380. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  26381. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26382. +
  26383. + VCHIQ_STATUS_T status;
  26384. + VCHIQ_INSTANCE_T instance;
  26385. + VCHIQ_SERVICE_HANDLE_T ka_handle;
  26386. +
  26387. + VCHIQ_SERVICE_PARAMS_T params = {
  26388. + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
  26389. + .callback = vchiq_keepalive_vchiq_callback,
  26390. + .version = KEEPALIVE_VER,
  26391. + .version_min = KEEPALIVE_VER_MIN
  26392. + };
  26393. +
  26394. + status = vchiq_initialise(&instance);
  26395. + if (status != VCHIQ_SUCCESS) {
  26396. + vchiq_log_error(vchiq_susp_log_level,
  26397. + "%s vchiq_initialise failed %d", __func__, status);
  26398. + goto exit;
  26399. + }
  26400. +
  26401. + status = vchiq_connect(instance);
  26402. + if (status != VCHIQ_SUCCESS) {
  26403. + vchiq_log_error(vchiq_susp_log_level,
  26404. + "%s vchiq_connect failed %d", __func__, status);
  26405. + goto shutdown;
  26406. + }
  26407. +
  26408. + status = vchiq_add_service(instance, &params, &ka_handle);
  26409. + if (status != VCHIQ_SUCCESS) {
  26410. + vchiq_log_error(vchiq_susp_log_level,
  26411. + "%s vchiq_open_service failed %d", __func__, status);
  26412. + goto shutdown;
  26413. + }
  26414. +
  26415. + while (1) {
  26416. + long rc = 0, uc = 0;
  26417. + if (wait_for_completion_interruptible(&arm_state->ka_evt)
  26418. + != 0) {
  26419. + vchiq_log_error(vchiq_susp_log_level,
  26420. + "%s interrupted", __func__);
  26421. + flush_signals(current);
  26422. + continue;
  26423. + }
  26424. +
  26425. + /* read and clear counters. Do release_count then use_count to
  26426. + * prevent getting more releases than uses */
  26427. + rc = atomic_xchg(&arm_state->ka_release_count, 0);
  26428. + uc = atomic_xchg(&arm_state->ka_use_count, 0);
  26429. +
  26430. + /* Call use/release service the requisite number of times.
  26431. + * Process use before release so use counts don't go negative */
  26432. + while (uc--) {
  26433. + atomic_inc(&arm_state->ka_use_ack_count);
  26434. + status = vchiq_use_service(ka_handle);
  26435. + if (status != VCHIQ_SUCCESS) {
  26436. + vchiq_log_error(vchiq_susp_log_level,
  26437. + "%s vchiq_use_service error %d",
  26438. + __func__, status);
  26439. + }
  26440. + }
  26441. + while (rc--) {
  26442. + status = vchiq_release_service(ka_handle);
  26443. + if (status != VCHIQ_SUCCESS) {
  26444. + vchiq_log_error(vchiq_susp_log_level,
  26445. + "%s vchiq_release_service error %d",
  26446. + __func__, status);
  26447. + }
  26448. + }
  26449. + }
  26450. +
  26451. +shutdown:
  26452. + vchiq_shutdown(instance);
  26453. +exit:
  26454. + return 0;
  26455. +}
  26456. +
  26457. +
  26458. +
  26459. +VCHIQ_STATUS_T
  26460. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
  26461. +{
  26462. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  26463. +
  26464. + if (arm_state) {
  26465. + rwlock_init(&arm_state->susp_res_lock);
  26466. +
  26467. + init_completion(&arm_state->ka_evt);
  26468. + atomic_set(&arm_state->ka_use_count, 0);
  26469. + atomic_set(&arm_state->ka_use_ack_count, 0);
  26470. + atomic_set(&arm_state->ka_release_count, 0);
  26471. +
  26472. + init_completion(&arm_state->vc_suspend_complete);
  26473. +
  26474. + init_completion(&arm_state->vc_resume_complete);
  26475. + /* Initialise to 'done' state. We only want to block on resume
  26476. + * completion while videocore is suspended. */
  26477. + set_resume_state(arm_state, VC_RESUME_RESUMED);
  26478. +
  26479. + init_completion(&arm_state->resume_blocker);
  26480. + /* Initialise to 'done' state. We only want to block on this
  26481. + * completion while resume is blocked */
  26482. + complete_all(&arm_state->resume_blocker);
  26483. +
  26484. + init_completion(&arm_state->blocked_blocker);
  26485. + /* Initialise to 'done' state. We only want to block on this
  26486. + * completion while things are waiting on the resume blocker */
  26487. + complete_all(&arm_state->blocked_blocker);
  26488. +
  26489. + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
  26490. + arm_state->suspend_timer_running = 0;
  26491. + init_timer(&arm_state->suspend_timer);
  26492. + arm_state->suspend_timer.data = (unsigned long)(state);
  26493. + arm_state->suspend_timer.function = suspend_timer_callback;
  26494. +
  26495. + arm_state->first_connect = 0;
  26496. +
  26497. + }
  26498. + return status;
  26499. +}
  26500. +
  26501. +/*
  26502. +** Functions to modify the state variables;
  26503. +** set_suspend_state
  26504. +** set_resume_state
  26505. +**
  26506. +** There are more state variables than we might like, so ensure they remain in
  26507. +** step. Suspend and resume state are maintained separately, since most of
  26508. +** these state machines can operate independently. However, there are a few
  26509. +** states where state transitions in one state machine cause a reset to the
  26510. +** other state machine. In addition, there are some completion events which
  26511. +** need to occur on state machine reset and end-state(s), so these are also
  26512. +** dealt with in these functions.
  26513. +**
  26514. +** In all states we set the state variable according to the input, but in some
  26515. +** cases we perform additional steps outlined below;
  26516. +**
  26517. +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
  26518. +** The suspend completion is completed after any suspend
  26519. +** attempt. When we reset the state machine we also reset
  26520. +** the completion. This reset occurs when videocore is
  26521. +** resumed, and also if we initiate suspend after a suspend
  26522. +** failure.
  26523. +**
  26524. +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
  26525. +** suspend - ie from this point on we must try to suspend
  26526. +** before resuming can occur. We therefore also reset the
  26527. +** resume state machine to VC_RESUME_IDLE in this state.
  26528. +**
  26529. +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
  26530. +** complete_all on the suspend completion to notify
  26531. +** anything waiting for suspend to happen.
  26532. +**
  26533. +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
  26534. +** initiate resume, so no need to alter resume state.
  26535. +** We call complete_all on the suspend completion to notify
  26536. +** of suspend rejection.
  26537. +**
  26538. +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
  26539. +** suspend completion and reset the resume state machine.
  26540. +**
  26541. +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
  26542. +** resume completion is in it's 'done' state whenever
  26543. +** videcore is running. Therfore, the VC_RESUME_IDLE state
  26544. +** implies that videocore is suspended.
  26545. +** Hence, any thread which needs to wait until videocore is
  26546. +** running can wait on this completion - it will only block
  26547. +** if videocore is suspended.
  26548. +**
  26549. +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
  26550. +** Call complete_all on the resume completion to unblock
  26551. +** any threads waiting for resume. Also reset the suspend
  26552. +** state machine to it's idle state.
  26553. +**
  26554. +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
  26555. +*/
  26556. +
  26557. +inline void
  26558. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  26559. + enum vc_suspend_status new_state)
  26560. +{
  26561. + /* set the state in all cases */
  26562. + arm_state->vc_suspend_state = new_state;
  26563. +
  26564. + /* state specific additional actions */
  26565. + switch (new_state) {
  26566. + case VC_SUSPEND_FORCE_CANCELED:
  26567. + complete_all(&arm_state->vc_suspend_complete);
  26568. + break;
  26569. + case VC_SUSPEND_REJECTED:
  26570. + complete_all(&arm_state->vc_suspend_complete);
  26571. + break;
  26572. + case VC_SUSPEND_FAILED:
  26573. + complete_all(&arm_state->vc_suspend_complete);
  26574. + arm_state->vc_resume_state = VC_RESUME_RESUMED;
  26575. + complete_all(&arm_state->vc_resume_complete);
  26576. + break;
  26577. + case VC_SUSPEND_IDLE:
  26578. + reinit_completion(&arm_state->vc_suspend_complete);
  26579. + break;
  26580. + case VC_SUSPEND_REQUESTED:
  26581. + break;
  26582. + case VC_SUSPEND_IN_PROGRESS:
  26583. + set_resume_state(arm_state, VC_RESUME_IDLE);
  26584. + break;
  26585. + case VC_SUSPEND_SUSPENDED:
  26586. + complete_all(&arm_state->vc_suspend_complete);
  26587. + break;
  26588. + default:
  26589. + BUG();
  26590. + break;
  26591. + }
  26592. +}
  26593. +
  26594. +inline void
  26595. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  26596. + enum vc_resume_status new_state)
  26597. +{
  26598. + /* set the state in all cases */
  26599. + arm_state->vc_resume_state = new_state;
  26600. +
  26601. + /* state specific additional actions */
  26602. + switch (new_state) {
  26603. + case VC_RESUME_FAILED:
  26604. + break;
  26605. + case VC_RESUME_IDLE:
  26606. + reinit_completion(&arm_state->vc_resume_complete);
  26607. + break;
  26608. + case VC_RESUME_REQUESTED:
  26609. + break;
  26610. + case VC_RESUME_IN_PROGRESS:
  26611. + break;
  26612. + case VC_RESUME_RESUMED:
  26613. + complete_all(&arm_state->vc_resume_complete);
  26614. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  26615. + break;
  26616. + default:
  26617. + BUG();
  26618. + break;
  26619. + }
  26620. +}
  26621. +
  26622. +
  26623. +/* should be called with the write lock held */
  26624. +inline void
  26625. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  26626. +{
  26627. + del_timer(&arm_state->suspend_timer);
  26628. + arm_state->suspend_timer.expires = jiffies +
  26629. + msecs_to_jiffies(arm_state->
  26630. + suspend_timer_timeout);
  26631. + add_timer(&arm_state->suspend_timer);
  26632. + arm_state->suspend_timer_running = 1;
  26633. +}
  26634. +
  26635. +/* should be called with the write lock held */
  26636. +static inline void
  26637. +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
  26638. +{
  26639. + if (arm_state->suspend_timer_running) {
  26640. + del_timer(&arm_state->suspend_timer);
  26641. + arm_state->suspend_timer_running = 0;
  26642. + }
  26643. +}
  26644. +
  26645. +static inline int
  26646. +need_resume(VCHIQ_STATE_T *state)
  26647. +{
  26648. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26649. + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
  26650. + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
  26651. + vchiq_videocore_wanted(state);
  26652. +}
  26653. +
  26654. +static int
  26655. +block_resume(VCHIQ_ARM_STATE_T *arm_state)
  26656. +{
  26657. + int status = VCHIQ_SUCCESS;
  26658. + const unsigned long timeout_val =
  26659. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
  26660. + int resume_count = 0;
  26661. +
  26662. + /* Allow any threads which were blocked by the last force suspend to
  26663. + * complete if they haven't already. Only give this one shot; if
  26664. + * blocked_count is incremented after blocked_blocker is completed
  26665. + * (which only happens when blocked_count hits 0) then those threads
  26666. + * will have to wait until next time around */
  26667. + if (arm_state->blocked_count) {
  26668. + reinit_completion(&arm_state->blocked_blocker);
  26669. + write_unlock_bh(&arm_state->susp_res_lock);
  26670. + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
  26671. + "blocked clients", __func__);
  26672. + if (wait_for_completion_interruptible_timeout(
  26673. + &arm_state->blocked_blocker, timeout_val)
  26674. + <= 0) {
  26675. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  26676. + "previously blocked clients failed" , __func__);
  26677. + status = VCHIQ_ERROR;
  26678. + write_lock_bh(&arm_state->susp_res_lock);
  26679. + goto out;
  26680. + }
  26681. + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
  26682. + "clients resumed", __func__);
  26683. + write_lock_bh(&arm_state->susp_res_lock);
  26684. + }
  26685. +
  26686. + /* We need to wait for resume to complete if it's in process */
  26687. + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
  26688. + arm_state->vc_resume_state > VC_RESUME_IDLE) {
  26689. + if (resume_count > 1) {
  26690. + status = VCHIQ_ERROR;
  26691. + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
  26692. + "many times for resume" , __func__);
  26693. + goto out;
  26694. + }
  26695. + write_unlock_bh(&arm_state->susp_res_lock);
  26696. + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
  26697. + __func__);
  26698. + if (wait_for_completion_interruptible_timeout(
  26699. + &arm_state->vc_resume_complete, timeout_val)
  26700. + <= 0) {
  26701. + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
  26702. + "resume failed (%s)", __func__,
  26703. + resume_state_names[arm_state->vc_resume_state +
  26704. + VC_RESUME_NUM_OFFSET]);
  26705. + status = VCHIQ_ERROR;
  26706. + write_lock_bh(&arm_state->susp_res_lock);
  26707. + goto out;
  26708. + }
  26709. + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
  26710. + write_lock_bh(&arm_state->susp_res_lock);
  26711. + resume_count++;
  26712. + }
  26713. + reinit_completion(&arm_state->resume_blocker);
  26714. + arm_state->resume_blocked = 1;
  26715. +
  26716. +out:
  26717. + return status;
  26718. +}
  26719. +
  26720. +static inline void
  26721. +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
  26722. +{
  26723. + complete_all(&arm_state->resume_blocker);
  26724. + arm_state->resume_blocked = 0;
  26725. +}
  26726. +
  26727. +/* Initiate suspend via slot handler. Should be called with the write lock
  26728. + * held */
  26729. +VCHIQ_STATUS_T
  26730. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
  26731. +{
  26732. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26733. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26734. +
  26735. + if (!arm_state)
  26736. + goto out;
  26737. +
  26738. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  26739. + status = VCHIQ_SUCCESS;
  26740. +
  26741. +
  26742. + switch (arm_state->vc_suspend_state) {
  26743. + case VC_SUSPEND_REQUESTED:
  26744. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
  26745. + "requested", __func__);
  26746. + break;
  26747. + case VC_SUSPEND_IN_PROGRESS:
  26748. + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
  26749. + "progress", __func__);
  26750. + break;
  26751. +
  26752. + default:
  26753. + /* We don't expect to be in other states, so log but continue
  26754. + * anyway */
  26755. + vchiq_log_error(vchiq_susp_log_level,
  26756. + "%s unexpected suspend state %s", __func__,
  26757. + suspend_state_names[arm_state->vc_suspend_state +
  26758. + VC_SUSPEND_NUM_OFFSET]);
  26759. + /* fall through */
  26760. + case VC_SUSPEND_REJECTED:
  26761. + case VC_SUSPEND_FAILED:
  26762. + /* Ensure any idle state actions have been run */
  26763. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  26764. + /* fall through */
  26765. + case VC_SUSPEND_IDLE:
  26766. + vchiq_log_info(vchiq_susp_log_level,
  26767. + "%s: suspending", __func__);
  26768. + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
  26769. + /* kick the slot handler thread to initiate suspend */
  26770. + request_poll(state, NULL, 0);
  26771. + break;
  26772. + }
  26773. +
  26774. +out:
  26775. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  26776. + return status;
  26777. +}
  26778. +
  26779. +void
  26780. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
  26781. +{
  26782. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26783. + int susp = 0;
  26784. +
  26785. + if (!arm_state)
  26786. + goto out;
  26787. +
  26788. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  26789. +
  26790. + write_lock_bh(&arm_state->susp_res_lock);
  26791. + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
  26792. + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
  26793. + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
  26794. + susp = 1;
  26795. + }
  26796. + write_unlock_bh(&arm_state->susp_res_lock);
  26797. +
  26798. + if (susp)
  26799. + vchiq_platform_suspend(state);
  26800. +
  26801. +out:
  26802. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  26803. + return;
  26804. +}
  26805. +
  26806. +
  26807. +static void
  26808. +output_timeout_error(VCHIQ_STATE_T *state)
  26809. +{
  26810. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26811. + char service_err[50] = "";
  26812. + int vc_use_count = arm_state->videocore_use_count;
  26813. + int active_services = state->unused_service;
  26814. + int i;
  26815. +
  26816. + if (!arm_state->videocore_use_count) {
  26817. + snprintf(service_err, 50, " Videocore usecount is 0");
  26818. + goto output_msg;
  26819. + }
  26820. + for (i = 0; i < active_services; i++) {
  26821. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  26822. + if (service_ptr && service_ptr->service_use_count &&
  26823. + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
  26824. + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
  26825. + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
  26826. + service_ptr->base.fourcc),
  26827. + service_ptr->client_id,
  26828. + service_ptr->service_use_count,
  26829. + service_ptr->service_use_count ==
  26830. + vc_use_count ? "" : " (+ more)");
  26831. + break;
  26832. + }
  26833. + }
  26834. +
  26835. +output_msg:
  26836. + vchiq_log_error(vchiq_susp_log_level,
  26837. + "timed out waiting for vc suspend (%d).%s",
  26838. + arm_state->autosuspend_override, service_err);
  26839. +
  26840. +}
  26841. +
  26842. +/* Try to get videocore into suspended state, regardless of autosuspend state.
  26843. +** We don't actually force suspend, since videocore may get into a bad state
  26844. +** if we force suspend at a bad time. Instead, we wait for autosuspend to
  26845. +** determine a good point to suspend. If this doesn't happen within 100ms we
  26846. +** report failure.
  26847. +**
  26848. +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
  26849. +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
  26850. +*/
  26851. +VCHIQ_STATUS_T
  26852. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
  26853. +{
  26854. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26855. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  26856. + long rc = 0;
  26857. + int repeat = -1;
  26858. +
  26859. + if (!arm_state)
  26860. + goto out;
  26861. +
  26862. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  26863. +
  26864. + write_lock_bh(&arm_state->susp_res_lock);
  26865. +
  26866. + status = block_resume(arm_state);
  26867. + if (status != VCHIQ_SUCCESS)
  26868. + goto unlock;
  26869. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  26870. + /* Already suspended - just block resume and exit */
  26871. + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
  26872. + __func__);
  26873. + status = VCHIQ_SUCCESS;
  26874. + goto unlock;
  26875. + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
  26876. + /* initiate suspend immediately in the case that we're waiting
  26877. + * for the timeout */
  26878. + stop_suspend_timer(arm_state);
  26879. + if (!vchiq_videocore_wanted(state)) {
  26880. + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
  26881. + "idle, initiating suspend", __func__);
  26882. + status = vchiq_arm_vcsuspend(state);
  26883. + } else if (arm_state->autosuspend_override <
  26884. + FORCE_SUSPEND_FAIL_MAX) {
  26885. + vchiq_log_info(vchiq_susp_log_level, "%s letting "
  26886. + "videocore go idle", __func__);
  26887. + status = VCHIQ_SUCCESS;
  26888. + } else {
  26889. + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
  26890. + "many times - attempting suspend", __func__);
  26891. + status = vchiq_arm_vcsuspend(state);
  26892. + }
  26893. + } else {
  26894. + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
  26895. + "in progress - wait for completion", __func__);
  26896. + status = VCHIQ_SUCCESS;
  26897. + }
  26898. +
  26899. + /* Wait for suspend to happen due to system idle (not forced..) */
  26900. + if (status != VCHIQ_SUCCESS)
  26901. + goto unblock_resume;
  26902. +
  26903. + do {
  26904. + write_unlock_bh(&arm_state->susp_res_lock);
  26905. +
  26906. + rc = wait_for_completion_interruptible_timeout(
  26907. + &arm_state->vc_suspend_complete,
  26908. + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
  26909. +
  26910. + write_lock_bh(&arm_state->susp_res_lock);
  26911. + if (rc < 0) {
  26912. + vchiq_log_warning(vchiq_susp_log_level, "%s "
  26913. + "interrupted waiting for suspend", __func__);
  26914. + status = VCHIQ_ERROR;
  26915. + goto unblock_resume;
  26916. + } else if (rc == 0) {
  26917. + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
  26918. + /* Repeat timeout once if in progress */
  26919. + if (repeat < 0) {
  26920. + repeat = 1;
  26921. + continue;
  26922. + }
  26923. + }
  26924. + arm_state->autosuspend_override++;
  26925. + output_timeout_error(state);
  26926. +
  26927. + status = VCHIQ_RETRY;
  26928. + goto unblock_resume;
  26929. + }
  26930. + } while (0 < (repeat--));
  26931. +
  26932. + /* Check and report state in case we need to abort ARM suspend */
  26933. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
  26934. + status = VCHIQ_RETRY;
  26935. + vchiq_log_error(vchiq_susp_log_level,
  26936. + "%s videocore suspend failed (state %s)", __func__,
  26937. + suspend_state_names[arm_state->vc_suspend_state +
  26938. + VC_SUSPEND_NUM_OFFSET]);
  26939. + /* Reset the state only if it's still in an error state.
  26940. + * Something could have already initiated another suspend. */
  26941. + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
  26942. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  26943. +
  26944. + goto unblock_resume;
  26945. + }
  26946. +
  26947. + /* successfully suspended - unlock and exit */
  26948. + goto unlock;
  26949. +
  26950. +unblock_resume:
  26951. + /* all error states need to unblock resume before exit */
  26952. + unblock_resume(arm_state);
  26953. +
  26954. +unlock:
  26955. + write_unlock_bh(&arm_state->susp_res_lock);
  26956. +
  26957. +out:
  26958. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
  26959. + return status;
  26960. +}
  26961. +
  26962. +void
  26963. +vchiq_check_suspend(VCHIQ_STATE_T *state)
  26964. +{
  26965. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26966. +
  26967. + if (!arm_state)
  26968. + goto out;
  26969. +
  26970. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  26971. +
  26972. + write_lock_bh(&arm_state->susp_res_lock);
  26973. + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
  26974. + arm_state->first_connect &&
  26975. + !vchiq_videocore_wanted(state)) {
  26976. + vchiq_arm_vcsuspend(state);
  26977. + }
  26978. + write_unlock_bh(&arm_state->susp_res_lock);
  26979. +
  26980. +out:
  26981. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  26982. + return;
  26983. +}
  26984. +
  26985. +
  26986. +int
  26987. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
  26988. +{
  26989. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  26990. + int resume = 0;
  26991. + int ret = -1;
  26992. +
  26993. + if (!arm_state)
  26994. + goto out;
  26995. +
  26996. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  26997. +
  26998. + write_lock_bh(&arm_state->susp_res_lock);
  26999. + unblock_resume(arm_state);
  27000. + resume = vchiq_check_resume(state);
  27001. + write_unlock_bh(&arm_state->susp_res_lock);
  27002. +
  27003. + if (resume) {
  27004. + if (wait_for_completion_interruptible(
  27005. + &arm_state->vc_resume_complete) < 0) {
  27006. + vchiq_log_error(vchiq_susp_log_level,
  27007. + "%s interrupted", __func__);
  27008. + /* failed, cannot accurately derive suspend
  27009. + * state, so exit early. */
  27010. + goto out;
  27011. + }
  27012. + }
  27013. +
  27014. + read_lock_bh(&arm_state->susp_res_lock);
  27015. + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
  27016. + vchiq_log_info(vchiq_susp_log_level,
  27017. + "%s: Videocore remains suspended", __func__);
  27018. + } else {
  27019. + vchiq_log_info(vchiq_susp_log_level,
  27020. + "%s: Videocore resumed", __func__);
  27021. + ret = 0;
  27022. + }
  27023. + read_unlock_bh(&arm_state->susp_res_lock);
  27024. +out:
  27025. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  27026. + return ret;
  27027. +}
  27028. +
  27029. +/* This function should be called with the write lock held */
  27030. +int
  27031. +vchiq_check_resume(VCHIQ_STATE_T *state)
  27032. +{
  27033. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27034. + int resume = 0;
  27035. +
  27036. + if (!arm_state)
  27037. + goto out;
  27038. +
  27039. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27040. +
  27041. + if (need_resume(state)) {
  27042. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  27043. + request_poll(state, NULL, 0);
  27044. + resume = 1;
  27045. + }
  27046. +
  27047. +out:
  27048. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  27049. + return resume;
  27050. +}
  27051. +
  27052. +void
  27053. +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
  27054. +{
  27055. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27056. + int res = 0;
  27057. +
  27058. + if (!arm_state)
  27059. + goto out;
  27060. +
  27061. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27062. +
  27063. + write_lock_bh(&arm_state->susp_res_lock);
  27064. + if (arm_state->wake_address == 0) {
  27065. + vchiq_log_info(vchiq_susp_log_level,
  27066. + "%s: already awake", __func__);
  27067. + goto unlock;
  27068. + }
  27069. + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
  27070. + vchiq_log_info(vchiq_susp_log_level,
  27071. + "%s: already resuming", __func__);
  27072. + goto unlock;
  27073. + }
  27074. +
  27075. + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
  27076. + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
  27077. + res = 1;
  27078. + } else
  27079. + vchiq_log_trace(vchiq_susp_log_level,
  27080. + "%s: not resuming (resume state %s)", __func__,
  27081. + resume_state_names[arm_state->vc_resume_state +
  27082. + VC_RESUME_NUM_OFFSET]);
  27083. +
  27084. +unlock:
  27085. + write_unlock_bh(&arm_state->susp_res_lock);
  27086. +
  27087. + if (res)
  27088. + vchiq_platform_resume(state);
  27089. +
  27090. +out:
  27091. + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
  27092. + return;
  27093. +
  27094. +}
  27095. +
  27096. +
  27097. +
  27098. +VCHIQ_STATUS_T
  27099. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  27100. + enum USE_TYPE_E use_type)
  27101. +{
  27102. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27103. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  27104. + char entity[16];
  27105. + int *entity_uc;
  27106. + int local_uc, local_entity_uc;
  27107. +
  27108. + if (!arm_state)
  27109. + goto out;
  27110. +
  27111. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27112. +
  27113. + if (use_type == USE_TYPE_VCHIQ) {
  27114. + sprintf(entity, "VCHIQ: ");
  27115. + entity_uc = &arm_state->peer_use_count;
  27116. + } else if (service) {
  27117. + sprintf(entity, "%c%c%c%c:%03d",
  27118. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  27119. + service->client_id);
  27120. + entity_uc = &service->service_use_count;
  27121. + } else {
  27122. + vchiq_log_error(vchiq_susp_log_level, "%s null service "
  27123. + "ptr", __func__);
  27124. + ret = VCHIQ_ERROR;
  27125. + goto out;
  27126. + }
  27127. +
  27128. + write_lock_bh(&arm_state->susp_res_lock);
  27129. + while (arm_state->resume_blocked) {
  27130. + /* If we call 'use' while force suspend is waiting for suspend,
  27131. + * then we're about to block the thread which the force is
  27132. + * waiting to complete, so we're bound to just time out. In this
  27133. + * case, set the suspend state such that the wait will be
  27134. + * canceled, so we can complete as quickly as possible. */
  27135. + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
  27136. + VC_SUSPEND_IDLE) {
  27137. + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
  27138. + break;
  27139. + }
  27140. + /* If suspend is already in progress then we need to block */
  27141. + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
  27142. + /* Indicate that there are threads waiting on the resume
  27143. + * blocker. These need to be allowed to complete before
  27144. + * a _second_ call to force suspend can complete,
  27145. + * otherwise low priority threads might never actually
  27146. + * continue */
  27147. + arm_state->blocked_count++;
  27148. + write_unlock_bh(&arm_state->susp_res_lock);
  27149. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  27150. + "blocked - waiting...", __func__, entity);
  27151. + if (wait_for_completion_killable(
  27152. + &arm_state->resume_blocker) != 0) {
  27153. + vchiq_log_error(vchiq_susp_log_level, "%s %s "
  27154. + "wait for resume blocker interrupted",
  27155. + __func__, entity);
  27156. + ret = VCHIQ_ERROR;
  27157. + write_lock_bh(&arm_state->susp_res_lock);
  27158. + arm_state->blocked_count--;
  27159. + write_unlock_bh(&arm_state->susp_res_lock);
  27160. + goto out;
  27161. + }
  27162. + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
  27163. + "unblocked", __func__, entity);
  27164. + write_lock_bh(&arm_state->susp_res_lock);
  27165. + if (--arm_state->blocked_count == 0)
  27166. + complete_all(&arm_state->blocked_blocker);
  27167. + }
  27168. + }
  27169. +
  27170. + stop_suspend_timer(arm_state);
  27171. +
  27172. + local_uc = ++arm_state->videocore_use_count;
  27173. + local_entity_uc = ++(*entity_uc);
  27174. +
  27175. + /* If there's a pending request which hasn't yet been serviced then
  27176. + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
  27177. + * vc_resume_complete will block until we either resume or fail to
  27178. + * suspend */
  27179. + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
  27180. + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
  27181. +
  27182. + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
  27183. + set_resume_state(arm_state, VC_RESUME_REQUESTED);
  27184. + vchiq_log_info(vchiq_susp_log_level,
  27185. + "%s %s count %d, state count %d",
  27186. + __func__, entity, local_entity_uc, local_uc);
  27187. + request_poll(state, NULL, 0);
  27188. + } else
  27189. + vchiq_log_trace(vchiq_susp_log_level,
  27190. + "%s %s count %d, state count %d",
  27191. + __func__, entity, *entity_uc, local_uc);
  27192. +
  27193. +
  27194. + write_unlock_bh(&arm_state->susp_res_lock);
  27195. +
  27196. + /* Completion is in a done state when we're not suspended, so this won't
  27197. + * block for the non-suspended case. */
  27198. + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
  27199. + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
  27200. + __func__, entity);
  27201. + if (wait_for_completion_killable(
  27202. + &arm_state->vc_resume_complete) != 0) {
  27203. + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
  27204. + "resume interrupted", __func__, entity);
  27205. + ret = VCHIQ_ERROR;
  27206. + goto out;
  27207. + }
  27208. + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
  27209. + entity);
  27210. + }
  27211. +
  27212. + if (ret == VCHIQ_SUCCESS) {
  27213. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  27214. + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
  27215. + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
  27216. + /* Send the use notify to videocore */
  27217. + status = vchiq_send_remote_use_active(state);
  27218. + if (status == VCHIQ_SUCCESS)
  27219. + ack_cnt--;
  27220. + else
  27221. + atomic_add(ack_cnt,
  27222. + &arm_state->ka_use_ack_count);
  27223. + }
  27224. + }
  27225. +
  27226. +out:
  27227. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  27228. + return ret;
  27229. +}
  27230. +
  27231. +VCHIQ_STATUS_T
  27232. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
  27233. +{
  27234. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27235. + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
  27236. + char entity[16];
  27237. + int *entity_uc;
  27238. + int local_uc, local_entity_uc;
  27239. +
  27240. + if (!arm_state)
  27241. + goto out;
  27242. +
  27243. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27244. +
  27245. + if (service) {
  27246. + sprintf(entity, "%c%c%c%c:%03d",
  27247. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  27248. + service->client_id);
  27249. + entity_uc = &service->service_use_count;
  27250. + } else {
  27251. + sprintf(entity, "PEER: ");
  27252. + entity_uc = &arm_state->peer_use_count;
  27253. + }
  27254. +
  27255. + write_lock_bh(&arm_state->susp_res_lock);
  27256. + if (!arm_state->videocore_use_count || !(*entity_uc)) {
  27257. + /* Don't use BUG_ON - don't allow user thread to crash kernel */
  27258. + WARN_ON(!arm_state->videocore_use_count);
  27259. + WARN_ON(!(*entity_uc));
  27260. + ret = VCHIQ_ERROR;
  27261. + goto unlock;
  27262. + }
  27263. + local_uc = --arm_state->videocore_use_count;
  27264. + local_entity_uc = --(*entity_uc);
  27265. +
  27266. + if (!vchiq_videocore_wanted(state)) {
  27267. + if (vchiq_platform_use_suspend_timer() &&
  27268. + !arm_state->resume_blocked) {
  27269. + /* Only use the timer if we're not trying to force
  27270. + * suspend (=> resume_blocked) */
  27271. + start_suspend_timer(arm_state);
  27272. + } else {
  27273. + vchiq_log_info(vchiq_susp_log_level,
  27274. + "%s %s count %d, state count %d - suspending",
  27275. + __func__, entity, *entity_uc,
  27276. + arm_state->videocore_use_count);
  27277. + vchiq_arm_vcsuspend(state);
  27278. + }
  27279. + } else
  27280. + vchiq_log_trace(vchiq_susp_log_level,
  27281. + "%s %s count %d, state count %d",
  27282. + __func__, entity, *entity_uc,
  27283. + arm_state->videocore_use_count);
  27284. +
  27285. +unlock:
  27286. + write_unlock_bh(&arm_state->susp_res_lock);
  27287. +
  27288. +out:
  27289. + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
  27290. + return ret;
  27291. +}
  27292. +
  27293. +void
  27294. +vchiq_on_remote_use(VCHIQ_STATE_T *state)
  27295. +{
  27296. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27297. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27298. + atomic_inc(&arm_state->ka_use_count);
  27299. + complete(&arm_state->ka_evt);
  27300. +}
  27301. +
  27302. +void
  27303. +vchiq_on_remote_release(VCHIQ_STATE_T *state)
  27304. +{
  27305. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27306. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27307. + atomic_inc(&arm_state->ka_release_count);
  27308. + complete(&arm_state->ka_evt);
  27309. +}
  27310. +
  27311. +VCHIQ_STATUS_T
  27312. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
  27313. +{
  27314. + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
  27315. +}
  27316. +
  27317. +VCHIQ_STATUS_T
  27318. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
  27319. +{
  27320. + return vchiq_release_internal(service->state, service);
  27321. +}
  27322. +
  27323. +static void suspend_timer_callback(unsigned long context)
  27324. +{
  27325. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
  27326. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27327. + if (!arm_state)
  27328. + goto out;
  27329. + vchiq_log_info(vchiq_susp_log_level,
  27330. + "%s - suspend timer expired - check suspend", __func__);
  27331. + vchiq_check_suspend(state);
  27332. +out:
  27333. + return;
  27334. +}
  27335. +
  27336. +VCHIQ_STATUS_T
  27337. +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
  27338. +{
  27339. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  27340. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27341. + if (service) {
  27342. + ret = vchiq_use_internal(service->state, service,
  27343. + USE_TYPE_SERVICE_NO_RESUME);
  27344. + unlock_service(service);
  27345. + }
  27346. + return ret;
  27347. +}
  27348. +
  27349. +VCHIQ_STATUS_T
  27350. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
  27351. +{
  27352. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  27353. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27354. + if (service) {
  27355. + ret = vchiq_use_internal(service->state, service,
  27356. + USE_TYPE_SERVICE);
  27357. + unlock_service(service);
  27358. + }
  27359. + return ret;
  27360. +}
  27361. +
  27362. +VCHIQ_STATUS_T
  27363. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
  27364. +{
  27365. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  27366. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  27367. + if (service) {
  27368. + ret = vchiq_release_internal(service->state, service);
  27369. + unlock_service(service);
  27370. + }
  27371. + return ret;
  27372. +}
  27373. +
  27374. +void
  27375. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
  27376. +{
  27377. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27378. + int i, j = 0;
  27379. + /* Only dump 64 services */
  27380. + static const int local_max_services = 64;
  27381. + /* If there's more than 64 services, only dump ones with
  27382. + * non-zero counts */
  27383. + int only_nonzero = 0;
  27384. + static const char *nz = "<-- preventing suspend";
  27385. +
  27386. + enum vc_suspend_status vc_suspend_state;
  27387. + enum vc_resume_status vc_resume_state;
  27388. + int peer_count;
  27389. + int vc_use_count;
  27390. + int active_services;
  27391. + struct service_data_struct {
  27392. + int fourcc;
  27393. + int clientid;
  27394. + int use_count;
  27395. + } service_data[local_max_services];
  27396. +
  27397. + if (!arm_state)
  27398. + return;
  27399. +
  27400. + read_lock_bh(&arm_state->susp_res_lock);
  27401. + vc_suspend_state = arm_state->vc_suspend_state;
  27402. + vc_resume_state = arm_state->vc_resume_state;
  27403. + peer_count = arm_state->peer_use_count;
  27404. + vc_use_count = arm_state->videocore_use_count;
  27405. + active_services = state->unused_service;
  27406. + if (active_services > local_max_services)
  27407. + only_nonzero = 1;
  27408. +
  27409. + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
  27410. + VCHIQ_SERVICE_T *service_ptr = state->services[i];
  27411. + if (!service_ptr)
  27412. + continue;
  27413. +
  27414. + if (only_nonzero && !service_ptr->service_use_count)
  27415. + continue;
  27416. +
  27417. + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
  27418. + service_data[j].fourcc = service_ptr->base.fourcc;
  27419. + service_data[j].clientid = service_ptr->client_id;
  27420. + service_data[j++].use_count = service_ptr->
  27421. + service_use_count;
  27422. + }
  27423. + }
  27424. +
  27425. + read_unlock_bh(&arm_state->susp_res_lock);
  27426. +
  27427. + vchiq_log_warning(vchiq_susp_log_level,
  27428. + "-- Videcore suspend state: %s --",
  27429. + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
  27430. + vchiq_log_warning(vchiq_susp_log_level,
  27431. + "-- Videcore resume state: %s --",
  27432. + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
  27433. +
  27434. + if (only_nonzero)
  27435. + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
  27436. + "services (%d). Only dumping up to first %d services "
  27437. + "with non-zero use-count", active_services,
  27438. + local_max_services);
  27439. +
  27440. + for (i = 0; i < j; i++) {
  27441. + vchiq_log_warning(vchiq_susp_log_level,
  27442. + "----- %c%c%c%c:%d service count %d %s",
  27443. + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
  27444. + service_data[i].clientid,
  27445. + service_data[i].use_count,
  27446. + service_data[i].use_count ? nz : "");
  27447. + }
  27448. + vchiq_log_warning(vchiq_susp_log_level,
  27449. + "----- VCHIQ use count count %d", peer_count);
  27450. + vchiq_log_warning(vchiq_susp_log_level,
  27451. + "--- Overall vchiq instance use count %d", vc_use_count);
  27452. +
  27453. + vchiq_dump_platform_use_state(state);
  27454. +}
  27455. +
  27456. +VCHIQ_STATUS_T
  27457. +vchiq_check_service(VCHIQ_SERVICE_T *service)
  27458. +{
  27459. + VCHIQ_ARM_STATE_T *arm_state;
  27460. + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
  27461. +
  27462. + if (!service || !service->state)
  27463. + goto out;
  27464. +
  27465. + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
  27466. +
  27467. + arm_state = vchiq_platform_get_arm_state(service->state);
  27468. +
  27469. + read_lock_bh(&arm_state->susp_res_lock);
  27470. + if (service->service_use_count)
  27471. + ret = VCHIQ_SUCCESS;
  27472. + read_unlock_bh(&arm_state->susp_res_lock);
  27473. +
  27474. + if (ret == VCHIQ_ERROR) {
  27475. + vchiq_log_error(vchiq_susp_log_level,
  27476. + "%s ERROR - %c%c%c%c:%d service count %d, "
  27477. + "state count %d, videocore suspend state %s", __func__,
  27478. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  27479. + service->client_id, service->service_use_count,
  27480. + arm_state->videocore_use_count,
  27481. + suspend_state_names[arm_state->vc_suspend_state +
  27482. + VC_SUSPEND_NUM_OFFSET]);
  27483. + vchiq_dump_service_use_state(service->state);
  27484. + }
  27485. +out:
  27486. + return ret;
  27487. +}
  27488. +
  27489. +/* stub functions */
  27490. +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
  27491. +{
  27492. + (void)state;
  27493. +}
  27494. +
  27495. +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  27496. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
  27497. +{
  27498. + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
  27499. + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
  27500. + get_conn_state_name(oldstate), get_conn_state_name(newstate));
  27501. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
  27502. + write_lock_bh(&arm_state->susp_res_lock);
  27503. + if (!arm_state->first_connect) {
  27504. + char threadname[10];
  27505. + arm_state->first_connect = 1;
  27506. + write_unlock_bh(&arm_state->susp_res_lock);
  27507. + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
  27508. + state->id);
  27509. + arm_state->ka_thread = kthread_create(
  27510. + &vchiq_keepalive_thread_func,
  27511. + (void *)state,
  27512. + threadname);
  27513. + if (arm_state->ka_thread == NULL) {
  27514. + vchiq_log_error(vchiq_susp_log_level,
  27515. + "vchiq: FATAL: couldn't create thread %s",
  27516. + threadname);
  27517. + } else {
  27518. + wake_up_process(arm_state->ka_thread);
  27519. + }
  27520. + } else
  27521. + write_unlock_bh(&arm_state->susp_res_lock);
  27522. + }
  27523. +}
  27524. +
  27525. +
  27526. +/****************************************************************************
  27527. +*
  27528. +* vchiq_init - called when the module is loaded.
  27529. +*
  27530. +***************************************************************************/
  27531. +
  27532. +static int __init
  27533. +vchiq_init(void)
  27534. +{
  27535. + int err;
  27536. + void *ptr_err;
  27537. +
  27538. + /* create proc entries */
  27539. + err = vchiq_proc_init();
  27540. + if (err != 0)
  27541. + goto failed_proc_init;
  27542. +
  27543. + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
  27544. + if (err != 0) {
  27545. + vchiq_log_error(vchiq_arm_log_level,
  27546. + "Unable to allocate device number");
  27547. + goto failed_alloc_chrdev;
  27548. + }
  27549. + cdev_init(&vchiq_cdev, &vchiq_fops);
  27550. + vchiq_cdev.owner = THIS_MODULE;
  27551. + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
  27552. + if (err != 0) {
  27553. + vchiq_log_error(vchiq_arm_log_level,
  27554. + "Unable to register device");
  27555. + goto failed_cdev_add;
  27556. + }
  27557. +
  27558. + /* create sysfs entries */
  27559. + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
  27560. + ptr_err = vchiq_class;
  27561. + if (IS_ERR(ptr_err))
  27562. + goto failed_class_create;
  27563. +
  27564. + vchiq_dev = device_create(vchiq_class, NULL,
  27565. + vchiq_devid, NULL, "vchiq");
  27566. + ptr_err = vchiq_dev;
  27567. + if (IS_ERR(ptr_err))
  27568. + goto failed_device_create;
  27569. +
  27570. + err = vchiq_platform_init(&g_state);
  27571. + if (err != 0)
  27572. + goto failed_platform_init;
  27573. +
  27574. + vchiq_log_info(vchiq_arm_log_level,
  27575. + "vchiq: initialised - version %d (min %d), device %d.%d",
  27576. + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
  27577. + MAJOR(vchiq_devid), MINOR(vchiq_devid));
  27578. +
  27579. + return 0;
  27580. +
  27581. +failed_platform_init:
  27582. + device_destroy(vchiq_class, vchiq_devid);
  27583. +failed_device_create:
  27584. + class_destroy(vchiq_class);
  27585. +failed_class_create:
  27586. + cdev_del(&vchiq_cdev);
  27587. + err = PTR_ERR(ptr_err);
  27588. +failed_cdev_add:
  27589. + unregister_chrdev_region(vchiq_devid, 1);
  27590. +failed_alloc_chrdev:
  27591. + vchiq_proc_deinit();
  27592. +failed_proc_init:
  27593. + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
  27594. + return err;
  27595. +}
  27596. +
  27597. +static int vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
  27598. +{
  27599. + VCHIQ_SERVICE_T *service;
  27600. + int use_count = 0, i;
  27601. + i = 0;
  27602. + while ((service = next_service_by_instance(instance->state,
  27603. + instance, &i)) != NULL) {
  27604. + use_count += service->service_use_count;
  27605. + unlock_service(service);
  27606. + }
  27607. + return use_count;
  27608. +}
  27609. +
  27610. +/* read the per-process use-count */
  27611. +static int proc_read_use_count(char *page, char **start,
  27612. + off_t off, int count,
  27613. + int *eof, void *data)
  27614. +{
  27615. + VCHIQ_INSTANCE_T instance = data;
  27616. + int len, use_count;
  27617. +
  27618. + use_count = vchiq_instance_get_use_count(instance);
  27619. + len = snprintf(page+off, count, "%d\n", use_count);
  27620. +
  27621. + return len;
  27622. +}
  27623. +
  27624. +/* add an instance (process) to the proc entries */
  27625. +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance)
  27626. +{
  27627. +#if 1
  27628. + return 0;
  27629. +#else
  27630. + char pidstr[32];
  27631. + struct proc_dir_entry *top, *use_count;
  27632. + struct proc_dir_entry *clients = vchiq_clients_top();
  27633. + int pid = instance->pid;
  27634. +
  27635. + snprintf(pidstr, sizeof(pidstr), "%d", pid);
  27636. + top = proc_mkdir(pidstr, clients);
  27637. + if (!top)
  27638. + goto fail_top;
  27639. +
  27640. + use_count = create_proc_read_entry("use_count",
  27641. + 0444, top,
  27642. + proc_read_use_count,
  27643. + instance);
  27644. + if (!use_count)
  27645. + goto fail_use_count;
  27646. +
  27647. + instance->proc_entry = top;
  27648. +
  27649. + return 0;
  27650. +
  27651. +fail_use_count:
  27652. + remove_proc_entry(top->name, clients);
  27653. +fail_top:
  27654. + return -ENOMEM;
  27655. +#endif
  27656. +}
  27657. +
  27658. +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance)
  27659. +{
  27660. +#if 0
  27661. + struct proc_dir_entry *clients = vchiq_clients_top();
  27662. + remove_proc_entry("use_count", instance->proc_entry);
  27663. + remove_proc_entry(instance->proc_entry->name, clients);
  27664. +#endif
  27665. +}
  27666. +
  27667. +/****************************************************************************
  27668. +*
  27669. +* vchiq_exit - called when the module is unloaded.
  27670. +*
  27671. +***************************************************************************/
  27672. +
  27673. +static void __exit
  27674. +vchiq_exit(void)
  27675. +{
  27676. + vchiq_platform_exit(&g_state);
  27677. + device_destroy(vchiq_class, vchiq_devid);
  27678. + class_destroy(vchiq_class);
  27679. + cdev_del(&vchiq_cdev);
  27680. + unregister_chrdev_region(vchiq_devid, 1);
  27681. +}
  27682. +
  27683. +module_init(vchiq_init);
  27684. +module_exit(vchiq_exit);
  27685. +MODULE_LICENSE("GPL");
  27686. +MODULE_AUTHOR("Broadcom Corporation");
  27687. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
  27688. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 1970-01-01 01:00:00.000000000 +0100
  27689. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h 2014-04-13 17:32:57.000000000 +0200
  27690. @@ -0,0 +1,212 @@
  27691. +/**
  27692. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27693. + *
  27694. + * Redistribution and use in source and binary forms, with or without
  27695. + * modification, are permitted provided that the following conditions
  27696. + * are met:
  27697. + * 1. Redistributions of source code must retain the above copyright
  27698. + * notice, this list of conditions, and the following disclaimer,
  27699. + * without modification.
  27700. + * 2. Redistributions in binary form must reproduce the above copyright
  27701. + * notice, this list of conditions and the following disclaimer in the
  27702. + * documentation and/or other materials provided with the distribution.
  27703. + * 3. The names of the above-listed copyright holders may not be used
  27704. + * to endorse or promote products derived from this software without
  27705. + * specific prior written permission.
  27706. + *
  27707. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27708. + * GNU General Public License ("GPL") version 2, as published by the Free
  27709. + * Software Foundation.
  27710. + *
  27711. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27712. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27713. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27714. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27715. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27716. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27717. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27718. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27719. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27720. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27721. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27722. + */
  27723. +
  27724. +#ifndef VCHIQ_ARM_H
  27725. +#define VCHIQ_ARM_H
  27726. +
  27727. +#include <linux/mutex.h>
  27728. +#include <linux/semaphore.h>
  27729. +#include <linux/atomic.h>
  27730. +#include "vchiq_core.h"
  27731. +
  27732. +
  27733. +enum vc_suspend_status {
  27734. + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
  27735. + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
  27736. + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
  27737. + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
  27738. + VC_SUSPEND_REQUESTED, /* User has requested suspend */
  27739. + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
  27740. + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
  27741. +};
  27742. +
  27743. +enum vc_resume_status {
  27744. + VC_RESUME_FAILED = -1, /* Videocore resume failed */
  27745. + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
  27746. + VC_RESUME_REQUESTED, /* User has requested resume */
  27747. + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
  27748. + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
  27749. +};
  27750. +
  27751. +
  27752. +enum USE_TYPE_E {
  27753. + USE_TYPE_SERVICE,
  27754. + USE_TYPE_SERVICE_NO_RESUME,
  27755. + USE_TYPE_VCHIQ
  27756. +};
  27757. +
  27758. +
  27759. +
  27760. +typedef struct vchiq_arm_state_struct {
  27761. + /* Keepalive-related data */
  27762. + struct task_struct *ka_thread;
  27763. + struct completion ka_evt;
  27764. + atomic_t ka_use_count;
  27765. + atomic_t ka_use_ack_count;
  27766. + atomic_t ka_release_count;
  27767. +
  27768. + struct completion vc_suspend_complete;
  27769. + struct completion vc_resume_complete;
  27770. +
  27771. + rwlock_t susp_res_lock;
  27772. + enum vc_suspend_status vc_suspend_state;
  27773. + enum vc_resume_status vc_resume_state;
  27774. +
  27775. + unsigned int wake_address;
  27776. +
  27777. + struct timer_list suspend_timer;
  27778. + int suspend_timer_timeout;
  27779. + int suspend_timer_running;
  27780. +
  27781. + /* Global use count for videocore.
  27782. + ** This is equal to the sum of the use counts for all services. When
  27783. + ** this hits zero the videocore suspend procedure will be initiated.
  27784. + */
  27785. + int videocore_use_count;
  27786. +
  27787. + /* Use count to track requests from videocore peer.
  27788. + ** This use count is not associated with a service, so needs to be
  27789. + ** tracked separately with the state.
  27790. + */
  27791. + int peer_use_count;
  27792. +
  27793. + /* Flag to indicate whether resume is blocked. This happens when the
  27794. + ** ARM is suspending
  27795. + */
  27796. + struct completion resume_blocker;
  27797. + int resume_blocked;
  27798. + struct completion blocked_blocker;
  27799. + int blocked_count;
  27800. +
  27801. + int autosuspend_override;
  27802. +
  27803. + /* Flag to indicate that the first vchiq connect has made it through.
  27804. + ** This means that both sides should be fully ready, and we should
  27805. + ** be able to suspend after this point.
  27806. + */
  27807. + int first_connect;
  27808. +
  27809. + unsigned long long suspend_start_time;
  27810. + unsigned long long sleep_start_time;
  27811. + unsigned long long resume_start_time;
  27812. + unsigned long long last_wake_time;
  27813. +
  27814. +} VCHIQ_ARM_STATE_T;
  27815. +
  27816. +extern int vchiq_arm_log_level;
  27817. +extern int vchiq_susp_log_level;
  27818. +
  27819. +extern int __init
  27820. +vchiq_platform_init(VCHIQ_STATE_T *state);
  27821. +
  27822. +extern void __exit
  27823. +vchiq_platform_exit(VCHIQ_STATE_T *state);
  27824. +
  27825. +extern VCHIQ_STATE_T *
  27826. +vchiq_get_state(void);
  27827. +
  27828. +extern VCHIQ_STATUS_T
  27829. +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
  27830. +
  27831. +extern VCHIQ_STATUS_T
  27832. +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
  27833. +
  27834. +extern int
  27835. +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
  27836. +
  27837. +extern VCHIQ_STATUS_T
  27838. +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
  27839. +
  27840. +extern VCHIQ_STATUS_T
  27841. +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
  27842. +
  27843. +extern int
  27844. +vchiq_check_resume(VCHIQ_STATE_T *state);
  27845. +
  27846. +extern void
  27847. +vchiq_check_suspend(VCHIQ_STATE_T *state);
  27848. +
  27849. +extern VCHIQ_STATUS_T
  27850. +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
  27851. +
  27852. +extern VCHIQ_STATUS_T
  27853. +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
  27854. +
  27855. +extern VCHIQ_STATUS_T
  27856. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  27857. +
  27858. +extern VCHIQ_STATUS_T
  27859. +vchiq_platform_suspend(VCHIQ_STATE_T *state);
  27860. +
  27861. +extern int
  27862. +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
  27863. +
  27864. +extern int
  27865. +vchiq_platform_use_suspend_timer(void);
  27866. +
  27867. +extern void
  27868. +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
  27869. +
  27870. +extern void
  27871. +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
  27872. +
  27873. +extern VCHIQ_ARM_STATE_T*
  27874. +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
  27875. +
  27876. +extern int
  27877. +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
  27878. +
  27879. +extern VCHIQ_STATUS_T
  27880. +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  27881. + enum USE_TYPE_E use_type);
  27882. +extern VCHIQ_STATUS_T
  27883. +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
  27884. +
  27885. +void
  27886. +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
  27887. + enum vc_suspend_status new_state);
  27888. +
  27889. +void
  27890. +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
  27891. + enum vc_resume_status new_state);
  27892. +
  27893. +void
  27894. +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
  27895. +
  27896. +extern int vchiq_proc_init(void);
  27897. +extern void vchiq_proc_deinit(void);
  27898. +extern struct proc_dir_entry *vchiq_proc_top(void);
  27899. +extern struct proc_dir_entry *vchiq_clients_top(void);
  27900. +
  27901. +
  27902. +#endif /* VCHIQ_ARM_H */
  27903. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
  27904. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 1970-01-01 01:00:00.000000000 +0100
  27905. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h 2014-04-13 17:32:57.000000000 +0200
  27906. @@ -0,0 +1,37 @@
  27907. +/**
  27908. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  27909. + *
  27910. + * Redistribution and use in source and binary forms, with or without
  27911. + * modification, are permitted provided that the following conditions
  27912. + * are met:
  27913. + * 1. Redistributions of source code must retain the above copyright
  27914. + * notice, this list of conditions, and the following disclaimer,
  27915. + * without modification.
  27916. + * 2. Redistributions in binary form must reproduce the above copyright
  27917. + * notice, this list of conditions and the following disclaimer in the
  27918. + * documentation and/or other materials provided with the distribution.
  27919. + * 3. The names of the above-listed copyright holders may not be used
  27920. + * to endorse or promote products derived from this software without
  27921. + * specific prior written permission.
  27922. + *
  27923. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27924. + * GNU General Public License ("GPL") version 2, as published by the Free
  27925. + * Software Foundation.
  27926. + *
  27927. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27928. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27929. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27930. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27931. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27932. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27933. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27934. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27935. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27936. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27937. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27938. + */
  27939. +
  27940. +const char *vchiq_get_build_hostname(void);
  27941. +const char *vchiq_get_build_version(void);
  27942. +const char *vchiq_get_build_time(void);
  27943. +const char *vchiq_get_build_date(void);
  27944. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
  27945. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 1970-01-01 01:00:00.000000000 +0100
  27946. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h 2014-09-14 19:03:25.000000000 +0200
  27947. @@ -0,0 +1,66 @@
  27948. +/**
  27949. + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
  27950. + *
  27951. + * Redistribution and use in source and binary forms, with or without
  27952. + * modification, are permitted provided that the following conditions
  27953. + * are met:
  27954. + * 1. Redistributions of source code must retain the above copyright
  27955. + * notice, this list of conditions, and the following disclaimer,
  27956. + * without modification.
  27957. + * 2. Redistributions in binary form must reproduce the above copyright
  27958. + * notice, this list of conditions and the following disclaimer in the
  27959. + * documentation and/or other materials provided with the distribution.
  27960. + * 3. The names of the above-listed copyright holders may not be used
  27961. + * to endorse or promote products derived from this software without
  27962. + * specific prior written permission.
  27963. + *
  27964. + * ALTERNATIVELY, this software may be distributed under the terms of the
  27965. + * GNU General Public License ("GPL") version 2, as published by the Free
  27966. + * Software Foundation.
  27967. + *
  27968. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  27969. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27970. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  27971. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27972. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  27973. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27974. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27975. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27976. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27977. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27978. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27979. + */
  27980. +
  27981. +#ifndef VCHIQ_CFG_H
  27982. +#define VCHIQ_CFG_H
  27983. +
  27984. +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
  27985. +/* The version of VCHIQ - change with any non-trivial change */
  27986. +#define VCHIQ_VERSION 7
  27987. +/* The minimum compatible version - update to match VCHIQ_VERSION with any
  27988. +** incompatible change */
  27989. +#define VCHIQ_VERSION_MIN 3
  27990. +
  27991. +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
  27992. +#define VCHIQ_VERSION_LIB_VERSION 7
  27993. +
  27994. +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
  27995. +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
  27996. +
  27997. +#define VCHIQ_MAX_STATES 1
  27998. +#define VCHIQ_MAX_SERVICES 4096
  27999. +#define VCHIQ_MAX_SLOTS 128
  28000. +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
  28001. +
  28002. +#define VCHIQ_NUM_CURRENT_BULKS 32
  28003. +#define VCHIQ_NUM_SERVICE_BULKS 4
  28004. +
  28005. +#ifndef VCHIQ_ENABLE_DEBUG
  28006. +#define VCHIQ_ENABLE_DEBUG 1
  28007. +#endif
  28008. +
  28009. +#ifndef VCHIQ_ENABLE_STATS
  28010. +#define VCHIQ_ENABLE_STATS 1
  28011. +#endif
  28012. +
  28013. +#endif /* VCHIQ_CFG_H */
  28014. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
  28015. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 1970-01-01 01:00:00.000000000 +0100
  28016. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c 2014-09-14 19:03:25.000000000 +0200
  28017. @@ -0,0 +1,120 @@
  28018. +/**
  28019. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28020. + *
  28021. + * Redistribution and use in source and binary forms, with or without
  28022. + * modification, are permitted provided that the following conditions
  28023. + * are met:
  28024. + * 1. Redistributions of source code must retain the above copyright
  28025. + * notice, this list of conditions, and the following disclaimer,
  28026. + * without modification.
  28027. + * 2. Redistributions in binary form must reproduce the above copyright
  28028. + * notice, this list of conditions and the following disclaimer in the
  28029. + * documentation and/or other materials provided with the distribution.
  28030. + * 3. The names of the above-listed copyright holders may not be used
  28031. + * to endorse or promote products derived from this software without
  28032. + * specific prior written permission.
  28033. + *
  28034. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28035. + * GNU General Public License ("GPL") version 2, as published by the Free
  28036. + * Software Foundation.
  28037. + *
  28038. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28039. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28040. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28041. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28042. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28043. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28044. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28045. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28046. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28047. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28048. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28049. + */
  28050. +
  28051. +#include "vchiq_connected.h"
  28052. +#include "vchiq_core.h"
  28053. +#include "vchiq_killable.h"
  28054. +#include <linux/module.h>
  28055. +#include <linux/mutex.h>
  28056. +
  28057. +#define MAX_CALLBACKS 10
  28058. +
  28059. +static int g_connected;
  28060. +static int g_num_deferred_callbacks;
  28061. +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
  28062. +static int g_once_init;
  28063. +static struct mutex g_connected_mutex;
  28064. +
  28065. +/****************************************************************************
  28066. +*
  28067. +* Function to initialize our lock.
  28068. +*
  28069. +***************************************************************************/
  28070. +
  28071. +static void connected_init(void)
  28072. +{
  28073. + if (!g_once_init) {
  28074. + mutex_init(&g_connected_mutex);
  28075. + g_once_init = 1;
  28076. + }
  28077. +}
  28078. +
  28079. +/****************************************************************************
  28080. +*
  28081. +* This function is used to defer initialization until the vchiq stack is
  28082. +* initialized. If the stack is already initialized, then the callback will
  28083. +* be made immediately, otherwise it will be deferred until
  28084. +* vchiq_call_connected_callbacks is called.
  28085. +*
  28086. +***************************************************************************/
  28087. +
  28088. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
  28089. +{
  28090. + connected_init();
  28091. +
  28092. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  28093. + return;
  28094. +
  28095. + if (g_connected)
  28096. + /* We're already connected. Call the callback immediately. */
  28097. +
  28098. + callback();
  28099. + else {
  28100. + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
  28101. + vchiq_log_error(vchiq_core_log_level,
  28102. + "There already %d callback registered - "
  28103. + "please increase MAX_CALLBACKS",
  28104. + g_num_deferred_callbacks);
  28105. + else {
  28106. + g_deferred_callback[g_num_deferred_callbacks] =
  28107. + callback;
  28108. + g_num_deferred_callbacks++;
  28109. + }
  28110. + }
  28111. + mutex_unlock(&g_connected_mutex);
  28112. +}
  28113. +
  28114. +/****************************************************************************
  28115. +*
  28116. +* This function is called by the vchiq stack once it has been connected to
  28117. +* the videocore and clients can start to use the stack.
  28118. +*
  28119. +***************************************************************************/
  28120. +
  28121. +void vchiq_call_connected_callbacks(void)
  28122. +{
  28123. + int i;
  28124. +
  28125. + connected_init();
  28126. +
  28127. + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
  28128. + return;
  28129. +
  28130. + for (i = 0; i < g_num_deferred_callbacks; i++)
  28131. + g_deferred_callback[i]();
  28132. +
  28133. + g_num_deferred_callbacks = 0;
  28134. + g_connected = 1;
  28135. + mutex_unlock(&g_connected_mutex);
  28136. +}
  28137. +EXPORT_SYMBOL(vchiq_add_connected_callback);
  28138. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
  28139. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 1970-01-01 01:00:00.000000000 +0100
  28140. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h 2014-09-14 19:03:25.000000000 +0200
  28141. @@ -0,0 +1,50 @@
  28142. +/**
  28143. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28144. + *
  28145. + * Redistribution and use in source and binary forms, with or without
  28146. + * modification, are permitted provided that the following conditions
  28147. + * are met:
  28148. + * 1. Redistributions of source code must retain the above copyright
  28149. + * notice, this list of conditions, and the following disclaimer,
  28150. + * without modification.
  28151. + * 2. Redistributions in binary form must reproduce the above copyright
  28152. + * notice, this list of conditions and the following disclaimer in the
  28153. + * documentation and/or other materials provided with the distribution.
  28154. + * 3. The names of the above-listed copyright holders may not be used
  28155. + * to endorse or promote products derived from this software without
  28156. + * specific prior written permission.
  28157. + *
  28158. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28159. + * GNU General Public License ("GPL") version 2, as published by the Free
  28160. + * Software Foundation.
  28161. + *
  28162. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28163. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28164. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28165. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28166. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28167. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28168. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28169. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28170. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28171. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28172. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28173. + */
  28174. +
  28175. +#ifndef VCHIQ_CONNECTED_H
  28176. +#define VCHIQ_CONNECTED_H
  28177. +
  28178. +/* ---- Include Files ----------------------------------------------------- */
  28179. +
  28180. +/* ---- Constants and Types ---------------------------------------------- */
  28181. +
  28182. +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
  28183. +
  28184. +/* ---- Variable Externs ------------------------------------------------- */
  28185. +
  28186. +/* ---- Function Prototypes ---------------------------------------------- */
  28187. +
  28188. +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
  28189. +void vchiq_call_connected_callbacks(void);
  28190. +
  28191. +#endif /* VCHIQ_CONNECTED_H */
  28192. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
  28193. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 1970-01-01 01:00:00.000000000 +0100
  28194. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c 2014-09-14 19:03:25.000000000 +0200
  28195. @@ -0,0 +1,3851 @@
  28196. +/**
  28197. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  28198. + *
  28199. + * Redistribution and use in source and binary forms, with or without
  28200. + * modification, are permitted provided that the following conditions
  28201. + * are met:
  28202. + * 1. Redistributions of source code must retain the above copyright
  28203. + * notice, this list of conditions, and the following disclaimer,
  28204. + * without modification.
  28205. + * 2. Redistributions in binary form must reproduce the above copyright
  28206. + * notice, this list of conditions and the following disclaimer in the
  28207. + * documentation and/or other materials provided with the distribution.
  28208. + * 3. The names of the above-listed copyright holders may not be used
  28209. + * to endorse or promote products derived from this software without
  28210. + * specific prior written permission.
  28211. + *
  28212. + * ALTERNATIVELY, this software may be distributed under the terms of the
  28213. + * GNU General Public License ("GPL") version 2, as published by the Free
  28214. + * Software Foundation.
  28215. + *
  28216. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  28217. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  28218. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28219. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28220. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28221. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  28222. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28223. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28224. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28225. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28226. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28227. + */
  28228. +
  28229. +#include "vchiq_core.h"
  28230. +#include "vchiq_killable.h"
  28231. +
  28232. +#define VCHIQ_SLOT_HANDLER_STACK 8192
  28233. +
  28234. +#define HANDLE_STATE_SHIFT 12
  28235. +
  28236. +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
  28237. +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
  28238. +#define SLOT_INDEX_FROM_DATA(state, data) \
  28239. + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
  28240. + VCHIQ_SLOT_SIZE)
  28241. +#define SLOT_INDEX_FROM_INFO(state, info) \
  28242. + ((unsigned int)(info - state->slot_info))
  28243. +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
  28244. + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
  28245. +
  28246. +
  28247. +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
  28248. +
  28249. +
  28250. +struct vchiq_open_payload {
  28251. + int fourcc;
  28252. + int client_id;
  28253. + short version;
  28254. + short version_min;
  28255. +};
  28256. +
  28257. +struct vchiq_openack_payload {
  28258. + short version;
  28259. +};
  28260. +
  28261. +/* we require this for consistency between endpoints */
  28262. +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
  28263. +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
  28264. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
  28265. +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
  28266. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
  28267. +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
  28268. +
  28269. +/* Run time control of log level, based on KERN_XXX level. */
  28270. +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
  28271. +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
  28272. +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
  28273. +
  28274. +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
  28275. +
  28276. +static DEFINE_SPINLOCK(service_spinlock);
  28277. +DEFINE_SPINLOCK(bulk_waiter_spinlock);
  28278. +DEFINE_SPINLOCK(quota_spinlock);
  28279. +
  28280. +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  28281. +static unsigned int handle_seq;
  28282. +
  28283. +static const char *const srvstate_names[] = {
  28284. + "FREE",
  28285. + "HIDDEN",
  28286. + "LISTENING",
  28287. + "OPENING",
  28288. + "OPEN",
  28289. + "OPENSYNC",
  28290. + "CLOSESENT",
  28291. + "CLOSERECVD",
  28292. + "CLOSEWAIT",
  28293. + "CLOSED"
  28294. +};
  28295. +
  28296. +static const char *const reason_names[] = {
  28297. + "SERVICE_OPENED",
  28298. + "SERVICE_CLOSED",
  28299. + "MESSAGE_AVAILABLE",
  28300. + "BULK_TRANSMIT_DONE",
  28301. + "BULK_RECEIVE_DONE",
  28302. + "BULK_TRANSMIT_ABORTED",
  28303. + "BULK_RECEIVE_ABORTED"
  28304. +};
  28305. +
  28306. +static const char *const conn_state_names[] = {
  28307. + "DISCONNECTED",
  28308. + "CONNECTING",
  28309. + "CONNECTED",
  28310. + "PAUSING",
  28311. + "PAUSE_SENT",
  28312. + "PAUSED",
  28313. + "RESUMING",
  28314. + "PAUSE_TIMEOUT",
  28315. + "RESUME_TIMEOUT"
  28316. +};
  28317. +
  28318. +
  28319. +static void
  28320. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
  28321. +
  28322. +static const char *msg_type_str(unsigned int msg_type)
  28323. +{
  28324. + switch (msg_type) {
  28325. + case VCHIQ_MSG_PADDING: return "PADDING";
  28326. + case VCHIQ_MSG_CONNECT: return "CONNECT";
  28327. + case VCHIQ_MSG_OPEN: return "OPEN";
  28328. + case VCHIQ_MSG_OPENACK: return "OPENACK";
  28329. + case VCHIQ_MSG_CLOSE: return "CLOSE";
  28330. + case VCHIQ_MSG_DATA: return "DATA";
  28331. + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
  28332. + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
  28333. + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
  28334. + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
  28335. + case VCHIQ_MSG_PAUSE: return "PAUSE";
  28336. + case VCHIQ_MSG_RESUME: return "RESUME";
  28337. + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
  28338. + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
  28339. + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
  28340. + }
  28341. + return "???";
  28342. +}
  28343. +
  28344. +static inline void
  28345. +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
  28346. +{
  28347. + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
  28348. + service->state->id, service->localport,
  28349. + srvstate_names[service->srvstate],
  28350. + srvstate_names[newstate]);
  28351. + service->srvstate = newstate;
  28352. +}
  28353. +
  28354. +VCHIQ_SERVICE_T *
  28355. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
  28356. +{
  28357. + VCHIQ_SERVICE_T *service;
  28358. +
  28359. + spin_lock(&service_spinlock);
  28360. + service = handle_to_service(handle);
  28361. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  28362. + (service->handle == handle)) {
  28363. + BUG_ON(service->ref_count == 0);
  28364. + service->ref_count++;
  28365. + } else
  28366. + service = NULL;
  28367. + spin_unlock(&service_spinlock);
  28368. +
  28369. + if (!service)
  28370. + vchiq_log_info(vchiq_core_log_level,
  28371. + "Invalid service handle 0x%x", handle);
  28372. +
  28373. + return service;
  28374. +}
  28375. +
  28376. +VCHIQ_SERVICE_T *
  28377. +find_service_by_port(VCHIQ_STATE_T *state, int localport)
  28378. +{
  28379. + VCHIQ_SERVICE_T *service = NULL;
  28380. + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
  28381. + spin_lock(&service_spinlock);
  28382. + service = state->services[localport];
  28383. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
  28384. + BUG_ON(service->ref_count == 0);
  28385. + service->ref_count++;
  28386. + } else
  28387. + service = NULL;
  28388. + spin_unlock(&service_spinlock);
  28389. + }
  28390. +
  28391. + if (!service)
  28392. + vchiq_log_info(vchiq_core_log_level,
  28393. + "Invalid port %d", localport);
  28394. +
  28395. + return service;
  28396. +}
  28397. +
  28398. +VCHIQ_SERVICE_T *
  28399. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  28400. + VCHIQ_SERVICE_HANDLE_T handle) {
  28401. + VCHIQ_SERVICE_T *service;
  28402. +
  28403. + spin_lock(&service_spinlock);
  28404. + service = handle_to_service(handle);
  28405. + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  28406. + (service->handle == handle) &&
  28407. + (service->instance == instance)) {
  28408. + BUG_ON(service->ref_count == 0);
  28409. + service->ref_count++;
  28410. + } else
  28411. + service = NULL;
  28412. + spin_unlock(&service_spinlock);
  28413. +
  28414. + if (!service)
  28415. + vchiq_log_info(vchiq_core_log_level,
  28416. + "Invalid service handle 0x%x", handle);
  28417. +
  28418. + return service;
  28419. +}
  28420. +
  28421. +VCHIQ_SERVICE_T *
  28422. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  28423. + VCHIQ_SERVICE_HANDLE_T handle) {
  28424. + VCHIQ_SERVICE_T *service;
  28425. +
  28426. + spin_lock(&service_spinlock);
  28427. + service = handle_to_service(handle);
  28428. + if (service &&
  28429. + ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  28430. + (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
  28431. + (service->handle == handle) &&
  28432. + (service->instance == instance)) {
  28433. + BUG_ON(service->ref_count == 0);
  28434. + service->ref_count++;
  28435. + } else
  28436. + service = NULL;
  28437. + spin_unlock(&service_spinlock);
  28438. +
  28439. + if (!service)
  28440. + vchiq_log_info(vchiq_core_log_level,
  28441. + "Invalid service handle 0x%x", handle);
  28442. +
  28443. + return service;
  28444. +}
  28445. +
  28446. +VCHIQ_SERVICE_T *
  28447. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  28448. + int *pidx)
  28449. +{
  28450. + VCHIQ_SERVICE_T *service = NULL;
  28451. + int idx = *pidx;
  28452. +
  28453. + spin_lock(&service_spinlock);
  28454. + while (idx < state->unused_service) {
  28455. + VCHIQ_SERVICE_T *srv = state->services[idx++];
  28456. + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
  28457. + (srv->instance == instance)) {
  28458. + service = srv;
  28459. + BUG_ON(service->ref_count == 0);
  28460. + service->ref_count++;
  28461. + break;
  28462. + }
  28463. + }
  28464. + spin_unlock(&service_spinlock);
  28465. +
  28466. + *pidx = idx;
  28467. +
  28468. + return service;
  28469. +}
  28470. +
  28471. +void
  28472. +lock_service(VCHIQ_SERVICE_T *service)
  28473. +{
  28474. + spin_lock(&service_spinlock);
  28475. + BUG_ON(!service || (service->ref_count == 0));
  28476. + if (service)
  28477. + service->ref_count++;
  28478. + spin_unlock(&service_spinlock);
  28479. +}
  28480. +
  28481. +void
  28482. +unlock_service(VCHIQ_SERVICE_T *service)
  28483. +{
  28484. + VCHIQ_STATE_T *state = service->state;
  28485. + spin_lock(&service_spinlock);
  28486. + BUG_ON(!service || (service->ref_count == 0));
  28487. + if (service && service->ref_count) {
  28488. + service->ref_count--;
  28489. + if (!service->ref_count) {
  28490. + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
  28491. + state->services[service->localport] = NULL;
  28492. + } else
  28493. + service = NULL;
  28494. + }
  28495. + spin_unlock(&service_spinlock);
  28496. +
  28497. + if (service && service->userdata_term)
  28498. + service->userdata_term(service->base.userdata);
  28499. +
  28500. + kfree(service);
  28501. +}
  28502. +
  28503. +int
  28504. +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
  28505. +{
  28506. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  28507. + int id;
  28508. +
  28509. + id = service ? service->client_id : 0;
  28510. + if (service)
  28511. + unlock_service(service);
  28512. +
  28513. + return id;
  28514. +}
  28515. +
  28516. +void *
  28517. +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
  28518. +{
  28519. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  28520. +
  28521. + return service ? service->base.userdata : NULL;
  28522. +}
  28523. +
  28524. +int
  28525. +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
  28526. +{
  28527. + VCHIQ_SERVICE_T *service = handle_to_service(handle);
  28528. +
  28529. + return service ? service->base.fourcc : 0;
  28530. +}
  28531. +
  28532. +static void
  28533. +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
  28534. +{
  28535. + VCHIQ_STATE_T *state = service->state;
  28536. + VCHIQ_SERVICE_QUOTA_T *service_quota;
  28537. +
  28538. + service->closing = 1;
  28539. +
  28540. + /* Synchronise with other threads. */
  28541. + mutex_lock(&state->recycle_mutex);
  28542. + mutex_unlock(&state->recycle_mutex);
  28543. + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
  28544. + /* If we're pausing then the slot_mutex is held until resume
  28545. + * by the slot handler. Therefore don't try to acquire this
  28546. + * mutex if we're the slot handler and in the pause sent state.
  28547. + * We don't need to in this case anyway. */
  28548. + mutex_lock(&state->slot_mutex);
  28549. + mutex_unlock(&state->slot_mutex);
  28550. + }
  28551. +
  28552. + /* Unblock any sending thread. */
  28553. + service_quota = &state->service_quotas[service->localport];
  28554. + up(&service_quota->quota_event);
  28555. +}
  28556. +
  28557. +static void
  28558. +mark_service_closing(VCHIQ_SERVICE_T *service)
  28559. +{
  28560. + mark_service_closing_internal(service, 0);
  28561. +}
  28562. +
  28563. +static inline VCHIQ_STATUS_T
  28564. +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
  28565. + VCHIQ_HEADER_T *header, void *bulk_userdata)
  28566. +{
  28567. + VCHIQ_STATUS_T status;
  28568. + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
  28569. + service->state->id, service->localport, reason_names[reason],
  28570. + (unsigned int)header, (unsigned int)bulk_userdata);
  28571. + status = service->base.callback(reason, header, service->handle,
  28572. + bulk_userdata);
  28573. + if (status == VCHIQ_ERROR) {
  28574. + vchiq_log_warning(vchiq_core_log_level,
  28575. + "%d: ignoring ERROR from callback to service %x",
  28576. + service->state->id, service->handle);
  28577. + status = VCHIQ_SUCCESS;
  28578. + }
  28579. + return status;
  28580. +}
  28581. +
  28582. +inline void
  28583. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
  28584. +{
  28585. + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
  28586. + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
  28587. + conn_state_names[oldstate],
  28588. + conn_state_names[newstate]);
  28589. + state->conn_state = newstate;
  28590. + vchiq_platform_conn_state_changed(state, oldstate, newstate);
  28591. +}
  28592. +
  28593. +static inline void
  28594. +remote_event_create(REMOTE_EVENT_T *event)
  28595. +{
  28596. + event->armed = 0;
  28597. + /* Don't clear the 'fired' flag because it may already have been set
  28598. + ** by the other side. */
  28599. + sema_init(event->event, 0);
  28600. +}
  28601. +
  28602. +static inline void
  28603. +remote_event_destroy(REMOTE_EVENT_T *event)
  28604. +{
  28605. + (void)event;
  28606. +}
  28607. +
  28608. +static inline int
  28609. +remote_event_wait(REMOTE_EVENT_T *event)
  28610. +{
  28611. + if (!event->fired) {
  28612. + event->armed = 1;
  28613. + dsb();
  28614. + if (!event->fired) {
  28615. + if (down_interruptible(event->event) != 0) {
  28616. + event->armed = 0;
  28617. + return 0;
  28618. + }
  28619. + }
  28620. + event->armed = 0;
  28621. + wmb();
  28622. + }
  28623. +
  28624. + event->fired = 0;
  28625. + return 1;
  28626. +}
  28627. +
  28628. +static inline void
  28629. +remote_event_signal_local(REMOTE_EVENT_T *event)
  28630. +{
  28631. + event->armed = 0;
  28632. + up(event->event);
  28633. +}
  28634. +
  28635. +static inline void
  28636. +remote_event_poll(REMOTE_EVENT_T *event)
  28637. +{
  28638. + if (event->fired && event->armed)
  28639. + remote_event_signal_local(event);
  28640. +}
  28641. +
  28642. +void
  28643. +remote_event_pollall(VCHIQ_STATE_T *state)
  28644. +{
  28645. + remote_event_poll(&state->local->sync_trigger);
  28646. + remote_event_poll(&state->local->sync_release);
  28647. + remote_event_poll(&state->local->trigger);
  28648. + remote_event_poll(&state->local->recycle);
  28649. +}
  28650. +
  28651. +/* Round up message sizes so that any space at the end of a slot is always big
  28652. +** enough for a header. This relies on header size being a power of two, which
  28653. +** has been verified earlier by a static assertion. */
  28654. +
  28655. +static inline unsigned int
  28656. +calc_stride(unsigned int size)
  28657. +{
  28658. + /* Allow room for the header */
  28659. + size += sizeof(VCHIQ_HEADER_T);
  28660. +
  28661. + /* Round up */
  28662. + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
  28663. + - 1);
  28664. +}
  28665. +
  28666. +/* Called by the slot handler thread */
  28667. +static VCHIQ_SERVICE_T *
  28668. +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
  28669. +{
  28670. + int i;
  28671. +
  28672. + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
  28673. +
  28674. + for (i = 0; i < state->unused_service; i++) {
  28675. + VCHIQ_SERVICE_T *service = state->services[i];
  28676. + if (service &&
  28677. + (service->public_fourcc == fourcc) &&
  28678. + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  28679. + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
  28680. + (service->remoteport == VCHIQ_PORT_FREE)))) {
  28681. + lock_service(service);
  28682. + return service;
  28683. + }
  28684. + }
  28685. +
  28686. + return NULL;
  28687. +}
  28688. +
  28689. +/* Called by the slot handler thread */
  28690. +static VCHIQ_SERVICE_T *
  28691. +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
  28692. +{
  28693. + int i;
  28694. + for (i = 0; i < state->unused_service; i++) {
  28695. + VCHIQ_SERVICE_T *service = state->services[i];
  28696. + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
  28697. + && (service->remoteport == port)) {
  28698. + lock_service(service);
  28699. + return service;
  28700. + }
  28701. + }
  28702. + return NULL;
  28703. +}
  28704. +
  28705. +inline void
  28706. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
  28707. +{
  28708. + uint32_t value;
  28709. +
  28710. + if (service) {
  28711. + do {
  28712. + value = atomic_read(&service->poll_flags);
  28713. + } while (atomic_cmpxchg(&service->poll_flags, value,
  28714. + value | (1 << poll_type)) != value);
  28715. +
  28716. + do {
  28717. + value = atomic_read(&state->poll_services[
  28718. + service->localport>>5]);
  28719. + } while (atomic_cmpxchg(
  28720. + &state->poll_services[service->localport>>5],
  28721. + value, value | (1 << (service->localport & 0x1f)))
  28722. + != value);
  28723. + }
  28724. +
  28725. + state->poll_needed = 1;
  28726. + wmb();
  28727. +
  28728. + /* ... and ensure the slot handler runs. */
  28729. + remote_event_signal_local(&state->local->trigger);
  28730. +}
  28731. +
  28732. +/* Called from queue_message, by the slot handler and application threads,
  28733. +** with slot_mutex held */
  28734. +static VCHIQ_HEADER_T *
  28735. +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
  28736. +{
  28737. + VCHIQ_SHARED_STATE_T *local = state->local;
  28738. + int tx_pos = state->local_tx_pos;
  28739. + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
  28740. +
  28741. + if (space > slot_space) {
  28742. + VCHIQ_HEADER_T *header;
  28743. + /* Fill the remaining space with padding */
  28744. + WARN_ON(state->tx_data == NULL);
  28745. + header = (VCHIQ_HEADER_T *)
  28746. + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  28747. + header->msgid = VCHIQ_MSGID_PADDING;
  28748. + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
  28749. +
  28750. + tx_pos += slot_space;
  28751. + }
  28752. +
  28753. + /* If necessary, get the next slot. */
  28754. + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
  28755. + int slot_index;
  28756. +
  28757. + /* If there is no free slot... */
  28758. +
  28759. + if (down_trylock(&state->slot_available_event) != 0) {
  28760. + /* ...wait for one. */
  28761. +
  28762. + VCHIQ_STATS_INC(state, slot_stalls);
  28763. +
  28764. + /* But first, flush through the last slot. */
  28765. + state->local_tx_pos = tx_pos;
  28766. + local->tx_pos = tx_pos;
  28767. + remote_event_signal(&state->remote->trigger);
  28768. +
  28769. + if (!is_blocking ||
  28770. + (down_interruptible(
  28771. + &state->slot_available_event) != 0))
  28772. + return NULL; /* No space available */
  28773. + }
  28774. +
  28775. + BUG_ON(tx_pos ==
  28776. + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
  28777. +
  28778. + slot_index = local->slot_queue[
  28779. + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
  28780. + VCHIQ_SLOT_QUEUE_MASK];
  28781. + state->tx_data =
  28782. + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  28783. + }
  28784. +
  28785. + state->local_tx_pos = tx_pos + space;
  28786. +
  28787. + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
  28788. +}
  28789. +
  28790. +/* Called by the recycle thread. */
  28791. +static void
  28792. +process_free_queue(VCHIQ_STATE_T *state)
  28793. +{
  28794. + VCHIQ_SHARED_STATE_T *local = state->local;
  28795. + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  28796. + int slot_queue_available;
  28797. +
  28798. + /* Use a read memory barrier to ensure that any state that may have
  28799. + ** been modified by another thread is not masked by stale prefetched
  28800. + ** values. */
  28801. + rmb();
  28802. +
  28803. + /* Find slots which have been freed by the other side, and return them
  28804. + ** to the available queue. */
  28805. + slot_queue_available = state->slot_queue_available;
  28806. +
  28807. + while (slot_queue_available != local->slot_queue_recycle) {
  28808. + unsigned int pos;
  28809. + int slot_index = local->slot_queue[slot_queue_available++ &
  28810. + VCHIQ_SLOT_QUEUE_MASK];
  28811. + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
  28812. + int data_found = 0;
  28813. +
  28814. + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
  28815. + state->id, slot_index, (unsigned int)data,
  28816. + local->slot_queue_recycle, slot_queue_available);
  28817. +
  28818. + /* Initialise the bitmask for services which have used this
  28819. + ** slot */
  28820. + BITSET_ZERO(service_found);
  28821. +
  28822. + pos = 0;
  28823. +
  28824. + while (pos < VCHIQ_SLOT_SIZE) {
  28825. + VCHIQ_HEADER_T *header =
  28826. + (VCHIQ_HEADER_T *)(data + pos);
  28827. + int msgid = header->msgid;
  28828. + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
  28829. + int port = VCHIQ_MSG_SRCPORT(msgid);
  28830. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  28831. + &state->service_quotas[port];
  28832. + int count;
  28833. + spin_lock(&quota_spinlock);
  28834. + count = service_quota->message_use_count;
  28835. + if (count > 0)
  28836. + service_quota->message_use_count =
  28837. + count - 1;
  28838. + spin_unlock(&quota_spinlock);
  28839. +
  28840. + if (count == service_quota->message_quota)
  28841. + /* Signal the service that it
  28842. + ** has dropped below its quota
  28843. + */
  28844. + up(&service_quota->quota_event);
  28845. + else if (count == 0) {
  28846. + vchiq_log_error(vchiq_core_log_level,
  28847. + "service %d "
  28848. + "message_use_count=%d "
  28849. + "(header %x, msgid %x, "
  28850. + "header->msgid %x, "
  28851. + "header->size %x)",
  28852. + port,
  28853. + service_quota->
  28854. + message_use_count,
  28855. + (unsigned int)header, msgid,
  28856. + header->msgid,
  28857. + header->size);
  28858. + WARN(1, "invalid message use count\n");
  28859. + }
  28860. + if (!BITSET_IS_SET(service_found, port)) {
  28861. + /* Set the found bit for this service */
  28862. + BITSET_SET(service_found, port);
  28863. +
  28864. + spin_lock(&quota_spinlock);
  28865. + count = service_quota->slot_use_count;
  28866. + if (count > 0)
  28867. + service_quota->slot_use_count =
  28868. + count - 1;
  28869. + spin_unlock(&quota_spinlock);
  28870. +
  28871. + if (count > 0) {
  28872. + /* Signal the service in case
  28873. + ** it has dropped below its
  28874. + ** quota */
  28875. + up(&service_quota->quota_event);
  28876. + vchiq_log_trace(
  28877. + vchiq_core_log_level,
  28878. + "%d: pfq:%d %x@%x - "
  28879. + "slot_use->%d",
  28880. + state->id, port,
  28881. + header->size,
  28882. + (unsigned int)header,
  28883. + count - 1);
  28884. + } else {
  28885. + vchiq_log_error(
  28886. + vchiq_core_log_level,
  28887. + "service %d "
  28888. + "slot_use_count"
  28889. + "=%d (header %x"
  28890. + ", msgid %x, "
  28891. + "header->msgid"
  28892. + " %x, header->"
  28893. + "size %x)",
  28894. + port, count,
  28895. + (unsigned int)header,
  28896. + msgid,
  28897. + header->msgid,
  28898. + header->size);
  28899. + WARN(1, "bad slot use count\n");
  28900. + }
  28901. + }
  28902. +
  28903. + data_found = 1;
  28904. + }
  28905. +
  28906. + pos += calc_stride(header->size);
  28907. + if (pos > VCHIQ_SLOT_SIZE) {
  28908. + vchiq_log_error(vchiq_core_log_level,
  28909. + "pfq - pos %x: header %x, msgid %x, "
  28910. + "header->msgid %x, header->size %x",
  28911. + pos, (unsigned int)header, msgid,
  28912. + header->msgid, header->size);
  28913. + WARN(1, "invalid slot position\n");
  28914. + }
  28915. + }
  28916. +
  28917. + if (data_found) {
  28918. + int count;
  28919. + spin_lock(&quota_spinlock);
  28920. + count = state->data_use_count;
  28921. + if (count > 0)
  28922. + state->data_use_count =
  28923. + count - 1;
  28924. + spin_unlock(&quota_spinlock);
  28925. + if (count == state->data_quota)
  28926. + up(&state->data_quota_event);
  28927. + }
  28928. +
  28929. + state->slot_queue_available = slot_queue_available;
  28930. + up(&state->slot_available_event);
  28931. + }
  28932. +}
  28933. +
  28934. +/* Called by the slot handler and application threads */
  28935. +static VCHIQ_STATUS_T
  28936. +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  28937. + int msgid, const VCHIQ_ELEMENT_T *elements,
  28938. + int count, int size, int is_blocking)
  28939. +{
  28940. + VCHIQ_SHARED_STATE_T *local;
  28941. + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
  28942. + VCHIQ_HEADER_T *header;
  28943. + int type = VCHIQ_MSG_TYPE(msgid);
  28944. +
  28945. + unsigned int stride;
  28946. +
  28947. + local = state->local;
  28948. +
  28949. + stride = calc_stride(size);
  28950. +
  28951. + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
  28952. +
  28953. + if ((type != VCHIQ_MSG_RESUME) &&
  28954. + (mutex_lock_interruptible(&state->slot_mutex) != 0))
  28955. + return VCHIQ_RETRY;
  28956. +
  28957. + if (type == VCHIQ_MSG_DATA) {
  28958. + int tx_end_index;
  28959. +
  28960. + BUG_ON(!service);
  28961. +
  28962. + if (service->closing) {
  28963. + /* The service has been closed */
  28964. + mutex_unlock(&state->slot_mutex);
  28965. + return VCHIQ_ERROR;
  28966. + }
  28967. +
  28968. + service_quota = &state->service_quotas[service->localport];
  28969. +
  28970. + spin_lock(&quota_spinlock);
  28971. +
  28972. + /* Ensure this service doesn't use more than its quota of
  28973. + ** messages or slots */
  28974. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  28975. + state->local_tx_pos + stride - 1);
  28976. +
  28977. + /* Ensure data messages don't use more than their quota of
  28978. + ** slots */
  28979. + while ((tx_end_index != state->previous_data_index) &&
  28980. + (state->data_use_count == state->data_quota)) {
  28981. + VCHIQ_STATS_INC(state, data_stalls);
  28982. + spin_unlock(&quota_spinlock);
  28983. + mutex_unlock(&state->slot_mutex);
  28984. +
  28985. + if (down_interruptible(&state->data_quota_event)
  28986. + != 0)
  28987. + return VCHIQ_RETRY;
  28988. +
  28989. + mutex_lock(&state->slot_mutex);
  28990. + spin_lock(&quota_spinlock);
  28991. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  28992. + state->local_tx_pos + stride - 1);
  28993. + if ((tx_end_index == state->previous_data_index) ||
  28994. + (state->data_use_count < state->data_quota)) {
  28995. + /* Pass the signal on to other waiters */
  28996. + up(&state->data_quota_event);
  28997. + break;
  28998. + }
  28999. + }
  29000. +
  29001. + while ((service_quota->message_use_count ==
  29002. + service_quota->message_quota) ||
  29003. + ((tx_end_index != service_quota->previous_tx_index) &&
  29004. + (service_quota->slot_use_count ==
  29005. + service_quota->slot_quota))) {
  29006. + spin_unlock(&quota_spinlock);
  29007. + vchiq_log_trace(vchiq_core_log_level,
  29008. + "%d: qm:%d %s,%x - quota stall "
  29009. + "(msg %d, slot %d)",
  29010. + state->id, service->localport,
  29011. + msg_type_str(type), size,
  29012. + service_quota->message_use_count,
  29013. + service_quota->slot_use_count);
  29014. + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
  29015. + mutex_unlock(&state->slot_mutex);
  29016. + if (down_interruptible(&service_quota->quota_event)
  29017. + != 0)
  29018. + return VCHIQ_RETRY;
  29019. + if (service->closing)
  29020. + return VCHIQ_ERROR;
  29021. + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
  29022. + return VCHIQ_RETRY;
  29023. + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
  29024. + /* The service has been closed */
  29025. + mutex_unlock(&state->slot_mutex);
  29026. + return VCHIQ_ERROR;
  29027. + }
  29028. + spin_lock(&quota_spinlock);
  29029. + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
  29030. + state->local_tx_pos + stride - 1);
  29031. + }
  29032. +
  29033. + spin_unlock(&quota_spinlock);
  29034. + }
  29035. +
  29036. + header = reserve_space(state, stride, is_blocking);
  29037. +
  29038. + if (!header) {
  29039. + if (service)
  29040. + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
  29041. + mutex_unlock(&state->slot_mutex);
  29042. + return VCHIQ_RETRY;
  29043. + }
  29044. +
  29045. + if (type == VCHIQ_MSG_DATA) {
  29046. + int i, pos;
  29047. + int tx_end_index;
  29048. + int slot_use_count;
  29049. +
  29050. + vchiq_log_info(vchiq_core_log_level,
  29051. + "%d: qm %s@%x,%x (%d->%d)",
  29052. + state->id,
  29053. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29054. + (unsigned int)header, size,
  29055. + VCHIQ_MSG_SRCPORT(msgid),
  29056. + VCHIQ_MSG_DSTPORT(msgid));
  29057. +
  29058. + BUG_ON(!service);
  29059. +
  29060. + for (i = 0, pos = 0; i < (unsigned int)count;
  29061. + pos += elements[i++].size)
  29062. + if (elements[i].size) {
  29063. + if (vchiq_copy_from_user
  29064. + (header->data + pos, elements[i].data,
  29065. + (size_t) elements[i].size) !=
  29066. + VCHIQ_SUCCESS) {
  29067. + mutex_unlock(&state->slot_mutex);
  29068. + VCHIQ_SERVICE_STATS_INC(service,
  29069. + error_count);
  29070. + return VCHIQ_ERROR;
  29071. + }
  29072. + if (i == 0) {
  29073. + if (vchiq_core_msg_log_level >=
  29074. + VCHIQ_LOG_INFO)
  29075. + vchiq_log_dump_mem("Sent", 0,
  29076. + header->data + pos,
  29077. + min(64u,
  29078. + elements[0].size));
  29079. + }
  29080. + }
  29081. +
  29082. + spin_lock(&quota_spinlock);
  29083. + service_quota->message_use_count++;
  29084. +
  29085. + tx_end_index =
  29086. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
  29087. +
  29088. + /* If this transmission can't fit in the last slot used by any
  29089. + ** service, the data_use_count must be increased. */
  29090. + if (tx_end_index != state->previous_data_index) {
  29091. + state->previous_data_index = tx_end_index;
  29092. + state->data_use_count++;
  29093. + }
  29094. +
  29095. + /* If this isn't the same slot last used by this service,
  29096. + ** the service's slot_use_count must be increased. */
  29097. + if (tx_end_index != service_quota->previous_tx_index) {
  29098. + service_quota->previous_tx_index = tx_end_index;
  29099. + slot_use_count = ++service_quota->slot_use_count;
  29100. + } else {
  29101. + slot_use_count = 0;
  29102. + }
  29103. +
  29104. + spin_unlock(&quota_spinlock);
  29105. +
  29106. + if (slot_use_count)
  29107. + vchiq_log_trace(vchiq_core_log_level,
  29108. + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
  29109. + state->id, service->localport,
  29110. + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
  29111. + slot_use_count, header);
  29112. +
  29113. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  29114. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  29115. + } else {
  29116. + vchiq_log_info(vchiq_core_log_level,
  29117. + "%d: qm %s@%x,%x (%d->%d)", state->id,
  29118. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29119. + (unsigned int)header, size,
  29120. + VCHIQ_MSG_SRCPORT(msgid),
  29121. + VCHIQ_MSG_DSTPORT(msgid));
  29122. + if (size != 0) {
  29123. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  29124. + memcpy(header->data, elements[0].data,
  29125. + elements[0].size);
  29126. + }
  29127. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  29128. + }
  29129. +
  29130. + header->msgid = msgid;
  29131. + header->size = size;
  29132. +
  29133. + {
  29134. + int svc_fourcc;
  29135. +
  29136. + svc_fourcc = service
  29137. + ? service->base.fourcc
  29138. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  29139. +
  29140. + vchiq_log_info(vchiq_core_msg_log_level,
  29141. + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  29142. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29143. + VCHIQ_MSG_TYPE(msgid),
  29144. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  29145. + VCHIQ_MSG_SRCPORT(msgid),
  29146. + VCHIQ_MSG_DSTPORT(msgid),
  29147. + size);
  29148. + }
  29149. +
  29150. + /* Make sure the new header is visible to the peer. */
  29151. + wmb();
  29152. +
  29153. + /* Make the new tx_pos visible to the peer. */
  29154. + local->tx_pos = state->local_tx_pos;
  29155. + wmb();
  29156. +
  29157. + if (service && (type == VCHIQ_MSG_CLOSE))
  29158. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
  29159. +
  29160. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  29161. + mutex_unlock(&state->slot_mutex);
  29162. +
  29163. + remote_event_signal(&state->remote->trigger);
  29164. +
  29165. + return VCHIQ_SUCCESS;
  29166. +}
  29167. +
  29168. +/* Called by the slot handler and application threads */
  29169. +static VCHIQ_STATUS_T
  29170. +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
  29171. + int msgid, const VCHIQ_ELEMENT_T *elements,
  29172. + int count, int size, int is_blocking)
  29173. +{
  29174. + VCHIQ_SHARED_STATE_T *local;
  29175. + VCHIQ_HEADER_T *header;
  29176. +
  29177. + local = state->local;
  29178. +
  29179. + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
  29180. + (mutex_lock_interruptible(&state->sync_mutex) != 0))
  29181. + return VCHIQ_RETRY;
  29182. +
  29183. + remote_event_wait(&local->sync_release);
  29184. +
  29185. + rmb();
  29186. +
  29187. + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  29188. + local->slot_sync);
  29189. +
  29190. + {
  29191. + int oldmsgid = header->msgid;
  29192. + if (oldmsgid != VCHIQ_MSGID_PADDING)
  29193. + vchiq_log_error(vchiq_core_log_level,
  29194. + "%d: qms - msgid %x, not PADDING",
  29195. + state->id, oldmsgid);
  29196. + }
  29197. +
  29198. + if (service) {
  29199. + int i, pos;
  29200. +
  29201. + vchiq_log_info(vchiq_sync_log_level,
  29202. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  29203. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29204. + (unsigned int)header, size,
  29205. + VCHIQ_MSG_SRCPORT(msgid),
  29206. + VCHIQ_MSG_DSTPORT(msgid));
  29207. +
  29208. + for (i = 0, pos = 0; i < (unsigned int)count;
  29209. + pos += elements[i++].size)
  29210. + if (elements[i].size) {
  29211. + if (vchiq_copy_from_user
  29212. + (header->data + pos, elements[i].data,
  29213. + (size_t) elements[i].size) !=
  29214. + VCHIQ_SUCCESS) {
  29215. + mutex_unlock(&state->sync_mutex);
  29216. + VCHIQ_SERVICE_STATS_INC(service,
  29217. + error_count);
  29218. + return VCHIQ_ERROR;
  29219. + }
  29220. + if (i == 0) {
  29221. + if (vchiq_sync_log_level >=
  29222. + VCHIQ_LOG_TRACE)
  29223. + vchiq_log_dump_mem("Sent Sync",
  29224. + 0, header->data + pos,
  29225. + min(64u,
  29226. + elements[0].size));
  29227. + }
  29228. + }
  29229. +
  29230. + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
  29231. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
  29232. + } else {
  29233. + vchiq_log_info(vchiq_sync_log_level,
  29234. + "%d: qms %s@%x,%x (%d->%d)", state->id,
  29235. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29236. + (unsigned int)header, size,
  29237. + VCHIQ_MSG_SRCPORT(msgid),
  29238. + VCHIQ_MSG_DSTPORT(msgid));
  29239. + if (size != 0) {
  29240. + WARN_ON(!((count == 1) && (size == elements[0].size)));
  29241. + memcpy(header->data, elements[0].data,
  29242. + elements[0].size);
  29243. + }
  29244. + VCHIQ_STATS_INC(state, ctrl_tx_count);
  29245. + }
  29246. +
  29247. + header->size = size;
  29248. + header->msgid = msgid;
  29249. +
  29250. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  29251. + int svc_fourcc;
  29252. +
  29253. + svc_fourcc = service
  29254. + ? service->base.fourcc
  29255. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  29256. +
  29257. + vchiq_log_trace(vchiq_sync_log_level,
  29258. + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
  29259. + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
  29260. + VCHIQ_MSG_TYPE(msgid),
  29261. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  29262. + VCHIQ_MSG_SRCPORT(msgid),
  29263. + VCHIQ_MSG_DSTPORT(msgid),
  29264. + size);
  29265. + }
  29266. +
  29267. + /* Make sure the new header is visible to the peer. */
  29268. + wmb();
  29269. +
  29270. + remote_event_signal(&state->remote->sync_trigger);
  29271. +
  29272. + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
  29273. + mutex_unlock(&state->sync_mutex);
  29274. +
  29275. + return VCHIQ_SUCCESS;
  29276. +}
  29277. +
  29278. +static inline void
  29279. +claim_slot(VCHIQ_SLOT_INFO_T *slot)
  29280. +{
  29281. + slot->use_count++;
  29282. +}
  29283. +
  29284. +static void
  29285. +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
  29286. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
  29287. +{
  29288. + int release_count;
  29289. +
  29290. + mutex_lock(&state->recycle_mutex);
  29291. +
  29292. + if (header) {
  29293. + int msgid = header->msgid;
  29294. + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
  29295. + (service && service->closing)) {
  29296. + mutex_unlock(&state->recycle_mutex);
  29297. + return;
  29298. + }
  29299. +
  29300. + /* Rewrite the message header to prevent a double
  29301. + ** release */
  29302. + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
  29303. + }
  29304. +
  29305. + release_count = slot_info->release_count;
  29306. + slot_info->release_count = ++release_count;
  29307. +
  29308. + if (release_count == slot_info->use_count) {
  29309. + int slot_queue_recycle;
  29310. + /* Add to the freed queue */
  29311. +
  29312. + /* A read barrier is necessary here to prevent speculative
  29313. + ** fetches of remote->slot_queue_recycle from overtaking the
  29314. + ** mutex. */
  29315. + rmb();
  29316. +
  29317. + slot_queue_recycle = state->remote->slot_queue_recycle;
  29318. + state->remote->slot_queue[slot_queue_recycle &
  29319. + VCHIQ_SLOT_QUEUE_MASK] =
  29320. + SLOT_INDEX_FROM_INFO(state, slot_info);
  29321. + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
  29322. + vchiq_log_info(vchiq_core_log_level,
  29323. + "%d: release_slot %d - recycle->%x",
  29324. + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
  29325. + state->remote->slot_queue_recycle);
  29326. +
  29327. + /* A write barrier is necessary, but remote_event_signal
  29328. + ** contains one. */
  29329. + remote_event_signal(&state->remote->recycle);
  29330. + }
  29331. +
  29332. + mutex_unlock(&state->recycle_mutex);
  29333. +}
  29334. +
  29335. +/* Called by the slot handler - don't hold the bulk mutex */
  29336. +static VCHIQ_STATUS_T
  29337. +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
  29338. + int retry_poll)
  29339. +{
  29340. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  29341. +
  29342. + vchiq_log_trace(vchiq_core_log_level,
  29343. + "%d: nb:%d %cx - p=%x rn=%x r=%x",
  29344. + service->state->id, service->localport,
  29345. + (queue == &service->bulk_tx) ? 't' : 'r',
  29346. + queue->process, queue->remote_notify, queue->remove);
  29347. +
  29348. + if (service->state->is_master) {
  29349. + while (queue->remote_notify != queue->process) {
  29350. + VCHIQ_BULK_T *bulk =
  29351. + &queue->bulks[BULK_INDEX(queue->remote_notify)];
  29352. + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
  29353. + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
  29354. + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
  29355. + service->remoteport);
  29356. + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
  29357. + /* Only reply to non-dummy bulk requests */
  29358. + if (bulk->remote_data) {
  29359. + status = queue_message(service->state, NULL,
  29360. + msgid, &element, 1, 4, 0);
  29361. + if (status != VCHIQ_SUCCESS)
  29362. + break;
  29363. + }
  29364. + queue->remote_notify++;
  29365. + }
  29366. + } else {
  29367. + queue->remote_notify = queue->process;
  29368. + }
  29369. +
  29370. + if (status == VCHIQ_SUCCESS) {
  29371. + while (queue->remove != queue->remote_notify) {
  29372. + VCHIQ_BULK_T *bulk =
  29373. + &queue->bulks[BULK_INDEX(queue->remove)];
  29374. +
  29375. + /* Only generate callbacks for non-dummy bulk
  29376. + ** requests, and non-terminated services */
  29377. + if (bulk->data && service->instance) {
  29378. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
  29379. + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
  29380. + VCHIQ_SERVICE_STATS_INC(service,
  29381. + bulk_tx_count);
  29382. + VCHIQ_SERVICE_STATS_ADD(service,
  29383. + bulk_tx_bytes,
  29384. + bulk->actual);
  29385. + } else {
  29386. + VCHIQ_SERVICE_STATS_INC(service,
  29387. + bulk_rx_count);
  29388. + VCHIQ_SERVICE_STATS_ADD(service,
  29389. + bulk_rx_bytes,
  29390. + bulk->actual);
  29391. + }
  29392. + } else {
  29393. + VCHIQ_SERVICE_STATS_INC(service,
  29394. + bulk_aborted_count);
  29395. + }
  29396. + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
  29397. + struct bulk_waiter *waiter;
  29398. + spin_lock(&bulk_waiter_spinlock);
  29399. + waiter = bulk->userdata;
  29400. + if (waiter) {
  29401. + waiter->actual = bulk->actual;
  29402. + up(&waiter->event);
  29403. + }
  29404. + spin_unlock(&bulk_waiter_spinlock);
  29405. + } else if (bulk->mode ==
  29406. + VCHIQ_BULK_MODE_CALLBACK) {
  29407. + VCHIQ_REASON_T reason = (bulk->dir ==
  29408. + VCHIQ_BULK_TRANSMIT) ?
  29409. + ((bulk->actual ==
  29410. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  29411. + VCHIQ_BULK_TRANSMIT_ABORTED :
  29412. + VCHIQ_BULK_TRANSMIT_DONE) :
  29413. + ((bulk->actual ==
  29414. + VCHIQ_BULK_ACTUAL_ABORTED) ?
  29415. + VCHIQ_BULK_RECEIVE_ABORTED :
  29416. + VCHIQ_BULK_RECEIVE_DONE);
  29417. + status = make_service_callback(service,
  29418. + reason, NULL, bulk->userdata);
  29419. + if (status == VCHIQ_RETRY)
  29420. + break;
  29421. + }
  29422. + }
  29423. +
  29424. + queue->remove++;
  29425. + up(&service->bulk_remove_event);
  29426. + }
  29427. + if (!retry_poll)
  29428. + status = VCHIQ_SUCCESS;
  29429. + }
  29430. +
  29431. + if (status == VCHIQ_RETRY)
  29432. + request_poll(service->state, service,
  29433. + (queue == &service->bulk_tx) ?
  29434. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  29435. +
  29436. + return status;
  29437. +}
  29438. +
  29439. +/* Called by the slot handler thread */
  29440. +static void
  29441. +poll_services(VCHIQ_STATE_T *state)
  29442. +{
  29443. + int group, i;
  29444. +
  29445. + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
  29446. + uint32_t flags;
  29447. + flags = atomic_xchg(&state->poll_services[group], 0);
  29448. + for (i = 0; flags; i++) {
  29449. + if (flags & (1 << i)) {
  29450. + VCHIQ_SERVICE_T *service =
  29451. + find_service_by_port(state,
  29452. + (group<<5) + i);
  29453. + uint32_t service_flags;
  29454. + flags &= ~(1 << i);
  29455. + if (!service)
  29456. + continue;
  29457. + service_flags =
  29458. + atomic_xchg(&service->poll_flags, 0);
  29459. + if (service_flags &
  29460. + (1 << VCHIQ_POLL_REMOVE)) {
  29461. + vchiq_log_info(vchiq_core_log_level,
  29462. + "%d: ps - remove %d<->%d",
  29463. + state->id, service->localport,
  29464. + service->remoteport);
  29465. +
  29466. + /* Make it look like a client, because
  29467. + it must be removed and not left in
  29468. + the LISTENING state. */
  29469. + service->public_fourcc =
  29470. + VCHIQ_FOURCC_INVALID;
  29471. +
  29472. + if (vchiq_close_service_internal(
  29473. + service, 0/*!close_recvd*/) !=
  29474. + VCHIQ_SUCCESS)
  29475. + request_poll(state, service,
  29476. + VCHIQ_POLL_REMOVE);
  29477. + } else if (service_flags &
  29478. + (1 << VCHIQ_POLL_TERMINATE)) {
  29479. + vchiq_log_info(vchiq_core_log_level,
  29480. + "%d: ps - terminate %d<->%d",
  29481. + state->id, service->localport,
  29482. + service->remoteport);
  29483. + if (vchiq_close_service_internal(
  29484. + service, 0/*!close_recvd*/) !=
  29485. + VCHIQ_SUCCESS)
  29486. + request_poll(state, service,
  29487. + VCHIQ_POLL_TERMINATE);
  29488. + }
  29489. + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
  29490. + notify_bulks(service,
  29491. + &service->bulk_tx,
  29492. + 1/*retry_poll*/);
  29493. + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
  29494. + notify_bulks(service,
  29495. + &service->bulk_rx,
  29496. + 1/*retry_poll*/);
  29497. + unlock_service(service);
  29498. + }
  29499. + }
  29500. + }
  29501. +}
  29502. +
  29503. +/* Called by the slot handler or application threads, holding the bulk mutex. */
  29504. +static int
  29505. +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  29506. +{
  29507. + VCHIQ_STATE_T *state = service->state;
  29508. + int resolved = 0;
  29509. + int rc;
  29510. +
  29511. + while ((queue->process != queue->local_insert) &&
  29512. + (queue->process != queue->remote_insert)) {
  29513. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  29514. +
  29515. + vchiq_log_trace(vchiq_core_log_level,
  29516. + "%d: rb:%d %cx - li=%x ri=%x p=%x",
  29517. + state->id, service->localport,
  29518. + (queue == &service->bulk_tx) ? 't' : 'r',
  29519. + queue->local_insert, queue->remote_insert,
  29520. + queue->process);
  29521. +
  29522. + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
  29523. + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
  29524. +
  29525. + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
  29526. + if (rc != 0)
  29527. + break;
  29528. +
  29529. + vchiq_transfer_bulk(bulk);
  29530. + mutex_unlock(&state->bulk_transfer_mutex);
  29531. +
  29532. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  29533. + const char *header = (queue == &service->bulk_tx) ?
  29534. + "Send Bulk to" : "Recv Bulk from";
  29535. + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
  29536. + vchiq_log_info(vchiq_core_msg_log_level,
  29537. + "%s %c%c%c%c d:%d len:%d %x<->%x",
  29538. + header,
  29539. + VCHIQ_FOURCC_AS_4CHARS(
  29540. + service->base.fourcc),
  29541. + service->remoteport,
  29542. + bulk->size,
  29543. + (unsigned int)bulk->data,
  29544. + (unsigned int)bulk->remote_data);
  29545. + else
  29546. + vchiq_log_info(vchiq_core_msg_log_level,
  29547. + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
  29548. + " rx len:%d %x<->%x",
  29549. + header,
  29550. + VCHIQ_FOURCC_AS_4CHARS(
  29551. + service->base.fourcc),
  29552. + service->remoteport,
  29553. + bulk->size,
  29554. + bulk->remote_size,
  29555. + (unsigned int)bulk->data,
  29556. + (unsigned int)bulk->remote_data);
  29557. + }
  29558. +
  29559. + vchiq_complete_bulk(bulk);
  29560. + queue->process++;
  29561. + resolved++;
  29562. + }
  29563. + return resolved;
  29564. +}
  29565. +
  29566. +/* Called with the bulk_mutex held */
  29567. +static void
  29568. +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
  29569. +{
  29570. + int is_tx = (queue == &service->bulk_tx);
  29571. + vchiq_log_trace(vchiq_core_log_level,
  29572. + "%d: aob:%d %cx - li=%x ri=%x p=%x",
  29573. + service->state->id, service->localport, is_tx ? 't' : 'r',
  29574. + queue->local_insert, queue->remote_insert, queue->process);
  29575. +
  29576. + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
  29577. + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
  29578. +
  29579. + while ((queue->process != queue->local_insert) ||
  29580. + (queue->process != queue->remote_insert)) {
  29581. + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
  29582. +
  29583. + if (queue->process == queue->remote_insert) {
  29584. + /* fabricate a matching dummy bulk */
  29585. + bulk->remote_data = NULL;
  29586. + bulk->remote_size = 0;
  29587. + queue->remote_insert++;
  29588. + }
  29589. +
  29590. + if (queue->process != queue->local_insert) {
  29591. + vchiq_complete_bulk(bulk);
  29592. +
  29593. + vchiq_log_info(vchiq_core_msg_log_level,
  29594. + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
  29595. + "rx len:%d",
  29596. + is_tx ? "Send Bulk to" : "Recv Bulk from",
  29597. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  29598. + service->remoteport,
  29599. + bulk->size,
  29600. + bulk->remote_size);
  29601. + } else {
  29602. + /* fabricate a matching dummy bulk */
  29603. + bulk->data = NULL;
  29604. + bulk->size = 0;
  29605. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  29606. + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
  29607. + VCHIQ_BULK_RECEIVE;
  29608. + queue->local_insert++;
  29609. + }
  29610. +
  29611. + queue->process++;
  29612. + }
  29613. +}
  29614. +
  29615. +/* Called from the slot handler thread */
  29616. +static void
  29617. +pause_bulks(VCHIQ_STATE_T *state)
  29618. +{
  29619. + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
  29620. + WARN_ON_ONCE(1);
  29621. + atomic_set(&pause_bulks_count, 1);
  29622. + return;
  29623. + }
  29624. +
  29625. + /* Block bulk transfers from all services */
  29626. + mutex_lock(&state->bulk_transfer_mutex);
  29627. +}
  29628. +
  29629. +/* Called from the slot handler thread */
  29630. +static void
  29631. +resume_bulks(VCHIQ_STATE_T *state)
  29632. +{
  29633. + int i;
  29634. + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
  29635. + WARN_ON_ONCE(1);
  29636. + atomic_set(&pause_bulks_count, 0);
  29637. + return;
  29638. + }
  29639. +
  29640. + /* Allow bulk transfers from all services */
  29641. + mutex_unlock(&state->bulk_transfer_mutex);
  29642. +
  29643. + if (state->deferred_bulks == 0)
  29644. + return;
  29645. +
  29646. + /* Deal with any bulks which had to be deferred due to being in
  29647. + * paused state. Don't try to match up to number of deferred bulks
  29648. + * in case we've had something come and close the service in the
  29649. + * interim - just process all bulk queues for all services */
  29650. + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
  29651. + __func__, state->deferred_bulks);
  29652. +
  29653. + for (i = 0; i < state->unused_service; i++) {
  29654. + VCHIQ_SERVICE_T *service = state->services[i];
  29655. + int resolved_rx = 0;
  29656. + int resolved_tx = 0;
  29657. + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
  29658. + continue;
  29659. +
  29660. + mutex_lock(&service->bulk_mutex);
  29661. + resolved_rx = resolve_bulks(service, &service->bulk_rx);
  29662. + resolved_tx = resolve_bulks(service, &service->bulk_tx);
  29663. + mutex_unlock(&service->bulk_mutex);
  29664. + if (resolved_rx)
  29665. + notify_bulks(service, &service->bulk_rx, 1);
  29666. + if (resolved_tx)
  29667. + notify_bulks(service, &service->bulk_tx, 1);
  29668. + }
  29669. + state->deferred_bulks = 0;
  29670. +}
  29671. +
  29672. +static int
  29673. +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  29674. +{
  29675. + VCHIQ_SERVICE_T *service = NULL;
  29676. + int msgid, size;
  29677. + int type;
  29678. + unsigned int localport, remoteport;
  29679. +
  29680. + msgid = header->msgid;
  29681. + size = header->size;
  29682. + type = VCHIQ_MSG_TYPE(msgid);
  29683. + localport = VCHIQ_MSG_DSTPORT(msgid);
  29684. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  29685. + if (size >= sizeof(struct vchiq_open_payload)) {
  29686. + const struct vchiq_open_payload *payload =
  29687. + (struct vchiq_open_payload *)header->data;
  29688. + unsigned int fourcc;
  29689. +
  29690. + fourcc = payload->fourcc;
  29691. + vchiq_log_info(vchiq_core_log_level,
  29692. + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
  29693. + state->id, (unsigned int)header,
  29694. + localport,
  29695. + VCHIQ_FOURCC_AS_4CHARS(fourcc));
  29696. +
  29697. + service = get_listening_service(state, fourcc);
  29698. +
  29699. + if (service) {
  29700. + /* A matching service exists */
  29701. + short version = payload->version;
  29702. + short version_min = payload->version_min;
  29703. + if ((service->version < version_min) ||
  29704. + (version < service->version_min)) {
  29705. + /* Version mismatch */
  29706. + vchiq_loud_error_header();
  29707. + vchiq_loud_error("%d: service %d (%c%c%c%c) "
  29708. + "version mismatch - local (%d, min %d)"
  29709. + " vs. remote (%d, min %d)",
  29710. + state->id, service->localport,
  29711. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  29712. + service->version, service->version_min,
  29713. + version, version_min);
  29714. + vchiq_loud_error_footer();
  29715. + unlock_service(service);
  29716. + service = NULL;
  29717. + goto fail_open;
  29718. + }
  29719. + service->peer_version = version;
  29720. +
  29721. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  29722. + struct vchiq_openack_payload ack_payload = {
  29723. + service->version
  29724. + };
  29725. + VCHIQ_ELEMENT_T body = {
  29726. + &ack_payload,
  29727. + sizeof(ack_payload)
  29728. + };
  29729. +
  29730. + /* Acknowledge the OPEN */
  29731. + if (service->sync) {
  29732. + if (queue_message_sync(state, NULL,
  29733. + VCHIQ_MAKE_MSG(
  29734. + VCHIQ_MSG_OPENACK,
  29735. + service->localport,
  29736. + remoteport),
  29737. + &body, 1, sizeof(ack_payload),
  29738. + 0) == VCHIQ_RETRY)
  29739. + goto bail_not_ready;
  29740. + } else {
  29741. + if (queue_message(state, NULL,
  29742. + VCHIQ_MAKE_MSG(
  29743. + VCHIQ_MSG_OPENACK,
  29744. + service->localport,
  29745. + remoteport),
  29746. + &body, 1, sizeof(ack_payload),
  29747. + 0) == VCHIQ_RETRY)
  29748. + goto bail_not_ready;
  29749. + }
  29750. +
  29751. + /* The service is now open */
  29752. + vchiq_set_service_state(service,
  29753. + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
  29754. + : VCHIQ_SRVSTATE_OPEN);
  29755. + }
  29756. +
  29757. + service->remoteport = remoteport;
  29758. + service->client_id = ((int *)header->data)[1];
  29759. + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
  29760. + NULL, NULL) == VCHIQ_RETRY) {
  29761. + /* Bail out if not ready */
  29762. + service->remoteport = VCHIQ_PORT_FREE;
  29763. + goto bail_not_ready;
  29764. + }
  29765. +
  29766. + /* Success - the message has been dealt with */
  29767. + unlock_service(service);
  29768. + return 1;
  29769. + }
  29770. + }
  29771. +
  29772. +fail_open:
  29773. + /* No available service, or an invalid request - send a CLOSE */
  29774. + if (queue_message(state, NULL,
  29775. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
  29776. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  29777. + goto bail_not_ready;
  29778. +
  29779. + return 1;
  29780. +
  29781. +bail_not_ready:
  29782. + if (service)
  29783. + unlock_service(service);
  29784. +
  29785. + return 0;
  29786. +}
  29787. +
  29788. +/* Called by the slot handler thread */
  29789. +static void
  29790. +parse_rx_slots(VCHIQ_STATE_T *state)
  29791. +{
  29792. + VCHIQ_SHARED_STATE_T *remote = state->remote;
  29793. + VCHIQ_SERVICE_T *service = NULL;
  29794. + int tx_pos;
  29795. + DEBUG_INITIALISE(state->local)
  29796. +
  29797. + tx_pos = remote->tx_pos;
  29798. +
  29799. + while (state->rx_pos != tx_pos) {
  29800. + VCHIQ_HEADER_T *header;
  29801. + int msgid, size;
  29802. + int type;
  29803. + unsigned int localport, remoteport;
  29804. +
  29805. + DEBUG_TRACE(PARSE_LINE);
  29806. + if (!state->rx_data) {
  29807. + int rx_index;
  29808. + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
  29809. + rx_index = remote->slot_queue[
  29810. + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
  29811. + VCHIQ_SLOT_QUEUE_MASK];
  29812. + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
  29813. + rx_index);
  29814. + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
  29815. +
  29816. + /* Initialise use_count to one, and increment
  29817. + ** release_count at the end of the slot to avoid
  29818. + ** releasing the slot prematurely. */
  29819. + state->rx_info->use_count = 1;
  29820. + state->rx_info->release_count = 0;
  29821. + }
  29822. +
  29823. + header = (VCHIQ_HEADER_T *)(state->rx_data +
  29824. + (state->rx_pos & VCHIQ_SLOT_MASK));
  29825. + DEBUG_VALUE(PARSE_HEADER, (int)header);
  29826. + msgid = header->msgid;
  29827. + DEBUG_VALUE(PARSE_MSGID, msgid);
  29828. + size = header->size;
  29829. + type = VCHIQ_MSG_TYPE(msgid);
  29830. + localport = VCHIQ_MSG_DSTPORT(msgid);
  29831. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  29832. +
  29833. + if (type != VCHIQ_MSG_DATA)
  29834. + VCHIQ_STATS_INC(state, ctrl_rx_count);
  29835. +
  29836. + switch (type) {
  29837. + case VCHIQ_MSG_OPENACK:
  29838. + case VCHIQ_MSG_CLOSE:
  29839. + case VCHIQ_MSG_DATA:
  29840. + case VCHIQ_MSG_BULK_RX:
  29841. + case VCHIQ_MSG_BULK_TX:
  29842. + case VCHIQ_MSG_BULK_RX_DONE:
  29843. + case VCHIQ_MSG_BULK_TX_DONE:
  29844. + service = find_service_by_port(state, localport);
  29845. + if ((!service || service->remoteport != remoteport) &&
  29846. + (localport == 0) &&
  29847. + (type == VCHIQ_MSG_CLOSE)) {
  29848. + /* This could be a CLOSE from a client which
  29849. + hadn't yet received the OPENACK - look for
  29850. + the connected service */
  29851. + if (service)
  29852. + unlock_service(service);
  29853. + service = get_connected_service(state,
  29854. + remoteport);
  29855. + if (service)
  29856. + vchiq_log_warning(vchiq_core_log_level,
  29857. + "%d: prs %s@%x (%d->%d) - "
  29858. + "found connected service %d",
  29859. + state->id, msg_type_str(type),
  29860. + (unsigned int)header,
  29861. + remoteport, localport,
  29862. + service->localport);
  29863. + }
  29864. +
  29865. + if (!service) {
  29866. + vchiq_log_error(vchiq_core_log_level,
  29867. + "%d: prs %s@%x (%d->%d) - "
  29868. + "invalid/closed service %d",
  29869. + state->id, msg_type_str(type),
  29870. + (unsigned int)header,
  29871. + remoteport, localport, localport);
  29872. + goto skip_message;
  29873. + }
  29874. + break;
  29875. + default:
  29876. + break;
  29877. + }
  29878. +
  29879. + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
  29880. + int svc_fourcc;
  29881. +
  29882. + svc_fourcc = service
  29883. + ? service->base.fourcc
  29884. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  29885. + vchiq_log_info(vchiq_core_msg_log_level,
  29886. + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
  29887. + "len:%d",
  29888. + msg_type_str(type), type,
  29889. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  29890. + remoteport, localport, size);
  29891. + if (size > 0)
  29892. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  29893. + min(64, size));
  29894. + }
  29895. +
  29896. + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
  29897. + > VCHIQ_SLOT_SIZE) {
  29898. + vchiq_log_error(vchiq_core_log_level,
  29899. + "header %x (msgid %x) - size %x too big for "
  29900. + "slot",
  29901. + (unsigned int)header, (unsigned int)msgid,
  29902. + (unsigned int)size);
  29903. + WARN(1, "oversized for slot\n");
  29904. + }
  29905. +
  29906. + switch (type) {
  29907. + case VCHIQ_MSG_OPEN:
  29908. + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
  29909. + if (!parse_open(state, header))
  29910. + goto bail_not_ready;
  29911. + break;
  29912. + case VCHIQ_MSG_OPENACK:
  29913. + if (size >= sizeof(struct vchiq_openack_payload)) {
  29914. + const struct vchiq_openack_payload *payload =
  29915. + (struct vchiq_openack_payload *)
  29916. + header->data;
  29917. + service->peer_version = payload->version;
  29918. + }
  29919. + vchiq_log_info(vchiq_core_log_level,
  29920. + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
  29921. + state->id, (unsigned int)header, size,
  29922. + remoteport, localport, service->peer_version);
  29923. + if (service->srvstate ==
  29924. + VCHIQ_SRVSTATE_OPENING) {
  29925. + service->remoteport = remoteport;
  29926. + vchiq_set_service_state(service,
  29927. + VCHIQ_SRVSTATE_OPEN);
  29928. + up(&service->remove_event);
  29929. + } else
  29930. + vchiq_log_error(vchiq_core_log_level,
  29931. + "OPENACK received in state %s",
  29932. + srvstate_names[service->srvstate]);
  29933. + break;
  29934. + case VCHIQ_MSG_CLOSE:
  29935. + WARN_ON(size != 0); /* There should be no data */
  29936. +
  29937. + vchiq_log_info(vchiq_core_log_level,
  29938. + "%d: prs CLOSE@%x (%d->%d)",
  29939. + state->id, (unsigned int)header,
  29940. + remoteport, localport);
  29941. +
  29942. + mark_service_closing_internal(service, 1);
  29943. +
  29944. + if (vchiq_close_service_internal(service,
  29945. + 1/*close_recvd*/) == VCHIQ_RETRY)
  29946. + goto bail_not_ready;
  29947. +
  29948. + vchiq_log_info(vchiq_core_log_level,
  29949. + "Close Service %c%c%c%c s:%u d:%d",
  29950. + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
  29951. + service->localport,
  29952. + service->remoteport);
  29953. + break;
  29954. + case VCHIQ_MSG_DATA:
  29955. + vchiq_log_trace(vchiq_core_log_level,
  29956. + "%d: prs DATA@%x,%x (%d->%d)",
  29957. + state->id, (unsigned int)header, size,
  29958. + remoteport, localport);
  29959. +
  29960. + if ((service->remoteport == remoteport)
  29961. + && (service->srvstate ==
  29962. + VCHIQ_SRVSTATE_OPEN)) {
  29963. + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
  29964. + claim_slot(state->rx_info);
  29965. + DEBUG_TRACE(PARSE_LINE);
  29966. + if (make_service_callback(service,
  29967. + VCHIQ_MESSAGE_AVAILABLE, header,
  29968. + NULL) == VCHIQ_RETRY) {
  29969. + DEBUG_TRACE(PARSE_LINE);
  29970. + goto bail_not_ready;
  29971. + }
  29972. + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
  29973. + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
  29974. + size);
  29975. + } else {
  29976. + VCHIQ_STATS_INC(state, error_count);
  29977. + }
  29978. + break;
  29979. + case VCHIQ_MSG_CONNECT:
  29980. + vchiq_log_info(vchiq_core_log_level,
  29981. + "%d: prs CONNECT@%x",
  29982. + state->id, (unsigned int)header);
  29983. + up(&state->connect);
  29984. + break;
  29985. + case VCHIQ_MSG_BULK_RX:
  29986. + case VCHIQ_MSG_BULK_TX: {
  29987. + VCHIQ_BULK_QUEUE_T *queue;
  29988. + WARN_ON(!state->is_master);
  29989. + queue = (type == VCHIQ_MSG_BULK_RX) ?
  29990. + &service->bulk_tx : &service->bulk_rx;
  29991. + if ((service->remoteport == remoteport)
  29992. + && (service->srvstate ==
  29993. + VCHIQ_SRVSTATE_OPEN)) {
  29994. + VCHIQ_BULK_T *bulk;
  29995. + int resolved = 0;
  29996. +
  29997. + DEBUG_TRACE(PARSE_LINE);
  29998. + if (mutex_lock_interruptible(
  29999. + &service->bulk_mutex) != 0) {
  30000. + DEBUG_TRACE(PARSE_LINE);
  30001. + goto bail_not_ready;
  30002. + }
  30003. +
  30004. + WARN_ON(!(queue->remote_insert < queue->remove +
  30005. + VCHIQ_NUM_SERVICE_BULKS));
  30006. + bulk = &queue->bulks[
  30007. + BULK_INDEX(queue->remote_insert)];
  30008. + bulk->remote_data =
  30009. + (void *)((int *)header->data)[0];
  30010. + bulk->remote_size = ((int *)header->data)[1];
  30011. + wmb();
  30012. +
  30013. + vchiq_log_info(vchiq_core_log_level,
  30014. + "%d: prs %s@%x (%d->%d) %x@%x",
  30015. + state->id, msg_type_str(type),
  30016. + (unsigned int)header,
  30017. + remoteport, localport,
  30018. + bulk->remote_size,
  30019. + (unsigned int)bulk->remote_data);
  30020. +
  30021. + queue->remote_insert++;
  30022. +
  30023. + if (atomic_read(&pause_bulks_count)) {
  30024. + state->deferred_bulks++;
  30025. + vchiq_log_info(vchiq_core_log_level,
  30026. + "%s: deferring bulk (%d)",
  30027. + __func__,
  30028. + state->deferred_bulks);
  30029. + if (state->conn_state !=
  30030. + VCHIQ_CONNSTATE_PAUSE_SENT)
  30031. + vchiq_log_error(
  30032. + vchiq_core_log_level,
  30033. + "%s: bulks paused in "
  30034. + "unexpected state %s",
  30035. + __func__,
  30036. + conn_state_names[
  30037. + state->conn_state]);
  30038. + } else if (state->conn_state ==
  30039. + VCHIQ_CONNSTATE_CONNECTED) {
  30040. + DEBUG_TRACE(PARSE_LINE);
  30041. + resolved = resolve_bulks(service,
  30042. + queue);
  30043. + }
  30044. +
  30045. + mutex_unlock(&service->bulk_mutex);
  30046. + if (resolved)
  30047. + notify_bulks(service, queue,
  30048. + 1/*retry_poll*/);
  30049. + }
  30050. + } break;
  30051. + case VCHIQ_MSG_BULK_RX_DONE:
  30052. + case VCHIQ_MSG_BULK_TX_DONE:
  30053. + WARN_ON(state->is_master);
  30054. + if ((service->remoteport == remoteport)
  30055. + && (service->srvstate !=
  30056. + VCHIQ_SRVSTATE_FREE)) {
  30057. + VCHIQ_BULK_QUEUE_T *queue;
  30058. + VCHIQ_BULK_T *bulk;
  30059. +
  30060. + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
  30061. + &service->bulk_rx : &service->bulk_tx;
  30062. +
  30063. + DEBUG_TRACE(PARSE_LINE);
  30064. + if (mutex_lock_interruptible(
  30065. + &service->bulk_mutex) != 0) {
  30066. + DEBUG_TRACE(PARSE_LINE);
  30067. + goto bail_not_ready;
  30068. + }
  30069. + if ((int)(queue->remote_insert -
  30070. + queue->local_insert) >= 0) {
  30071. + vchiq_log_error(vchiq_core_log_level,
  30072. + "%d: prs %s@%x (%d->%d) "
  30073. + "unexpected (ri=%d,li=%d)",
  30074. + state->id, msg_type_str(type),
  30075. + (unsigned int)header,
  30076. + remoteport, localport,
  30077. + queue->remote_insert,
  30078. + queue->local_insert);
  30079. + mutex_unlock(&service->bulk_mutex);
  30080. + break;
  30081. + }
  30082. +
  30083. + BUG_ON(queue->process == queue->local_insert);
  30084. + BUG_ON(queue->process != queue->remote_insert);
  30085. +
  30086. + bulk = &queue->bulks[
  30087. + BULK_INDEX(queue->remote_insert)];
  30088. + bulk->actual = *(int *)header->data;
  30089. + queue->remote_insert++;
  30090. +
  30091. + vchiq_log_info(vchiq_core_log_level,
  30092. + "%d: prs %s@%x (%d->%d) %x@%x",
  30093. + state->id, msg_type_str(type),
  30094. + (unsigned int)header,
  30095. + remoteport, localport,
  30096. + bulk->actual, (unsigned int)bulk->data);
  30097. +
  30098. + vchiq_log_trace(vchiq_core_log_level,
  30099. + "%d: prs:%d %cx li=%x ri=%x p=%x",
  30100. + state->id, localport,
  30101. + (type == VCHIQ_MSG_BULK_RX_DONE) ?
  30102. + 'r' : 't',
  30103. + queue->local_insert,
  30104. + queue->remote_insert, queue->process);
  30105. +
  30106. + DEBUG_TRACE(PARSE_LINE);
  30107. + WARN_ON(queue->process == queue->local_insert);
  30108. + vchiq_complete_bulk(bulk);
  30109. + queue->process++;
  30110. + mutex_unlock(&service->bulk_mutex);
  30111. + DEBUG_TRACE(PARSE_LINE);
  30112. + notify_bulks(service, queue, 1/*retry_poll*/);
  30113. + DEBUG_TRACE(PARSE_LINE);
  30114. + }
  30115. + break;
  30116. + case VCHIQ_MSG_PADDING:
  30117. + vchiq_log_trace(vchiq_core_log_level,
  30118. + "%d: prs PADDING@%x,%x",
  30119. + state->id, (unsigned int)header, size);
  30120. + break;
  30121. + case VCHIQ_MSG_PAUSE:
  30122. + /* If initiated, signal the application thread */
  30123. + vchiq_log_trace(vchiq_core_log_level,
  30124. + "%d: prs PAUSE@%x,%x",
  30125. + state->id, (unsigned int)header, size);
  30126. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  30127. + vchiq_log_error(vchiq_core_log_level,
  30128. + "%d: PAUSE received in state PAUSED",
  30129. + state->id);
  30130. + break;
  30131. + }
  30132. + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
  30133. + /* Send a PAUSE in response */
  30134. + if (queue_message(state, NULL,
  30135. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  30136. + NULL, 0, 0, 0) == VCHIQ_RETRY)
  30137. + goto bail_not_ready;
  30138. + if (state->is_master)
  30139. + pause_bulks(state);
  30140. + }
  30141. + /* At this point slot_mutex is held */
  30142. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
  30143. + vchiq_platform_paused(state);
  30144. + break;
  30145. + case VCHIQ_MSG_RESUME:
  30146. + vchiq_log_trace(vchiq_core_log_level,
  30147. + "%d: prs RESUME@%x,%x",
  30148. + state->id, (unsigned int)header, size);
  30149. + /* Release the slot mutex */
  30150. + mutex_unlock(&state->slot_mutex);
  30151. + if (state->is_master)
  30152. + resume_bulks(state);
  30153. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  30154. + vchiq_platform_resumed(state);
  30155. + break;
  30156. +
  30157. + case VCHIQ_MSG_REMOTE_USE:
  30158. + vchiq_on_remote_use(state);
  30159. + break;
  30160. + case VCHIQ_MSG_REMOTE_RELEASE:
  30161. + vchiq_on_remote_release(state);
  30162. + break;
  30163. + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
  30164. + vchiq_on_remote_use_active(state);
  30165. + break;
  30166. +
  30167. + default:
  30168. + vchiq_log_error(vchiq_core_log_level,
  30169. + "%d: prs invalid msgid %x@%x,%x",
  30170. + state->id, msgid, (unsigned int)header, size);
  30171. + WARN(1, "invalid message\n");
  30172. + break;
  30173. + }
  30174. +
  30175. +skip_message:
  30176. + if (service) {
  30177. + unlock_service(service);
  30178. + service = NULL;
  30179. + }
  30180. +
  30181. + state->rx_pos += calc_stride(size);
  30182. +
  30183. + DEBUG_TRACE(PARSE_LINE);
  30184. + /* Perform some housekeeping when the end of the slot is
  30185. + ** reached. */
  30186. + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
  30187. + /* Remove the extra reference count. */
  30188. + release_slot(state, state->rx_info, NULL, NULL);
  30189. + state->rx_data = NULL;
  30190. + }
  30191. + }
  30192. +
  30193. +bail_not_ready:
  30194. + if (service)
  30195. + unlock_service(service);
  30196. +}
  30197. +
  30198. +/* Called by the slot handler thread */
  30199. +static int
  30200. +slot_handler_func(void *v)
  30201. +{
  30202. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  30203. + VCHIQ_SHARED_STATE_T *local = state->local;
  30204. + DEBUG_INITIALISE(local)
  30205. +
  30206. + while (1) {
  30207. + DEBUG_COUNT(SLOT_HANDLER_COUNT);
  30208. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  30209. + remote_event_wait(&local->trigger);
  30210. +
  30211. + rmb();
  30212. +
  30213. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  30214. + if (state->poll_needed) {
  30215. + /* Check if we need to suspend - may change our
  30216. + * conn_state */
  30217. + vchiq_platform_check_suspend(state);
  30218. +
  30219. + state->poll_needed = 0;
  30220. +
  30221. + /* Handle service polling and other rare conditions here
  30222. + ** out of the mainline code */
  30223. + switch (state->conn_state) {
  30224. + case VCHIQ_CONNSTATE_CONNECTED:
  30225. + /* Poll the services as requested */
  30226. + poll_services(state);
  30227. + break;
  30228. +
  30229. + case VCHIQ_CONNSTATE_PAUSING:
  30230. + if (state->is_master)
  30231. + pause_bulks(state);
  30232. + if (queue_message(state, NULL,
  30233. + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
  30234. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  30235. + vchiq_set_conn_state(state,
  30236. + VCHIQ_CONNSTATE_PAUSE_SENT);
  30237. + } else {
  30238. + if (state->is_master)
  30239. + resume_bulks(state);
  30240. + /* Retry later */
  30241. + state->poll_needed = 1;
  30242. + }
  30243. + break;
  30244. +
  30245. + case VCHIQ_CONNSTATE_PAUSED:
  30246. + vchiq_platform_resume(state);
  30247. + break;
  30248. +
  30249. + case VCHIQ_CONNSTATE_RESUMING:
  30250. + if (queue_message(state, NULL,
  30251. + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
  30252. + NULL, 0, 0, 0) != VCHIQ_RETRY) {
  30253. + if (state->is_master)
  30254. + resume_bulks(state);
  30255. + vchiq_set_conn_state(state,
  30256. + VCHIQ_CONNSTATE_CONNECTED);
  30257. + vchiq_platform_resumed(state);
  30258. + } else {
  30259. + /* This should really be impossible,
  30260. + ** since the PAUSE should have flushed
  30261. + ** through outstanding messages. */
  30262. + vchiq_log_error(vchiq_core_log_level,
  30263. + "Failed to send RESUME "
  30264. + "message");
  30265. + BUG();
  30266. + }
  30267. + break;
  30268. +
  30269. + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
  30270. + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
  30271. + vchiq_platform_handle_timeout(state);
  30272. + break;
  30273. + default:
  30274. + break;
  30275. + }
  30276. +
  30277. +
  30278. + }
  30279. +
  30280. + DEBUG_TRACE(SLOT_HANDLER_LINE);
  30281. + parse_rx_slots(state);
  30282. + }
  30283. + return 0;
  30284. +}
  30285. +
  30286. +
  30287. +/* Called by the recycle thread */
  30288. +static int
  30289. +recycle_func(void *v)
  30290. +{
  30291. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  30292. + VCHIQ_SHARED_STATE_T *local = state->local;
  30293. +
  30294. + while (1) {
  30295. + remote_event_wait(&local->recycle);
  30296. +
  30297. + process_free_queue(state);
  30298. + }
  30299. + return 0;
  30300. +}
  30301. +
  30302. +
  30303. +/* Called by the sync thread */
  30304. +static int
  30305. +sync_func(void *v)
  30306. +{
  30307. + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
  30308. + VCHIQ_SHARED_STATE_T *local = state->local;
  30309. + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
  30310. + state->remote->slot_sync);
  30311. +
  30312. + while (1) {
  30313. + VCHIQ_SERVICE_T *service;
  30314. + int msgid, size;
  30315. + int type;
  30316. + unsigned int localport, remoteport;
  30317. +
  30318. + remote_event_wait(&local->sync_trigger);
  30319. +
  30320. + rmb();
  30321. +
  30322. + msgid = header->msgid;
  30323. + size = header->size;
  30324. + type = VCHIQ_MSG_TYPE(msgid);
  30325. + localport = VCHIQ_MSG_DSTPORT(msgid);
  30326. + remoteport = VCHIQ_MSG_SRCPORT(msgid);
  30327. +
  30328. + service = find_service_by_port(state, localport);
  30329. +
  30330. + if (!service) {
  30331. + vchiq_log_error(vchiq_sync_log_level,
  30332. + "%d: sf %s@%x (%d->%d) - "
  30333. + "invalid/closed service %d",
  30334. + state->id, msg_type_str(type),
  30335. + (unsigned int)header,
  30336. + remoteport, localport, localport);
  30337. + release_message_sync(state, header);
  30338. + continue;
  30339. + }
  30340. +
  30341. + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
  30342. + int svc_fourcc;
  30343. +
  30344. + svc_fourcc = service
  30345. + ? service->base.fourcc
  30346. + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
  30347. + vchiq_log_trace(vchiq_sync_log_level,
  30348. + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
  30349. + msg_type_str(type),
  30350. + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
  30351. + remoteport, localport, size);
  30352. + if (size > 0)
  30353. + vchiq_log_dump_mem("Rcvd", 0, header->data,
  30354. + min(64, size));
  30355. + }
  30356. +
  30357. + switch (type) {
  30358. + case VCHIQ_MSG_OPENACK:
  30359. + if (size >= sizeof(struct vchiq_openack_payload)) {
  30360. + const struct vchiq_openack_payload *payload =
  30361. + (struct vchiq_openack_payload *)
  30362. + header->data;
  30363. + service->peer_version = payload->version;
  30364. + }
  30365. + vchiq_log_info(vchiq_sync_log_level,
  30366. + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
  30367. + state->id, (unsigned int)header, size,
  30368. + remoteport, localport, service->peer_version);
  30369. + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
  30370. + service->remoteport = remoteport;
  30371. + vchiq_set_service_state(service,
  30372. + VCHIQ_SRVSTATE_OPENSYNC);
  30373. + up(&service->remove_event);
  30374. + }
  30375. + release_message_sync(state, header);
  30376. + break;
  30377. +
  30378. + case VCHIQ_MSG_DATA:
  30379. + vchiq_log_trace(vchiq_sync_log_level,
  30380. + "%d: sf DATA@%x,%x (%d->%d)",
  30381. + state->id, (unsigned int)header, size,
  30382. + remoteport, localport);
  30383. +
  30384. + if ((service->remoteport == remoteport) &&
  30385. + (service->srvstate ==
  30386. + VCHIQ_SRVSTATE_OPENSYNC)) {
  30387. + if (make_service_callback(service,
  30388. + VCHIQ_MESSAGE_AVAILABLE, header,
  30389. + NULL) == VCHIQ_RETRY)
  30390. + vchiq_log_error(vchiq_sync_log_level,
  30391. + "synchronous callback to "
  30392. + "service %d returns "
  30393. + "VCHIQ_RETRY",
  30394. + localport);
  30395. + }
  30396. + break;
  30397. +
  30398. + default:
  30399. + vchiq_log_error(vchiq_sync_log_level,
  30400. + "%d: sf unexpected msgid %x@%x,%x",
  30401. + state->id, msgid, (unsigned int)header, size);
  30402. + release_message_sync(state, header);
  30403. + break;
  30404. + }
  30405. +
  30406. + unlock_service(service);
  30407. + }
  30408. +
  30409. + return 0;
  30410. +}
  30411. +
  30412. +
  30413. +static void
  30414. +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
  30415. +{
  30416. + queue->local_insert = 0;
  30417. + queue->remote_insert = 0;
  30418. + queue->process = 0;
  30419. + queue->remote_notify = 0;
  30420. + queue->remove = 0;
  30421. +}
  30422. +
  30423. +
  30424. +inline const char *
  30425. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
  30426. +{
  30427. + return conn_state_names[conn_state];
  30428. +}
  30429. +
  30430. +
  30431. +VCHIQ_SLOT_ZERO_T *
  30432. +vchiq_init_slots(void *mem_base, int mem_size)
  30433. +{
  30434. + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
  30435. + VCHIQ_SLOT_ZERO_T *slot_zero =
  30436. + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
  30437. + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
  30438. + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
  30439. +
  30440. + /* Ensure there is enough memory to run an absolutely minimum system */
  30441. + num_slots -= first_data_slot;
  30442. +
  30443. + if (num_slots < 4) {
  30444. + vchiq_log_error(vchiq_core_log_level,
  30445. + "vchiq_init_slots - insufficient memory %x bytes",
  30446. + mem_size);
  30447. + return NULL;
  30448. + }
  30449. +
  30450. + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
  30451. +
  30452. + slot_zero->magic = VCHIQ_MAGIC;
  30453. + slot_zero->version = VCHIQ_VERSION;
  30454. + slot_zero->version_min = VCHIQ_VERSION_MIN;
  30455. + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
  30456. + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
  30457. + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
  30458. + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
  30459. +
  30460. + slot_zero->master.slot_sync = first_data_slot;
  30461. + slot_zero->master.slot_first = first_data_slot + 1;
  30462. + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
  30463. + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
  30464. + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
  30465. + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
  30466. +
  30467. + return slot_zero;
  30468. +}
  30469. +
  30470. +VCHIQ_STATUS_T
  30471. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  30472. + int is_master)
  30473. +{
  30474. + VCHIQ_SHARED_STATE_T *local;
  30475. + VCHIQ_SHARED_STATE_T *remote;
  30476. + VCHIQ_STATUS_T status;
  30477. + char threadname[10];
  30478. + static int id;
  30479. + int i;
  30480. +
  30481. + vchiq_log_warning(vchiq_core_log_level,
  30482. + "%s: slot_zero = 0x%08lx, is_master = %d",
  30483. + __func__, (unsigned long)slot_zero, is_master);
  30484. +
  30485. + /* Check the input configuration */
  30486. +
  30487. + if (slot_zero->magic != VCHIQ_MAGIC) {
  30488. + vchiq_loud_error_header();
  30489. + vchiq_loud_error("Invalid VCHIQ magic value found.");
  30490. + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
  30491. + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
  30492. + vchiq_loud_error_footer();
  30493. + return VCHIQ_ERROR;
  30494. + }
  30495. +
  30496. + if (slot_zero->version < VCHIQ_VERSION_MIN) {
  30497. + vchiq_loud_error_header();
  30498. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  30499. + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
  30500. + "(minimum %d)",
  30501. + (unsigned int)slot_zero, slot_zero->version,
  30502. + VCHIQ_VERSION_MIN);
  30503. + vchiq_loud_error("Restart with a newer VideoCore image.");
  30504. + vchiq_loud_error_footer();
  30505. + return VCHIQ_ERROR;
  30506. + }
  30507. +
  30508. + if (VCHIQ_VERSION < slot_zero->version_min) {
  30509. + vchiq_loud_error_header();
  30510. + vchiq_loud_error("Incompatible VCHIQ versions found.");
  30511. + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
  30512. + "minimum %d)",
  30513. + (unsigned int)slot_zero, VCHIQ_VERSION,
  30514. + slot_zero->version_min);
  30515. + vchiq_loud_error("Restart with a newer kernel.");
  30516. + vchiq_loud_error_footer();
  30517. + return VCHIQ_ERROR;
  30518. + }
  30519. +
  30520. + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
  30521. + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
  30522. + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
  30523. + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
  30524. + vchiq_loud_error_header();
  30525. + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
  30526. + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
  30527. + "(expected %x)",
  30528. + (unsigned int)slot_zero,
  30529. + slot_zero->slot_zero_size,
  30530. + sizeof(VCHIQ_SLOT_ZERO_T));
  30531. + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
  30532. + vchiq_loud_error("slot_zero=%x: slot_size=%d "
  30533. + "(expected %d",
  30534. + (unsigned int)slot_zero, slot_zero->slot_size,
  30535. + VCHIQ_SLOT_SIZE);
  30536. + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
  30537. + vchiq_loud_error("slot_zero=%x: max_slots=%d "
  30538. + "(expected %d)",
  30539. + (unsigned int)slot_zero, slot_zero->max_slots,
  30540. + VCHIQ_MAX_SLOTS);
  30541. + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
  30542. + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
  30543. + "(expected %d)",
  30544. + (unsigned int)slot_zero,
  30545. + slot_zero->max_slots_per_side,
  30546. + VCHIQ_MAX_SLOTS_PER_SIDE);
  30547. + vchiq_loud_error_footer();
  30548. + return VCHIQ_ERROR;
  30549. + }
  30550. +
  30551. + if (is_master) {
  30552. + local = &slot_zero->master;
  30553. + remote = &slot_zero->slave;
  30554. + } else {
  30555. + local = &slot_zero->slave;
  30556. + remote = &slot_zero->master;
  30557. + }
  30558. +
  30559. + if (local->initialised) {
  30560. + vchiq_loud_error_header();
  30561. + if (remote->initialised)
  30562. + vchiq_loud_error("local state has already been "
  30563. + "initialised");
  30564. + else
  30565. + vchiq_loud_error("master/slave mismatch - two %ss",
  30566. + is_master ? "master" : "slave");
  30567. + vchiq_loud_error_footer();
  30568. + return VCHIQ_ERROR;
  30569. + }
  30570. +
  30571. + memset(state, 0, sizeof(VCHIQ_STATE_T));
  30572. +
  30573. + state->id = id++;
  30574. + state->is_master = is_master;
  30575. +
  30576. + /*
  30577. + initialize shared state pointers
  30578. + */
  30579. +
  30580. + state->local = local;
  30581. + state->remote = remote;
  30582. + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
  30583. +
  30584. + /*
  30585. + initialize events and mutexes
  30586. + */
  30587. +
  30588. + sema_init(&state->connect, 0);
  30589. + mutex_init(&state->mutex);
  30590. + sema_init(&state->trigger_event, 0);
  30591. + sema_init(&state->recycle_event, 0);
  30592. + sema_init(&state->sync_trigger_event, 0);
  30593. + sema_init(&state->sync_release_event, 0);
  30594. +
  30595. + mutex_init(&state->slot_mutex);
  30596. + mutex_init(&state->recycle_mutex);
  30597. + mutex_init(&state->sync_mutex);
  30598. + mutex_init(&state->bulk_transfer_mutex);
  30599. +
  30600. + sema_init(&state->slot_available_event, 0);
  30601. + sema_init(&state->slot_remove_event, 0);
  30602. + sema_init(&state->data_quota_event, 0);
  30603. +
  30604. + state->slot_queue_available = 0;
  30605. +
  30606. + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
  30607. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  30608. + &state->service_quotas[i];
  30609. + sema_init(&service_quota->quota_event, 0);
  30610. + }
  30611. +
  30612. + for (i = local->slot_first; i <= local->slot_last; i++) {
  30613. + local->slot_queue[state->slot_queue_available++] = i;
  30614. + up(&state->slot_available_event);
  30615. + }
  30616. +
  30617. + state->default_slot_quota = state->slot_queue_available/2;
  30618. + state->default_message_quota =
  30619. + min((unsigned short)(state->default_slot_quota * 256),
  30620. + (unsigned short)~0);
  30621. +
  30622. + state->previous_data_index = -1;
  30623. + state->data_use_count = 0;
  30624. + state->data_quota = state->slot_queue_available - 1;
  30625. +
  30626. + local->trigger.event = &state->trigger_event;
  30627. + remote_event_create(&local->trigger);
  30628. + local->tx_pos = 0;
  30629. +
  30630. + local->recycle.event = &state->recycle_event;
  30631. + remote_event_create(&local->recycle);
  30632. + local->slot_queue_recycle = state->slot_queue_available;
  30633. +
  30634. + local->sync_trigger.event = &state->sync_trigger_event;
  30635. + remote_event_create(&local->sync_trigger);
  30636. +
  30637. + local->sync_release.event = &state->sync_release_event;
  30638. + remote_event_create(&local->sync_release);
  30639. +
  30640. + /* At start-of-day, the slot is empty and available */
  30641. + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
  30642. + = VCHIQ_MSGID_PADDING;
  30643. + remote_event_signal_local(&local->sync_release);
  30644. +
  30645. + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
  30646. +
  30647. + status = vchiq_platform_init_state(state);
  30648. +
  30649. + /*
  30650. + bring up slot handler thread
  30651. + */
  30652. + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
  30653. + state->slot_handler_thread = kthread_create(&slot_handler_func,
  30654. + (void *)state,
  30655. + threadname);
  30656. +
  30657. + if (state->slot_handler_thread == NULL) {
  30658. + vchiq_loud_error_header();
  30659. + vchiq_loud_error("couldn't create thread %s", threadname);
  30660. + vchiq_loud_error_footer();
  30661. + return VCHIQ_ERROR;
  30662. + }
  30663. + set_user_nice(state->slot_handler_thread, -19);
  30664. + wake_up_process(state->slot_handler_thread);
  30665. +
  30666. + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
  30667. + state->recycle_thread = kthread_create(&recycle_func,
  30668. + (void *)state,
  30669. + threadname);
  30670. + if (state->recycle_thread == NULL) {
  30671. + vchiq_loud_error_header();
  30672. + vchiq_loud_error("couldn't create thread %s", threadname);
  30673. + vchiq_loud_error_footer();
  30674. + return VCHIQ_ERROR;
  30675. + }
  30676. + set_user_nice(state->recycle_thread, -19);
  30677. + wake_up_process(state->recycle_thread);
  30678. +
  30679. + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
  30680. + state->sync_thread = kthread_create(&sync_func,
  30681. + (void *)state,
  30682. + threadname);
  30683. + if (state->sync_thread == NULL) {
  30684. + vchiq_loud_error_header();
  30685. + vchiq_loud_error("couldn't create thread %s", threadname);
  30686. + vchiq_loud_error_footer();
  30687. + return VCHIQ_ERROR;
  30688. + }
  30689. + set_user_nice(state->sync_thread, -20);
  30690. + wake_up_process(state->sync_thread);
  30691. +
  30692. + BUG_ON(state->id >= VCHIQ_MAX_STATES);
  30693. + vchiq_states[state->id] = state;
  30694. +
  30695. + /* Indicate readiness to the other side */
  30696. + local->initialised = 1;
  30697. +
  30698. + return status;
  30699. +}
  30700. +
  30701. +/* Called from application thread when a client or server service is created. */
  30702. +VCHIQ_SERVICE_T *
  30703. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  30704. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  30705. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
  30706. +{
  30707. + VCHIQ_SERVICE_T *service;
  30708. +
  30709. + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
  30710. + if (service) {
  30711. + service->base.fourcc = params->fourcc;
  30712. + service->base.callback = params->callback;
  30713. + service->base.userdata = params->userdata;
  30714. + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
  30715. + service->ref_count = 1;
  30716. + service->srvstate = VCHIQ_SRVSTATE_FREE;
  30717. + service->userdata_term = userdata_term;
  30718. + service->localport = VCHIQ_PORT_FREE;
  30719. + service->remoteport = VCHIQ_PORT_FREE;
  30720. +
  30721. + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
  30722. + VCHIQ_FOURCC_INVALID : params->fourcc;
  30723. + service->client_id = 0;
  30724. + service->auto_close = 1;
  30725. + service->sync = 0;
  30726. + service->closing = 0;
  30727. + atomic_set(&service->poll_flags, 0);
  30728. + service->version = params->version;
  30729. + service->version_min = params->version_min;
  30730. + service->state = state;
  30731. + service->instance = instance;
  30732. + service->service_use_count = 0;
  30733. + init_bulk_queue(&service->bulk_tx);
  30734. + init_bulk_queue(&service->bulk_rx);
  30735. + sema_init(&service->remove_event, 0);
  30736. + sema_init(&service->bulk_remove_event, 0);
  30737. + mutex_init(&service->bulk_mutex);
  30738. + memset(&service->stats, 0, sizeof(service->stats));
  30739. + } else {
  30740. + vchiq_log_error(vchiq_core_log_level,
  30741. + "Out of memory");
  30742. + }
  30743. +
  30744. + if (service) {
  30745. + VCHIQ_SERVICE_T **pservice = NULL;
  30746. + int i;
  30747. +
  30748. + /* Although it is perfectly possible to use service_spinlock
  30749. + ** to protect the creation of services, it is overkill as it
  30750. + ** disables interrupts while the array is searched.
  30751. + ** The only danger is of another thread trying to create a
  30752. + ** service - service deletion is safe.
  30753. + ** Therefore it is preferable to use state->mutex which,
  30754. + ** although slower to claim, doesn't block interrupts while
  30755. + ** it is held.
  30756. + */
  30757. +
  30758. + mutex_lock(&state->mutex);
  30759. +
  30760. + /* Prepare to use a previously unused service */
  30761. + if (state->unused_service < VCHIQ_MAX_SERVICES)
  30762. + pservice = &state->services[state->unused_service];
  30763. +
  30764. + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
  30765. + for (i = 0; i < state->unused_service; i++) {
  30766. + VCHIQ_SERVICE_T *srv = state->services[i];
  30767. + if (!srv) {
  30768. + pservice = &state->services[i];
  30769. + break;
  30770. + }
  30771. + }
  30772. + } else {
  30773. + for (i = (state->unused_service - 1); i >= 0; i--) {
  30774. + VCHIQ_SERVICE_T *srv = state->services[i];
  30775. + if (!srv)
  30776. + pservice = &state->services[i];
  30777. + else if ((srv->public_fourcc == params->fourcc)
  30778. + && ((srv->instance != instance) ||
  30779. + (srv->base.callback !=
  30780. + params->callback))) {
  30781. + /* There is another server using this
  30782. + ** fourcc which doesn't match. */
  30783. + pservice = NULL;
  30784. + break;
  30785. + }
  30786. + }
  30787. + }
  30788. +
  30789. + if (pservice) {
  30790. + service->localport = (pservice - state->services);
  30791. + if (!handle_seq)
  30792. + handle_seq = VCHIQ_MAX_STATES *
  30793. + VCHIQ_MAX_SERVICES;
  30794. + service->handle = handle_seq |
  30795. + (state->id * VCHIQ_MAX_SERVICES) |
  30796. + service->localport;
  30797. + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
  30798. + *pservice = service;
  30799. + if (pservice == &state->services[state->unused_service])
  30800. + state->unused_service++;
  30801. + }
  30802. +
  30803. + mutex_unlock(&state->mutex);
  30804. +
  30805. + if (!pservice) {
  30806. + kfree(service);
  30807. + service = NULL;
  30808. + }
  30809. + }
  30810. +
  30811. + if (service) {
  30812. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  30813. + &state->service_quotas[service->localport];
  30814. + service_quota->slot_quota = state->default_slot_quota;
  30815. + service_quota->message_quota = state->default_message_quota;
  30816. + if (service_quota->slot_use_count == 0)
  30817. + service_quota->previous_tx_index =
  30818. + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
  30819. + - 1;
  30820. +
  30821. + /* Bring this service online */
  30822. + vchiq_set_service_state(service, srvstate);
  30823. +
  30824. + vchiq_log_info(vchiq_core_msg_log_level,
  30825. + "%s Service %c%c%c%c SrcPort:%d",
  30826. + (srvstate == VCHIQ_SRVSTATE_OPENING)
  30827. + ? "Open" : "Add",
  30828. + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
  30829. + service->localport);
  30830. + }
  30831. +
  30832. + /* Don't unlock the service - leave it with a ref_count of 1. */
  30833. +
  30834. + return service;
  30835. +}
  30836. +
  30837. +VCHIQ_STATUS_T
  30838. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
  30839. +{
  30840. + struct vchiq_open_payload payload = {
  30841. + service->base.fourcc,
  30842. + client_id,
  30843. + service->version,
  30844. + service->version_min
  30845. + };
  30846. + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
  30847. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  30848. +
  30849. + service->client_id = client_id;
  30850. + vchiq_use_service_internal(service);
  30851. + status = queue_message(service->state, NULL,
  30852. + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
  30853. + &body, 1, sizeof(payload), 1);
  30854. + if (status == VCHIQ_SUCCESS) {
  30855. + /* Wait for the ACK/NAK */
  30856. + if (down_interruptible(&service->remove_event) != 0) {
  30857. + status = VCHIQ_RETRY;
  30858. + vchiq_release_service_internal(service);
  30859. + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
  30860. + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
  30861. + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
  30862. + vchiq_log_error(vchiq_core_log_level,
  30863. + "%d: osi - srvstate = %s (ref %d)",
  30864. + service->state->id,
  30865. + srvstate_names[service->srvstate],
  30866. + service->ref_count);
  30867. + status = VCHIQ_ERROR;
  30868. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  30869. + vchiq_release_service_internal(service);
  30870. + }
  30871. + }
  30872. + return status;
  30873. +}
  30874. +
  30875. +static void
  30876. +release_service_messages(VCHIQ_SERVICE_T *service)
  30877. +{
  30878. + VCHIQ_STATE_T *state = service->state;
  30879. + int slot_last = state->remote->slot_last;
  30880. + int i;
  30881. +
  30882. + /* Release any claimed messages */
  30883. + for (i = state->remote->slot_first; i <= slot_last; i++) {
  30884. + VCHIQ_SLOT_INFO_T *slot_info =
  30885. + SLOT_INFO_FROM_INDEX(state, i);
  30886. + if (slot_info->release_count != slot_info->use_count) {
  30887. + char *data =
  30888. + (char *)SLOT_DATA_FROM_INDEX(state, i);
  30889. + unsigned int pos, end;
  30890. +
  30891. + end = VCHIQ_SLOT_SIZE;
  30892. + if (data == state->rx_data)
  30893. + /* This buffer is still being read from - stop
  30894. + ** at the current read position */
  30895. + end = state->rx_pos & VCHIQ_SLOT_MASK;
  30896. +
  30897. + pos = 0;
  30898. +
  30899. + while (pos < end) {
  30900. + VCHIQ_HEADER_T *header =
  30901. + (VCHIQ_HEADER_T *)(data + pos);
  30902. + int msgid = header->msgid;
  30903. + int port = VCHIQ_MSG_DSTPORT(msgid);
  30904. + if ((port == service->localport) &&
  30905. + (msgid & VCHIQ_MSGID_CLAIMED)) {
  30906. + vchiq_log_info(vchiq_core_log_level,
  30907. + " fsi - hdr %x",
  30908. + (unsigned int)header);
  30909. + release_slot(state, slot_info, header,
  30910. + NULL);
  30911. + }
  30912. + pos += calc_stride(header->size);
  30913. + if (pos > VCHIQ_SLOT_SIZE) {
  30914. + vchiq_log_error(vchiq_core_log_level,
  30915. + "fsi - pos %x: header %x, "
  30916. + "msgid %x, header->msgid %x, "
  30917. + "header->size %x",
  30918. + pos, (unsigned int)header,
  30919. + msgid, header->msgid,
  30920. + header->size);
  30921. + WARN(1, "invalid slot position\n");
  30922. + }
  30923. + }
  30924. + }
  30925. + }
  30926. +}
  30927. +
  30928. +static int
  30929. +do_abort_bulks(VCHIQ_SERVICE_T *service)
  30930. +{
  30931. + VCHIQ_STATUS_T status;
  30932. +
  30933. + /* Abort any outstanding bulk transfers */
  30934. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
  30935. + return 0;
  30936. + abort_outstanding_bulks(service, &service->bulk_tx);
  30937. + abort_outstanding_bulks(service, &service->bulk_rx);
  30938. + mutex_unlock(&service->bulk_mutex);
  30939. +
  30940. + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
  30941. + if (status == VCHIQ_SUCCESS)
  30942. + status = notify_bulks(service, &service->bulk_rx,
  30943. + 0/*!retry_poll*/);
  30944. + return (status == VCHIQ_SUCCESS);
  30945. +}
  30946. +
  30947. +static VCHIQ_STATUS_T
  30948. +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
  30949. +{
  30950. + VCHIQ_STATUS_T status;
  30951. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  30952. + int newstate;
  30953. +
  30954. + switch (service->srvstate) {
  30955. + case VCHIQ_SRVSTATE_OPEN:
  30956. + case VCHIQ_SRVSTATE_CLOSESENT:
  30957. + case VCHIQ_SRVSTATE_CLOSERECVD:
  30958. + if (is_server) {
  30959. + if (service->auto_close) {
  30960. + service->client_id = 0;
  30961. + service->remoteport = VCHIQ_PORT_FREE;
  30962. + newstate = VCHIQ_SRVSTATE_LISTENING;
  30963. + } else
  30964. + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
  30965. + } else
  30966. + newstate = VCHIQ_SRVSTATE_CLOSED;
  30967. + vchiq_set_service_state(service, newstate);
  30968. + break;
  30969. + case VCHIQ_SRVSTATE_LISTENING:
  30970. + break;
  30971. + default:
  30972. + vchiq_log_error(vchiq_core_log_level,
  30973. + "close_service_complete(%x) called in state %s",
  30974. + service->handle, srvstate_names[service->srvstate]);
  30975. + WARN(1, "close_service_complete in unexpected state\n");
  30976. + return VCHIQ_ERROR;
  30977. + }
  30978. +
  30979. + status = make_service_callback(service,
  30980. + VCHIQ_SERVICE_CLOSED, NULL, NULL);
  30981. +
  30982. + if (status != VCHIQ_RETRY) {
  30983. + int uc = service->service_use_count;
  30984. + int i;
  30985. + /* Complete the close process */
  30986. + for (i = 0; i < uc; i++)
  30987. + /* cater for cases where close is forced and the
  30988. + ** client may not close all it's handles */
  30989. + vchiq_release_service_internal(service);
  30990. +
  30991. + service->client_id = 0;
  30992. + service->remoteport = VCHIQ_PORT_FREE;
  30993. +
  30994. + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
  30995. + vchiq_free_service_internal(service);
  30996. + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
  30997. + if (is_server)
  30998. + service->closing = 0;
  30999. +
  31000. + up(&service->remove_event);
  31001. + }
  31002. + } else
  31003. + vchiq_set_service_state(service, failstate);
  31004. +
  31005. + return status;
  31006. +}
  31007. +
  31008. +/* Called by the slot handler */
  31009. +VCHIQ_STATUS_T
  31010. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
  31011. +{
  31012. + VCHIQ_STATE_T *state = service->state;
  31013. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  31014. + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
  31015. +
  31016. + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
  31017. + service->state->id, service->localport, close_recvd,
  31018. + srvstate_names[service->srvstate]);
  31019. +
  31020. + switch (service->srvstate) {
  31021. + case VCHIQ_SRVSTATE_CLOSED:
  31022. + case VCHIQ_SRVSTATE_HIDDEN:
  31023. + case VCHIQ_SRVSTATE_LISTENING:
  31024. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  31025. + if (close_recvd)
  31026. + vchiq_log_error(vchiq_core_log_level,
  31027. + "vchiq_close_service_internal(1) called "
  31028. + "in state %s",
  31029. + srvstate_names[service->srvstate]);
  31030. + else if (is_server) {
  31031. + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
  31032. + status = VCHIQ_ERROR;
  31033. + } else {
  31034. + service->client_id = 0;
  31035. + service->remoteport = VCHIQ_PORT_FREE;
  31036. + if (service->srvstate ==
  31037. + VCHIQ_SRVSTATE_CLOSEWAIT)
  31038. + vchiq_set_service_state(service,
  31039. + VCHIQ_SRVSTATE_LISTENING);
  31040. + }
  31041. + up(&service->remove_event);
  31042. + } else
  31043. + vchiq_free_service_internal(service);
  31044. + break;
  31045. + case VCHIQ_SRVSTATE_OPENING:
  31046. + if (close_recvd) {
  31047. + /* The open was rejected - tell the user */
  31048. + vchiq_set_service_state(service,
  31049. + VCHIQ_SRVSTATE_CLOSEWAIT);
  31050. + up(&service->remove_event);
  31051. + } else {
  31052. + /* Shutdown mid-open - let the other side know */
  31053. + status = queue_message(state, service,
  31054. + VCHIQ_MAKE_MSG
  31055. + (VCHIQ_MSG_CLOSE,
  31056. + service->localport,
  31057. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  31058. + NULL, 0, 0, 0);
  31059. + }
  31060. + break;
  31061. +
  31062. + case VCHIQ_SRVSTATE_OPENSYNC:
  31063. + mutex_lock(&state->sync_mutex);
  31064. + /* Drop through */
  31065. +
  31066. + case VCHIQ_SRVSTATE_OPEN:
  31067. + if (state->is_master || close_recvd) {
  31068. + if (!do_abort_bulks(service))
  31069. + status = VCHIQ_RETRY;
  31070. + }
  31071. +
  31072. + release_service_messages(service);
  31073. +
  31074. + if (status == VCHIQ_SUCCESS)
  31075. + status = queue_message(state, service,
  31076. + VCHIQ_MAKE_MSG
  31077. + (VCHIQ_MSG_CLOSE,
  31078. + service->localport,
  31079. + VCHIQ_MSG_DSTPORT(service->remoteport)),
  31080. + NULL, 0, 0, 0);
  31081. +
  31082. + if (status == VCHIQ_SUCCESS) {
  31083. + if (!close_recvd)
  31084. + break;
  31085. + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
  31086. + mutex_unlock(&state->sync_mutex);
  31087. + break;
  31088. + } else
  31089. + break;
  31090. +
  31091. + status = close_service_complete(service,
  31092. + VCHIQ_SRVSTATE_CLOSERECVD);
  31093. + break;
  31094. +
  31095. + case VCHIQ_SRVSTATE_CLOSESENT:
  31096. + if (!close_recvd)
  31097. + /* This happens when a process is killed mid-close */
  31098. + break;
  31099. +
  31100. + if (!state->is_master) {
  31101. + if (!do_abort_bulks(service)) {
  31102. + status = VCHIQ_RETRY;
  31103. + break;
  31104. + }
  31105. + }
  31106. +
  31107. + if (status == VCHIQ_SUCCESS)
  31108. + status = close_service_complete(service,
  31109. + VCHIQ_SRVSTATE_CLOSERECVD);
  31110. + break;
  31111. +
  31112. + case VCHIQ_SRVSTATE_CLOSERECVD:
  31113. + if (!close_recvd && is_server)
  31114. + /* Force into LISTENING mode */
  31115. + vchiq_set_service_state(service,
  31116. + VCHIQ_SRVSTATE_LISTENING);
  31117. + status = close_service_complete(service,
  31118. + VCHIQ_SRVSTATE_CLOSERECVD);
  31119. + break;
  31120. +
  31121. + default:
  31122. + vchiq_log_error(vchiq_core_log_level,
  31123. + "vchiq_close_service_internal(%d) called in state %s",
  31124. + close_recvd, srvstate_names[service->srvstate]);
  31125. + break;
  31126. + }
  31127. +
  31128. + return status;
  31129. +}
  31130. +
  31131. +/* Called from the application process upon process death */
  31132. +void
  31133. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
  31134. +{
  31135. + VCHIQ_STATE_T *state = service->state;
  31136. +
  31137. + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
  31138. + state->id, service->localport, service->remoteport);
  31139. +
  31140. + mark_service_closing(service);
  31141. +
  31142. + /* Mark the service for removal by the slot handler */
  31143. + request_poll(state, service, VCHIQ_POLL_REMOVE);
  31144. +}
  31145. +
  31146. +/* Called from the slot handler */
  31147. +void
  31148. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
  31149. +{
  31150. + VCHIQ_STATE_T *state = service->state;
  31151. +
  31152. + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
  31153. + state->id, service->localport);
  31154. +
  31155. + switch (service->srvstate) {
  31156. + case VCHIQ_SRVSTATE_OPENING:
  31157. + case VCHIQ_SRVSTATE_CLOSED:
  31158. + case VCHIQ_SRVSTATE_HIDDEN:
  31159. + case VCHIQ_SRVSTATE_LISTENING:
  31160. + case VCHIQ_SRVSTATE_CLOSEWAIT:
  31161. + break;
  31162. + default:
  31163. + vchiq_log_error(vchiq_core_log_level,
  31164. + "%d: fsi - (%d) in state %s",
  31165. + state->id, service->localport,
  31166. + srvstate_names[service->srvstate]);
  31167. + return;
  31168. + }
  31169. +
  31170. + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
  31171. +
  31172. + up(&service->remove_event);
  31173. +
  31174. + /* Release the initial lock */
  31175. + unlock_service(service);
  31176. +}
  31177. +
  31178. +VCHIQ_STATUS_T
  31179. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  31180. +{
  31181. + VCHIQ_SERVICE_T *service;
  31182. + int i;
  31183. +
  31184. + /* Find all services registered to this client and enable them. */
  31185. + i = 0;
  31186. + while ((service = next_service_by_instance(state, instance,
  31187. + &i)) != NULL) {
  31188. + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
  31189. + vchiq_set_service_state(service,
  31190. + VCHIQ_SRVSTATE_LISTENING);
  31191. + unlock_service(service);
  31192. + }
  31193. +
  31194. + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
  31195. + if (queue_message(state, NULL,
  31196. + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
  31197. + 0, 1) == VCHIQ_RETRY)
  31198. + return VCHIQ_RETRY;
  31199. +
  31200. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
  31201. + }
  31202. +
  31203. + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
  31204. + if (down_interruptible(&state->connect) != 0)
  31205. + return VCHIQ_RETRY;
  31206. +
  31207. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
  31208. + up(&state->connect);
  31209. + }
  31210. +
  31211. + return VCHIQ_SUCCESS;
  31212. +}
  31213. +
  31214. +VCHIQ_STATUS_T
  31215. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
  31216. +{
  31217. + VCHIQ_SERVICE_T *service;
  31218. + int i;
  31219. +
  31220. + /* Find all services registered to this client and enable them. */
  31221. + i = 0;
  31222. + while ((service = next_service_by_instance(state, instance,
  31223. + &i)) != NULL) {
  31224. + (void)vchiq_remove_service(service->handle);
  31225. + unlock_service(service);
  31226. + }
  31227. +
  31228. + return VCHIQ_SUCCESS;
  31229. +}
  31230. +
  31231. +VCHIQ_STATUS_T
  31232. +vchiq_pause_internal(VCHIQ_STATE_T *state)
  31233. +{
  31234. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  31235. +
  31236. + switch (state->conn_state) {
  31237. + case VCHIQ_CONNSTATE_CONNECTED:
  31238. + /* Request a pause */
  31239. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
  31240. + request_poll(state, NULL, 0);
  31241. + break;
  31242. + default:
  31243. + vchiq_log_error(vchiq_core_log_level,
  31244. + "vchiq_pause_internal in state %s\n",
  31245. + conn_state_names[state->conn_state]);
  31246. + status = VCHIQ_ERROR;
  31247. + VCHIQ_STATS_INC(state, error_count);
  31248. + break;
  31249. + }
  31250. +
  31251. + return status;
  31252. +}
  31253. +
  31254. +VCHIQ_STATUS_T
  31255. +vchiq_resume_internal(VCHIQ_STATE_T *state)
  31256. +{
  31257. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  31258. +
  31259. + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
  31260. + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
  31261. + request_poll(state, NULL, 0);
  31262. + } else {
  31263. + status = VCHIQ_ERROR;
  31264. + VCHIQ_STATS_INC(state, error_count);
  31265. + }
  31266. +
  31267. + return status;
  31268. +}
  31269. +
  31270. +VCHIQ_STATUS_T
  31271. +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
  31272. +{
  31273. + /* Unregister the service */
  31274. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  31275. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  31276. +
  31277. + if (!service)
  31278. + return VCHIQ_ERROR;
  31279. +
  31280. + vchiq_log_info(vchiq_core_log_level,
  31281. + "%d: close_service:%d",
  31282. + service->state->id, service->localport);
  31283. +
  31284. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  31285. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  31286. + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
  31287. + unlock_service(service);
  31288. + return VCHIQ_ERROR;
  31289. + }
  31290. +
  31291. + mark_service_closing(service);
  31292. +
  31293. + if (current == service->state->slot_handler_thread) {
  31294. + status = vchiq_close_service_internal(service,
  31295. + 0/*!close_recvd*/);
  31296. + BUG_ON(status == VCHIQ_RETRY);
  31297. + } else {
  31298. + /* Mark the service for termination by the slot handler */
  31299. + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
  31300. + }
  31301. +
  31302. + while (1) {
  31303. + if (down_interruptible(&service->remove_event) != 0) {
  31304. + status = VCHIQ_RETRY;
  31305. + break;
  31306. + }
  31307. +
  31308. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  31309. + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
  31310. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  31311. + break;
  31312. +
  31313. + vchiq_log_warning(vchiq_core_log_level,
  31314. + "%d: close_service:%d - waiting in state %s",
  31315. + service->state->id, service->localport,
  31316. + srvstate_names[service->srvstate]);
  31317. + }
  31318. +
  31319. + if ((status == VCHIQ_SUCCESS) &&
  31320. + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
  31321. + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
  31322. + status = VCHIQ_ERROR;
  31323. +
  31324. + unlock_service(service);
  31325. +
  31326. + return status;
  31327. +}
  31328. +
  31329. +VCHIQ_STATUS_T
  31330. +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
  31331. +{
  31332. + /* Unregister the service */
  31333. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  31334. + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
  31335. +
  31336. + if (!service)
  31337. + return VCHIQ_ERROR;
  31338. +
  31339. + vchiq_log_info(vchiq_core_log_level,
  31340. + "%d: remove_service:%d",
  31341. + service->state->id, service->localport);
  31342. +
  31343. + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
  31344. + unlock_service(service);
  31345. + return VCHIQ_ERROR;
  31346. + }
  31347. +
  31348. + mark_service_closing(service);
  31349. +
  31350. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  31351. + (current == service->state->slot_handler_thread)) {
  31352. + /* Make it look like a client, because it must be removed and
  31353. + not left in the LISTENING state. */
  31354. + service->public_fourcc = VCHIQ_FOURCC_INVALID;
  31355. +
  31356. + status = vchiq_close_service_internal(service,
  31357. + 0/*!close_recvd*/);
  31358. + BUG_ON(status == VCHIQ_RETRY);
  31359. + } else {
  31360. + /* Mark the service for removal by the slot handler */
  31361. + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
  31362. + }
  31363. + while (1) {
  31364. + if (down_interruptible(&service->remove_event) != 0) {
  31365. + status = VCHIQ_RETRY;
  31366. + break;
  31367. + }
  31368. +
  31369. + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
  31370. + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
  31371. + break;
  31372. +
  31373. + vchiq_log_warning(vchiq_core_log_level,
  31374. + "%d: remove_service:%d - waiting in state %s",
  31375. + service->state->id, service->localport,
  31376. + srvstate_names[service->srvstate]);
  31377. + }
  31378. +
  31379. + if ((status == VCHIQ_SUCCESS) &&
  31380. + (service->srvstate != VCHIQ_SRVSTATE_FREE))
  31381. + status = VCHIQ_ERROR;
  31382. +
  31383. + unlock_service(service);
  31384. +
  31385. + return status;
  31386. +}
  31387. +
  31388. +
  31389. +/* This function may be called by kernel threads or user threads.
  31390. + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
  31391. + * received and the call should be retried after being returned to user
  31392. + * context.
  31393. + * When called in blocking mode, the userdata field points to a bulk_waiter
  31394. + * structure.
  31395. + */
  31396. +VCHIQ_STATUS_T
  31397. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  31398. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  31399. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
  31400. +{
  31401. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  31402. + VCHIQ_BULK_QUEUE_T *queue;
  31403. + VCHIQ_BULK_T *bulk;
  31404. + VCHIQ_STATE_T *state;
  31405. + struct bulk_waiter *bulk_waiter = NULL;
  31406. + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
  31407. + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
  31408. + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
  31409. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  31410. +
  31411. + if (!service ||
  31412. + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
  31413. + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
  31414. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  31415. + goto error_exit;
  31416. +
  31417. + switch (mode) {
  31418. + case VCHIQ_BULK_MODE_NOCALLBACK:
  31419. + case VCHIQ_BULK_MODE_CALLBACK:
  31420. + break;
  31421. + case VCHIQ_BULK_MODE_BLOCKING:
  31422. + bulk_waiter = (struct bulk_waiter *)userdata;
  31423. + sema_init(&bulk_waiter->event, 0);
  31424. + bulk_waiter->actual = 0;
  31425. + bulk_waiter->bulk = NULL;
  31426. + break;
  31427. + case VCHIQ_BULK_MODE_WAITING:
  31428. + bulk_waiter = (struct bulk_waiter *)userdata;
  31429. + bulk = bulk_waiter->bulk;
  31430. + goto waiting;
  31431. + default:
  31432. + goto error_exit;
  31433. + }
  31434. +
  31435. + state = service->state;
  31436. +
  31437. + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
  31438. + &service->bulk_tx : &service->bulk_rx;
  31439. +
  31440. + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
  31441. + status = VCHIQ_RETRY;
  31442. + goto error_exit;
  31443. + }
  31444. +
  31445. + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
  31446. + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
  31447. + do {
  31448. + mutex_unlock(&service->bulk_mutex);
  31449. + if (down_interruptible(&service->bulk_remove_event)
  31450. + != 0) {
  31451. + status = VCHIQ_RETRY;
  31452. + goto error_exit;
  31453. + }
  31454. + if (mutex_lock_interruptible(&service->bulk_mutex)
  31455. + != 0) {
  31456. + status = VCHIQ_RETRY;
  31457. + goto error_exit;
  31458. + }
  31459. + } while (queue->local_insert == queue->remove +
  31460. + VCHIQ_NUM_SERVICE_BULKS);
  31461. + }
  31462. +
  31463. + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
  31464. +
  31465. + bulk->mode = mode;
  31466. + bulk->dir = dir;
  31467. + bulk->userdata = userdata;
  31468. + bulk->size = size;
  31469. + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
  31470. +
  31471. + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
  31472. + VCHIQ_SUCCESS)
  31473. + goto unlock_error_exit;
  31474. +
  31475. + wmb();
  31476. +
  31477. + vchiq_log_info(vchiq_core_log_level,
  31478. + "%d: bt (%d->%d) %cx %x@%x %x",
  31479. + state->id,
  31480. + service->localport, service->remoteport, dir_char,
  31481. + size, (unsigned int)bulk->data, (unsigned int)userdata);
  31482. +
  31483. + if (state->is_master) {
  31484. + queue->local_insert++;
  31485. + if (resolve_bulks(service, queue))
  31486. + request_poll(state, service,
  31487. + (dir == VCHIQ_BULK_TRANSMIT) ?
  31488. + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
  31489. + } else {
  31490. + int payload[2] = { (int)bulk->data, bulk->size };
  31491. + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
  31492. +
  31493. + status = queue_message(state, NULL,
  31494. + VCHIQ_MAKE_MSG(dir_msgtype,
  31495. + service->localport, service->remoteport),
  31496. + &element, 1, sizeof(payload), 1);
  31497. + if (status != VCHIQ_SUCCESS) {
  31498. + vchiq_complete_bulk(bulk);
  31499. + goto unlock_error_exit;
  31500. + }
  31501. + queue->local_insert++;
  31502. + }
  31503. +
  31504. + mutex_unlock(&service->bulk_mutex);
  31505. +
  31506. + vchiq_log_trace(vchiq_core_log_level,
  31507. + "%d: bt:%d %cx li=%x ri=%x p=%x",
  31508. + state->id,
  31509. + service->localport, dir_char,
  31510. + queue->local_insert, queue->remote_insert, queue->process);
  31511. +
  31512. +waiting:
  31513. + unlock_service(service);
  31514. +
  31515. + status = VCHIQ_SUCCESS;
  31516. +
  31517. + if (bulk_waiter) {
  31518. + bulk_waiter->bulk = bulk;
  31519. + if (down_interruptible(&bulk_waiter->event) != 0)
  31520. + status = VCHIQ_RETRY;
  31521. + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
  31522. + status = VCHIQ_ERROR;
  31523. + }
  31524. +
  31525. + return status;
  31526. +
  31527. +unlock_error_exit:
  31528. + mutex_unlock(&service->bulk_mutex);
  31529. +
  31530. +error_exit:
  31531. + if (service)
  31532. + unlock_service(service);
  31533. + return status;
  31534. +}
  31535. +
  31536. +VCHIQ_STATUS_T
  31537. +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
  31538. + const VCHIQ_ELEMENT_T *elements, unsigned int count)
  31539. +{
  31540. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  31541. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  31542. +
  31543. + unsigned int size = 0;
  31544. + unsigned int i;
  31545. +
  31546. + if (!service ||
  31547. + (vchiq_check_service(service) != VCHIQ_SUCCESS))
  31548. + goto error_exit;
  31549. +
  31550. + for (i = 0; i < (unsigned int)count; i++) {
  31551. + if (elements[i].size) {
  31552. + if (elements[i].data == NULL) {
  31553. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  31554. + goto error_exit;
  31555. + }
  31556. + size += elements[i].size;
  31557. + }
  31558. + }
  31559. +
  31560. + if (size > VCHIQ_MAX_MSG_SIZE) {
  31561. + VCHIQ_SERVICE_STATS_INC(service, error_count);
  31562. + goto error_exit;
  31563. + }
  31564. +
  31565. + switch (service->srvstate) {
  31566. + case VCHIQ_SRVSTATE_OPEN:
  31567. + status = queue_message(service->state, service,
  31568. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  31569. + service->localport,
  31570. + service->remoteport),
  31571. + elements, count, size, 1);
  31572. + break;
  31573. + case VCHIQ_SRVSTATE_OPENSYNC:
  31574. + status = queue_message_sync(service->state, service,
  31575. + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
  31576. + service->localport,
  31577. + service->remoteport),
  31578. + elements, count, size, 1);
  31579. + break;
  31580. + default:
  31581. + status = VCHIQ_ERROR;
  31582. + break;
  31583. + }
  31584. +
  31585. +error_exit:
  31586. + if (service)
  31587. + unlock_service(service);
  31588. +
  31589. + return status;
  31590. +}
  31591. +
  31592. +void
  31593. +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
  31594. +{
  31595. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  31596. + VCHIQ_SHARED_STATE_T *remote;
  31597. + VCHIQ_STATE_T *state;
  31598. + int slot_index;
  31599. +
  31600. + if (!service)
  31601. + return;
  31602. +
  31603. + state = service->state;
  31604. + remote = state->remote;
  31605. +
  31606. + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
  31607. +
  31608. + if ((slot_index >= remote->slot_first) &&
  31609. + (slot_index <= remote->slot_last)) {
  31610. + int msgid = header->msgid;
  31611. + if (msgid & VCHIQ_MSGID_CLAIMED) {
  31612. + VCHIQ_SLOT_INFO_T *slot_info =
  31613. + SLOT_INFO_FROM_INDEX(state, slot_index);
  31614. +
  31615. + release_slot(state, slot_info, header, service);
  31616. + }
  31617. + } else if (slot_index == remote->slot_sync)
  31618. + release_message_sync(state, header);
  31619. +
  31620. + unlock_service(service);
  31621. +}
  31622. +
  31623. +static void
  31624. +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
  31625. +{
  31626. + header->msgid = VCHIQ_MSGID_PADDING;
  31627. + wmb();
  31628. + remote_event_signal(&state->remote->sync_release);
  31629. +}
  31630. +
  31631. +VCHIQ_STATUS_T
  31632. +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
  31633. +{
  31634. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  31635. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  31636. +
  31637. + if (!service ||
  31638. + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
  31639. + !peer_version)
  31640. + goto exit;
  31641. + *peer_version = service->peer_version;
  31642. + status = VCHIQ_SUCCESS;
  31643. +
  31644. +exit:
  31645. + if (service)
  31646. + unlock_service(service);
  31647. + return status;
  31648. +}
  31649. +
  31650. +VCHIQ_STATUS_T
  31651. +vchiq_get_config(VCHIQ_INSTANCE_T instance,
  31652. + int config_size, VCHIQ_CONFIG_T *pconfig)
  31653. +{
  31654. + VCHIQ_CONFIG_T config;
  31655. +
  31656. + (void)instance;
  31657. +
  31658. + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
  31659. + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
  31660. + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
  31661. + config.max_services = VCHIQ_MAX_SERVICES;
  31662. + config.version = VCHIQ_VERSION;
  31663. + config.version_min = VCHIQ_VERSION_MIN;
  31664. +
  31665. + if (config_size > sizeof(VCHIQ_CONFIG_T))
  31666. + return VCHIQ_ERROR;
  31667. +
  31668. + memcpy(pconfig, &config,
  31669. + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
  31670. +
  31671. + return VCHIQ_SUCCESS;
  31672. +}
  31673. +
  31674. +VCHIQ_STATUS_T
  31675. +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
  31676. + VCHIQ_SERVICE_OPTION_T option, int value)
  31677. +{
  31678. + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
  31679. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  31680. +
  31681. + if (service) {
  31682. + switch (option) {
  31683. + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
  31684. + service->auto_close = value;
  31685. + status = VCHIQ_SUCCESS;
  31686. + break;
  31687. +
  31688. + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
  31689. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  31690. + &service->state->service_quotas[
  31691. + service->localport];
  31692. + if (value == 0)
  31693. + value = service->state->default_slot_quota;
  31694. + if ((value >= service_quota->slot_use_count) &&
  31695. + (value < (unsigned short)~0)) {
  31696. + service_quota->slot_quota = value;
  31697. + if ((value >= service_quota->slot_use_count) &&
  31698. + (service_quota->message_quota >=
  31699. + service_quota->message_use_count)) {
  31700. + /* Signal the service that it may have
  31701. + ** dropped below its quota */
  31702. + up(&service_quota->quota_event);
  31703. + }
  31704. + status = VCHIQ_SUCCESS;
  31705. + }
  31706. + } break;
  31707. +
  31708. + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
  31709. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  31710. + &service->state->service_quotas[
  31711. + service->localport];
  31712. + if (value == 0)
  31713. + value = service->state->default_message_quota;
  31714. + if ((value >= service_quota->message_use_count) &&
  31715. + (value < (unsigned short)~0)) {
  31716. + service_quota->message_quota = value;
  31717. + if ((value >=
  31718. + service_quota->message_use_count) &&
  31719. + (service_quota->slot_quota >=
  31720. + service_quota->slot_use_count))
  31721. + /* Signal the service that it may have
  31722. + ** dropped below its quota */
  31723. + up(&service_quota->quota_event);
  31724. + status = VCHIQ_SUCCESS;
  31725. + }
  31726. + } break;
  31727. +
  31728. + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
  31729. + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
  31730. + (service->srvstate ==
  31731. + VCHIQ_SRVSTATE_LISTENING)) {
  31732. + service->sync = value;
  31733. + status = VCHIQ_SUCCESS;
  31734. + }
  31735. + break;
  31736. +
  31737. + default:
  31738. + break;
  31739. + }
  31740. + unlock_service(service);
  31741. + }
  31742. +
  31743. + return status;
  31744. +}
  31745. +
  31746. +void
  31747. +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
  31748. + VCHIQ_SHARED_STATE_T *shared, const char *label)
  31749. +{
  31750. + static const char *const debug_names[] = {
  31751. + "<entries>",
  31752. + "SLOT_HANDLER_COUNT",
  31753. + "SLOT_HANDLER_LINE",
  31754. + "PARSE_LINE",
  31755. + "PARSE_HEADER",
  31756. + "PARSE_MSGID",
  31757. + "AWAIT_COMPLETION_LINE",
  31758. + "DEQUEUE_MESSAGE_LINE",
  31759. + "SERVICE_CALLBACK_LINE",
  31760. + "MSG_QUEUE_FULL_COUNT",
  31761. + "COMPLETION_QUEUE_FULL_COUNT"
  31762. + };
  31763. + int i;
  31764. +
  31765. + char buf[80];
  31766. + int len;
  31767. + len = snprintf(buf, sizeof(buf),
  31768. + " %s: slots %d-%d tx_pos=%x recycle=%x",
  31769. + label, shared->slot_first, shared->slot_last,
  31770. + shared->tx_pos, shared->slot_queue_recycle);
  31771. + vchiq_dump(dump_context, buf, len + 1);
  31772. +
  31773. + len = snprintf(buf, sizeof(buf),
  31774. + " Slots claimed:");
  31775. + vchiq_dump(dump_context, buf, len + 1);
  31776. +
  31777. + for (i = shared->slot_first; i <= shared->slot_last; i++) {
  31778. + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
  31779. + if (slot_info.use_count != slot_info.release_count) {
  31780. + len = snprintf(buf, sizeof(buf),
  31781. + " %d: %d/%d", i, slot_info.use_count,
  31782. + slot_info.release_count);
  31783. + vchiq_dump(dump_context, buf, len + 1);
  31784. + }
  31785. + }
  31786. +
  31787. + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
  31788. + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
  31789. + debug_names[i], shared->debug[i], shared->debug[i]);
  31790. + vchiq_dump(dump_context, buf, len + 1);
  31791. + }
  31792. +}
  31793. +
  31794. +void
  31795. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
  31796. +{
  31797. + char buf[80];
  31798. + int len;
  31799. + int i;
  31800. +
  31801. + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
  31802. + conn_state_names[state->conn_state]);
  31803. + vchiq_dump(dump_context, buf, len + 1);
  31804. +
  31805. + len = snprintf(buf, sizeof(buf),
  31806. + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
  31807. + state->local->tx_pos,
  31808. + (uint32_t)state->tx_data +
  31809. + (state->local_tx_pos & VCHIQ_SLOT_MASK),
  31810. + state->rx_pos,
  31811. + (uint32_t)state->rx_data +
  31812. + (state->rx_pos & VCHIQ_SLOT_MASK));
  31813. + vchiq_dump(dump_context, buf, len + 1);
  31814. +
  31815. + len = snprintf(buf, sizeof(buf),
  31816. + " Version: %d (min %d)",
  31817. + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
  31818. + vchiq_dump(dump_context, buf, len + 1);
  31819. +
  31820. + if (VCHIQ_ENABLE_STATS) {
  31821. + len = snprintf(buf, sizeof(buf),
  31822. + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
  31823. + "error_count=%d",
  31824. + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
  31825. + state->stats.error_count);
  31826. + vchiq_dump(dump_context, buf, len + 1);
  31827. + }
  31828. +
  31829. + len = snprintf(buf, sizeof(buf),
  31830. + " Slots: %d available (%d data), %d recyclable, %d stalls "
  31831. + "(%d data)",
  31832. + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
  31833. + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
  31834. + state->data_quota - state->data_use_count,
  31835. + state->local->slot_queue_recycle - state->slot_queue_available,
  31836. + state->stats.slot_stalls, state->stats.data_stalls);
  31837. + vchiq_dump(dump_context, buf, len + 1);
  31838. +
  31839. + vchiq_dump_platform_state(dump_context);
  31840. +
  31841. + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
  31842. + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
  31843. +
  31844. + vchiq_dump_platform_instances(dump_context);
  31845. +
  31846. + for (i = 0; i < state->unused_service; i++) {
  31847. + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
  31848. +
  31849. + if (service) {
  31850. + vchiq_dump_service_state(dump_context, service);
  31851. + unlock_service(service);
  31852. + }
  31853. + }
  31854. +}
  31855. +
  31856. +void
  31857. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
  31858. +{
  31859. + char buf[80];
  31860. + int len;
  31861. +
  31862. + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
  31863. + service->localport, srvstate_names[service->srvstate],
  31864. + service->ref_count - 1); /*Don't include the lock just taken*/
  31865. +
  31866. + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
  31867. + char remoteport[30];
  31868. + VCHIQ_SERVICE_QUOTA_T *service_quota =
  31869. + &service->state->service_quotas[service->localport];
  31870. + int fourcc = service->base.fourcc;
  31871. + int tx_pending, rx_pending;
  31872. + if (service->remoteport != VCHIQ_PORT_FREE) {
  31873. + int len2 = snprintf(remoteport, sizeof(remoteport),
  31874. + "%d", service->remoteport);
  31875. + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
  31876. + snprintf(remoteport + len2,
  31877. + sizeof(remoteport) - len2,
  31878. + " (client %x)", service->client_id);
  31879. + } else
  31880. + strcpy(remoteport, "n/a");
  31881. +
  31882. + len += snprintf(buf + len, sizeof(buf) - len,
  31883. + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
  31884. + VCHIQ_FOURCC_AS_4CHARS(fourcc),
  31885. + remoteport,
  31886. + service_quota->message_use_count,
  31887. + service_quota->message_quota,
  31888. + service_quota->slot_use_count,
  31889. + service_quota->slot_quota);
  31890. +
  31891. + vchiq_dump(dump_context, buf, len + 1);
  31892. +
  31893. + tx_pending = service->bulk_tx.local_insert -
  31894. + service->bulk_tx.remote_insert;
  31895. +
  31896. + rx_pending = service->bulk_rx.local_insert -
  31897. + service->bulk_rx.remote_insert;
  31898. +
  31899. + len = snprintf(buf, sizeof(buf),
  31900. + " Bulk: tx_pending=%d (size %d),"
  31901. + " rx_pending=%d (size %d)",
  31902. + tx_pending,
  31903. + tx_pending ? service->bulk_tx.bulks[
  31904. + BULK_INDEX(service->bulk_tx.remove)].size : 0,
  31905. + rx_pending,
  31906. + rx_pending ? service->bulk_rx.bulks[
  31907. + BULK_INDEX(service->bulk_rx.remove)].size : 0);
  31908. +
  31909. + if (VCHIQ_ENABLE_STATS) {
  31910. + vchiq_dump(dump_context, buf, len + 1);
  31911. +
  31912. + len = snprintf(buf, sizeof(buf),
  31913. + " Ctrl: tx_count=%d, tx_bytes=%llu, "
  31914. + "rx_count=%d, rx_bytes=%llu",
  31915. + service->stats.ctrl_tx_count,
  31916. + service->stats.ctrl_tx_bytes,
  31917. + service->stats.ctrl_rx_count,
  31918. + service->stats.ctrl_rx_bytes);
  31919. + vchiq_dump(dump_context, buf, len + 1);
  31920. +
  31921. + len = snprintf(buf, sizeof(buf),
  31922. + " Bulk: tx_count=%d, tx_bytes=%llu, "
  31923. + "rx_count=%d, rx_bytes=%llu",
  31924. + service->stats.bulk_tx_count,
  31925. + service->stats.bulk_tx_bytes,
  31926. + service->stats.bulk_rx_count,
  31927. + service->stats.bulk_rx_bytes);
  31928. + vchiq_dump(dump_context, buf, len + 1);
  31929. +
  31930. + len = snprintf(buf, sizeof(buf),
  31931. + " %d quota stalls, %d slot stalls, "
  31932. + "%d bulk stalls, %d aborted, %d errors",
  31933. + service->stats.quota_stalls,
  31934. + service->stats.slot_stalls,
  31935. + service->stats.bulk_stalls,
  31936. + service->stats.bulk_aborted_count,
  31937. + service->stats.error_count);
  31938. + }
  31939. + }
  31940. +
  31941. + vchiq_dump(dump_context, buf, len + 1);
  31942. +
  31943. + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
  31944. + vchiq_dump_platform_service_state(dump_context, service);
  31945. +}
  31946. +
  31947. +
  31948. +void
  31949. +vchiq_loud_error_header(void)
  31950. +{
  31951. + vchiq_log_error(vchiq_core_log_level,
  31952. + "============================================================"
  31953. + "================");
  31954. + vchiq_log_error(vchiq_core_log_level,
  31955. + "============================================================"
  31956. + "================");
  31957. + vchiq_log_error(vchiq_core_log_level, "=====");
  31958. +}
  31959. +
  31960. +void
  31961. +vchiq_loud_error_footer(void)
  31962. +{
  31963. + vchiq_log_error(vchiq_core_log_level, "=====");
  31964. + vchiq_log_error(vchiq_core_log_level,
  31965. + "============================================================"
  31966. + "================");
  31967. + vchiq_log_error(vchiq_core_log_level,
  31968. + "============================================================"
  31969. + "================");
  31970. +}
  31971. +
  31972. +
  31973. +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
  31974. +{
  31975. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  31976. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  31977. + status = queue_message(state, NULL,
  31978. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
  31979. + NULL, 0, 0, 0);
  31980. + return status;
  31981. +}
  31982. +
  31983. +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
  31984. +{
  31985. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  31986. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  31987. + status = queue_message(state, NULL,
  31988. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
  31989. + NULL, 0, 0, 0);
  31990. + return status;
  31991. +}
  31992. +
  31993. +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
  31994. +{
  31995. + VCHIQ_STATUS_T status = VCHIQ_RETRY;
  31996. + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
  31997. + status = queue_message(state, NULL,
  31998. + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
  31999. + NULL, 0, 0, 0);
  32000. + return status;
  32001. +}
  32002. +
  32003. +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  32004. + size_t numBytes)
  32005. +{
  32006. + const uint8_t *mem = (const uint8_t *)voidMem;
  32007. + size_t offset;
  32008. + char lineBuf[100];
  32009. + char *s;
  32010. +
  32011. + while (numBytes > 0) {
  32012. + s = lineBuf;
  32013. +
  32014. + for (offset = 0; offset < 16; offset++) {
  32015. + if (offset < numBytes)
  32016. + s += snprintf(s, 4, "%02x ", mem[offset]);
  32017. + else
  32018. + s += snprintf(s, 4, " ");
  32019. + }
  32020. +
  32021. + for (offset = 0; offset < 16; offset++) {
  32022. + if (offset < numBytes) {
  32023. + uint8_t ch = mem[offset];
  32024. +
  32025. + if ((ch < ' ') || (ch > '~'))
  32026. + ch = '.';
  32027. + *s++ = (char)ch;
  32028. + }
  32029. + }
  32030. + *s++ = '\0';
  32031. +
  32032. + if ((label != NULL) && (*label != '\0'))
  32033. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  32034. + "%s: %08x: %s", label, addr, lineBuf);
  32035. + else
  32036. + vchiq_log_trace(VCHIQ_LOG_TRACE,
  32037. + "%08x: %s", addr, lineBuf);
  32038. +
  32039. + addr += 16;
  32040. + mem += 16;
  32041. + if (numBytes > 16)
  32042. + numBytes -= 16;
  32043. + else
  32044. + numBytes = 0;
  32045. + }
  32046. +}
  32047. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
  32048. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 1970-01-01 01:00:00.000000000 +0100
  32049. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h 2014-09-14 19:03:25.000000000 +0200
  32050. @@ -0,0 +1,710 @@
  32051. +/**
  32052. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  32053. + *
  32054. + * Redistribution and use in source and binary forms, with or without
  32055. + * modification, are permitted provided that the following conditions
  32056. + * are met:
  32057. + * 1. Redistributions of source code must retain the above copyright
  32058. + * notice, this list of conditions, and the following disclaimer,
  32059. + * without modification.
  32060. + * 2. Redistributions in binary form must reproduce the above copyright
  32061. + * notice, this list of conditions and the following disclaimer in the
  32062. + * documentation and/or other materials provided with the distribution.
  32063. + * 3. The names of the above-listed copyright holders may not be used
  32064. + * to endorse or promote products derived from this software without
  32065. + * specific prior written permission.
  32066. + *
  32067. + * ALTERNATIVELY, this software may be distributed under the terms of the
  32068. + * GNU General Public License ("GPL") version 2, as published by the Free
  32069. + * Software Foundation.
  32070. + *
  32071. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  32072. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  32073. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32074. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32075. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32076. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32077. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32078. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32079. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32080. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32081. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32082. + */
  32083. +
  32084. +#ifndef VCHIQ_CORE_H
  32085. +#define VCHIQ_CORE_H
  32086. +
  32087. +#include <linux/mutex.h>
  32088. +#include <linux/semaphore.h>
  32089. +#include <linux/kthread.h>
  32090. +
  32091. +#include "vchiq_cfg.h"
  32092. +
  32093. +#include "vchiq.h"
  32094. +
  32095. +/* Run time control of log level, based on KERN_XXX level. */
  32096. +#define VCHIQ_LOG_DEFAULT 4
  32097. +#define VCHIQ_LOG_ERROR 3
  32098. +#define VCHIQ_LOG_WARNING 4
  32099. +#define VCHIQ_LOG_INFO 6
  32100. +#define VCHIQ_LOG_TRACE 7
  32101. +
  32102. +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
  32103. +
  32104. +#ifndef vchiq_log_error
  32105. +#define vchiq_log_error(cat, fmt, ...) \
  32106. + do { if (cat >= VCHIQ_LOG_ERROR) \
  32107. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32108. +#endif
  32109. +#ifndef vchiq_log_warning
  32110. +#define vchiq_log_warning(cat, fmt, ...) \
  32111. + do { if (cat >= VCHIQ_LOG_WARNING) \
  32112. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32113. +#endif
  32114. +#ifndef vchiq_log_info
  32115. +#define vchiq_log_info(cat, fmt, ...) \
  32116. + do { if (cat >= VCHIQ_LOG_INFO) \
  32117. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32118. +#endif
  32119. +#ifndef vchiq_log_trace
  32120. +#define vchiq_log_trace(cat, fmt, ...) \
  32121. + do { if (cat >= VCHIQ_LOG_TRACE) \
  32122. + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
  32123. +#endif
  32124. +
  32125. +#define vchiq_loud_error(...) \
  32126. + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
  32127. +
  32128. +#ifndef vchiq_static_assert
  32129. +#define vchiq_static_assert(cond) __attribute__((unused)) \
  32130. + extern int vchiq_static_assert[(cond) ? 1 : -1]
  32131. +#endif
  32132. +
  32133. +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
  32134. +
  32135. +/* Ensure that the slot size and maximum number of slots are powers of 2 */
  32136. +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
  32137. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
  32138. +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
  32139. +
  32140. +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
  32141. +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
  32142. +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
  32143. + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
  32144. +
  32145. +#define VCHIQ_MSG_PADDING 0 /* - */
  32146. +#define VCHIQ_MSG_CONNECT 1 /* - */
  32147. +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
  32148. +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
  32149. +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
  32150. +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
  32151. +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
  32152. +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
  32153. +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
  32154. +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
  32155. +#define VCHIQ_MSG_PAUSE 10 /* - */
  32156. +#define VCHIQ_MSG_RESUME 11 /* - */
  32157. +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
  32158. +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
  32159. +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
  32160. +
  32161. +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
  32162. +#define VCHIQ_PORT_FREE 0x1000
  32163. +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
  32164. +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
  32165. + ((type<<24) | (srcport<<12) | (dstport<<0))
  32166. +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
  32167. +#define VCHIQ_MSG_SRCPORT(msgid) \
  32168. + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
  32169. +#define VCHIQ_MSG_DSTPORT(msgid) \
  32170. + ((unsigned short)msgid & 0xfff)
  32171. +
  32172. +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
  32173. + ((fourcc) >> 24) & 0xff, \
  32174. + ((fourcc) >> 16) & 0xff, \
  32175. + ((fourcc) >> 8) & 0xff, \
  32176. + (fourcc) & 0xff
  32177. +
  32178. +/* Ensure the fields are wide enough */
  32179. +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
  32180. + == 0);
  32181. +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
  32182. +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
  32183. + (unsigned int)VCHIQ_PORT_FREE);
  32184. +
  32185. +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
  32186. +#define VCHIQ_MSGID_CLAIMED 0x40000000
  32187. +
  32188. +#define VCHIQ_FOURCC_INVALID 0x00000000
  32189. +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
  32190. +
  32191. +#define VCHIQ_BULK_ACTUAL_ABORTED -1
  32192. +
  32193. +typedef uint32_t BITSET_T;
  32194. +
  32195. +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
  32196. +
  32197. +#define BITSET_SIZE(b) ((b + 31) >> 5)
  32198. +#define BITSET_WORD(b) (b >> 5)
  32199. +#define BITSET_BIT(b) (1 << (b & 31))
  32200. +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
  32201. +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
  32202. +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
  32203. +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
  32204. +
  32205. +#if VCHIQ_ENABLE_STATS
  32206. +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
  32207. +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
  32208. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
  32209. + (service->stats. stat += addend)
  32210. +#else
  32211. +#define VCHIQ_STATS_INC(state, stat) ((void)0)
  32212. +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
  32213. +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
  32214. +#endif
  32215. +
  32216. +enum {
  32217. + DEBUG_ENTRIES,
  32218. +#if VCHIQ_ENABLE_DEBUG
  32219. + DEBUG_SLOT_HANDLER_COUNT,
  32220. + DEBUG_SLOT_HANDLER_LINE,
  32221. + DEBUG_PARSE_LINE,
  32222. + DEBUG_PARSE_HEADER,
  32223. + DEBUG_PARSE_MSGID,
  32224. + DEBUG_AWAIT_COMPLETION_LINE,
  32225. + DEBUG_DEQUEUE_MESSAGE_LINE,
  32226. + DEBUG_SERVICE_CALLBACK_LINE,
  32227. + DEBUG_MSG_QUEUE_FULL_COUNT,
  32228. + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
  32229. +#endif
  32230. + DEBUG_MAX
  32231. +};
  32232. +
  32233. +#if VCHIQ_ENABLE_DEBUG
  32234. +
  32235. +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
  32236. +#define DEBUG_TRACE(d) \
  32237. + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
  32238. +#define DEBUG_VALUE(d, v) \
  32239. + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
  32240. +#define DEBUG_COUNT(d) \
  32241. + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
  32242. +
  32243. +#else /* VCHIQ_ENABLE_DEBUG */
  32244. +
  32245. +#define DEBUG_INITIALISE(local)
  32246. +#define DEBUG_TRACE(d)
  32247. +#define DEBUG_VALUE(d, v)
  32248. +#define DEBUG_COUNT(d)
  32249. +
  32250. +#endif /* VCHIQ_ENABLE_DEBUG */
  32251. +
  32252. +typedef enum {
  32253. + VCHIQ_CONNSTATE_DISCONNECTED,
  32254. + VCHIQ_CONNSTATE_CONNECTING,
  32255. + VCHIQ_CONNSTATE_CONNECTED,
  32256. + VCHIQ_CONNSTATE_PAUSING,
  32257. + VCHIQ_CONNSTATE_PAUSE_SENT,
  32258. + VCHIQ_CONNSTATE_PAUSED,
  32259. + VCHIQ_CONNSTATE_RESUMING,
  32260. + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
  32261. + VCHIQ_CONNSTATE_RESUME_TIMEOUT
  32262. +} VCHIQ_CONNSTATE_T;
  32263. +
  32264. +enum {
  32265. + VCHIQ_SRVSTATE_FREE,
  32266. + VCHIQ_SRVSTATE_HIDDEN,
  32267. + VCHIQ_SRVSTATE_LISTENING,
  32268. + VCHIQ_SRVSTATE_OPENING,
  32269. + VCHIQ_SRVSTATE_OPEN,
  32270. + VCHIQ_SRVSTATE_OPENSYNC,
  32271. + VCHIQ_SRVSTATE_CLOSESENT,
  32272. + VCHIQ_SRVSTATE_CLOSERECVD,
  32273. + VCHIQ_SRVSTATE_CLOSEWAIT,
  32274. + VCHIQ_SRVSTATE_CLOSED
  32275. +};
  32276. +
  32277. +enum {
  32278. + VCHIQ_POLL_TERMINATE,
  32279. + VCHIQ_POLL_REMOVE,
  32280. + VCHIQ_POLL_TXNOTIFY,
  32281. + VCHIQ_POLL_RXNOTIFY,
  32282. + VCHIQ_POLL_COUNT
  32283. +};
  32284. +
  32285. +typedef enum {
  32286. + VCHIQ_BULK_TRANSMIT,
  32287. + VCHIQ_BULK_RECEIVE
  32288. +} VCHIQ_BULK_DIR_T;
  32289. +
  32290. +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
  32291. +
  32292. +typedef struct vchiq_bulk_struct {
  32293. + short mode;
  32294. + short dir;
  32295. + void *userdata;
  32296. + VCHI_MEM_HANDLE_T handle;
  32297. + void *data;
  32298. + int size;
  32299. + void *remote_data;
  32300. + int remote_size;
  32301. + int actual;
  32302. +} VCHIQ_BULK_T;
  32303. +
  32304. +typedef struct vchiq_bulk_queue_struct {
  32305. + int local_insert; /* Where to insert the next local bulk */
  32306. + int remote_insert; /* Where to insert the next remote bulk (master) */
  32307. + int process; /* Bulk to transfer next */
  32308. + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
  32309. + int remove; /* Bulk to notify the local client of, and remove,
  32310. + ** next */
  32311. + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
  32312. +} VCHIQ_BULK_QUEUE_T;
  32313. +
  32314. +typedef struct remote_event_struct {
  32315. + int armed;
  32316. + int fired;
  32317. + struct semaphore *event;
  32318. +} REMOTE_EVENT_T;
  32319. +
  32320. +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
  32321. +
  32322. +typedef struct vchiq_state_struct VCHIQ_STATE_T;
  32323. +
  32324. +typedef struct vchiq_slot_struct {
  32325. + char data[VCHIQ_SLOT_SIZE];
  32326. +} VCHIQ_SLOT_T;
  32327. +
  32328. +typedef struct vchiq_slot_info_struct {
  32329. + /* Use two counters rather than one to avoid the need for a mutex. */
  32330. + short use_count;
  32331. + short release_count;
  32332. +} VCHIQ_SLOT_INFO_T;
  32333. +
  32334. +typedef struct vchiq_service_struct {
  32335. + VCHIQ_SERVICE_BASE_T base;
  32336. + VCHIQ_SERVICE_HANDLE_T handle;
  32337. + unsigned int ref_count;
  32338. + int srvstate;
  32339. + VCHIQ_USERDATA_TERM_T userdata_term;
  32340. + unsigned int localport;
  32341. + unsigned int remoteport;
  32342. + int public_fourcc;
  32343. + int client_id;
  32344. + char auto_close;
  32345. + char sync;
  32346. + char closing;
  32347. + atomic_t poll_flags;
  32348. + short version;
  32349. + short version_min;
  32350. + short peer_version;
  32351. +
  32352. + VCHIQ_STATE_T *state;
  32353. + VCHIQ_INSTANCE_T instance;
  32354. +
  32355. + int service_use_count;
  32356. +
  32357. + VCHIQ_BULK_QUEUE_T bulk_tx;
  32358. + VCHIQ_BULK_QUEUE_T bulk_rx;
  32359. +
  32360. + struct semaphore remove_event;
  32361. + struct semaphore bulk_remove_event;
  32362. + struct mutex bulk_mutex;
  32363. +
  32364. + struct service_stats_struct {
  32365. + int quota_stalls;
  32366. + int slot_stalls;
  32367. + int bulk_stalls;
  32368. + int error_count;
  32369. + int ctrl_tx_count;
  32370. + int ctrl_rx_count;
  32371. + int bulk_tx_count;
  32372. + int bulk_rx_count;
  32373. + int bulk_aborted_count;
  32374. + uint64_t ctrl_tx_bytes;
  32375. + uint64_t ctrl_rx_bytes;
  32376. + uint64_t bulk_tx_bytes;
  32377. + uint64_t bulk_rx_bytes;
  32378. + } stats;
  32379. +} VCHIQ_SERVICE_T;
  32380. +
  32381. +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
  32382. + statically allocated, since for accounting reasons a service's slot
  32383. + usage is carried over between users of the same port number.
  32384. + */
  32385. +typedef struct vchiq_service_quota_struct {
  32386. + unsigned short slot_quota;
  32387. + unsigned short slot_use_count;
  32388. + unsigned short message_quota;
  32389. + unsigned short message_use_count;
  32390. + struct semaphore quota_event;
  32391. + int previous_tx_index;
  32392. +} VCHIQ_SERVICE_QUOTA_T;
  32393. +
  32394. +typedef struct vchiq_shared_state_struct {
  32395. +
  32396. + /* A non-zero value here indicates that the content is valid. */
  32397. + int initialised;
  32398. +
  32399. + /* The first and last (inclusive) slots allocated to the owner. */
  32400. + int slot_first;
  32401. + int slot_last;
  32402. +
  32403. + /* The slot allocated to synchronous messages from the owner. */
  32404. + int slot_sync;
  32405. +
  32406. + /* Signalling this event indicates that owner's slot handler thread
  32407. + ** should run. */
  32408. + REMOTE_EVENT_T trigger;
  32409. +
  32410. + /* Indicates the byte position within the stream where the next message
  32411. + ** will be written. The least significant bits are an index into the
  32412. + ** slot. The next bits are the index of the slot in slot_queue. */
  32413. + int tx_pos;
  32414. +
  32415. + /* This event should be signalled when a slot is recycled. */
  32416. + REMOTE_EVENT_T recycle;
  32417. +
  32418. + /* The slot_queue index where the next recycled slot will be written. */
  32419. + int slot_queue_recycle;
  32420. +
  32421. + /* This event should be signalled when a synchronous message is sent. */
  32422. + REMOTE_EVENT_T sync_trigger;
  32423. +
  32424. + /* This event should be signalled when a synchronous message has been
  32425. + ** released. */
  32426. + REMOTE_EVENT_T sync_release;
  32427. +
  32428. + /* A circular buffer of slot indexes. */
  32429. + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
  32430. +
  32431. + /* Debugging state */
  32432. + int debug[DEBUG_MAX];
  32433. +} VCHIQ_SHARED_STATE_T;
  32434. +
  32435. +typedef struct vchiq_slot_zero_struct {
  32436. + int magic;
  32437. + short version;
  32438. + short version_min;
  32439. + int slot_zero_size;
  32440. + int slot_size;
  32441. + int max_slots;
  32442. + int max_slots_per_side;
  32443. + int platform_data[2];
  32444. + VCHIQ_SHARED_STATE_T master;
  32445. + VCHIQ_SHARED_STATE_T slave;
  32446. + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
  32447. +} VCHIQ_SLOT_ZERO_T;
  32448. +
  32449. +struct vchiq_state_struct {
  32450. + int id;
  32451. + int initialised;
  32452. + VCHIQ_CONNSTATE_T conn_state;
  32453. + int is_master;
  32454. +
  32455. + VCHIQ_SHARED_STATE_T *local;
  32456. + VCHIQ_SHARED_STATE_T *remote;
  32457. + VCHIQ_SLOT_T *slot_data;
  32458. +
  32459. + unsigned short default_slot_quota;
  32460. + unsigned short default_message_quota;
  32461. +
  32462. + /* Event indicating connect message received */
  32463. + struct semaphore connect;
  32464. +
  32465. + /* Mutex protecting services */
  32466. + struct mutex mutex;
  32467. + VCHIQ_INSTANCE_T *instance;
  32468. +
  32469. + /* Processes incoming messages */
  32470. + struct task_struct *slot_handler_thread;
  32471. +
  32472. + /* Processes recycled slots */
  32473. + struct task_struct *recycle_thread;
  32474. +
  32475. + /* Processes synchronous messages */
  32476. + struct task_struct *sync_thread;
  32477. +
  32478. + /* Local implementation of the trigger remote event */
  32479. + struct semaphore trigger_event;
  32480. +
  32481. + /* Local implementation of the recycle remote event */
  32482. + struct semaphore recycle_event;
  32483. +
  32484. + /* Local implementation of the sync trigger remote event */
  32485. + struct semaphore sync_trigger_event;
  32486. +
  32487. + /* Local implementation of the sync release remote event */
  32488. + struct semaphore sync_release_event;
  32489. +
  32490. + char *tx_data;
  32491. + char *rx_data;
  32492. + VCHIQ_SLOT_INFO_T *rx_info;
  32493. +
  32494. + struct mutex slot_mutex;
  32495. +
  32496. + struct mutex recycle_mutex;
  32497. +
  32498. + struct mutex sync_mutex;
  32499. +
  32500. + struct mutex bulk_transfer_mutex;
  32501. +
  32502. + /* Indicates the byte position within the stream from where the next
  32503. + ** message will be read. The least significant bits are an index into
  32504. + ** the slot.The next bits are the index of the slot in
  32505. + ** remote->slot_queue. */
  32506. + int rx_pos;
  32507. +
  32508. + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
  32509. + from remote->tx_pos. */
  32510. + int local_tx_pos;
  32511. +
  32512. + /* The slot_queue index of the slot to become available next. */
  32513. + int slot_queue_available;
  32514. +
  32515. + /* A flag to indicate if any poll has been requested */
  32516. + int poll_needed;
  32517. +
  32518. + /* Ths index of the previous slot used for data messages. */
  32519. + int previous_data_index;
  32520. +
  32521. + /* The number of slots occupied by data messages. */
  32522. + unsigned short data_use_count;
  32523. +
  32524. + /* The maximum number of slots to be occupied by data messages. */
  32525. + unsigned short data_quota;
  32526. +
  32527. + /* An array of bit sets indicating which services must be polled. */
  32528. + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
  32529. +
  32530. + /* The number of the first unused service */
  32531. + int unused_service;
  32532. +
  32533. + /* Signalled when a free slot becomes available. */
  32534. + struct semaphore slot_available_event;
  32535. +
  32536. + struct semaphore slot_remove_event;
  32537. +
  32538. + /* Signalled when a free data slot becomes available. */
  32539. + struct semaphore data_quota_event;
  32540. +
  32541. + /* Incremented when there are bulk transfers which cannot be processed
  32542. + * whilst paused and must be processed on resume */
  32543. + int deferred_bulks;
  32544. +
  32545. + struct state_stats_struct {
  32546. + int slot_stalls;
  32547. + int data_stalls;
  32548. + int ctrl_tx_count;
  32549. + int ctrl_rx_count;
  32550. + int error_count;
  32551. + } stats;
  32552. +
  32553. + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
  32554. + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
  32555. + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
  32556. +
  32557. + VCHIQ_PLATFORM_STATE_T platform_state;
  32558. +};
  32559. +
  32560. +struct bulk_waiter {
  32561. + VCHIQ_BULK_T *bulk;
  32562. + struct semaphore event;
  32563. + int actual;
  32564. +};
  32565. +
  32566. +extern spinlock_t bulk_waiter_spinlock;
  32567. +
  32568. +extern int vchiq_core_log_level;
  32569. +extern int vchiq_core_msg_log_level;
  32570. +extern int vchiq_sync_log_level;
  32571. +
  32572. +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
  32573. +
  32574. +extern const char *
  32575. +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
  32576. +
  32577. +extern VCHIQ_SLOT_ZERO_T *
  32578. +vchiq_init_slots(void *mem_base, int mem_size);
  32579. +
  32580. +extern VCHIQ_STATUS_T
  32581. +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
  32582. + int is_master);
  32583. +
  32584. +extern VCHIQ_STATUS_T
  32585. +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  32586. +
  32587. +extern VCHIQ_SERVICE_T *
  32588. +vchiq_add_service_internal(VCHIQ_STATE_T *state,
  32589. + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
  32590. + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
  32591. +
  32592. +extern VCHIQ_STATUS_T
  32593. +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
  32594. +
  32595. +extern VCHIQ_STATUS_T
  32596. +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
  32597. +
  32598. +extern void
  32599. +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
  32600. +
  32601. +extern void
  32602. +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
  32603. +
  32604. +extern VCHIQ_STATUS_T
  32605. +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
  32606. +
  32607. +extern VCHIQ_STATUS_T
  32608. +vchiq_pause_internal(VCHIQ_STATE_T *state);
  32609. +
  32610. +extern VCHIQ_STATUS_T
  32611. +vchiq_resume_internal(VCHIQ_STATE_T *state);
  32612. +
  32613. +extern void
  32614. +remote_event_pollall(VCHIQ_STATE_T *state);
  32615. +
  32616. +extern VCHIQ_STATUS_T
  32617. +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
  32618. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
  32619. + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
  32620. +
  32621. +extern void
  32622. +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
  32623. +
  32624. +extern void
  32625. +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
  32626. +
  32627. +extern void
  32628. +vchiq_loud_error_header(void);
  32629. +
  32630. +extern void
  32631. +vchiq_loud_error_footer(void);
  32632. +
  32633. +extern void
  32634. +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
  32635. +
  32636. +static inline VCHIQ_SERVICE_T *
  32637. +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
  32638. +{
  32639. + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
  32640. + (VCHIQ_MAX_STATES - 1)];
  32641. + if (!state)
  32642. + return NULL;
  32643. +
  32644. + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
  32645. +}
  32646. +
  32647. +extern VCHIQ_SERVICE_T *
  32648. +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
  32649. +
  32650. +extern VCHIQ_SERVICE_T *
  32651. +find_service_by_port(VCHIQ_STATE_T *state, int localport);
  32652. +
  32653. +extern VCHIQ_SERVICE_T *
  32654. +find_service_for_instance(VCHIQ_INSTANCE_T instance,
  32655. + VCHIQ_SERVICE_HANDLE_T handle);
  32656. +
  32657. +extern VCHIQ_SERVICE_T *
  32658. +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
  32659. + VCHIQ_SERVICE_HANDLE_T handle);
  32660. +
  32661. +extern VCHIQ_SERVICE_T *
  32662. +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
  32663. + int *pidx);
  32664. +
  32665. +extern void
  32666. +lock_service(VCHIQ_SERVICE_T *service);
  32667. +
  32668. +extern void
  32669. +unlock_service(VCHIQ_SERVICE_T *service);
  32670. +
  32671. +/* The following functions are called from vchiq_core, and external
  32672. +** implementations must be provided. */
  32673. +
  32674. +extern VCHIQ_STATUS_T
  32675. +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
  32676. + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
  32677. +
  32678. +extern void
  32679. +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
  32680. +
  32681. +extern void
  32682. +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
  32683. +
  32684. +extern VCHIQ_STATUS_T
  32685. +vchiq_copy_from_user(void *dst, const void *src, int size);
  32686. +
  32687. +extern void
  32688. +remote_event_signal(REMOTE_EVENT_T *event);
  32689. +
  32690. +void
  32691. +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
  32692. +
  32693. +extern void
  32694. +vchiq_platform_paused(VCHIQ_STATE_T *state);
  32695. +
  32696. +extern VCHIQ_STATUS_T
  32697. +vchiq_platform_resume(VCHIQ_STATE_T *state);
  32698. +
  32699. +extern void
  32700. +vchiq_platform_resumed(VCHIQ_STATE_T *state);
  32701. +
  32702. +extern void
  32703. +vchiq_dump(void *dump_context, const char *str, int len);
  32704. +
  32705. +extern void
  32706. +vchiq_dump_platform_state(void *dump_context);
  32707. +
  32708. +extern void
  32709. +vchiq_dump_platform_instances(void *dump_context);
  32710. +
  32711. +extern void
  32712. +vchiq_dump_platform_service_state(void *dump_context,
  32713. + VCHIQ_SERVICE_T *service);
  32714. +
  32715. +extern VCHIQ_STATUS_T
  32716. +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
  32717. +
  32718. +extern VCHIQ_STATUS_T
  32719. +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
  32720. +
  32721. +extern void
  32722. +vchiq_on_remote_use(VCHIQ_STATE_T *state);
  32723. +
  32724. +extern void
  32725. +vchiq_on_remote_release(VCHIQ_STATE_T *state);
  32726. +
  32727. +extern VCHIQ_STATUS_T
  32728. +vchiq_platform_init_state(VCHIQ_STATE_T *state);
  32729. +
  32730. +extern VCHIQ_STATUS_T
  32731. +vchiq_check_service(VCHIQ_SERVICE_T *service);
  32732. +
  32733. +extern void
  32734. +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
  32735. +
  32736. +extern VCHIQ_STATUS_T
  32737. +vchiq_send_remote_use(VCHIQ_STATE_T *state);
  32738. +
  32739. +extern VCHIQ_STATUS_T
  32740. +vchiq_send_remote_release(VCHIQ_STATE_T *state);
  32741. +
  32742. +extern VCHIQ_STATUS_T
  32743. +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
  32744. +
  32745. +extern void
  32746. +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
  32747. + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
  32748. +
  32749. +extern void
  32750. +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
  32751. +
  32752. +extern void
  32753. +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
  32754. +
  32755. +
  32756. +extern void
  32757. +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
  32758. + size_t numBytes);
  32759. +
  32760. +#endif
  32761. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
  32762. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 1970-01-01 01:00:00.000000000 +0100
  32763. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion 2014-09-14 19:03:25.000000000 +0200
  32764. @@ -0,0 +1,87 @@
  32765. +#!/usr/bin/perl -w
  32766. +
  32767. +use strict;
  32768. +
  32769. +#
  32770. +# Generate a version from available information
  32771. +#
  32772. +
  32773. +my $prefix = shift @ARGV;
  32774. +my $root = shift @ARGV;
  32775. +
  32776. +
  32777. +if ( not defined $root ) {
  32778. + die "usage: $0 prefix root-dir\n";
  32779. +}
  32780. +
  32781. +if ( ! -d $root ) {
  32782. + die "root directory $root not found\n";
  32783. +}
  32784. +
  32785. +my $version = "unknown";
  32786. +my $tainted = "";
  32787. +
  32788. +if ( -d "$root/.git" ) {
  32789. + # attempt to work out git version. only do so
  32790. + # on a linux build host, as cygwin builds are
  32791. + # already slow enough
  32792. +
  32793. + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
  32794. + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
  32795. + $version = "no git version";
  32796. + }
  32797. + else {
  32798. + $version = <F>;
  32799. + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  32800. + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  32801. + }
  32802. +
  32803. + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
  32804. + $tainted = <G>;
  32805. + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  32806. + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  32807. + if (length $tainted) {
  32808. + $version = join ' ', $version, "(tainted)";
  32809. + }
  32810. + else {
  32811. + $version = join ' ', $version, "(clean)";
  32812. + }
  32813. + }
  32814. + }
  32815. +}
  32816. +
  32817. +my $hostname = `hostname`;
  32818. +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
  32819. +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
  32820. +
  32821. +
  32822. +print STDERR "Version $version\n";
  32823. +print <<EOF;
  32824. +#include "${prefix}_build_info.h"
  32825. +#include <linux/broadcom/vc_debug_sym.h>
  32826. +
  32827. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
  32828. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
  32829. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
  32830. +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
  32831. +
  32832. +const char *vchiq_get_build_hostname( void )
  32833. +{
  32834. + return vchiq_build_hostname;
  32835. +}
  32836. +
  32837. +const char *vchiq_get_build_version( void )
  32838. +{
  32839. + return vchiq_build_version;
  32840. +}
  32841. +
  32842. +const char *vchiq_get_build_date( void )
  32843. +{
  32844. + return vchiq_build_date;
  32845. +}
  32846. +
  32847. +const char *vchiq_get_build_time( void )
  32848. +{
  32849. + return vchiq_build_time;
  32850. +}
  32851. +EOF
  32852. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
  32853. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 1970-01-01 01:00:00.000000000 +0100
  32854. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h 2014-09-14 19:03:25.000000000 +0200
  32855. @@ -0,0 +1,40 @@
  32856. +/**
  32857. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  32858. + *
  32859. + * Redistribution and use in source and binary forms, with or without
  32860. + * modification, are permitted provided that the following conditions
  32861. + * are met:
  32862. + * 1. Redistributions of source code must retain the above copyright
  32863. + * notice, this list of conditions, and the following disclaimer,
  32864. + * without modification.
  32865. + * 2. Redistributions in binary form must reproduce the above copyright
  32866. + * notice, this list of conditions and the following disclaimer in the
  32867. + * documentation and/or other materials provided with the distribution.
  32868. + * 3. The names of the above-listed copyright holders may not be used
  32869. + * to endorse or promote products derived from this software without
  32870. + * specific prior written permission.
  32871. + *
  32872. + * ALTERNATIVELY, this software may be distributed under the terms of the
  32873. + * GNU General Public License ("GPL") version 2, as published by the Free
  32874. + * Software Foundation.
  32875. + *
  32876. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  32877. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  32878. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32879. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32880. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32881. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32882. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32883. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32884. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32885. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32886. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32887. + */
  32888. +
  32889. +#ifndef VCHIQ_VCHIQ_H
  32890. +#define VCHIQ_VCHIQ_H
  32891. +
  32892. +#include "vchiq_if.h"
  32893. +#include "vchiq_util.h"
  32894. +
  32895. +#endif
  32896. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
  32897. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 1970-01-01 01:00:00.000000000 +0100
  32898. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h 2014-04-13 17:32:57.000000000 +0200
  32899. @@ -0,0 +1,188 @@
  32900. +/**
  32901. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  32902. + *
  32903. + * Redistribution and use in source and binary forms, with or without
  32904. + * modification, are permitted provided that the following conditions
  32905. + * are met:
  32906. + * 1. Redistributions of source code must retain the above copyright
  32907. + * notice, this list of conditions, and the following disclaimer,
  32908. + * without modification.
  32909. + * 2. Redistributions in binary form must reproduce the above copyright
  32910. + * notice, this list of conditions and the following disclaimer in the
  32911. + * documentation and/or other materials provided with the distribution.
  32912. + * 3. The names of the above-listed copyright holders may not be used
  32913. + * to endorse or promote products derived from this software without
  32914. + * specific prior written permission.
  32915. + *
  32916. + * ALTERNATIVELY, this software may be distributed under the terms of the
  32917. + * GNU General Public License ("GPL") version 2, as published by the Free
  32918. + * Software Foundation.
  32919. + *
  32920. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  32921. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  32922. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  32923. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  32924. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  32925. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  32926. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32927. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32928. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32929. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32930. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32931. + */
  32932. +
  32933. +#ifndef VCHIQ_IF_H
  32934. +#define VCHIQ_IF_H
  32935. +
  32936. +#include "interface/vchi/vchi_mh.h"
  32937. +
  32938. +#define VCHIQ_SERVICE_HANDLE_INVALID 0
  32939. +
  32940. +#define VCHIQ_SLOT_SIZE 4096
  32941. +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
  32942. +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
  32943. +
  32944. +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
  32945. + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
  32946. +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
  32947. +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
  32948. +
  32949. +typedef enum {
  32950. + VCHIQ_SERVICE_OPENED, /* service, -, - */
  32951. + VCHIQ_SERVICE_CLOSED, /* service, -, - */
  32952. + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
  32953. + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
  32954. + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
  32955. + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
  32956. + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
  32957. +} VCHIQ_REASON_T;
  32958. +
  32959. +typedef enum {
  32960. + VCHIQ_ERROR = -1,
  32961. + VCHIQ_SUCCESS = 0,
  32962. + VCHIQ_RETRY = 1
  32963. +} VCHIQ_STATUS_T;
  32964. +
  32965. +typedef enum {
  32966. + VCHIQ_BULK_MODE_CALLBACK,
  32967. + VCHIQ_BULK_MODE_BLOCKING,
  32968. + VCHIQ_BULK_MODE_NOCALLBACK,
  32969. + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
  32970. +} VCHIQ_BULK_MODE_T;
  32971. +
  32972. +typedef enum {
  32973. + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
  32974. + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
  32975. + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
  32976. + VCHIQ_SERVICE_OPTION_SYNCHRONOUS
  32977. +} VCHIQ_SERVICE_OPTION_T;
  32978. +
  32979. +typedef struct vchiq_header_struct {
  32980. + /* The message identifier - opaque to applications. */
  32981. + int msgid;
  32982. +
  32983. + /* Size of message data. */
  32984. + unsigned int size;
  32985. +
  32986. + char data[0]; /* message */
  32987. +} VCHIQ_HEADER_T;
  32988. +
  32989. +typedef struct {
  32990. + const void *data;
  32991. + unsigned int size;
  32992. +} VCHIQ_ELEMENT_T;
  32993. +
  32994. +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
  32995. +
  32996. +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
  32997. + VCHIQ_SERVICE_HANDLE_T, void *);
  32998. +
  32999. +typedef struct vchiq_service_base_struct {
  33000. + int fourcc;
  33001. + VCHIQ_CALLBACK_T callback;
  33002. + void *userdata;
  33003. +} VCHIQ_SERVICE_BASE_T;
  33004. +
  33005. +typedef struct vchiq_service_params_struct {
  33006. + int fourcc;
  33007. + VCHIQ_CALLBACK_T callback;
  33008. + void *userdata;
  33009. + short version; /* Increment for non-trivial changes */
  33010. + short version_min; /* Update for incompatible changes */
  33011. +} VCHIQ_SERVICE_PARAMS_T;
  33012. +
  33013. +typedef struct vchiq_config_struct {
  33014. + unsigned int max_msg_size;
  33015. + unsigned int bulk_threshold; /* The message size above which it
  33016. + is better to use a bulk transfer
  33017. + (<= max_msg_size) */
  33018. + unsigned int max_outstanding_bulks;
  33019. + unsigned int max_services;
  33020. + short version; /* The version of VCHIQ */
  33021. + short version_min; /* The minimum compatible version of VCHIQ */
  33022. +} VCHIQ_CONFIG_T;
  33023. +
  33024. +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
  33025. +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
  33026. +
  33027. +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
  33028. +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
  33029. +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
  33030. +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
  33031. + const VCHIQ_SERVICE_PARAMS_T *params,
  33032. + VCHIQ_SERVICE_HANDLE_T *pservice);
  33033. +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
  33034. + const VCHIQ_SERVICE_PARAMS_T *params,
  33035. + VCHIQ_SERVICE_HANDLE_T *pservice);
  33036. +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
  33037. +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
  33038. +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
  33039. +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
  33040. + VCHIQ_SERVICE_HANDLE_T service);
  33041. +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
  33042. +
  33043. +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
  33044. + const VCHIQ_ELEMENT_T *elements, unsigned int count);
  33045. +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
  33046. + VCHIQ_HEADER_T *header);
  33047. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  33048. + const void *data, unsigned int size, void *userdata);
  33049. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  33050. + void *data, unsigned int size, void *userdata);
  33051. +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
  33052. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  33053. + const void *offset, unsigned int size, void *userdata);
  33054. +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
  33055. + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
  33056. + void *offset, unsigned int size, void *userdata);
  33057. +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
  33058. + const void *data, unsigned int size, void *userdata,
  33059. + VCHIQ_BULK_MODE_T mode);
  33060. +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
  33061. + void *data, unsigned int size, void *userdata,
  33062. + VCHIQ_BULK_MODE_T mode);
  33063. +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
  33064. + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
  33065. + void *userdata, VCHIQ_BULK_MODE_T mode);
  33066. +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
  33067. + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
  33068. + void *userdata, VCHIQ_BULK_MODE_T mode);
  33069. +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
  33070. +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
  33071. +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
  33072. +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
  33073. + int config_size, VCHIQ_CONFIG_T *pconfig);
  33074. +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
  33075. + VCHIQ_SERVICE_OPTION_T option, int value);
  33076. +
  33077. +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
  33078. + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
  33079. +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
  33080. +
  33081. +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
  33082. + void *ptr, size_t num_bytes);
  33083. +
  33084. +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
  33085. + short *peer_version);
  33086. +
  33087. +#endif /* VCHIQ_IF_H */
  33088. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
  33089. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 1970-01-01 01:00:00.000000000 +0100
  33090. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h 2014-09-14 19:03:25.000000000 +0200
  33091. @@ -0,0 +1,131 @@
  33092. +/**
  33093. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33094. + *
  33095. + * Redistribution and use in source and binary forms, with or without
  33096. + * modification, are permitted provided that the following conditions
  33097. + * are met:
  33098. + * 1. Redistributions of source code must retain the above copyright
  33099. + * notice, this list of conditions, and the following disclaimer,
  33100. + * without modification.
  33101. + * 2. Redistributions in binary form must reproduce the above copyright
  33102. + * notice, this list of conditions and the following disclaimer in the
  33103. + * documentation and/or other materials provided with the distribution.
  33104. + * 3. The names of the above-listed copyright holders may not be used
  33105. + * to endorse or promote products derived from this software without
  33106. + * specific prior written permission.
  33107. + *
  33108. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33109. + * GNU General Public License ("GPL") version 2, as published by the Free
  33110. + * Software Foundation.
  33111. + *
  33112. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33113. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33114. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33115. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33116. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33117. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33118. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33119. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33120. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33121. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33122. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33123. + */
  33124. +
  33125. +#ifndef VCHIQ_IOCTLS_H
  33126. +#define VCHIQ_IOCTLS_H
  33127. +
  33128. +#include <linux/ioctl.h>
  33129. +#include "vchiq_if.h"
  33130. +
  33131. +#define VCHIQ_IOC_MAGIC 0xc4
  33132. +#define VCHIQ_INVALID_HANDLE (~0)
  33133. +
  33134. +typedef struct {
  33135. + VCHIQ_SERVICE_PARAMS_T params;
  33136. + int is_open;
  33137. + int is_vchi;
  33138. + unsigned int handle; /* OUT */
  33139. +} VCHIQ_CREATE_SERVICE_T;
  33140. +
  33141. +typedef struct {
  33142. + unsigned int handle;
  33143. + unsigned int count;
  33144. + const VCHIQ_ELEMENT_T *elements;
  33145. +} VCHIQ_QUEUE_MESSAGE_T;
  33146. +
  33147. +typedef struct {
  33148. + unsigned int handle;
  33149. + void *data;
  33150. + unsigned int size;
  33151. + void *userdata;
  33152. + VCHIQ_BULK_MODE_T mode;
  33153. +} VCHIQ_QUEUE_BULK_TRANSFER_T;
  33154. +
  33155. +typedef struct {
  33156. + VCHIQ_REASON_T reason;
  33157. + VCHIQ_HEADER_T *header;
  33158. + void *service_userdata;
  33159. + void *bulk_userdata;
  33160. +} VCHIQ_COMPLETION_DATA_T;
  33161. +
  33162. +typedef struct {
  33163. + unsigned int count;
  33164. + VCHIQ_COMPLETION_DATA_T *buf;
  33165. + unsigned int msgbufsize;
  33166. + unsigned int msgbufcount; /* IN/OUT */
  33167. + void **msgbufs;
  33168. +} VCHIQ_AWAIT_COMPLETION_T;
  33169. +
  33170. +typedef struct {
  33171. + unsigned int handle;
  33172. + int blocking;
  33173. + unsigned int bufsize;
  33174. + void *buf;
  33175. +} VCHIQ_DEQUEUE_MESSAGE_T;
  33176. +
  33177. +typedef struct {
  33178. + unsigned int config_size;
  33179. + VCHIQ_CONFIG_T *pconfig;
  33180. +} VCHIQ_GET_CONFIG_T;
  33181. +
  33182. +typedef struct {
  33183. + unsigned int handle;
  33184. + VCHIQ_SERVICE_OPTION_T option;
  33185. + int value;
  33186. +} VCHIQ_SET_SERVICE_OPTION_T;
  33187. +
  33188. +typedef struct {
  33189. + void *virt_addr;
  33190. + size_t num_bytes;
  33191. +} VCHIQ_DUMP_MEM_T;
  33192. +
  33193. +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
  33194. +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
  33195. +#define VCHIQ_IOC_CREATE_SERVICE \
  33196. + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
  33197. +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
  33198. +#define VCHIQ_IOC_QUEUE_MESSAGE \
  33199. + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
  33200. +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
  33201. + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
  33202. +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
  33203. + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
  33204. +#define VCHIQ_IOC_AWAIT_COMPLETION \
  33205. + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
  33206. +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
  33207. + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
  33208. +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
  33209. +#define VCHIQ_IOC_GET_CONFIG \
  33210. + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
  33211. +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
  33212. +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
  33213. +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
  33214. +#define VCHIQ_IOC_SET_SERVICE_OPTION \
  33215. + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
  33216. +#define VCHIQ_IOC_DUMP_PHYS_MEM \
  33217. + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
  33218. +#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
  33219. +#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
  33220. +#define VCHIQ_IOC_MAX 17
  33221. +
  33222. +#endif
  33223. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
  33224. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 1970-01-01 01:00:00.000000000 +0100
  33225. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c 2014-09-14 19:03:25.000000000 +0200
  33226. @@ -0,0 +1,458 @@
  33227. +/**
  33228. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33229. + *
  33230. + * Redistribution and use in source and binary forms, with or without
  33231. + * modification, are permitted provided that the following conditions
  33232. + * are met:
  33233. + * 1. Redistributions of source code must retain the above copyright
  33234. + * notice, this list of conditions, and the following disclaimer,
  33235. + * without modification.
  33236. + * 2. Redistributions in binary form must reproduce the above copyright
  33237. + * notice, this list of conditions and the following disclaimer in the
  33238. + * documentation and/or other materials provided with the distribution.
  33239. + * 3. The names of the above-listed copyright holders may not be used
  33240. + * to endorse or promote products derived from this software without
  33241. + * specific prior written permission.
  33242. + *
  33243. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33244. + * GNU General Public License ("GPL") version 2, as published by the Free
  33245. + * Software Foundation.
  33246. + *
  33247. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33248. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33249. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33250. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33251. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33252. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33253. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33254. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33255. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33256. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33257. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33258. + */
  33259. +
  33260. +/* ---- Include Files ---------------------------------------------------- */
  33261. +
  33262. +#include <linux/kernel.h>
  33263. +#include <linux/module.h>
  33264. +#include <linux/mutex.h>
  33265. +
  33266. +#include "vchiq_core.h"
  33267. +#include "vchiq_arm.h"
  33268. +#include "vchiq_killable.h"
  33269. +
  33270. +/* ---- Public Variables ------------------------------------------------- */
  33271. +
  33272. +/* ---- Private Constants and Types -------------------------------------- */
  33273. +
  33274. +struct bulk_waiter_node {
  33275. + struct bulk_waiter bulk_waiter;
  33276. + int pid;
  33277. + struct list_head list;
  33278. +};
  33279. +
  33280. +struct vchiq_instance_struct {
  33281. + VCHIQ_STATE_T *state;
  33282. +
  33283. + int connected;
  33284. +
  33285. + struct list_head bulk_waiter_list;
  33286. + struct mutex bulk_waiter_list_mutex;
  33287. +};
  33288. +
  33289. +static VCHIQ_STATUS_T
  33290. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  33291. + unsigned int size, VCHIQ_BULK_DIR_T dir);
  33292. +
  33293. +/****************************************************************************
  33294. +*
  33295. +* vchiq_initialise
  33296. +*
  33297. +***************************************************************************/
  33298. +#define VCHIQ_INIT_RETRIES 10
  33299. +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
  33300. +{
  33301. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  33302. + VCHIQ_STATE_T *state;
  33303. + VCHIQ_INSTANCE_T instance = NULL;
  33304. + int i;
  33305. +
  33306. + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
  33307. +
  33308. + /* VideoCore may not be ready due to boot up timing.
  33309. + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
  33310. + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
  33311. + state = vchiq_get_state();
  33312. + if (state)
  33313. + break;
  33314. + udelay(500);
  33315. + }
  33316. + if (i==VCHIQ_INIT_RETRIES) {
  33317. + vchiq_log_error(vchiq_core_log_level,
  33318. + "%s: videocore not initialized\n", __func__);
  33319. + goto failed;
  33320. + } else if (i>0) {
  33321. + vchiq_log_warning(vchiq_core_log_level,
  33322. + "%s: videocore initialized after %d retries\n", __func__, i);
  33323. + }
  33324. +
  33325. + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
  33326. + if (!instance) {
  33327. + vchiq_log_error(vchiq_core_log_level,
  33328. + "%s: error allocating vchiq instance\n", __func__);
  33329. + goto failed;
  33330. + }
  33331. +
  33332. + instance->connected = 0;
  33333. + instance->state = state;
  33334. + mutex_init(&instance->bulk_waiter_list_mutex);
  33335. + INIT_LIST_HEAD(&instance->bulk_waiter_list);
  33336. +
  33337. + *instanceOut = instance;
  33338. +
  33339. + status = VCHIQ_SUCCESS;
  33340. +
  33341. +failed:
  33342. + vchiq_log_trace(vchiq_core_log_level,
  33343. + "%s(%p): returning %d", __func__, instance, status);
  33344. +
  33345. + return status;
  33346. +}
  33347. +EXPORT_SYMBOL(vchiq_initialise);
  33348. +
  33349. +/****************************************************************************
  33350. +*
  33351. +* vchiq_shutdown
  33352. +*
  33353. +***************************************************************************/
  33354. +
  33355. +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
  33356. +{
  33357. + VCHIQ_STATUS_T status;
  33358. + VCHIQ_STATE_T *state = instance->state;
  33359. +
  33360. + vchiq_log_trace(vchiq_core_log_level,
  33361. + "%s(%p) called", __func__, instance);
  33362. +
  33363. + if (mutex_lock_interruptible(&state->mutex) != 0)
  33364. + return VCHIQ_RETRY;
  33365. +
  33366. + /* Remove all services */
  33367. + status = vchiq_shutdown_internal(state, instance);
  33368. +
  33369. + mutex_unlock(&state->mutex);
  33370. +
  33371. + vchiq_log_trace(vchiq_core_log_level,
  33372. + "%s(%p): returning %d", __func__, instance, status);
  33373. +
  33374. + if (status == VCHIQ_SUCCESS) {
  33375. + struct list_head *pos, *next;
  33376. + list_for_each_safe(pos, next,
  33377. + &instance->bulk_waiter_list) {
  33378. + struct bulk_waiter_node *waiter;
  33379. + waiter = list_entry(pos,
  33380. + struct bulk_waiter_node,
  33381. + list);
  33382. + list_del(pos);
  33383. + vchiq_log_info(vchiq_arm_log_level,
  33384. + "bulk_waiter - cleaned up %x "
  33385. + "for pid %d",
  33386. + (unsigned int)waiter, waiter->pid);
  33387. + kfree(waiter);
  33388. + }
  33389. + kfree(instance);
  33390. + }
  33391. +
  33392. + return status;
  33393. +}
  33394. +EXPORT_SYMBOL(vchiq_shutdown);
  33395. +
  33396. +/****************************************************************************
  33397. +*
  33398. +* vchiq_is_connected
  33399. +*
  33400. +***************************************************************************/
  33401. +
  33402. +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
  33403. +{
  33404. + return instance->connected;
  33405. +}
  33406. +
  33407. +/****************************************************************************
  33408. +*
  33409. +* vchiq_connect
  33410. +*
  33411. +***************************************************************************/
  33412. +
  33413. +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
  33414. +{
  33415. + VCHIQ_STATUS_T status;
  33416. + VCHIQ_STATE_T *state = instance->state;
  33417. +
  33418. + vchiq_log_trace(vchiq_core_log_level,
  33419. + "%s(%p) called", __func__, instance);
  33420. +
  33421. + if (mutex_lock_interruptible(&state->mutex) != 0) {
  33422. + vchiq_log_trace(vchiq_core_log_level,
  33423. + "%s: call to mutex_lock failed", __func__);
  33424. + status = VCHIQ_RETRY;
  33425. + goto failed;
  33426. + }
  33427. + status = vchiq_connect_internal(state, instance);
  33428. +
  33429. + if (status == VCHIQ_SUCCESS)
  33430. + instance->connected = 1;
  33431. +
  33432. + mutex_unlock(&state->mutex);
  33433. +
  33434. +failed:
  33435. + vchiq_log_trace(vchiq_core_log_level,
  33436. + "%s(%p): returning %d", __func__, instance, status);
  33437. +
  33438. + return status;
  33439. +}
  33440. +EXPORT_SYMBOL(vchiq_connect);
  33441. +
  33442. +/****************************************************************************
  33443. +*
  33444. +* vchiq_add_service
  33445. +*
  33446. +***************************************************************************/
  33447. +
  33448. +VCHIQ_STATUS_T vchiq_add_service(
  33449. + VCHIQ_INSTANCE_T instance,
  33450. + const VCHIQ_SERVICE_PARAMS_T *params,
  33451. + VCHIQ_SERVICE_HANDLE_T *phandle)
  33452. +{
  33453. + VCHIQ_STATUS_T status;
  33454. + VCHIQ_STATE_T *state = instance->state;
  33455. + VCHIQ_SERVICE_T *service = NULL;
  33456. + int srvstate;
  33457. +
  33458. + vchiq_log_trace(vchiq_core_log_level,
  33459. + "%s(%p) called", __func__, instance);
  33460. +
  33461. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  33462. +
  33463. + srvstate = vchiq_is_connected(instance)
  33464. + ? VCHIQ_SRVSTATE_LISTENING
  33465. + : VCHIQ_SRVSTATE_HIDDEN;
  33466. +
  33467. + service = vchiq_add_service_internal(
  33468. + state,
  33469. + params,
  33470. + srvstate,
  33471. + instance,
  33472. + NULL);
  33473. +
  33474. + if (service) {
  33475. + *phandle = service->handle;
  33476. + status = VCHIQ_SUCCESS;
  33477. + } else
  33478. + status = VCHIQ_ERROR;
  33479. +
  33480. + vchiq_log_trace(vchiq_core_log_level,
  33481. + "%s(%p): returning %d", __func__, instance, status);
  33482. +
  33483. + return status;
  33484. +}
  33485. +EXPORT_SYMBOL(vchiq_add_service);
  33486. +
  33487. +/****************************************************************************
  33488. +*
  33489. +* vchiq_open_service
  33490. +*
  33491. +***************************************************************************/
  33492. +
  33493. +VCHIQ_STATUS_T vchiq_open_service(
  33494. + VCHIQ_INSTANCE_T instance,
  33495. + const VCHIQ_SERVICE_PARAMS_T *params,
  33496. + VCHIQ_SERVICE_HANDLE_T *phandle)
  33497. +{
  33498. + VCHIQ_STATUS_T status = VCHIQ_ERROR;
  33499. + VCHIQ_STATE_T *state = instance->state;
  33500. + VCHIQ_SERVICE_T *service = NULL;
  33501. +
  33502. + vchiq_log_trace(vchiq_core_log_level,
  33503. + "%s(%p) called", __func__, instance);
  33504. +
  33505. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  33506. +
  33507. + if (!vchiq_is_connected(instance))
  33508. + goto failed;
  33509. +
  33510. + service = vchiq_add_service_internal(state,
  33511. + params,
  33512. + VCHIQ_SRVSTATE_OPENING,
  33513. + instance,
  33514. + NULL);
  33515. +
  33516. + if (service) {
  33517. + *phandle = service->handle;
  33518. + status = vchiq_open_service_internal(service, current->pid);
  33519. + if (status != VCHIQ_SUCCESS) {
  33520. + vchiq_remove_service(service->handle);
  33521. + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
  33522. + }
  33523. + }
  33524. +
  33525. +failed:
  33526. + vchiq_log_trace(vchiq_core_log_level,
  33527. + "%s(%p): returning %d", __func__, instance, status);
  33528. +
  33529. + return status;
  33530. +}
  33531. +EXPORT_SYMBOL(vchiq_open_service);
  33532. +
  33533. +VCHIQ_STATUS_T
  33534. +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
  33535. + const void *data, unsigned int size, void *userdata)
  33536. +{
  33537. + return vchiq_bulk_transfer(handle,
  33538. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  33539. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
  33540. +}
  33541. +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
  33542. +
  33543. +VCHIQ_STATUS_T
  33544. +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  33545. + unsigned int size, void *userdata)
  33546. +{
  33547. + return vchiq_bulk_transfer(handle,
  33548. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  33549. + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
  33550. +}
  33551. +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
  33552. +
  33553. +VCHIQ_STATUS_T
  33554. +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
  33555. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  33556. +{
  33557. + VCHIQ_STATUS_T status;
  33558. +
  33559. + switch (mode) {
  33560. + case VCHIQ_BULK_MODE_NOCALLBACK:
  33561. + case VCHIQ_BULK_MODE_CALLBACK:
  33562. + status = vchiq_bulk_transfer(handle,
  33563. + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
  33564. + mode, VCHIQ_BULK_TRANSMIT);
  33565. + break;
  33566. + case VCHIQ_BULK_MODE_BLOCKING:
  33567. + status = vchiq_blocking_bulk_transfer(handle,
  33568. + (void *)data, size, VCHIQ_BULK_TRANSMIT);
  33569. + break;
  33570. + default:
  33571. + return VCHIQ_ERROR;
  33572. + }
  33573. +
  33574. + return status;
  33575. +}
  33576. +EXPORT_SYMBOL(vchiq_bulk_transmit);
  33577. +
  33578. +VCHIQ_STATUS_T
  33579. +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  33580. + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
  33581. +{
  33582. + VCHIQ_STATUS_T status;
  33583. +
  33584. + switch (mode) {
  33585. + case VCHIQ_BULK_MODE_NOCALLBACK:
  33586. + case VCHIQ_BULK_MODE_CALLBACK:
  33587. + status = vchiq_bulk_transfer(handle,
  33588. + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
  33589. + mode, VCHIQ_BULK_RECEIVE);
  33590. + break;
  33591. + case VCHIQ_BULK_MODE_BLOCKING:
  33592. + status = vchiq_blocking_bulk_transfer(handle,
  33593. + (void *)data, size, VCHIQ_BULK_RECEIVE);
  33594. + break;
  33595. + default:
  33596. + return VCHIQ_ERROR;
  33597. + }
  33598. +
  33599. + return status;
  33600. +}
  33601. +EXPORT_SYMBOL(vchiq_bulk_receive);
  33602. +
  33603. +static VCHIQ_STATUS_T
  33604. +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
  33605. + unsigned int size, VCHIQ_BULK_DIR_T dir)
  33606. +{
  33607. + VCHIQ_INSTANCE_T instance;
  33608. + VCHIQ_SERVICE_T *service;
  33609. + VCHIQ_STATUS_T status;
  33610. + struct bulk_waiter_node *waiter = NULL;
  33611. + struct list_head *pos;
  33612. +
  33613. + service = find_service_by_handle(handle);
  33614. + if (!service)
  33615. + return VCHIQ_ERROR;
  33616. +
  33617. + instance = service->instance;
  33618. +
  33619. + unlock_service(service);
  33620. +
  33621. + mutex_lock(&instance->bulk_waiter_list_mutex);
  33622. + list_for_each(pos, &instance->bulk_waiter_list) {
  33623. + if (list_entry(pos, struct bulk_waiter_node,
  33624. + list)->pid == current->pid) {
  33625. + waiter = list_entry(pos,
  33626. + struct bulk_waiter_node,
  33627. + list);
  33628. + list_del(pos);
  33629. + break;
  33630. + }
  33631. + }
  33632. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  33633. +
  33634. + if (waiter) {
  33635. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  33636. + if (bulk) {
  33637. + /* This thread has an outstanding bulk transfer. */
  33638. + if ((bulk->data != data) ||
  33639. + (bulk->size != size)) {
  33640. + /* This is not a retry of the previous one.
  33641. + ** Cancel the signal when the transfer
  33642. + ** completes. */
  33643. + spin_lock(&bulk_waiter_spinlock);
  33644. + bulk->userdata = NULL;
  33645. + spin_unlock(&bulk_waiter_spinlock);
  33646. + }
  33647. + }
  33648. + }
  33649. +
  33650. + if (!waiter) {
  33651. + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
  33652. + if (!waiter) {
  33653. + vchiq_log_error(vchiq_core_log_level,
  33654. + "%s - out of memory", __func__);
  33655. + return VCHIQ_ERROR;
  33656. + }
  33657. + }
  33658. +
  33659. + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
  33660. + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
  33661. + dir);
  33662. + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
  33663. + !waiter->bulk_waiter.bulk) {
  33664. + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
  33665. + if (bulk) {
  33666. + /* Cancel the signal when the transfer
  33667. + ** completes. */
  33668. + spin_lock(&bulk_waiter_spinlock);
  33669. + bulk->userdata = NULL;
  33670. + spin_unlock(&bulk_waiter_spinlock);
  33671. + }
  33672. + kfree(waiter);
  33673. + } else {
  33674. + waiter->pid = current->pid;
  33675. + mutex_lock(&instance->bulk_waiter_list_mutex);
  33676. + list_add(&waiter->list, &instance->bulk_waiter_list);
  33677. + mutex_unlock(&instance->bulk_waiter_list_mutex);
  33678. + vchiq_log_info(vchiq_arm_log_level,
  33679. + "saved bulk_waiter %x for pid %d",
  33680. + (unsigned int)waiter, current->pid);
  33681. + }
  33682. +
  33683. + return status;
  33684. +}
  33685. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h
  33686. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h 1970-01-01 01:00:00.000000000 +0100
  33687. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_killable.h 2014-09-14 19:03:25.000000000 +0200
  33688. @@ -0,0 +1,69 @@
  33689. +/**
  33690. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33691. + *
  33692. + * Redistribution and use in source and binary forms, with or without
  33693. + * modification, are permitted provided that the following conditions
  33694. + * are met:
  33695. + * 1. Redistributions of source code must retain the above copyright
  33696. + * notice, this list of conditions, and the following disclaimer,
  33697. + * without modification.
  33698. + * 2. Redistributions in binary form must reproduce the above copyright
  33699. + * notice, this list of conditions and the following disclaimer in the
  33700. + * documentation and/or other materials provided with the distribution.
  33701. + * 3. The names of the above-listed copyright holders may not be used
  33702. + * to endorse or promote products derived from this software without
  33703. + * specific prior written permission.
  33704. + *
  33705. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33706. + * GNU General Public License ("GPL") version 2, as published by the Free
  33707. + * Software Foundation.
  33708. + *
  33709. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33710. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33711. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33712. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33713. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33714. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33715. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33716. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33717. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33718. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33719. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33720. + */
  33721. +
  33722. +#ifndef VCHIQ_KILLABLE_H
  33723. +#define VCHIQ_KILLABLE_H
  33724. +
  33725. +#include <linux/mutex.h>
  33726. +#include <linux/semaphore.h>
  33727. +
  33728. +#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTRAP) | sigmask(SIGSTOP) | sigmask(SIGCONT))
  33729. +
  33730. +static inline int __must_check down_interruptible_killable(struct semaphore *sem)
  33731. +{
  33732. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  33733. + int ret;
  33734. + sigset_t blocked, oldset;
  33735. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  33736. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  33737. + ret = down_interruptible(sem);
  33738. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  33739. + return ret;
  33740. +}
  33741. +#define down_interruptible down_interruptible_killable
  33742. +
  33743. +
  33744. +static inline int __must_check mutex_lock_interruptible_killable(struct mutex *lock)
  33745. +{
  33746. + /* Allow interception of killable signals only. We don't want to be interrupted by harmless signals like SIGALRM */
  33747. + int ret;
  33748. + sigset_t blocked, oldset;
  33749. + siginitsetinv(&blocked, SHUTDOWN_SIGS);
  33750. + sigprocmask(SIG_SETMASK, &blocked, &oldset);
  33751. + ret = mutex_lock_interruptible(lock);
  33752. + sigprocmask(SIG_SETMASK, &oldset, NULL);
  33753. + return ret;
  33754. +}
  33755. +#define mutex_lock_interruptible mutex_lock_interruptible_killable
  33756. +
  33757. +#endif
  33758. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
  33759. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 1970-01-01 01:00:00.000000000 +0100
  33760. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h 2014-04-13 17:32:57.000000000 +0200
  33761. @@ -0,0 +1,71 @@
  33762. +/**
  33763. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33764. + *
  33765. + * Redistribution and use in source and binary forms, with or without
  33766. + * modification, are permitted provided that the following conditions
  33767. + * are met:
  33768. + * 1. Redistributions of source code must retain the above copyright
  33769. + * notice, this list of conditions, and the following disclaimer,
  33770. + * without modification.
  33771. + * 2. Redistributions in binary form must reproduce the above copyright
  33772. + * notice, this list of conditions and the following disclaimer in the
  33773. + * documentation and/or other materials provided with the distribution.
  33774. + * 3. The names of the above-listed copyright holders may not be used
  33775. + * to endorse or promote products derived from this software without
  33776. + * specific prior written permission.
  33777. + *
  33778. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33779. + * GNU General Public License ("GPL") version 2, as published by the Free
  33780. + * Software Foundation.
  33781. + *
  33782. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33783. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33784. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33785. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33786. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33787. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33788. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33789. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33790. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33791. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33792. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33793. + */
  33794. +
  33795. +#ifndef VCHIQ_MEMDRV_H
  33796. +#define VCHIQ_MEMDRV_H
  33797. +
  33798. +/* ---- Include Files ----------------------------------------------------- */
  33799. +
  33800. +#include <linux/kernel.h>
  33801. +#include "vchiq_if.h"
  33802. +
  33803. +/* ---- Constants and Types ---------------------------------------------- */
  33804. +
  33805. +typedef struct {
  33806. + void *armSharedMemVirt;
  33807. + dma_addr_t armSharedMemPhys;
  33808. + size_t armSharedMemSize;
  33809. +
  33810. + void *vcSharedMemVirt;
  33811. + dma_addr_t vcSharedMemPhys;
  33812. + size_t vcSharedMemSize;
  33813. +} VCHIQ_SHARED_MEM_INFO_T;
  33814. +
  33815. +/* ---- Variable Externs ------------------------------------------------- */
  33816. +
  33817. +/* ---- Function Prototypes ---------------------------------------------- */
  33818. +
  33819. +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
  33820. +
  33821. +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
  33822. +
  33823. +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
  33824. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  33825. +
  33826. +VCHIQ_STATUS_T vchiq_userdrv_suspend(
  33827. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  33828. +
  33829. +VCHIQ_STATUS_T vchiq_userdrv_resume(
  33830. + const VCHIQ_PLATFORM_DATA_T * platform_data);
  33831. +
  33832. +#endif
  33833. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
  33834. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 1970-01-01 01:00:00.000000000 +0100
  33835. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h 2014-04-13 17:32:57.000000000 +0200
  33836. @@ -0,0 +1,58 @@
  33837. +/**
  33838. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33839. + *
  33840. + * Redistribution and use in source and binary forms, with or without
  33841. + * modification, are permitted provided that the following conditions
  33842. + * are met:
  33843. + * 1. Redistributions of source code must retain the above copyright
  33844. + * notice, this list of conditions, and the following disclaimer,
  33845. + * without modification.
  33846. + * 2. Redistributions in binary form must reproduce the above copyright
  33847. + * notice, this list of conditions and the following disclaimer in the
  33848. + * documentation and/or other materials provided with the distribution.
  33849. + * 3. The names of the above-listed copyright holders may not be used
  33850. + * to endorse or promote products derived from this software without
  33851. + * specific prior written permission.
  33852. + *
  33853. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33854. + * GNU General Public License ("GPL") version 2, as published by the Free
  33855. + * Software Foundation.
  33856. + *
  33857. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33858. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33859. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33860. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33861. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33862. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33863. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33864. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33865. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33866. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33867. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33868. + */
  33869. +
  33870. +#ifndef VCHIQ_PAGELIST_H
  33871. +#define VCHIQ_PAGELIST_H
  33872. +
  33873. +#ifndef PAGE_SIZE
  33874. +#define PAGE_SIZE 4096
  33875. +#endif
  33876. +#define CACHE_LINE_SIZE 32
  33877. +#define PAGELIST_WRITE 0
  33878. +#define PAGELIST_READ 1
  33879. +#define PAGELIST_READ_WITH_FRAGMENTS 2
  33880. +
  33881. +typedef struct pagelist_struct {
  33882. + unsigned long length;
  33883. + unsigned short type;
  33884. + unsigned short offset;
  33885. + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
  33886. + pages at consecutive addresses. */
  33887. +} PAGELIST_T;
  33888. +
  33889. +typedef struct fragments_struct {
  33890. + char headbuf[CACHE_LINE_SIZE];
  33891. + char tailbuf[CACHE_LINE_SIZE];
  33892. +} FRAGMENTS_T;
  33893. +
  33894. +#endif /* VCHIQ_PAGELIST_H */
  33895. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c
  33896. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 1970-01-01 01:00:00.000000000 +0100
  33897. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c 2014-09-14 19:03:25.000000000 +0200
  33898. @@ -0,0 +1,253 @@
  33899. +/**
  33900. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  33901. + *
  33902. + * Redistribution and use in source and binary forms, with or without
  33903. + * modification, are permitted provided that the following conditions
  33904. + * are met:
  33905. + * 1. Redistributions of source code must retain the above copyright
  33906. + * notice, this list of conditions, and the following disclaimer,
  33907. + * without modification.
  33908. + * 2. Redistributions in binary form must reproduce the above copyright
  33909. + * notice, this list of conditions and the following disclaimer in the
  33910. + * documentation and/or other materials provided with the distribution.
  33911. + * 3. The names of the above-listed copyright holders may not be used
  33912. + * to endorse or promote products derived from this software without
  33913. + * specific prior written permission.
  33914. + *
  33915. + * ALTERNATIVELY, this software may be distributed under the terms of the
  33916. + * GNU General Public License ("GPL") version 2, as published by the Free
  33917. + * Software Foundation.
  33918. + *
  33919. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  33920. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33921. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33922. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  33923. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  33924. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  33925. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  33926. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33927. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33928. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33929. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33930. + */
  33931. +
  33932. +
  33933. +#include <linux/proc_fs.h>
  33934. +#include "vchiq_core.h"
  33935. +#include "vchiq_arm.h"
  33936. +
  33937. +#if 1
  33938. +
  33939. +int vchiq_proc_init(void)
  33940. +{
  33941. + return 0;
  33942. +}
  33943. +
  33944. +void vchiq_proc_deinit(void)
  33945. +{
  33946. +}
  33947. +
  33948. +#else
  33949. +
  33950. +struct vchiq_proc_info {
  33951. + /* Global 'vc' proc entry used by all instances */
  33952. + struct proc_dir_entry *vc_cfg_dir;
  33953. +
  33954. + /* one entry per client process */
  33955. + struct proc_dir_entry *clients;
  33956. +
  33957. + /* log categories */
  33958. + struct proc_dir_entry *log_categories;
  33959. +};
  33960. +
  33961. +static struct vchiq_proc_info proc_info;
  33962. +
  33963. +struct proc_dir_entry *vchiq_proc_top(void)
  33964. +{
  33965. + BUG_ON(proc_info.vc_cfg_dir == NULL);
  33966. + return proc_info.vc_cfg_dir;
  33967. +}
  33968. +
  33969. +/****************************************************************************
  33970. +*
  33971. +* log category entries
  33972. +*
  33973. +***************************************************************************/
  33974. +#define PROC_WRITE_BUF_SIZE 256
  33975. +
  33976. +#define VCHIQ_LOG_ERROR_STR "error"
  33977. +#define VCHIQ_LOG_WARNING_STR "warning"
  33978. +#define VCHIQ_LOG_INFO_STR "info"
  33979. +#define VCHIQ_LOG_TRACE_STR "trace"
  33980. +
  33981. +static int log_cfg_read(char *buffer,
  33982. + char **start,
  33983. + off_t off,
  33984. + int count,
  33985. + int *eof,
  33986. + void *data)
  33987. +{
  33988. + int len = 0;
  33989. + char *log_value = NULL;
  33990. +
  33991. + switch (*((int *)data)) {
  33992. + case VCHIQ_LOG_ERROR:
  33993. + log_value = VCHIQ_LOG_ERROR_STR;
  33994. + break;
  33995. + case VCHIQ_LOG_WARNING:
  33996. + log_value = VCHIQ_LOG_WARNING_STR;
  33997. + break;
  33998. + case VCHIQ_LOG_INFO:
  33999. + log_value = VCHIQ_LOG_INFO_STR;
  34000. + break;
  34001. + case VCHIQ_LOG_TRACE:
  34002. + log_value = VCHIQ_LOG_TRACE_STR;
  34003. + break;
  34004. + default:
  34005. + break;
  34006. + }
  34007. +
  34008. + len += sprintf(buffer + len,
  34009. + "%s\n",
  34010. + log_value ? log_value : "(null)");
  34011. +
  34012. + return len;
  34013. +}
  34014. +
  34015. +
  34016. +static int log_cfg_write(struct file *file,
  34017. + const char __user *buffer,
  34018. + unsigned long count,
  34019. + void *data)
  34020. +{
  34021. + int *log_module = data;
  34022. + char kbuf[PROC_WRITE_BUF_SIZE + 1];
  34023. +
  34024. + (void)file;
  34025. +
  34026. + memset(kbuf, 0, PROC_WRITE_BUF_SIZE + 1);
  34027. + if (count >= PROC_WRITE_BUF_SIZE)
  34028. + count = PROC_WRITE_BUF_SIZE;
  34029. +
  34030. + if (copy_from_user(kbuf,
  34031. + buffer,
  34032. + count) != 0)
  34033. + return -EFAULT;
  34034. + kbuf[count - 1] = 0;
  34035. +
  34036. + if (strncmp("error", kbuf, strlen("error")) == 0)
  34037. + *log_module = VCHIQ_LOG_ERROR;
  34038. + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
  34039. + *log_module = VCHIQ_LOG_WARNING;
  34040. + else if (strncmp("info", kbuf, strlen("info")) == 0)
  34041. + *log_module = VCHIQ_LOG_INFO;
  34042. + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
  34043. + *log_module = VCHIQ_LOG_TRACE;
  34044. + else
  34045. + *log_module = VCHIQ_LOG_DEFAULT;
  34046. +
  34047. + return count;
  34048. +}
  34049. +
  34050. +/* Log category proc entries */
  34051. +struct vchiq_proc_log_entry {
  34052. + const char *name;
  34053. + int *plevel;
  34054. + struct proc_dir_entry *dir;
  34055. +};
  34056. +
  34057. +static struct vchiq_proc_log_entry vchiq_proc_log_entries[] = {
  34058. + { "core", &vchiq_core_log_level },
  34059. + { "msg", &vchiq_core_msg_log_level },
  34060. + { "sync", &vchiq_sync_log_level },
  34061. + { "susp", &vchiq_susp_log_level },
  34062. + { "arm", &vchiq_arm_log_level },
  34063. +};
  34064. +static int n_log_entries =
  34065. + sizeof(vchiq_proc_log_entries)/sizeof(vchiq_proc_log_entries[0]);
  34066. +
  34067. +/* create an entry under /proc/vc/log for each log category */
  34068. +static int vchiq_proc_create_log_entries(struct proc_dir_entry *top)
  34069. +{
  34070. + struct proc_dir_entry *dir;
  34071. + size_t i;
  34072. + int ret = 0;
  34073. + dir = proc_mkdir("log", proc_info.vc_cfg_dir);
  34074. + if (!dir)
  34075. + return -ENOMEM;
  34076. + proc_info.log_categories = dir;
  34077. +
  34078. + for (i = 0; i < n_log_entries; i++) {
  34079. + dir = create_proc_entry(vchiq_proc_log_entries[i].name,
  34080. + 0644,
  34081. + proc_info.log_categories);
  34082. + if (!dir) {
  34083. + ret = -ENOMEM;
  34084. + break;
  34085. + }
  34086. +
  34087. + dir->read_proc = &log_cfg_read;
  34088. + dir->write_proc = &log_cfg_write;
  34089. + dir->data = (void *)vchiq_proc_log_entries[i].plevel;
  34090. +
  34091. + vchiq_proc_log_entries[i].dir = dir;
  34092. + }
  34093. + return ret;
  34094. +}
  34095. +
  34096. +
  34097. +int vchiq_proc_init(void)
  34098. +{
  34099. + BUG_ON(proc_info.vc_cfg_dir != NULL);
  34100. +
  34101. + proc_info.vc_cfg_dir = proc_mkdir("vc", NULL);
  34102. + if (proc_info.vc_cfg_dir == NULL)
  34103. + goto fail;
  34104. +
  34105. + proc_info.clients = proc_mkdir("clients",
  34106. + proc_info.vc_cfg_dir);
  34107. + if (!proc_info.clients)
  34108. + goto fail;
  34109. +
  34110. + if (vchiq_proc_create_log_entries(proc_info.vc_cfg_dir) != 0)
  34111. + goto fail;
  34112. +
  34113. + return 0;
  34114. +
  34115. +fail:
  34116. + vchiq_proc_deinit();
  34117. + vchiq_log_error(vchiq_arm_log_level,
  34118. + "%s: failed to create proc directory",
  34119. + __func__);
  34120. +
  34121. + return -ENOMEM;
  34122. +}
  34123. +
  34124. +/* remove all the proc entries */
  34125. +void vchiq_proc_deinit(void)
  34126. +{
  34127. + /* log category entries */
  34128. + if (proc_info.log_categories) {
  34129. + size_t i;
  34130. + for (i = 0; i < n_log_entries; i++)
  34131. + if (vchiq_proc_log_entries[i].dir)
  34132. + remove_proc_entry(
  34133. + vchiq_proc_log_entries[i].name,
  34134. + proc_info.log_categories);
  34135. +
  34136. + remove_proc_entry(proc_info.log_categories->name,
  34137. + proc_info.vc_cfg_dir);
  34138. + }
  34139. + if (proc_info.clients)
  34140. + remove_proc_entry(proc_info.clients->name,
  34141. + proc_info.vc_cfg_dir);
  34142. + if (proc_info.vc_cfg_dir)
  34143. + remove_proc_entry(proc_info.vc_cfg_dir->name, NULL);
  34144. +}
  34145. +
  34146. +struct proc_dir_entry *vchiq_clients_top(void)
  34147. +{
  34148. + return proc_info.clients;
  34149. +}
  34150. +
  34151. +#endif
  34152. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
  34153. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 1970-01-01 01:00:00.000000000 +0100
  34154. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c 2014-09-14 19:03:25.000000000 +0200
  34155. @@ -0,0 +1,832 @@
  34156. +/**
  34157. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34158. + *
  34159. + * Redistribution and use in source and binary forms, with or without
  34160. + * modification, are permitted provided that the following conditions
  34161. + * are met:
  34162. + * 1. Redistributions of source code must retain the above copyright
  34163. + * notice, this list of conditions, and the following disclaimer,
  34164. + * without modification.
  34165. + * 2. Redistributions in binary form must reproduce the above copyright
  34166. + * notice, this list of conditions and the following disclaimer in the
  34167. + * documentation and/or other materials provided with the distribution.
  34168. + * 3. The names of the above-listed copyright holders may not be used
  34169. + * to endorse or promote products derived from this software without
  34170. + * specific prior written permission.
  34171. + *
  34172. + * ALTERNATIVELY, this software may be distributed under the terms of the
  34173. + * GNU General Public License ("GPL") version 2, as published by the Free
  34174. + * Software Foundation.
  34175. + *
  34176. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  34177. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  34178. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34179. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34180. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  34181. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  34182. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  34183. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  34184. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34185. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34186. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34187. + */
  34188. +#include <linux/module.h>
  34189. +#include <linux/types.h>
  34190. +
  34191. +#include "interface/vchi/vchi.h"
  34192. +#include "vchiq.h"
  34193. +#include "vchiq_core.h"
  34194. +
  34195. +#include "vchiq_util.h"
  34196. +
  34197. +#include <stddef.h>
  34198. +
  34199. +#define vchiq_status_to_vchi(status) ((int32_t)status)
  34200. +
  34201. +typedef struct {
  34202. + VCHIQ_SERVICE_HANDLE_T handle;
  34203. +
  34204. + VCHIU_QUEUE_T queue;
  34205. +
  34206. + VCHI_CALLBACK_T callback;
  34207. + void *callback_param;
  34208. +} SHIM_SERVICE_T;
  34209. +
  34210. +/* ----------------------------------------------------------------------
  34211. + * return pointer to the mphi message driver function table
  34212. + * -------------------------------------------------------------------- */
  34213. +const VCHI_MESSAGE_DRIVER_T *
  34214. +vchi_mphi_message_driver_func_table(void)
  34215. +{
  34216. + return NULL;
  34217. +}
  34218. +
  34219. +/* ----------------------------------------------------------------------
  34220. + * return a pointer to the 'single' connection driver fops
  34221. + * -------------------------------------------------------------------- */
  34222. +const VCHI_CONNECTION_API_T *
  34223. +single_get_func_table(void)
  34224. +{
  34225. + return NULL;
  34226. +}
  34227. +
  34228. +VCHI_CONNECTION_T *vchi_create_connection(
  34229. + const VCHI_CONNECTION_API_T *function_table,
  34230. + const VCHI_MESSAGE_DRIVER_T *low_level)
  34231. +{
  34232. + (void)function_table;
  34233. + (void)low_level;
  34234. + return NULL;
  34235. +}
  34236. +
  34237. +/***********************************************************
  34238. + * Name: vchi_msg_peek
  34239. + *
  34240. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  34241. + * void **data,
  34242. + * uint32_t *msg_size,
  34243. +
  34244. +
  34245. + * VCHI_FLAGS_T flags
  34246. + *
  34247. + * Description: Routine to return a pointer to the current message (to allow in
  34248. + * place processing). The message can be removed using
  34249. + * vchi_msg_remove when you're finished
  34250. + *
  34251. + * Returns: int32_t - success == 0
  34252. + *
  34253. + ***********************************************************/
  34254. +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
  34255. + void **data,
  34256. + uint32_t *msg_size,
  34257. + VCHI_FLAGS_T flags)
  34258. +{
  34259. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34260. + VCHIQ_HEADER_T *header;
  34261. +
  34262. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  34263. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  34264. +
  34265. + if (flags == VCHI_FLAGS_NONE)
  34266. + if (vchiu_queue_is_empty(&service->queue))
  34267. + return -1;
  34268. +
  34269. + header = vchiu_queue_peek(&service->queue);
  34270. +
  34271. + *data = header->data;
  34272. + *msg_size = header->size;
  34273. +
  34274. + return 0;
  34275. +}
  34276. +EXPORT_SYMBOL(vchi_msg_peek);
  34277. +
  34278. +/***********************************************************
  34279. + * Name: vchi_msg_remove
  34280. + *
  34281. + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
  34282. + *
  34283. + * Description: Routine to remove a message (after it has been read with
  34284. + * vchi_msg_peek)
  34285. + *
  34286. + * Returns: int32_t - success == 0
  34287. + *
  34288. + ***********************************************************/
  34289. +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
  34290. +{
  34291. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34292. + VCHIQ_HEADER_T *header;
  34293. +
  34294. + header = vchiu_queue_pop(&service->queue);
  34295. +
  34296. + vchiq_release_message(service->handle, header);
  34297. +
  34298. + return 0;
  34299. +}
  34300. +EXPORT_SYMBOL(vchi_msg_remove);
  34301. +
  34302. +/***********************************************************
  34303. + * Name: vchi_msg_queue
  34304. + *
  34305. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  34306. + * const void *data,
  34307. + * uint32_t data_size,
  34308. + * VCHI_FLAGS_T flags,
  34309. + * void *msg_handle,
  34310. + *
  34311. + * Description: Thin wrapper to queue a message onto a connection
  34312. + *
  34313. + * Returns: int32_t - success == 0
  34314. + *
  34315. + ***********************************************************/
  34316. +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
  34317. + const void *data,
  34318. + uint32_t data_size,
  34319. + VCHI_FLAGS_T flags,
  34320. + void *msg_handle)
  34321. +{
  34322. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34323. + VCHIQ_ELEMENT_T element = {data, data_size};
  34324. + VCHIQ_STATUS_T status;
  34325. +
  34326. + (void)msg_handle;
  34327. +
  34328. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  34329. +
  34330. + status = vchiq_queue_message(service->handle, &element, 1);
  34331. +
  34332. + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
  34333. + ** implement a retry mechanism since this function is supposed
  34334. + ** to block until queued
  34335. + */
  34336. + while (status == VCHIQ_RETRY) {
  34337. + msleep(1);
  34338. + status = vchiq_queue_message(service->handle, &element, 1);
  34339. + }
  34340. +
  34341. + return vchiq_status_to_vchi(status);
  34342. +}
  34343. +EXPORT_SYMBOL(vchi_msg_queue);
  34344. +
  34345. +/***********************************************************
  34346. + * Name: vchi_bulk_queue_receive
  34347. + *
  34348. + * Arguments: VCHI_BULK_HANDLE_T handle,
  34349. + * void *data_dst,
  34350. + * const uint32_t data_size,
  34351. + * VCHI_FLAGS_T flags
  34352. + * void *bulk_handle
  34353. + *
  34354. + * Description: Routine to setup a rcv buffer
  34355. + *
  34356. + * Returns: int32_t - success == 0
  34357. + *
  34358. + ***********************************************************/
  34359. +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
  34360. + void *data_dst,
  34361. + uint32_t data_size,
  34362. + VCHI_FLAGS_T flags,
  34363. + void *bulk_handle)
  34364. +{
  34365. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34366. + VCHIQ_BULK_MODE_T mode;
  34367. + VCHIQ_STATUS_T status;
  34368. +
  34369. + switch ((int)flags) {
  34370. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  34371. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  34372. + WARN_ON(!service->callback);
  34373. + mode = VCHIQ_BULK_MODE_CALLBACK;
  34374. + break;
  34375. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  34376. + mode = VCHIQ_BULK_MODE_BLOCKING;
  34377. + break;
  34378. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  34379. + case VCHI_FLAGS_NONE:
  34380. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  34381. + break;
  34382. + default:
  34383. + WARN(1, "unsupported message\n");
  34384. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  34385. + }
  34386. +
  34387. + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
  34388. + bulk_handle, mode);
  34389. +
  34390. + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
  34391. + ** implement a retry mechanism since this function is supposed
  34392. + ** to block until queued
  34393. + */
  34394. + while (status == VCHIQ_RETRY) {
  34395. + msleep(1);
  34396. + status = vchiq_bulk_receive(service->handle, data_dst,
  34397. + data_size, bulk_handle, mode);
  34398. + }
  34399. +
  34400. + return vchiq_status_to_vchi(status);
  34401. +}
  34402. +EXPORT_SYMBOL(vchi_bulk_queue_receive);
  34403. +
  34404. +/***********************************************************
  34405. + * Name: vchi_bulk_queue_transmit
  34406. + *
  34407. + * Arguments: VCHI_BULK_HANDLE_T handle,
  34408. + * const void *data_src,
  34409. + * uint32_t data_size,
  34410. + * VCHI_FLAGS_T flags,
  34411. + * void *bulk_handle
  34412. + *
  34413. + * Description: Routine to transmit some data
  34414. + *
  34415. + * Returns: int32_t - success == 0
  34416. + *
  34417. + ***********************************************************/
  34418. +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
  34419. + const void *data_src,
  34420. + uint32_t data_size,
  34421. + VCHI_FLAGS_T flags,
  34422. + void *bulk_handle)
  34423. +{
  34424. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34425. + VCHIQ_BULK_MODE_T mode;
  34426. + VCHIQ_STATUS_T status;
  34427. +
  34428. + switch ((int)flags) {
  34429. + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
  34430. + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  34431. + WARN_ON(!service->callback);
  34432. + mode = VCHIQ_BULK_MODE_CALLBACK;
  34433. + break;
  34434. + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
  34435. + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
  34436. + mode = VCHIQ_BULK_MODE_BLOCKING;
  34437. + break;
  34438. + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
  34439. + case VCHI_FLAGS_NONE:
  34440. + mode = VCHIQ_BULK_MODE_NOCALLBACK;
  34441. + break;
  34442. + default:
  34443. + WARN(1, "unsupported message\n");
  34444. + return vchiq_status_to_vchi(VCHIQ_ERROR);
  34445. + }
  34446. +
  34447. + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
  34448. + bulk_handle, mode);
  34449. +
  34450. + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
  34451. + ** implement a retry mechanism since this function is supposed
  34452. + ** to block until queued
  34453. + */
  34454. + while (status == VCHIQ_RETRY) {
  34455. + msleep(1);
  34456. + status = vchiq_bulk_transmit(service->handle, data_src,
  34457. + data_size, bulk_handle, mode);
  34458. + }
  34459. +
  34460. + return vchiq_status_to_vchi(status);
  34461. +}
  34462. +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
  34463. +
  34464. +/***********************************************************
  34465. + * Name: vchi_msg_dequeue
  34466. + *
  34467. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  34468. + * void *data,
  34469. + * uint32_t max_data_size_to_read,
  34470. + * uint32_t *actual_msg_size
  34471. + * VCHI_FLAGS_T flags
  34472. + *
  34473. + * Description: Routine to dequeue a message into the supplied buffer
  34474. + *
  34475. + * Returns: int32_t - success == 0
  34476. + *
  34477. + ***********************************************************/
  34478. +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
  34479. + void *data,
  34480. + uint32_t max_data_size_to_read,
  34481. + uint32_t *actual_msg_size,
  34482. + VCHI_FLAGS_T flags)
  34483. +{
  34484. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34485. + VCHIQ_HEADER_T *header;
  34486. +
  34487. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  34488. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  34489. +
  34490. + if (flags == VCHI_FLAGS_NONE)
  34491. + if (vchiu_queue_is_empty(&service->queue))
  34492. + return -1;
  34493. +
  34494. + header = vchiu_queue_pop(&service->queue);
  34495. +
  34496. + memcpy(data, header->data, header->size < max_data_size_to_read ?
  34497. + header->size : max_data_size_to_read);
  34498. +
  34499. + *actual_msg_size = header->size;
  34500. +
  34501. + vchiq_release_message(service->handle, header);
  34502. +
  34503. + return 0;
  34504. +}
  34505. +EXPORT_SYMBOL(vchi_msg_dequeue);
  34506. +
  34507. +/***********************************************************
  34508. + * Name: vchi_msg_queuev
  34509. + *
  34510. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  34511. + * VCHI_MSG_VECTOR_T *vector,
  34512. + * uint32_t count,
  34513. + * VCHI_FLAGS_T flags,
  34514. + * void *msg_handle
  34515. + *
  34516. + * Description: Thin wrapper to queue a message onto a connection
  34517. + *
  34518. + * Returns: int32_t - success == 0
  34519. + *
  34520. + ***********************************************************/
  34521. +
  34522. +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
  34523. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
  34524. + offsetof(VCHIQ_ELEMENT_T, data));
  34525. +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
  34526. + offsetof(VCHIQ_ELEMENT_T, size));
  34527. +
  34528. +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
  34529. + VCHI_MSG_VECTOR_T *vector,
  34530. + uint32_t count,
  34531. + VCHI_FLAGS_T flags,
  34532. + void *msg_handle)
  34533. +{
  34534. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34535. +
  34536. + (void)msg_handle;
  34537. +
  34538. + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
  34539. +
  34540. + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
  34541. + (const VCHIQ_ELEMENT_T *)vector, count));
  34542. +}
  34543. +EXPORT_SYMBOL(vchi_msg_queuev);
  34544. +
  34545. +/***********************************************************
  34546. + * Name: vchi_held_msg_release
  34547. + *
  34548. + * Arguments: VCHI_HELD_MSG_T *message
  34549. + *
  34550. + * Description: Routine to release a held message (after it has been read with
  34551. + * vchi_msg_hold)
  34552. + *
  34553. + * Returns: int32_t - success == 0
  34554. + *
  34555. + ***********************************************************/
  34556. +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
  34557. +{
  34558. + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
  34559. + (VCHIQ_HEADER_T *)message->message);
  34560. +
  34561. + return 0;
  34562. +}
  34563. +EXPORT_SYMBOL(vchi_held_msg_release);
  34564. +
  34565. +/***********************************************************
  34566. + * Name: vchi_msg_hold
  34567. + *
  34568. + * Arguments: VCHI_SERVICE_HANDLE_T handle,
  34569. + * void **data,
  34570. + * uint32_t *msg_size,
  34571. + * VCHI_FLAGS_T flags,
  34572. + * VCHI_HELD_MSG_T *message_handle
  34573. + *
  34574. + * Description: Routine to return a pointer to the current message (to allow
  34575. + * in place processing). The message is dequeued - don't forget
  34576. + * to release the message using vchi_held_msg_release when you're
  34577. + * finished.
  34578. + *
  34579. + * Returns: int32_t - success == 0
  34580. + *
  34581. + ***********************************************************/
  34582. +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
  34583. + void **data,
  34584. + uint32_t *msg_size,
  34585. + VCHI_FLAGS_T flags,
  34586. + VCHI_HELD_MSG_T *message_handle)
  34587. +{
  34588. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34589. + VCHIQ_HEADER_T *header;
  34590. +
  34591. + WARN_ON((flags != VCHI_FLAGS_NONE) &&
  34592. + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
  34593. +
  34594. + if (flags == VCHI_FLAGS_NONE)
  34595. + if (vchiu_queue_is_empty(&service->queue))
  34596. + return -1;
  34597. +
  34598. + header = vchiu_queue_pop(&service->queue);
  34599. +
  34600. + *data = header->data;
  34601. + *msg_size = header->size;
  34602. +
  34603. + message_handle->service =
  34604. + (struct opaque_vchi_service_t *)service->handle;
  34605. + message_handle->message = header;
  34606. +
  34607. + return 0;
  34608. +}
  34609. +EXPORT_SYMBOL(vchi_msg_hold);
  34610. +
  34611. +/***********************************************************
  34612. + * Name: vchi_initialise
  34613. + *
  34614. + * Arguments: VCHI_INSTANCE_T *instance_handle
  34615. + * VCHI_CONNECTION_T **connections
  34616. + * const uint32_t num_connections
  34617. + *
  34618. + * Description: Initialises the hardware but does not transmit anything
  34619. + * When run as a Host App this will be called twice hence the need
  34620. + * to malloc the state information
  34621. + *
  34622. + * Returns: 0 if successful, failure otherwise
  34623. + *
  34624. + ***********************************************************/
  34625. +
  34626. +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
  34627. +{
  34628. + VCHIQ_INSTANCE_T instance;
  34629. + VCHIQ_STATUS_T status;
  34630. +
  34631. + status = vchiq_initialise(&instance);
  34632. +
  34633. + *instance_handle = (VCHI_INSTANCE_T)instance;
  34634. +
  34635. + return vchiq_status_to_vchi(status);
  34636. +}
  34637. +EXPORT_SYMBOL(vchi_initialise);
  34638. +
  34639. +/***********************************************************
  34640. + * Name: vchi_connect
  34641. + *
  34642. + * Arguments: VCHI_CONNECTION_T **connections
  34643. + * const uint32_t num_connections
  34644. + * VCHI_INSTANCE_T instance_handle)
  34645. + *
  34646. + * Description: Starts the command service on each connection,
  34647. + * causing INIT messages to be pinged back and forth
  34648. + *
  34649. + * Returns: 0 if successful, failure otherwise
  34650. + *
  34651. + ***********************************************************/
  34652. +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
  34653. + const uint32_t num_connections,
  34654. + VCHI_INSTANCE_T instance_handle)
  34655. +{
  34656. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  34657. +
  34658. + (void)connections;
  34659. + (void)num_connections;
  34660. +
  34661. + return vchiq_connect(instance);
  34662. +}
  34663. +EXPORT_SYMBOL(vchi_connect);
  34664. +
  34665. +
  34666. +/***********************************************************
  34667. + * Name: vchi_disconnect
  34668. + *
  34669. + * Arguments: VCHI_INSTANCE_T instance_handle
  34670. + *
  34671. + * Description: Stops the command service on each connection,
  34672. + * causing DE-INIT messages to be pinged back and forth
  34673. + *
  34674. + * Returns: 0 if successful, failure otherwise
  34675. + *
  34676. + ***********************************************************/
  34677. +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
  34678. +{
  34679. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  34680. + return vchiq_status_to_vchi(vchiq_shutdown(instance));
  34681. +}
  34682. +EXPORT_SYMBOL(vchi_disconnect);
  34683. +
  34684. +
  34685. +/***********************************************************
  34686. + * Name: vchi_service_open
  34687. + * Name: vchi_service_create
  34688. + *
  34689. + * Arguments: VCHI_INSTANCE_T *instance_handle
  34690. + * SERVICE_CREATION_T *setup,
  34691. + * VCHI_SERVICE_HANDLE_T *handle
  34692. + *
  34693. + * Description: Routine to open a service
  34694. + *
  34695. + * Returns: int32_t - success == 0
  34696. + *
  34697. + ***********************************************************/
  34698. +
  34699. +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
  34700. + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
  34701. +{
  34702. + SHIM_SERVICE_T *service =
  34703. + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
  34704. +
  34705. + if (!service->callback)
  34706. + goto release;
  34707. +
  34708. + switch (reason) {
  34709. + case VCHIQ_MESSAGE_AVAILABLE:
  34710. + vchiu_queue_push(&service->queue, header);
  34711. +
  34712. + service->callback(service->callback_param,
  34713. + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
  34714. +
  34715. + goto done;
  34716. + break;
  34717. +
  34718. + case VCHIQ_BULK_TRANSMIT_DONE:
  34719. + service->callback(service->callback_param,
  34720. + VCHI_CALLBACK_BULK_SENT, bulk_user);
  34721. + break;
  34722. +
  34723. + case VCHIQ_BULK_RECEIVE_DONE:
  34724. + service->callback(service->callback_param,
  34725. + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
  34726. + break;
  34727. +
  34728. + case VCHIQ_SERVICE_CLOSED:
  34729. + service->callback(service->callback_param,
  34730. + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
  34731. + break;
  34732. +
  34733. + case VCHIQ_SERVICE_OPENED:
  34734. + /* No equivalent VCHI reason */
  34735. + break;
  34736. +
  34737. + case VCHIQ_BULK_TRANSMIT_ABORTED:
  34738. + service->callback(service->callback_param,
  34739. + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
  34740. + bulk_user);
  34741. + break;
  34742. +
  34743. + case VCHIQ_BULK_RECEIVE_ABORTED:
  34744. + service->callback(service->callback_param,
  34745. + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
  34746. + bulk_user);
  34747. + break;
  34748. +
  34749. + default:
  34750. + WARN(1, "not supported\n");
  34751. + break;
  34752. + }
  34753. +
  34754. +release:
  34755. + vchiq_release_message(service->handle, header);
  34756. +done:
  34757. + return VCHIQ_SUCCESS;
  34758. +}
  34759. +
  34760. +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
  34761. + SERVICE_CREATION_T *setup)
  34762. +{
  34763. + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
  34764. +
  34765. + (void)instance;
  34766. +
  34767. + if (service) {
  34768. + if (vchiu_queue_init(&service->queue, 64)) {
  34769. + service->callback = setup->callback;
  34770. + service->callback_param = setup->callback_param;
  34771. + } else {
  34772. + kfree(service);
  34773. + service = NULL;
  34774. + }
  34775. + }
  34776. +
  34777. + return service;
  34778. +}
  34779. +
  34780. +static void service_free(SHIM_SERVICE_T *service)
  34781. +{
  34782. + if (service) {
  34783. + vchiu_queue_delete(&service->queue);
  34784. + kfree(service);
  34785. + }
  34786. +}
  34787. +
  34788. +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
  34789. + SERVICE_CREATION_T *setup,
  34790. + VCHI_SERVICE_HANDLE_T *handle)
  34791. +{
  34792. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  34793. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  34794. +
  34795. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  34796. +
  34797. + if (service) {
  34798. + VCHIQ_SERVICE_PARAMS_T params;
  34799. + VCHIQ_STATUS_T status;
  34800. +
  34801. + memset(&params, 0, sizeof(params));
  34802. + params.fourcc = setup->service_id;
  34803. + params.callback = shim_callback;
  34804. + params.userdata = service;
  34805. + params.version = setup->version.version;
  34806. + params.version_min = setup->version.version_min;
  34807. +
  34808. + status = vchiq_open_service(instance, &params,
  34809. + &service->handle);
  34810. + if (status != VCHIQ_SUCCESS) {
  34811. + service_free(service);
  34812. + service = NULL;
  34813. + *handle = NULL;
  34814. + }
  34815. + }
  34816. +
  34817. + return (service != NULL) ? 0 : -1;
  34818. +}
  34819. +EXPORT_SYMBOL(vchi_service_open);
  34820. +
  34821. +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
  34822. + SERVICE_CREATION_T *setup,
  34823. + VCHI_SERVICE_HANDLE_T *handle)
  34824. +{
  34825. + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
  34826. + SHIM_SERVICE_T *service = service_alloc(instance, setup);
  34827. +
  34828. + *handle = (VCHI_SERVICE_HANDLE_T)service;
  34829. +
  34830. + if (service) {
  34831. + VCHIQ_SERVICE_PARAMS_T params;
  34832. + VCHIQ_STATUS_T status;
  34833. +
  34834. + memset(&params, 0, sizeof(params));
  34835. + params.fourcc = setup->service_id;
  34836. + params.callback = shim_callback;
  34837. + params.userdata = service;
  34838. + params.version = setup->version.version;
  34839. + params.version_min = setup->version.version_min;
  34840. + status = vchiq_add_service(instance, &params, &service->handle);
  34841. +
  34842. + if (status != VCHIQ_SUCCESS) {
  34843. + service_free(service);
  34844. + service = NULL;
  34845. + *handle = NULL;
  34846. + }
  34847. + }
  34848. +
  34849. + return (service != NULL) ? 0 : -1;
  34850. +}
  34851. +EXPORT_SYMBOL(vchi_service_create);
  34852. +
  34853. +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
  34854. +{
  34855. + int32_t ret = -1;
  34856. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34857. + if (service) {
  34858. + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
  34859. + if (status == VCHIQ_SUCCESS) {
  34860. + service_free(service);
  34861. + service = NULL;
  34862. + }
  34863. +
  34864. + ret = vchiq_status_to_vchi(status);
  34865. + }
  34866. + return ret;
  34867. +}
  34868. +EXPORT_SYMBOL(vchi_service_close);
  34869. +
  34870. +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
  34871. +{
  34872. + int32_t ret = -1;
  34873. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34874. + if (service) {
  34875. + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
  34876. + if (status == VCHIQ_SUCCESS) {
  34877. + service_free(service);
  34878. + service = NULL;
  34879. + }
  34880. +
  34881. + ret = vchiq_status_to_vchi(status);
  34882. + }
  34883. + return ret;
  34884. +}
  34885. +EXPORT_SYMBOL(vchi_service_destroy);
  34886. +
  34887. +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
  34888. +{
  34889. + int32_t ret = -1;
  34890. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34891. + if(service)
  34892. + {
  34893. + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
  34894. + ret = vchiq_status_to_vchi( status );
  34895. + }
  34896. + return ret;
  34897. +}
  34898. +EXPORT_SYMBOL(vchi_get_peer_version);
  34899. +
  34900. +/* ----------------------------------------------------------------------
  34901. + * read a uint32_t from buffer.
  34902. + * network format is defined to be little endian
  34903. + * -------------------------------------------------------------------- */
  34904. +uint32_t
  34905. +vchi_readbuf_uint32(const void *_ptr)
  34906. +{
  34907. + const unsigned char *ptr = _ptr;
  34908. + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
  34909. +}
  34910. +
  34911. +/* ----------------------------------------------------------------------
  34912. + * write a uint32_t to buffer.
  34913. + * network format is defined to be little endian
  34914. + * -------------------------------------------------------------------- */
  34915. +void
  34916. +vchi_writebuf_uint32(void *_ptr, uint32_t value)
  34917. +{
  34918. + unsigned char *ptr = _ptr;
  34919. + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
  34920. + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
  34921. + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
  34922. + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
  34923. +}
  34924. +
  34925. +/* ----------------------------------------------------------------------
  34926. + * read a uint16_t from buffer.
  34927. + * network format is defined to be little endian
  34928. + * -------------------------------------------------------------------- */
  34929. +uint16_t
  34930. +vchi_readbuf_uint16(const void *_ptr)
  34931. +{
  34932. + const unsigned char *ptr = _ptr;
  34933. + return ptr[0] | (ptr[1] << 8);
  34934. +}
  34935. +
  34936. +/* ----------------------------------------------------------------------
  34937. + * write a uint16_t into the buffer.
  34938. + * network format is defined to be little endian
  34939. + * -------------------------------------------------------------------- */
  34940. +void
  34941. +vchi_writebuf_uint16(void *_ptr, uint16_t value)
  34942. +{
  34943. + unsigned char *ptr = _ptr;
  34944. + ptr[0] = (value >> 0) & 0xFF;
  34945. + ptr[1] = (value >> 8) & 0xFF;
  34946. +}
  34947. +
  34948. +/***********************************************************
  34949. + * Name: vchi_service_use
  34950. + *
  34951. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  34952. + *
  34953. + * Description: Routine to increment refcount on a service
  34954. + *
  34955. + * Returns: void
  34956. + *
  34957. + ***********************************************************/
  34958. +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
  34959. +{
  34960. + int32_t ret = -1;
  34961. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34962. + if (service)
  34963. + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
  34964. + return ret;
  34965. +}
  34966. +EXPORT_SYMBOL(vchi_service_use);
  34967. +
  34968. +/***********************************************************
  34969. + * Name: vchi_service_release
  34970. + *
  34971. + * Arguments: const VCHI_SERVICE_HANDLE_T handle
  34972. + *
  34973. + * Description: Routine to decrement refcount on a service
  34974. + *
  34975. + * Returns: void
  34976. + *
  34977. + ***********************************************************/
  34978. +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
  34979. +{
  34980. + int32_t ret = -1;
  34981. + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
  34982. + if (service)
  34983. + ret = vchiq_status_to_vchi(
  34984. + vchiq_release_service(service->handle));
  34985. + return ret;
  34986. +}
  34987. +EXPORT_SYMBOL(vchi_service_release);
  34988. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
  34989. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 1970-01-01 01:00:00.000000000 +0100
  34990. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c 2014-09-14 19:03:25.000000000 +0200
  34991. @@ -0,0 +1,152 @@
  34992. +/**
  34993. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  34994. + *
  34995. + * Redistribution and use in source and binary forms, with or without
  34996. + * modification, are permitted provided that the following conditions
  34997. + * are met:
  34998. + * 1. Redistributions of source code must retain the above copyright
  34999. + * notice, this list of conditions, and the following disclaimer,
  35000. + * without modification.
  35001. + * 2. Redistributions in binary form must reproduce the above copyright
  35002. + * notice, this list of conditions and the following disclaimer in the
  35003. + * documentation and/or other materials provided with the distribution.
  35004. + * 3. The names of the above-listed copyright holders may not be used
  35005. + * to endorse or promote products derived from this software without
  35006. + * specific prior written permission.
  35007. + *
  35008. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35009. + * GNU General Public License ("GPL") version 2, as published by the Free
  35010. + * Software Foundation.
  35011. + *
  35012. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35013. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35014. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35015. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35016. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35017. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35018. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35019. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35020. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35021. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35022. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35023. + */
  35024. +
  35025. +#include "vchiq_util.h"
  35026. +#include "vchiq_killable.h"
  35027. +
  35028. +static inline int is_pow2(int i)
  35029. +{
  35030. + return i && !(i & (i - 1));
  35031. +}
  35032. +
  35033. +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
  35034. +{
  35035. + WARN_ON(!is_pow2(size));
  35036. +
  35037. + queue->size = size;
  35038. + queue->read = 0;
  35039. + queue->write = 0;
  35040. +
  35041. + sema_init(&queue->pop, 0);
  35042. + sema_init(&queue->push, 0);
  35043. +
  35044. + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
  35045. + if (queue->storage == NULL) {
  35046. + vchiu_queue_delete(queue);
  35047. + return 0;
  35048. + }
  35049. + return 1;
  35050. +}
  35051. +
  35052. +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
  35053. +{
  35054. + if (queue->storage != NULL)
  35055. + kfree(queue->storage);
  35056. +}
  35057. +
  35058. +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
  35059. +{
  35060. + return queue->read == queue->write;
  35061. +}
  35062. +
  35063. +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
  35064. +{
  35065. + return queue->write == queue->read + queue->size;
  35066. +}
  35067. +
  35068. +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
  35069. +{
  35070. + while (queue->write == queue->read + queue->size) {
  35071. + if (down_interruptible(&queue->pop) != 0) {
  35072. + flush_signals(current);
  35073. + }
  35074. + }
  35075. +
  35076. + /*
  35077. + * Write to queue->storage must be visible after read from
  35078. + * queue->read
  35079. + */
  35080. + smp_mb();
  35081. +
  35082. + queue->storage[queue->write & (queue->size - 1)] = header;
  35083. +
  35084. + /*
  35085. + * Write to queue->storage must be visible before write to
  35086. + * queue->write
  35087. + */
  35088. + smp_wmb();
  35089. +
  35090. + queue->write++;
  35091. +
  35092. + up(&queue->push);
  35093. +}
  35094. +
  35095. +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
  35096. +{
  35097. + while (queue->write == queue->read) {
  35098. + if (down_interruptible(&queue->push) != 0) {
  35099. + flush_signals(current);
  35100. + }
  35101. + }
  35102. +
  35103. + up(&queue->push); // We haven't removed anything from the queue.
  35104. +
  35105. + /*
  35106. + * Read from queue->storage must be visible after read from
  35107. + * queue->write
  35108. + */
  35109. + smp_rmb();
  35110. +
  35111. + return queue->storage[queue->read & (queue->size - 1)];
  35112. +}
  35113. +
  35114. +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
  35115. +{
  35116. + VCHIQ_HEADER_T *header;
  35117. +
  35118. + while (queue->write == queue->read) {
  35119. + if (down_interruptible(&queue->push) != 0) {
  35120. + flush_signals(current);
  35121. + }
  35122. + }
  35123. +
  35124. + /*
  35125. + * Read from queue->storage must be visible after read from
  35126. + * queue->write
  35127. + */
  35128. + smp_rmb();
  35129. +
  35130. + header = queue->storage[queue->read & (queue->size - 1)];
  35131. +
  35132. + /*
  35133. + * Read from queue->storage must be visible before write to
  35134. + * queue->read
  35135. + */
  35136. + smp_mb();
  35137. +
  35138. + queue->read++;
  35139. +
  35140. + up(&queue->pop);
  35141. +
  35142. + return header;
  35143. +}
  35144. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
  35145. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 1970-01-01 01:00:00.000000000 +0100
  35146. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h 2014-09-14 19:03:25.000000000 +0200
  35147. @@ -0,0 +1,81 @@
  35148. +/**
  35149. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35150. + *
  35151. + * Redistribution and use in source and binary forms, with or without
  35152. + * modification, are permitted provided that the following conditions
  35153. + * are met:
  35154. + * 1. Redistributions of source code must retain the above copyright
  35155. + * notice, this list of conditions, and the following disclaimer,
  35156. + * without modification.
  35157. + * 2. Redistributions in binary form must reproduce the above copyright
  35158. + * notice, this list of conditions and the following disclaimer in the
  35159. + * documentation and/or other materials provided with the distribution.
  35160. + * 3. The names of the above-listed copyright holders may not be used
  35161. + * to endorse or promote products derived from this software without
  35162. + * specific prior written permission.
  35163. + *
  35164. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35165. + * GNU General Public License ("GPL") version 2, as published by the Free
  35166. + * Software Foundation.
  35167. + *
  35168. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35169. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35170. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35171. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35172. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35173. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35174. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35175. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35176. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35177. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35178. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35179. + */
  35180. +
  35181. +#ifndef VCHIQ_UTIL_H
  35182. +#define VCHIQ_UTIL_H
  35183. +
  35184. +#include <linux/types.h>
  35185. +#include <linux/semaphore.h>
  35186. +#include <linux/mutex.h>
  35187. +#include <linux/bitops.h>
  35188. +#include <linux/kthread.h>
  35189. +#include <linux/wait.h>
  35190. +#include <linux/vmalloc.h>
  35191. +#include <linux/jiffies.h>
  35192. +#include <linux/delay.h>
  35193. +#include <linux/string.h>
  35194. +#include <linux/types.h>
  35195. +#include <linux/interrupt.h>
  35196. +#include <linux/random.h>
  35197. +#include <linux/sched.h>
  35198. +#include <linux/ctype.h>
  35199. +#include <linux/uaccess.h>
  35200. +#include <linux/time.h> /* for time_t */
  35201. +#include <linux/slab.h>
  35202. +#include <linux/vmalloc.h>
  35203. +
  35204. +#include "vchiq_if.h"
  35205. +
  35206. +typedef struct {
  35207. + int size;
  35208. + int read;
  35209. + int write;
  35210. +
  35211. + struct semaphore pop;
  35212. + struct semaphore push;
  35213. +
  35214. + VCHIQ_HEADER_T **storage;
  35215. +} VCHIU_QUEUE_T;
  35216. +
  35217. +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
  35218. +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
  35219. +
  35220. +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
  35221. +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
  35222. +
  35223. +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
  35224. +
  35225. +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
  35226. +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
  35227. +
  35228. +#endif
  35229. diff -Nur linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
  35230. --- linux-3.16.2/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 1970-01-01 01:00:00.000000000 +0100
  35231. +++ linux-3.16-rpi/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c 2014-04-13 17:32:57.000000000 +0200
  35232. @@ -0,0 +1,59 @@
  35233. +/**
  35234. + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
  35235. + *
  35236. + * Redistribution and use in source and binary forms, with or without
  35237. + * modification, are permitted provided that the following conditions
  35238. + * are met:
  35239. + * 1. Redistributions of source code must retain the above copyright
  35240. + * notice, this list of conditions, and the following disclaimer,
  35241. + * without modification.
  35242. + * 2. Redistributions in binary form must reproduce the above copyright
  35243. + * notice, this list of conditions and the following disclaimer in the
  35244. + * documentation and/or other materials provided with the distribution.
  35245. + * 3. The names of the above-listed copyright holders may not be used
  35246. + * to endorse or promote products derived from this software without
  35247. + * specific prior written permission.
  35248. + *
  35249. + * ALTERNATIVELY, this software may be distributed under the terms of the
  35250. + * GNU General Public License ("GPL") version 2, as published by the Free
  35251. + * Software Foundation.
  35252. + *
  35253. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35254. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  35255. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  35256. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35257. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35258. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35259. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  35260. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35261. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35262. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35263. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35264. + */
  35265. +#include "vchiq_build_info.h"
  35266. +#include <linux/broadcom/vc_debug_sym.h>
  35267. +
  35268. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
  35269. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
  35270. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
  35271. +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
  35272. +
  35273. +const char *vchiq_get_build_hostname( void )
  35274. +{
  35275. + return vchiq_build_hostname;
  35276. +}
  35277. +
  35278. +const char *vchiq_get_build_version( void )
  35279. +{
  35280. + return vchiq_build_version;
  35281. +}
  35282. +
  35283. +const char *vchiq_get_build_date( void )
  35284. +{
  35285. + return vchiq_build_date;
  35286. +}
  35287. +
  35288. +const char *vchiq_get_build_time( void )
  35289. +{
  35290. + return vchiq_build_time;
  35291. +}
  35292. diff -Nur linux-3.16.2/drivers/misc/vc04_services/Kconfig linux-3.16-rpi/drivers/misc/vc04_services/Kconfig
  35293. --- linux-3.16.2/drivers/misc/vc04_services/Kconfig 1970-01-01 01:00:00.000000000 +0100
  35294. +++ linux-3.16-rpi/drivers/misc/vc04_services/Kconfig 2014-09-14 19:03:25.000000000 +0200
  35295. @@ -0,0 +1,9 @@
  35296. +config BCM2708_VCHIQ
  35297. + tristate "Videocore VCHIQ"
  35298. + depends on MACH_BCM2708
  35299. + default y
  35300. + help
  35301. + Kernel to VideoCore communication interface for the
  35302. + BCM2708 family of products.
  35303. + Defaults to Y when the Broadcom Videocore services
  35304. + are included in the build, N otherwise.
  35305. diff -Nur linux-3.16.2/drivers/misc/vc04_services/Makefile linux-3.16-rpi/drivers/misc/vc04_services/Makefile
  35306. --- linux-3.16.2/drivers/misc/vc04_services/Makefile 1970-01-01 01:00:00.000000000 +0100
  35307. +++ linux-3.16-rpi/drivers/misc/vc04_services/Makefile 2014-09-14 19:03:25.000000000 +0200
  35308. @@ -0,0 +1,17 @@
  35309. +ifeq ($(CONFIG_MACH_BCM2708),y)
  35310. +
  35311. +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
  35312. +
  35313. +vchiq-objs := \
  35314. + interface/vchiq_arm/vchiq_core.o \
  35315. + interface/vchiq_arm/vchiq_arm.o \
  35316. + interface/vchiq_arm/vchiq_kern_lib.o \
  35317. + interface/vchiq_arm/vchiq_2835_arm.o \
  35318. + interface/vchiq_arm/vchiq_proc.o \
  35319. + interface/vchiq_arm/vchiq_shim.o \
  35320. + interface/vchiq_arm/vchiq_util.o \
  35321. + interface/vchiq_arm/vchiq_connected.o \
  35322. +
  35323. +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
  35324. +
  35325. +endif
  35326. diff -Nur linux-3.16.2/drivers/mmc/card/block.c linux-3.16-rpi/drivers/mmc/card/block.c
  35327. --- linux-3.16.2/drivers/mmc/card/block.c 2014-09-06 01:37:11.000000000 +0200
  35328. +++ linux-3.16-rpi/drivers/mmc/card/block.c 2014-09-14 19:03:25.000000000 +0200
  35329. @@ -1404,7 +1404,7 @@
  35330. brq->data.blocks = 1;
  35331. }
  35332. - if (brq->data.blocks > 1 || do_rel_wr) {
  35333. + if (brq->data.blocks > 1 || do_rel_wr || card->host->caps2 & MMC_CAP2_FORCE_MULTIBLOCK) {
  35334. /* SPI multiblock writes terminate using a special
  35335. * token, not a STOP_TRANSMISSION request.
  35336. */
  35337. diff -Nur linux-3.16.2/drivers/mmc/core/sd.c linux-3.16-rpi/drivers/mmc/core/sd.c
  35338. --- linux-3.16.2/drivers/mmc/core/sd.c 2014-09-06 01:37:11.000000000 +0200
  35339. +++ linux-3.16-rpi/drivers/mmc/core/sd.c 2014-09-14 19:03:25.000000000 +0200
  35340. @@ -15,6 +15,8 @@
  35341. #include <linux/slab.h>
  35342. #include <linux/stat.h>
  35343. #include <linux/pm_runtime.h>
  35344. +#include <linux/jiffies.h>
  35345. +#include <linux/nmi.h>
  35346. #include <linux/mmc/host.h>
  35347. #include <linux/mmc/card.h>
  35348. @@ -67,6 +69,15 @@
  35349. __res & __mask; \
  35350. })
  35351. +// timeout for tries
  35352. +static const unsigned long retry_timeout_ms= 10*1000;
  35353. +
  35354. +// try at least 10 times, even if timeout is reached
  35355. +static const int retry_min_tries= 10;
  35356. +
  35357. +// delay between tries
  35358. +static const unsigned long retry_delay_ms= 10;
  35359. +
  35360. /*
  35361. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  35362. */
  35363. @@ -219,12 +230,63 @@
  35364. }
  35365. /*
  35366. - * Fetch and process SD Status register.
  35367. + * Fetch and process SD Configuration Register.
  35368. + */
  35369. +static int mmc_read_scr(struct mmc_card *card)
  35370. +{
  35371. + unsigned long timeout_at;
  35372. + int err, tries;
  35373. +
  35374. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  35375. + tries= 0;
  35376. +
  35377. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  35378. + {
  35379. + unsigned long delay_at;
  35380. + tries++;
  35381. +
  35382. + err = mmc_app_send_scr(card, card->raw_scr);
  35383. + if( !err )
  35384. + break; // success!!!
  35385. +
  35386. + touch_nmi_watchdog(); // we are still alive!
  35387. +
  35388. + // delay
  35389. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  35390. + while( time_before( jiffies, delay_at ) )
  35391. + {
  35392. + mdelay( 1 );
  35393. + touch_nmi_watchdog(); // we are still alive!
  35394. + }
  35395. + }
  35396. +
  35397. + if( err)
  35398. + {
  35399. + pr_err("%s: failed to read SD Configuration register (SCR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
  35400. + return err;
  35401. + }
  35402. +
  35403. + if( tries > 1 )
  35404. + {
  35405. + pr_info("%s: could read SD Configuration register (SCR) at the %dth attempt\n", mmc_hostname(card->host), tries );
  35406. + }
  35407. +
  35408. + err = mmc_decode_scr(card);
  35409. + if (err)
  35410. + return err;
  35411. +
  35412. + return err;
  35413. +}
  35414. +
  35415. +/*
  35416. + * Fetch and process SD Status Register.
  35417. */
  35418. static int mmc_read_ssr(struct mmc_card *card)
  35419. {
  35420. + unsigned long timeout_at;
  35421. unsigned int au, es, et, eo;
  35422. int err, i;
  35423. + int tries;
  35424. u32 *ssr;
  35425. if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
  35426. @@ -237,14 +299,40 @@
  35427. if (!ssr)
  35428. return -ENOMEM;
  35429. - err = mmc_app_sd_status(card, ssr);
  35430. - if (err) {
  35431. - pr_warning("%s: problem reading SD Status "
  35432. - "register.\n", mmc_hostname(card->host));
  35433. - err = 0;
  35434. + timeout_at= jiffies + msecs_to_jiffies( retry_timeout_ms );
  35435. + tries= 0;
  35436. +
  35437. + while( tries < retry_min_tries || time_before( jiffies, timeout_at ) )
  35438. + {
  35439. + unsigned long delay_at;
  35440. + tries++;
  35441. +
  35442. + err= mmc_app_sd_status(card, ssr);
  35443. + if( !err )
  35444. + break; // sucess!!!
  35445. +
  35446. + touch_nmi_watchdog(); // we are still alive!
  35447. +
  35448. + // delay
  35449. + delay_at= jiffies + msecs_to_jiffies( retry_delay_ms );
  35450. + while( time_before( jiffies, delay_at ) )
  35451. + {
  35452. + mdelay( 1 );
  35453. + touch_nmi_watchdog(); // we are still alive!
  35454. + }
  35455. + }
  35456. +
  35457. + if( err)
  35458. + {
  35459. + pr_err("%s: failed to read SD Status register (SSR) after %d tries during %lu ms, error %d\n", mmc_hostname(card->host), tries, retry_timeout_ms, err );
  35460. goto out;
  35461. }
  35462. + if( tries > 1 )
  35463. + {
  35464. + pr_info("%s: read SD Status register (SSR) after %d attempts\n", mmc_hostname(card->host), tries );
  35465. + }
  35466. +
  35467. for (i = 0; i < 16; i++)
  35468. ssr[i] = be32_to_cpu(ssr[i]);
  35469. @@ -818,14 +906,10 @@
  35470. if (!reinit) {
  35471. /*
  35472. - * Fetch SCR from card.
  35473. + * Fetch and decode SD Configuration register.
  35474. */
  35475. - err = mmc_app_send_scr(card, card->raw_scr);
  35476. - if (err)
  35477. - return err;
  35478. -
  35479. - err = mmc_decode_scr(card);
  35480. - if (err)
  35481. + err = mmc_read_scr(card);
  35482. + if( err )
  35483. return err;
  35484. /*
  35485. diff -Nur linux-3.16.2/drivers/mmc/host/bcm2835-mmc.c linux-3.16-rpi/drivers/mmc/host/bcm2835-mmc.c
  35486. --- linux-3.16.2/drivers/mmc/host/bcm2835-mmc.c 1970-01-01 01:00:00.000000000 +0100
  35487. +++ linux-3.16-rpi/drivers/mmc/host/bcm2835-mmc.c 2014-09-14 19:03:25.000000000 +0200
  35488. @@ -0,0 +1,1546 @@
  35489. +/*
  35490. + * BCM2835 MMC host driver.
  35491. + *
  35492. + * Author: Gellert Weisz <gellert@raspberrypi.org>
  35493. + * Copyright 2014
  35494. + *
  35495. + * Based on
  35496. + * sdhci-bcm2708.c by Broadcom
  35497. + * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko
  35498. + * sdhci.c and sdhci-pci.c by Pierre Ossman
  35499. + *
  35500. + * This program is free software; you can redistribute it and/or modify it
  35501. + * under the terms and conditions of the GNU General Public License,
  35502. + * version 2, as published by the Free Software Foundation.
  35503. + *
  35504. + * This program is distributed in the hope it will be useful, but WITHOUT
  35505. + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  35506. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  35507. + * more details.
  35508. + *
  35509. + * You should have received a copy of the GNU General Public License
  35510. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35511. + */
  35512. +
  35513. +#ifndef CONFIG_OF
  35514. + #define BCM2835_CLOCK_FREQ 250000000
  35515. +#endif
  35516. +
  35517. +#define DRIVER_NAME "mmc-bcm2835"
  35518. +
  35519. +#define DBG(f, x...) \
  35520. +pr_debug(DRIVER_NAME " [%s()]: " f, __func__, ## x)
  35521. +
  35522. +#ifndef CONFIG_MMC_BCM2835_DMA
  35523. + #define FORCE_PIO
  35524. +#endif
  35525. +
  35526. +
  35527. +/* the inclusive limit in bytes under which PIO will be used instead of DMA */
  35528. +#ifdef CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  35529. +#define PIO_DMA_BARRIER CONFIG_MMC_BCM2835_PIO_DMA_BARRIER
  35530. +#else
  35531. +#define PIO_DMA_BARRIER 00
  35532. +#endif
  35533. +
  35534. +#define MIN_FREQ 400000
  35535. +#define TIMEOUT_VAL 0xE
  35536. +#define BCM2835_SDHCI_WRITE_DELAY(f) (((2 * 1000000) / f) + 1)
  35537. +
  35538. +#ifndef BCM2708_PERI_BASE
  35539. + #define BCM2708_PERI_BASE 0x20000000
  35540. +#endif
  35541. +
  35542. +/* FIXME: Needs IOMMU support */
  35543. +#define BCM2835_VCMMU_SHIFT (0x7E000000 - BCM2708_PERI_BASE)
  35544. +
  35545. +#include <linux/delay.h>
  35546. +#include <linux/module.h>
  35547. +#include <linux/io.h>
  35548. +#include <linux/mmc/mmc.h>
  35549. +#include <linux/mmc/host.h>
  35550. +#include <linux/mmc/sd.h>
  35551. +#include <linux/scatterlist.h>
  35552. +#include <linux/of_address.h>
  35553. +#include <linux/of_irq.h>
  35554. +#include <linux/clk.h>
  35555. +#include <linux/platform_device.h>
  35556. +#include <linux/err.h>
  35557. +#include <linux/blkdev.h>
  35558. +#include <linux/dmaengine.h>
  35559. +#include <linux/dma-mapping.h>
  35560. +#include <linux/of_dma.h>
  35561. +
  35562. +#include "sdhci.h"
  35563. +
  35564. +
  35565. +struct bcm2835_host {
  35566. + spinlock_t lock;
  35567. +
  35568. + void __iomem *ioaddr;
  35569. + u32 phys_addr;
  35570. +
  35571. + struct mmc_host *mmc;
  35572. +
  35573. + u32 timeout;
  35574. +
  35575. + int clock; /* Current clock speed */
  35576. + u8 pwr; /* Current voltage */
  35577. +
  35578. + unsigned int max_clk; /* Max possible freq */
  35579. + unsigned int timeout_clk; /* Timeout freq (KHz) */
  35580. + unsigned int clk_mul; /* Clock Muliplier value */
  35581. +
  35582. + struct tasklet_struct finish_tasklet; /* Tasklet structures */
  35583. +
  35584. + struct timer_list timer; /* Timer for timeouts */
  35585. +
  35586. + struct sg_mapping_iter sg_miter; /* SG state for PIO */
  35587. + unsigned int blocks; /* remaining PIO blocks */
  35588. +
  35589. + int irq; /* Device IRQ */
  35590. +
  35591. +
  35592. + u32 ier; /* cached registers */
  35593. +
  35594. + struct mmc_request *mrq; /* Current request */
  35595. + struct mmc_command *cmd; /* Current command */
  35596. + struct mmc_data *data; /* Current data request */
  35597. + unsigned int data_early:1; /* Data finished before cmd */
  35598. +
  35599. + wait_queue_head_t buf_ready_int; /* Waitqueue for Buffer Read Ready interrupt */
  35600. +
  35601. + u32 thread_isr;
  35602. +
  35603. + u32 shadow;
  35604. +
  35605. + /*DMA part*/
  35606. + struct dma_chan *dma_chan_rx; /* DMA channel for reads */
  35607. + struct dma_chan *dma_chan_tx; /* DMA channel for writes */
  35608. + struct dma_async_tx_descriptor *tx_desc; /* descriptor */
  35609. +
  35610. + bool have_dma;
  35611. + bool use_dma;
  35612. + /*end of DMA part*/
  35613. +
  35614. + int max_delay; /* maximum length of time spent waiting */
  35615. +
  35616. + int flags; /* Host attributes */
  35617. +#define SDHCI_USE_SDMA (1<<0) /* Host is SDMA capable */
  35618. +#define SDHCI_USE_ADMA (1<<1) /* Host is ADMA capable */
  35619. +#define SDHCI_REQ_USE_DMA (1<<2) /* Use DMA for this req. */
  35620. +#define SDHCI_DEVICE_DEAD (1<<3) /* Device unresponsive */
  35621. +#define SDHCI_AUTO_CMD12 (1<<6) /* Auto CMD12 support */
  35622. +#define SDHCI_AUTO_CMD23 (1<<7) /* Auto CMD23 support */
  35623. +#define SDHCI_PV_ENABLED (1<<8) /* Preset value enabled */
  35624. +#define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  35625. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  35626. +};
  35627. +
  35628. +
  35629. +static inline void bcm2835_mmc_writel(struct bcm2835_host *host, u32 val, int reg)
  35630. +{
  35631. + writel(val, host->ioaddr + reg);
  35632. + udelay(BCM2835_SDHCI_WRITE_DELAY(max(host->clock, MIN_FREQ)));
  35633. +}
  35634. +
  35635. +static inline void mmc_raw_writel(struct bcm2835_host *host, u32 val, int reg)
  35636. +{
  35637. + writel(val, host->ioaddr + reg);
  35638. +}
  35639. +
  35640. +static inline u32 bcm2835_mmc_readl(struct bcm2835_host *host, int reg)
  35641. +{
  35642. + return readl(host->ioaddr + reg);
  35643. +}
  35644. +
  35645. +static inline void bcm2835_mmc_writew(struct bcm2835_host *host, u16 val, int reg)
  35646. +{
  35647. + u32 oldval = (reg == SDHCI_COMMAND) ? host->shadow :
  35648. + bcm2835_mmc_readl(host, reg & ~3);
  35649. + u32 word_num = (reg >> 1) & 1;
  35650. + u32 word_shift = word_num * 16;
  35651. + u32 mask = 0xffff << word_shift;
  35652. + u32 newval = (oldval & ~mask) | (val << word_shift);
  35653. +
  35654. + if (reg == SDHCI_TRANSFER_MODE)
  35655. + host->shadow = newval;
  35656. + else
  35657. + bcm2835_mmc_writel(host, newval, reg & ~3);
  35658. +
  35659. +}
  35660. +
  35661. +static inline void bcm2835_mmc_writeb(struct bcm2835_host *host, u8 val, int reg)
  35662. +{
  35663. + u32 oldval = bcm2835_mmc_readl(host, reg & ~3);
  35664. + u32 byte_num = reg & 3;
  35665. + u32 byte_shift = byte_num * 8;
  35666. + u32 mask = 0xff << byte_shift;
  35667. + u32 newval = (oldval & ~mask) | (val << byte_shift);
  35668. +
  35669. + bcm2835_mmc_writel(host, newval, reg & ~3);
  35670. +}
  35671. +
  35672. +
  35673. +static inline u16 bcm2835_mmc_readw(struct bcm2835_host *host, int reg)
  35674. +{
  35675. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  35676. + u32 word_num = (reg >> 1) & 1;
  35677. + u32 word_shift = word_num * 16;
  35678. + u32 word = (val >> word_shift) & 0xffff;
  35679. +
  35680. + return word;
  35681. +}
  35682. +
  35683. +static inline u8 bcm2835_mmc_readb(struct bcm2835_host *host, int reg)
  35684. +{
  35685. + u32 val = bcm2835_mmc_readl(host, (reg & ~3));
  35686. + u32 byte_num = reg & 3;
  35687. + u32 byte_shift = byte_num * 8;
  35688. + u32 byte = (val >> byte_shift) & 0xff;
  35689. +
  35690. + return byte;
  35691. +}
  35692. +
  35693. +static void bcm2835_mmc_unsignal_irqs(struct bcm2835_host *host, u32 clear)
  35694. +{
  35695. + u32 ier;
  35696. +
  35697. + ier = bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE);
  35698. + ier &= ~clear;
  35699. + /* change which requests generate IRQs - makes no difference to
  35700. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  35701. + bcm2835_mmc_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  35702. +}
  35703. +
  35704. +
  35705. +static void bcm2835_mmc_dumpregs(struct bcm2835_host *host)
  35706. +{
  35707. + pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
  35708. + mmc_hostname(host->mmc));
  35709. +
  35710. + pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
  35711. + bcm2835_mmc_readl(host, SDHCI_DMA_ADDRESS),
  35712. + bcm2835_mmc_readw(host, SDHCI_HOST_VERSION));
  35713. + pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  35714. + bcm2835_mmc_readw(host, SDHCI_BLOCK_SIZE),
  35715. + bcm2835_mmc_readw(host, SDHCI_BLOCK_COUNT));
  35716. + pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
  35717. + bcm2835_mmc_readl(host, SDHCI_ARGUMENT),
  35718. + bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE));
  35719. + pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
  35720. + bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE),
  35721. + bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL));
  35722. + pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
  35723. + bcm2835_mmc_readb(host, SDHCI_POWER_CONTROL),
  35724. + bcm2835_mmc_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  35725. + pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
  35726. + bcm2835_mmc_readb(host, SDHCI_WAKE_UP_CONTROL),
  35727. + bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL));
  35728. + pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
  35729. + bcm2835_mmc_readb(host, SDHCI_TIMEOUT_CONTROL),
  35730. + bcm2835_mmc_readl(host, SDHCI_INT_STATUS));
  35731. + pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
  35732. + bcm2835_mmc_readl(host, SDHCI_INT_ENABLE),
  35733. + bcm2835_mmc_readl(host, SDHCI_SIGNAL_ENABLE));
  35734. + pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
  35735. + bcm2835_mmc_readw(host, SDHCI_ACMD12_ERR),
  35736. + bcm2835_mmc_readw(host, SDHCI_SLOT_INT_STATUS));
  35737. + pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
  35738. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES),
  35739. + bcm2835_mmc_readl(host, SDHCI_CAPABILITIES_1));
  35740. + pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
  35741. + bcm2835_mmc_readw(host, SDHCI_COMMAND),
  35742. + bcm2835_mmc_readl(host, SDHCI_MAX_CURRENT));
  35743. + pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
  35744. + bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2));
  35745. +
  35746. + pr_debug(DRIVER_NAME ": ===========================================\n");
  35747. +}
  35748. +
  35749. +
  35750. +static void bcm2835_mmc_reset(struct bcm2835_host *host, u8 mask)
  35751. +{
  35752. + unsigned long timeout;
  35753. +
  35754. + bcm2835_mmc_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  35755. +
  35756. + if (mask & SDHCI_RESET_ALL)
  35757. + host->clock = 0;
  35758. +
  35759. + /* Wait max 100 ms */
  35760. + timeout = 100;
  35761. +
  35762. + /* hw clears the bit when it's done */
  35763. + while (bcm2835_mmc_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  35764. + if (timeout == 0) {
  35765. + pr_err("%s: Reset 0x%x never completed.\n",
  35766. + mmc_hostname(host->mmc), (int)mask);
  35767. + bcm2835_mmc_dumpregs(host);
  35768. + return;
  35769. + }
  35770. + timeout--;
  35771. + mdelay(1);
  35772. + }
  35773. +
  35774. + if (100-timeout > 10 && 100-timeout > host->max_delay) {
  35775. + host->max_delay = 100-timeout;
  35776. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  35777. + }
  35778. +}
  35779. +
  35780. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
  35781. +
  35782. +static void bcm2835_mmc_init(struct bcm2835_host *host, int soft)
  35783. +{
  35784. + if (soft)
  35785. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
  35786. + else
  35787. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  35788. +
  35789. + host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  35790. + SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  35791. + SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  35792. + SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  35793. + SDHCI_INT_RESPONSE;
  35794. +
  35795. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  35796. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  35797. +
  35798. + if (soft) {
  35799. + /* force clock reconfiguration */
  35800. + host->clock = 0;
  35801. + bcm2835_mmc_set_ios(host->mmc, &host->mmc->ios);
  35802. + }
  35803. +}
  35804. +
  35805. +
  35806. +
  35807. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host);
  35808. +
  35809. +static void bcm2835_mmc_dma_complete(void *param)
  35810. +{
  35811. + struct bcm2835_host *host = param;
  35812. + struct dma_chan *dma_chan;
  35813. + unsigned long flags;
  35814. + u32 dir_data;
  35815. +
  35816. + spin_lock_irqsave(&host->lock, flags);
  35817. +
  35818. + if (host->data && !(host->data->flags & MMC_DATA_WRITE)) {
  35819. + /* otherwise handled in SDHCI IRQ */
  35820. + dma_chan = host->dma_chan_rx;
  35821. + dir_data = DMA_FROM_DEVICE;
  35822. +
  35823. + dma_unmap_sg(dma_chan->device->dev,
  35824. + host->data->sg, host->data->sg_len,
  35825. + dir_data);
  35826. +
  35827. + bcm2835_mmc_finish_data(host);
  35828. + }
  35829. +
  35830. + spin_unlock_irqrestore(&host->lock, flags);
  35831. +}
  35832. +
  35833. +static void bcm2835_bcm2835_mmc_read_block_pio(struct bcm2835_host *host)
  35834. +{
  35835. + unsigned long flags;
  35836. + size_t blksize, len, chunk;
  35837. +
  35838. + u32 uninitialized_var(scratch);
  35839. + u8 *buf;
  35840. +
  35841. + blksize = host->data->blksz;
  35842. + chunk = 0;
  35843. +
  35844. + local_irq_save(flags);
  35845. +
  35846. + while (blksize) {
  35847. + if (!sg_miter_next(&host->sg_miter))
  35848. + BUG();
  35849. +
  35850. + len = min(host->sg_miter.length, blksize);
  35851. +
  35852. + blksize -= len;
  35853. + host->sg_miter.consumed = len;
  35854. +
  35855. + buf = host->sg_miter.addr;
  35856. +
  35857. + while (len) {
  35858. + if (chunk == 0) {
  35859. + scratch = bcm2835_mmc_readl(host, SDHCI_BUFFER);
  35860. + chunk = 4;
  35861. + }
  35862. +
  35863. + *buf = scratch & 0xFF;
  35864. +
  35865. + buf++;
  35866. + scratch >>= 8;
  35867. + chunk--;
  35868. + len--;
  35869. + }
  35870. + }
  35871. +
  35872. + sg_miter_stop(&host->sg_miter);
  35873. +
  35874. + local_irq_restore(flags);
  35875. +}
  35876. +
  35877. +static void bcm2835_bcm2835_mmc_write_block_pio(struct bcm2835_host *host)
  35878. +{
  35879. + unsigned long flags;
  35880. + size_t blksize, len, chunk;
  35881. + u32 scratch;
  35882. + u8 *buf;
  35883. +
  35884. + blksize = host->data->blksz;
  35885. + chunk = 0;
  35886. + chunk = 0;
  35887. + scratch = 0;
  35888. +
  35889. + local_irq_save(flags);
  35890. +
  35891. + while (blksize) {
  35892. + if (!sg_miter_next(&host->sg_miter))
  35893. + BUG();
  35894. +
  35895. + len = min(host->sg_miter.length, blksize);
  35896. +
  35897. + blksize -= len;
  35898. + host->sg_miter.consumed = len;
  35899. +
  35900. + buf = host->sg_miter.addr;
  35901. +
  35902. + while (len) {
  35903. + scratch |= (u32)*buf << (chunk * 8);
  35904. +
  35905. + buf++;
  35906. + chunk++;
  35907. + len--;
  35908. +
  35909. + if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  35910. + mmc_raw_writel(host, scratch, SDHCI_BUFFER);
  35911. + chunk = 0;
  35912. + scratch = 0;
  35913. + }
  35914. + }
  35915. + }
  35916. +
  35917. + sg_miter_stop(&host->sg_miter);
  35918. +
  35919. + local_irq_restore(flags);
  35920. +}
  35921. +
  35922. +
  35923. +static void bcm2835_mmc_transfer_pio(struct bcm2835_host *host)
  35924. +{
  35925. + u32 mask;
  35926. +
  35927. + BUG_ON(!host->data);
  35928. +
  35929. + if (host->blocks == 0)
  35930. + return;
  35931. +
  35932. + if (host->data->flags & MMC_DATA_READ)
  35933. + mask = SDHCI_DATA_AVAILABLE;
  35934. + else
  35935. + mask = SDHCI_SPACE_AVAILABLE;
  35936. +
  35937. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  35938. +
  35939. + if (host->data->flags & MMC_DATA_READ)
  35940. + bcm2835_bcm2835_mmc_read_block_pio(host);
  35941. + else
  35942. + bcm2835_bcm2835_mmc_write_block_pio(host);
  35943. +
  35944. + host->blocks--;
  35945. +
  35946. + /* QUIRK used in sdhci.c removes the 'if' */
  35947. + /* but it seems this is unnecessary */
  35948. + if (host->blocks == 0)
  35949. + break;
  35950. +
  35951. +
  35952. + }
  35953. +}
  35954. +
  35955. +
  35956. +static void bcm2835_mmc_transfer_dma(struct bcm2835_host *host)
  35957. +{
  35958. + u32 len, dir_data, dir_slave;
  35959. + struct dma_async_tx_descriptor *desc = NULL;
  35960. + struct dma_chan *dma_chan;
  35961. +
  35962. +
  35963. + WARN_ON(!host->data);
  35964. +
  35965. + if (!host->data)
  35966. + return;
  35967. +
  35968. + if (host->blocks == 0)
  35969. + return;
  35970. +
  35971. + if (host->data->flags & MMC_DATA_READ) {
  35972. + dma_chan = host->dma_chan_rx;
  35973. + dir_data = DMA_FROM_DEVICE;
  35974. + dir_slave = DMA_DEV_TO_MEM;
  35975. + } else {
  35976. + dma_chan = host->dma_chan_tx;
  35977. + dir_data = DMA_TO_DEVICE;
  35978. + dir_slave = DMA_MEM_TO_DEV;
  35979. + }
  35980. +
  35981. + BUG_ON(!dma_chan->device);
  35982. + BUG_ON(!dma_chan->device->dev);
  35983. + BUG_ON(!host->data->sg);
  35984. +
  35985. + len = dma_map_sg(dma_chan->device->dev, host->data->sg,
  35986. + host->data->sg_len, dir_data);
  35987. + if (len > 0) {
  35988. + desc = dmaengine_prep_slave_sg(dma_chan, host->data->sg,
  35989. + len, dir_slave,
  35990. + DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  35991. + } else {
  35992. + dev_err(mmc_dev(host->mmc), "dma_map_sg returned zero length\n");
  35993. + }
  35994. + if (desc) {
  35995. + bcm2835_mmc_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  35996. + SDHCI_INT_SPACE_AVAIL);
  35997. + host->tx_desc = desc;
  35998. + desc->callback = bcm2835_mmc_dma_complete;
  35999. + desc->callback_param = host;
  36000. + dmaengine_submit(desc);
  36001. + dma_async_issue_pending(dma_chan);
  36002. + }
  36003. +
  36004. +}
  36005. +
  36006. +
  36007. +
  36008. +static void bcm2835_mmc_set_transfer_irqs(struct bcm2835_host *host)
  36009. +{
  36010. + u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  36011. + u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  36012. +
  36013. + if (host->use_dma)
  36014. + host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  36015. + else
  36016. + host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  36017. +
  36018. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  36019. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  36020. +}
  36021. +
  36022. +
  36023. +static void bcm2835_mmc_prepare_data(struct bcm2835_host *host, struct mmc_command *cmd)
  36024. +{
  36025. + u8 count;
  36026. + struct mmc_data *data = cmd->data;
  36027. +
  36028. + WARN_ON(host->data);
  36029. +
  36030. + if (data || (cmd->flags & MMC_RSP_BUSY)) {
  36031. + count = TIMEOUT_VAL;
  36032. + bcm2835_mmc_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  36033. + }
  36034. +
  36035. + if (!data)
  36036. + return;
  36037. +
  36038. + /* Sanity checks */
  36039. + BUG_ON(data->blksz * data->blocks > 524288);
  36040. + BUG_ON(data->blksz > host->mmc->max_blk_size);
  36041. + BUG_ON(data->blocks > 65535);
  36042. +
  36043. + host->data = data;
  36044. + host->data_early = 0;
  36045. + host->data->bytes_xfered = 0;
  36046. +
  36047. +
  36048. + if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  36049. + int flags;
  36050. +
  36051. + flags = SG_MITER_ATOMIC;
  36052. + if (host->data->flags & MMC_DATA_READ)
  36053. + flags |= SG_MITER_TO_SG;
  36054. + else
  36055. + flags |= SG_MITER_FROM_SG;
  36056. + sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  36057. + host->blocks = data->blocks;
  36058. + }
  36059. +
  36060. + host->use_dma = host->have_dma && data->blocks > PIO_DMA_BARRIER;
  36061. +
  36062. + bcm2835_mmc_set_transfer_irqs(host);
  36063. +
  36064. + /* Set the DMA boundary value and block size */
  36065. + bcm2835_mmc_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
  36066. + data->blksz), SDHCI_BLOCK_SIZE);
  36067. + bcm2835_mmc_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  36068. +
  36069. + BUG_ON(!host->data);
  36070. +}
  36071. +
  36072. +static void bcm2835_mmc_set_transfer_mode(struct bcm2835_host *host,
  36073. + struct mmc_command *cmd)
  36074. +{
  36075. + u16 mode;
  36076. + struct mmc_data *data = cmd->data;
  36077. +
  36078. + if (data == NULL) {
  36079. + /* clear Auto CMD settings for no data CMDs */
  36080. + mode = bcm2835_mmc_readw(host, SDHCI_TRANSFER_MODE);
  36081. + bcm2835_mmc_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  36082. + SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  36083. + return;
  36084. + }
  36085. +
  36086. + WARN_ON(!host->data);
  36087. +
  36088. + mode = SDHCI_TRNS_BLK_CNT_EN;
  36089. +
  36090. + if ((mmc_op_multi(cmd->opcode) || data->blocks > 1)) {
  36091. + mode |= SDHCI_TRNS_MULTI;
  36092. +
  36093. + /*
  36094. + * If we are sending CMD23, CMD12 never gets sent
  36095. + * on successful completion (so no Auto-CMD12).
  36096. + */
  36097. + if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12))
  36098. + mode |= SDHCI_TRNS_AUTO_CMD12;
  36099. + else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  36100. + mode |= SDHCI_TRNS_AUTO_CMD23;
  36101. + bcm2835_mmc_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
  36102. + }
  36103. + }
  36104. +
  36105. + if (data->flags & MMC_DATA_READ)
  36106. + mode |= SDHCI_TRNS_READ;
  36107. + if (host->flags & SDHCI_REQ_USE_DMA)
  36108. + mode |= SDHCI_TRNS_DMA;
  36109. +
  36110. + bcm2835_mmc_writew(host, mode, SDHCI_TRANSFER_MODE);
  36111. +}
  36112. +
  36113. +void bcm2835_mmc_send_command(struct bcm2835_host *host, struct mmc_command *cmd)
  36114. +{
  36115. + int flags;
  36116. + u32 mask;
  36117. + unsigned long timeout;
  36118. +
  36119. + WARN_ON(host->cmd);
  36120. +
  36121. + /* Wait max 10 ms */
  36122. + timeout = 1000;
  36123. +
  36124. + mask = SDHCI_CMD_INHIBIT;
  36125. + if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  36126. + mask |= SDHCI_DATA_INHIBIT;
  36127. +
  36128. + /* We shouldn't wait for data inihibit for stop commands, even
  36129. + though they might use busy signaling */
  36130. + if (host->mrq->data && (cmd == host->mrq->data->stop))
  36131. + mask &= ~SDHCI_DATA_INHIBIT;
  36132. +
  36133. + while (bcm2835_mmc_readl(host, SDHCI_PRESENT_STATE) & mask) {
  36134. + if (timeout == 0) {
  36135. + pr_err("%s: Controller never released inhibit bit(s).\n",
  36136. + mmc_hostname(host->mmc));
  36137. + bcm2835_mmc_dumpregs(host);
  36138. + cmd->error = -EIO;
  36139. + tasklet_schedule(&host->finish_tasklet);
  36140. + return;
  36141. + }
  36142. + timeout--;
  36143. + udelay(10);
  36144. + }
  36145. +
  36146. + if ((1000-timeout)/100 > 1 && (1000-timeout)/100 > host->max_delay) {
  36147. + host->max_delay = (1000-timeout)/100;
  36148. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  36149. + }
  36150. +
  36151. + timeout = jiffies;
  36152. +#ifdef CONFIG_OF
  36153. + if (!cmd->data && cmd->busy_timeout > 9000)
  36154. + timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  36155. + else
  36156. +#endif
  36157. + timeout += 10 * HZ;
  36158. + mod_timer(&host->timer, timeout);
  36159. +
  36160. + host->cmd = cmd;
  36161. +
  36162. + bcm2835_mmc_prepare_data(host, cmd);
  36163. +
  36164. + bcm2835_mmc_writel(host, cmd->arg, SDHCI_ARGUMENT);
  36165. +
  36166. + bcm2835_mmc_set_transfer_mode(host, cmd);
  36167. +
  36168. + if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  36169. + pr_err("%s: Unsupported response type!\n",
  36170. + mmc_hostname(host->mmc));
  36171. + cmd->error = -EINVAL;
  36172. + tasklet_schedule(&host->finish_tasklet);
  36173. + return;
  36174. + }
  36175. +
  36176. + if (!(cmd->flags & MMC_RSP_PRESENT))
  36177. + flags = SDHCI_CMD_RESP_NONE;
  36178. + else if (cmd->flags & MMC_RSP_136)
  36179. + flags = SDHCI_CMD_RESP_LONG;
  36180. + else if (cmd->flags & MMC_RSP_BUSY)
  36181. + flags = SDHCI_CMD_RESP_SHORT_BUSY;
  36182. + else
  36183. + flags = SDHCI_CMD_RESP_SHORT;
  36184. +
  36185. + if (cmd->flags & MMC_RSP_CRC)
  36186. + flags |= SDHCI_CMD_CRC;
  36187. + if (cmd->flags & MMC_RSP_OPCODE)
  36188. + flags |= SDHCI_CMD_INDEX;
  36189. +
  36190. + if (cmd->data)
  36191. + flags |= SDHCI_CMD_DATA;
  36192. +
  36193. + bcm2835_mmc_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  36194. +}
  36195. +
  36196. +
  36197. +static void bcm2835_mmc_finish_data(struct bcm2835_host *host)
  36198. +{
  36199. + struct mmc_data *data;
  36200. +
  36201. + BUG_ON(!host->data);
  36202. +
  36203. + data = host->data;
  36204. + host->data = NULL;
  36205. +
  36206. + if (data->error)
  36207. + data->bytes_xfered = 0;
  36208. + else
  36209. + data->bytes_xfered = data->blksz * data->blocks;
  36210. +
  36211. + /*
  36212. + * Need to send CMD12 if -
  36213. + * a) open-ended multiblock transfer (no CMD23)
  36214. + * b) error in multiblock transfer
  36215. + */
  36216. + if (data->stop &&
  36217. + (data->error ||
  36218. + !host->mrq->sbc)) {
  36219. +
  36220. + /*
  36221. + * The controller needs a reset of internal state machines
  36222. + * upon error conditions.
  36223. + */
  36224. + if (data->error) {
  36225. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  36226. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  36227. + }
  36228. +
  36229. + bcm2835_mmc_send_command(host, data->stop);
  36230. + } else
  36231. + tasklet_schedule(&host->finish_tasklet);
  36232. +}
  36233. +
  36234. +static void bcm2835_mmc_finish_command(struct bcm2835_host *host)
  36235. +{
  36236. + int i;
  36237. +
  36238. + BUG_ON(host->cmd == NULL);
  36239. +
  36240. + if (host->cmd->flags & MMC_RSP_PRESENT) {
  36241. + if (host->cmd->flags & MMC_RSP_136) {
  36242. + /* CRC is stripped so we need to do some shifting. */
  36243. + for (i = 0; i < 4; i++) {
  36244. + host->cmd->resp[i] = bcm2835_mmc_readl(host,
  36245. + SDHCI_RESPONSE + (3-i)*4) << 8;
  36246. + if (i != 3)
  36247. + host->cmd->resp[i] |=
  36248. + bcm2835_mmc_readb(host,
  36249. + SDHCI_RESPONSE + (3-i)*4-1);
  36250. + }
  36251. + } else {
  36252. + host->cmd->resp[0] = bcm2835_mmc_readl(host, SDHCI_RESPONSE);
  36253. + }
  36254. + }
  36255. +
  36256. + host->cmd->error = 0;
  36257. +
  36258. + /* Finished CMD23, now send actual command. */
  36259. + if (host->cmd == host->mrq->sbc) {
  36260. + host->cmd = NULL;
  36261. + bcm2835_mmc_send_command(host, host->mrq->cmd);
  36262. + } else {
  36263. +
  36264. + /* Processed actual command. */
  36265. + if (host->data && host->data_early)
  36266. + bcm2835_mmc_finish_data(host);
  36267. +
  36268. + if (!host->cmd->data)
  36269. + tasklet_schedule(&host->finish_tasklet);
  36270. +
  36271. + host->cmd = NULL;
  36272. + }
  36273. +}
  36274. +
  36275. +
  36276. +static void bcm2835_mmc_timeout_timer(unsigned long data)
  36277. +{
  36278. + struct bcm2835_host *host;
  36279. + unsigned long flags;
  36280. +
  36281. + host = (struct bcm2835_host *)data;
  36282. +
  36283. + spin_lock_irqsave(&host->lock, flags);
  36284. +
  36285. + if (host->mrq) {
  36286. + pr_err("%s: Timeout waiting for hardware interrupt.\n",
  36287. + mmc_hostname(host->mmc));
  36288. + bcm2835_mmc_dumpregs(host);
  36289. +
  36290. + if (host->data) {
  36291. + host->data->error = -ETIMEDOUT;
  36292. + bcm2835_mmc_finish_data(host);
  36293. + } else {
  36294. + if (host->cmd)
  36295. + host->cmd->error = -ETIMEDOUT;
  36296. + else
  36297. + host->mrq->cmd->error = -ETIMEDOUT;
  36298. +
  36299. + tasklet_schedule(&host->finish_tasklet);
  36300. + }
  36301. + }
  36302. +
  36303. + mmiowb();
  36304. + spin_unlock_irqrestore(&host->lock, flags);
  36305. +}
  36306. +
  36307. +
  36308. +static void bcm2835_mmc_enable_sdio_irq_nolock(struct bcm2835_host *host, int enable)
  36309. +{
  36310. + if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  36311. + if (enable)
  36312. + host->ier |= SDHCI_INT_CARD_INT;
  36313. + else
  36314. + host->ier &= ~SDHCI_INT_CARD_INT;
  36315. +
  36316. + bcm2835_mmc_writel(host, host->ier, SDHCI_INT_ENABLE);
  36317. + bcm2835_mmc_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  36318. + mmiowb();
  36319. + }
  36320. +}
  36321. +
  36322. +static void bcm2835_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
  36323. +{
  36324. + struct bcm2835_host *host = mmc_priv(mmc);
  36325. + unsigned long flags;
  36326. +
  36327. + spin_lock_irqsave(&host->lock, flags);
  36328. + if (enable)
  36329. + host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  36330. + else
  36331. + host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  36332. +
  36333. + bcm2835_mmc_enable_sdio_irq_nolock(host, enable);
  36334. + spin_unlock_irqrestore(&host->lock, flags);
  36335. +}
  36336. +
  36337. +static void bcm2835_mmc_cmd_irq(struct bcm2835_host *host, u32 intmask)
  36338. +{
  36339. +
  36340. + BUG_ON(intmask == 0);
  36341. +
  36342. + if (!host->cmd) {
  36343. + pr_err("%s: Got command interrupt 0x%08x even "
  36344. + "though no command operation was in progress.\n",
  36345. + mmc_hostname(host->mmc), (unsigned)intmask);
  36346. + bcm2835_mmc_dumpregs(host);
  36347. + return;
  36348. + }
  36349. +
  36350. + if (intmask & SDHCI_INT_TIMEOUT)
  36351. + host->cmd->error = -ETIMEDOUT;
  36352. + else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
  36353. + SDHCI_INT_INDEX)) {
  36354. + host->cmd->error = -EILSEQ;
  36355. + }
  36356. +
  36357. + if (host->cmd->error) {
  36358. + tasklet_schedule(&host->finish_tasklet);
  36359. + return;
  36360. + }
  36361. +
  36362. + if (intmask & SDHCI_INT_RESPONSE)
  36363. + bcm2835_mmc_finish_command(host);
  36364. +
  36365. +}
  36366. +
  36367. +static void bcm2835_mmc_data_irq(struct bcm2835_host *host, u32 intmask)
  36368. +{
  36369. + struct dma_chan *dma_chan;
  36370. + u32 dir_data;
  36371. +
  36372. + BUG_ON(intmask == 0);
  36373. +
  36374. + if (!host->data) {
  36375. + /*
  36376. + * The "data complete" interrupt is also used to
  36377. + * indicate that a busy state has ended. See comment
  36378. + * above in sdhci_cmd_irq().
  36379. + */
  36380. + if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
  36381. + if (intmask & SDHCI_INT_DATA_END) {
  36382. + bcm2835_mmc_finish_command(host);
  36383. + return;
  36384. + }
  36385. + }
  36386. +
  36387. + pr_debug("%s: Got data interrupt 0x%08x even "
  36388. + "though no data operation was in progress.\n",
  36389. + mmc_hostname(host->mmc), (unsigned)intmask);
  36390. + bcm2835_mmc_dumpregs(host);
  36391. +
  36392. + return;
  36393. + }
  36394. +
  36395. + if (intmask & SDHCI_INT_DATA_TIMEOUT)
  36396. + host->data->error = -ETIMEDOUT;
  36397. + else if (intmask & SDHCI_INT_DATA_END_BIT)
  36398. + host->data->error = -EILSEQ;
  36399. + else if ((intmask & SDHCI_INT_DATA_CRC) &&
  36400. + SDHCI_GET_CMD(bcm2835_mmc_readw(host, SDHCI_COMMAND))
  36401. + != MMC_BUS_TEST_R)
  36402. + host->data->error = -EILSEQ;
  36403. +
  36404. + if (host->use_dma) {
  36405. + if (host->data->flags & MMC_DATA_WRITE) {
  36406. + /* IRQ handled here */
  36407. +
  36408. + dma_chan = host->dma_chan_tx;
  36409. + dir_data = DMA_TO_DEVICE;
  36410. + dma_unmap_sg(dma_chan->device->dev,
  36411. + host->data->sg, host->data->sg_len,
  36412. + dir_data);
  36413. +
  36414. + bcm2835_mmc_finish_data(host);
  36415. + }
  36416. +
  36417. + } else {
  36418. + if (host->data->error)
  36419. + bcm2835_mmc_finish_data(host);
  36420. + else {
  36421. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  36422. + bcm2835_mmc_transfer_pio(host);
  36423. +
  36424. + if (intmask & SDHCI_INT_DATA_END) {
  36425. + if (host->cmd) {
  36426. + /*
  36427. + * Data managed to finish before the
  36428. + * command completed. Make sure we do
  36429. + * things in the proper order.
  36430. + */
  36431. + host->data_early = 1;
  36432. + } else {
  36433. + bcm2835_mmc_finish_data(host);
  36434. + }
  36435. + }
  36436. + }
  36437. + }
  36438. +}
  36439. +
  36440. +
  36441. +static irqreturn_t bcm2835_mmc_irq(int irq, void *dev_id)
  36442. +{
  36443. + irqreturn_t result = IRQ_NONE;
  36444. + struct bcm2835_host *host = dev_id;
  36445. + u32 intmask, mask, unexpected = 0;
  36446. + int max_loops = 16;
  36447. +#ifndef CONFIG_OF
  36448. + int cardint = 0;
  36449. +#endif
  36450. +
  36451. + spin_lock(&host->lock);
  36452. +
  36453. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  36454. +
  36455. + if (!intmask || intmask == 0xffffffff) {
  36456. + result = IRQ_NONE;
  36457. + goto out;
  36458. + }
  36459. +
  36460. + do {
  36461. + /* Clear selected interrupts. */
  36462. + mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  36463. + SDHCI_INT_BUS_POWER);
  36464. + bcm2835_mmc_writel(host, mask, SDHCI_INT_STATUS);
  36465. +
  36466. +
  36467. + if (intmask & SDHCI_INT_CMD_MASK)
  36468. + bcm2835_mmc_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  36469. +
  36470. + if (intmask & SDHCI_INT_DATA_MASK)
  36471. + bcm2835_mmc_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  36472. +
  36473. + if (intmask & SDHCI_INT_BUS_POWER)
  36474. + pr_err("%s: Card is consuming too much power!\n",
  36475. + mmc_hostname(host->mmc));
  36476. +
  36477. + if (intmask & SDHCI_INT_CARD_INT) {
  36478. +#ifndef CONFIG_OF
  36479. + cardint = 1;
  36480. +#else
  36481. + bcm2835_mmc_enable_sdio_irq_nolock(host, false);
  36482. + host->thread_isr |= SDHCI_INT_CARD_INT;
  36483. + result = IRQ_WAKE_THREAD;
  36484. +#endif
  36485. + }
  36486. +
  36487. + intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  36488. + SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  36489. + SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  36490. + SDHCI_INT_CARD_INT);
  36491. +
  36492. + if (intmask) {
  36493. + unexpected |= intmask;
  36494. + bcm2835_mmc_writel(host, intmask, SDHCI_INT_STATUS);
  36495. + }
  36496. +
  36497. + if (result == IRQ_NONE)
  36498. + result = IRQ_HANDLED;
  36499. +
  36500. + intmask = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  36501. + } while (intmask && --max_loops);
  36502. +out:
  36503. + spin_unlock(&host->lock);
  36504. +
  36505. + if (unexpected) {
  36506. + pr_err("%s: Unexpected interrupt 0x%08x.\n",
  36507. + mmc_hostname(host->mmc), unexpected);
  36508. + bcm2835_mmc_dumpregs(host);
  36509. + }
  36510. +
  36511. +#ifndef CONFIG_OF
  36512. + if (cardint)
  36513. + mmc_signal_sdio_irq(host->mmc);
  36514. +#endif
  36515. +
  36516. + return result;
  36517. +}
  36518. +
  36519. +#ifdef CONFIG_OF
  36520. +static irqreturn_t bcm2835_mmc_thread_irq(int irq, void *dev_id)
  36521. +{
  36522. + struct bcm2835_host *host = dev_id;
  36523. + unsigned long flags;
  36524. + u32 isr;
  36525. +
  36526. + spin_lock_irqsave(&host->lock, flags);
  36527. + isr = host->thread_isr;
  36528. + host->thread_isr = 0;
  36529. + spin_unlock_irqrestore(&host->lock, flags);
  36530. +
  36531. + if (isr & SDHCI_INT_CARD_INT) {
  36532. + sdio_run_irqs(host->mmc);
  36533. +
  36534. + spin_lock_irqsave(&host->lock, flags);
  36535. + if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  36536. + bcm2835_mmc_enable_sdio_irq_nolock(host, true);
  36537. + spin_unlock_irqrestore(&host->lock, flags);
  36538. + }
  36539. +
  36540. + return isr ? IRQ_HANDLED : IRQ_NONE;
  36541. +}
  36542. +#endif
  36543. +
  36544. +
  36545. +
  36546. +void bcm2835_mmc_set_clock(struct bcm2835_host *host, unsigned int clock)
  36547. +{
  36548. + int div = 0; /* Initialized for compiler warning */
  36549. + int real_div = div, clk_mul = 1;
  36550. + u16 clk = 0;
  36551. + unsigned long timeout;
  36552. +
  36553. +
  36554. + host->mmc->actual_clock = 0;
  36555. +
  36556. + bcm2835_mmc_writew(host, 0, SDHCI_CLOCK_CONTROL);
  36557. +
  36558. + if (clock == 0)
  36559. + return;
  36560. +
  36561. + /* Version 3.00 divisors must be a multiple of 2. */
  36562. + if (host->max_clk <= clock)
  36563. + div = 1;
  36564. + else {
  36565. + for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  36566. + div += 2) {
  36567. + if ((host->max_clk / div) <= clock)
  36568. + break;
  36569. + }
  36570. + }
  36571. +
  36572. + real_div = div;
  36573. + div >>= 1;
  36574. +
  36575. + if (real_div)
  36576. + host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
  36577. +
  36578. + clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  36579. + clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  36580. + << SDHCI_DIVIDER_HI_SHIFT;
  36581. + clk |= SDHCI_CLOCK_INT_EN;
  36582. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  36583. +
  36584. + /* Wait max 20 ms */
  36585. + timeout = 20;
  36586. + while (!((clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL))
  36587. + & SDHCI_CLOCK_INT_STABLE)) {
  36588. + if (timeout == 0) {
  36589. + pr_err("%s: Internal clock never "
  36590. + "stabilised.\n", mmc_hostname(host->mmc));
  36591. + bcm2835_mmc_dumpregs(host);
  36592. + return;
  36593. + }
  36594. + timeout--;
  36595. + mdelay(1);
  36596. + }
  36597. +
  36598. + if (20-timeout > 10 && 20-timeout > host->max_delay) {
  36599. + host->max_delay = 20-timeout;
  36600. + pr_warning("Warning: MMC controller hung for %d ms\n", host->max_delay);
  36601. + }
  36602. +
  36603. + clk |= SDHCI_CLOCK_CARD_EN;
  36604. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  36605. +}
  36606. +
  36607. +static void bcm2835_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  36608. +{
  36609. + struct bcm2835_host *host;
  36610. + unsigned long flags;
  36611. +
  36612. + host = mmc_priv(mmc);
  36613. +
  36614. + spin_lock_irqsave(&host->lock, flags);
  36615. +
  36616. + WARN_ON(host->mrq != NULL);
  36617. +
  36618. + host->mrq = mrq;
  36619. + bcm2835_mmc_send_command(host, mrq->cmd);
  36620. + mmiowb();
  36621. + spin_unlock_irqrestore(&host->lock, flags);
  36622. +
  36623. + if (mrq->cmd->data && host->use_dma) {
  36624. + /* DMA transfer starts now, PIO starts after interrupt */
  36625. + bcm2835_mmc_transfer_dma(host);
  36626. + }
  36627. +}
  36628. +
  36629. +
  36630. +static void bcm2835_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  36631. +{
  36632. +
  36633. + struct bcm2835_host *host = mmc_priv(mmc);
  36634. + unsigned long flags;
  36635. + u8 ctrl;
  36636. + u16 clk, ctrl_2;
  36637. +
  36638. +
  36639. + spin_lock_irqsave(&host->lock, flags);
  36640. +
  36641. + if (!ios->clock || ios->clock != host->clock) {
  36642. + bcm2835_mmc_set_clock(host, ios->clock);
  36643. + host->clock = ios->clock;
  36644. + }
  36645. +
  36646. + if (host->pwr != SDHCI_POWER_330) {
  36647. + host->pwr = SDHCI_POWER_330;
  36648. + bcm2835_mmc_writeb(host, SDHCI_POWER_330 | SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  36649. + }
  36650. +
  36651. + ctrl = bcm2835_mmc_readb(host, SDHCI_HOST_CONTROL);
  36652. +
  36653. + /* set bus width */
  36654. + ctrl &= ~SDHCI_CTRL_8BITBUS;
  36655. + if (ios->bus_width == MMC_BUS_WIDTH_4)
  36656. + ctrl |= SDHCI_CTRL_4BITBUS;
  36657. + else
  36658. + ctrl &= ~SDHCI_CTRL_4BITBUS;
  36659. +
  36660. + ctrl &= ~SDHCI_CTRL_HISPD; /* NO_HISPD_BIT */
  36661. +
  36662. +
  36663. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  36664. + /*
  36665. + * We only need to set Driver Strength if the
  36666. + * preset value enable is not set.
  36667. + */
  36668. + ctrl_2 = bcm2835_mmc_readw(host, SDHCI_HOST_CONTROL2);
  36669. + ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  36670. + if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  36671. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  36672. + else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  36673. + ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  36674. +
  36675. + bcm2835_mmc_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  36676. +
  36677. + /* Reset SD Clock Enable */
  36678. + clk = bcm2835_mmc_readw(host, SDHCI_CLOCK_CONTROL);
  36679. + clk &= ~SDHCI_CLOCK_CARD_EN;
  36680. + bcm2835_mmc_writew(host, clk, SDHCI_CLOCK_CONTROL);
  36681. +
  36682. + /* Re-enable SD Clock */
  36683. + bcm2835_mmc_set_clock(host, host->clock);
  36684. + bcm2835_mmc_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  36685. +
  36686. + mmiowb();
  36687. +
  36688. + spin_unlock_irqrestore(&host->lock, flags);
  36689. +}
  36690. +
  36691. +
  36692. +static struct mmc_host_ops bcm2835_ops = {
  36693. + .request = bcm2835_mmc_request,
  36694. + .set_ios = bcm2835_mmc_set_ios,
  36695. + .enable_sdio_irq = bcm2835_mmc_enable_sdio_irq,
  36696. +};
  36697. +
  36698. +
  36699. +static void bcm2835_mmc_tasklet_finish(unsigned long param)
  36700. +{
  36701. + struct bcm2835_host *host;
  36702. + unsigned long flags;
  36703. + struct mmc_request *mrq;
  36704. +
  36705. + host = (struct bcm2835_host *)param;
  36706. +
  36707. + spin_lock_irqsave(&host->lock, flags);
  36708. +
  36709. + /*
  36710. + * If this tasklet gets rescheduled while running, it will
  36711. + * be run again afterwards but without any active request.
  36712. + */
  36713. + if (!host->mrq) {
  36714. + spin_unlock_irqrestore(&host->lock, flags);
  36715. + return;
  36716. + }
  36717. +
  36718. + del_timer(&host->timer);
  36719. +
  36720. + mrq = host->mrq;
  36721. +
  36722. + /*
  36723. + * The controller needs a reset of internal state machines
  36724. + * upon error conditions.
  36725. + */
  36726. + if (!(host->flags & SDHCI_DEVICE_DEAD) &&
  36727. + ((mrq->cmd && mrq->cmd->error) ||
  36728. + (mrq->data && (mrq->data->error ||
  36729. + (mrq->data->stop && mrq->data->stop->error))))) {
  36730. +
  36731. + bcm2835_mmc_reset(host, SDHCI_RESET_CMD);
  36732. + bcm2835_mmc_reset(host, SDHCI_RESET_DATA);
  36733. + }
  36734. +
  36735. + host->mrq = NULL;
  36736. + host->cmd = NULL;
  36737. + host->data = NULL;
  36738. +
  36739. + mmiowb();
  36740. +
  36741. + spin_unlock_irqrestore(&host->lock, flags);
  36742. + mmc_request_done(host->mmc, mrq);
  36743. +}
  36744. +
  36745. +
  36746. +
  36747. +int bcm2835_mmc_add_host(struct bcm2835_host *host)
  36748. +{
  36749. + struct mmc_host *mmc;
  36750. +#ifndef FORCE_PIO
  36751. + struct dma_slave_config cfg;
  36752. +#endif
  36753. + int ret;
  36754. +
  36755. + mmc = host->mmc;
  36756. +
  36757. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  36758. +
  36759. + host->clk_mul = 0;
  36760. +
  36761. + mmc->ops = &bcm2835_ops;
  36762. + mmc->f_max = host->max_clk;
  36763. + mmc->f_max = host->max_clk;
  36764. + mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  36765. +
  36766. + /* SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK */
  36767. + host->timeout_clk = mmc->f_max / 1000;
  36768. +#ifdef CONFIG_OF
  36769. + mmc->max_busy_timeout = (1 << 27) / host->timeout_clk;
  36770. +#endif
  36771. + /* host controller capabilities */
  36772. + mmc->caps = MMC_CAP_CMD23 | MMC_CAP_ERASE | MMC_CAP_NEEDS_POLL | MMC_CAP_SDIO_IRQ |
  36773. + MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED | MMC_CAP_4_BIT_DATA;
  36774. +
  36775. + host->flags = SDHCI_AUTO_CMD23;
  36776. +
  36777. + spin_lock_init(&host->lock);
  36778. +
  36779. +
  36780. +#ifdef FORCE_PIO
  36781. + pr_info("Forcing PIO mode\n");
  36782. + host->have_dma = false;
  36783. +#else
  36784. + if (!host->dma_chan_tx || !host->dma_chan_rx ||
  36785. + IS_ERR(host->dma_chan_tx) || IS_ERR(host->dma_chan_rx)) {
  36786. + pr_err("%s: Unable to initialise DMA channels. Falling back to PIO\n", DRIVER_NAME);
  36787. + host->have_dma = false;
  36788. + } else {
  36789. + pr_info("DMA channels allocated for the MMC driver");
  36790. + host->have_dma = true;
  36791. +
  36792. + cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  36793. + cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  36794. + cfg.slave_id = 11; /* DREQ channel */
  36795. +
  36796. + cfg.direction = DMA_MEM_TO_DEV;
  36797. + cfg.src_addr = 0;
  36798. + cfg.dst_addr = host->phys_addr + SDHCI_BUFFER;
  36799. + ret = dmaengine_slave_config(host->dma_chan_tx, &cfg);
  36800. +
  36801. + cfg.direction = DMA_DEV_TO_MEM;
  36802. + cfg.src_addr = host->phys_addr + SDHCI_BUFFER;
  36803. + cfg.dst_addr = 0;
  36804. + ret = dmaengine_slave_config(host->dma_chan_rx, &cfg);
  36805. + }
  36806. +#endif
  36807. +
  36808. +
  36809. + mmc->max_segs = 128;
  36810. + mmc->max_req_size = 524288;
  36811. + mmc->max_seg_size = mmc->max_req_size;
  36812. + mmc->max_blk_size = 512;
  36813. + mmc->max_blk_count = 65535;
  36814. +
  36815. + /* report supported voltage ranges */
  36816. + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  36817. +
  36818. + tasklet_init(&host->finish_tasklet,
  36819. + bcm2835_mmc_tasklet_finish, (unsigned long)host);
  36820. +
  36821. + setup_timer(&host->timer, bcm2835_mmc_timeout_timer, (unsigned long)host);
  36822. + init_waitqueue_head(&host->buf_ready_int);
  36823. +
  36824. + bcm2835_mmc_init(host, 0);
  36825. +#ifndef CONFIG_OF
  36826. + ret = request_irq(host->irq, bcm2835_mmc_irq, 0 /*IRQF_SHARED*/,
  36827. + mmc_hostname(mmc), host);
  36828. +#else
  36829. + ret = request_threaded_irq(host->irq, bcm2835_mmc_irq, bcm2835_mmc_thread_irq,
  36830. + IRQF_SHARED, mmc_hostname(mmc), host);
  36831. +#endif
  36832. + if (ret) {
  36833. + pr_err("%s: Failed to request IRQ %d: %d\n",
  36834. + mmc_hostname(mmc), host->irq, ret);
  36835. + goto untasklet;
  36836. + }
  36837. +
  36838. + mmiowb();
  36839. + mmc_add_host(mmc);
  36840. +
  36841. + pr_info("Load BCM2835 MMC driver\n");
  36842. +
  36843. + return 0;
  36844. +
  36845. +untasklet:
  36846. + tasklet_kill(&host->finish_tasklet);
  36847. +
  36848. + return ret;
  36849. +}
  36850. +
  36851. +static int bcm2835_mmc_probe(struct platform_device *pdev)
  36852. +{
  36853. + struct device *dev = &pdev->dev;
  36854. +#ifdef CONFIG_OF
  36855. + struct device_node *node = dev->of_node;
  36856. + struct clk *clk;
  36857. +#endif
  36858. + struct resource *iomem;
  36859. + struct bcm2835_host *host = NULL;
  36860. +
  36861. + int ret;
  36862. + struct mmc_host *mmc;
  36863. +#if !defined(CONFIG_OF) && !defined(FORCE_PIO)
  36864. + dma_cap_mask_t mask;
  36865. +#endif
  36866. +
  36867. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  36868. + if (!iomem) {
  36869. + ret = -ENOMEM;
  36870. + goto err;
  36871. + }
  36872. +
  36873. + if (resource_size(iomem) < 0x100)
  36874. + dev_err(&pdev->dev, "Invalid iomem size!\n");
  36875. +
  36876. + mmc = mmc_alloc_host(sizeof(struct bcm2835_host), dev);
  36877. + host = mmc_priv(mmc);
  36878. + host->mmc = mmc;
  36879. +
  36880. +
  36881. + if (IS_ERR(host)) {
  36882. + ret = PTR_ERR(host);
  36883. + goto err;
  36884. + }
  36885. +
  36886. + host->phys_addr = iomem->start + BCM2835_VCMMU_SHIFT;
  36887. +
  36888. +#ifndef CONFIG_OF
  36889. +#ifndef FORCE_PIO
  36890. + dma_cap_zero(mask);
  36891. + /* we don't care about the channel, any would work */
  36892. + dma_cap_set(DMA_SLAVE, mask);
  36893. +
  36894. + host->dma_chan_tx = dma_request_channel(mask, NULL, NULL);
  36895. + host->dma_chan_rx = dma_request_channel(mask, NULL, NULL);
  36896. +#endif
  36897. + host->max_clk = BCM2835_CLOCK_FREQ;
  36898. +
  36899. +#else
  36900. +#ifndef FORCE_PIO
  36901. + host->dma_chan_tx = of_dma_request_slave_channel(node, "tx");
  36902. + host->dma_chan_rx = of_dma_request_slave_channel(node, "rx");
  36903. +#endif
  36904. + clk = of_clk_get(node, 0);
  36905. + if (IS_ERR(clk)) {
  36906. + dev_err(dev, "get CLOCK failed\n");
  36907. + ret = PTR_ERR(clk);
  36908. + goto out;
  36909. + }
  36910. + host->max_clk = (clk_get_rate(clk));
  36911. +#endif
  36912. + host->irq = platform_get_irq(pdev, 0);
  36913. +
  36914. + if (!request_mem_region(iomem->start, resource_size(iomem),
  36915. + mmc_hostname(host->mmc))) {
  36916. + dev_err(&pdev->dev, "cannot request region\n");
  36917. + ret = -EBUSY;
  36918. + goto err_request;
  36919. + }
  36920. +
  36921. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  36922. + if (!host->ioaddr) {
  36923. + dev_err(&pdev->dev, "failed to remap registers\n");
  36924. + ret = -ENOMEM;
  36925. + goto err_remap;
  36926. + }
  36927. +
  36928. + platform_set_drvdata(pdev, host);
  36929. +
  36930. +
  36931. + if (host->irq <= 0) {
  36932. + dev_err(dev, "get IRQ failed\n");
  36933. + ret = -EINVAL;
  36934. + goto out;
  36935. + }
  36936. +
  36937. +
  36938. +#ifndef CONFIG_OF
  36939. + mmc->caps |= MMC_CAP_4_BIT_DATA;
  36940. +#else
  36941. + mmc_of_parse(mmc);
  36942. +#endif
  36943. + host->timeout = msecs_to_jiffies(1000);
  36944. + spin_lock_init(&host->lock);
  36945. + mmc->ops = &bcm2835_ops;
  36946. + return bcm2835_mmc_add_host(host);
  36947. +
  36948. +
  36949. +err_remap:
  36950. + release_mem_region(iomem->start, resource_size(iomem));
  36951. +err_request:
  36952. + mmc_free_host(host->mmc);
  36953. +err:
  36954. + dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
  36955. + return ret;
  36956. +out:
  36957. + if (mmc)
  36958. + mmc_free_host(mmc);
  36959. + return ret;
  36960. +}
  36961. +
  36962. +static int bcm2835_mmc_remove(struct platform_device *pdev)
  36963. +{
  36964. + struct bcm2835_host *host = platform_get_drvdata(pdev);
  36965. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  36966. + unsigned long flags;
  36967. + int dead;
  36968. + u32 scratch;
  36969. +
  36970. + dead = 0;
  36971. + scratch = bcm2835_mmc_readl(host, SDHCI_INT_STATUS);
  36972. + if (scratch == (u32)-1)
  36973. + dead = 1;
  36974. +
  36975. +
  36976. + if (dead) {
  36977. + spin_lock_irqsave(&host->lock, flags);
  36978. +
  36979. + host->flags |= SDHCI_DEVICE_DEAD;
  36980. +
  36981. + if (host->mrq) {
  36982. + pr_err("%s: Controller removed during "
  36983. + " transfer!\n", mmc_hostname(host->mmc));
  36984. +
  36985. + host->mrq->cmd->error = -ENOMEDIUM;
  36986. + tasklet_schedule(&host->finish_tasklet);
  36987. + }
  36988. +
  36989. + spin_unlock_irqrestore(&host->lock, flags);
  36990. + }
  36991. +
  36992. + mmc_remove_host(host->mmc);
  36993. +
  36994. + if (!dead)
  36995. + bcm2835_mmc_reset(host, SDHCI_RESET_ALL);
  36996. +
  36997. + free_irq(host->irq, host);
  36998. +
  36999. + del_timer_sync(&host->timer);
  37000. +
  37001. + tasklet_kill(&host->finish_tasklet);
  37002. +
  37003. + iounmap(host->ioaddr);
  37004. + release_mem_region(iomem->start, resource_size(iomem));
  37005. + mmc_free_host(host->mmc);
  37006. + platform_set_drvdata(pdev, NULL);
  37007. +
  37008. + return 0;
  37009. +}
  37010. +
  37011. +
  37012. +static const struct of_device_id bcm2835_mmc_match[] = {
  37013. + { .compatible = "brcm,bcm2835-mmc" },
  37014. + { }
  37015. +};
  37016. +MODULE_DEVICE_TABLE(of, bcm2835_mmc_match);
  37017. +
  37018. +
  37019. +
  37020. +static struct platform_driver bcm2835_mmc_driver = {
  37021. + .probe = bcm2835_mmc_probe,
  37022. + .remove = bcm2835_mmc_remove,
  37023. + .driver = {
  37024. + .name = DRIVER_NAME,
  37025. + .owner = THIS_MODULE,
  37026. + .of_match_table = bcm2835_mmc_match,
  37027. + },
  37028. +};
  37029. +module_platform_driver(bcm2835_mmc_driver);
  37030. +
  37031. +MODULE_ALIAS("platform:mmc-bcm2835");
  37032. +MODULE_DESCRIPTION("BCM2835 SDHCI driver");
  37033. +MODULE_LICENSE("GPL v2");
  37034. +MODULE_AUTHOR("Gellert Weisz");
  37035. diff -Nur linux-3.16.2/drivers/mmc/host/Kconfig linux-3.16-rpi/drivers/mmc/host/Kconfig
  37036. --- linux-3.16.2/drivers/mmc/host/Kconfig 2014-09-06 01:37:11.000000000 +0200
  37037. +++ linux-3.16-rpi/drivers/mmc/host/Kconfig 2014-09-14 19:03:25.000000000 +0200
  37038. @@ -270,6 +270,27 @@
  37039. If you have a controller with this interface, say Y or M here.
  37040. +config MMC_SDHCI_BCM2708
  37041. + tristate "SDHCI support on BCM2708"
  37042. + depends on MMC_SDHCI && MACH_BCM2708
  37043. + select MMC_SDHCI_IO_ACCESSORS
  37044. + help
  37045. + This selects the Secure Digital Host Controller Interface (SDHCI)
  37046. + often referrered to as the eMMC block.
  37047. +
  37048. + If you have a controller with this interface, say Y or M here.
  37049. +
  37050. + If unsure, say N.
  37051. +
  37052. +config MMC_SDHCI_BCM2708_DMA
  37053. + bool "DMA support on BCM2708 Arasan controller"
  37054. + depends on MMC_SDHCI_BCM2708
  37055. + help
  37056. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  37057. + based chips.
  37058. +
  37059. + If unsure, say N.
  37060. +
  37061. config MMC_SDHCI_BCM2835
  37062. tristate "SDHCI platform support for the BCM2835 SD/MMC Controller"
  37063. depends on ARCH_BCM2835
  37064. @@ -290,6 +311,35 @@
  37065. be found on some embedded hardware such as UC-7112-LX.
  37066. If you have a controller with this interface, say Y here.
  37067. +config MMC_BCM2835
  37068. + tristate "MMC support on BCM2835"
  37069. + depends on MACH_BCM2708
  37070. + help
  37071. + This selects the MMC Interface on BCM2835.
  37072. +
  37073. + If you have a controller with this interface, say Y or M here.
  37074. +
  37075. + If unsure, say N.
  37076. +
  37077. +config MMC_BCM2835_DMA
  37078. + bool "DMA support on BCM2835 Arasan controller"
  37079. + depends on MMC_BCM2835
  37080. + help
  37081. + Enable DMA support on the Arasan SDHCI controller in Broadcom 2708
  37082. + based chips.
  37083. +
  37084. + If unsure, say N.
  37085. +
  37086. +config MMC_BCM2835_PIO_DMA_BARRIER
  37087. + int "Block count limit for PIO transfers"
  37088. + depends on MMC_BCM2835 && MMC_BCM2835_DMA
  37089. + range 0 256
  37090. + default 2
  37091. + help
  37092. + The inclusive limit in bytes under which PIO will be used instead of DMA
  37093. +
  37094. + If unsure, say 2 here.
  37095. +
  37096. config MMC_OMAP
  37097. tristate "TI OMAP Multimedia Card Interface support"
  37098. depends on ARCH_OMAP
  37099. diff -Nur linux-3.16.2/drivers/mmc/host/Makefile linux-3.16-rpi/drivers/mmc/host/Makefile
  37100. --- linux-3.16.2/drivers/mmc/host/Makefile 2014-09-06 01:37:11.000000000 +0200
  37101. +++ linux-3.16-rpi/drivers/mmc/host/Makefile 2014-09-14 19:03:25.000000000 +0200
  37102. @@ -16,6 +16,8 @@
  37103. obj-$(CONFIG_MMC_SDHCI_S3C) += sdhci-s3c.o
  37104. obj-$(CONFIG_MMC_SDHCI_SIRF) += sdhci-sirf.o
  37105. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  37106. +obj-$(CONFIG_MMC_SDHCI_BCM2708) += sdhci-bcm2708.o
  37107. +obj-$(CONFIG_MMC_BCM2835) += bcm2835-mmc.o
  37108. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  37109. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  37110. obj-$(CONFIG_MMC_OMAP) += omap.o
  37111. diff -Nur linux-3.16.2/drivers/mmc/host/sdhci-bcm2708.c linux-3.16-rpi/drivers/mmc/host/sdhci-bcm2708.c
  37112. --- linux-3.16.2/drivers/mmc/host/sdhci-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  37113. +++ linux-3.16-rpi/drivers/mmc/host/sdhci-bcm2708.c 2014-09-14 19:03:25.000000000 +0200
  37114. @@ -0,0 +1,1417 @@
  37115. +/*
  37116. + * sdhci-bcm2708.c Support for SDHCI device on BCM2708
  37117. + * Copyright (c) 2010 Broadcom
  37118. + *
  37119. + * This program is free software; you can redistribute it and/or modify
  37120. + * it under the terms of the GNU General Public License version 2 as
  37121. + * published by the Free Software Foundation.
  37122. + *
  37123. + * This program is distributed in the hope that it will be useful,
  37124. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37125. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37126. + * GNU General Public License for more details.
  37127. + *
  37128. + * You should have received a copy of the GNU General Public License
  37129. + * along with this program; if not, write to the Free Software
  37130. + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  37131. + */
  37132. +
  37133. +/* Supports:
  37134. + * SDHCI platform device - Arasan SD controller in BCM2708
  37135. + *
  37136. + * Inspired by sdhci-pci.c, by Pierre Ossman
  37137. + */
  37138. +
  37139. +#include <linux/delay.h>
  37140. +#include <linux/highmem.h>
  37141. +#include <linux/platform_device.h>
  37142. +#include <linux/module.h>
  37143. +#include <linux/mmc/mmc.h>
  37144. +#include <linux/mmc/host.h>
  37145. +#include <linux/mmc/sd.h>
  37146. +
  37147. +#include <linux/io.h>
  37148. +#include <linux/dma-mapping.h>
  37149. +#include <mach/dma.h>
  37150. +
  37151. +#include "sdhci.h"
  37152. +
  37153. +/*****************************************************************************\
  37154. + * *
  37155. + * Configuration *
  37156. + * *
  37157. +\*****************************************************************************/
  37158. +
  37159. +#define DRIVER_NAME "bcm2708_sdhci"
  37160. +
  37161. +/* for the time being insist on DMA mode - PIO seems not to work */
  37162. +#ifndef CONFIG_MMC_SDHCI_BCM2708_DMA
  37163. +#warning Non-DMA (PIO) version of this driver currently unavailable
  37164. +#endif
  37165. +#undef CONFIG_MMC_SDHCI_BCM2708_DMA
  37166. +#define CONFIG_MMC_SDHCI_BCM2708_DMA y
  37167. +
  37168. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  37169. +/* #define CHECK_DMA_USE */
  37170. +#endif
  37171. +//#define LOG_REGISTERS
  37172. +
  37173. +#define USE_SCHED_TIME
  37174. +#define USE_SPACED_WRITES_2CLK 1 /* space consecutive register writes */
  37175. +#define USE_SOFTWARE_TIMEOUTS 1 /* not hardware timeouts */
  37176. +#define SOFTWARE_ERASE_TIMEOUT_SEC 30
  37177. +
  37178. +#define SDHCI_BCM_DMA_CHAN 4 /* this default is normally overriden */
  37179. +#define SDHCI_BCM_DMA_WAITS 0 /* delays slowing DMA transfers: 0-31 */
  37180. +/* We are worried that SD card DMA use may be blocking the AXI bus for others */
  37181. +
  37182. +/*! TODO: obtain these from the physical address */
  37183. +#define DMA_SDHCI_BASE 0x7e300000 /* EMMC register block on Videocore */
  37184. +#define DMA_SDHCI_BUFFER (DMA_SDHCI_BASE + SDHCI_BUFFER)
  37185. +
  37186. +#define MAX_LITE_TRANSFER 32768
  37187. +#define MAX_NORMAL_TRANSFER 1073741824
  37188. +
  37189. +#define BCM2708_SDHCI_SLEEP_TIMEOUT 1000 /* msecs */
  37190. +
  37191. +/* Mhz clock that the EMMC core is running at. Should match the platform clockman settings */
  37192. +#define BCM2708_EMMC_CLOCK_FREQ 50000000
  37193. +
  37194. +#define REG_EXRDFIFO_EN 0x80
  37195. +#define REG_EXRDFIFO_CFG 0x84
  37196. +
  37197. +int cycle_delay=2;
  37198. +
  37199. +/*****************************************************************************\
  37200. + * *
  37201. + * Debug *
  37202. + * *
  37203. +\*****************************************************************************/
  37204. +
  37205. +
  37206. +
  37207. +#define DBG(f, x...) \
  37208. + pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
  37209. +// printk(KERN_INFO DRIVER_NAME " [%s()]: " f, __func__,## x)//GRAYG
  37210. +
  37211. +
  37212. +/*****************************************************************************\
  37213. + * *
  37214. + * High Precision Time *
  37215. + * *
  37216. +\*****************************************************************************/
  37217. +
  37218. +#ifdef USE_SCHED_TIME
  37219. +
  37220. +#include <mach/frc.h>
  37221. +
  37222. +typedef unsigned long hptime_t;
  37223. +
  37224. +#define FMT_HPT "lu"
  37225. +
  37226. +static inline hptime_t hptime(void)
  37227. +{
  37228. + return frc_clock_ticks32();
  37229. +}
  37230. +
  37231. +#define HPTIME_CLK_NS 1000ul
  37232. +
  37233. +#else
  37234. +
  37235. +typedef unsigned long hptime_t;
  37236. +
  37237. +#define FMT_HPT "lu"
  37238. +
  37239. +static inline hptime_t hptime(void)
  37240. +{
  37241. + return jiffies;
  37242. +}
  37243. +
  37244. +#define HPTIME_CLK_NS (1000000000ul/HZ)
  37245. +
  37246. +#endif
  37247. +
  37248. +static inline unsigned long int since_ns(hptime_t t)
  37249. +{
  37250. + return (unsigned long)((hptime() - t) * HPTIME_CLK_NS);
  37251. +}
  37252. +
  37253. +static bool allow_highspeed = 1;
  37254. +static int emmc_clock_freq = BCM2708_EMMC_CLOCK_FREQ;
  37255. +static bool sync_after_dma = 1;
  37256. +static bool missing_status = 1;
  37257. +bool extra_messages = 0;
  37258. +
  37259. +#if 0
  37260. +static void hptime_test(void)
  37261. +{
  37262. + hptime_t now;
  37263. + hptime_t later;
  37264. +
  37265. + now = hptime();
  37266. + msleep(10);
  37267. + later = hptime();
  37268. +
  37269. + printk(KERN_INFO DRIVER_NAME": 10ms = %"FMT_HPT" clks "
  37270. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  37271. + later-now, now, later,
  37272. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  37273. +
  37274. + now = hptime();
  37275. + msleep(1000);
  37276. + later = hptime();
  37277. +
  37278. + printk(KERN_INFO DRIVER_NAME": 1s = %"FMT_HPT" clks "
  37279. + "(from %"FMT_HPT" to %"FMT_HPT") = %luns\n",
  37280. + later-now, now, later,
  37281. + (unsigned long)(HPTIME_CLK_NS * (later - now)));
  37282. +}
  37283. +#endif
  37284. +
  37285. +/*****************************************************************************\
  37286. + * *
  37287. + * SDHCI core callbacks *
  37288. + * *
  37289. +\*****************************************************************************/
  37290. +
  37291. +
  37292. +#ifdef CHECK_DMA_USE
  37293. +/*#define CHECK_DMA_REG_USE*/
  37294. +#endif
  37295. +
  37296. +#ifdef CHECK_DMA_REG_USE
  37297. +/* we don't expect anything to be using these registers during a
  37298. + DMA (except the IRQ status) - so check */
  37299. +static void check_dma_reg_use(struct sdhci_host *host, int reg);
  37300. +#else
  37301. +#define check_dma_reg_use(host, reg)
  37302. +#endif
  37303. +
  37304. +
  37305. +static inline u32 sdhci_bcm2708_raw_readl(struct sdhci_host *host, int reg)
  37306. +{
  37307. + return readl(host->ioaddr + reg);
  37308. +}
  37309. +
  37310. +u32 sdhci_bcm2708_readl(struct sdhci_host *host, int reg)
  37311. +{
  37312. + u32 l = sdhci_bcm2708_raw_readl(host, reg);
  37313. +
  37314. +#ifdef LOG_REGISTERS
  37315. + printk(KERN_ERR "%s: readl from 0x%02x, value 0x%08x\n",
  37316. + mmc_hostname(host->mmc), reg, l);
  37317. +#endif
  37318. + check_dma_reg_use(host, reg);
  37319. +
  37320. + return l;
  37321. +}
  37322. +
  37323. +u16 sdhci_bcm2708_readw(struct sdhci_host *host, int reg)
  37324. +{
  37325. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  37326. + u32 w = l >> (reg << 3 & 0x18) & 0xffff;
  37327. +
  37328. +#ifdef LOG_REGISTERS
  37329. + printk(KERN_ERR "%s: readw from 0x%02x, value 0x%04x\n",
  37330. + mmc_hostname(host->mmc), reg, w);
  37331. +#endif
  37332. + check_dma_reg_use(host, reg);
  37333. +
  37334. + return (u16)w;
  37335. +}
  37336. +
  37337. +u8 sdhci_bcm2708_readb(struct sdhci_host *host, int reg)
  37338. +{
  37339. + u32 l = sdhci_bcm2708_raw_readl(host, reg & ~3);
  37340. + u32 b = l >> (reg << 3 & 0x18) & 0xff;
  37341. +
  37342. +#ifdef LOG_REGISTERS
  37343. + printk(KERN_ERR "%s: readb from 0x%02x, value 0x%02x\n",
  37344. + mmc_hostname(host->mmc), reg, b);
  37345. +#endif
  37346. + check_dma_reg_use(host, reg);
  37347. +
  37348. + return (u8)b;
  37349. +}
  37350. +
  37351. +
  37352. +static void sdhci_bcm2708_raw_writel(struct sdhci_host *host, u32 val, int reg)
  37353. +{
  37354. + u32 ier;
  37355. +
  37356. +#if USE_SPACED_WRITES_2CLK
  37357. + static bool timeout_disabled = false;
  37358. + unsigned int ns_2clk = 0;
  37359. +
  37360. + /* The Arasan has a bugette whereby it may lose the content of
  37361. + * successive writes to registers that are within two SD-card clock
  37362. + * cycles of each other (a clock domain crossing problem).
  37363. + * It seems, however, that the data register does not have this problem.
  37364. + * (Which is just as well - otherwise we'd have to nobble the DMA engine
  37365. + * too)
  37366. + */
  37367. + if (reg != SDHCI_BUFFER && host->clock != 0) {
  37368. + /* host->clock is the clock freq in Hz */
  37369. + static hptime_t last_write_hpt;
  37370. + hptime_t now = hptime();
  37371. + ns_2clk = cycle_delay*1000000/(host->clock/1000);
  37372. +
  37373. + if (now == last_write_hpt || now == last_write_hpt+1) {
  37374. + /* we can't guarantee any significant time has
  37375. + * passed - we'll have to wait anyway ! */
  37376. + ndelay(ns_2clk);
  37377. + } else
  37378. + {
  37379. + /* we must have waited at least this many ns: */
  37380. + unsigned int ns_wait = HPTIME_CLK_NS *
  37381. + (now - last_write_hpt - 1);
  37382. + if (ns_wait < ns_2clk)
  37383. + ndelay(ns_2clk - ns_wait);
  37384. + }
  37385. + last_write_hpt = now;
  37386. + }
  37387. +#if USE_SOFTWARE_TIMEOUTS
  37388. + /* The Arasan is clocked for timeouts using the SD clock which is too
  37389. + * fast for ERASE commands and causes issues. So we disable timeouts
  37390. + * for ERASE */
  37391. + if (host->cmd != NULL && host->cmd->opcode == MMC_ERASE &&
  37392. + reg == (SDHCI_COMMAND & ~3)) {
  37393. + mod_timer(&host->timer,
  37394. + jiffies + SOFTWARE_ERASE_TIMEOUT_SEC * HZ);
  37395. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  37396. + ier &= ~SDHCI_INT_DATA_TIMEOUT;
  37397. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  37398. + timeout_disabled = true;
  37399. + ndelay(ns_2clk);
  37400. + } else if (timeout_disabled) {
  37401. + ier = readl(host->ioaddr + SDHCI_SIGNAL_ENABLE);
  37402. + ier |= SDHCI_INT_DATA_TIMEOUT;
  37403. + writel(ier, host->ioaddr + SDHCI_SIGNAL_ENABLE);
  37404. + timeout_disabled = false;
  37405. + ndelay(ns_2clk);
  37406. + }
  37407. +#endif
  37408. + writel(val, host->ioaddr + reg);
  37409. +#else
  37410. + void __iomem * regaddr = host->ioaddr + reg;
  37411. +
  37412. + writel(val, regaddr);
  37413. +
  37414. + if (reg != SDHCI_BUFFER && reg != SDHCI_INT_STATUS && host->clock != 0)
  37415. + {
  37416. + int timeout = 100000;
  37417. + while (val != readl(regaddr) && --timeout > 0)
  37418. + continue;
  37419. +
  37420. + if (timeout <= 0)
  37421. + printk(KERN_ERR "%s: writing 0x%X to reg 0x%X "
  37422. + "always gives 0x%X\n",
  37423. + mmc_hostname(host->mmc),
  37424. + val, reg, readl(regaddr));
  37425. + BUG_ON(timeout <= 0);
  37426. + }
  37427. +#endif
  37428. +}
  37429. +
  37430. +
  37431. +void sdhci_bcm2708_writel(struct sdhci_host *host, u32 val, int reg)
  37432. +{
  37433. +#ifdef LOG_REGISTERS
  37434. + printk(KERN_ERR "%s: writel to 0x%02x, value 0x%08x\n",
  37435. + mmc_hostname(host->mmc), reg, val);
  37436. +#endif
  37437. + check_dma_reg_use(host, reg);
  37438. +
  37439. + sdhci_bcm2708_raw_writel(host, val, reg);
  37440. +}
  37441. +
  37442. +void sdhci_bcm2708_writew(struct sdhci_host *host, u16 val, int reg)
  37443. +{
  37444. + static u32 shadow = 0;
  37445. +
  37446. + u32 p = reg == SDHCI_COMMAND ? shadow :
  37447. + sdhci_bcm2708_raw_readl(host, reg & ~3);
  37448. + u32 s = reg << 3 & 0x18;
  37449. + u32 l = val << s;
  37450. + u32 m = 0xffff << s;
  37451. +
  37452. +#ifdef LOG_REGISTERS
  37453. + printk(KERN_ERR "%s: writew to 0x%02x, value 0x%04x\n",
  37454. + mmc_hostname(host->mmc), reg, val);
  37455. +#endif
  37456. +
  37457. + if (reg == SDHCI_TRANSFER_MODE)
  37458. + shadow = (p & ~m) | l;
  37459. + else {
  37460. + check_dma_reg_use(host, reg);
  37461. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  37462. + }
  37463. +}
  37464. +
  37465. +void sdhci_bcm2708_writeb(struct sdhci_host *host, u8 val, int reg)
  37466. +{
  37467. + u32 p = sdhci_bcm2708_raw_readl(host, reg & ~3);
  37468. + u32 s = reg << 3 & 0x18;
  37469. + u32 l = val << s;
  37470. + u32 m = 0xff << s;
  37471. +
  37472. +#ifdef LOG_REGISTERS
  37473. + printk(KERN_ERR "%s: writeb to 0x%02x, value 0x%02x\n",
  37474. + mmc_hostname(host->mmc), reg, val);
  37475. +#endif
  37476. +
  37477. + check_dma_reg_use(host, reg);
  37478. + sdhci_bcm2708_raw_writel(host, (p & ~m) | l, reg & ~3);
  37479. +}
  37480. +
  37481. +static unsigned int sdhci_bcm2708_get_max_clock(struct sdhci_host *host)
  37482. +{
  37483. + return emmc_clock_freq;
  37484. +}
  37485. +
  37486. +/*****************************************************************************\
  37487. + * *
  37488. + * DMA Operation *
  37489. + * *
  37490. +\*****************************************************************************/
  37491. +
  37492. +struct sdhci_bcm2708_priv {
  37493. + int dma_chan;
  37494. + int dma_irq;
  37495. + void __iomem *dma_chan_base;
  37496. + struct bcm2708_dma_cb *cb_base; /* DMA control blocks */
  37497. + dma_addr_t cb_handle;
  37498. + /* tracking scatter gather progress */
  37499. + unsigned sg_ix; /* scatter gather list index */
  37500. + unsigned sg_done; /* bytes in current sg_ix done */
  37501. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  37502. + unsigned char dma_wanted; /* DMA transfer requested */
  37503. + unsigned char dma_waits; /* wait states in DMAs */
  37504. +#ifdef CHECK_DMA_USE
  37505. + unsigned char dmas_pending; /* no of unfinished DMAs */
  37506. + hptime_t when_started;
  37507. + hptime_t when_reset;
  37508. + hptime_t when_stopped;
  37509. +#endif
  37510. +#endif
  37511. + /* signalling the end of a transfer */
  37512. + void (*complete)(struct sdhci_host *);
  37513. +};
  37514. +
  37515. +#define SDHCI_HOST_PRIV(host) \
  37516. + (struct sdhci_bcm2708_priv *)((struct sdhci_host *)(host)+1)
  37517. +
  37518. +
  37519. +
  37520. +#ifdef CHECK_DMA_REG_USE
  37521. +static void check_dma_reg_use(struct sdhci_host *host, int reg)
  37522. +{
  37523. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37524. + if (host_priv->dma_wanted && reg != SDHCI_INT_STATUS) {
  37525. + printk(KERN_INFO"%s: accessing register 0x%x during DMA\n",
  37526. + mmc_hostname(host->mmc), reg);
  37527. + }
  37528. +}
  37529. +#endif
  37530. +
  37531. +
  37532. +
  37533. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  37534. +
  37535. +static void sdhci_clear_set_irqgen(struct sdhci_host *host, u32 clear, u32 set)
  37536. +{
  37537. + u32 ier;
  37538. +
  37539. + ier = sdhci_bcm2708_raw_readl(host, SDHCI_SIGNAL_ENABLE);
  37540. + ier &= ~clear;
  37541. + ier |= set;
  37542. + /* change which requests generate IRQs - makes no difference to
  37543. + the content of SDHCI_INT_STATUS, or the need to acknowledge IRQs */
  37544. + sdhci_bcm2708_raw_writel(host, ier, SDHCI_SIGNAL_ENABLE);
  37545. +}
  37546. +
  37547. +static void sdhci_signal_irqs(struct sdhci_host *host, u32 irqs)
  37548. +{
  37549. + sdhci_clear_set_irqgen(host, 0, irqs);
  37550. +}
  37551. +
  37552. +static void sdhci_unsignal_irqs(struct sdhci_host *host, u32 irqs)
  37553. +{
  37554. + sdhci_clear_set_irqgen(host, irqs, 0);
  37555. +}
  37556. +
  37557. +
  37558. +
  37559. +static void schci_bcm2708_cb_read(struct sdhci_bcm2708_priv *host,
  37560. + int ix,
  37561. + dma_addr_t dma_addr, unsigned len,
  37562. + int /*bool*/ is_last)
  37563. +{
  37564. + struct bcm2708_dma_cb *cb;
  37565. + unsigned char dmawaits = host->dma_waits;
  37566. + unsigned i, max_size;
  37567. +
  37568. + if (host->dma_chan >= 8) /* we have a LITE channel */
  37569. + max_size = MAX_LITE_TRANSFER;
  37570. + else
  37571. + max_size = MAX_NORMAL_TRANSFER;
  37572. +
  37573. + for (i = 0; i < len; i += max_size) {
  37574. + cb = &host->cb_base[ix+i/max_size];
  37575. +
  37576. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  37577. + BCM2708_DMA_WAITS(dmawaits) |
  37578. + BCM2708_DMA_WAIT_RESP |
  37579. + BCM2708_DMA_S_DREQ |
  37580. + BCM2708_DMA_D_WIDTH |
  37581. + BCM2708_DMA_D_INC;
  37582. + cb->src = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  37583. + cb->dst = dma_addr + (dma_addr_t)i;
  37584. + cb->length = min(len-i, max_size);
  37585. + cb->stride = 0;
  37586. +
  37587. + if (is_last && len-i <= max_size) {
  37588. + cb->info |= BCM2708_DMA_INT_EN;
  37589. + cb->next = 0;
  37590. + } else
  37591. + cb->next = host->cb_handle +
  37592. + (ix+1 + i/max_size)*sizeof(struct bcm2708_dma_cb);
  37593. +
  37594. + cb->pad[0] = 0;
  37595. + cb->pad[1] = 0;
  37596. + }
  37597. +}
  37598. +
  37599. +static void schci_bcm2708_cb_write(struct sdhci_bcm2708_priv *host,
  37600. + int ix,
  37601. + dma_addr_t dma_addr, unsigned len,
  37602. + int /*bool*/ is_last)
  37603. +{
  37604. + struct bcm2708_dma_cb *cb = &host->cb_base[ix];
  37605. + unsigned char dmawaits = host->dma_waits;
  37606. + unsigned i, max_size;
  37607. +
  37608. + if (host->dma_chan >= 8) /* we have a LITE channel */
  37609. + max_size = MAX_LITE_TRANSFER;
  37610. + else
  37611. + max_size = MAX_NORMAL_TRANSFER;
  37612. +
  37613. + /* We can make arbitrarily large writes as long as we specify DREQ to
  37614. + pace the delivery of bytes to the Arasan hardware. However we need
  37615. + to take care when using LITE channels */
  37616. +
  37617. + for (i = 0; i < len; i += max_size) {
  37618. + cb = &host->cb_base[ix+i/max_size];
  37619. +
  37620. + cb->info = BCM2708_DMA_PER_MAP(BCM2708_DMA_DREQ_EMMC) |
  37621. + BCM2708_DMA_WAITS(dmawaits) |
  37622. + BCM2708_DMA_WAIT_RESP |
  37623. + BCM2708_DMA_D_DREQ |
  37624. + BCM2708_DMA_S_WIDTH |
  37625. + BCM2708_DMA_S_INC;
  37626. + cb->src = dma_addr + (dma_addr_t)i;
  37627. + cb->dst = DMA_SDHCI_BUFFER; /* DATA register DMA address */
  37628. + cb->length = min(len-i, max_size);
  37629. + cb->stride = 0;
  37630. +
  37631. + if (is_last && len-i <= max_size) {
  37632. + cb->info |= BCM2708_DMA_INT_EN;
  37633. + cb->next = 0;
  37634. + } else
  37635. + cb->next = host->cb_handle +
  37636. + (ix+1 + i/max_size)*sizeof(struct bcm2708_dma_cb);
  37637. +
  37638. + cb->pad[0] = 0;
  37639. + cb->pad[1] = 0;
  37640. + }
  37641. +}
  37642. +
  37643. +
  37644. +static void schci_bcm2708_dma_go(struct sdhci_host *host)
  37645. +{
  37646. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37647. + void __iomem *dma_chan_base = host_priv->dma_chan_base;
  37648. +
  37649. + BUG_ON(host_priv->dma_wanted);
  37650. +#ifdef CHECK_DMA_USE
  37651. + if (host_priv->dma_wanted)
  37652. + printk(KERN_ERR "%s: DMA already in progress - "
  37653. + "now %"FMT_HPT", last started %lu "
  37654. + "reset %lu stopped %lu\n",
  37655. + mmc_hostname(host->mmc),
  37656. + hptime(), since_ns(host_priv->when_started),
  37657. + since_ns(host_priv->when_reset),
  37658. + since_ns(host_priv->when_stopped));
  37659. + else if (host_priv->dmas_pending > 0)
  37660. + printk(KERN_INFO "%s: note - new DMA when %d reset DMAs "
  37661. + "already in progress - "
  37662. + "now %"FMT_HPT", started %lu reset %lu stopped %lu\n",
  37663. + mmc_hostname(host->mmc),
  37664. + host_priv->dmas_pending,
  37665. + hptime(), since_ns(host_priv->when_started),
  37666. + since_ns(host_priv->when_reset),
  37667. + since_ns(host_priv->when_stopped));
  37668. + host_priv->dmas_pending += 1;
  37669. + host_priv->when_started = hptime();
  37670. +#endif
  37671. + host_priv->dma_wanted = 1;
  37672. + DBG("PDMA go - base %p handle %08X\n", dma_chan_base,
  37673. + host_priv->cb_handle);
  37674. + bcm_dma_start(dma_chan_base, host_priv->cb_handle);
  37675. +}
  37676. +
  37677. +
  37678. +static void
  37679. +sdhci_platdma_read(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  37680. +{
  37681. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37682. +
  37683. + DBG("PDMA to read %d bytes\n", len);
  37684. + host_priv->sg_done += len;
  37685. + schci_bcm2708_cb_read(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  37686. + schci_bcm2708_dma_go(host);
  37687. +}
  37688. +
  37689. +
  37690. +static void
  37691. +sdhci_platdma_write(struct sdhci_host *host, dma_addr_t dma_addr, size_t len)
  37692. +{
  37693. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37694. +
  37695. + DBG("PDMA to write %d bytes\n", len);
  37696. + //BUG_ON(0 != (len & 0x1ff));
  37697. +
  37698. + host_priv->sg_done += len;
  37699. + schci_bcm2708_cb_write(host_priv, 0, dma_addr, len, 1/*TRUE*/);
  37700. + schci_bcm2708_dma_go(host);
  37701. +}
  37702. +
  37703. +/*! space is avaiable to receive into or data is available to write
  37704. + Platform DMA exported function
  37705. +*/
  37706. +void
  37707. +sdhci_bcm2708_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  37708. + void(*completion_callback)(struct sdhci_host *host))
  37709. +{
  37710. + struct mmc_data *data = host->data;
  37711. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37712. + int sg_ix;
  37713. + size_t bytes;
  37714. + dma_addr_t addr;
  37715. +
  37716. + BUG_ON(NULL == data);
  37717. + BUG_ON(0 == data->blksz);
  37718. +
  37719. + host_priv->complete = completion_callback;
  37720. +
  37721. + sg_ix = host_priv->sg_ix;
  37722. + BUG_ON(sg_ix >= data->sg_len);
  37723. +
  37724. + /* we can DMA blocks larger than blksz - it may hang the DMA
  37725. + channel but we are its only user */
  37726. + bytes = sg_dma_len(&data->sg[sg_ix]) - host_priv->sg_done;
  37727. + addr = sg_dma_address(&data->sg[sg_ix]) + host_priv->sg_done;
  37728. +
  37729. + if (bytes > 0) {
  37730. + /* We're going to poll for read/write available state until
  37731. + we finish this DMA
  37732. + */
  37733. +
  37734. + if (data->flags & MMC_DATA_READ) {
  37735. + if (*ref_intmask & SDHCI_INT_DATA_AVAIL) {
  37736. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  37737. + SDHCI_INT_SPACE_AVAIL);
  37738. + sdhci_platdma_read(host, addr, bytes);
  37739. + }
  37740. + } else {
  37741. + if (*ref_intmask & SDHCI_INT_SPACE_AVAIL) {
  37742. + sdhci_unsignal_irqs(host, SDHCI_INT_DATA_AVAIL |
  37743. + SDHCI_INT_SPACE_AVAIL);
  37744. + sdhci_platdma_write(host, addr, bytes);
  37745. + }
  37746. + }
  37747. + }
  37748. + /* else:
  37749. + we have run out of bytes that need transferring (e.g. we may be in
  37750. + the middle of the last DMA transfer), or
  37751. + it is also possible that we've been called when another IRQ is
  37752. + signalled, even though we've turned off signalling of our own IRQ */
  37753. +
  37754. + *ref_intmask &= ~SDHCI_INT_DATA_END;
  37755. + /* don't let the main sdhci driver act on this .. we'll deal with it
  37756. + when we respond to the DMA - if one is currently in progress */
  37757. +}
  37758. +
  37759. +/* is it possible to DMA the given mmc_data structure?
  37760. + Platform DMA exported function
  37761. +*/
  37762. +int /*bool*/
  37763. +sdhci_bcm2708_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  37764. +{
  37765. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37766. + int ok = bcm_sg_suitable_for_dma(data->sg, data->sg_len);
  37767. +
  37768. + if (!ok)
  37769. + DBG("Reverting to PIO - bad cache alignment\n");
  37770. +
  37771. + else {
  37772. + host_priv->sg_ix = 0; /* first SG index */
  37773. + host_priv->sg_done = 0; /* no bytes done */
  37774. + }
  37775. +
  37776. + return ok;
  37777. +}
  37778. +
  37779. +#include <mach/arm_control.h> //GRAYG
  37780. +/*! the current SD transacton has been abandonned
  37781. + We need to tidy up if we were in the middle of a DMA
  37782. + Platform DMA exported function
  37783. +*/
  37784. +void
  37785. +sdhci_bcm2708_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  37786. +{
  37787. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37788. +// unsigned long flags;
  37789. +
  37790. + BUG_ON(NULL == host);
  37791. +
  37792. +// spin_lock_irqsave(&host->lock, flags);
  37793. +
  37794. + if (host_priv->dma_wanted) {
  37795. + if (NULL == data) {
  37796. + printk(KERN_ERR "%s: ongoing DMA reset - no data!\n",
  37797. + mmc_hostname(host->mmc));
  37798. + BUG_ON(NULL == data);
  37799. + } else {
  37800. + struct scatterlist *sg;
  37801. + int sg_len;
  37802. + int sg_todo;
  37803. + int rc;
  37804. + unsigned long cs;
  37805. +
  37806. + sg = data->sg;
  37807. + sg_len = data->sg_len;
  37808. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  37809. +
  37810. + cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  37811. +
  37812. + if (!(BCM2708_DMA_ACTIVE & cs))
  37813. + {
  37814. + if (extra_messages)
  37815. + printk(KERN_INFO "%s: missed completion of "
  37816. + "cmd DMA (%d/%d [%d]/[%d]) - "
  37817. + "ignoring it\n",
  37818. + mmc_hostname(host->mmc),
  37819. + host_priv->sg_done, sg_todo,
  37820. + host_priv->sg_ix+1, sg_len);
  37821. + }
  37822. + else
  37823. + printk(KERN_INFO "%s: resetting ongoing cmd"
  37824. + "DMA before %d/%d [%d]/[%d] complete\n",
  37825. + mmc_hostname(host->mmc),
  37826. + host_priv->sg_done, sg_todo,
  37827. + host_priv->sg_ix+1, sg_len);
  37828. +#ifdef CHECK_DMA_USE
  37829. + printk(KERN_INFO "%s: now %"FMT_HPT" started %lu "
  37830. + "last reset %lu last stopped %lu\n",
  37831. + mmc_hostname(host->mmc),
  37832. + hptime(), since_ns(host_priv->when_started),
  37833. + since_ns(host_priv->when_reset),
  37834. + since_ns(host_priv->when_stopped));
  37835. + { unsigned long info, debug;
  37836. + void __iomem *base;
  37837. + unsigned long pend0, pend1, pend2;
  37838. +
  37839. + base = host_priv->dma_chan_base;
  37840. + cs = readl(base + BCM2708_DMA_CS);
  37841. + info = readl(base + BCM2708_DMA_INFO);
  37842. + debug = readl(base + BCM2708_DMA_DEBUG);
  37843. + printk(KERN_INFO "%s: DMA%d CS=%08lX TI=%08lX "
  37844. + "DEBUG=%08lX\n",
  37845. + mmc_hostname(host->mmc),
  37846. + host_priv->dma_chan,
  37847. + cs, info, debug);
  37848. + pend0 = readl(__io_address(ARM_IRQ_PEND0));
  37849. + pend1 = readl(__io_address(ARM_IRQ_PEND1));
  37850. + pend2 = readl(__io_address(ARM_IRQ_PEND2));
  37851. +
  37852. + printk(KERN_INFO "%s: PEND0=%08lX "
  37853. + "PEND1=%08lX PEND2=%08lX\n",
  37854. + mmc_hostname(host->mmc),
  37855. + pend0, pend1, pend2);
  37856. +
  37857. + //gintsts = readl(__io_address(GINTSTS));
  37858. + //gintmsk = readl(__io_address(GINTMSK));
  37859. + //printk(KERN_INFO "%s: USB GINTSTS=%08lX"
  37860. + // "GINTMSK=%08lX\n",
  37861. + // mmc_hostname(host->mmc), gintsts, gintmsk);
  37862. + }
  37863. +#endif
  37864. + rc = bcm_dma_abort(host_priv->dma_chan_base);
  37865. + BUG_ON(rc != 0);
  37866. + }
  37867. + host_priv->dma_wanted = 0;
  37868. +#ifdef CHECK_DMA_USE
  37869. + host_priv->when_reset = hptime();
  37870. +#endif
  37871. + }
  37872. +
  37873. +// spin_unlock_irqrestore(&host->lock, flags);
  37874. +}
  37875. +
  37876. +
  37877. +static void sdhci_bcm2708_dma_complete_irq(struct sdhci_host *host,
  37878. + u32 dma_cs)
  37879. +{
  37880. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  37881. + struct mmc_data *data;
  37882. + struct scatterlist *sg;
  37883. + int sg_len;
  37884. + int sg_ix;
  37885. + int sg_todo;
  37886. +// unsigned long flags;
  37887. +
  37888. + BUG_ON(NULL == host);
  37889. +
  37890. +// spin_lock_irqsave(&host->lock, flags);
  37891. + data = host->data;
  37892. +
  37893. +#ifdef CHECK_DMA_USE
  37894. + if (host_priv->dmas_pending <= 0)
  37895. + DBG("on completion no DMA in progress - "
  37896. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  37897. + hptime(), since_ns(host_priv->when_started),
  37898. + since_ns(host_priv->when_reset),
  37899. + since_ns(host_priv->when_stopped));
  37900. + else if (host_priv->dmas_pending > 1)
  37901. + DBG("still %d DMA in progress after completion - "
  37902. + "now %"FMT_HPT" started %lu reset %lu stopped %lu\n",
  37903. + host_priv->dmas_pending - 1,
  37904. + hptime(), since_ns(host_priv->when_started),
  37905. + since_ns(host_priv->when_reset),
  37906. + since_ns(host_priv->when_stopped));
  37907. + BUG_ON(host_priv->dmas_pending <= 0);
  37908. + host_priv->dmas_pending -= 1;
  37909. + host_priv->when_stopped = hptime();
  37910. +#endif
  37911. + host_priv->dma_wanted = 0;
  37912. +
  37913. + if (NULL == data) {
  37914. + DBG("PDMA unused completion - status 0x%X\n", dma_cs);
  37915. +// spin_unlock_irqrestore(&host->lock, flags);
  37916. + return;
  37917. + }
  37918. + sg = data->sg;
  37919. + sg_len = data->sg_len;
  37920. + sg_todo = sg_dma_len(&sg[host_priv->sg_ix]);
  37921. +
  37922. + DBG("PDMA complete %d/%d [%d]/[%d]..\n",
  37923. + host_priv->sg_done, sg_todo,
  37924. + host_priv->sg_ix+1, sg_len);
  37925. +
  37926. + BUG_ON(host_priv->sg_done > sg_todo);
  37927. +
  37928. + if (host_priv->sg_done >= sg_todo) {
  37929. + host_priv->sg_ix++;
  37930. + host_priv->sg_done = 0;
  37931. + }
  37932. +
  37933. + sg_ix = host_priv->sg_ix;
  37934. + if (sg_ix < sg_len) {
  37935. + u32 irq_mask;
  37936. + /* Set off next DMA if we've got the capacity */
  37937. +
  37938. + if (data->flags & MMC_DATA_READ)
  37939. + irq_mask = SDHCI_INT_DATA_AVAIL;
  37940. + else
  37941. + irq_mask = SDHCI_INT_SPACE_AVAIL;
  37942. +
  37943. + /* We have to use the interrupt status register on the BCM2708
  37944. + rather than the SDHCI_PRESENT_STATE register because latency
  37945. + in the glue logic means that the information retrieved from
  37946. + the latter is not always up-to-date w.r.t the DMA engine -
  37947. + it may not indicate that a read or a write is ready yet */
  37948. + if (sdhci_bcm2708_raw_readl(host, SDHCI_INT_STATUS) &
  37949. + irq_mask) {
  37950. + size_t bytes = sg_dma_len(&sg[sg_ix]) -
  37951. + host_priv->sg_done;
  37952. + dma_addr_t addr = sg_dma_address(&data->sg[sg_ix]) +
  37953. + host_priv->sg_done;
  37954. +
  37955. + /* acknowledge interrupt */
  37956. + sdhci_bcm2708_raw_writel(host, irq_mask,
  37957. + SDHCI_INT_STATUS);
  37958. +
  37959. + BUG_ON(0 == bytes);
  37960. +
  37961. + if (data->flags & MMC_DATA_READ)
  37962. + sdhci_platdma_read(host, addr, bytes);
  37963. + else
  37964. + sdhci_platdma_write(host, addr, bytes);
  37965. + } else {
  37966. + DBG("PDMA - wait avail\n");
  37967. + /* may generate an IRQ if already present */
  37968. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  37969. + SDHCI_INT_SPACE_AVAIL);
  37970. + }
  37971. + } else {
  37972. + if (sync_after_dma) {
  37973. + /* On the Arasan controller the stop command (which will be
  37974. + scheduled after this completes) does not seem to work
  37975. + properly if we allow it to be issued when we are
  37976. + transferring data to/from the SD card.
  37977. + We get CRC and DEND errors unless we wait for
  37978. + the SD controller to finish reading/writing to the card. */
  37979. + u32 state_mask;
  37980. + int timeout=3*1000*1000;
  37981. +
  37982. + DBG("PDMA over - sync card\n");
  37983. + if (data->flags & MMC_DATA_READ)
  37984. + state_mask = SDHCI_DOING_READ;
  37985. + else
  37986. + state_mask = SDHCI_DOING_WRITE;
  37987. +
  37988. + while (0 != (sdhci_bcm2708_raw_readl(host, SDHCI_PRESENT_STATE)
  37989. + & state_mask) && --timeout > 0)
  37990. + {
  37991. + udelay(1);
  37992. + continue;
  37993. + }
  37994. + if (timeout <= 0)
  37995. + printk(KERN_ERR"%s: final %s to SD card still "
  37996. + "running\n",
  37997. + mmc_hostname(host->mmc),
  37998. + data->flags & MMC_DATA_READ? "read": "write");
  37999. + }
  38000. + if (host_priv->complete) {
  38001. + (*host_priv->complete)(host);
  38002. + DBG("PDMA %s complete\n",
  38003. + data->flags & MMC_DATA_READ?"read":"write");
  38004. + sdhci_signal_irqs(host, SDHCI_INT_DATA_AVAIL |
  38005. + SDHCI_INT_SPACE_AVAIL);
  38006. + }
  38007. + }
  38008. +// spin_unlock_irqrestore(&host->lock, flags);
  38009. +}
  38010. +
  38011. +static irqreturn_t sdhci_bcm2708_dma_irq(int irq, void *dev_id)
  38012. +{
  38013. + irqreturn_t result = IRQ_NONE;
  38014. + struct sdhci_host *host = dev_id;
  38015. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  38016. + u32 dma_cs; /* control and status register */
  38017. + unsigned long flags;
  38018. +
  38019. + BUG_ON(NULL == dev_id);
  38020. + BUG_ON(NULL == host_priv->dma_chan_base);
  38021. +
  38022. + spin_lock_irqsave(&host->lock, flags);
  38023. +
  38024. + dma_cs = readl(host_priv->dma_chan_base + BCM2708_DMA_CS);
  38025. +
  38026. + if (dma_cs & BCM2708_DMA_ERR) {
  38027. + unsigned long debug;
  38028. + debug = readl(host_priv->dma_chan_base +
  38029. + BCM2708_DMA_DEBUG);
  38030. + printk(KERN_ERR "%s: DMA error - CS %lX DEBUG %lX\n",
  38031. + mmc_hostname(host->mmc), (unsigned long)dma_cs,
  38032. + (unsigned long)debug);
  38033. + /* reset error */
  38034. + writel(debug, host_priv->dma_chan_base +
  38035. + BCM2708_DMA_DEBUG);
  38036. + }
  38037. + if (dma_cs & BCM2708_DMA_INT) {
  38038. + /* acknowledge interrupt */
  38039. + writel(BCM2708_DMA_INT,
  38040. + host_priv->dma_chan_base + BCM2708_DMA_CS);
  38041. +
  38042. + dsb(); /* ARM data synchronization (push) operation */
  38043. +
  38044. + if (!host_priv->dma_wanted) {
  38045. + /* ignore this interrupt - it was reset */
  38046. + if (extra_messages)
  38047. + printk(KERN_INFO "%s: DMA IRQ %X ignored - "
  38048. + "results were reset\n",
  38049. + mmc_hostname(host->mmc), dma_cs);
  38050. +#ifdef CHECK_DMA_USE
  38051. + printk(KERN_INFO "%s: now %"FMT_HPT
  38052. + " started %lu reset %lu stopped %lu\n",
  38053. + mmc_hostname(host->mmc), hptime(),
  38054. + since_ns(host_priv->when_started),
  38055. + since_ns(host_priv->when_reset),
  38056. + since_ns(host_priv->when_stopped));
  38057. + host_priv->dmas_pending--;
  38058. +#endif
  38059. + } else
  38060. + sdhci_bcm2708_dma_complete_irq(host, dma_cs);
  38061. +
  38062. + result = IRQ_HANDLED;
  38063. + }
  38064. + spin_unlock_irqrestore(&host->lock, flags);
  38065. +
  38066. + return result;
  38067. +}
  38068. +#endif /* CONFIG_MMC_SDHCI_BCM2708_DMA */
  38069. +
  38070. +
  38071. +/***************************************************************************** \
  38072. + * *
  38073. + * Device Attributes *
  38074. + * *
  38075. +\*****************************************************************************/
  38076. +
  38077. +
  38078. +/**
  38079. + * Show the DMA-using status
  38080. + */
  38081. +static ssize_t attr_dma_show(struct device *_dev,
  38082. + struct device_attribute *attr, char *buf)
  38083. +{
  38084. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  38085. +
  38086. + if (host) {
  38087. + int use_dma = (host->flags & SDHCI_USE_PLATDMA? 1:0);
  38088. + return sprintf(buf, "%d\n", use_dma);
  38089. + } else
  38090. + return -EINVAL;
  38091. +}
  38092. +
  38093. +/**
  38094. + * Set the DMA-using status
  38095. + */
  38096. +static ssize_t attr_dma_store(struct device *_dev,
  38097. + struct device_attribute *attr,
  38098. + const char *buf, size_t count)
  38099. +{
  38100. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  38101. +
  38102. + if (host) {
  38103. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38104. + int on = simple_strtol(buf, NULL, 0);
  38105. + if (on) {
  38106. + host->flags |= SDHCI_USE_PLATDMA;
  38107. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  38108. + printk(KERN_INFO "%s: DMA enabled\n",
  38109. + mmc_hostname(host->mmc));
  38110. + } else {
  38111. + host->flags &= ~(SDHCI_USE_PLATDMA | SDHCI_REQ_USE_DMA);
  38112. + sdhci_bcm2708_writel(host, 0, REG_EXRDFIFO_EN);
  38113. + printk(KERN_INFO "%s: DMA disabled\n",
  38114. + mmc_hostname(host->mmc));
  38115. + }
  38116. +#endif
  38117. + return count;
  38118. + } else
  38119. + return -EINVAL;
  38120. +}
  38121. +
  38122. +static DEVICE_ATTR(use_dma, S_IRUGO | S_IWUGO, attr_dma_show, attr_dma_store);
  38123. +
  38124. +
  38125. +/**
  38126. + * Show the DMA wait states used
  38127. + */
  38128. +static ssize_t attr_dmawait_show(struct device *_dev,
  38129. + struct device_attribute *attr, char *buf)
  38130. +{
  38131. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  38132. +
  38133. + if (host) {
  38134. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  38135. + int dmawait = host_priv->dma_waits;
  38136. + return sprintf(buf, "%d\n", dmawait);
  38137. + } else
  38138. + return -EINVAL;
  38139. +}
  38140. +
  38141. +/**
  38142. + * Set the DMA wait state used
  38143. + */
  38144. +static ssize_t attr_dmawait_store(struct device *_dev,
  38145. + struct device_attribute *attr,
  38146. + const char *buf, size_t count)
  38147. +{
  38148. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  38149. +
  38150. + if (host) {
  38151. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38152. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  38153. + int dma_waits = simple_strtol(buf, NULL, 0);
  38154. + if (dma_waits >= 0 && dma_waits < 32)
  38155. + host_priv->dma_waits = dma_waits;
  38156. + else
  38157. + printk(KERN_ERR "%s: illegal dma_waits value - %d",
  38158. + mmc_hostname(host->mmc), dma_waits);
  38159. +#endif
  38160. + return count;
  38161. + } else
  38162. + return -EINVAL;
  38163. +}
  38164. +
  38165. +static DEVICE_ATTR(dma_wait, S_IRUGO | S_IWUGO,
  38166. + attr_dmawait_show, attr_dmawait_store);
  38167. +
  38168. +
  38169. +/**
  38170. + * Show the DMA-using status
  38171. + */
  38172. +static ssize_t attr_status_show(struct device *_dev,
  38173. + struct device_attribute *attr, char *buf)
  38174. +{
  38175. + struct sdhci_host *host = (struct sdhci_host *)dev_get_drvdata(_dev);
  38176. +
  38177. + if (host) {
  38178. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  38179. + return sprintf(buf,
  38180. + "present: yes\n"
  38181. + "power: %s\n"
  38182. + "clock: %u Hz\n"
  38183. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38184. + "dma: %s (%d waits)\n",
  38185. +#else
  38186. + "dma: unconfigured\n",
  38187. +#endif
  38188. + "always on",
  38189. + host->clock
  38190. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38191. + , (host->flags & SDHCI_USE_PLATDMA)? "on": "off"
  38192. + , host_priv->dma_waits
  38193. +#endif
  38194. + );
  38195. + } else
  38196. + return -EINVAL;
  38197. +}
  38198. +
  38199. +static DEVICE_ATTR(status, S_IRUGO, attr_status_show, NULL);
  38200. +
  38201. +/***************************************************************************** \
  38202. + * *
  38203. + * Power Management *
  38204. + * *
  38205. +\*****************************************************************************/
  38206. +
  38207. +
  38208. +#ifdef CONFIG_PM
  38209. +static int sdhci_bcm2708_suspend(struct platform_device *dev, pm_message_t state)
  38210. +{
  38211. + struct sdhci_host *host = (struct sdhci_host *)
  38212. + platform_get_drvdata(dev);
  38213. + int ret = 0;
  38214. +
  38215. + if (host->mmc) {
  38216. + //ret = mmc_suspend_host(host->mmc);
  38217. + }
  38218. +
  38219. + return ret;
  38220. +}
  38221. +
  38222. +static int sdhci_bcm2708_resume(struct platform_device *dev)
  38223. +{
  38224. + struct sdhci_host *host = (struct sdhci_host *)
  38225. + platform_get_drvdata(dev);
  38226. + int ret = 0;
  38227. +
  38228. + if (host->mmc) {
  38229. + //ret = mmc_resume_host(host->mmc);
  38230. + }
  38231. +
  38232. + return ret;
  38233. +}
  38234. +#endif
  38235. +
  38236. +
  38237. +/*****************************************************************************\
  38238. + * *
  38239. + * Device quirk functions. Implemented as local ops because the flags *
  38240. + * field is out of space with newer kernels. This implementation can be *
  38241. + * back ported to older kernels as well. *
  38242. +\****************************************************************************/
  38243. +static unsigned int sdhci_bcm2708_quirk_extra_ints(struct sdhci_host *host)
  38244. +{
  38245. + return 1;
  38246. +}
  38247. +
  38248. +static unsigned int sdhci_bcm2708_missing_status(struct sdhci_host *host)
  38249. +{
  38250. + return 1;
  38251. +}
  38252. +
  38253. +/***************************************************************************** \
  38254. + * *
  38255. + * Device ops *
  38256. + * *
  38257. +\*****************************************************************************/
  38258. +
  38259. +static struct sdhci_ops sdhci_bcm2708_ops = {
  38260. +#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
  38261. + .read_l = sdhci_bcm2708_readl,
  38262. + .read_w = sdhci_bcm2708_readw,
  38263. + .read_b = sdhci_bcm2708_readb,
  38264. + .write_l = sdhci_bcm2708_writel,
  38265. + .write_w = sdhci_bcm2708_writew,
  38266. + .write_b = sdhci_bcm2708_writeb,
  38267. +#else
  38268. +#error The BCM2708 SDHCI driver needs CONFIG_MMC_SDHCI_IO_ACCESSORS to be set
  38269. +#endif
  38270. + .get_max_clock = sdhci_bcm2708_get_max_clock,
  38271. +
  38272. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38273. + // Platform DMA operations
  38274. + .pdma_able = sdhci_bcm2708_platdma_dmaable,
  38275. + .pdma_avail = sdhci_bcm2708_platdma_avail,
  38276. + .pdma_reset = sdhci_bcm2708_platdma_reset,
  38277. +#endif
  38278. + .extra_ints = sdhci_bcm2708_quirk_extra_ints,
  38279. +
  38280. + .set_clock = sdhci_set_clock,
  38281. + .set_bus_width = sdhci_set_bus_width,
  38282. + .reset = sdhci_reset,
  38283. + .set_uhs_signaling = sdhci_set_uhs_signaling,
  38284. +};
  38285. +
  38286. +/*****************************************************************************\
  38287. + * *
  38288. + * Device probing/removal *
  38289. + * *
  38290. +\*****************************************************************************/
  38291. +
  38292. +static int sdhci_bcm2708_probe(struct platform_device *pdev)
  38293. +{
  38294. + struct sdhci_host *host;
  38295. + struct resource *iomem;
  38296. + struct sdhci_bcm2708_priv *host_priv;
  38297. + int ret;
  38298. +
  38299. + BUG_ON(pdev == NULL);
  38300. +
  38301. + iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  38302. + if (!iomem) {
  38303. + ret = -ENOMEM;
  38304. + goto err;
  38305. + }
  38306. +
  38307. + if (resource_size(iomem) != 0x100)
  38308. + dev_err(&pdev->dev, "Invalid iomem size. You may "
  38309. + "experience problems.\n");
  38310. +
  38311. + if (pdev->dev.parent)
  38312. + host = sdhci_alloc_host(pdev->dev.parent,
  38313. + sizeof(struct sdhci_bcm2708_priv));
  38314. + else
  38315. + host = sdhci_alloc_host(&pdev->dev,
  38316. + sizeof(struct sdhci_bcm2708_priv));
  38317. +
  38318. + if (IS_ERR(host)) {
  38319. + ret = PTR_ERR(host);
  38320. + goto err;
  38321. + }
  38322. + if (missing_status) {
  38323. + sdhci_bcm2708_ops.missing_status = sdhci_bcm2708_missing_status;
  38324. + }
  38325. +
  38326. + host->hw_name = "BCM2708_Arasan";
  38327. + host->ops = &sdhci_bcm2708_ops;
  38328. + host->irq = platform_get_irq(pdev, 0);
  38329. +
  38330. + host->quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  38331. + SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
  38332. + SDHCI_QUIRK_BROKEN_TIMEOUT_VAL |
  38333. + SDHCI_QUIRK_MISSING_CAPS |
  38334. + SDHCI_QUIRK_NO_HISPD_BIT |
  38335. + (sync_after_dma ? 0:SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12);
  38336. +
  38337. +
  38338. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38339. + host->flags = SDHCI_USE_PLATDMA;
  38340. +#endif
  38341. +
  38342. + if (!request_mem_region(iomem->start, resource_size(iomem),
  38343. + mmc_hostname(host->mmc))) {
  38344. + dev_err(&pdev->dev, "cannot request region\n");
  38345. + ret = -EBUSY;
  38346. + goto err_request;
  38347. + }
  38348. +
  38349. + host->ioaddr = ioremap(iomem->start, resource_size(iomem));
  38350. + if (!host->ioaddr) {
  38351. + dev_err(&pdev->dev, "failed to remap registers\n");
  38352. + ret = -ENOMEM;
  38353. + goto err_remap;
  38354. + }
  38355. +
  38356. + host_priv = SDHCI_HOST_PRIV(host);
  38357. +
  38358. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38359. + host_priv->dma_wanted = 0;
  38360. +#ifdef CHECK_DMA_USE
  38361. + host_priv->dmas_pending = 0;
  38362. + host_priv->when_started = 0;
  38363. + host_priv->when_reset = 0;
  38364. + host_priv->when_stopped = 0;
  38365. +#endif
  38366. + host_priv->sg_ix = 0;
  38367. + host_priv->sg_done = 0;
  38368. + host_priv->complete = NULL;
  38369. + host_priv->dma_waits = SDHCI_BCM_DMA_WAITS;
  38370. +
  38371. + host_priv->cb_base = dma_alloc_writecombine(&pdev->dev, SZ_4K,
  38372. + &host_priv->cb_handle,
  38373. + GFP_KERNEL);
  38374. + if (!host_priv->cb_base) {
  38375. + dev_err(&pdev->dev, "cannot allocate DMA CBs\n");
  38376. + ret = -ENOMEM;
  38377. + goto err_alloc_cb;
  38378. + }
  38379. +
  38380. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_FAST,
  38381. + &host_priv->dma_chan_base,
  38382. + &host_priv->dma_irq);
  38383. + if (ret < 0) {
  38384. + dev_err(&pdev->dev, "couldn't allocate a DMA channel\n");
  38385. + goto err_add_dma;
  38386. + }
  38387. + host_priv->dma_chan = ret;
  38388. +
  38389. + ret = request_irq(host_priv->dma_irq, sdhci_bcm2708_dma_irq,
  38390. + IRQF_SHARED, DRIVER_NAME " (dma)", host);
  38391. + if (ret) {
  38392. + dev_err(&pdev->dev, "cannot set DMA IRQ\n");
  38393. + goto err_add_dma_irq;
  38394. + }
  38395. + DBG("DMA CBs %p handle %08X DMA%d %p DMA IRQ %d\n",
  38396. + host_priv->cb_base, (unsigned)host_priv->cb_handle,
  38397. + host_priv->dma_chan, host_priv->dma_chan_base,
  38398. + host_priv->dma_irq);
  38399. +
  38400. + // we support 3.3V
  38401. + host->caps |= SDHCI_CAN_VDD_330;
  38402. + if (allow_highspeed)
  38403. + host->mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  38404. +
  38405. + /* single block writes cause data loss with some SD cards! */
  38406. + host->mmc->caps2 |= MMC_CAP2_FORCE_MULTIBLOCK;
  38407. +#endif
  38408. +
  38409. + ret = sdhci_add_host(host);
  38410. + if (ret)
  38411. + goto err_add_host;
  38412. +
  38413. + platform_set_drvdata(pdev, host);
  38414. + ret = device_create_file(&pdev->dev, &dev_attr_use_dma);
  38415. + ret = device_create_file(&pdev->dev, &dev_attr_dma_wait);
  38416. + ret = device_create_file(&pdev->dev, &dev_attr_status);
  38417. +
  38418. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38419. + /* enable extension fifo for paced DMA transfers */
  38420. + sdhci_bcm2708_writel(host, 1, REG_EXRDFIFO_EN);
  38421. + sdhci_bcm2708_writel(host, 4, REG_EXRDFIFO_CFG);
  38422. +#endif
  38423. +
  38424. + printk(KERN_INFO "%s: BCM2708 SDHC host at 0x%08llx DMA %d IRQ %d\n",
  38425. + mmc_hostname(host->mmc), (unsigned long long)iomem->start,
  38426. + host_priv->dma_chan, host_priv->dma_irq);
  38427. +
  38428. + return 0;
  38429. +
  38430. +err_add_host:
  38431. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38432. + free_irq(host_priv->dma_irq, host);
  38433. +err_add_dma_irq:
  38434. + bcm_dma_chan_free(host_priv->dma_chan);
  38435. +err_add_dma:
  38436. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  38437. + host_priv->cb_handle);
  38438. +err_alloc_cb:
  38439. +#endif
  38440. + iounmap(host->ioaddr);
  38441. +err_remap:
  38442. + release_mem_region(iomem->start, resource_size(iomem));
  38443. +err_request:
  38444. + sdhci_free_host(host);
  38445. +err:
  38446. + dev_err(&pdev->dev, "probe failed, err %d\n", ret);
  38447. + return ret;
  38448. +}
  38449. +
  38450. +static int sdhci_bcm2708_remove(struct platform_device *pdev)
  38451. +{
  38452. + struct sdhci_host *host = platform_get_drvdata(pdev);
  38453. + struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  38454. + struct sdhci_bcm2708_priv *host_priv = SDHCI_HOST_PRIV(host);
  38455. + int dead;
  38456. + u32 scratch;
  38457. +
  38458. + dead = 0;
  38459. + scratch = sdhci_bcm2708_readl(host, SDHCI_INT_STATUS);
  38460. + if (scratch == (u32)-1)
  38461. + dead = 1;
  38462. +
  38463. + device_remove_file(&pdev->dev, &dev_attr_status);
  38464. + device_remove_file(&pdev->dev, &dev_attr_dma_wait);
  38465. + device_remove_file(&pdev->dev, &dev_attr_use_dma);
  38466. +
  38467. +#ifdef CONFIG_MMC_SDHCI_BCM2708_DMA
  38468. + free_irq(host_priv->dma_irq, host);
  38469. + dma_free_writecombine(&pdev->dev, SZ_4K, host_priv->cb_base,
  38470. + host_priv->cb_handle);
  38471. +#endif
  38472. + sdhci_remove_host(host, dead);
  38473. + iounmap(host->ioaddr);
  38474. + release_mem_region(iomem->start, resource_size(iomem));
  38475. + sdhci_free_host(host);
  38476. + platform_set_drvdata(pdev, NULL);
  38477. +
  38478. + return 0;
  38479. +}
  38480. +
  38481. +static struct platform_driver sdhci_bcm2708_driver = {
  38482. + .driver = {
  38483. + .name = DRIVER_NAME,
  38484. + .owner = THIS_MODULE,
  38485. + },
  38486. + .probe = sdhci_bcm2708_probe,
  38487. + .remove = sdhci_bcm2708_remove,
  38488. +
  38489. +#ifdef CONFIG_PM
  38490. + .suspend = sdhci_bcm2708_suspend,
  38491. + .resume = sdhci_bcm2708_resume,
  38492. +#endif
  38493. +
  38494. +};
  38495. +
  38496. +/*****************************************************************************\
  38497. + * *
  38498. + * Driver init/exit *
  38499. + * *
  38500. +\*****************************************************************************/
  38501. +
  38502. +static int __init sdhci_drv_init(void)
  38503. +{
  38504. + return platform_driver_register(&sdhci_bcm2708_driver);
  38505. +}
  38506. +
  38507. +static void __exit sdhci_drv_exit(void)
  38508. +{
  38509. + platform_driver_unregister(&sdhci_bcm2708_driver);
  38510. +}
  38511. +
  38512. +module_init(sdhci_drv_init);
  38513. +module_exit(sdhci_drv_exit);
  38514. +
  38515. +module_param(allow_highspeed, bool, 0444);
  38516. +module_param(emmc_clock_freq, int, 0444);
  38517. +module_param(sync_after_dma, bool, 0444);
  38518. +module_param(missing_status, bool, 0444);
  38519. +module_param(cycle_delay, int, 0444);
  38520. +module_param(extra_messages, bool, 0444);
  38521. +
  38522. +MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
  38523. +MODULE_AUTHOR("Broadcom <info@broadcom.com>");
  38524. +MODULE_LICENSE("GPL v2");
  38525. +MODULE_ALIAS("platform:"DRIVER_NAME);
  38526. +
  38527. +MODULE_PARM_DESC(allow_highspeed, "Allow high speed transfers modes");
  38528. +MODULE_PARM_DESC(emmc_clock_freq, "Specify the speed of emmc clock");
  38529. +MODULE_PARM_DESC(sync_after_dma, "Block in driver until dma complete");
  38530. +MODULE_PARM_DESC(missing_status, "Use the missing status quirk");
  38531. +MODULE_PARM_DESC(extra_messages, "Enable more sdcard warning messages");
  38532. diff -Nur linux-3.16.2/drivers/mmc/host/sdhci.c linux-3.16-rpi/drivers/mmc/host/sdhci.c
  38533. --- linux-3.16.2/drivers/mmc/host/sdhci.c 2014-09-06 01:37:11.000000000 +0200
  38534. +++ linux-3.16-rpi/drivers/mmc/host/sdhci.c 2014-09-14 19:03:25.000000000 +0200
  38535. @@ -28,6 +28,7 @@
  38536. #include <linux/mmc/mmc.h>
  38537. #include <linux/mmc/host.h>
  38538. #include <linux/mmc/card.h>
  38539. +#include <linux/mmc/sd.h>
  38540. #include <linux/mmc/slot-gpio.h>
  38541. #include "sdhci.h"
  38542. @@ -312,7 +313,7 @@
  38543. u32 uninitialized_var(scratch);
  38544. u8 *buf;
  38545. - DBG("PIO reading\n");
  38546. + DBG("PIO reading %db\n", host->data->blksz);
  38547. blksize = host->data->blksz;
  38548. chunk = 0;
  38549. @@ -357,7 +358,7 @@
  38550. u32 scratch;
  38551. u8 *buf;
  38552. - DBG("PIO writing\n");
  38553. + DBG("PIO writing %db\n", host->data->blksz);
  38554. blksize = host->data->blksz;
  38555. chunk = 0;
  38556. @@ -396,19 +397,28 @@
  38557. local_irq_restore(flags);
  38558. }
  38559. -static void sdhci_transfer_pio(struct sdhci_host *host)
  38560. +static void sdhci_transfer_pio(struct sdhci_host *host, u32 intstate)
  38561. {
  38562. u32 mask;
  38563. + u32 state = 0;
  38564. + u32 intmask;
  38565. + int available;
  38566. BUG_ON(!host->data);
  38567. if (host->blocks == 0)
  38568. return;
  38569. - if (host->data->flags & MMC_DATA_READ)
  38570. + if (host->data->flags & MMC_DATA_READ) {
  38571. mask = SDHCI_DATA_AVAILABLE;
  38572. - else
  38573. + intmask = SDHCI_INT_DATA_AVAIL;
  38574. + } else {
  38575. mask = SDHCI_SPACE_AVAILABLE;
  38576. + intmask = SDHCI_INT_SPACE_AVAIL;
  38577. + }
  38578. +
  38579. + /* initially we can see whether we can procede using intstate */
  38580. + available = (intstate & intmask);
  38581. /*
  38582. * Some controllers (JMicron JMB38x) mess up the buffer bits
  38583. @@ -419,7 +429,7 @@
  38584. (host->data->blocks == 1))
  38585. mask = ~0;
  38586. - while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  38587. + while (available) {
  38588. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  38589. udelay(100);
  38590. @@ -431,9 +441,12 @@
  38591. host->blocks--;
  38592. if (host->blocks == 0)
  38593. break;
  38594. + state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  38595. + available = state & mask;
  38596. + break;
  38597. }
  38598. - DBG("PIO transfer complete.\n");
  38599. + DBG("PIO transfer complete - %d blocks left.\n", host->blocks);
  38600. }
  38601. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  38602. @@ -698,7 +711,9 @@
  38603. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  38604. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  38605. - if (host->flags & SDHCI_REQ_USE_DMA)
  38606. + /* platform DMA will begin on receipt of PIO irqs */
  38607. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  38608. + !(host->flags & SDHCI_USE_PLATDMA))
  38609. host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  38610. else
  38611. host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  38612. @@ -733,44 +748,25 @@
  38613. host->data_early = 0;
  38614. host->data->bytes_xfered = 0;
  38615. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))
  38616. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA | SDHCI_USE_PLATDMA))
  38617. host->flags |= SDHCI_REQ_USE_DMA;
  38618. /*
  38619. * FIXME: This doesn't account for merging when mapping the
  38620. * scatterlist.
  38621. */
  38622. - if (host->flags & SDHCI_REQ_USE_DMA) {
  38623. - int broken, i;
  38624. - struct scatterlist *sg;
  38625. -
  38626. - broken = 0;
  38627. - if (host->flags & SDHCI_USE_ADMA) {
  38628. - if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE)
  38629. - broken = 1;
  38630. - } else {
  38631. - if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  38632. - broken = 1;
  38633. - }
  38634. -
  38635. - if (unlikely(broken)) {
  38636. - for_each_sg(data->sg, sg, data->sg_len, i) {
  38637. - if (sg->length & 0x3) {
  38638. - DBG("Reverting to PIO because of "
  38639. - "transfer size (%d)\n",
  38640. - sg->length);
  38641. - host->flags &= ~SDHCI_REQ_USE_DMA;
  38642. - break;
  38643. - }
  38644. - }
  38645. - }
  38646. - }
  38647. /*
  38648. * The assumption here being that alignment is the same after
  38649. * translation to device address space.
  38650. */
  38651. - if (host->flags & SDHCI_REQ_USE_DMA) {
  38652. + if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) ==
  38653. + (SDHCI_REQ_USE_DMA | SDHCI_USE_PLATDMA)) {
  38654. +
  38655. + if (! sdhci_platdma_dmaable(host, data))
  38656. + host->flags &= ~SDHCI_REQ_USE_DMA;
  38657. +
  38658. + } else if (host->flags & SDHCI_REQ_USE_DMA) {
  38659. int broken, i;
  38660. struct scatterlist *sg;
  38661. @@ -829,7 +825,8 @@
  38662. */
  38663. WARN_ON(1);
  38664. host->flags &= ~SDHCI_REQ_USE_DMA;
  38665. - } else {
  38666. + } else
  38667. + if (!(host->flags & SDHCI_USE_PLATDMA)) {
  38668. WARN_ON(sg_cnt != 1);
  38669. sdhci_writel(host, sg_dma_address(data->sg),
  38670. SDHCI_DMA_ADDRESS);
  38671. @@ -845,11 +842,13 @@
  38672. if (host->version >= SDHCI_SPEC_200) {
  38673. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  38674. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  38675. + if (! (host->flags & SDHCI_USE_PLATDMA)) {
  38676. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  38677. (host->flags & SDHCI_USE_ADMA))
  38678. ctrl |= SDHCI_CTRL_ADMA32;
  38679. else
  38680. ctrl |= SDHCI_CTRL_SDMA;
  38681. + }
  38682. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  38683. }
  38684. @@ -906,7 +905,8 @@
  38685. if (data->flags & MMC_DATA_READ)
  38686. mode |= SDHCI_TRNS_READ;
  38687. - if (host->flags & SDHCI_REQ_USE_DMA)
  38688. + if ((host->flags & SDHCI_REQ_USE_DMA) &&
  38689. + !(host->flags & SDHCI_USE_PLATDMA))
  38690. mode |= SDHCI_TRNS_DMA;
  38691. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  38692. @@ -922,13 +922,16 @@
  38693. host->data = NULL;
  38694. if (host->flags & SDHCI_REQ_USE_DMA) {
  38695. - if (host->flags & SDHCI_USE_ADMA)
  38696. - sdhci_adma_table_post(host, data);
  38697. - else {
  38698. + /* we may have to abandon an ongoing platform DMA */
  38699. + if (host->flags & SDHCI_USE_PLATDMA)
  38700. + sdhci_platdma_reset(host, data);
  38701. +
  38702. + if (host->flags & (SDHCI_USE_PLATDMA | SDHCI_USE_SDMA)) {
  38703. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  38704. data->sg_len, (data->flags & MMC_DATA_READ) ?
  38705. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  38706. - }
  38707. + } else if (host->flags & SDHCI_USE_ADMA)
  38708. + sdhci_adma_table_post(host, data);
  38709. }
  38710. /*
  38711. @@ -981,6 +984,12 @@
  38712. if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
  38713. mask |= SDHCI_DATA_INHIBIT;
  38714. + if(host->ops->missing_status && (cmd->opcode == MMC_SEND_STATUS)) {
  38715. + timeout = 5000; // Really obscenely large delay to send the status, due to bug in controller
  38716. + // which might cause the STATUS command to get stuck when a data operation is in flow
  38717. + mask |= SDHCI_DATA_INHIBIT;
  38718. + }
  38719. +
  38720. /* We shouldn't wait for data inihibit for stop commands, even
  38721. though they might use busy signaling */
  38722. if (host->mrq->data && (cmd == host->mrq->data->stop))
  38723. @@ -998,6 +1007,8 @@
  38724. timeout--;
  38725. mdelay(1);
  38726. }
  38727. + DBG("send cmd %d - wait 0x%X irq 0x%x\n", cmd->opcode, mask,
  38728. + sdhci_readl(host, SDHCI_INT_STATUS));
  38729. timeout = jiffies;
  38730. if (!cmd->data && cmd->busy_timeout > 9000)
  38731. @@ -2205,10 +2216,13 @@
  38732. BUG_ON(intmask == 0);
  38733. if (!host->cmd) {
  38734. + if (!(host->ops->extra_ints)) {
  38735. pr_err("%s: Got command interrupt 0x%08x even "
  38736. "though no command operation was in progress.\n",
  38737. mmc_hostname(host->mmc), (unsigned)intmask);
  38738. sdhci_dumpregs(host);
  38739. + } else
  38740. + DBG("cmd irq 0x%08x cmd complete\n", (unsigned)intmask);
  38741. return;
  38742. }
  38743. @@ -2278,6 +2292,19 @@
  38744. static void sdhci_show_adma_error(struct sdhci_host *host) { }
  38745. #endif
  38746. +static void sdhci_data_end(struct sdhci_host *host)
  38747. +{
  38748. + if (host->cmd) {
  38749. + /*
  38750. + * Data managed to finish before the
  38751. + * command completed. Make sure we do
  38752. + * things in the proper order.
  38753. + */
  38754. + host->data_early = 1;
  38755. + } else
  38756. + sdhci_finish_data(host);
  38757. +}
  38758. +
  38759. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  38760. {
  38761. u32 command;
  38762. @@ -2307,10 +2334,13 @@
  38763. }
  38764. }
  38765. + if (!(host->ops->extra_ints)) {
  38766. pr_err("%s: Got data interrupt 0x%08x even "
  38767. "though no data operation was in progress.\n",
  38768. mmc_hostname(host->mmc), (unsigned)intmask);
  38769. sdhci_dumpregs(host);
  38770. + } else
  38771. + DBG("data irq 0x%08x but no data\n", (unsigned)intmask);
  38772. return;
  38773. }
  38774. @@ -2334,8 +2364,14 @@
  38775. if (host->data->error)
  38776. sdhci_finish_data(host);
  38777. else {
  38778. - if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  38779. - sdhci_transfer_pio(host);
  38780. + if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL)) {
  38781. + if (host->flags & SDHCI_REQ_USE_DMA) {
  38782. + /* possible only in PLATDMA mode */
  38783. + sdhci_platdma_avail(host, &intmask,
  38784. + &sdhci_data_end);
  38785. + } else
  38786. + sdhci_transfer_pio(host, intmask);
  38787. + }
  38788. /*
  38789. * We currently don't do anything fancy with DMA
  38790. @@ -2364,18 +2400,8 @@
  38791. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  38792. }
  38793. - if (intmask & SDHCI_INT_DATA_END) {
  38794. - if (host->cmd) {
  38795. - /*
  38796. - * Data managed to finish before the
  38797. - * command completed. Make sure we do
  38798. - * things in the proper order.
  38799. - */
  38800. - host->data_early = 1;
  38801. - } else {
  38802. - sdhci_finish_data(host);
  38803. - }
  38804. - }
  38805. + if (intmask & SDHCI_INT_DATA_END)
  38806. + sdhci_data_end(host);
  38807. }
  38808. }
  38809. @@ -2438,6 +2464,22 @@
  38810. result = IRQ_WAKE_THREAD;
  38811. }
  38812. + if (intmask & SDHCI_INT_ERROR_MASK & ~SDHCI_INT_ERROR)
  38813. + DBG("controller reports error 0x%x -"
  38814. + "%s%s%s%s%s%s%s%s%s%s",
  38815. + intmask,
  38816. + intmask & SDHCI_INT_TIMEOUT? " timeout": "",
  38817. + intmask & SDHCI_INT_CRC ? " crc": "",
  38818. + intmask & SDHCI_INT_END_BIT? " endbit": "",
  38819. + intmask & SDHCI_INT_INDEX? " index": "",
  38820. + intmask & SDHCI_INT_DATA_TIMEOUT? " data_timeout": "",
  38821. + intmask & SDHCI_INT_DATA_CRC? " data_crc": "",
  38822. + intmask & SDHCI_INT_DATA_END_BIT? " data_endbit": "",
  38823. + intmask & SDHCI_INT_BUS_POWER? " buspower": "",
  38824. + intmask & SDHCI_INT_ACMD12ERR? " acmd12": "",
  38825. + intmask & SDHCI_INT_ADMA_ERROR? " adma": ""
  38826. + );
  38827. +
  38828. if (intmask & SDHCI_INT_CMD_MASK)
  38829. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  38830. @@ -2459,6 +2501,14 @@
  38831. SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  38832. SDHCI_INT_CARD_INT);
  38833. + if (intmask & SDHCI_INT_ERROR_MASK) {
  38834. + /* collect any uncovered errors */
  38835. + sdhci_writel(host, intmask & SDHCI_INT_ERROR_MASK,
  38836. + SDHCI_INT_STATUS);
  38837. + }
  38838. +
  38839. + intmask &= ~SDHCI_INT_ERROR_MASK;
  38840. +
  38841. if (intmask) {
  38842. unexpected |= intmask;
  38843. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  38844. @@ -2571,7 +2621,8 @@
  38845. {
  38846. int ret = 0;
  38847. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  38848. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  38849. + SDHCI_USE_PLATDMA)) {
  38850. if (host->ops->enable_dma)
  38851. host->ops->enable_dma(host);
  38852. }
  38853. @@ -2802,14 +2853,16 @@
  38854. host->flags &= ~SDHCI_USE_ADMA;
  38855. }
  38856. - if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  38857. + if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  38858. + SDHCI_USE_PLATDMA)) {
  38859. if (host->ops->enable_dma) {
  38860. if (host->ops->enable_dma(host)) {
  38861. pr_warning("%s: No suitable DMA "
  38862. "available. Falling back to PIO.\n",
  38863. mmc_hostname(mmc));
  38864. host->flags &=
  38865. - ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  38866. + ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA |
  38867. + SDHCI_USE_PLATDMA);
  38868. }
  38869. }
  38870. }
  38871. @@ -3249,6 +3302,7 @@
  38872. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  38873. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  38874. + (host->flags & SDHCI_USE_PLATDMA) ? "platform's DMA" :
  38875. (host->flags & SDHCI_USE_ADMA) ? "ADMA" :
  38876. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  38877. diff -Nur linux-3.16.2/drivers/mmc/host/sdhci.h linux-3.16-rpi/drivers/mmc/host/sdhci.h
  38878. --- linux-3.16.2/drivers/mmc/host/sdhci.h 2014-09-06 01:37:11.000000000 +0200
  38879. +++ linux-3.16-rpi/drivers/mmc/host/sdhci.h 2014-09-14 19:03:25.000000000 +0200
  38880. @@ -288,6 +288,17 @@
  38881. void (*reset)(struct sdhci_host *host, u8 mask);
  38882. int (*platform_execute_tuning)(struct sdhci_host *host, u32 opcode);
  38883. void (*set_uhs_signaling)(struct sdhci_host *host, unsigned int uhs);
  38884. +
  38885. + int (*pdma_able)(struct sdhci_host *host,
  38886. + struct mmc_data *data);
  38887. + void (*pdma_avail)(struct sdhci_host *host,
  38888. + unsigned int *ref_intmask,
  38889. + void(*complete)(struct sdhci_host *));
  38890. + void (*pdma_reset)(struct sdhci_host *host,
  38891. + struct mmc_data *data);
  38892. + unsigned int (*extra_ints)(struct sdhci_host *host);
  38893. + unsigned int (*missing_status)(struct sdhci_host *host);
  38894. +
  38895. void (*hw_reset)(struct sdhci_host *host);
  38896. void (*adma_workaround)(struct sdhci_host *host, u32 intmask);
  38897. void (*platform_init)(struct sdhci_host *host);
  38898. @@ -409,6 +420,29 @@
  38899. extern void sdhci_enable_irq_wakeups(struct sdhci_host *host);
  38900. #endif
  38901. +static inline int /*bool*/
  38902. +sdhci_platdma_dmaable(struct sdhci_host *host, struct mmc_data *data)
  38903. +{
  38904. + if (host->ops->pdma_able)
  38905. + return host->ops->pdma_able(host, data);
  38906. + else
  38907. + return 1;
  38908. +}
  38909. +static inline void
  38910. +sdhci_platdma_avail(struct sdhci_host *host, unsigned int *ref_intmask,
  38911. + void(*completion_callback)(struct sdhci_host *))
  38912. +{
  38913. + if (host->ops->pdma_avail)
  38914. + host->ops->pdma_avail(host, ref_intmask, completion_callback);
  38915. +}
  38916. +
  38917. +static inline void
  38918. +sdhci_platdma_reset(struct sdhci_host *host, struct mmc_data *data)
  38919. +{
  38920. + if (host->ops->pdma_reset)
  38921. + host->ops->pdma_reset(host, data);
  38922. +}
  38923. +
  38924. #ifdef CONFIG_PM_RUNTIME
  38925. extern int sdhci_runtime_suspend_host(struct sdhci_host *host);
  38926. extern int sdhci_runtime_resume_host(struct sdhci_host *host);
  38927. diff -Nur linux-3.16.2/drivers/net/usb/smsc95xx.c linux-3.16-rpi/drivers/net/usb/smsc95xx.c
  38928. --- linux-3.16.2/drivers/net/usb/smsc95xx.c 2014-09-06 01:37:11.000000000 +0200
  38929. +++ linux-3.16-rpi/drivers/net/usb/smsc95xx.c 2014-09-14 19:03:42.000000000 +0200
  38930. @@ -59,6 +59,7 @@
  38931. #define SUSPEND_SUSPEND3 (0x08)
  38932. #define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
  38933. SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
  38934. +#define MAC_ADDR_LEN (6)
  38935. struct smsc95xx_priv {
  38936. u32 mac_cr;
  38937. @@ -74,6 +75,10 @@
  38938. module_param(turbo_mode, bool, 0644);
  38939. MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
  38940. +static char *macaddr = ":";
  38941. +module_param(macaddr, charp, 0);
  38942. +MODULE_PARM_DESC(macaddr, "MAC address");
  38943. +
  38944. static int __must_check __smsc95xx_read_reg(struct usbnet *dev, u32 index,
  38945. u32 *data, int in_pm)
  38946. {
  38947. @@ -763,8 +768,59 @@
  38948. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  38949. }
  38950. +/* Check the macaddr module parameter for a MAC address */
  38951. +static int smsc95xx_is_macaddr_param(struct usbnet *dev, u8 *dev_mac)
  38952. +{
  38953. + int i, j, got_num, num;
  38954. + u8 mtbl[MAC_ADDR_LEN];
  38955. +
  38956. + if (macaddr[0] == ':')
  38957. + return 0;
  38958. +
  38959. + i = 0;
  38960. + j = 0;
  38961. + num = 0;
  38962. + got_num = 0;
  38963. + while (j < MAC_ADDR_LEN) {
  38964. + if (macaddr[i] && macaddr[i] != ':') {
  38965. + got_num++;
  38966. + if ('0' <= macaddr[i] && macaddr[i] <= '9')
  38967. + num = num * 16 + macaddr[i] - '0';
  38968. + else if ('A' <= macaddr[i] && macaddr[i] <= 'F')
  38969. + num = num * 16 + 10 + macaddr[i] - 'A';
  38970. + else if ('a' <= macaddr[i] && macaddr[i] <= 'f')
  38971. + num = num * 16 + 10 + macaddr[i] - 'a';
  38972. + else
  38973. + break;
  38974. + i++;
  38975. + } else if (got_num == 2) {
  38976. + mtbl[j++] = (u8) num;
  38977. + num = 0;
  38978. + got_num = 0;
  38979. + i++;
  38980. + } else {
  38981. + break;
  38982. + }
  38983. + }
  38984. +
  38985. + if (j == MAC_ADDR_LEN) {
  38986. + netif_dbg(dev, ifup, dev->net, "Overriding MAC address with: "
  38987. + "%02x:%02x:%02x:%02x:%02x:%02x\n", mtbl[0], mtbl[1], mtbl[2],
  38988. + mtbl[3], mtbl[4], mtbl[5]);
  38989. + for (i = 0; i < MAC_ADDR_LEN; i++)
  38990. + dev_mac[i] = mtbl[i];
  38991. + return 1;
  38992. + } else {
  38993. + return 0;
  38994. + }
  38995. +}
  38996. +
  38997. static void smsc95xx_init_mac_address(struct usbnet *dev)
  38998. {
  38999. + /* Check module parameters */
  39000. + if (smsc95xx_is_macaddr_param(dev, dev->net->dev_addr))
  39001. + return;
  39002. +
  39003. /* try reading mac address from EEPROM */
  39004. if (smsc95xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
  39005. dev->net->dev_addr) == 0) {
  39006. diff -Nur linux-3.16.2/drivers/pinctrl/Kconfig linux-3.16-rpi/drivers/pinctrl/Kconfig
  39007. --- linux-3.16.2/drivers/pinctrl/Kconfig 2014-09-06 01:37:11.000000000 +0200
  39008. +++ linux-3.16-rpi/drivers/pinctrl/Kconfig 2014-09-14 19:03:47.000000000 +0200
  39009. @@ -102,6 +102,11 @@
  39010. Requires ACPI device enumeration code to set up a platform device.
  39011. +config PINCTRL_BCM2708
  39012. + bool
  39013. + select PINMUX
  39014. + select PINCONF
  39015. +
  39016. config PINCTRL_BCM2835
  39017. bool
  39018. select PINMUX
  39019. diff -Nur linux-3.16.2/drivers/pinctrl/Makefile linux-3.16-rpi/drivers/pinctrl/Makefile
  39020. --- linux-3.16.2/drivers/pinctrl/Makefile 2014-09-06 01:37:11.000000000 +0200
  39021. +++ linux-3.16-rpi/drivers/pinctrl/Makefile 2014-09-14 19:03:47.000000000 +0200
  39022. @@ -19,6 +19,7 @@
  39023. obj-$(CONFIG_PINCTRL_BF54x) += pinctrl-adi2-bf54x.o
  39024. obj-$(CONFIG_PINCTRL_BF60x) += pinctrl-adi2-bf60x.o
  39025. obj-$(CONFIG_PINCTRL_AT91) += pinctrl-at91.o
  39026. +obj-$(CONFIG_PINCTRL_BCM2708) += pinctrl-bcm2708.o
  39027. obj-$(CONFIG_PINCTRL_BCM2835) += pinctrl-bcm2835.o
  39028. obj-$(CONFIG_PINCTRL_BAYTRAIL) += pinctrl-baytrail.o
  39029. obj-$(CONFIG_PINCTRL_BCM281XX) += pinctrl-bcm281xx.o
  39030. diff -Nur linux-3.16.2/drivers/pinctrl/pinctrl-bcm2708.c linux-3.16-rpi/drivers/pinctrl/pinctrl-bcm2708.c
  39031. --- linux-3.16.2/drivers/pinctrl/pinctrl-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  39032. +++ linux-3.16-rpi/drivers/pinctrl/pinctrl-bcm2708.c 2014-09-14 19:03:47.000000000 +0200
  39033. @@ -0,0 +1,773 @@
  39034. +/*
  39035. + * Driver for Broadcom BCM2708 GPIO unit (pinctrl only)
  39036. + *
  39037. + * Copyright (C) 2012 Chris Boot, Simon Arlott, Stephen Warren
  39038. + * Copyright (C) 2014 Noralf Tronnes
  39039. + *
  39040. + * This driver is a verbatim copy of the pinctrl-bcm2835 driver, except for:
  39041. + * - changed 2835 to 2708
  39042. + * - gpio_chip and IRQ part are removed
  39043. + * - Probing function is changed.
  39044. + *
  39045. + * Because armctrl sets up the gpio irqs, we use the bcm2708_gpio driver.
  39046. + * This hack is used to be able to support both DT and non-DT builds.
  39047. + * It's not possible to set trigger type and level flags for IRQs in the DT.
  39048. + *
  39049. + * This driver is inspired by:
  39050. + * pinctrl-nomadik.c, please see original file for copyright information
  39051. + * pinctrl-tegra.c, please see original file for copyright information
  39052. + *
  39053. + * This program is free software; you can redistribute it and/or modify
  39054. + * it under the terms of the GNU General Public License as published by
  39055. + * the Free Software Foundation; either version 2 of the License, or
  39056. + * (at your option) any later version.
  39057. + *
  39058. + * This program is distributed in the hope that it will be useful,
  39059. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39060. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39061. + * GNU General Public License for more details.
  39062. + */
  39063. +
  39064. +#include <linux/bitmap.h>
  39065. +#include <linux/bug.h>
  39066. +#include <linux/delay.h>
  39067. +#include <linux/device.h>
  39068. +#include <linux/err.h>
  39069. +#include <linux/gpio.h>
  39070. +#include <linux/interrupt.h>
  39071. +#include <linux/io.h>
  39072. +#include <linux/irq.h>
  39073. +#include <linux/irqdesc.h>
  39074. +#include <linux/irqdomain.h>
  39075. +#include <linux/module.h>
  39076. +#include <linux/of_address.h>
  39077. +#include <linux/of.h>
  39078. +#include <linux/of_gpio.h>
  39079. +#include <linux/of_irq.h>
  39080. +#include <linux/pinctrl/consumer.h>
  39081. +#include <linux/pinctrl/machine.h>
  39082. +#include <linux/pinctrl/pinconf.h>
  39083. +#include <linux/pinctrl/pinctrl.h>
  39084. +#include <linux/pinctrl/pinmux.h>
  39085. +#include <linux/platform_device.h>
  39086. +#include <linux/seq_file.h>
  39087. +#include <linux/slab.h>
  39088. +#include <linux/spinlock.h>
  39089. +#include <linux/types.h>
  39090. +
  39091. +#define MODULE_NAME "pinctrl-bcm2708"
  39092. +#define BCM2708_NUM_GPIOS 54
  39093. +#define BCM2708_NUM_BANKS 2
  39094. +
  39095. +#define BCM2708_PIN_BITMAP_SZ \
  39096. + DIV_ROUND_UP(BCM2708_NUM_GPIOS, sizeof(unsigned long) * 8)
  39097. +
  39098. +/* GPIO register offsets */
  39099. +#define GPFSEL0 0x0 /* Function Select */
  39100. +#define GPSET0 0x1c /* Pin Output Set */
  39101. +#define GPCLR0 0x28 /* Pin Output Clear */
  39102. +#define GPLEV0 0x34 /* Pin Level */
  39103. +#define GPEDS0 0x40 /* Pin Event Detect Status */
  39104. +#define GPREN0 0x4c /* Pin Rising Edge Detect Enable */
  39105. +#define GPFEN0 0x58 /* Pin Falling Edge Detect Enable */
  39106. +#define GPHEN0 0x64 /* Pin High Detect Enable */
  39107. +#define GPLEN0 0x70 /* Pin Low Detect Enable */
  39108. +#define GPAREN0 0x7c /* Pin Async Rising Edge Detect */
  39109. +#define GPAFEN0 0x88 /* Pin Async Falling Edge Detect */
  39110. +#define GPPUD 0x94 /* Pin Pull-up/down Enable */
  39111. +#define GPPUDCLK0 0x98 /* Pin Pull-up/down Enable Clock */
  39112. +
  39113. +#define FSEL_REG(p) (GPFSEL0 + (((p) / 10) * 4))
  39114. +#define FSEL_SHIFT(p) (((p) % 10) * 3)
  39115. +#define GPIO_REG_OFFSET(p) ((p) / 32)
  39116. +#define GPIO_REG_SHIFT(p) ((p) % 32)
  39117. +
  39118. +enum bcm2708_pinconf_param {
  39119. + /* argument: bcm2708_pinconf_pull */
  39120. + BCM2708_PINCONF_PARAM_PULL,
  39121. +};
  39122. +
  39123. +enum bcm2708_pinconf_pull {
  39124. + BCM2708_PINCONFIG_PULL_NONE,
  39125. + BCM2708_PINCONFIG_PULL_DOWN,
  39126. + BCM2708_PINCONFIG_PULL_UP,
  39127. +};
  39128. +
  39129. +#define BCM2708_PINCONF_PACK(_param_, _arg_) ((_param_) << 16 | (_arg_))
  39130. +#define BCM2708_PINCONF_UNPACK_PARAM(_conf_) ((_conf_) >> 16)
  39131. +#define BCM2708_PINCONF_UNPACK_ARG(_conf_) ((_conf_) & 0xffff)
  39132. +
  39133. +struct bcm2708_gpio_irqdata {
  39134. + struct bcm2708_pinctrl *pc;
  39135. + int bank;
  39136. +};
  39137. +
  39138. +struct bcm2708_pinctrl {
  39139. + struct device *dev;
  39140. + void __iomem *base;
  39141. + int irq[BCM2708_NUM_BANKS];
  39142. +
  39143. + /* note: locking assumes each bank will have its own unsigned long */
  39144. + unsigned long enabled_irq_map[BCM2708_NUM_BANKS];
  39145. + unsigned int irq_type[BCM2708_NUM_GPIOS];
  39146. +
  39147. + struct pinctrl_dev *pctl_dev;
  39148. + struct irq_domain *irq_domain;
  39149. + struct gpio_chip gpio_chip;
  39150. + struct pinctrl_gpio_range gpio_range;
  39151. +
  39152. + struct bcm2708_gpio_irqdata irq_data[BCM2708_NUM_BANKS];
  39153. + spinlock_t irq_lock[BCM2708_NUM_BANKS];
  39154. +};
  39155. +
  39156. +/* pins are just named GPIO0..GPIO53 */
  39157. +#define BCM2708_GPIO_PIN(a) PINCTRL_PIN(a, "gpio" #a)
  39158. +static struct pinctrl_pin_desc bcm2708_gpio_pins[] = {
  39159. + BCM2708_GPIO_PIN(0),
  39160. + BCM2708_GPIO_PIN(1),
  39161. + BCM2708_GPIO_PIN(2),
  39162. + BCM2708_GPIO_PIN(3),
  39163. + BCM2708_GPIO_PIN(4),
  39164. + BCM2708_GPIO_PIN(5),
  39165. + BCM2708_GPIO_PIN(6),
  39166. + BCM2708_GPIO_PIN(7),
  39167. + BCM2708_GPIO_PIN(8),
  39168. + BCM2708_GPIO_PIN(9),
  39169. + BCM2708_GPIO_PIN(10),
  39170. + BCM2708_GPIO_PIN(11),
  39171. + BCM2708_GPIO_PIN(12),
  39172. + BCM2708_GPIO_PIN(13),
  39173. + BCM2708_GPIO_PIN(14),
  39174. + BCM2708_GPIO_PIN(15),
  39175. + BCM2708_GPIO_PIN(16),
  39176. + BCM2708_GPIO_PIN(17),
  39177. + BCM2708_GPIO_PIN(18),
  39178. + BCM2708_GPIO_PIN(19),
  39179. + BCM2708_GPIO_PIN(20),
  39180. + BCM2708_GPIO_PIN(21),
  39181. + BCM2708_GPIO_PIN(22),
  39182. + BCM2708_GPIO_PIN(23),
  39183. + BCM2708_GPIO_PIN(24),
  39184. + BCM2708_GPIO_PIN(25),
  39185. + BCM2708_GPIO_PIN(26),
  39186. + BCM2708_GPIO_PIN(27),
  39187. + BCM2708_GPIO_PIN(28),
  39188. + BCM2708_GPIO_PIN(29),
  39189. + BCM2708_GPIO_PIN(30),
  39190. + BCM2708_GPIO_PIN(31),
  39191. + BCM2708_GPIO_PIN(32),
  39192. + BCM2708_GPIO_PIN(33),
  39193. + BCM2708_GPIO_PIN(34),
  39194. + BCM2708_GPIO_PIN(35),
  39195. + BCM2708_GPIO_PIN(36),
  39196. + BCM2708_GPIO_PIN(37),
  39197. + BCM2708_GPIO_PIN(38),
  39198. + BCM2708_GPIO_PIN(39),
  39199. + BCM2708_GPIO_PIN(40),
  39200. + BCM2708_GPIO_PIN(41),
  39201. + BCM2708_GPIO_PIN(42),
  39202. + BCM2708_GPIO_PIN(43),
  39203. + BCM2708_GPIO_PIN(44),
  39204. + BCM2708_GPIO_PIN(45),
  39205. + BCM2708_GPIO_PIN(46),
  39206. + BCM2708_GPIO_PIN(47),
  39207. + BCM2708_GPIO_PIN(48),
  39208. + BCM2708_GPIO_PIN(49),
  39209. + BCM2708_GPIO_PIN(50),
  39210. + BCM2708_GPIO_PIN(51),
  39211. + BCM2708_GPIO_PIN(52),
  39212. + BCM2708_GPIO_PIN(53),
  39213. +};
  39214. +
  39215. +/* one pin per group */
  39216. +static const char * const bcm2708_gpio_groups[] = {
  39217. + "gpio0",
  39218. + "gpio1",
  39219. + "gpio2",
  39220. + "gpio3",
  39221. + "gpio4",
  39222. + "gpio5",
  39223. + "gpio6",
  39224. + "gpio7",
  39225. + "gpio8",
  39226. + "gpio9",
  39227. + "gpio10",
  39228. + "gpio11",
  39229. + "gpio12",
  39230. + "gpio13",
  39231. + "gpio14",
  39232. + "gpio15",
  39233. + "gpio16",
  39234. + "gpio17",
  39235. + "gpio18",
  39236. + "gpio19",
  39237. + "gpio20",
  39238. + "gpio21",
  39239. + "gpio22",
  39240. + "gpio23",
  39241. + "gpio24",
  39242. + "gpio25",
  39243. + "gpio26",
  39244. + "gpio27",
  39245. + "gpio28",
  39246. + "gpio29",
  39247. + "gpio30",
  39248. + "gpio31",
  39249. + "gpio32",
  39250. + "gpio33",
  39251. + "gpio34",
  39252. + "gpio35",
  39253. + "gpio36",
  39254. + "gpio37",
  39255. + "gpio38",
  39256. + "gpio39",
  39257. + "gpio40",
  39258. + "gpio41",
  39259. + "gpio42",
  39260. + "gpio43",
  39261. + "gpio44",
  39262. + "gpio45",
  39263. + "gpio46",
  39264. + "gpio47",
  39265. + "gpio48",
  39266. + "gpio49",
  39267. + "gpio50",
  39268. + "gpio51",
  39269. + "gpio52",
  39270. + "gpio53",
  39271. +};
  39272. +
  39273. +enum bcm2708_fsel {
  39274. + BCM2708_FSEL_GPIO_IN = 0,
  39275. + BCM2708_FSEL_GPIO_OUT = 1,
  39276. + BCM2708_FSEL_ALT0 = 4,
  39277. + BCM2708_FSEL_ALT1 = 5,
  39278. + BCM2708_FSEL_ALT2 = 6,
  39279. + BCM2708_FSEL_ALT3 = 7,
  39280. + BCM2708_FSEL_ALT4 = 3,
  39281. + BCM2708_FSEL_ALT5 = 2,
  39282. + BCM2708_FSEL_COUNT = 8,
  39283. + BCM2708_FSEL_MASK = 0x7,
  39284. +};
  39285. +
  39286. +static const char * const bcm2708_functions[BCM2708_FSEL_COUNT] = {
  39287. + [BCM2708_FSEL_GPIO_IN] = "gpio_in",
  39288. + [BCM2708_FSEL_GPIO_OUT] = "gpio_out",
  39289. + [BCM2708_FSEL_ALT0] = "alt0",
  39290. + [BCM2708_FSEL_ALT1] = "alt1",
  39291. + [BCM2708_FSEL_ALT2] = "alt2",
  39292. + [BCM2708_FSEL_ALT3] = "alt3",
  39293. + [BCM2708_FSEL_ALT4] = "alt4",
  39294. + [BCM2708_FSEL_ALT5] = "alt5",
  39295. +};
  39296. +
  39297. +static const char * const irq_type_names[] = {
  39298. + [IRQ_TYPE_NONE] = "none",
  39299. + [IRQ_TYPE_EDGE_RISING] = "edge-rising",
  39300. + [IRQ_TYPE_EDGE_FALLING] = "edge-falling",
  39301. + [IRQ_TYPE_EDGE_BOTH] = "edge-both",
  39302. + [IRQ_TYPE_LEVEL_HIGH] = "level-high",
  39303. + [IRQ_TYPE_LEVEL_LOW] = "level-low",
  39304. +};
  39305. +
  39306. +static inline u32 bcm2708_gpio_rd(struct bcm2708_pinctrl *pc, unsigned reg)
  39307. +{
  39308. + return readl(pc->base + reg);
  39309. +}
  39310. +
  39311. +static inline void bcm2708_gpio_wr(struct bcm2708_pinctrl *pc, unsigned reg,
  39312. + u32 val)
  39313. +{
  39314. + writel(val, pc->base + reg);
  39315. +}
  39316. +
  39317. +static inline int bcm2708_gpio_get_bit(struct bcm2708_pinctrl *pc, unsigned reg,
  39318. + unsigned bit)
  39319. +{
  39320. + reg += GPIO_REG_OFFSET(bit) * 4;
  39321. + return (bcm2708_gpio_rd(pc, reg) >> GPIO_REG_SHIFT(bit)) & 1;
  39322. +}
  39323. +
  39324. +/* note NOT a read/modify/write cycle */
  39325. +static inline void bcm2708_gpio_set_bit(struct bcm2708_pinctrl *pc,
  39326. + unsigned reg, unsigned bit)
  39327. +{
  39328. + reg += GPIO_REG_OFFSET(bit) * 4;
  39329. + bcm2708_gpio_wr(pc, reg, BIT(GPIO_REG_SHIFT(bit)));
  39330. +}
  39331. +
  39332. +static inline enum bcm2708_fsel bcm2708_pinctrl_fsel_get(
  39333. + struct bcm2708_pinctrl *pc, unsigned pin)
  39334. +{
  39335. + u32 val = bcm2708_gpio_rd(pc, FSEL_REG(pin));
  39336. + enum bcm2708_fsel status = (val >> FSEL_SHIFT(pin)) & BCM2708_FSEL_MASK;
  39337. +
  39338. + dev_dbg(pc->dev, "get %08x (%u => %s)\n", val, pin,
  39339. + bcm2708_functions[status]);
  39340. +
  39341. + return status;
  39342. +}
  39343. +
  39344. +static inline void bcm2708_pinctrl_fsel_set(
  39345. + struct bcm2708_pinctrl *pc, unsigned pin,
  39346. + enum bcm2708_fsel fsel)
  39347. +{
  39348. + u32 val = bcm2708_gpio_rd(pc, FSEL_REG(pin));
  39349. + enum bcm2708_fsel cur = (val >> FSEL_SHIFT(pin)) & BCM2708_FSEL_MASK;
  39350. +
  39351. + dev_dbg(pc->dev, "read %08x (%u => %s)\n", val, pin,
  39352. + bcm2708_functions[cur]);
  39353. +
  39354. + if (cur == fsel)
  39355. + return;
  39356. +
  39357. + if (cur != BCM2708_FSEL_GPIO_IN && fsel != BCM2708_FSEL_GPIO_IN) {
  39358. + /* always transition through GPIO_IN */
  39359. + val &= ~(BCM2708_FSEL_MASK << FSEL_SHIFT(pin));
  39360. + val |= BCM2708_FSEL_GPIO_IN << FSEL_SHIFT(pin);
  39361. +
  39362. + dev_dbg(pc->dev, "trans %08x (%u <= %s)\n", val, pin,
  39363. + bcm2708_functions[BCM2708_FSEL_GPIO_IN]);
  39364. + bcm2708_gpio_wr(pc, FSEL_REG(pin), val);
  39365. + }
  39366. +
  39367. + val &= ~(BCM2708_FSEL_MASK << FSEL_SHIFT(pin));
  39368. + val |= fsel << FSEL_SHIFT(pin);
  39369. +
  39370. + dev_dbg(pc->dev, "write %08x (%u <= %s)\n", val, pin,
  39371. + bcm2708_functions[fsel]);
  39372. + bcm2708_gpio_wr(pc, FSEL_REG(pin), val);
  39373. +}
  39374. +
  39375. +static int bcm2708_pctl_get_groups_count(struct pinctrl_dev *pctldev)
  39376. +{
  39377. + return ARRAY_SIZE(bcm2708_gpio_groups);
  39378. +}
  39379. +
  39380. +static const char *bcm2708_pctl_get_group_name(struct pinctrl_dev *pctldev,
  39381. + unsigned selector)
  39382. +{
  39383. + return bcm2708_gpio_groups[selector];
  39384. +}
  39385. +
  39386. +static int bcm2708_pctl_get_group_pins(struct pinctrl_dev *pctldev,
  39387. + unsigned selector,
  39388. + const unsigned **pins,
  39389. + unsigned *num_pins)
  39390. +{
  39391. + *pins = &bcm2708_gpio_pins[selector].number;
  39392. + *num_pins = 1;
  39393. +
  39394. + return 0;
  39395. +}
  39396. +
  39397. +static void bcm2708_pctl_pin_dbg_show(struct pinctrl_dev *pctldev,
  39398. + struct seq_file *s,
  39399. + unsigned offset)
  39400. +{
  39401. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  39402. + enum bcm2708_fsel fsel = bcm2708_pinctrl_fsel_get(pc, offset);
  39403. + const char *fname = bcm2708_functions[fsel];
  39404. + int value = bcm2708_gpio_get_bit(pc, GPLEV0, offset);
  39405. + int irq = irq_find_mapping(pc->irq_domain, offset);
  39406. +
  39407. + seq_printf(s, "function %s in %s; irq %d (%s)",
  39408. + fname, value ? "hi" : "lo",
  39409. + irq, irq_type_names[pc->irq_type[offset]]);
  39410. +}
  39411. +
  39412. +static void bcm2708_pctl_dt_free_map(struct pinctrl_dev *pctldev,
  39413. + struct pinctrl_map *maps, unsigned num_maps)
  39414. +{
  39415. + int i;
  39416. +
  39417. + for (i = 0; i < num_maps; i++)
  39418. + if (maps[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
  39419. + kfree(maps[i].data.configs.configs);
  39420. +
  39421. + kfree(maps);
  39422. +}
  39423. +
  39424. +static int bcm2708_pctl_dt_node_to_map_func(struct bcm2708_pinctrl *pc,
  39425. + struct device_node *np, u32 pin, u32 fnum,
  39426. + struct pinctrl_map **maps)
  39427. +{
  39428. + struct pinctrl_map *map = *maps;
  39429. +
  39430. + if (fnum >= ARRAY_SIZE(bcm2708_functions)) {
  39431. + dev_err(pc->dev, "%s: invalid brcm,function %d\n",
  39432. + of_node_full_name(np), fnum);
  39433. + return -EINVAL;
  39434. + }
  39435. +
  39436. + map->type = PIN_MAP_TYPE_MUX_GROUP;
  39437. + map->data.mux.group = bcm2708_gpio_groups[pin];
  39438. + map->data.mux.function = bcm2708_functions[fnum];
  39439. + (*maps)++;
  39440. +
  39441. + return 0;
  39442. +}
  39443. +
  39444. +static int bcm2708_pctl_dt_node_to_map_pull(struct bcm2708_pinctrl *pc,
  39445. + struct device_node *np, u32 pin, u32 pull,
  39446. + struct pinctrl_map **maps)
  39447. +{
  39448. + struct pinctrl_map *map = *maps;
  39449. + unsigned long *configs;
  39450. +
  39451. + if (pull > 2) {
  39452. + dev_err(pc->dev, "%s: invalid brcm,pull %d\n",
  39453. + of_node_full_name(np), pull);
  39454. + return -EINVAL;
  39455. + }
  39456. +
  39457. + configs = kzalloc(sizeof(*configs), GFP_KERNEL);
  39458. + if (!configs)
  39459. + return -ENOMEM;
  39460. + configs[0] = BCM2708_PINCONF_PACK(BCM2708_PINCONF_PARAM_PULL, pull);
  39461. +
  39462. + map->type = PIN_MAP_TYPE_CONFIGS_PIN;
  39463. + map->data.configs.group_or_pin = bcm2708_gpio_pins[pin].name;
  39464. + map->data.configs.configs = configs;
  39465. + map->data.configs.num_configs = 1;
  39466. + (*maps)++;
  39467. +
  39468. + return 0;
  39469. +}
  39470. +
  39471. +static int bcm2708_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
  39472. + struct device_node *np,
  39473. + struct pinctrl_map **map, unsigned *num_maps)
  39474. +{
  39475. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  39476. + struct property *pins, *funcs, *pulls;
  39477. + int num_pins, num_funcs, num_pulls, maps_per_pin;
  39478. + struct pinctrl_map *maps, *cur_map;
  39479. + int i, err;
  39480. + u32 pin, func, pull;
  39481. +
  39482. + pins = of_find_property(np, "brcm,pins", NULL);
  39483. + if (!pins) {
  39484. + dev_err(pc->dev, "%s: missing brcm,pins property\n",
  39485. + of_node_full_name(np));
  39486. + return -EINVAL;
  39487. + }
  39488. +
  39489. + funcs = of_find_property(np, "brcm,function", NULL);
  39490. + pulls = of_find_property(np, "brcm,pull", NULL);
  39491. +
  39492. + if (!funcs && !pulls) {
  39493. + dev_err(pc->dev,
  39494. + "%s: neither brcm,function nor brcm,pull specified\n",
  39495. + of_node_full_name(np));
  39496. + return -EINVAL;
  39497. + }
  39498. +
  39499. + num_pins = pins->length / 4;
  39500. + num_funcs = funcs ? (funcs->length / 4) : 0;
  39501. + num_pulls = pulls ? (pulls->length / 4) : 0;
  39502. +
  39503. + if (num_funcs > 1 && num_funcs != num_pins) {
  39504. + dev_err(pc->dev,
  39505. + "%s: brcm,function must have 1 or %d entries\n",
  39506. + of_node_full_name(np), num_pins);
  39507. + return -EINVAL;
  39508. + }
  39509. +
  39510. + if (num_pulls > 1 && num_pulls != num_pins) {
  39511. + dev_err(pc->dev,
  39512. + "%s: brcm,pull must have 1 or %d entries\n",
  39513. + of_node_full_name(np), num_pins);
  39514. + return -EINVAL;
  39515. + }
  39516. +
  39517. + maps_per_pin = 0;
  39518. + if (num_funcs)
  39519. + maps_per_pin++;
  39520. + if (num_pulls)
  39521. + maps_per_pin++;
  39522. + cur_map = maps = kzalloc(num_pins * maps_per_pin * sizeof(*maps),
  39523. + GFP_KERNEL);
  39524. + if (!maps)
  39525. + return -ENOMEM;
  39526. +
  39527. + for (i = 0; i < num_pins; i++) {
  39528. + err = of_property_read_u32_index(np, "brcm,pins", i, &pin);
  39529. + if (err)
  39530. + goto out;
  39531. + if (pin >= ARRAY_SIZE(bcm2708_gpio_pins)) {
  39532. + dev_err(pc->dev, "%s: invalid brcm,pins value %d\n",
  39533. + of_node_full_name(np), pin);
  39534. + err = -EINVAL;
  39535. + goto out;
  39536. + }
  39537. +
  39538. + if (num_funcs) {
  39539. + err = of_property_read_u32_index(np, "brcm,function",
  39540. + (num_funcs > 1) ? i : 0, &func);
  39541. + if (err)
  39542. + goto out;
  39543. + err = bcm2708_pctl_dt_node_to_map_func(pc, np, pin,
  39544. + func, &cur_map);
  39545. + if (err)
  39546. + goto out;
  39547. + }
  39548. + if (num_pulls) {
  39549. + err = of_property_read_u32_index(np, "brcm,pull",
  39550. + (num_funcs > 1) ? i : 0, &pull);
  39551. + if (err)
  39552. + goto out;
  39553. + err = bcm2708_pctl_dt_node_to_map_pull(pc, np, pin,
  39554. + pull, &cur_map);
  39555. + if (err)
  39556. + goto out;
  39557. + }
  39558. + }
  39559. +
  39560. + *map = maps;
  39561. + *num_maps = num_pins * maps_per_pin;
  39562. +
  39563. + return 0;
  39564. +
  39565. +out:
  39566. + kfree(maps);
  39567. + return err;
  39568. +}
  39569. +
  39570. +static const struct pinctrl_ops bcm2708_pctl_ops = {
  39571. + .get_groups_count = bcm2708_pctl_get_groups_count,
  39572. + .get_group_name = bcm2708_pctl_get_group_name,
  39573. + .get_group_pins = bcm2708_pctl_get_group_pins,
  39574. + .pin_dbg_show = bcm2708_pctl_pin_dbg_show,
  39575. + .dt_node_to_map = bcm2708_pctl_dt_node_to_map,
  39576. + .dt_free_map = bcm2708_pctl_dt_free_map,
  39577. +};
  39578. +
  39579. +static int bcm2708_pmx_get_functions_count(struct pinctrl_dev *pctldev)
  39580. +{
  39581. + return BCM2708_FSEL_COUNT;
  39582. +}
  39583. +
  39584. +static const char *bcm2708_pmx_get_function_name(struct pinctrl_dev *pctldev,
  39585. + unsigned selector)
  39586. +{
  39587. + return bcm2708_functions[selector];
  39588. +}
  39589. +
  39590. +static int bcm2708_pmx_get_function_groups(struct pinctrl_dev *pctldev,
  39591. + unsigned selector,
  39592. + const char * const **groups,
  39593. + unsigned * const num_groups)
  39594. +{
  39595. + /* every pin can do every function */
  39596. + *groups = bcm2708_gpio_groups;
  39597. + *num_groups = ARRAY_SIZE(bcm2708_gpio_groups);
  39598. +
  39599. + return 0;
  39600. +}
  39601. +
  39602. +static int bcm2708_pmx_enable(struct pinctrl_dev *pctldev,
  39603. + unsigned func_selector,
  39604. + unsigned group_selector)
  39605. +{
  39606. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  39607. +
  39608. + bcm2708_pinctrl_fsel_set(pc, group_selector, func_selector);
  39609. +
  39610. + return 0;
  39611. +}
  39612. +
  39613. +static void bcm2708_pmx_disable(struct pinctrl_dev *pctldev,
  39614. + unsigned func_selector,
  39615. + unsigned group_selector)
  39616. +{
  39617. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  39618. +
  39619. + /* disable by setting to GPIO_IN */
  39620. + bcm2708_pinctrl_fsel_set(pc, group_selector, BCM2708_FSEL_GPIO_IN);
  39621. +}
  39622. +
  39623. +static void bcm2708_pmx_gpio_disable_free(struct pinctrl_dev *pctldev,
  39624. + struct pinctrl_gpio_range *range,
  39625. + unsigned offset)
  39626. +{
  39627. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  39628. +
  39629. + /* disable by setting to GPIO_IN */
  39630. + bcm2708_pinctrl_fsel_set(pc, offset, BCM2708_FSEL_GPIO_IN);
  39631. +}
  39632. +
  39633. +static int bcm2708_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
  39634. + struct pinctrl_gpio_range *range,
  39635. + unsigned offset,
  39636. + bool input)
  39637. +{
  39638. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  39639. + enum bcm2708_fsel fsel = input ?
  39640. + BCM2708_FSEL_GPIO_IN : BCM2708_FSEL_GPIO_OUT;
  39641. +
  39642. + bcm2708_pinctrl_fsel_set(pc, offset, fsel);
  39643. +
  39644. + return 0;
  39645. +}
  39646. +
  39647. +static const struct pinmux_ops bcm2708_pmx_ops = {
  39648. + .get_functions_count = bcm2708_pmx_get_functions_count,
  39649. + .get_function_name = bcm2708_pmx_get_function_name,
  39650. + .get_function_groups = bcm2708_pmx_get_function_groups,
  39651. + .enable = bcm2708_pmx_enable,
  39652. + .disable = bcm2708_pmx_disable,
  39653. + .gpio_disable_free = bcm2708_pmx_gpio_disable_free,
  39654. + .gpio_set_direction = bcm2708_pmx_gpio_set_direction,
  39655. +};
  39656. +
  39657. +static int bcm2708_pinconf_get(struct pinctrl_dev *pctldev,
  39658. + unsigned pin, unsigned long *config)
  39659. +{
  39660. + /* No way to read back config in HW */
  39661. + return -ENOTSUPP;
  39662. +}
  39663. +
  39664. +static int bcm2708_pinconf_set(struct pinctrl_dev *pctldev,
  39665. + unsigned pin, unsigned long *configs,
  39666. + unsigned num_configs)
  39667. +{
  39668. + struct bcm2708_pinctrl *pc = pinctrl_dev_get_drvdata(pctldev);
  39669. + enum bcm2708_pinconf_param param;
  39670. + u16 arg;
  39671. + u32 off, bit;
  39672. + int i;
  39673. +
  39674. + for (i = 0; i < num_configs; i++) {
  39675. + param = BCM2708_PINCONF_UNPACK_PARAM(configs[i]);
  39676. + arg = BCM2708_PINCONF_UNPACK_ARG(configs[i]);
  39677. +
  39678. + dev_dbg(pc->dev, "configure pin %u (%s) = %04X\n", pin, bcm2708_gpio_groups[pin], arg);
  39679. + if (param != BCM2708_PINCONF_PARAM_PULL)
  39680. + return -EINVAL;
  39681. +
  39682. + off = GPIO_REG_OFFSET(pin);
  39683. + bit = GPIO_REG_SHIFT(pin);
  39684. +
  39685. + bcm2708_gpio_wr(pc, GPPUD, arg & 3);
  39686. + /*
  39687. + * Docs say to wait 150 cycles, but not of what. We assume a
  39688. + * 1 MHz clock here, which is pretty slow...
  39689. + */
  39690. + udelay(150);
  39691. + bcm2708_gpio_wr(pc, GPPUDCLK0 + (off * 4), BIT(bit));
  39692. + udelay(150);
  39693. + bcm2708_gpio_wr(pc, GPPUDCLK0 + (off * 4), 0);
  39694. + } /* for each config */
  39695. +
  39696. + return 0;
  39697. +}
  39698. +
  39699. +static const struct pinconf_ops bcm2708_pinconf_ops = {
  39700. + .pin_config_get = bcm2708_pinconf_get,
  39701. + .pin_config_set = bcm2708_pinconf_set,
  39702. +};
  39703. +
  39704. +static struct pinctrl_desc bcm2708_pinctrl_desc = {
  39705. + .name = MODULE_NAME,
  39706. + .pins = bcm2708_gpio_pins,
  39707. + .npins = ARRAY_SIZE(bcm2708_gpio_pins),
  39708. + .pctlops = &bcm2708_pctl_ops,
  39709. + .pmxops = &bcm2708_pmx_ops,
  39710. + .confops = &bcm2708_pinconf_ops,
  39711. + .owner = THIS_MODULE,
  39712. +};
  39713. +
  39714. +static struct pinctrl_gpio_range bcm2708_pinctrl_gpio_range = {
  39715. + .name = MODULE_NAME,
  39716. + .npins = BCM2708_NUM_GPIOS,
  39717. +};
  39718. +
  39719. +/* bcm2708_gpio has base=0 */
  39720. +static int bcm2708_pinctrl_gpiochip_find(struct gpio_chip *gc, void *data)
  39721. +{
  39722. + pr_debug("%s: base = %d\n", __func__, gc->base);
  39723. + return gc->base == 0 ? 1 : 0;
  39724. +}
  39725. +
  39726. +static int bcm2708_pinctrl_probe(struct platform_device *pdev)
  39727. +{
  39728. + struct device *dev = &pdev->dev;
  39729. + struct device_node *np = dev->of_node;
  39730. + struct bcm2708_pinctrl *pc;
  39731. + struct gpio_chip *gc;
  39732. + struct resource iomem;
  39733. + int err;
  39734. + BUILD_BUG_ON(ARRAY_SIZE(bcm2708_gpio_pins) != BCM2708_NUM_GPIOS);
  39735. + BUILD_BUG_ON(ARRAY_SIZE(bcm2708_gpio_groups) != BCM2708_NUM_GPIOS);
  39736. +
  39737. + /* use gpio_chip registered by the bcm2708_gpio driver */
  39738. + gc = gpiochip_find(NULL, bcm2708_pinctrl_gpiochip_find);
  39739. + if (!gc)
  39740. + return -EPROBE_DEFER;
  39741. +
  39742. + gc->of_node = np;
  39743. + gc->of_gpio_n_cells = 2;
  39744. + gc->of_xlate = of_gpio_simple_xlate;
  39745. +
  39746. + pc = devm_kzalloc(dev, sizeof(*pc), GFP_KERNEL);
  39747. + if (!pc)
  39748. + return -ENOMEM;
  39749. +
  39750. + platform_set_drvdata(pdev, pc);
  39751. + pc->dev = dev;
  39752. +
  39753. + err = of_address_to_resource(np, 0, &iomem);
  39754. + if (err) {
  39755. + dev_err(dev, "could not get IO memory\n");
  39756. + return err;
  39757. + }
  39758. +
  39759. + pc->base = devm_ioremap_resource(dev, &iomem);
  39760. + if (IS_ERR(pc->base))
  39761. + return PTR_ERR(pc->base);
  39762. +
  39763. + pc->gpio_chip = *gc;
  39764. +
  39765. + pc->pctl_dev = pinctrl_register(&bcm2708_pinctrl_desc, dev, pc);
  39766. + if (!pc->pctl_dev)
  39767. + return -EINVAL;
  39768. +
  39769. + pc->gpio_range = bcm2708_pinctrl_gpio_range;
  39770. + pc->gpio_range.base = pc->gpio_chip.base;
  39771. + pc->gpio_range.gc = &pc->gpio_chip;
  39772. + pinctrl_add_gpio_range(pc->pctl_dev, &pc->gpio_range);
  39773. +
  39774. + return 0;
  39775. +}
  39776. +
  39777. +static int bcm2708_pinctrl_remove(struct platform_device *pdev)
  39778. +{
  39779. + struct bcm2708_pinctrl *pc = platform_get_drvdata(pdev);
  39780. +
  39781. + pinctrl_unregister(pc->pctl_dev);
  39782. + gpiochip_remove(&pc->gpio_chip);
  39783. +
  39784. + return 0;
  39785. +}
  39786. +
  39787. +static struct of_device_id bcm2708_pinctrl_match[] = {
  39788. + { .compatible = "brcm,bcm2708-pinctrl" },
  39789. + {}
  39790. +};
  39791. +MODULE_DEVICE_TABLE(of, bcm2708_pinctrl_match);
  39792. +
  39793. +static struct platform_driver bcm2708_pinctrl_driver = {
  39794. + .probe = bcm2708_pinctrl_probe,
  39795. + .remove = bcm2708_pinctrl_remove,
  39796. + .driver = {
  39797. + .name = MODULE_NAME,
  39798. + .owner = THIS_MODULE,
  39799. + .of_match_table = bcm2708_pinctrl_match,
  39800. + },
  39801. +};
  39802. +module_platform_driver(bcm2708_pinctrl_driver);
  39803. +
  39804. +MODULE_AUTHOR("Chris Boot, Simon Arlott, Stephen Warren, Noralf Tronnes");
  39805. +MODULE_DESCRIPTION("BCM2708 Pin control driver");
  39806. +MODULE_LICENSE("GPL");
  39807. diff -Nur linux-3.16.2/drivers/spi/Kconfig linux-3.16-rpi/drivers/spi/Kconfig
  39808. --- linux-3.16.2/drivers/spi/Kconfig 2014-09-06 01:37:11.000000000 +0200
  39809. +++ linux-3.16-rpi/drivers/spi/Kconfig 2014-09-14 19:03:53.000000000 +0200
  39810. @@ -76,7 +76,7 @@
  39811. config SPI_BCM2835
  39812. tristate "BCM2835 SPI controller"
  39813. - depends on ARCH_BCM2835 || COMPILE_TEST
  39814. + depends on ARCH_BCM2835 || ARCH_BCM2708 || COMPILE_TEST
  39815. help
  39816. This selects a driver for the Broadcom BCM2835 SPI master.
  39817. @@ -85,6 +85,14 @@
  39818. is for the regular SPI controller. Slave mode operation is not also
  39819. not supported.
  39820. +config SPI_BCM2708
  39821. + tristate "BCM2708 SPI controller driver (SPI0)"
  39822. + depends on MACH_BCM2708
  39823. + help
  39824. + This selects a driver for the Broadcom BCM2708 SPI master (SPI0). This
  39825. + driver is not compatible with the "Universal SPI Master" or the SPI slave
  39826. + device.
  39827. +
  39828. config SPI_BFIN5XX
  39829. tristate "SPI controller driver for ADI Blackfin5xx"
  39830. depends on BLACKFIN && !BF60x
  39831. diff -Nur linux-3.16.2/drivers/spi/Makefile linux-3.16-rpi/drivers/spi/Makefile
  39832. --- linux-3.16.2/drivers/spi/Makefile 2014-09-06 01:37:11.000000000 +0200
  39833. +++ linux-3.16-rpi/drivers/spi/Makefile 2014-09-14 19:03:53.000000000 +0200
  39834. @@ -19,6 +19,7 @@
  39835. obj-$(CONFIG_SPI_BCM63XX_HSSPI) += spi-bcm63xx-hsspi.o
  39836. obj-$(CONFIG_SPI_BFIN5XX) += spi-bfin5xx.o
  39837. obj-$(CONFIG_SPI_ADI_V3) += spi-adi-v3.o
  39838. +obj-$(CONFIG_SPI_BCM2708) += spi-bcm2708.o
  39839. obj-$(CONFIG_SPI_BFIN_SPORT) += spi-bfin-sport.o
  39840. obj-$(CONFIG_SPI_BITBANG) += spi-bitbang.o
  39841. obj-$(CONFIG_SPI_BUTTERFLY) += spi-butterfly.o
  39842. diff -Nur linux-3.16.2/drivers/spi/spi-bcm2708.c linux-3.16-rpi/drivers/spi/spi-bcm2708.c
  39843. --- linux-3.16.2/drivers/spi/spi-bcm2708.c 1970-01-01 01:00:00.000000000 +0100
  39844. +++ linux-3.16-rpi/drivers/spi/spi-bcm2708.c 2014-09-14 19:03:53.000000000 +0200
  39845. @@ -0,0 +1,635 @@
  39846. +/*
  39847. + * Driver for Broadcom BCM2708 SPI Controllers
  39848. + *
  39849. + * Copyright (C) 2012 Chris Boot
  39850. + *
  39851. + * This driver is inspired by:
  39852. + * spi-ath79.c, Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  39853. + * spi-atmel.c, Copyright (C) 2006 Atmel Corporation
  39854. + *
  39855. + * This program is free software; you can redistribute it and/or modify
  39856. + * it under the terms of the GNU General Public License as published by
  39857. + * the Free Software Foundation; either version 2 of the License, or
  39858. + * (at your option) any later version.
  39859. + *
  39860. + * This program is distributed in the hope that it will be useful,
  39861. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39862. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39863. + * GNU General Public License for more details.
  39864. + *
  39865. + * You should have received a copy of the GNU General Public License
  39866. + * along with this program; if not, write to the Free Software
  39867. + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  39868. + */
  39869. +
  39870. +#include <linux/kernel.h>
  39871. +#include <linux/module.h>
  39872. +#include <linux/spinlock.h>
  39873. +#include <linux/clk.h>
  39874. +#include <linux/err.h>
  39875. +#include <linux/platform_device.h>
  39876. +#include <linux/io.h>
  39877. +#include <linux/spi/spi.h>
  39878. +#include <linux/interrupt.h>
  39879. +#include <linux/delay.h>
  39880. +#include <linux/log2.h>
  39881. +#include <linux/sched.h>
  39882. +#include <linux/wait.h>
  39883. +
  39884. +/* SPI register offsets */
  39885. +#define SPI_CS 0x00
  39886. +#define SPI_FIFO 0x04
  39887. +#define SPI_CLK 0x08
  39888. +#define SPI_DLEN 0x0c
  39889. +#define SPI_LTOH 0x10
  39890. +#define SPI_DC 0x14
  39891. +
  39892. +/* Bitfields in CS */
  39893. +#define SPI_CS_LEN_LONG 0x02000000
  39894. +#define SPI_CS_DMA_LEN 0x01000000
  39895. +#define SPI_CS_CSPOL2 0x00800000
  39896. +#define SPI_CS_CSPOL1 0x00400000
  39897. +#define SPI_CS_CSPOL0 0x00200000
  39898. +#define SPI_CS_RXF 0x00100000
  39899. +#define SPI_CS_RXR 0x00080000
  39900. +#define SPI_CS_TXD 0x00040000
  39901. +#define SPI_CS_RXD 0x00020000
  39902. +#define SPI_CS_DONE 0x00010000
  39903. +#define SPI_CS_LEN 0x00002000
  39904. +#define SPI_CS_REN 0x00001000
  39905. +#define SPI_CS_ADCS 0x00000800
  39906. +#define SPI_CS_INTR 0x00000400
  39907. +#define SPI_CS_INTD 0x00000200
  39908. +#define SPI_CS_DMAEN 0x00000100
  39909. +#define SPI_CS_TA 0x00000080
  39910. +#define SPI_CS_CSPOL 0x00000040
  39911. +#define SPI_CS_CLEAR_RX 0x00000020
  39912. +#define SPI_CS_CLEAR_TX 0x00000010
  39913. +#define SPI_CS_CPOL 0x00000008
  39914. +#define SPI_CS_CPHA 0x00000004
  39915. +#define SPI_CS_CS_10 0x00000002
  39916. +#define SPI_CS_CS_01 0x00000001
  39917. +
  39918. +#define SPI_TIMEOUT_MS 150
  39919. +
  39920. +#define DRV_NAME "bcm2708_spi"
  39921. +
  39922. +struct bcm2708_spi {
  39923. + spinlock_t lock;
  39924. + void __iomem *base;
  39925. + int irq;
  39926. + struct clk *clk;
  39927. + bool stopping;
  39928. +
  39929. + struct list_head queue;
  39930. + struct workqueue_struct *workq;
  39931. + struct work_struct work;
  39932. + struct completion done;
  39933. +
  39934. + const u8 *tx_buf;
  39935. + u8 *rx_buf;
  39936. + int len;
  39937. +};
  39938. +
  39939. +struct bcm2708_spi_state {
  39940. + u32 cs;
  39941. + u16 cdiv;
  39942. +};
  39943. +
  39944. +/*
  39945. + * This function sets the ALT mode on the SPI pins so that we can use them with
  39946. + * the SPI hardware.
  39947. + *
  39948. + * FIXME: This is a hack. Use pinmux / pinctrl.
  39949. + */
  39950. +static void bcm2708_init_pinmode(void)
  39951. +{
  39952. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  39953. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  39954. +
  39955. + int pin;
  39956. + u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
  39957. +
  39958. + /* SPI is on GPIO 7..11 */
  39959. + for (pin = 7; pin <= 11; pin++) {
  39960. + INP_GPIO(pin); /* set mode to GPIO input first */
  39961. + SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
  39962. + }
  39963. +
  39964. + iounmap(gpio);
  39965. +
  39966. +#undef INP_GPIO
  39967. +#undef SET_GPIO_ALT
  39968. +}
  39969. +
  39970. +static inline u32 bcm2708_rd(struct bcm2708_spi *bs, unsigned reg)
  39971. +{
  39972. + return readl(bs->base + reg);
  39973. +}
  39974. +
  39975. +static inline void bcm2708_wr(struct bcm2708_spi *bs, unsigned reg, u32 val)
  39976. +{
  39977. + writel(val, bs->base + reg);
  39978. +}
  39979. +
  39980. +static inline void bcm2708_rd_fifo(struct bcm2708_spi *bs, int len)
  39981. +{
  39982. + u8 byte;
  39983. +
  39984. + while (len--) {
  39985. + byte = bcm2708_rd(bs, SPI_FIFO);
  39986. + if (bs->rx_buf)
  39987. + *bs->rx_buf++ = byte;
  39988. + }
  39989. +}
  39990. +
  39991. +static inline void bcm2708_wr_fifo(struct bcm2708_spi *bs, int len)
  39992. +{
  39993. + u8 byte;
  39994. + u16 val;
  39995. +
  39996. + if (len > bs->len)
  39997. + len = bs->len;
  39998. +
  39999. + if (unlikely(bcm2708_rd(bs, SPI_CS) & SPI_CS_LEN)) {
  40000. + /* LoSSI mode */
  40001. + if (unlikely(len % 2)) {
  40002. + printk(KERN_ERR"bcm2708_wr_fifo: length must be even, skipping.\n");
  40003. + bs->len = 0;
  40004. + return;
  40005. + }
  40006. + while (len) {
  40007. + if (bs->tx_buf) {
  40008. + val = *(const u16 *)bs->tx_buf;
  40009. + bs->tx_buf += 2;
  40010. + } else
  40011. + val = 0;
  40012. + bcm2708_wr(bs, SPI_FIFO, val);
  40013. + bs->len -= 2;
  40014. + len -= 2;
  40015. + }
  40016. + return;
  40017. + }
  40018. +
  40019. + while (len--) {
  40020. + byte = bs->tx_buf ? *bs->tx_buf++ : 0;
  40021. + bcm2708_wr(bs, SPI_FIFO, byte);
  40022. + bs->len--;
  40023. + }
  40024. +}
  40025. +
  40026. +static irqreturn_t bcm2708_spi_interrupt(int irq, void *dev_id)
  40027. +{
  40028. + struct spi_master *master = dev_id;
  40029. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  40030. + u32 cs;
  40031. +
  40032. + spin_lock(&bs->lock);
  40033. +
  40034. + cs = bcm2708_rd(bs, SPI_CS);
  40035. +
  40036. + if (cs & SPI_CS_DONE) {
  40037. + if (bs->len) { /* first interrupt in a transfer */
  40038. + /* fill the TX fifo with up to 16 bytes */
  40039. + bcm2708_wr_fifo(bs, 16);
  40040. + } else { /* transfer complete */
  40041. + /* disable interrupts */
  40042. + cs &= ~(SPI_CS_INTR | SPI_CS_INTD);
  40043. + bcm2708_wr(bs, SPI_CS, cs);
  40044. +
  40045. + /* drain RX FIFO */
  40046. + while (cs & SPI_CS_RXD) {
  40047. + bcm2708_rd_fifo(bs, 1);
  40048. + cs = bcm2708_rd(bs, SPI_CS);
  40049. + }
  40050. +
  40051. + /* wake up our bh */
  40052. + complete(&bs->done);
  40053. + }
  40054. + } else if (cs & SPI_CS_RXR) {
  40055. + /* read 12 bytes of data */
  40056. + bcm2708_rd_fifo(bs, 12);
  40057. +
  40058. + /* write up to 12 bytes */
  40059. + bcm2708_wr_fifo(bs, 12);
  40060. + }
  40061. +
  40062. + spin_unlock(&bs->lock);
  40063. +
  40064. + return IRQ_HANDLED;
  40065. +}
  40066. +
  40067. +static int bcm2708_setup_state(struct spi_master *master,
  40068. + struct device *dev, struct bcm2708_spi_state *state,
  40069. + u32 hz, u8 csel, u8 mode, u8 bpw)
  40070. +{
  40071. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  40072. + int cdiv;
  40073. + unsigned long bus_hz;
  40074. + u32 cs = 0;
  40075. +
  40076. + bus_hz = clk_get_rate(bs->clk);
  40077. +
  40078. + if (hz >= bus_hz) {
  40079. + cdiv = 2; /* bus_hz / 2 is as fast as we can go */
  40080. + } else if (hz) {
  40081. + cdiv = DIV_ROUND_UP(bus_hz, hz);
  40082. +
  40083. + /* CDIV must be a power of 2, so round up */
  40084. + cdiv = roundup_pow_of_two(cdiv);
  40085. +
  40086. + if (cdiv > 65536) {
  40087. + dev_dbg(dev,
  40088. + "setup: %d Hz too slow, cdiv %u; min %ld Hz\n",
  40089. + hz, cdiv, bus_hz / 65536);
  40090. + return -EINVAL;
  40091. + } else if (cdiv == 65536) {
  40092. + cdiv = 0;
  40093. + } else if (cdiv == 1) {
  40094. + cdiv = 2; /* 1 gets rounded down to 0; == 65536 */
  40095. + }
  40096. + } else {
  40097. + cdiv = 0;
  40098. + }
  40099. +
  40100. + switch (bpw) {
  40101. + case 8:
  40102. + break;
  40103. + case 9:
  40104. + /* Reading in LoSSI mode is a special case. See 'BCM2835 ARM Peripherals' datasheet */
  40105. + cs |= SPI_CS_LEN;
  40106. + break;
  40107. + default:
  40108. + dev_dbg(dev, "setup: invalid bits_per_word %u (must be 8 or 9)\n",
  40109. + bpw);
  40110. + return -EINVAL;
  40111. + }
  40112. +
  40113. + if (mode & SPI_CPOL)
  40114. + cs |= SPI_CS_CPOL;
  40115. + if (mode & SPI_CPHA)
  40116. + cs |= SPI_CS_CPHA;
  40117. +
  40118. + if (!(mode & SPI_NO_CS)) {
  40119. + if (mode & SPI_CS_HIGH) {
  40120. + cs |= SPI_CS_CSPOL;
  40121. + cs |= SPI_CS_CSPOL0 << csel;
  40122. + }
  40123. +
  40124. + cs |= csel;
  40125. + } else {
  40126. + cs |= SPI_CS_CS_10 | SPI_CS_CS_01;
  40127. + }
  40128. +
  40129. + if (state) {
  40130. + state->cs = cs;
  40131. + state->cdiv = cdiv;
  40132. + dev_dbg(dev, "setup: want %d Hz; "
  40133. + "bus_hz=%lu / cdiv=%u == %lu Hz; "
  40134. + "mode %u: cs 0x%08X\n",
  40135. + hz, bus_hz, cdiv, bus_hz/cdiv, mode, cs);
  40136. + }
  40137. +
  40138. + return 0;
  40139. +}
  40140. +
  40141. +static int bcm2708_process_transfer(struct bcm2708_spi *bs,
  40142. + struct spi_message *msg, struct spi_transfer *xfer)
  40143. +{
  40144. + struct spi_device *spi = msg->spi;
  40145. + struct bcm2708_spi_state state, *stp;
  40146. + int ret;
  40147. + u32 cs;
  40148. +
  40149. + if (bs->stopping)
  40150. + return -ESHUTDOWN;
  40151. +
  40152. + if (xfer->bits_per_word || xfer->speed_hz) {
  40153. + ret = bcm2708_setup_state(spi->master, &spi->dev, &state,
  40154. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  40155. + spi->chip_select, spi->mode,
  40156. + xfer->bits_per_word ? xfer->bits_per_word :
  40157. + spi->bits_per_word);
  40158. + if (ret)
  40159. + return ret;
  40160. +
  40161. + stp = &state;
  40162. + } else {
  40163. + stp = spi->controller_state;
  40164. + }
  40165. +
  40166. + reinit_completion(&bs->done);
  40167. + bs->tx_buf = xfer->tx_buf;
  40168. + bs->rx_buf = xfer->rx_buf;
  40169. + bs->len = xfer->len;
  40170. +
  40171. + cs = stp->cs | SPI_CS_INTR | SPI_CS_INTD | SPI_CS_TA;
  40172. +
  40173. + bcm2708_wr(bs, SPI_CLK, stp->cdiv);
  40174. + bcm2708_wr(bs, SPI_CS, cs);
  40175. +
  40176. + ret = wait_for_completion_timeout(&bs->done,
  40177. + msecs_to_jiffies(SPI_TIMEOUT_MS));
  40178. + if (ret == 0) {
  40179. + dev_err(&spi->dev, "transfer timed out\n");
  40180. + return -ETIMEDOUT;
  40181. + }
  40182. +
  40183. + if (xfer->delay_usecs)
  40184. + udelay(xfer->delay_usecs);
  40185. +
  40186. + if (list_is_last(&xfer->transfer_list, &msg->transfers) ||
  40187. + xfer->cs_change) {
  40188. + /* clear TA and interrupt flags */
  40189. + bcm2708_wr(bs, SPI_CS, stp->cs);
  40190. + }
  40191. +
  40192. + msg->actual_length += (xfer->len - bs->len);
  40193. +
  40194. + return 0;
  40195. +}
  40196. +
  40197. +static void bcm2708_work(struct work_struct *work)
  40198. +{
  40199. + struct bcm2708_spi *bs = container_of(work, struct bcm2708_spi, work);
  40200. + unsigned long flags;
  40201. + struct spi_message *msg;
  40202. + struct spi_transfer *xfer;
  40203. + int status = 0;
  40204. +
  40205. + spin_lock_irqsave(&bs->lock, flags);
  40206. + while (!list_empty(&bs->queue)) {
  40207. + msg = list_first_entry(&bs->queue, struct spi_message, queue);
  40208. + list_del_init(&msg->queue);
  40209. + spin_unlock_irqrestore(&bs->lock, flags);
  40210. +
  40211. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  40212. + status = bcm2708_process_transfer(bs, msg, xfer);
  40213. + if (status)
  40214. + break;
  40215. + }
  40216. +
  40217. + msg->status = status;
  40218. + msg->complete(msg->context);
  40219. +
  40220. + spin_lock_irqsave(&bs->lock, flags);
  40221. + }
  40222. + spin_unlock_irqrestore(&bs->lock, flags);
  40223. +}
  40224. +
  40225. +static int bcm2708_spi_setup(struct spi_device *spi)
  40226. +{
  40227. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  40228. + struct bcm2708_spi_state *state;
  40229. + int ret;
  40230. +
  40231. + if (bs->stopping)
  40232. + return -ESHUTDOWN;
  40233. +
  40234. + if (!(spi->mode & SPI_NO_CS) &&
  40235. + (spi->chip_select > spi->master->num_chipselect)) {
  40236. + dev_dbg(&spi->dev,
  40237. + "setup: invalid chipselect %u (%u defined)\n",
  40238. + spi->chip_select, spi->master->num_chipselect);
  40239. + return -EINVAL;
  40240. + }
  40241. +
  40242. + state = spi->controller_state;
  40243. + if (!state) {
  40244. + state = kzalloc(sizeof(*state), GFP_KERNEL);
  40245. + if (!state)
  40246. + return -ENOMEM;
  40247. +
  40248. + spi->controller_state = state;
  40249. + }
  40250. +
  40251. + ret = bcm2708_setup_state(spi->master, &spi->dev, state,
  40252. + spi->max_speed_hz, spi->chip_select, spi->mode,
  40253. + spi->bits_per_word);
  40254. + if (ret < 0) {
  40255. + kfree(state);
  40256. + spi->controller_state = NULL;
  40257. + return ret;
  40258. + }
  40259. +
  40260. + dev_dbg(&spi->dev,
  40261. + "setup: cd %d: %d Hz, bpw %u, mode 0x%x -> CS=%08x CDIV=%04x\n",
  40262. + spi->chip_select, spi->max_speed_hz, spi->bits_per_word,
  40263. + spi->mode, state->cs, state->cdiv);
  40264. +
  40265. + return 0;
  40266. +}
  40267. +
  40268. +static int bcm2708_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  40269. +{
  40270. + struct bcm2708_spi *bs = spi_master_get_devdata(spi->master);
  40271. + struct spi_transfer *xfer;
  40272. + int ret;
  40273. + unsigned long flags;
  40274. +
  40275. + if (unlikely(list_empty(&msg->transfers)))
  40276. + return -EINVAL;
  40277. +
  40278. + if (bs->stopping)
  40279. + return -ESHUTDOWN;
  40280. +
  40281. + list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  40282. + if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
  40283. + dev_dbg(&spi->dev, "missing rx or tx buf\n");
  40284. + return -EINVAL;
  40285. + }
  40286. +
  40287. + if (!xfer->bits_per_word || xfer->speed_hz)
  40288. + continue;
  40289. +
  40290. + ret = bcm2708_setup_state(spi->master, &spi->dev, NULL,
  40291. + xfer->speed_hz ? xfer->speed_hz : spi->max_speed_hz,
  40292. + spi->chip_select, spi->mode,
  40293. + xfer->bits_per_word ? xfer->bits_per_word :
  40294. + spi->bits_per_word);
  40295. + if (ret)
  40296. + return ret;
  40297. + }
  40298. +
  40299. + msg->status = -EINPROGRESS;
  40300. + msg->actual_length = 0;
  40301. +
  40302. + spin_lock_irqsave(&bs->lock, flags);
  40303. + list_add_tail(&msg->queue, &bs->queue);
  40304. + queue_work(bs->workq, &bs->work);
  40305. + spin_unlock_irqrestore(&bs->lock, flags);
  40306. +
  40307. + return 0;
  40308. +}
  40309. +
  40310. +static void bcm2708_spi_cleanup(struct spi_device *spi)
  40311. +{
  40312. + if (spi->controller_state) {
  40313. + kfree(spi->controller_state);
  40314. + spi->controller_state = NULL;
  40315. + }
  40316. +}
  40317. +
  40318. +static int bcm2708_spi_probe(struct platform_device *pdev)
  40319. +{
  40320. + struct resource *regs;
  40321. + int irq, err = -ENOMEM;
  40322. + struct clk *clk;
  40323. + struct spi_master *master;
  40324. + struct bcm2708_spi *bs;
  40325. +
  40326. + regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  40327. + if (!regs) {
  40328. + dev_err(&pdev->dev, "could not get IO memory\n");
  40329. + return -ENXIO;
  40330. + }
  40331. +
  40332. + irq = platform_get_irq(pdev, 0);
  40333. + if (irq < 0) {
  40334. + dev_err(&pdev->dev, "could not get IRQ\n");
  40335. + return irq;
  40336. + }
  40337. +
  40338. + clk = clk_get(&pdev->dev, NULL);
  40339. + if (IS_ERR(clk)) {
  40340. + dev_err(&pdev->dev, "could not find clk: %ld\n", PTR_ERR(clk));
  40341. + return PTR_ERR(clk);
  40342. + }
  40343. +
  40344. + bcm2708_init_pinmode();
  40345. +
  40346. + master = spi_alloc_master(&pdev->dev, sizeof(*bs));
  40347. + if (!master) {
  40348. + dev_err(&pdev->dev, "spi_alloc_master() failed\n");
  40349. + goto out_clk_put;
  40350. + }
  40351. +
  40352. + /* the spi->mode bits understood by this driver: */
  40353. + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
  40354. +
  40355. + master->bus_num = pdev->id;
  40356. + master->num_chipselect = 3;
  40357. + master->setup = bcm2708_spi_setup;
  40358. + master->transfer = bcm2708_spi_transfer;
  40359. + master->cleanup = bcm2708_spi_cleanup;
  40360. + master->dev.of_node = pdev->dev.of_node;
  40361. + platform_set_drvdata(pdev, master);
  40362. +
  40363. + bs = spi_master_get_devdata(master);
  40364. +
  40365. + spin_lock_init(&bs->lock);
  40366. + INIT_LIST_HEAD(&bs->queue);
  40367. + init_completion(&bs->done);
  40368. + INIT_WORK(&bs->work, bcm2708_work);
  40369. +
  40370. + bs->base = ioremap(regs->start, resource_size(regs));
  40371. + if (!bs->base) {
  40372. + dev_err(&pdev->dev, "could not remap memory\n");
  40373. + goto out_master_put;
  40374. + }
  40375. +
  40376. + bs->workq = create_singlethread_workqueue(dev_name(&pdev->dev));
  40377. + if (!bs->workq) {
  40378. + dev_err(&pdev->dev, "could not create workqueue\n");
  40379. + goto out_iounmap;
  40380. + }
  40381. +
  40382. + bs->irq = irq;
  40383. + bs->clk = clk;
  40384. + bs->stopping = false;
  40385. +
  40386. + err = request_irq(irq, bcm2708_spi_interrupt, 0, dev_name(&pdev->dev),
  40387. + master);
  40388. + if (err) {
  40389. + dev_err(&pdev->dev, "could not request IRQ: %d\n", err);
  40390. + goto out_workqueue;
  40391. + }
  40392. +
  40393. + /* initialise the hardware */
  40394. + clk_prepare_enable(clk);
  40395. + bcm2708_wr(bs, SPI_CS, SPI_CS_REN | SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  40396. +
  40397. + err = spi_register_master(master);
  40398. + if (err) {
  40399. + dev_err(&pdev->dev, "could not register SPI master: %d\n", err);
  40400. + goto out_free_irq;
  40401. + }
  40402. +
  40403. + dev_info(&pdev->dev, "SPI Controller at 0x%08lx (irq %d)\n",
  40404. + (unsigned long)regs->start, irq);
  40405. +
  40406. + return 0;
  40407. +
  40408. +out_free_irq:
  40409. + free_irq(bs->irq, master);
  40410. + clk_disable_unprepare(bs->clk);
  40411. +out_workqueue:
  40412. + destroy_workqueue(bs->workq);
  40413. +out_iounmap:
  40414. + iounmap(bs->base);
  40415. +out_master_put:
  40416. + spi_master_put(master);
  40417. +out_clk_put:
  40418. + clk_put(clk);
  40419. + return err;
  40420. +}
  40421. +
  40422. +static int bcm2708_spi_remove(struct platform_device *pdev)
  40423. +{
  40424. + struct spi_master *master = platform_get_drvdata(pdev);
  40425. + struct bcm2708_spi *bs = spi_master_get_devdata(master);
  40426. +
  40427. + /* reset the hardware and block queue progress */
  40428. + spin_lock_irq(&bs->lock);
  40429. + bs->stopping = true;
  40430. + bcm2708_wr(bs, SPI_CS, SPI_CS_CLEAR_RX | SPI_CS_CLEAR_TX);
  40431. + spin_unlock_irq(&bs->lock);
  40432. +
  40433. + flush_work(&bs->work);
  40434. +
  40435. + clk_disable_unprepare(bs->clk);
  40436. + clk_put(bs->clk);
  40437. + free_irq(bs->irq, master);
  40438. + iounmap(bs->base);
  40439. +
  40440. + spi_unregister_master(master);
  40441. +
  40442. + return 0;
  40443. +}
  40444. +
  40445. +static const struct of_device_id bcm2708_spi_match[] = {
  40446. + { .compatible = "brcm,bcm2708-spi", },
  40447. + {}
  40448. +};
  40449. +MODULE_DEVICE_TABLE(of, bcm2708_spi_match);
  40450. +
  40451. +static struct platform_driver bcm2708_spi_driver = {
  40452. + .driver = {
  40453. + .name = DRV_NAME,
  40454. + .owner = THIS_MODULE,
  40455. + .of_match_table = bcm2708_spi_match,
  40456. + },
  40457. + .probe = bcm2708_spi_probe,
  40458. + .remove = bcm2708_spi_remove,
  40459. +};
  40460. +
  40461. +
  40462. +static int __init bcm2708_spi_init(void)
  40463. +{
  40464. + return platform_driver_probe(&bcm2708_spi_driver, bcm2708_spi_probe);
  40465. +}
  40466. +module_init(bcm2708_spi_init);
  40467. +
  40468. +static void __exit bcm2708_spi_exit(void)
  40469. +{
  40470. + platform_driver_unregister(&bcm2708_spi_driver);
  40471. +}
  40472. +module_exit(bcm2708_spi_exit);
  40473. +
  40474. +
  40475. +//module_platform_driver(bcm2708_spi_driver);
  40476. +
  40477. +MODULE_DESCRIPTION("SPI controller driver for Broadcom BCM2708");
  40478. +MODULE_AUTHOR("Chris Boot <bootc@bootc.net>");
  40479. +MODULE_LICENSE("GPL v2");
  40480. +MODULE_ALIAS("platform:" DRV_NAME);
  40481. diff -Nur linux-3.16.2/drivers/staging/media/lirc/Kconfig linux-3.16-rpi/drivers/staging/media/lirc/Kconfig
  40482. --- linux-3.16.2/drivers/staging/media/lirc/Kconfig 2014-09-06 01:37:11.000000000 +0200
  40483. +++ linux-3.16-rpi/drivers/staging/media/lirc/Kconfig 2014-04-13 17:33:09.000000000 +0200
  40484. @@ -38,6 +38,12 @@
  40485. help
  40486. Driver for Homebrew Parallel Port Receivers
  40487. +config LIRC_RPI
  40488. + tristate "Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi"
  40489. + depends on LIRC
  40490. + help
  40491. + Driver for Homebrew GPIO Port Receiver/Transmitter for the RaspberryPi
  40492. +
  40493. config LIRC_SASEM
  40494. tristate "Sasem USB IR Remote"
  40495. depends on LIRC && USB
  40496. diff -Nur linux-3.16.2/drivers/staging/media/lirc/lirc_rpi.c linux-3.16-rpi/drivers/staging/media/lirc/lirc_rpi.c
  40497. --- linux-3.16.2/drivers/staging/media/lirc/lirc_rpi.c 1970-01-01 01:00:00.000000000 +0100
  40498. +++ linux-3.16-rpi/drivers/staging/media/lirc/lirc_rpi.c 2014-06-29 11:34:17.000000000 +0200
  40499. @@ -0,0 +1,695 @@
  40500. +/*
  40501. + * lirc_rpi.c
  40502. + *
  40503. + * lirc_rpi - Device driver that records pulse- and pause-lengths
  40504. + * (space-lengths) (just like the lirc_serial driver does)
  40505. + * between GPIO interrupt events on the Raspberry Pi.
  40506. + * Lots of code has been taken from the lirc_serial module,
  40507. + * so I would like say thanks to the authors.
  40508. + *
  40509. + * Copyright (C) 2012 Aron Robert Szabo <aron@reon.hu>,
  40510. + * Michael Bishop <cleverca22@gmail.com>
  40511. + * This program is free software; you can redistribute it and/or modify
  40512. + * it under the terms of the GNU General Public License as published by
  40513. + * the Free Software Foundation; either version 2 of the License, or
  40514. + * (at your option) any later version.
  40515. + *
  40516. + * This program is distributed in the hope that it will be useful,
  40517. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40518. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40519. + * GNU General Public License for more details.
  40520. + *
  40521. + * You should have received a copy of the GNU General Public License
  40522. + * along with this program; if not, write to the Free Software
  40523. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  40524. + */
  40525. +
  40526. +#include <linux/module.h>
  40527. +#include <linux/errno.h>
  40528. +#include <linux/interrupt.h>
  40529. +#include <linux/sched.h>
  40530. +#include <linux/kernel.h>
  40531. +#include <linux/time.h>
  40532. +#include <linux/timex.h>
  40533. +#include <linux/string.h>
  40534. +#include <linux/delay.h>
  40535. +#include <linux/platform_device.h>
  40536. +#include <linux/irq.h>
  40537. +#include <linux/spinlock.h>
  40538. +#include <media/lirc.h>
  40539. +#include <media/lirc_dev.h>
  40540. +#include <linux/gpio.h>
  40541. +
  40542. +#define LIRC_DRIVER_NAME "lirc_rpi"
  40543. +#define RBUF_LEN 256
  40544. +#define LIRC_TRANSMITTER_LATENCY 50
  40545. +
  40546. +#ifndef MAX_UDELAY_MS
  40547. +#define MAX_UDELAY_US 5000
  40548. +#else
  40549. +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
  40550. +#endif
  40551. +
  40552. +#define dprintk(fmt, args...) \
  40553. + do { \
  40554. + if (debug) \
  40555. + printk(KERN_DEBUG LIRC_DRIVER_NAME ": " \
  40556. + fmt, ## args); \
  40557. + } while (0)
  40558. +
  40559. +/* module parameters */
  40560. +
  40561. +/* set the default GPIO input pin */
  40562. +static int gpio_in_pin = 18;
  40563. +/* set the default GPIO output pin */
  40564. +static int gpio_out_pin = 17;
  40565. +/* enable debugging messages */
  40566. +static bool debug;
  40567. +/* -1 = auto, 0 = active high, 1 = active low */
  40568. +static int sense = -1;
  40569. +/* use softcarrier by default */
  40570. +static bool softcarrier = 1;
  40571. +/* 0 = do not invert output, 1 = invert output */
  40572. +static bool invert = 0;
  40573. +
  40574. +struct gpio_chip *gpiochip;
  40575. +struct irq_chip *irqchip;
  40576. +struct irq_data *irqdata;
  40577. +
  40578. +/* forward declarations */
  40579. +static long send_pulse(unsigned long length);
  40580. +static void send_space(long length);
  40581. +static void lirc_rpi_exit(void);
  40582. +
  40583. +int valid_gpio_pins[] = { 0, 1, 2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21,
  40584. + 22, 23, 24, 25 ,27, 28, 29, 30, 31 };
  40585. +
  40586. +static struct platform_device *lirc_rpi_dev;
  40587. +static struct timeval lasttv = { 0, 0 };
  40588. +static struct lirc_buffer rbuf;
  40589. +static spinlock_t lock;
  40590. +
  40591. +/* initialized/set in init_timing_params() */
  40592. +static unsigned int freq = 38000;
  40593. +static unsigned int duty_cycle = 50;
  40594. +static unsigned long period;
  40595. +static unsigned long pulse_width;
  40596. +static unsigned long space_width;
  40597. +
  40598. +static void safe_udelay(unsigned long usecs)
  40599. +{
  40600. + while (usecs > MAX_UDELAY_US) {
  40601. + udelay(MAX_UDELAY_US);
  40602. + usecs -= MAX_UDELAY_US;
  40603. + }
  40604. + udelay(usecs);
  40605. +}
  40606. +
  40607. +static int init_timing_params(unsigned int new_duty_cycle,
  40608. + unsigned int new_freq)
  40609. +{
  40610. + if (1000 * 1000000L / new_freq * new_duty_cycle / 100 <=
  40611. + LIRC_TRANSMITTER_LATENCY)
  40612. + return -EINVAL;
  40613. + if (1000 * 1000000L / new_freq * (100 - new_duty_cycle) / 100 <=
  40614. + LIRC_TRANSMITTER_LATENCY)
  40615. + return -EINVAL;
  40616. + duty_cycle = new_duty_cycle;
  40617. + freq = new_freq;
  40618. + period = 1000 * 1000000L / freq;
  40619. + pulse_width = period * duty_cycle / 100;
  40620. + space_width = period - pulse_width;
  40621. + dprintk("in init_timing_params, freq=%d pulse=%ld, "
  40622. + "space=%ld\n", freq, pulse_width, space_width);
  40623. + return 0;
  40624. +}
  40625. +
  40626. +static long send_pulse_softcarrier(unsigned long length)
  40627. +{
  40628. + int flag;
  40629. + unsigned long actual, target;
  40630. + unsigned long actual_us, initial_us, target_us;
  40631. +
  40632. + length *= 1000;
  40633. +
  40634. + actual = 0; target = 0; flag = 0;
  40635. + read_current_timer(&actual_us);
  40636. +
  40637. + while (actual < length) {
  40638. + if (flag) {
  40639. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  40640. + target += space_width;
  40641. + } else {
  40642. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  40643. + target += pulse_width;
  40644. + }
  40645. + initial_us = actual_us;
  40646. + target_us = actual_us + (target - actual) / 1000;
  40647. + /*
  40648. + * Note - we've checked in ioctl that the pulse/space
  40649. + * widths are big enough so that d is > 0
  40650. + */
  40651. + if ((int)(target_us - actual_us) > 0)
  40652. + udelay(target_us - actual_us);
  40653. + read_current_timer(&actual_us);
  40654. + actual += (actual_us - initial_us) * 1000;
  40655. + flag = !flag;
  40656. + }
  40657. + return (actual-length) / 1000;
  40658. +}
  40659. +
  40660. +static long send_pulse(unsigned long length)
  40661. +{
  40662. + if (length <= 0)
  40663. + return 0;
  40664. +
  40665. + if (softcarrier) {
  40666. + return send_pulse_softcarrier(length);
  40667. + } else {
  40668. + gpiochip->set(gpiochip, gpio_out_pin, !invert);
  40669. + safe_udelay(length);
  40670. + return 0;
  40671. + }
  40672. +}
  40673. +
  40674. +static void send_space(long length)
  40675. +{
  40676. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  40677. + if (length <= 0)
  40678. + return;
  40679. + safe_udelay(length);
  40680. +}
  40681. +
  40682. +static void rbwrite(int l)
  40683. +{
  40684. + if (lirc_buffer_full(&rbuf)) {
  40685. + /* no new signals will be accepted */
  40686. + dprintk("Buffer overrun\n");
  40687. + return;
  40688. + }
  40689. + lirc_buffer_write(&rbuf, (void *)&l);
  40690. +}
  40691. +
  40692. +static void frbwrite(int l)
  40693. +{
  40694. + /* simple noise filter */
  40695. + static int pulse, space;
  40696. + static unsigned int ptr;
  40697. +
  40698. + if (ptr > 0 && (l & PULSE_BIT)) {
  40699. + pulse += l & PULSE_MASK;
  40700. + if (pulse > 250) {
  40701. + rbwrite(space);
  40702. + rbwrite(pulse | PULSE_BIT);
  40703. + ptr = 0;
  40704. + pulse = 0;
  40705. + }
  40706. + return;
  40707. + }
  40708. + if (!(l & PULSE_BIT)) {
  40709. + if (ptr == 0) {
  40710. + if (l > 20000) {
  40711. + space = l;
  40712. + ptr++;
  40713. + return;
  40714. + }
  40715. + } else {
  40716. + if (l > 20000) {
  40717. + space += pulse;
  40718. + if (space > PULSE_MASK)
  40719. + space = PULSE_MASK;
  40720. + space += l;
  40721. + if (space > PULSE_MASK)
  40722. + space = PULSE_MASK;
  40723. + pulse = 0;
  40724. + return;
  40725. + }
  40726. + rbwrite(space);
  40727. + rbwrite(pulse | PULSE_BIT);
  40728. + ptr = 0;
  40729. + pulse = 0;
  40730. + }
  40731. + }
  40732. + rbwrite(l);
  40733. +}
  40734. +
  40735. +static irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
  40736. +{
  40737. + struct timeval tv;
  40738. + long deltv;
  40739. + int data;
  40740. + int signal;
  40741. +
  40742. + /* use the GPIO signal level */
  40743. + signal = gpiochip->get(gpiochip, gpio_in_pin);
  40744. +
  40745. + /* unmask the irq */
  40746. + irqchip->irq_unmask(irqdata);
  40747. +
  40748. + if (sense != -1) {
  40749. + /* get current time */
  40750. + do_gettimeofday(&tv);
  40751. +
  40752. + /* calc time since last interrupt in microseconds */
  40753. + deltv = tv.tv_sec-lasttv.tv_sec;
  40754. + if (tv.tv_sec < lasttv.tv_sec ||
  40755. + (tv.tv_sec == lasttv.tv_sec &&
  40756. + tv.tv_usec < lasttv.tv_usec)) {
  40757. + printk(KERN_WARNING LIRC_DRIVER_NAME
  40758. + ": AIEEEE: your clock just jumped backwards\n");
  40759. + printk(KERN_WARNING LIRC_DRIVER_NAME
  40760. + ": %d %d %lx %lx %lx %lx\n", signal, sense,
  40761. + tv.tv_sec, lasttv.tv_sec,
  40762. + tv.tv_usec, lasttv.tv_usec);
  40763. + data = PULSE_MASK;
  40764. + } else if (deltv > 15) {
  40765. + data = PULSE_MASK; /* really long time */
  40766. + if (!(signal^sense)) {
  40767. + /* sanity check */
  40768. + printk(KERN_WARNING LIRC_DRIVER_NAME
  40769. + ": AIEEEE: %d %d %lx %lx %lx %lx\n",
  40770. + signal, sense, tv.tv_sec, lasttv.tv_sec,
  40771. + tv.tv_usec, lasttv.tv_usec);
  40772. + /*
  40773. + * detecting pulse while this
  40774. + * MUST be a space!
  40775. + */
  40776. + sense = sense ? 0 : 1;
  40777. + }
  40778. + } else {
  40779. + data = (int) (deltv*1000000 +
  40780. + (tv.tv_usec - lasttv.tv_usec));
  40781. + }
  40782. + frbwrite(signal^sense ? data : (data|PULSE_BIT));
  40783. + lasttv = tv;
  40784. + wake_up_interruptible(&rbuf.wait_poll);
  40785. + }
  40786. +
  40787. + return IRQ_HANDLED;
  40788. +}
  40789. +
  40790. +static int is_right_chip(struct gpio_chip *chip, void *data)
  40791. +{
  40792. + dprintk("is_right_chip %s %d\n", chip->label, strcmp(data, chip->label));
  40793. +
  40794. + if (strcmp(data, chip->label) == 0)
  40795. + return 1;
  40796. + return 0;
  40797. +}
  40798. +
  40799. +static int init_port(void)
  40800. +{
  40801. + int i, nlow, nhigh, ret, irq;
  40802. +
  40803. + gpiochip = gpiochip_find("bcm2708_gpio", is_right_chip);
  40804. +
  40805. + if (!gpiochip)
  40806. + return -ENODEV;
  40807. +
  40808. + if (gpio_request(gpio_out_pin, LIRC_DRIVER_NAME " ir/out")) {
  40809. + printk(KERN_ALERT LIRC_DRIVER_NAME
  40810. + ": cant claim gpio pin %d\n", gpio_out_pin);
  40811. + ret = -ENODEV;
  40812. + goto exit_init_port;
  40813. + }
  40814. +
  40815. + if (gpio_request(gpio_in_pin, LIRC_DRIVER_NAME " ir/in")) {
  40816. + printk(KERN_ALERT LIRC_DRIVER_NAME
  40817. + ": cant claim gpio pin %d\n", gpio_in_pin);
  40818. + ret = -ENODEV;
  40819. + goto exit_gpio_free_out_pin;
  40820. + }
  40821. +
  40822. + gpiochip->direction_input(gpiochip, gpio_in_pin);
  40823. + gpiochip->direction_output(gpiochip, gpio_out_pin, 1);
  40824. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  40825. +
  40826. + irq = gpiochip->to_irq(gpiochip, gpio_in_pin);
  40827. + dprintk("to_irq %d\n", irq);
  40828. + irqdata = irq_get_irq_data(irq);
  40829. +
  40830. + if (irqdata && irqdata->chip) {
  40831. + irqchip = irqdata->chip;
  40832. + } else {
  40833. + ret = -ENODEV;
  40834. + goto exit_gpio_free_in_pin;
  40835. + }
  40836. +
  40837. + /* if pin is high, then this must be an active low receiver. */
  40838. + if (sense == -1) {
  40839. + /* wait 1/2 sec for the power supply */
  40840. + msleep(500);
  40841. +
  40842. + /*
  40843. + * probe 9 times every 0.04s, collect "votes" for
  40844. + * active high/low
  40845. + */
  40846. + nlow = 0;
  40847. + nhigh = 0;
  40848. + for (i = 0; i < 9; i++) {
  40849. + if (gpiochip->get(gpiochip, gpio_in_pin))
  40850. + nlow++;
  40851. + else
  40852. + nhigh++;
  40853. + msleep(40);
  40854. + }
  40855. + sense = (nlow >= nhigh ? 1 : 0);
  40856. + printk(KERN_INFO LIRC_DRIVER_NAME
  40857. + ": auto-detected active %s receiver on GPIO pin %d\n",
  40858. + sense ? "low" : "high", gpio_in_pin);
  40859. + } else {
  40860. + printk(KERN_INFO LIRC_DRIVER_NAME
  40861. + ": manually using active %s receiver on GPIO pin %d\n",
  40862. + sense ? "low" : "high", gpio_in_pin);
  40863. + }
  40864. +
  40865. + return 0;
  40866. +
  40867. + exit_gpio_free_in_pin:
  40868. + gpio_free(gpio_in_pin);
  40869. +
  40870. + exit_gpio_free_out_pin:
  40871. + gpio_free(gpio_out_pin);
  40872. +
  40873. + exit_init_port:
  40874. + return ret;
  40875. +}
  40876. +
  40877. +// called when the character device is opened
  40878. +static int set_use_inc(void *data)
  40879. +{
  40880. + int result;
  40881. + unsigned long flags;
  40882. +
  40883. + /* initialize timestamp */
  40884. + do_gettimeofday(&lasttv);
  40885. +
  40886. + result = request_irq(gpiochip->to_irq(gpiochip, gpio_in_pin),
  40887. + (irq_handler_t) irq_handler, 0,
  40888. + LIRC_DRIVER_NAME, (void*) 0);
  40889. +
  40890. + switch (result) {
  40891. + case -EBUSY:
  40892. + printk(KERN_ERR LIRC_DRIVER_NAME
  40893. + ": IRQ %d is busy\n",
  40894. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  40895. + return -EBUSY;
  40896. + case -EINVAL:
  40897. + printk(KERN_ERR LIRC_DRIVER_NAME
  40898. + ": Bad irq number or handler\n");
  40899. + return -EINVAL;
  40900. + default:
  40901. + dprintk("Interrupt %d obtained\n",
  40902. + gpiochip->to_irq(gpiochip, gpio_in_pin));
  40903. + break;
  40904. + };
  40905. +
  40906. + /* initialize pulse/space widths */
  40907. + init_timing_params(duty_cycle, freq);
  40908. +
  40909. + spin_lock_irqsave(&lock, flags);
  40910. +
  40911. + /* GPIO Pin Falling/Rising Edge Detect Enable */
  40912. + irqchip->irq_set_type(irqdata,
  40913. + IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING);
  40914. +
  40915. + /* unmask the irq */
  40916. + irqchip->irq_unmask(irqdata);
  40917. +
  40918. + spin_unlock_irqrestore(&lock, flags);
  40919. +
  40920. + return 0;
  40921. +}
  40922. +
  40923. +static void set_use_dec(void *data)
  40924. +{
  40925. + unsigned long flags;
  40926. +
  40927. + spin_lock_irqsave(&lock, flags);
  40928. +
  40929. + /* GPIO Pin Falling/Rising Edge Detect Disable */
  40930. + irqchip->irq_set_type(irqdata, 0);
  40931. + irqchip->irq_mask(irqdata);
  40932. +
  40933. + spin_unlock_irqrestore(&lock, flags);
  40934. +
  40935. + free_irq(gpiochip->to_irq(gpiochip, gpio_in_pin), (void *) 0);
  40936. +
  40937. + dprintk(KERN_INFO LIRC_DRIVER_NAME
  40938. + ": freed IRQ %d\n", gpiochip->to_irq(gpiochip, gpio_in_pin));
  40939. +}
  40940. +
  40941. +static ssize_t lirc_write(struct file *file, const char *buf,
  40942. + size_t n, loff_t *ppos)
  40943. +{
  40944. + int i, count;
  40945. + unsigned long flags;
  40946. + long delta = 0;
  40947. + int *wbuf;
  40948. +
  40949. + count = n / sizeof(int);
  40950. + if (n % sizeof(int) || count % 2 == 0)
  40951. + return -EINVAL;
  40952. + wbuf = memdup_user(buf, n);
  40953. + if (IS_ERR(wbuf))
  40954. + return PTR_ERR(wbuf);
  40955. + spin_lock_irqsave(&lock, flags);
  40956. +
  40957. + for (i = 0; i < count; i++) {
  40958. + if (i%2)
  40959. + send_space(wbuf[i] - delta);
  40960. + else
  40961. + delta = send_pulse(wbuf[i]);
  40962. + }
  40963. + gpiochip->set(gpiochip, gpio_out_pin, invert);
  40964. +
  40965. + spin_unlock_irqrestore(&lock, flags);
  40966. + kfree(wbuf);
  40967. + return n;
  40968. +}
  40969. +
  40970. +static long lirc_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
  40971. +{
  40972. + int result;
  40973. + __u32 value;
  40974. +
  40975. + switch (cmd) {
  40976. + case LIRC_GET_SEND_MODE:
  40977. + return -ENOIOCTLCMD;
  40978. + break;
  40979. +
  40980. + case LIRC_SET_SEND_MODE:
  40981. + result = get_user(value, (__u32 *) arg);
  40982. + if (result)
  40983. + return result;
  40984. + /* only LIRC_MODE_PULSE supported */
  40985. + if (value != LIRC_MODE_PULSE)
  40986. + return -ENOSYS;
  40987. + break;
  40988. +
  40989. + case LIRC_GET_LENGTH:
  40990. + return -ENOSYS;
  40991. + break;
  40992. +
  40993. + case LIRC_SET_SEND_DUTY_CYCLE:
  40994. + dprintk("SET_SEND_DUTY_CYCLE\n");
  40995. + result = get_user(value, (__u32 *) arg);
  40996. + if (result)
  40997. + return result;
  40998. + if (value <= 0 || value > 100)
  40999. + return -EINVAL;
  41000. + return init_timing_params(value, freq);
  41001. + break;
  41002. +
  41003. + case LIRC_SET_SEND_CARRIER:
  41004. + dprintk("SET_SEND_CARRIER\n");
  41005. + result = get_user(value, (__u32 *) arg);
  41006. + if (result)
  41007. + return result;
  41008. + if (value > 500000 || value < 20000)
  41009. + return -EINVAL;
  41010. + return init_timing_params(duty_cycle, value);
  41011. + break;
  41012. +
  41013. + default:
  41014. + return lirc_dev_fop_ioctl(filep, cmd, arg);
  41015. + }
  41016. + return 0;
  41017. +}
  41018. +
  41019. +static const struct file_operations lirc_fops = {
  41020. + .owner = THIS_MODULE,
  41021. + .write = lirc_write,
  41022. + .unlocked_ioctl = lirc_ioctl,
  41023. + .read = lirc_dev_fop_read,
  41024. + .poll = lirc_dev_fop_poll,
  41025. + .open = lirc_dev_fop_open,
  41026. + .release = lirc_dev_fop_close,
  41027. + .llseek = no_llseek,
  41028. +};
  41029. +
  41030. +static struct lirc_driver driver = {
  41031. + .name = LIRC_DRIVER_NAME,
  41032. + .minor = -1,
  41033. + .code_length = 1,
  41034. + .sample_rate = 0,
  41035. + .data = NULL,
  41036. + .add_to_buf = NULL,
  41037. + .rbuf = &rbuf,
  41038. + .set_use_inc = set_use_inc,
  41039. + .set_use_dec = set_use_dec,
  41040. + .fops = &lirc_fops,
  41041. + .dev = NULL,
  41042. + .owner = THIS_MODULE,
  41043. +};
  41044. +
  41045. +static struct platform_driver lirc_rpi_driver = {
  41046. + .driver = {
  41047. + .name = LIRC_DRIVER_NAME,
  41048. + .owner = THIS_MODULE,
  41049. + },
  41050. +};
  41051. +
  41052. +static int __init lirc_rpi_init(void)
  41053. +{
  41054. + int result;
  41055. +
  41056. + /* Init read buffer. */
  41057. + result = lirc_buffer_init(&rbuf, sizeof(int), RBUF_LEN);
  41058. + if (result < 0)
  41059. + return -ENOMEM;
  41060. +
  41061. + result = platform_driver_register(&lirc_rpi_driver);
  41062. + if (result) {
  41063. + printk(KERN_ERR LIRC_DRIVER_NAME
  41064. + ": lirc register returned %d\n", result);
  41065. + goto exit_buffer_free;
  41066. + }
  41067. +
  41068. + lirc_rpi_dev = platform_device_alloc(LIRC_DRIVER_NAME, 0);
  41069. + if (!lirc_rpi_dev) {
  41070. + result = -ENOMEM;
  41071. + goto exit_driver_unregister;
  41072. + }
  41073. +
  41074. + result = platform_device_add(lirc_rpi_dev);
  41075. + if (result)
  41076. + goto exit_device_put;
  41077. +
  41078. + return 0;
  41079. +
  41080. + exit_device_put:
  41081. + platform_device_put(lirc_rpi_dev);
  41082. +
  41083. + exit_driver_unregister:
  41084. + platform_driver_unregister(&lirc_rpi_driver);
  41085. +
  41086. + exit_buffer_free:
  41087. + lirc_buffer_free(&rbuf);
  41088. +
  41089. + return result;
  41090. +}
  41091. +
  41092. +static void lirc_rpi_exit(void)
  41093. +{
  41094. + platform_device_unregister(lirc_rpi_dev);
  41095. + platform_driver_unregister(&lirc_rpi_driver);
  41096. + lirc_buffer_free(&rbuf);
  41097. +}
  41098. +
  41099. +static int __init lirc_rpi_init_module(void)
  41100. +{
  41101. + int result, i;
  41102. +
  41103. + result = lirc_rpi_init();
  41104. + if (result)
  41105. + return result;
  41106. +
  41107. + /* check if the module received valid gpio pin numbers */
  41108. + result = 0;
  41109. + if (gpio_in_pin != gpio_out_pin) {
  41110. + for(i = 0; (i < ARRAY_SIZE(valid_gpio_pins)) && (result != 2); i++) {
  41111. + if (gpio_in_pin == valid_gpio_pins[i] ||
  41112. + gpio_out_pin == valid_gpio_pins[i]) {
  41113. + result++;
  41114. + }
  41115. + }
  41116. + }
  41117. +
  41118. + if (result != 2) {
  41119. + result = -EINVAL;
  41120. + printk(KERN_ERR LIRC_DRIVER_NAME
  41121. + ": invalid GPIO pin(s) specified!\n");
  41122. + goto exit_rpi;
  41123. + }
  41124. +
  41125. + result = init_port();
  41126. + if (result < 0)
  41127. + goto exit_rpi;
  41128. +
  41129. + driver.features = LIRC_CAN_SET_SEND_DUTY_CYCLE |
  41130. + LIRC_CAN_SET_SEND_CARRIER |
  41131. + LIRC_CAN_SEND_PULSE |
  41132. + LIRC_CAN_REC_MODE2;
  41133. +
  41134. + driver.dev = &lirc_rpi_dev->dev;
  41135. + driver.minor = lirc_register_driver(&driver);
  41136. +
  41137. + if (driver.minor < 0) {
  41138. + printk(KERN_ERR LIRC_DRIVER_NAME
  41139. + ": device registration failed with %d\n", result);
  41140. + result = -EIO;
  41141. + goto exit_rpi;
  41142. + }
  41143. +
  41144. + printk(KERN_INFO LIRC_DRIVER_NAME ": driver registered!\n");
  41145. +
  41146. + return 0;
  41147. +
  41148. + exit_rpi:
  41149. + lirc_rpi_exit();
  41150. +
  41151. + return result;
  41152. +}
  41153. +
  41154. +static void __exit lirc_rpi_exit_module(void)
  41155. +{
  41156. + gpio_free(gpio_out_pin);
  41157. + gpio_free(gpio_in_pin);
  41158. +
  41159. + lirc_rpi_exit();
  41160. +
  41161. + lirc_unregister_driver(driver.minor);
  41162. + printk(KERN_INFO LIRC_DRIVER_NAME ": cleaned up module\n");
  41163. +}
  41164. +
  41165. +module_init(lirc_rpi_init_module);
  41166. +module_exit(lirc_rpi_exit_module);
  41167. +
  41168. +MODULE_DESCRIPTION("Infra-red receiver and blaster driver for Raspberry Pi GPIO.");
  41169. +MODULE_AUTHOR("Aron Robert Szabo <aron@reon.hu>");
  41170. +MODULE_AUTHOR("Michael Bishop <cleverca22@gmail.com>");
  41171. +MODULE_LICENSE("GPL");
  41172. +
  41173. +module_param(gpio_out_pin, int, S_IRUGO);
  41174. +MODULE_PARM_DESC(gpio_out_pin, "GPIO output/transmitter pin number of the BCM"
  41175. + " processor. Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11,"
  41176. + " 14, 15, 17, 18, 21, 22, 23, 24, 25, default 17");
  41177. +
  41178. +module_param(gpio_in_pin, int, S_IRUGO);
  41179. +MODULE_PARM_DESC(gpio_in_pin, "GPIO input pin number of the BCM processor."
  41180. + " Valid pin numbers are: 0, 1, 4, 8, 7, 9, 10, 11, 14, 15,"
  41181. + " 17, 18, 21, 22, 23, 24, 25, default 18");
  41182. +
  41183. +module_param(sense, int, S_IRUGO);
  41184. +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
  41185. + " (0 = active high, 1 = active low )");
  41186. +
  41187. +module_param(softcarrier, bool, S_IRUGO);
  41188. +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on, default on)");
  41189. +
  41190. +module_param(invert, bool, S_IRUGO);
  41191. +MODULE_PARM_DESC(invert, "Invert output (0 = off, 1 = on, default off");
  41192. +
  41193. +module_param(debug, bool, S_IRUGO | S_IWUSR);
  41194. +MODULE_PARM_DESC(debug, "Enable debugging messages");
  41195. diff -Nur linux-3.16.2/drivers/staging/media/lirc/Makefile linux-3.16-rpi/drivers/staging/media/lirc/Makefile
  41196. --- linux-3.16.2/drivers/staging/media/lirc/Makefile 2014-09-06 01:37:11.000000000 +0200
  41197. +++ linux-3.16-rpi/drivers/staging/media/lirc/Makefile 2014-04-13 17:33:09.000000000 +0200
  41198. @@ -7,6 +7,7 @@
  41199. obj-$(CONFIG_LIRC_IGORPLUGUSB) += lirc_igorplugusb.o
  41200. obj-$(CONFIG_LIRC_IMON) += lirc_imon.o
  41201. obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
  41202. +obj-$(CONFIG_LIRC_RPI) += lirc_rpi.o
  41203. obj-$(CONFIG_LIRC_SASEM) += lirc_sasem.o
  41204. obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
  41205. obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
  41206. diff -Nur linux-3.16.2/drivers/thermal/bcm2835-thermal.c linux-3.16-rpi/drivers/thermal/bcm2835-thermal.c
  41207. --- linux-3.16.2/drivers/thermal/bcm2835-thermal.c 1970-01-01 01:00:00.000000000 +0100
  41208. +++ linux-3.16-rpi/drivers/thermal/bcm2835-thermal.c 2014-04-13 17:33:10.000000000 +0200
  41209. @@ -0,0 +1,184 @@
  41210. +/*****************************************************************************
  41211. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  41212. +*
  41213. +* Unless you and Broadcom execute a separate written software license
  41214. +* agreement governing use of this software, this software is licensed to you
  41215. +* under the terms of the GNU General Public License version 2, available at
  41216. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  41217. +*
  41218. +* Notwithstanding the above, under no circumstances may you combine this
  41219. +* software in any way with any other Broadcom software provided under a
  41220. +* license other than the GPL, without Broadcom's express prior written
  41221. +* consent.
  41222. +*****************************************************************************/
  41223. +
  41224. +#include <linux/kernel.h>
  41225. +#include <linux/module.h>
  41226. +#include <linux/init.h>
  41227. +#include <linux/platform_device.h>
  41228. +#include <linux/slab.h>
  41229. +#include <linux/sysfs.h>
  41230. +#include <mach/vcio.h>
  41231. +#include <linux/thermal.h>
  41232. +
  41233. +
  41234. +/* --- DEFINITIONS --- */
  41235. +#define MODULE_NAME "bcm2835_thermal"
  41236. +
  41237. +/*#define THERMAL_DEBUG_ENABLE*/
  41238. +
  41239. +#ifdef THERMAL_DEBUG_ENABLE
  41240. +#define print_debug(fmt,...) printk(KERN_INFO "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  41241. +#else
  41242. +#define print_debug(fmt,...)
  41243. +#endif
  41244. +#define print_err(fmt,...) printk(KERN_ERR "%s:%s:%d: "fmt"\n", MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  41245. +
  41246. +#define VC_TAG_GET_TEMP 0x00030006
  41247. +#define VC_TAG_GET_MAX_TEMP 0x0003000A
  41248. +
  41249. +typedef enum {
  41250. + TEMP,
  41251. + MAX_TEMP,
  41252. +} temp_type;
  41253. +
  41254. +/* --- STRUCTS --- */
  41255. +/* tag part of the message */
  41256. +struct vc_msg_tag {
  41257. + uint32_t tag_id; /* the tag ID for the temperature */
  41258. + uint32_t buffer_size; /* size of the buffer (should be 8) */
  41259. + uint32_t request_code; /* identifies message as a request (should be 0) */
  41260. + uint32_t id; /* extra ID field (should be 0) */
  41261. + uint32_t val; /* returned value of the temperature */
  41262. +};
  41263. +
  41264. +/* message structure to be sent to videocore */
  41265. +struct vc_msg {
  41266. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  41267. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  41268. + struct vc_msg_tag tag; /* the tag structure above to make */
  41269. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  41270. +};
  41271. +
  41272. +struct bcm2835_thermal_data {
  41273. + struct thermal_zone_device *thermal_dev;
  41274. + struct vc_msg msg;
  41275. +};
  41276. +
  41277. +/* --- GLOBALS --- */
  41278. +static struct bcm2835_thermal_data bcm2835_data;
  41279. +
  41280. +/* Thermal Device Operations */
  41281. +static struct thermal_zone_device_ops ops;
  41282. +
  41283. +/* --- FUNCTIONS --- */
  41284. +
  41285. +static int bcm2835_get_temp_or_max(struct thermal_zone_device *thermal_dev, unsigned long *temp, unsigned tag_id)
  41286. +{
  41287. + int result = -1, retry = 3;
  41288. + print_debug("IN");
  41289. +
  41290. + *temp = 0;
  41291. + while (result != 0 && retry-- > 0) {
  41292. + /* wipe all previous message data */
  41293. + memset(&bcm2835_data.msg, 0, sizeof bcm2835_data.msg);
  41294. +
  41295. + /* prepare message */
  41296. + bcm2835_data.msg.msg_size = sizeof bcm2835_data.msg;
  41297. + bcm2835_data.msg.tag.buffer_size = 8;
  41298. + bcm2835_data.msg.tag.tag_id = tag_id;
  41299. +
  41300. + /* send the message */
  41301. + result = bcm_mailbox_property(&bcm2835_data.msg, sizeof bcm2835_data.msg);
  41302. + print_debug("Got %stemperature as %u (%d,%x)\n", tag_id==VC_TAG_GET_MAX_TEMP ? "max ":"", (uint)bcm2835_data.msg.tag.val, result, bcm2835_data.msg.request_code);
  41303. + if (!(bcm2835_data.msg.request_code & 0x80000000))
  41304. + result = -1;
  41305. + }
  41306. +
  41307. + /* check if it was all ok and return the rate in milli degrees C */
  41308. + if (result == 0)
  41309. + *temp = (uint)bcm2835_data.msg.tag.val;
  41310. + else
  41311. + print_err("Failed to get temperature! (%x:%d)\n", tag_id, result);
  41312. + print_debug("OUT");
  41313. + return result;
  41314. +}
  41315. +
  41316. +static int bcm2835_get_temp(struct thermal_zone_device *thermal_dev, unsigned long *temp)
  41317. +{
  41318. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_TEMP);
  41319. +}
  41320. +
  41321. +static int bcm2835_get_max_temp(struct thermal_zone_device *thermal_dev, int trip_num, unsigned long *temp)
  41322. +{
  41323. + return bcm2835_get_temp_or_max(thermal_dev, temp, VC_TAG_GET_MAX_TEMP);
  41324. +}
  41325. +
  41326. +static int bcm2835_get_trip_type(struct thermal_zone_device * thermal_dev, int trip_num, enum thermal_trip_type *trip_type)
  41327. +{
  41328. + *trip_type = THERMAL_TRIP_HOT;
  41329. + return 0;
  41330. +}
  41331. +
  41332. +
  41333. +static int bcm2835_get_mode(struct thermal_zone_device *thermal_dev, enum thermal_device_mode *dev_mode)
  41334. +{
  41335. + *dev_mode = THERMAL_DEVICE_ENABLED;
  41336. + return 0;
  41337. +}
  41338. +
  41339. +
  41340. +static int bcm2835_thermal_probe(struct platform_device *pdev)
  41341. +{
  41342. + print_debug("IN");
  41343. + print_debug("THERMAL Driver has been probed!");
  41344. +
  41345. + /* check that the device isn't null!*/
  41346. + if(pdev == NULL)
  41347. + {
  41348. + print_debug("Platform device is empty!");
  41349. + return -ENODEV;
  41350. + }
  41351. +
  41352. + if(!(bcm2835_data.thermal_dev = thermal_zone_device_register("bcm2835_thermal", 1, 0, NULL, &ops, NULL, 0, 0)))
  41353. + {
  41354. + print_debug("Unable to register the thermal device!");
  41355. + return -EFAULT;
  41356. + }
  41357. + return 0;
  41358. +}
  41359. +
  41360. +
  41361. +static int bcm2835_thermal_remove(struct platform_device *pdev)
  41362. +{
  41363. + print_debug("IN");
  41364. +
  41365. + thermal_zone_device_unregister(bcm2835_data.thermal_dev);
  41366. +
  41367. + print_debug("OUT");
  41368. +
  41369. + return 0;
  41370. +}
  41371. +
  41372. +static struct thermal_zone_device_ops ops = {
  41373. + .get_temp = bcm2835_get_temp,
  41374. + .get_trip_temp = bcm2835_get_max_temp,
  41375. + .get_trip_type = bcm2835_get_trip_type,
  41376. + .get_mode = bcm2835_get_mode,
  41377. +};
  41378. +
  41379. +/* Thermal Driver */
  41380. +static struct platform_driver bcm2835_thermal_driver = {
  41381. + .probe = bcm2835_thermal_probe,
  41382. + .remove = bcm2835_thermal_remove,
  41383. + .driver = {
  41384. + .name = "bcm2835_thermal",
  41385. + .owner = THIS_MODULE,
  41386. + },
  41387. +};
  41388. +
  41389. +MODULE_LICENSE("GPL");
  41390. +MODULE_AUTHOR("Dorian Peake");
  41391. +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  41392. +
  41393. +module_platform_driver(bcm2835_thermal_driver);
  41394. diff -Nur linux-3.16.2/drivers/thermal/Kconfig linux-3.16-rpi/drivers/thermal/Kconfig
  41395. --- linux-3.16.2/drivers/thermal/Kconfig 2014-09-06 01:37:11.000000000 +0200
  41396. +++ linux-3.16-rpi/drivers/thermal/Kconfig 2014-09-14 19:04:04.000000000 +0200
  41397. @@ -196,6 +196,12 @@
  41398. enforce idle time which results in more package C-state residency. The
  41399. user interface is exposed via generic thermal framework.
  41400. +config THERMAL_BCM2835
  41401. + tristate "BCM2835 Thermal Driver"
  41402. + help
  41403. + This will enable temperature monitoring for the Broadcom BCM2835
  41404. + chip. If built as a module, it will be called 'bcm2835-thermal'.
  41405. +
  41406. config X86_PKG_TEMP_THERMAL
  41407. tristate "X86 package temperature thermal driver"
  41408. depends on X86_THERMAL_VECTOR
  41409. diff -Nur linux-3.16.2/drivers/thermal/Makefile linux-3.16-rpi/drivers/thermal/Makefile
  41410. --- linux-3.16.2/drivers/thermal/Makefile 2014-09-06 01:37:11.000000000 +0200
  41411. +++ linux-3.16-rpi/drivers/thermal/Makefile 2014-09-14 19:04:05.000000000 +0200
  41412. @@ -28,6 +28,7 @@
  41413. obj-$(CONFIG_IMX_THERMAL) += imx_thermal.o
  41414. obj-$(CONFIG_DB8500_CPUFREQ_COOLING) += db8500_cpufreq_cooling.o
  41415. obj-$(CONFIG_INTEL_POWERCLAMP) += intel_powerclamp.o
  41416. +obj-$(CONFIG_THERMAL_BCM2835) += bcm2835-thermal.o
  41417. obj-$(CONFIG_X86_PKG_TEMP_THERMAL) += x86_pkg_temp_thermal.o
  41418. obj-$(CONFIG_INTEL_SOC_DTS_THERMAL) += intel_soc_dts_thermal.o
  41419. obj-$(CONFIG_TI_SOC_THERMAL) += ti-soc-thermal/
  41420. diff -Nur linux-3.16.2/drivers/tty/serial/amba-pl011.c linux-3.16-rpi/drivers/tty/serial/amba-pl011.c
  41421. --- linux-3.16.2/drivers/tty/serial/amba-pl011.c 2014-09-06 01:37:11.000000000 +0200
  41422. +++ linux-3.16-rpi/drivers/tty/serial/amba-pl011.c 2014-09-14 19:04:10.000000000 +0200
  41423. @@ -84,7 +84,7 @@
  41424. static unsigned int get_fifosize_arm(struct amba_device *dev)
  41425. {
  41426. - return amba_rev(dev) < 3 ? 16 : 32;
  41427. + return 16; //TODO: fix: amba_rev(dev) < 3 ? 16 : 32;
  41428. }
  41429. static struct vendor_data vendor_arm = {
  41430. diff -Nur linux-3.16.2/drivers/usb/core/generic.c linux-3.16-rpi/drivers/usb/core/generic.c
  41431. --- linux-3.16.2/drivers/usb/core/generic.c 2014-09-06 01:37:11.000000000 +0200
  41432. +++ linux-3.16-rpi/drivers/usb/core/generic.c 2014-09-14 19:04:12.000000000 +0200
  41433. @@ -152,6 +152,7 @@
  41434. dev_warn(&udev->dev,
  41435. "no configuration chosen from %d choice%s\n",
  41436. num_configs, plural(num_configs));
  41437. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  41438. }
  41439. return i;
  41440. }
  41441. diff -Nur linux-3.16.2/drivers/usb/core/hub.c linux-3.16-rpi/drivers/usb/core/hub.c
  41442. --- linux-3.16.2/drivers/usb/core/hub.c 2014-09-06 01:37:11.000000000 +0200
  41443. +++ linux-3.16-rpi/drivers/usb/core/hub.c 2014-09-14 19:04:12.000000000 +0200
  41444. @@ -4897,7 +4897,7 @@
  41445. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  41446. u16 status = 0, unused;
  41447. - dev_dbg(&port_dev->dev, "over-current change\n");
  41448. + dev_notice(&port_dev->dev, "over-current change\n");
  41449. usb_clear_port_feature(hdev, port1,
  41450. USB_PORT_FEAT_C_OVER_CURRENT);
  41451. msleep(100); /* Cool down */
  41452. diff -Nur linux-3.16.2/drivers/usb/core/message.c linux-3.16-rpi/drivers/usb/core/message.c
  41453. --- linux-3.16.2/drivers/usb/core/message.c 2014-09-06 01:37:11.000000000 +0200
  41454. +++ linux-3.16-rpi/drivers/usb/core/message.c 2014-09-14 19:04:12.000000000 +0200
  41455. @@ -1891,6 +1891,85 @@
  41456. if (cp->string == NULL &&
  41457. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  41458. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  41459. +/* Uncomment this define to enable the HS Electrical Test support */
  41460. +#define DWC_HS_ELECT_TST 1
  41461. +#ifdef DWC_HS_ELECT_TST
  41462. + /* Here we implement the HS Electrical Test support. The
  41463. + * tester uses a vendor ID of 0x1A0A to indicate we should
  41464. + * run a special test sequence. The product ID tells us
  41465. + * which sequence to run. We invoke the test sequence by
  41466. + * sending a non-standard SetFeature command to our root
  41467. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  41468. + * recognize the command and perform the desired test
  41469. + * sequence.
  41470. + */
  41471. + if (dev->descriptor.idVendor == 0x1A0A) {
  41472. + /* HSOTG Electrical Test */
  41473. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  41474. +
  41475. + if (dev->bus && dev->bus->root_hub) {
  41476. + struct usb_device *hdev = dev->bus->root_hub;
  41477. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  41478. +
  41479. + switch (dev->descriptor.idProduct) {
  41480. + case 0x0101: /* TEST_SE0_NAK */
  41481. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  41482. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41483. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41484. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  41485. + break;
  41486. +
  41487. + case 0x0102: /* TEST_J */
  41488. + dev_warn(&dev->dev, "TEST_J\n");
  41489. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41490. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41491. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  41492. + break;
  41493. +
  41494. + case 0x0103: /* TEST_K */
  41495. + dev_warn(&dev->dev, "TEST_K\n");
  41496. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41497. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41498. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  41499. + break;
  41500. +
  41501. + case 0x0104: /* TEST_PACKET */
  41502. + dev_warn(&dev->dev, "TEST_PACKET\n");
  41503. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41504. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41505. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  41506. + break;
  41507. +
  41508. + case 0x0105: /* TEST_FORCE_ENABLE */
  41509. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  41510. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41511. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41512. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  41513. + break;
  41514. +
  41515. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  41516. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  41517. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41518. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41519. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  41520. + break;
  41521. +
  41522. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  41523. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  41524. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41525. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41526. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  41527. + break;
  41528. +
  41529. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  41530. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  41531. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  41532. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  41533. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  41534. + }
  41535. + }
  41536. + }
  41537. +#endif /* DWC_HS_ELECT_TST */
  41538. /* Now that the interfaces are installed, re-enable LPM. */
  41539. usb_unlocked_enable_lpm(dev);
  41540. diff -Nur linux-3.16.2/drivers/usb/core/otg_whitelist.h linux-3.16-rpi/drivers/usb/core/otg_whitelist.h
  41541. --- linux-3.16.2/drivers/usb/core/otg_whitelist.h 2014-09-06 01:37:11.000000000 +0200
  41542. +++ linux-3.16-rpi/drivers/usb/core/otg_whitelist.h 2014-09-14 19:04:12.000000000 +0200
  41543. @@ -19,33 +19,82 @@
  41544. static struct usb_device_id whitelist_table [] = {
  41545. /* hubs are optional in OTG, but very handy ... */
  41546. +#define CERT_WITHOUT_HUBS
  41547. +#if defined(CERT_WITHOUT_HUBS)
  41548. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  41549. +#else
  41550. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  41551. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  41552. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  41553. +#endif
  41554. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  41555. /* FIXME actually, printers are NOT supposed to use device classes;
  41556. * they're supposed to use interface classes...
  41557. */
  41558. -{ USB_DEVICE_INFO(7, 1, 1) },
  41559. -{ USB_DEVICE_INFO(7, 1, 2) },
  41560. -{ USB_DEVICE_INFO(7, 1, 3) },
  41561. +//{ USB_DEVICE_INFO(7, 1, 1) },
  41562. +//{ USB_DEVICE_INFO(7, 1, 2) },
  41563. +//{ USB_DEVICE_INFO(7, 1, 3) },
  41564. #endif
  41565. #ifdef CONFIG_USB_NET_CDCETHER
  41566. /* Linux-USB CDC Ethernet gadget */
  41567. -{ USB_DEVICE(0x0525, 0xa4a1), },
  41568. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  41569. /* Linux-USB CDC Ethernet + RNDIS gadget */
  41570. -{ USB_DEVICE(0x0525, 0xa4a2), },
  41571. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  41572. #endif
  41573. #if defined(CONFIG_USB_TEST) || defined(CONFIG_USB_TEST_MODULE)
  41574. /* gadget zero, for testing */
  41575. -{ USB_DEVICE(0x0525, 0xa4a0), },
  41576. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  41577. #endif
  41578. +/* OPT Tester */
  41579. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  41580. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  41581. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  41582. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  41583. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  41584. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  41585. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  41586. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  41587. +
  41588. +/* Sony cameras */
  41589. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  41590. +
  41591. +/* Memory Devices */
  41592. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  41593. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  41594. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  41595. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  41596. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  41597. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  41598. +
  41599. +/* HP Printers */
  41600. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  41601. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  41602. +
  41603. +/* Speakers */
  41604. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  41605. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  41606. +
  41607. { } /* Terminating entry */
  41608. };
  41609. +static inline void report_errors(struct usb_device *dev)
  41610. +{
  41611. + /* OTG MESSAGE: report errors here, customize to match your product */
  41612. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  41613. + le16_to_cpu(dev->descriptor.idVendor),
  41614. + le16_to_cpu(dev->descriptor.idProduct));
  41615. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  41616. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  41617. + } else {
  41618. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  41619. + }
  41620. +}
  41621. +
  41622. +
  41623. static int is_targeted(struct usb_device *dev)
  41624. {
  41625. struct usb_device_id *id = whitelist_table;
  41626. @@ -55,58 +104,83 @@
  41627. return 1;
  41628. /* HNP test device is _never_ targeted (see OTG spec 6.6.6) */
  41629. - if ((le16_to_cpu(dev->descriptor.idVendor) == 0x1a0a &&
  41630. - le16_to_cpu(dev->descriptor.idProduct) == 0xbadd))
  41631. - return 0;
  41632. + if (dev->descriptor.idVendor == 0x1a0a &&
  41633. + dev->descriptor.idProduct == 0xbadd) {
  41634. + return 0;
  41635. + } else if (!enable_whitelist) {
  41636. + return 1;
  41637. + } else {
  41638. - /* NOTE: can't use usb_match_id() since interface caches
  41639. - * aren't set up yet. this is cut/paste from that code.
  41640. - */
  41641. - for (id = whitelist_table; id->match_flags; id++) {
  41642. - if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  41643. - id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  41644. - continue;
  41645. -
  41646. - if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  41647. - id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  41648. - continue;
  41649. -
  41650. - /* No need to test id->bcdDevice_lo != 0, since 0 is never
  41651. - greater than any unsigned number. */
  41652. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  41653. - (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  41654. - continue;
  41655. -
  41656. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  41657. - (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  41658. - continue;
  41659. -
  41660. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  41661. - (id->bDeviceClass != dev->descriptor.bDeviceClass))
  41662. - continue;
  41663. -
  41664. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  41665. - (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  41666. - continue;
  41667. -
  41668. - if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  41669. - (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  41670. - continue;
  41671. +#ifdef DEBUG
  41672. + dev_dbg(&dev->dev, "device V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  41673. + dev->descriptor.idVendor,
  41674. + dev->descriptor.idProduct,
  41675. + dev->descriptor.bDeviceClass,
  41676. + dev->descriptor.bDeviceSubClass,
  41677. + dev->descriptor.bDeviceProtocol);
  41678. +#endif
  41679. return 1;
  41680. + /* NOTE: can't use usb_match_id() since interface caches
  41681. + * aren't set up yet. this is cut/paste from that code.
  41682. + */
  41683. + for (id = whitelist_table; id->match_flags; id++) {
  41684. +#ifdef DEBUG
  41685. + dev_dbg(&dev->dev,
  41686. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  41687. + id->idVendor,
  41688. + id->idProduct,
  41689. + id->bDeviceClass,
  41690. + id->bDeviceSubClass,
  41691. + id->bDeviceProtocol);
  41692. +#endif
  41693. +
  41694. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  41695. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  41696. + continue;
  41697. +
  41698. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  41699. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  41700. + continue;
  41701. +
  41702. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  41703. + greater than any unsigned number. */
  41704. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  41705. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  41706. + continue;
  41707. +
  41708. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  41709. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  41710. + continue;
  41711. +
  41712. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  41713. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  41714. + continue;
  41715. +
  41716. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  41717. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  41718. + continue;
  41719. +
  41720. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  41721. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  41722. + continue;
  41723. +
  41724. + return 1;
  41725. + }
  41726. }
  41727. /* add other match criteria here ... */
  41728. -
  41729. - /* OTG MESSAGE: report errors here, customize to match your product */
  41730. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  41731. - le16_to_cpu(dev->descriptor.idVendor),
  41732. - le16_to_cpu(dev->descriptor.idProduct));
  41733. #ifdef CONFIG_USB_OTG_WHITELIST
  41734. + report_errors(dev);
  41735. return 0;
  41736. #else
  41737. - return 1;
  41738. + if (enable_whitelist) {
  41739. + report_errors(dev);
  41740. + return 0;
  41741. + } else {
  41742. + return 1;
  41743. + }
  41744. #endif
  41745. }
  41746. diff -Nur linux-3.16.2/drivers/usb/gadget/file_storage.c linux-3.16-rpi/drivers/usb/gadget/file_storage.c
  41747. --- linux-3.16.2/drivers/usb/gadget/file_storage.c 1970-01-01 01:00:00.000000000 +0100
  41748. +++ linux-3.16-rpi/drivers/usb/gadget/file_storage.c 2014-04-13 17:33:11.000000000 +0200
  41749. @@ -0,0 +1,3676 @@
  41750. +/*
  41751. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  41752. + *
  41753. + * Copyright (C) 2003-2008 Alan Stern
  41754. + * All rights reserved.
  41755. + *
  41756. + * Redistribution and use in source and binary forms, with or without
  41757. + * modification, are permitted provided that the following conditions
  41758. + * are met:
  41759. + * 1. Redistributions of source code must retain the above copyright
  41760. + * notice, this list of conditions, and the following disclaimer,
  41761. + * without modification.
  41762. + * 2. Redistributions in binary form must reproduce the above copyright
  41763. + * notice, this list of conditions and the following disclaimer in the
  41764. + * documentation and/or other materials provided with the distribution.
  41765. + * 3. The names of the above-listed copyright holders may not be used
  41766. + * to endorse or promote products derived from this software without
  41767. + * specific prior written permission.
  41768. + *
  41769. + * ALTERNATIVELY, this software may be distributed under the terms of the
  41770. + * GNU General Public License ("GPL") as published by the Free Software
  41771. + * Foundation, either version 2 of that License or (at your option) any
  41772. + * later version.
  41773. + *
  41774. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  41775. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  41776. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41777. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  41778. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  41779. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  41780. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  41781. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  41782. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  41783. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  41784. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41785. + */
  41786. +
  41787. +
  41788. +/*
  41789. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  41790. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  41791. + * to providing an example of a genuinely useful gadget driver for a USB
  41792. + * device, it also illustrates a technique of double-buffering for increased
  41793. + * throughput. Last but not least, it gives an easy way to probe the
  41794. + * behavior of the Mass Storage drivers in a USB host.
  41795. + *
  41796. + * Backing storage is provided by a regular file or a block device, specified
  41797. + * by the "file" module parameter. Access can be limited to read-only by
  41798. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  41799. + * access is always read-only.) The gadget will indicate that it has
  41800. + * removable media if the optional "removable" module parameter is set.
  41801. + *
  41802. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  41803. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  41804. + * by the optional "transport" module parameter. It also supports the
  41805. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  41806. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  41807. + * the optional "protocol" module parameter. In addition, the default
  41808. + * Vendor ID, Product ID, release number and serial number can be overridden.
  41809. + *
  41810. + * There is support for multiple logical units (LUNs), each of which has
  41811. + * its own backing file. The number of LUNs can be set using the optional
  41812. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  41813. + * files are specified using comma-separated lists for "file" and "ro".
  41814. + * The default number of LUNs is taken from the number of "file" elements;
  41815. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  41816. + * file must be specified for each LUN. If it is set, then an unspecified
  41817. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  41818. + * each LUN would be settable independently as a disk drive or a CD-ROM
  41819. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  41820. + * emulation includes a single data track and no audio tracks; hence there
  41821. + * need be only one backing file per LUN.
  41822. + *
  41823. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  41824. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  41825. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  41826. + * Support is included for both full-speed and high-speed operation.
  41827. + *
  41828. + * Note that the driver is slightly non-portable in that it assumes a
  41829. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  41830. + * interrupt-in endpoints. With most device controllers this isn't an
  41831. + * issue, but there may be some with hardware restrictions that prevent
  41832. + * a buffer from being used by more than one endpoint.
  41833. + *
  41834. + * Module options:
  41835. + *
  41836. + * file=filename[,filename...]
  41837. + * Required if "removable" is not set, names of
  41838. + * the files or block devices used for
  41839. + * backing storage
  41840. + * serial=HHHH... Required serial number (string of hex chars)
  41841. + * ro=b[,b...] Default false, booleans for read-only access
  41842. + * removable Default false, boolean for removable media
  41843. + * luns=N Default N = number of filenames, number of
  41844. + * LUNs to support
  41845. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  41846. + * in SCSI WRITE(10,12) commands
  41847. + * stall Default determined according to the type of
  41848. + * USB device controller (usually true),
  41849. + * boolean to permit the driver to halt
  41850. + * bulk endpoints
  41851. + * cdrom Default false, boolean for whether to emulate
  41852. + * a CD-ROM drive
  41853. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  41854. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  41855. + * ATAPI, QIC, UFI, 8070, or SCSI;
  41856. + * also 1 - 6)
  41857. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  41858. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  41859. + * release=0xRRRR Override the USB release number (bcdDevice)
  41860. + * buflen=N Default N=16384, buffer size used (will be
  41861. + * rounded down to a multiple of
  41862. + * PAGE_CACHE_SIZE)
  41863. + *
  41864. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  41865. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  41866. + * default values are used for everything else.
  41867. + *
  41868. + * The pathnames of the backing files and the ro settings are available in
  41869. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  41870. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  41871. + * these files will simulate ejecting/loading the medium (writing an empty
  41872. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  41873. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  41874. + * is being used.
  41875. + *
  41876. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  41877. + * The driver's SCSI command interface was based on the "Information
  41878. + * technology - Small Computer System Interface - 2" document from
  41879. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  41880. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  41881. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  41882. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  41883. + * document, Revision 1.0, December 14, 1998, available at
  41884. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  41885. + */
  41886. +
  41887. +
  41888. +/*
  41889. + * Driver Design
  41890. + *
  41891. + * The FSG driver is fairly straightforward. There is a main kernel
  41892. + * thread that handles most of the work. Interrupt routines field
  41893. + * callbacks from the controller driver: bulk- and interrupt-request
  41894. + * completion notifications, endpoint-0 events, and disconnect events.
  41895. + * Completion events are passed to the main thread by wakeup calls. Many
  41896. + * ep0 requests are handled at interrupt time, but SetInterface,
  41897. + * SetConfiguration, and device reset requests are forwarded to the
  41898. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  41899. + * should interrupt any ongoing file I/O operations).
  41900. + *
  41901. + * The thread's main routine implements the standard command/data/status
  41902. + * parts of a SCSI interaction. It and its subroutines are full of tests
  41903. + * for pending signals/exceptions -- all this polling is necessary since
  41904. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  41905. + * indication that the driver really wants to be running in userspace.)
  41906. + * An important point is that so long as the thread is alive it keeps an
  41907. + * open reference to the backing file. This will prevent unmounting
  41908. + * the backing file's underlying filesystem and could cause problems
  41909. + * during system shutdown, for example. To prevent such problems, the
  41910. + * thread catches INT, TERM, and KILL signals and converts them into
  41911. + * an EXIT exception.
  41912. + *
  41913. + * In normal operation the main thread is started during the gadget's
  41914. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  41915. + * exit when it receives a signal, and there's no point leaving the
  41916. + * gadget running when the thread is dead. So just before the thread
  41917. + * exits, it deregisters the gadget driver. This makes things a little
  41918. + * tricky: The driver is deregistered at two places, and the exiting
  41919. + * thread can indirectly call fsg_unbind() which in turn can tell the
  41920. + * thread to exit. The first problem is resolved through the use of the
  41921. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  41922. + * The second problem is resolved by having fsg_unbind() check
  41923. + * fsg->state; it won't try to stop the thread if the state is already
  41924. + * FSG_STATE_TERMINATED.
  41925. + *
  41926. + * To provide maximum throughput, the driver uses a circular pipeline of
  41927. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  41928. + * arbitrarily long; in practice the benefits don't justify having more
  41929. + * than 2 stages (i.e., double buffering). But it helps to think of the
  41930. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  41931. + * a bulk-out request pointer (since the buffer can be used for both
  41932. + * output and input -- directions always are given from the host's
  41933. + * point of view) as well as a pointer to the buffer and various state
  41934. + * variables.
  41935. + *
  41936. + * Use of the pipeline follows a simple protocol. There is a variable
  41937. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  41938. + * At any time that buffer head may still be in use from an earlier
  41939. + * request, so each buffer head has a state variable indicating whether
  41940. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  41941. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  41942. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  41943. + * head FULL when the I/O is complete. Then the buffer will be emptied
  41944. + * (again possibly by USB I/O, during which it is marked BUSY) and
  41945. + * finally marked EMPTY again (possibly by a completion routine).
  41946. + *
  41947. + * A module parameter tells the driver to avoid stalling the bulk
  41948. + * endpoints wherever the transport specification allows. This is
  41949. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  41950. + * halt on a bulk endpoint. However, under certain circumstances the
  41951. + * Bulk-only specification requires a stall. In such cases the driver
  41952. + * will halt the endpoint and set a flag indicating that it should clear
  41953. + * the halt in software during the next device reset. Hopefully this
  41954. + * will permit everything to work correctly. Furthermore, although the
  41955. + * specification allows the bulk-out endpoint to halt when the host sends
  41956. + * too much data, implementing this would cause an unavoidable race.
  41957. + * The driver will always use the "no-stall" approach for OUT transfers.
  41958. + *
  41959. + * One subtle point concerns sending status-stage responses for ep0
  41960. + * requests. Some of these requests, such as device reset, can involve
  41961. + * interrupting an ongoing file I/O operation, which might take an
  41962. + * arbitrarily long time. During that delay the host might give up on
  41963. + * the original ep0 request and issue a new one. When that happens the
  41964. + * driver should not notify the host about completion of the original
  41965. + * request, as the host will no longer be waiting for it. So the driver
  41966. + * assigns to each ep0 request a unique tag, and it keeps track of the
  41967. + * tag value of the request associated with a long-running exception
  41968. + * (device-reset, interface-change, or configuration-change). When the
  41969. + * exception handler is finished, the status-stage response is submitted
  41970. + * only if the current ep0 request tag is equal to the exception request
  41971. + * tag. Thus only the most recently received ep0 request will get a
  41972. + * status-stage response.
  41973. + *
  41974. + * Warning: This driver source file is too long. It ought to be split up
  41975. + * into a header file plus about 3 separate .c files, to handle the details
  41976. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  41977. + */
  41978. +
  41979. +
  41980. +/* #define VERBOSE_DEBUG */
  41981. +/* #define DUMP_MSGS */
  41982. +
  41983. +
  41984. +#include <linux/blkdev.h>
  41985. +#include <linux/completion.h>
  41986. +#include <linux/dcache.h>
  41987. +#include <linux/delay.h>
  41988. +#include <linux/device.h>
  41989. +#include <linux/fcntl.h>
  41990. +#include <linux/file.h>
  41991. +#include <linux/fs.h>
  41992. +#include <linux/kref.h>
  41993. +#include <linux/kthread.h>
  41994. +#include <linux/limits.h>
  41995. +#include <linux/module.h>
  41996. +#include <linux/rwsem.h>
  41997. +#include <linux/slab.h>
  41998. +#include <linux/spinlock.h>
  41999. +#include <linux/string.h>
  42000. +#include <linux/freezer.h>
  42001. +#include <linux/utsname.h>
  42002. +
  42003. +#include <linux/usb/ch9.h>
  42004. +#include <linux/usb/gadget.h>
  42005. +
  42006. +#include "gadget_chips.h"
  42007. +
  42008. +
  42009. +
  42010. +/*
  42011. + * Kbuild is not very cooperative with respect to linking separately
  42012. + * compiled library objects into one module. So for now we won't use
  42013. + * separate compilation ... ensuring init/exit sections work to shrink
  42014. + * the runtime footprint, and giving us at least some parts of what
  42015. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  42016. + */
  42017. +#include "usbstring.c"
  42018. +#include "config.c"
  42019. +#include "epautoconf.c"
  42020. +
  42021. +/*-------------------------------------------------------------------------*/
  42022. +
  42023. +#define DRIVER_DESC "File-backed Storage Gadget"
  42024. +#define DRIVER_NAME "g_file_storage"
  42025. +#define DRIVER_VERSION "1 September 2010"
  42026. +
  42027. +static char fsg_string_manufacturer[64];
  42028. +static const char fsg_string_product[] = DRIVER_DESC;
  42029. +static const char fsg_string_config[] = "Self-powered";
  42030. +static const char fsg_string_interface[] = "Mass Storage";
  42031. +
  42032. +
  42033. +#include "storage_common.c"
  42034. +
  42035. +
  42036. +MODULE_DESCRIPTION(DRIVER_DESC);
  42037. +MODULE_AUTHOR("Alan Stern");
  42038. +MODULE_LICENSE("Dual BSD/GPL");
  42039. +
  42040. +/*
  42041. + * This driver assumes self-powered hardware and has no way for users to
  42042. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  42043. + * and endpoint addresses.
  42044. + */
  42045. +
  42046. +
  42047. +/*-------------------------------------------------------------------------*/
  42048. +
  42049. +
  42050. +/* Encapsulate the module parameter settings */
  42051. +
  42052. +static struct {
  42053. + char *file[FSG_MAX_LUNS];
  42054. + char *serial;
  42055. + bool ro[FSG_MAX_LUNS];
  42056. + bool nofua[FSG_MAX_LUNS];
  42057. + unsigned int num_filenames;
  42058. + unsigned int num_ros;
  42059. + unsigned int num_nofuas;
  42060. + unsigned int nluns;
  42061. +
  42062. + bool removable;
  42063. + bool can_stall;
  42064. + bool cdrom;
  42065. +
  42066. + char *transport_parm;
  42067. + char *protocol_parm;
  42068. + unsigned short vendor;
  42069. + unsigned short product;
  42070. + unsigned short release;
  42071. + unsigned int buflen;
  42072. +
  42073. + int transport_type;
  42074. + char *transport_name;
  42075. + int protocol_type;
  42076. + char *protocol_name;
  42077. +
  42078. +} mod_data = { // Default values
  42079. + .transport_parm = "BBB",
  42080. + .protocol_parm = "SCSI",
  42081. + .removable = 0,
  42082. + .can_stall = 1,
  42083. + .cdrom = 0,
  42084. + .vendor = FSG_VENDOR_ID,
  42085. + .product = FSG_PRODUCT_ID,
  42086. + .release = 0xffff, // Use controller chip type
  42087. + .buflen = 16384,
  42088. + };
  42089. +
  42090. +
  42091. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  42092. + S_IRUGO);
  42093. +MODULE_PARM_DESC(file, "names of backing files or devices");
  42094. +
  42095. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  42096. +MODULE_PARM_DESC(serial, "USB serial number");
  42097. +
  42098. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  42099. +MODULE_PARM_DESC(ro, "true to force read-only");
  42100. +
  42101. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  42102. + S_IRUGO);
  42103. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  42104. +
  42105. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  42106. +MODULE_PARM_DESC(luns, "number of LUNs");
  42107. +
  42108. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  42109. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  42110. +
  42111. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  42112. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  42113. +
  42114. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  42115. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  42116. +
  42117. +/* In the non-TEST version, only the module parameters listed above
  42118. + * are available. */
  42119. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  42120. +
  42121. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  42122. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  42123. +
  42124. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  42125. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  42126. + "8070, or SCSI)");
  42127. +
  42128. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  42129. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  42130. +
  42131. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  42132. +MODULE_PARM_DESC(product, "USB Product ID");
  42133. +
  42134. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  42135. +MODULE_PARM_DESC(release, "USB release number");
  42136. +
  42137. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  42138. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  42139. +
  42140. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  42141. +
  42142. +
  42143. +/*
  42144. + * These definitions will permit the compiler to avoid generating code for
  42145. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  42146. + * can recognize when a test of a constant expression yields a dead code
  42147. + * path.
  42148. + */
  42149. +
  42150. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  42151. +
  42152. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  42153. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  42154. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  42155. +
  42156. +#else
  42157. +
  42158. +#define transport_is_bbb() 1
  42159. +#define transport_is_cbi() 0
  42160. +#define protocol_is_scsi() 1
  42161. +
  42162. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  42163. +
  42164. +
  42165. +/*-------------------------------------------------------------------------*/
  42166. +
  42167. +
  42168. +struct fsg_dev {
  42169. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  42170. + spinlock_t lock;
  42171. + struct usb_gadget *gadget;
  42172. +
  42173. + /* filesem protects: backing files in use */
  42174. + struct rw_semaphore filesem;
  42175. +
  42176. + /* reference counting: wait until all LUNs are released */
  42177. + struct kref ref;
  42178. +
  42179. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  42180. + struct usb_request *ep0req; // For control responses
  42181. + unsigned int ep0_req_tag;
  42182. + const char *ep0req_name;
  42183. +
  42184. + struct usb_request *intreq; // For interrupt responses
  42185. + int intreq_busy;
  42186. + struct fsg_buffhd *intr_buffhd;
  42187. +
  42188. + unsigned int bulk_out_maxpacket;
  42189. + enum fsg_state state; // For exception handling
  42190. + unsigned int exception_req_tag;
  42191. +
  42192. + u8 config, new_config;
  42193. +
  42194. + unsigned int running : 1;
  42195. + unsigned int bulk_in_enabled : 1;
  42196. + unsigned int bulk_out_enabled : 1;
  42197. + unsigned int intr_in_enabled : 1;
  42198. + unsigned int phase_error : 1;
  42199. + unsigned int short_packet_received : 1;
  42200. + unsigned int bad_lun_okay : 1;
  42201. +
  42202. + unsigned long atomic_bitflags;
  42203. +#define REGISTERED 0
  42204. +#define IGNORE_BULK_OUT 1
  42205. +#define SUSPENDED 2
  42206. +
  42207. + struct usb_ep *bulk_in;
  42208. + struct usb_ep *bulk_out;
  42209. + struct usb_ep *intr_in;
  42210. +
  42211. + struct fsg_buffhd *next_buffhd_to_fill;
  42212. + struct fsg_buffhd *next_buffhd_to_drain;
  42213. +
  42214. + int thread_wakeup_needed;
  42215. + struct completion thread_notifier;
  42216. + struct task_struct *thread_task;
  42217. +
  42218. + int cmnd_size;
  42219. + u8 cmnd[MAX_COMMAND_SIZE];
  42220. + enum data_direction data_dir;
  42221. + u32 data_size;
  42222. + u32 data_size_from_cmnd;
  42223. + u32 tag;
  42224. + unsigned int lun;
  42225. + u32 residue;
  42226. + u32 usb_amount_left;
  42227. +
  42228. + /* The CB protocol offers no way for a host to know when a command
  42229. + * has completed. As a result the next command may arrive early,
  42230. + * and we will still have to handle it. For that reason we need
  42231. + * a buffer to store new commands when using CB (or CBI, which
  42232. + * does not oblige a host to wait for command completion either). */
  42233. + int cbbuf_cmnd_size;
  42234. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  42235. +
  42236. + unsigned int nluns;
  42237. + struct fsg_lun *luns;
  42238. + struct fsg_lun *curlun;
  42239. + /* Must be the last entry */
  42240. + struct fsg_buffhd buffhds[];
  42241. +};
  42242. +
  42243. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  42244. +
  42245. +static int exception_in_progress(struct fsg_dev *fsg)
  42246. +{
  42247. + return (fsg->state > FSG_STATE_IDLE);
  42248. +}
  42249. +
  42250. +/* Make bulk-out requests be divisible by the maxpacket size */
  42251. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  42252. + struct fsg_buffhd *bh, unsigned int length)
  42253. +{
  42254. + unsigned int rem;
  42255. +
  42256. + bh->bulk_out_intended_length = length;
  42257. + rem = length % fsg->bulk_out_maxpacket;
  42258. + if (rem > 0)
  42259. + length += fsg->bulk_out_maxpacket - rem;
  42260. + bh->outreq->length = length;
  42261. +}
  42262. +
  42263. +static struct fsg_dev *the_fsg;
  42264. +static struct usb_gadget_driver fsg_driver;
  42265. +
  42266. +
  42267. +/*-------------------------------------------------------------------------*/
  42268. +
  42269. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  42270. +{
  42271. + const char *name;
  42272. +
  42273. + if (ep == fsg->bulk_in)
  42274. + name = "bulk-in";
  42275. + else if (ep == fsg->bulk_out)
  42276. + name = "bulk-out";
  42277. + else
  42278. + name = ep->name;
  42279. + DBG(fsg, "%s set halt\n", name);
  42280. + return usb_ep_set_halt(ep);
  42281. +}
  42282. +
  42283. +
  42284. +/*-------------------------------------------------------------------------*/
  42285. +
  42286. +/*
  42287. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  42288. + * descriptors are built on demand. Also the (static) config and interface
  42289. + * descriptors are adjusted during fsg_bind().
  42290. + */
  42291. +
  42292. +/* There is only one configuration. */
  42293. +#define CONFIG_VALUE 1
  42294. +
  42295. +static struct usb_device_descriptor
  42296. +device_desc = {
  42297. + .bLength = sizeof device_desc,
  42298. + .bDescriptorType = USB_DT_DEVICE,
  42299. +
  42300. + .bcdUSB = cpu_to_le16(0x0200),
  42301. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  42302. +
  42303. + /* The next three values can be overridden by module parameters */
  42304. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  42305. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  42306. + .bcdDevice = cpu_to_le16(0xffff),
  42307. +
  42308. + .iManufacturer = FSG_STRING_MANUFACTURER,
  42309. + .iProduct = FSG_STRING_PRODUCT,
  42310. + .iSerialNumber = FSG_STRING_SERIAL,
  42311. + .bNumConfigurations = 1,
  42312. +};
  42313. +
  42314. +static struct usb_config_descriptor
  42315. +config_desc = {
  42316. + .bLength = sizeof config_desc,
  42317. + .bDescriptorType = USB_DT_CONFIG,
  42318. +
  42319. + /* wTotalLength computed by usb_gadget_config_buf() */
  42320. + .bNumInterfaces = 1,
  42321. + .bConfigurationValue = CONFIG_VALUE,
  42322. + .iConfiguration = FSG_STRING_CONFIG,
  42323. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  42324. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  42325. +};
  42326. +
  42327. +
  42328. +static struct usb_qualifier_descriptor
  42329. +dev_qualifier = {
  42330. + .bLength = sizeof dev_qualifier,
  42331. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  42332. +
  42333. + .bcdUSB = cpu_to_le16(0x0200),
  42334. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  42335. +
  42336. + .bNumConfigurations = 1,
  42337. +};
  42338. +
  42339. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  42340. +{
  42341. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  42342. + buf += USB_DT_BOS_SIZE;
  42343. +
  42344. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  42345. + buf += USB_DT_USB_EXT_CAP_SIZE;
  42346. +
  42347. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  42348. +
  42349. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  42350. + + USB_DT_USB_EXT_CAP_SIZE;
  42351. +}
  42352. +
  42353. +/*
  42354. + * Config descriptors must agree with the code that sets configurations
  42355. + * and with code managing interfaces and their altsettings. They must
  42356. + * also handle different speeds and other-speed requests.
  42357. + */
  42358. +static int populate_config_buf(struct usb_gadget *gadget,
  42359. + u8 *buf, u8 type, unsigned index)
  42360. +{
  42361. + enum usb_device_speed speed = gadget->speed;
  42362. + int len;
  42363. + const struct usb_descriptor_header **function;
  42364. +
  42365. + if (index > 0)
  42366. + return -EINVAL;
  42367. +
  42368. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  42369. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  42370. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  42371. + ? (const struct usb_descriptor_header **)fsg_hs_function
  42372. + : (const struct usb_descriptor_header **)fsg_fs_function;
  42373. +
  42374. + /* for now, don't advertise srp-only devices */
  42375. + if (!gadget_is_otg(gadget))
  42376. + function++;
  42377. +
  42378. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  42379. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  42380. + return len;
  42381. +}
  42382. +
  42383. +
  42384. +/*-------------------------------------------------------------------------*/
  42385. +
  42386. +/* These routines may be called in process context or in_irq */
  42387. +
  42388. +/* Caller must hold fsg->lock */
  42389. +static void wakeup_thread(struct fsg_dev *fsg)
  42390. +{
  42391. + /* Tell the main thread that something has happened */
  42392. + fsg->thread_wakeup_needed = 1;
  42393. + if (fsg->thread_task)
  42394. + wake_up_process(fsg->thread_task);
  42395. +}
  42396. +
  42397. +
  42398. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  42399. +{
  42400. + unsigned long flags;
  42401. +
  42402. + /* Do nothing if a higher-priority exception is already in progress.
  42403. + * If a lower-or-equal priority exception is in progress, preempt it
  42404. + * and notify the main thread by sending it a signal. */
  42405. + spin_lock_irqsave(&fsg->lock, flags);
  42406. + if (fsg->state <= new_state) {
  42407. + fsg->exception_req_tag = fsg->ep0_req_tag;
  42408. + fsg->state = new_state;
  42409. + if (fsg->thread_task)
  42410. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  42411. + fsg->thread_task);
  42412. + }
  42413. + spin_unlock_irqrestore(&fsg->lock, flags);
  42414. +}
  42415. +
  42416. +
  42417. +/*-------------------------------------------------------------------------*/
  42418. +
  42419. +/* The disconnect callback and ep0 routines. These always run in_irq,
  42420. + * except that ep0_queue() is called in the main thread to acknowledge
  42421. + * completion of various requests: set config, set interface, and
  42422. + * Bulk-only device reset. */
  42423. +
  42424. +static void fsg_disconnect(struct usb_gadget *gadget)
  42425. +{
  42426. + struct fsg_dev *fsg = get_gadget_data(gadget);
  42427. +
  42428. + DBG(fsg, "disconnect or port reset\n");
  42429. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  42430. +}
  42431. +
  42432. +
  42433. +static int ep0_queue(struct fsg_dev *fsg)
  42434. +{
  42435. + int rc;
  42436. +
  42437. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  42438. + if (rc != 0 && rc != -ESHUTDOWN) {
  42439. +
  42440. + /* We can't do much more than wait for a reset */
  42441. + WARNING(fsg, "error in submission: %s --> %d\n",
  42442. + fsg->ep0->name, rc);
  42443. + }
  42444. + return rc;
  42445. +}
  42446. +
  42447. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  42448. +{
  42449. + struct fsg_dev *fsg = ep->driver_data;
  42450. +
  42451. + if (req->actual > 0)
  42452. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  42453. + if (req->status || req->actual != req->length)
  42454. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  42455. + req->status, req->actual, req->length);
  42456. + if (req->status == -ECONNRESET) // Request was cancelled
  42457. + usb_ep_fifo_flush(ep);
  42458. +
  42459. + if (req->status == 0 && req->context)
  42460. + ((fsg_routine_t) (req->context))(fsg);
  42461. +}
  42462. +
  42463. +
  42464. +/*-------------------------------------------------------------------------*/
  42465. +
  42466. +/* Bulk and interrupt endpoint completion handlers.
  42467. + * These always run in_irq. */
  42468. +
  42469. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  42470. +{
  42471. + struct fsg_dev *fsg = ep->driver_data;
  42472. + struct fsg_buffhd *bh = req->context;
  42473. +
  42474. + if (req->status || req->actual != req->length)
  42475. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  42476. + req->status, req->actual, req->length);
  42477. + if (req->status == -ECONNRESET) // Request was cancelled
  42478. + usb_ep_fifo_flush(ep);
  42479. +
  42480. + /* Hold the lock while we update the request and buffer states */
  42481. + smp_wmb();
  42482. + spin_lock(&fsg->lock);
  42483. + bh->inreq_busy = 0;
  42484. + bh->state = BUF_STATE_EMPTY;
  42485. + wakeup_thread(fsg);
  42486. + spin_unlock(&fsg->lock);
  42487. +}
  42488. +
  42489. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  42490. +{
  42491. + struct fsg_dev *fsg = ep->driver_data;
  42492. + struct fsg_buffhd *bh = req->context;
  42493. +
  42494. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  42495. + if (req->status || req->actual != bh->bulk_out_intended_length)
  42496. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  42497. + req->status, req->actual,
  42498. + bh->bulk_out_intended_length);
  42499. + if (req->status == -ECONNRESET) // Request was cancelled
  42500. + usb_ep_fifo_flush(ep);
  42501. +
  42502. + /* Hold the lock while we update the request and buffer states */
  42503. + smp_wmb();
  42504. + spin_lock(&fsg->lock);
  42505. + bh->outreq_busy = 0;
  42506. + bh->state = BUF_STATE_FULL;
  42507. + wakeup_thread(fsg);
  42508. + spin_unlock(&fsg->lock);
  42509. +}
  42510. +
  42511. +
  42512. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  42513. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  42514. +{
  42515. + struct fsg_dev *fsg = ep->driver_data;
  42516. + struct fsg_buffhd *bh = req->context;
  42517. +
  42518. + if (req->status || req->actual != req->length)
  42519. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  42520. + req->status, req->actual, req->length);
  42521. + if (req->status == -ECONNRESET) // Request was cancelled
  42522. + usb_ep_fifo_flush(ep);
  42523. +
  42524. + /* Hold the lock while we update the request and buffer states */
  42525. + smp_wmb();
  42526. + spin_lock(&fsg->lock);
  42527. + fsg->intreq_busy = 0;
  42528. + bh->state = BUF_STATE_EMPTY;
  42529. + wakeup_thread(fsg);
  42530. + spin_unlock(&fsg->lock);
  42531. +}
  42532. +
  42533. +#else
  42534. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  42535. +{}
  42536. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  42537. +
  42538. +
  42539. +/*-------------------------------------------------------------------------*/
  42540. +
  42541. +/* Ep0 class-specific handlers. These always run in_irq. */
  42542. +
  42543. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  42544. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42545. +{
  42546. + struct usb_request *req = fsg->ep0req;
  42547. + static u8 cbi_reset_cmnd[6] = {
  42548. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  42549. +
  42550. + /* Error in command transfer? */
  42551. + if (req->status || req->length != req->actual ||
  42552. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  42553. +
  42554. + /* Not all controllers allow a protocol stall after
  42555. + * receiving control-out data, but we'll try anyway. */
  42556. + fsg_set_halt(fsg, fsg->ep0);
  42557. + return; // Wait for reset
  42558. + }
  42559. +
  42560. + /* Is it the special reset command? */
  42561. + if (req->actual >= sizeof cbi_reset_cmnd &&
  42562. + memcmp(req->buf, cbi_reset_cmnd,
  42563. + sizeof cbi_reset_cmnd) == 0) {
  42564. +
  42565. + /* Raise an exception to stop the current operation
  42566. + * and reinitialize our state. */
  42567. + DBG(fsg, "cbi reset request\n");
  42568. + raise_exception(fsg, FSG_STATE_RESET);
  42569. + return;
  42570. + }
  42571. +
  42572. + VDBG(fsg, "CB[I] accept device-specific command\n");
  42573. + spin_lock(&fsg->lock);
  42574. +
  42575. + /* Save the command for later */
  42576. + if (fsg->cbbuf_cmnd_size)
  42577. + WARNING(fsg, "CB[I] overwriting previous command\n");
  42578. + fsg->cbbuf_cmnd_size = req->actual;
  42579. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  42580. +
  42581. + wakeup_thread(fsg);
  42582. + spin_unlock(&fsg->lock);
  42583. +}
  42584. +
  42585. +#else
  42586. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  42587. +{}
  42588. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  42589. +
  42590. +
  42591. +static int class_setup_req(struct fsg_dev *fsg,
  42592. + const struct usb_ctrlrequest *ctrl)
  42593. +{
  42594. + struct usb_request *req = fsg->ep0req;
  42595. + int value = -EOPNOTSUPP;
  42596. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  42597. + u16 w_value = le16_to_cpu(ctrl->wValue);
  42598. + u16 w_length = le16_to_cpu(ctrl->wLength);
  42599. +
  42600. + if (!fsg->config)
  42601. + return value;
  42602. +
  42603. + /* Handle Bulk-only class-specific requests */
  42604. + if (transport_is_bbb()) {
  42605. + switch (ctrl->bRequest) {
  42606. +
  42607. + case US_BULK_RESET_REQUEST:
  42608. + if (ctrl->bRequestType != (USB_DIR_OUT |
  42609. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  42610. + break;
  42611. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  42612. + value = -EDOM;
  42613. + break;
  42614. + }
  42615. +
  42616. + /* Raise an exception to stop the current operation
  42617. + * and reinitialize our state. */
  42618. + DBG(fsg, "bulk reset request\n");
  42619. + raise_exception(fsg, FSG_STATE_RESET);
  42620. + value = DELAYED_STATUS;
  42621. + break;
  42622. +
  42623. + case US_BULK_GET_MAX_LUN:
  42624. + if (ctrl->bRequestType != (USB_DIR_IN |
  42625. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  42626. + break;
  42627. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  42628. + value = -EDOM;
  42629. + break;
  42630. + }
  42631. + VDBG(fsg, "get max LUN\n");
  42632. + *(u8 *) req->buf = fsg->nluns - 1;
  42633. + value = 1;
  42634. + break;
  42635. + }
  42636. + }
  42637. +
  42638. + /* Handle CBI class-specific requests */
  42639. + else {
  42640. + switch (ctrl->bRequest) {
  42641. +
  42642. + case USB_CBI_ADSC_REQUEST:
  42643. + if (ctrl->bRequestType != (USB_DIR_OUT |
  42644. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  42645. + break;
  42646. + if (w_index != 0 || w_value != 0) {
  42647. + value = -EDOM;
  42648. + break;
  42649. + }
  42650. + if (w_length > MAX_COMMAND_SIZE) {
  42651. + value = -EOVERFLOW;
  42652. + break;
  42653. + }
  42654. + value = w_length;
  42655. + fsg->ep0req->context = received_cbi_adsc;
  42656. + break;
  42657. + }
  42658. + }
  42659. +
  42660. + if (value == -EOPNOTSUPP)
  42661. + VDBG(fsg,
  42662. + "unknown class-specific control req "
  42663. + "%02x.%02x v%04x i%04x l%u\n",
  42664. + ctrl->bRequestType, ctrl->bRequest,
  42665. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  42666. + return value;
  42667. +}
  42668. +
  42669. +
  42670. +/*-------------------------------------------------------------------------*/
  42671. +
  42672. +/* Ep0 standard request handlers. These always run in_irq. */
  42673. +
  42674. +static int standard_setup_req(struct fsg_dev *fsg,
  42675. + const struct usb_ctrlrequest *ctrl)
  42676. +{
  42677. + struct usb_request *req = fsg->ep0req;
  42678. + int value = -EOPNOTSUPP;
  42679. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  42680. + u16 w_value = le16_to_cpu(ctrl->wValue);
  42681. +
  42682. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  42683. + * but config change events will also reconfigure hardware. */
  42684. + switch (ctrl->bRequest) {
  42685. +
  42686. + case USB_REQ_GET_DESCRIPTOR:
  42687. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  42688. + USB_RECIP_DEVICE))
  42689. + break;
  42690. + switch (w_value >> 8) {
  42691. +
  42692. + case USB_DT_DEVICE:
  42693. + VDBG(fsg, "get device descriptor\n");
  42694. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  42695. + value = sizeof device_desc;
  42696. + memcpy(req->buf, &device_desc, value);
  42697. + break;
  42698. + case USB_DT_DEVICE_QUALIFIER:
  42699. + VDBG(fsg, "get device qualifier\n");
  42700. + if (!gadget_is_dualspeed(fsg->gadget) ||
  42701. + fsg->gadget->speed == USB_SPEED_SUPER)
  42702. + break;
  42703. + /*
  42704. + * Assume ep0 uses the same maxpacket value for both
  42705. + * speeds
  42706. + */
  42707. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  42708. + value = sizeof dev_qualifier;
  42709. + memcpy(req->buf, &dev_qualifier, value);
  42710. + break;
  42711. +
  42712. + case USB_DT_OTHER_SPEED_CONFIG:
  42713. + VDBG(fsg, "get other-speed config descriptor\n");
  42714. + if (!gadget_is_dualspeed(fsg->gadget) ||
  42715. + fsg->gadget->speed == USB_SPEED_SUPER)
  42716. + break;
  42717. + goto get_config;
  42718. + case USB_DT_CONFIG:
  42719. + VDBG(fsg, "get configuration descriptor\n");
  42720. +get_config:
  42721. + value = populate_config_buf(fsg->gadget,
  42722. + req->buf,
  42723. + w_value >> 8,
  42724. + w_value & 0xff);
  42725. + break;
  42726. +
  42727. + case USB_DT_STRING:
  42728. + VDBG(fsg, "get string descriptor\n");
  42729. +
  42730. + /* wIndex == language code */
  42731. + value = usb_gadget_get_string(&fsg_stringtab,
  42732. + w_value & 0xff, req->buf);
  42733. + break;
  42734. +
  42735. + case USB_DT_BOS:
  42736. + VDBG(fsg, "get bos descriptor\n");
  42737. +
  42738. + if (gadget_is_superspeed(fsg->gadget))
  42739. + value = populate_bos(fsg, req->buf);
  42740. + break;
  42741. + }
  42742. +
  42743. + break;
  42744. +
  42745. + /* One config, two speeds */
  42746. + case USB_REQ_SET_CONFIGURATION:
  42747. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  42748. + USB_RECIP_DEVICE))
  42749. + break;
  42750. + VDBG(fsg, "set configuration\n");
  42751. + if (w_value == CONFIG_VALUE || w_value == 0) {
  42752. + fsg->new_config = w_value;
  42753. +
  42754. + /* Raise an exception to wipe out previous transaction
  42755. + * state (queued bufs, etc) and set the new config. */
  42756. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  42757. + value = DELAYED_STATUS;
  42758. + }
  42759. + break;
  42760. + case USB_REQ_GET_CONFIGURATION:
  42761. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  42762. + USB_RECIP_DEVICE))
  42763. + break;
  42764. + VDBG(fsg, "get configuration\n");
  42765. + *(u8 *) req->buf = fsg->config;
  42766. + value = 1;
  42767. + break;
  42768. +
  42769. + case USB_REQ_SET_INTERFACE:
  42770. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  42771. + USB_RECIP_INTERFACE))
  42772. + break;
  42773. + if (fsg->config && w_index == 0) {
  42774. +
  42775. + /* Raise an exception to wipe out previous transaction
  42776. + * state (queued bufs, etc) and install the new
  42777. + * interface altsetting. */
  42778. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  42779. + value = DELAYED_STATUS;
  42780. + }
  42781. + break;
  42782. + case USB_REQ_GET_INTERFACE:
  42783. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  42784. + USB_RECIP_INTERFACE))
  42785. + break;
  42786. + if (!fsg->config)
  42787. + break;
  42788. + if (w_index != 0) {
  42789. + value = -EDOM;
  42790. + break;
  42791. + }
  42792. + VDBG(fsg, "get interface\n");
  42793. + *(u8 *) req->buf = 0;
  42794. + value = 1;
  42795. + break;
  42796. +
  42797. + default:
  42798. + VDBG(fsg,
  42799. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  42800. + ctrl->bRequestType, ctrl->bRequest,
  42801. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  42802. + }
  42803. +
  42804. + return value;
  42805. +}
  42806. +
  42807. +
  42808. +static int fsg_setup(struct usb_gadget *gadget,
  42809. + const struct usb_ctrlrequest *ctrl)
  42810. +{
  42811. + struct fsg_dev *fsg = get_gadget_data(gadget);
  42812. + int rc;
  42813. + int w_length = le16_to_cpu(ctrl->wLength);
  42814. +
  42815. + ++fsg->ep0_req_tag; // Record arrival of a new request
  42816. + fsg->ep0req->context = NULL;
  42817. + fsg->ep0req->length = 0;
  42818. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  42819. +
  42820. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  42821. + rc = class_setup_req(fsg, ctrl);
  42822. + else
  42823. + rc = standard_setup_req(fsg, ctrl);
  42824. +
  42825. + /* Respond with data/status or defer until later? */
  42826. + if (rc >= 0 && rc != DELAYED_STATUS) {
  42827. + rc = min(rc, w_length);
  42828. + fsg->ep0req->length = rc;
  42829. + fsg->ep0req->zero = rc < w_length;
  42830. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  42831. + "ep0-in" : "ep0-out");
  42832. + rc = ep0_queue(fsg);
  42833. + }
  42834. +
  42835. + /* Device either stalls (rc < 0) or reports success */
  42836. + return rc;
  42837. +}
  42838. +
  42839. +
  42840. +/*-------------------------------------------------------------------------*/
  42841. +
  42842. +/* All the following routines run in process context */
  42843. +
  42844. +
  42845. +/* Use this for bulk or interrupt transfers, not ep0 */
  42846. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  42847. + struct usb_request *req, int *pbusy,
  42848. + enum fsg_buffer_state *state)
  42849. +{
  42850. + int rc;
  42851. +
  42852. + if (ep == fsg->bulk_in)
  42853. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  42854. + else if (ep == fsg->intr_in)
  42855. + dump_msg(fsg, "intr-in", req->buf, req->length);
  42856. +
  42857. + spin_lock_irq(&fsg->lock);
  42858. + *pbusy = 1;
  42859. + *state = BUF_STATE_BUSY;
  42860. + spin_unlock_irq(&fsg->lock);
  42861. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  42862. + if (rc != 0) {
  42863. + *pbusy = 0;
  42864. + *state = BUF_STATE_EMPTY;
  42865. +
  42866. + /* We can't do much more than wait for a reset */
  42867. +
  42868. + /* Note: currently the net2280 driver fails zero-length
  42869. + * submissions if DMA is enabled. */
  42870. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  42871. + req->length == 0))
  42872. + WARNING(fsg, "error in submission: %s --> %d\n",
  42873. + ep->name, rc);
  42874. + }
  42875. +}
  42876. +
  42877. +
  42878. +static int sleep_thread(struct fsg_dev *fsg)
  42879. +{
  42880. + int rc = 0;
  42881. +
  42882. + /* Wait until a signal arrives or we are woken up */
  42883. + for (;;) {
  42884. + try_to_freeze();
  42885. + set_current_state(TASK_INTERRUPTIBLE);
  42886. + if (signal_pending(current)) {
  42887. + rc = -EINTR;
  42888. + break;
  42889. + }
  42890. + if (fsg->thread_wakeup_needed)
  42891. + break;
  42892. + schedule();
  42893. + }
  42894. + __set_current_state(TASK_RUNNING);
  42895. + fsg->thread_wakeup_needed = 0;
  42896. + return rc;
  42897. +}
  42898. +
  42899. +
  42900. +/*-------------------------------------------------------------------------*/
  42901. +
  42902. +static int do_read(struct fsg_dev *fsg)
  42903. +{
  42904. + struct fsg_lun *curlun = fsg->curlun;
  42905. + u32 lba;
  42906. + struct fsg_buffhd *bh;
  42907. + int rc;
  42908. + u32 amount_left;
  42909. + loff_t file_offset, file_offset_tmp;
  42910. + unsigned int amount;
  42911. + ssize_t nread;
  42912. +
  42913. + /* Get the starting Logical Block Address and check that it's
  42914. + * not too big */
  42915. + if (fsg->cmnd[0] == READ_6)
  42916. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  42917. + else {
  42918. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  42919. +
  42920. + /* We allow DPO (Disable Page Out = don't save data in the
  42921. + * cache) and FUA (Force Unit Access = don't read from the
  42922. + * cache), but we don't implement them. */
  42923. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  42924. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  42925. + return -EINVAL;
  42926. + }
  42927. + }
  42928. + if (lba >= curlun->num_sectors) {
  42929. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42930. + return -EINVAL;
  42931. + }
  42932. + file_offset = ((loff_t) lba) << curlun->blkbits;
  42933. +
  42934. + /* Carry out the file reads */
  42935. + amount_left = fsg->data_size_from_cmnd;
  42936. + if (unlikely(amount_left == 0))
  42937. + return -EIO; // No default reply
  42938. +
  42939. + for (;;) {
  42940. +
  42941. + /* Figure out how much we need to read:
  42942. + * Try to read the remaining amount.
  42943. + * But don't read more than the buffer size.
  42944. + * And don't try to read past the end of the file.
  42945. + */
  42946. + amount = min((unsigned int) amount_left, mod_data.buflen);
  42947. + amount = min((loff_t) amount,
  42948. + curlun->file_length - file_offset);
  42949. +
  42950. + /* Wait for the next buffer to become available */
  42951. + bh = fsg->next_buffhd_to_fill;
  42952. + while (bh->state != BUF_STATE_EMPTY) {
  42953. + rc = sleep_thread(fsg);
  42954. + if (rc)
  42955. + return rc;
  42956. + }
  42957. +
  42958. + /* If we were asked to read past the end of file,
  42959. + * end with an empty buffer. */
  42960. + if (amount == 0) {
  42961. + curlun->sense_data =
  42962. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  42963. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  42964. + curlun->info_valid = 1;
  42965. + bh->inreq->length = 0;
  42966. + bh->state = BUF_STATE_FULL;
  42967. + break;
  42968. + }
  42969. +
  42970. + /* Perform the read */
  42971. + file_offset_tmp = file_offset;
  42972. + nread = vfs_read(curlun->filp,
  42973. + (char __user *) bh->buf,
  42974. + amount, &file_offset_tmp);
  42975. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  42976. + (unsigned long long) file_offset,
  42977. + (int) nread);
  42978. + if (signal_pending(current))
  42979. + return -EINTR;
  42980. +
  42981. + if (nread < 0) {
  42982. + LDBG(curlun, "error in file read: %d\n",
  42983. + (int) nread);
  42984. + nread = 0;
  42985. + } else if (nread < amount) {
  42986. + LDBG(curlun, "partial file read: %d/%u\n",
  42987. + (int) nread, amount);
  42988. + nread = round_down(nread, curlun->blksize);
  42989. + }
  42990. + file_offset += nread;
  42991. + amount_left -= nread;
  42992. + fsg->residue -= nread;
  42993. +
  42994. + /* Except at the end of the transfer, nread will be
  42995. + * equal to the buffer size, which is divisible by the
  42996. + * bulk-in maxpacket size.
  42997. + */
  42998. + bh->inreq->length = nread;
  42999. + bh->state = BUF_STATE_FULL;
  43000. +
  43001. + /* If an error occurred, report it and its position */
  43002. + if (nread < amount) {
  43003. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  43004. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  43005. + curlun->info_valid = 1;
  43006. + break;
  43007. + }
  43008. +
  43009. + if (amount_left == 0)
  43010. + break; // No more left to read
  43011. +
  43012. + /* Send this buffer and go read some more */
  43013. + bh->inreq->zero = 0;
  43014. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  43015. + &bh->inreq_busy, &bh->state);
  43016. + fsg->next_buffhd_to_fill = bh->next;
  43017. + }
  43018. +
  43019. + return -EIO; // No default reply
  43020. +}
  43021. +
  43022. +
  43023. +/*-------------------------------------------------------------------------*/
  43024. +
  43025. +static int do_write(struct fsg_dev *fsg)
  43026. +{
  43027. + struct fsg_lun *curlun = fsg->curlun;
  43028. + u32 lba;
  43029. + struct fsg_buffhd *bh;
  43030. + int get_some_more;
  43031. + u32 amount_left_to_req, amount_left_to_write;
  43032. + loff_t usb_offset, file_offset, file_offset_tmp;
  43033. + unsigned int amount;
  43034. + ssize_t nwritten;
  43035. + int rc;
  43036. +
  43037. + if (curlun->ro) {
  43038. + curlun->sense_data = SS_WRITE_PROTECTED;
  43039. + return -EINVAL;
  43040. + }
  43041. + spin_lock(&curlun->filp->f_lock);
  43042. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  43043. + spin_unlock(&curlun->filp->f_lock);
  43044. +
  43045. + /* Get the starting Logical Block Address and check that it's
  43046. + * not too big */
  43047. + if (fsg->cmnd[0] == WRITE_6)
  43048. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  43049. + else {
  43050. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  43051. +
  43052. + /* We allow DPO (Disable Page Out = don't save data in the
  43053. + * cache) and FUA (Force Unit Access = write directly to the
  43054. + * medium). We don't implement DPO; we implement FUA by
  43055. + * performing synchronous output. */
  43056. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  43057. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43058. + return -EINVAL;
  43059. + }
  43060. + /* FUA */
  43061. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  43062. + spin_lock(&curlun->filp->f_lock);
  43063. + curlun->filp->f_flags |= O_DSYNC;
  43064. + spin_unlock(&curlun->filp->f_lock);
  43065. + }
  43066. + }
  43067. + if (lba >= curlun->num_sectors) {
  43068. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  43069. + return -EINVAL;
  43070. + }
  43071. +
  43072. + /* Carry out the file writes */
  43073. + get_some_more = 1;
  43074. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  43075. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  43076. +
  43077. + while (amount_left_to_write > 0) {
  43078. +
  43079. + /* Queue a request for more data from the host */
  43080. + bh = fsg->next_buffhd_to_fill;
  43081. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  43082. +
  43083. + /* Figure out how much we want to get:
  43084. + * Try to get the remaining amount,
  43085. + * but not more than the buffer size.
  43086. + */
  43087. + amount = min(amount_left_to_req, mod_data.buflen);
  43088. +
  43089. + /* Beyond the end of the backing file? */
  43090. + if (usb_offset >= curlun->file_length) {
  43091. + get_some_more = 0;
  43092. + curlun->sense_data =
  43093. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  43094. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  43095. + curlun->info_valid = 1;
  43096. + continue;
  43097. + }
  43098. +
  43099. + /* Get the next buffer */
  43100. + usb_offset += amount;
  43101. + fsg->usb_amount_left -= amount;
  43102. + amount_left_to_req -= amount;
  43103. + if (amount_left_to_req == 0)
  43104. + get_some_more = 0;
  43105. +
  43106. + /* Except at the end of the transfer, amount will be
  43107. + * equal to the buffer size, which is divisible by
  43108. + * the bulk-out maxpacket size.
  43109. + */
  43110. + set_bulk_out_req_length(fsg, bh, amount);
  43111. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  43112. + &bh->outreq_busy, &bh->state);
  43113. + fsg->next_buffhd_to_fill = bh->next;
  43114. + continue;
  43115. + }
  43116. +
  43117. + /* Write the received data to the backing file */
  43118. + bh = fsg->next_buffhd_to_drain;
  43119. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  43120. + break; // We stopped early
  43121. + if (bh->state == BUF_STATE_FULL) {
  43122. + smp_rmb();
  43123. + fsg->next_buffhd_to_drain = bh->next;
  43124. + bh->state = BUF_STATE_EMPTY;
  43125. +
  43126. + /* Did something go wrong with the transfer? */
  43127. + if (bh->outreq->status != 0) {
  43128. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  43129. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  43130. + curlun->info_valid = 1;
  43131. + break;
  43132. + }
  43133. +
  43134. + amount = bh->outreq->actual;
  43135. + if (curlun->file_length - file_offset < amount) {
  43136. + LERROR(curlun,
  43137. + "write %u @ %llu beyond end %llu\n",
  43138. + amount, (unsigned long long) file_offset,
  43139. + (unsigned long long) curlun->file_length);
  43140. + amount = curlun->file_length - file_offset;
  43141. + }
  43142. +
  43143. + /* Don't accept excess data. The spec doesn't say
  43144. + * what to do in this case. We'll ignore the error.
  43145. + */
  43146. + amount = min(amount, bh->bulk_out_intended_length);
  43147. +
  43148. + /* Don't write a partial block */
  43149. + amount = round_down(amount, curlun->blksize);
  43150. + if (amount == 0)
  43151. + goto empty_write;
  43152. +
  43153. + /* Perform the write */
  43154. + file_offset_tmp = file_offset;
  43155. + nwritten = vfs_write(curlun->filp,
  43156. + (char __user *) bh->buf,
  43157. + amount, &file_offset_tmp);
  43158. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  43159. + (unsigned long long) file_offset,
  43160. + (int) nwritten);
  43161. + if (signal_pending(current))
  43162. + return -EINTR; // Interrupted!
  43163. +
  43164. + if (nwritten < 0) {
  43165. + LDBG(curlun, "error in file write: %d\n",
  43166. + (int) nwritten);
  43167. + nwritten = 0;
  43168. + } else if (nwritten < amount) {
  43169. + LDBG(curlun, "partial file write: %d/%u\n",
  43170. + (int) nwritten, amount);
  43171. + nwritten = round_down(nwritten, curlun->blksize);
  43172. + }
  43173. + file_offset += nwritten;
  43174. + amount_left_to_write -= nwritten;
  43175. + fsg->residue -= nwritten;
  43176. +
  43177. + /* If an error occurred, report it and its position */
  43178. + if (nwritten < amount) {
  43179. + curlun->sense_data = SS_WRITE_ERROR;
  43180. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  43181. + curlun->info_valid = 1;
  43182. + break;
  43183. + }
  43184. +
  43185. + empty_write:
  43186. + /* Did the host decide to stop early? */
  43187. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  43188. + fsg->short_packet_received = 1;
  43189. + break;
  43190. + }
  43191. + continue;
  43192. + }
  43193. +
  43194. + /* Wait for something to happen */
  43195. + rc = sleep_thread(fsg);
  43196. + if (rc)
  43197. + return rc;
  43198. + }
  43199. +
  43200. + return -EIO; // No default reply
  43201. +}
  43202. +
  43203. +
  43204. +/*-------------------------------------------------------------------------*/
  43205. +
  43206. +static int do_synchronize_cache(struct fsg_dev *fsg)
  43207. +{
  43208. + struct fsg_lun *curlun = fsg->curlun;
  43209. + int rc;
  43210. +
  43211. + /* We ignore the requested LBA and write out all file's
  43212. + * dirty data buffers. */
  43213. + rc = fsg_lun_fsync_sub(curlun);
  43214. + if (rc)
  43215. + curlun->sense_data = SS_WRITE_ERROR;
  43216. + return 0;
  43217. +}
  43218. +
  43219. +
  43220. +/*-------------------------------------------------------------------------*/
  43221. +
  43222. +static void invalidate_sub(struct fsg_lun *curlun)
  43223. +{
  43224. + struct file *filp = curlun->filp;
  43225. + struct inode *inode = filp->f_path.dentry->d_inode;
  43226. + unsigned long rc;
  43227. +
  43228. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  43229. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  43230. +}
  43231. +
  43232. +static int do_verify(struct fsg_dev *fsg)
  43233. +{
  43234. + struct fsg_lun *curlun = fsg->curlun;
  43235. + u32 lba;
  43236. + u32 verification_length;
  43237. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  43238. + loff_t file_offset, file_offset_tmp;
  43239. + u32 amount_left;
  43240. + unsigned int amount;
  43241. + ssize_t nread;
  43242. +
  43243. + /* Get the starting Logical Block Address and check that it's
  43244. + * not too big */
  43245. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  43246. + if (lba >= curlun->num_sectors) {
  43247. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  43248. + return -EINVAL;
  43249. + }
  43250. +
  43251. + /* We allow DPO (Disable Page Out = don't save data in the
  43252. + * cache) but we don't implement it. */
  43253. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  43254. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43255. + return -EINVAL;
  43256. + }
  43257. +
  43258. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  43259. + if (unlikely(verification_length == 0))
  43260. + return -EIO; // No default reply
  43261. +
  43262. + /* Prepare to carry out the file verify */
  43263. + amount_left = verification_length << curlun->blkbits;
  43264. + file_offset = ((loff_t) lba) << curlun->blkbits;
  43265. +
  43266. + /* Write out all the dirty buffers before invalidating them */
  43267. + fsg_lun_fsync_sub(curlun);
  43268. + if (signal_pending(current))
  43269. + return -EINTR;
  43270. +
  43271. + invalidate_sub(curlun);
  43272. + if (signal_pending(current))
  43273. + return -EINTR;
  43274. +
  43275. + /* Just try to read the requested blocks */
  43276. + while (amount_left > 0) {
  43277. +
  43278. + /* Figure out how much we need to read:
  43279. + * Try to read the remaining amount, but not more than
  43280. + * the buffer size.
  43281. + * And don't try to read past the end of the file.
  43282. + */
  43283. + amount = min((unsigned int) amount_left, mod_data.buflen);
  43284. + amount = min((loff_t) amount,
  43285. + curlun->file_length - file_offset);
  43286. + if (amount == 0) {
  43287. + curlun->sense_data =
  43288. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  43289. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  43290. + curlun->info_valid = 1;
  43291. + break;
  43292. + }
  43293. +
  43294. + /* Perform the read */
  43295. + file_offset_tmp = file_offset;
  43296. + nread = vfs_read(curlun->filp,
  43297. + (char __user *) bh->buf,
  43298. + amount, &file_offset_tmp);
  43299. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  43300. + (unsigned long long) file_offset,
  43301. + (int) nread);
  43302. + if (signal_pending(current))
  43303. + return -EINTR;
  43304. +
  43305. + if (nread < 0) {
  43306. + LDBG(curlun, "error in file verify: %d\n",
  43307. + (int) nread);
  43308. + nread = 0;
  43309. + } else if (nread < amount) {
  43310. + LDBG(curlun, "partial file verify: %d/%u\n",
  43311. + (int) nread, amount);
  43312. + nread = round_down(nread, curlun->blksize);
  43313. + }
  43314. + if (nread == 0) {
  43315. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  43316. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  43317. + curlun->info_valid = 1;
  43318. + break;
  43319. + }
  43320. + file_offset += nread;
  43321. + amount_left -= nread;
  43322. + }
  43323. + return 0;
  43324. +}
  43325. +
  43326. +
  43327. +/*-------------------------------------------------------------------------*/
  43328. +
  43329. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43330. +{
  43331. + u8 *buf = (u8 *) bh->buf;
  43332. +
  43333. + static char vendor_id[] = "Linux ";
  43334. + static char product_disk_id[] = "File-Stor Gadget";
  43335. + static char product_cdrom_id[] = "File-CD Gadget ";
  43336. +
  43337. + if (!fsg->curlun) { // Unsupported LUNs are okay
  43338. + fsg->bad_lun_okay = 1;
  43339. + memset(buf, 0, 36);
  43340. + buf[0] = 0x7f; // Unsupported, no device-type
  43341. + buf[4] = 31; // Additional length
  43342. + return 36;
  43343. + }
  43344. +
  43345. + memset(buf, 0, 8);
  43346. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  43347. + if (mod_data.removable)
  43348. + buf[1] = 0x80;
  43349. + buf[2] = 2; // ANSI SCSI level 2
  43350. + buf[3] = 2; // SCSI-2 INQUIRY data format
  43351. + buf[4] = 31; // Additional length
  43352. + // No special options
  43353. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  43354. + (mod_data.cdrom ? product_cdrom_id :
  43355. + product_disk_id),
  43356. + mod_data.release);
  43357. + return 36;
  43358. +}
  43359. +
  43360. +
  43361. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43362. +{
  43363. + struct fsg_lun *curlun = fsg->curlun;
  43364. + u8 *buf = (u8 *) bh->buf;
  43365. + u32 sd, sdinfo;
  43366. + int valid;
  43367. +
  43368. + /*
  43369. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  43370. + *
  43371. + * If a REQUEST SENSE command is received from an initiator
  43372. + * with a pending unit attention condition (before the target
  43373. + * generates the contingent allegiance condition), then the
  43374. + * target shall either:
  43375. + * a) report any pending sense data and preserve the unit
  43376. + * attention condition on the logical unit, or,
  43377. + * b) report the unit attention condition, may discard any
  43378. + * pending sense data, and clear the unit attention
  43379. + * condition on the logical unit for that initiator.
  43380. + *
  43381. + * FSG normally uses option a); enable this code to use option b).
  43382. + */
  43383. +#if 0
  43384. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  43385. + curlun->sense_data = curlun->unit_attention_data;
  43386. + curlun->unit_attention_data = SS_NO_SENSE;
  43387. + }
  43388. +#endif
  43389. +
  43390. + if (!curlun) { // Unsupported LUNs are okay
  43391. + fsg->bad_lun_okay = 1;
  43392. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  43393. + sdinfo = 0;
  43394. + valid = 0;
  43395. + } else {
  43396. + sd = curlun->sense_data;
  43397. + sdinfo = curlun->sense_data_info;
  43398. + valid = curlun->info_valid << 7;
  43399. + curlun->sense_data = SS_NO_SENSE;
  43400. + curlun->sense_data_info = 0;
  43401. + curlun->info_valid = 0;
  43402. + }
  43403. +
  43404. + memset(buf, 0, 18);
  43405. + buf[0] = valid | 0x70; // Valid, current error
  43406. + buf[2] = SK(sd);
  43407. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  43408. + buf[7] = 18 - 8; // Additional sense length
  43409. + buf[12] = ASC(sd);
  43410. + buf[13] = ASCQ(sd);
  43411. + return 18;
  43412. +}
  43413. +
  43414. +
  43415. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43416. +{
  43417. + struct fsg_lun *curlun = fsg->curlun;
  43418. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  43419. + int pmi = fsg->cmnd[8];
  43420. + u8 *buf = (u8 *) bh->buf;
  43421. +
  43422. + /* Check the PMI and LBA fields */
  43423. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  43424. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43425. + return -EINVAL;
  43426. + }
  43427. +
  43428. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  43429. + /* Max logical block */
  43430. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  43431. + return 8;
  43432. +}
  43433. +
  43434. +
  43435. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43436. +{
  43437. + struct fsg_lun *curlun = fsg->curlun;
  43438. + int msf = fsg->cmnd[1] & 0x02;
  43439. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  43440. + u8 *buf = (u8 *) bh->buf;
  43441. +
  43442. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  43443. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43444. + return -EINVAL;
  43445. + }
  43446. + if (lba >= curlun->num_sectors) {
  43447. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  43448. + return -EINVAL;
  43449. + }
  43450. +
  43451. + memset(buf, 0, 8);
  43452. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  43453. + store_cdrom_address(&buf[4], msf, lba);
  43454. + return 8;
  43455. +}
  43456. +
  43457. +
  43458. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43459. +{
  43460. + struct fsg_lun *curlun = fsg->curlun;
  43461. + int msf = fsg->cmnd[1] & 0x02;
  43462. + int start_track = fsg->cmnd[6];
  43463. + u8 *buf = (u8 *) bh->buf;
  43464. +
  43465. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  43466. + start_track > 1) {
  43467. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43468. + return -EINVAL;
  43469. + }
  43470. +
  43471. + memset(buf, 0, 20);
  43472. + buf[1] = (20-2); /* TOC data length */
  43473. + buf[2] = 1; /* First track number */
  43474. + buf[3] = 1; /* Last track number */
  43475. + buf[5] = 0x16; /* Data track, copying allowed */
  43476. + buf[6] = 0x01; /* Only track is number 1 */
  43477. + store_cdrom_address(&buf[8], msf, 0);
  43478. +
  43479. + buf[13] = 0x16; /* Lead-out track is data */
  43480. + buf[14] = 0xAA; /* Lead-out track number */
  43481. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  43482. + return 20;
  43483. +}
  43484. +
  43485. +
  43486. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43487. +{
  43488. + struct fsg_lun *curlun = fsg->curlun;
  43489. + int mscmnd = fsg->cmnd[0];
  43490. + u8 *buf = (u8 *) bh->buf;
  43491. + u8 *buf0 = buf;
  43492. + int pc, page_code;
  43493. + int changeable_values, all_pages;
  43494. + int valid_page = 0;
  43495. + int len, limit;
  43496. +
  43497. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  43498. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43499. + return -EINVAL;
  43500. + }
  43501. + pc = fsg->cmnd[2] >> 6;
  43502. + page_code = fsg->cmnd[2] & 0x3f;
  43503. + if (pc == 3) {
  43504. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  43505. + return -EINVAL;
  43506. + }
  43507. + changeable_values = (pc == 1);
  43508. + all_pages = (page_code == 0x3f);
  43509. +
  43510. + /* Write the mode parameter header. Fixed values are: default
  43511. + * medium type, no cache control (DPOFUA), and no block descriptors.
  43512. + * The only variable value is the WriteProtect bit. We will fill in
  43513. + * the mode data length later. */
  43514. + memset(buf, 0, 8);
  43515. + if (mscmnd == MODE_SENSE) {
  43516. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  43517. + buf += 4;
  43518. + limit = 255;
  43519. + } else { // MODE_SENSE_10
  43520. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  43521. + buf += 8;
  43522. + limit = 65535; // Should really be mod_data.buflen
  43523. + }
  43524. +
  43525. + /* No block descriptors */
  43526. +
  43527. + /* The mode pages, in numerical order. The only page we support
  43528. + * is the Caching page. */
  43529. + if (page_code == 0x08 || all_pages) {
  43530. + valid_page = 1;
  43531. + buf[0] = 0x08; // Page code
  43532. + buf[1] = 10; // Page length
  43533. + memset(buf+2, 0, 10); // None of the fields are changeable
  43534. +
  43535. + if (!changeable_values) {
  43536. + buf[2] = 0x04; // Write cache enable,
  43537. + // Read cache not disabled
  43538. + // No cache retention priorities
  43539. + put_unaligned_be16(0xffff, &buf[4]);
  43540. + /* Don't disable prefetch */
  43541. + /* Minimum prefetch = 0 */
  43542. + put_unaligned_be16(0xffff, &buf[8]);
  43543. + /* Maximum prefetch */
  43544. + put_unaligned_be16(0xffff, &buf[10]);
  43545. + /* Maximum prefetch ceiling */
  43546. + }
  43547. + buf += 12;
  43548. + }
  43549. +
  43550. + /* Check that a valid page was requested and the mode data length
  43551. + * isn't too long. */
  43552. + len = buf - buf0;
  43553. + if (!valid_page || len > limit) {
  43554. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43555. + return -EINVAL;
  43556. + }
  43557. +
  43558. + /* Store the mode data length */
  43559. + if (mscmnd == MODE_SENSE)
  43560. + buf0[0] = len - 1;
  43561. + else
  43562. + put_unaligned_be16(len - 2, buf0);
  43563. + return len;
  43564. +}
  43565. +
  43566. +
  43567. +static int do_start_stop(struct fsg_dev *fsg)
  43568. +{
  43569. + struct fsg_lun *curlun = fsg->curlun;
  43570. + int loej, start;
  43571. +
  43572. + if (!mod_data.removable) {
  43573. + curlun->sense_data = SS_INVALID_COMMAND;
  43574. + return -EINVAL;
  43575. + }
  43576. +
  43577. + // int immed = fsg->cmnd[1] & 0x01;
  43578. + loej = fsg->cmnd[4] & 0x02;
  43579. + start = fsg->cmnd[4] & 0x01;
  43580. +
  43581. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  43582. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  43583. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  43584. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43585. + return -EINVAL;
  43586. + }
  43587. +
  43588. + if (!start) {
  43589. +
  43590. + /* Are we allowed to unload the media? */
  43591. + if (curlun->prevent_medium_removal) {
  43592. + LDBG(curlun, "unload attempt prevented\n");
  43593. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  43594. + return -EINVAL;
  43595. + }
  43596. + if (loej) { // Simulate an unload/eject
  43597. + up_read(&fsg->filesem);
  43598. + down_write(&fsg->filesem);
  43599. + fsg_lun_close(curlun);
  43600. + up_write(&fsg->filesem);
  43601. + down_read(&fsg->filesem);
  43602. + }
  43603. + } else {
  43604. +
  43605. + /* Our emulation doesn't support mounting; the medium is
  43606. + * available for use as soon as it is loaded. */
  43607. + if (!fsg_lun_is_open(curlun)) {
  43608. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  43609. + return -EINVAL;
  43610. + }
  43611. + }
  43612. +#endif
  43613. + return 0;
  43614. +}
  43615. +
  43616. +
  43617. +static int do_prevent_allow(struct fsg_dev *fsg)
  43618. +{
  43619. + struct fsg_lun *curlun = fsg->curlun;
  43620. + int prevent;
  43621. +
  43622. + if (!mod_data.removable) {
  43623. + curlun->sense_data = SS_INVALID_COMMAND;
  43624. + return -EINVAL;
  43625. + }
  43626. +
  43627. + prevent = fsg->cmnd[4] & 0x01;
  43628. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  43629. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  43630. + return -EINVAL;
  43631. + }
  43632. +
  43633. + if (curlun->prevent_medium_removal && !prevent)
  43634. + fsg_lun_fsync_sub(curlun);
  43635. + curlun->prevent_medium_removal = prevent;
  43636. + return 0;
  43637. +}
  43638. +
  43639. +
  43640. +static int do_read_format_capacities(struct fsg_dev *fsg,
  43641. + struct fsg_buffhd *bh)
  43642. +{
  43643. + struct fsg_lun *curlun = fsg->curlun;
  43644. + u8 *buf = (u8 *) bh->buf;
  43645. +
  43646. + buf[0] = buf[1] = buf[2] = 0;
  43647. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  43648. + buf += 4;
  43649. +
  43650. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  43651. + /* Number of blocks */
  43652. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  43653. + buf[4] = 0x02; /* Current capacity */
  43654. + return 12;
  43655. +}
  43656. +
  43657. +
  43658. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  43659. +{
  43660. + struct fsg_lun *curlun = fsg->curlun;
  43661. +
  43662. + /* We don't support MODE SELECT */
  43663. + curlun->sense_data = SS_INVALID_COMMAND;
  43664. + return -EINVAL;
  43665. +}
  43666. +
  43667. +
  43668. +/*-------------------------------------------------------------------------*/
  43669. +
  43670. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  43671. +{
  43672. + int rc;
  43673. +
  43674. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  43675. + if (rc == -EAGAIN)
  43676. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  43677. + while (rc != 0) {
  43678. + if (rc != -EAGAIN) {
  43679. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  43680. + rc = 0;
  43681. + break;
  43682. + }
  43683. +
  43684. + /* Wait for a short time and then try again */
  43685. + if (msleep_interruptible(100) != 0)
  43686. + return -EINTR;
  43687. + rc = usb_ep_set_halt(fsg->bulk_in);
  43688. + }
  43689. + return rc;
  43690. +}
  43691. +
  43692. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  43693. +{
  43694. + int rc;
  43695. +
  43696. + DBG(fsg, "bulk-in set wedge\n");
  43697. + rc = usb_ep_set_wedge(fsg->bulk_in);
  43698. + if (rc == -EAGAIN)
  43699. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  43700. + while (rc != 0) {
  43701. + if (rc != -EAGAIN) {
  43702. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  43703. + rc = 0;
  43704. + break;
  43705. + }
  43706. +
  43707. + /* Wait for a short time and then try again */
  43708. + if (msleep_interruptible(100) != 0)
  43709. + return -EINTR;
  43710. + rc = usb_ep_set_wedge(fsg->bulk_in);
  43711. + }
  43712. + return rc;
  43713. +}
  43714. +
  43715. +static int throw_away_data(struct fsg_dev *fsg)
  43716. +{
  43717. + struct fsg_buffhd *bh;
  43718. + u32 amount;
  43719. + int rc;
  43720. +
  43721. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  43722. + fsg->usb_amount_left > 0) {
  43723. +
  43724. + /* Throw away the data in a filled buffer */
  43725. + if (bh->state == BUF_STATE_FULL) {
  43726. + smp_rmb();
  43727. + bh->state = BUF_STATE_EMPTY;
  43728. + fsg->next_buffhd_to_drain = bh->next;
  43729. +
  43730. + /* A short packet or an error ends everything */
  43731. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  43732. + bh->outreq->status != 0) {
  43733. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  43734. + return -EINTR;
  43735. + }
  43736. + continue;
  43737. + }
  43738. +
  43739. + /* Try to submit another request if we need one */
  43740. + bh = fsg->next_buffhd_to_fill;
  43741. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  43742. + amount = min(fsg->usb_amount_left,
  43743. + (u32) mod_data.buflen);
  43744. +
  43745. + /* Except at the end of the transfer, amount will be
  43746. + * equal to the buffer size, which is divisible by
  43747. + * the bulk-out maxpacket size.
  43748. + */
  43749. + set_bulk_out_req_length(fsg, bh, amount);
  43750. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  43751. + &bh->outreq_busy, &bh->state);
  43752. + fsg->next_buffhd_to_fill = bh->next;
  43753. + fsg->usb_amount_left -= amount;
  43754. + continue;
  43755. + }
  43756. +
  43757. + /* Otherwise wait for something to happen */
  43758. + rc = sleep_thread(fsg);
  43759. + if (rc)
  43760. + return rc;
  43761. + }
  43762. + return 0;
  43763. +}
  43764. +
  43765. +
  43766. +static int finish_reply(struct fsg_dev *fsg)
  43767. +{
  43768. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  43769. + int rc = 0;
  43770. +
  43771. + switch (fsg->data_dir) {
  43772. + case DATA_DIR_NONE:
  43773. + break; // Nothing to send
  43774. +
  43775. + /* If we don't know whether the host wants to read or write,
  43776. + * this must be CB or CBI with an unknown command. We mustn't
  43777. + * try to send or receive any data. So stall both bulk pipes
  43778. + * if we can and wait for a reset. */
  43779. + case DATA_DIR_UNKNOWN:
  43780. + if (mod_data.can_stall) {
  43781. + fsg_set_halt(fsg, fsg->bulk_out);
  43782. + rc = halt_bulk_in_endpoint(fsg);
  43783. + }
  43784. + break;
  43785. +
  43786. + /* All but the last buffer of data must have already been sent */
  43787. + case DATA_DIR_TO_HOST:
  43788. + if (fsg->data_size == 0)
  43789. + ; // Nothing to send
  43790. +
  43791. + /* If there's no residue, simply send the last buffer */
  43792. + else if (fsg->residue == 0) {
  43793. + bh->inreq->zero = 0;
  43794. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  43795. + &bh->inreq_busy, &bh->state);
  43796. + fsg->next_buffhd_to_fill = bh->next;
  43797. + }
  43798. +
  43799. + /* There is a residue. For CB and CBI, simply mark the end
  43800. + * of the data with a short packet. However, if we are
  43801. + * allowed to stall, there was no data at all (residue ==
  43802. + * data_size), and the command failed (invalid LUN or
  43803. + * sense data is set), then halt the bulk-in endpoint
  43804. + * instead. */
  43805. + else if (!transport_is_bbb()) {
  43806. + if (mod_data.can_stall &&
  43807. + fsg->residue == fsg->data_size &&
  43808. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  43809. + bh->state = BUF_STATE_EMPTY;
  43810. + rc = halt_bulk_in_endpoint(fsg);
  43811. + } else {
  43812. + bh->inreq->zero = 1;
  43813. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  43814. + &bh->inreq_busy, &bh->state);
  43815. + fsg->next_buffhd_to_fill = bh->next;
  43816. + }
  43817. + }
  43818. +
  43819. + /*
  43820. + * For Bulk-only, mark the end of the data with a short
  43821. + * packet. If we are allowed to stall, halt the bulk-in
  43822. + * endpoint. (Note: This violates the Bulk-Only Transport
  43823. + * specification, which requires us to pad the data if we
  43824. + * don't halt the endpoint. Presumably nobody will mind.)
  43825. + */
  43826. + else {
  43827. + bh->inreq->zero = 1;
  43828. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  43829. + &bh->inreq_busy, &bh->state);
  43830. + fsg->next_buffhd_to_fill = bh->next;
  43831. + if (mod_data.can_stall)
  43832. + rc = halt_bulk_in_endpoint(fsg);
  43833. + }
  43834. + break;
  43835. +
  43836. + /* We have processed all we want from the data the host has sent.
  43837. + * There may still be outstanding bulk-out requests. */
  43838. + case DATA_DIR_FROM_HOST:
  43839. + if (fsg->residue == 0)
  43840. + ; // Nothing to receive
  43841. +
  43842. + /* Did the host stop sending unexpectedly early? */
  43843. + else if (fsg->short_packet_received) {
  43844. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  43845. + rc = -EINTR;
  43846. + }
  43847. +
  43848. + /* We haven't processed all the incoming data. Even though
  43849. + * we may be allowed to stall, doing so would cause a race.
  43850. + * The controller may already have ACK'ed all the remaining
  43851. + * bulk-out packets, in which case the host wouldn't see a
  43852. + * STALL. Not realizing the endpoint was halted, it wouldn't
  43853. + * clear the halt -- leading to problems later on. */
  43854. +#if 0
  43855. + else if (mod_data.can_stall) {
  43856. + fsg_set_halt(fsg, fsg->bulk_out);
  43857. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  43858. + rc = -EINTR;
  43859. + }
  43860. +#endif
  43861. +
  43862. + /* We can't stall. Read in the excess data and throw it
  43863. + * all away. */
  43864. + else
  43865. + rc = throw_away_data(fsg);
  43866. + break;
  43867. + }
  43868. + return rc;
  43869. +}
  43870. +
  43871. +
  43872. +static int send_status(struct fsg_dev *fsg)
  43873. +{
  43874. + struct fsg_lun *curlun = fsg->curlun;
  43875. + struct fsg_buffhd *bh;
  43876. + int rc;
  43877. + u8 status = US_BULK_STAT_OK;
  43878. + u32 sd, sdinfo = 0;
  43879. +
  43880. + /* Wait for the next buffer to become available */
  43881. + bh = fsg->next_buffhd_to_fill;
  43882. + while (bh->state != BUF_STATE_EMPTY) {
  43883. + rc = sleep_thread(fsg);
  43884. + if (rc)
  43885. + return rc;
  43886. + }
  43887. +
  43888. + if (curlun) {
  43889. + sd = curlun->sense_data;
  43890. + sdinfo = curlun->sense_data_info;
  43891. + } else if (fsg->bad_lun_okay)
  43892. + sd = SS_NO_SENSE;
  43893. + else
  43894. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  43895. +
  43896. + if (fsg->phase_error) {
  43897. + DBG(fsg, "sending phase-error status\n");
  43898. + status = US_BULK_STAT_PHASE;
  43899. + sd = SS_INVALID_COMMAND;
  43900. + } else if (sd != SS_NO_SENSE) {
  43901. + DBG(fsg, "sending command-failure status\n");
  43902. + status = US_BULK_STAT_FAIL;
  43903. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  43904. + " info x%x\n",
  43905. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  43906. + }
  43907. +
  43908. + if (transport_is_bbb()) {
  43909. + struct bulk_cs_wrap *csw = bh->buf;
  43910. +
  43911. + /* Store and send the Bulk-only CSW */
  43912. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  43913. + csw->Tag = fsg->tag;
  43914. + csw->Residue = cpu_to_le32(fsg->residue);
  43915. + csw->Status = status;
  43916. +
  43917. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  43918. + bh->inreq->zero = 0;
  43919. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  43920. + &bh->inreq_busy, &bh->state);
  43921. +
  43922. + } else if (mod_data.transport_type == USB_PR_CB) {
  43923. +
  43924. + /* Control-Bulk transport has no status phase! */
  43925. + return 0;
  43926. +
  43927. + } else { // USB_PR_CBI
  43928. + struct interrupt_data *buf = bh->buf;
  43929. +
  43930. + /* Store and send the Interrupt data. UFI sends the ASC
  43931. + * and ASCQ bytes. Everything else sends a Type (which
  43932. + * is always 0) and the status Value. */
  43933. + if (mod_data.protocol_type == USB_SC_UFI) {
  43934. + buf->bType = ASC(sd);
  43935. + buf->bValue = ASCQ(sd);
  43936. + } else {
  43937. + buf->bType = 0;
  43938. + buf->bValue = status;
  43939. + }
  43940. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  43941. +
  43942. + fsg->intr_buffhd = bh; // Point to the right buffhd
  43943. + fsg->intreq->buf = bh->inreq->buf;
  43944. + fsg->intreq->context = bh;
  43945. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  43946. + &fsg->intreq_busy, &bh->state);
  43947. + }
  43948. +
  43949. + fsg->next_buffhd_to_fill = bh->next;
  43950. + return 0;
  43951. +}
  43952. +
  43953. +
  43954. +/*-------------------------------------------------------------------------*/
  43955. +
  43956. +/* Check whether the command is properly formed and whether its data size
  43957. + * and direction agree with the values we already have. */
  43958. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  43959. + enum data_direction data_dir, unsigned int mask,
  43960. + int needs_medium, const char *name)
  43961. +{
  43962. + int i;
  43963. + int lun = fsg->cmnd[1] >> 5;
  43964. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  43965. + char hdlen[20];
  43966. + struct fsg_lun *curlun;
  43967. +
  43968. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  43969. + * Transparent SCSI doesn't pad. */
  43970. + if (protocol_is_scsi())
  43971. + ;
  43972. +
  43973. + /* There's some disagreement as to whether RBC pads commands or not.
  43974. + * We'll play it safe and accept either form. */
  43975. + else if (mod_data.protocol_type == USB_SC_RBC) {
  43976. + if (fsg->cmnd_size == 12)
  43977. + cmnd_size = 12;
  43978. +
  43979. + /* All the other protocols pad to 12 bytes */
  43980. + } else
  43981. + cmnd_size = 12;
  43982. +
  43983. + hdlen[0] = 0;
  43984. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  43985. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  43986. + fsg->data_size);
  43987. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  43988. + name, cmnd_size, dirletter[(int) data_dir],
  43989. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  43990. +
  43991. + /* We can't reply at all until we know the correct data direction
  43992. + * and size. */
  43993. + if (fsg->data_size_from_cmnd == 0)
  43994. + data_dir = DATA_DIR_NONE;
  43995. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  43996. + fsg->data_dir = data_dir;
  43997. + fsg->data_size = fsg->data_size_from_cmnd;
  43998. +
  43999. + } else { // Bulk-only
  44000. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  44001. +
  44002. + /* Host data size < Device data size is a phase error.
  44003. + * Carry out the command, but only transfer as much
  44004. + * as we are allowed. */
  44005. + fsg->data_size_from_cmnd = fsg->data_size;
  44006. + fsg->phase_error = 1;
  44007. + }
  44008. + }
  44009. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  44010. +
  44011. + /* Conflicting data directions is a phase error */
  44012. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  44013. + fsg->phase_error = 1;
  44014. + return -EINVAL;
  44015. + }
  44016. +
  44017. + /* Verify the length of the command itself */
  44018. + if (cmnd_size != fsg->cmnd_size) {
  44019. +
  44020. + /* Special case workaround: There are plenty of buggy SCSI
  44021. + * implementations. Many have issues with cbw->Length
  44022. + * field passing a wrong command size. For those cases we
  44023. + * always try to work around the problem by using the length
  44024. + * sent by the host side provided it is at least as large
  44025. + * as the correct command length.
  44026. + * Examples of such cases would be MS-Windows, which issues
  44027. + * REQUEST SENSE with cbw->Length == 12 where it should
  44028. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  44029. + * REQUEST SENSE with cbw->Length == 10 where it should
  44030. + * be 6 as well.
  44031. + */
  44032. + if (cmnd_size <= fsg->cmnd_size) {
  44033. + DBG(fsg, "%s is buggy! Expected length %d "
  44034. + "but we got %d\n", name,
  44035. + cmnd_size, fsg->cmnd_size);
  44036. + cmnd_size = fsg->cmnd_size;
  44037. + } else {
  44038. + fsg->phase_error = 1;
  44039. + return -EINVAL;
  44040. + }
  44041. + }
  44042. +
  44043. + /* Check that the LUN values are consistent */
  44044. + if (transport_is_bbb()) {
  44045. + if (fsg->lun != lun)
  44046. + DBG(fsg, "using LUN %d from CBW, "
  44047. + "not LUN %d from CDB\n",
  44048. + fsg->lun, lun);
  44049. + }
  44050. +
  44051. + /* Check the LUN */
  44052. + curlun = fsg->curlun;
  44053. + if (curlun) {
  44054. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  44055. + curlun->sense_data = SS_NO_SENSE;
  44056. + curlun->sense_data_info = 0;
  44057. + curlun->info_valid = 0;
  44058. + }
  44059. + } else {
  44060. + fsg->bad_lun_okay = 0;
  44061. +
  44062. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  44063. + * to use unsupported LUNs; all others may not. */
  44064. + if (fsg->cmnd[0] != INQUIRY &&
  44065. + fsg->cmnd[0] != REQUEST_SENSE) {
  44066. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  44067. + return -EINVAL;
  44068. + }
  44069. + }
  44070. +
  44071. + /* If a unit attention condition exists, only INQUIRY and
  44072. + * REQUEST SENSE commands are allowed; anything else must fail. */
  44073. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  44074. + fsg->cmnd[0] != INQUIRY &&
  44075. + fsg->cmnd[0] != REQUEST_SENSE) {
  44076. + curlun->sense_data = curlun->unit_attention_data;
  44077. + curlun->unit_attention_data = SS_NO_SENSE;
  44078. + return -EINVAL;
  44079. + }
  44080. +
  44081. + /* Check that only command bytes listed in the mask are non-zero */
  44082. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  44083. + for (i = 1; i < cmnd_size; ++i) {
  44084. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  44085. + if (curlun)
  44086. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  44087. + return -EINVAL;
  44088. + }
  44089. + }
  44090. +
  44091. + /* If the medium isn't mounted and the command needs to access
  44092. + * it, return an error. */
  44093. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  44094. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  44095. + return -EINVAL;
  44096. + }
  44097. +
  44098. + return 0;
  44099. +}
  44100. +
  44101. +/* wrapper of check_command for data size in blocks handling */
  44102. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  44103. + enum data_direction data_dir, unsigned int mask,
  44104. + int needs_medium, const char *name)
  44105. +{
  44106. + if (fsg->curlun)
  44107. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  44108. + return check_command(fsg, cmnd_size, data_dir,
  44109. + mask, needs_medium, name);
  44110. +}
  44111. +
  44112. +static int do_scsi_command(struct fsg_dev *fsg)
  44113. +{
  44114. + struct fsg_buffhd *bh;
  44115. + int rc;
  44116. + int reply = -EINVAL;
  44117. + int i;
  44118. + static char unknown[16];
  44119. +
  44120. + dump_cdb(fsg);
  44121. +
  44122. + /* Wait for the next buffer to become available for data or status */
  44123. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  44124. + while (bh->state != BUF_STATE_EMPTY) {
  44125. + rc = sleep_thread(fsg);
  44126. + if (rc)
  44127. + return rc;
  44128. + }
  44129. + fsg->phase_error = 0;
  44130. + fsg->short_packet_received = 0;
  44131. +
  44132. + down_read(&fsg->filesem); // We're using the backing file
  44133. + switch (fsg->cmnd[0]) {
  44134. +
  44135. + case INQUIRY:
  44136. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  44137. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  44138. + (1<<4), 0,
  44139. + "INQUIRY")) == 0)
  44140. + reply = do_inquiry(fsg, bh);
  44141. + break;
  44142. +
  44143. + case MODE_SELECT:
  44144. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  44145. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  44146. + (1<<1) | (1<<4), 0,
  44147. + "MODE SELECT(6)")) == 0)
  44148. + reply = do_mode_select(fsg, bh);
  44149. + break;
  44150. +
  44151. + case MODE_SELECT_10:
  44152. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  44153. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  44154. + (1<<1) | (3<<7), 0,
  44155. + "MODE SELECT(10)")) == 0)
  44156. + reply = do_mode_select(fsg, bh);
  44157. + break;
  44158. +
  44159. + case MODE_SENSE:
  44160. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  44161. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  44162. + (1<<1) | (1<<2) | (1<<4), 0,
  44163. + "MODE SENSE(6)")) == 0)
  44164. + reply = do_mode_sense(fsg, bh);
  44165. + break;
  44166. +
  44167. + case MODE_SENSE_10:
  44168. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  44169. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  44170. + (1<<1) | (1<<2) | (3<<7), 0,
  44171. + "MODE SENSE(10)")) == 0)
  44172. + reply = do_mode_sense(fsg, bh);
  44173. + break;
  44174. +
  44175. + case ALLOW_MEDIUM_REMOVAL:
  44176. + fsg->data_size_from_cmnd = 0;
  44177. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  44178. + (1<<4), 0,
  44179. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  44180. + reply = do_prevent_allow(fsg);
  44181. + break;
  44182. +
  44183. + case READ_6:
  44184. + i = fsg->cmnd[4];
  44185. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  44186. + if ((reply = check_command_size_in_blocks(fsg, 6,
  44187. + DATA_DIR_TO_HOST,
  44188. + (7<<1) | (1<<4), 1,
  44189. + "READ(6)")) == 0)
  44190. + reply = do_read(fsg);
  44191. + break;
  44192. +
  44193. + case READ_10:
  44194. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  44195. + if ((reply = check_command_size_in_blocks(fsg, 10,
  44196. + DATA_DIR_TO_HOST,
  44197. + (1<<1) | (0xf<<2) | (3<<7), 1,
  44198. + "READ(10)")) == 0)
  44199. + reply = do_read(fsg);
  44200. + break;
  44201. +
  44202. + case READ_12:
  44203. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  44204. + if ((reply = check_command_size_in_blocks(fsg, 12,
  44205. + DATA_DIR_TO_HOST,
  44206. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  44207. + "READ(12)")) == 0)
  44208. + reply = do_read(fsg);
  44209. + break;
  44210. +
  44211. + case READ_CAPACITY:
  44212. + fsg->data_size_from_cmnd = 8;
  44213. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  44214. + (0xf<<2) | (1<<8), 1,
  44215. + "READ CAPACITY")) == 0)
  44216. + reply = do_read_capacity(fsg, bh);
  44217. + break;
  44218. +
  44219. + case READ_HEADER:
  44220. + if (!mod_data.cdrom)
  44221. + goto unknown_cmnd;
  44222. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  44223. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  44224. + (3<<7) | (0x1f<<1), 1,
  44225. + "READ HEADER")) == 0)
  44226. + reply = do_read_header(fsg, bh);
  44227. + break;
  44228. +
  44229. + case READ_TOC:
  44230. + if (!mod_data.cdrom)
  44231. + goto unknown_cmnd;
  44232. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  44233. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  44234. + (7<<6) | (1<<1), 1,
  44235. + "READ TOC")) == 0)
  44236. + reply = do_read_toc(fsg, bh);
  44237. + break;
  44238. +
  44239. + case READ_FORMAT_CAPACITIES:
  44240. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  44241. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  44242. + (3<<7), 1,
  44243. + "READ FORMAT CAPACITIES")) == 0)
  44244. + reply = do_read_format_capacities(fsg, bh);
  44245. + break;
  44246. +
  44247. + case REQUEST_SENSE:
  44248. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  44249. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  44250. + (1<<4), 0,
  44251. + "REQUEST SENSE")) == 0)
  44252. + reply = do_request_sense(fsg, bh);
  44253. + break;
  44254. +
  44255. + case START_STOP:
  44256. + fsg->data_size_from_cmnd = 0;
  44257. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  44258. + (1<<1) | (1<<4), 0,
  44259. + "START-STOP UNIT")) == 0)
  44260. + reply = do_start_stop(fsg);
  44261. + break;
  44262. +
  44263. + case SYNCHRONIZE_CACHE:
  44264. + fsg->data_size_from_cmnd = 0;
  44265. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  44266. + (0xf<<2) | (3<<7), 1,
  44267. + "SYNCHRONIZE CACHE")) == 0)
  44268. + reply = do_synchronize_cache(fsg);
  44269. + break;
  44270. +
  44271. + case TEST_UNIT_READY:
  44272. + fsg->data_size_from_cmnd = 0;
  44273. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  44274. + 0, 1,
  44275. + "TEST UNIT READY");
  44276. + break;
  44277. +
  44278. + /* Although optional, this command is used by MS-Windows. We
  44279. + * support a minimal version: BytChk must be 0. */
  44280. + case VERIFY:
  44281. + fsg->data_size_from_cmnd = 0;
  44282. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  44283. + (1<<1) | (0xf<<2) | (3<<7), 1,
  44284. + "VERIFY")) == 0)
  44285. + reply = do_verify(fsg);
  44286. + break;
  44287. +
  44288. + case WRITE_6:
  44289. + i = fsg->cmnd[4];
  44290. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  44291. + if ((reply = check_command_size_in_blocks(fsg, 6,
  44292. + DATA_DIR_FROM_HOST,
  44293. + (7<<1) | (1<<4), 1,
  44294. + "WRITE(6)")) == 0)
  44295. + reply = do_write(fsg);
  44296. + break;
  44297. +
  44298. + case WRITE_10:
  44299. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  44300. + if ((reply = check_command_size_in_blocks(fsg, 10,
  44301. + DATA_DIR_FROM_HOST,
  44302. + (1<<1) | (0xf<<2) | (3<<7), 1,
  44303. + "WRITE(10)")) == 0)
  44304. + reply = do_write(fsg);
  44305. + break;
  44306. +
  44307. + case WRITE_12:
  44308. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  44309. + if ((reply = check_command_size_in_blocks(fsg, 12,
  44310. + DATA_DIR_FROM_HOST,
  44311. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  44312. + "WRITE(12)")) == 0)
  44313. + reply = do_write(fsg);
  44314. + break;
  44315. +
  44316. + /* Some mandatory commands that we recognize but don't implement.
  44317. + * They don't mean much in this setting. It's left as an exercise
  44318. + * for anyone interested to implement RESERVE and RELEASE in terms
  44319. + * of Posix locks. */
  44320. + case FORMAT_UNIT:
  44321. + case RELEASE:
  44322. + case RESERVE:
  44323. + case SEND_DIAGNOSTIC:
  44324. + // Fall through
  44325. +
  44326. + default:
  44327. + unknown_cmnd:
  44328. + fsg->data_size_from_cmnd = 0;
  44329. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  44330. + if ((reply = check_command(fsg, fsg->cmnd_size,
  44331. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  44332. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  44333. + reply = -EINVAL;
  44334. + }
  44335. + break;
  44336. + }
  44337. + up_read(&fsg->filesem);
  44338. +
  44339. + if (reply == -EINTR || signal_pending(current))
  44340. + return -EINTR;
  44341. +
  44342. + /* Set up the single reply buffer for finish_reply() */
  44343. + if (reply == -EINVAL)
  44344. + reply = 0; // Error reply length
  44345. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  44346. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  44347. + bh->inreq->length = reply;
  44348. + bh->state = BUF_STATE_FULL;
  44349. + fsg->residue -= reply;
  44350. + } // Otherwise it's already set
  44351. +
  44352. + return 0;
  44353. +}
  44354. +
  44355. +
  44356. +/*-------------------------------------------------------------------------*/
  44357. +
  44358. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  44359. +{
  44360. + struct usb_request *req = bh->outreq;
  44361. + struct bulk_cb_wrap *cbw = req->buf;
  44362. +
  44363. + /* Was this a real packet? Should it be ignored? */
  44364. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  44365. + return -EINVAL;
  44366. +
  44367. + /* Is the CBW valid? */
  44368. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  44369. + cbw->Signature != cpu_to_le32(
  44370. + US_BULK_CB_SIGN)) {
  44371. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  44372. + req->actual,
  44373. + le32_to_cpu(cbw->Signature));
  44374. +
  44375. + /* The Bulk-only spec says we MUST stall the IN endpoint
  44376. + * (6.6.1), so it's unavoidable. It also says we must
  44377. + * retain this state until the next reset, but there's
  44378. + * no way to tell the controller driver it should ignore
  44379. + * Clear-Feature(HALT) requests.
  44380. + *
  44381. + * We aren't required to halt the OUT endpoint; instead
  44382. + * we can simply accept and discard any data received
  44383. + * until the next reset. */
  44384. + wedge_bulk_in_endpoint(fsg);
  44385. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  44386. + return -EINVAL;
  44387. + }
  44388. +
  44389. + /* Is the CBW meaningful? */
  44390. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  44391. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  44392. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  44393. + "cmdlen %u\n",
  44394. + cbw->Lun, cbw->Flags, cbw->Length);
  44395. +
  44396. + /* We can do anything we want here, so let's stall the
  44397. + * bulk pipes if we are allowed to. */
  44398. + if (mod_data.can_stall) {
  44399. + fsg_set_halt(fsg, fsg->bulk_out);
  44400. + halt_bulk_in_endpoint(fsg);
  44401. + }
  44402. + return -EINVAL;
  44403. + }
  44404. +
  44405. + /* Save the command for later */
  44406. + fsg->cmnd_size = cbw->Length;
  44407. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  44408. + if (cbw->Flags & US_BULK_FLAG_IN)
  44409. + fsg->data_dir = DATA_DIR_TO_HOST;
  44410. + else
  44411. + fsg->data_dir = DATA_DIR_FROM_HOST;
  44412. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  44413. + if (fsg->data_size == 0)
  44414. + fsg->data_dir = DATA_DIR_NONE;
  44415. + fsg->lun = cbw->Lun;
  44416. + fsg->tag = cbw->Tag;
  44417. + return 0;
  44418. +}
  44419. +
  44420. +
  44421. +static int get_next_command(struct fsg_dev *fsg)
  44422. +{
  44423. + struct fsg_buffhd *bh;
  44424. + int rc = 0;
  44425. +
  44426. + if (transport_is_bbb()) {
  44427. +
  44428. + /* Wait for the next buffer to become available */
  44429. + bh = fsg->next_buffhd_to_fill;
  44430. + while (bh->state != BUF_STATE_EMPTY) {
  44431. + rc = sleep_thread(fsg);
  44432. + if (rc)
  44433. + return rc;
  44434. + }
  44435. +
  44436. + /* Queue a request to read a Bulk-only CBW */
  44437. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  44438. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  44439. + &bh->outreq_busy, &bh->state);
  44440. +
  44441. + /* We will drain the buffer in software, which means we
  44442. + * can reuse it for the next filling. No need to advance
  44443. + * next_buffhd_to_fill. */
  44444. +
  44445. + /* Wait for the CBW to arrive */
  44446. + while (bh->state != BUF_STATE_FULL) {
  44447. + rc = sleep_thread(fsg);
  44448. + if (rc)
  44449. + return rc;
  44450. + }
  44451. + smp_rmb();
  44452. + rc = received_cbw(fsg, bh);
  44453. + bh->state = BUF_STATE_EMPTY;
  44454. +
  44455. + } else { // USB_PR_CB or USB_PR_CBI
  44456. +
  44457. + /* Wait for the next command to arrive */
  44458. + while (fsg->cbbuf_cmnd_size == 0) {
  44459. + rc = sleep_thread(fsg);
  44460. + if (rc)
  44461. + return rc;
  44462. + }
  44463. +
  44464. + /* Is the previous status interrupt request still busy?
  44465. + * The host is allowed to skip reading the status,
  44466. + * so we must cancel it. */
  44467. + if (fsg->intreq_busy)
  44468. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  44469. +
  44470. + /* Copy the command and mark the buffer empty */
  44471. + fsg->data_dir = DATA_DIR_UNKNOWN;
  44472. + spin_lock_irq(&fsg->lock);
  44473. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  44474. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  44475. + fsg->cbbuf_cmnd_size = 0;
  44476. + spin_unlock_irq(&fsg->lock);
  44477. +
  44478. + /* Use LUN from the command */
  44479. + fsg->lun = fsg->cmnd[1] >> 5;
  44480. + }
  44481. +
  44482. + /* Update current lun */
  44483. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  44484. + fsg->curlun = &fsg->luns[fsg->lun];
  44485. + else
  44486. + fsg->curlun = NULL;
  44487. +
  44488. + return rc;
  44489. +}
  44490. +
  44491. +
  44492. +/*-------------------------------------------------------------------------*/
  44493. +
  44494. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  44495. + const struct usb_endpoint_descriptor *d)
  44496. +{
  44497. + int rc;
  44498. +
  44499. + ep->driver_data = fsg;
  44500. + ep->desc = d;
  44501. + rc = usb_ep_enable(ep);
  44502. + if (rc)
  44503. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  44504. + return rc;
  44505. +}
  44506. +
  44507. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  44508. + struct usb_request **preq)
  44509. +{
  44510. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  44511. + if (*preq)
  44512. + return 0;
  44513. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  44514. + return -ENOMEM;
  44515. +}
  44516. +
  44517. +/*
  44518. + * Reset interface setting and re-init endpoint state (toggle etc).
  44519. + * Call with altsetting < 0 to disable the interface. The only other
  44520. + * available altsetting is 0, which enables the interface.
  44521. + */
  44522. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  44523. +{
  44524. + int rc = 0;
  44525. + int i;
  44526. + const struct usb_endpoint_descriptor *d;
  44527. +
  44528. + if (fsg->running)
  44529. + DBG(fsg, "reset interface\n");
  44530. +
  44531. +reset:
  44532. + /* Deallocate the requests */
  44533. + for (i = 0; i < fsg_num_buffers; ++i) {
  44534. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  44535. +
  44536. + if (bh->inreq) {
  44537. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  44538. + bh->inreq = NULL;
  44539. + }
  44540. + if (bh->outreq) {
  44541. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  44542. + bh->outreq = NULL;
  44543. + }
  44544. + }
  44545. + if (fsg->intreq) {
  44546. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  44547. + fsg->intreq = NULL;
  44548. + }
  44549. +
  44550. + /* Disable the endpoints */
  44551. + if (fsg->bulk_in_enabled) {
  44552. + usb_ep_disable(fsg->bulk_in);
  44553. + fsg->bulk_in_enabled = 0;
  44554. + }
  44555. + if (fsg->bulk_out_enabled) {
  44556. + usb_ep_disable(fsg->bulk_out);
  44557. + fsg->bulk_out_enabled = 0;
  44558. + }
  44559. + if (fsg->intr_in_enabled) {
  44560. + usb_ep_disable(fsg->intr_in);
  44561. + fsg->intr_in_enabled = 0;
  44562. + }
  44563. +
  44564. + fsg->running = 0;
  44565. + if (altsetting < 0 || rc != 0)
  44566. + return rc;
  44567. +
  44568. + DBG(fsg, "set interface %d\n", altsetting);
  44569. +
  44570. + /* Enable the endpoints */
  44571. + d = fsg_ep_desc(fsg->gadget,
  44572. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  44573. + &fsg_ss_bulk_in_desc);
  44574. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  44575. + goto reset;
  44576. + fsg->bulk_in_enabled = 1;
  44577. +
  44578. + d = fsg_ep_desc(fsg->gadget,
  44579. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  44580. + &fsg_ss_bulk_out_desc);
  44581. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  44582. + goto reset;
  44583. + fsg->bulk_out_enabled = 1;
  44584. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  44585. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  44586. +
  44587. + if (transport_is_cbi()) {
  44588. + d = fsg_ep_desc(fsg->gadget,
  44589. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  44590. + &fsg_ss_intr_in_desc);
  44591. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  44592. + goto reset;
  44593. + fsg->intr_in_enabled = 1;
  44594. + }
  44595. +
  44596. + /* Allocate the requests */
  44597. + for (i = 0; i < fsg_num_buffers; ++i) {
  44598. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  44599. +
  44600. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  44601. + goto reset;
  44602. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  44603. + goto reset;
  44604. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  44605. + bh->inreq->context = bh->outreq->context = bh;
  44606. + bh->inreq->complete = bulk_in_complete;
  44607. + bh->outreq->complete = bulk_out_complete;
  44608. + }
  44609. + if (transport_is_cbi()) {
  44610. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  44611. + goto reset;
  44612. + fsg->intreq->complete = intr_in_complete;
  44613. + }
  44614. +
  44615. + fsg->running = 1;
  44616. + for (i = 0; i < fsg->nluns; ++i)
  44617. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  44618. + return rc;
  44619. +}
  44620. +
  44621. +
  44622. +/*
  44623. + * Change our operational configuration. This code must agree with the code
  44624. + * that returns config descriptors, and with interface altsetting code.
  44625. + *
  44626. + * It's also responsible for power management interactions. Some
  44627. + * configurations might not work with our current power sources.
  44628. + * For now we just assume the gadget is always self-powered.
  44629. + */
  44630. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  44631. +{
  44632. + int rc = 0;
  44633. +
  44634. + /* Disable the single interface */
  44635. + if (fsg->config != 0) {
  44636. + DBG(fsg, "reset config\n");
  44637. + fsg->config = 0;
  44638. + rc = do_set_interface(fsg, -1);
  44639. + }
  44640. +
  44641. + /* Enable the interface */
  44642. + if (new_config != 0) {
  44643. + fsg->config = new_config;
  44644. + if ((rc = do_set_interface(fsg, 0)) != 0)
  44645. + fsg->config = 0; // Reset on errors
  44646. + else
  44647. + INFO(fsg, "%s config #%d\n",
  44648. + usb_speed_string(fsg->gadget->speed),
  44649. + fsg->config);
  44650. + }
  44651. + return rc;
  44652. +}
  44653. +
  44654. +
  44655. +/*-------------------------------------------------------------------------*/
  44656. +
  44657. +static void handle_exception(struct fsg_dev *fsg)
  44658. +{
  44659. + siginfo_t info;
  44660. + int sig;
  44661. + int i;
  44662. + int num_active;
  44663. + struct fsg_buffhd *bh;
  44664. + enum fsg_state old_state;
  44665. + u8 new_config;
  44666. + struct fsg_lun *curlun;
  44667. + unsigned int exception_req_tag;
  44668. + int rc;
  44669. +
  44670. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  44671. + * into a high-priority EXIT exception. */
  44672. + for (;;) {
  44673. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  44674. + if (!sig)
  44675. + break;
  44676. + if (sig != SIGUSR1) {
  44677. + if (fsg->state < FSG_STATE_EXIT)
  44678. + DBG(fsg, "Main thread exiting on signal\n");
  44679. + raise_exception(fsg, FSG_STATE_EXIT);
  44680. + }
  44681. + }
  44682. +
  44683. + /* Cancel all the pending transfers */
  44684. + if (fsg->intreq_busy)
  44685. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  44686. + for (i = 0; i < fsg_num_buffers; ++i) {
  44687. + bh = &fsg->buffhds[i];
  44688. + if (bh->inreq_busy)
  44689. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  44690. + if (bh->outreq_busy)
  44691. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  44692. + }
  44693. +
  44694. + /* Wait until everything is idle */
  44695. + for (;;) {
  44696. + num_active = fsg->intreq_busy;
  44697. + for (i = 0; i < fsg_num_buffers; ++i) {
  44698. + bh = &fsg->buffhds[i];
  44699. + num_active += bh->inreq_busy + bh->outreq_busy;
  44700. + }
  44701. + if (num_active == 0)
  44702. + break;
  44703. + if (sleep_thread(fsg))
  44704. + return;
  44705. + }
  44706. +
  44707. + /* Clear out the controller's fifos */
  44708. + if (fsg->bulk_in_enabled)
  44709. + usb_ep_fifo_flush(fsg->bulk_in);
  44710. + if (fsg->bulk_out_enabled)
  44711. + usb_ep_fifo_flush(fsg->bulk_out);
  44712. + if (fsg->intr_in_enabled)
  44713. + usb_ep_fifo_flush(fsg->intr_in);
  44714. +
  44715. + /* Reset the I/O buffer states and pointers, the SCSI
  44716. + * state, and the exception. Then invoke the handler. */
  44717. + spin_lock_irq(&fsg->lock);
  44718. +
  44719. + for (i = 0; i < fsg_num_buffers; ++i) {
  44720. + bh = &fsg->buffhds[i];
  44721. + bh->state = BUF_STATE_EMPTY;
  44722. + }
  44723. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  44724. + &fsg->buffhds[0];
  44725. +
  44726. + exception_req_tag = fsg->exception_req_tag;
  44727. + new_config = fsg->new_config;
  44728. + old_state = fsg->state;
  44729. +
  44730. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  44731. + fsg->state = FSG_STATE_STATUS_PHASE;
  44732. + else {
  44733. + for (i = 0; i < fsg->nluns; ++i) {
  44734. + curlun = &fsg->luns[i];
  44735. + curlun->prevent_medium_removal = 0;
  44736. + curlun->sense_data = curlun->unit_attention_data =
  44737. + SS_NO_SENSE;
  44738. + curlun->sense_data_info = 0;
  44739. + curlun->info_valid = 0;
  44740. + }
  44741. + fsg->state = FSG_STATE_IDLE;
  44742. + }
  44743. + spin_unlock_irq(&fsg->lock);
  44744. +
  44745. + /* Carry out any extra actions required for the exception */
  44746. + switch (old_state) {
  44747. + default:
  44748. + break;
  44749. +
  44750. + case FSG_STATE_ABORT_BULK_OUT:
  44751. + send_status(fsg);
  44752. + spin_lock_irq(&fsg->lock);
  44753. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  44754. + fsg->state = FSG_STATE_IDLE;
  44755. + spin_unlock_irq(&fsg->lock);
  44756. + break;
  44757. +
  44758. + case FSG_STATE_RESET:
  44759. + /* In case we were forced against our will to halt a
  44760. + * bulk endpoint, clear the halt now. (The SuperH UDC
  44761. + * requires this.) */
  44762. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  44763. + usb_ep_clear_halt(fsg->bulk_in);
  44764. +
  44765. + if (transport_is_bbb()) {
  44766. + if (fsg->ep0_req_tag == exception_req_tag)
  44767. + ep0_queue(fsg); // Complete the status stage
  44768. +
  44769. + } else if (transport_is_cbi())
  44770. + send_status(fsg); // Status by interrupt pipe
  44771. +
  44772. + /* Technically this should go here, but it would only be
  44773. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  44774. + * CONFIG_CHANGE cases. */
  44775. + // for (i = 0; i < fsg->nluns; ++i)
  44776. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  44777. + break;
  44778. +
  44779. + case FSG_STATE_INTERFACE_CHANGE:
  44780. + rc = do_set_interface(fsg, 0);
  44781. + if (fsg->ep0_req_tag != exception_req_tag)
  44782. + break;
  44783. + if (rc != 0) // STALL on errors
  44784. + fsg_set_halt(fsg, fsg->ep0);
  44785. + else // Complete the status stage
  44786. + ep0_queue(fsg);
  44787. + break;
  44788. +
  44789. + case FSG_STATE_CONFIG_CHANGE:
  44790. + rc = do_set_config(fsg, new_config);
  44791. + if (fsg->ep0_req_tag != exception_req_tag)
  44792. + break;
  44793. + if (rc != 0) // STALL on errors
  44794. + fsg_set_halt(fsg, fsg->ep0);
  44795. + else // Complete the status stage
  44796. + ep0_queue(fsg);
  44797. + break;
  44798. +
  44799. + case FSG_STATE_DISCONNECT:
  44800. + for (i = 0; i < fsg->nluns; ++i)
  44801. + fsg_lun_fsync_sub(fsg->luns + i);
  44802. + do_set_config(fsg, 0); // Unconfigured state
  44803. + break;
  44804. +
  44805. + case FSG_STATE_EXIT:
  44806. + case FSG_STATE_TERMINATED:
  44807. + do_set_config(fsg, 0); // Free resources
  44808. + spin_lock_irq(&fsg->lock);
  44809. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  44810. + spin_unlock_irq(&fsg->lock);
  44811. + break;
  44812. + }
  44813. +}
  44814. +
  44815. +
  44816. +/*-------------------------------------------------------------------------*/
  44817. +
  44818. +static int fsg_main_thread(void *fsg_)
  44819. +{
  44820. + struct fsg_dev *fsg = fsg_;
  44821. +
  44822. + /* Allow the thread to be killed by a signal, but set the signal mask
  44823. + * to block everything but INT, TERM, KILL, and USR1. */
  44824. + allow_signal(SIGINT);
  44825. + allow_signal(SIGTERM);
  44826. + allow_signal(SIGKILL);
  44827. + allow_signal(SIGUSR1);
  44828. +
  44829. + /* Allow the thread to be frozen */
  44830. + set_freezable();
  44831. +
  44832. + /* Arrange for userspace references to be interpreted as kernel
  44833. + * pointers. That way we can pass a kernel pointer to a routine
  44834. + * that expects a __user pointer and it will work okay. */
  44835. + set_fs(get_ds());
  44836. +
  44837. + /* The main loop */
  44838. + while (fsg->state != FSG_STATE_TERMINATED) {
  44839. + if (exception_in_progress(fsg) || signal_pending(current)) {
  44840. + handle_exception(fsg);
  44841. + continue;
  44842. + }
  44843. +
  44844. + if (!fsg->running) {
  44845. + sleep_thread(fsg);
  44846. + continue;
  44847. + }
  44848. +
  44849. + if (get_next_command(fsg))
  44850. + continue;
  44851. +
  44852. + spin_lock_irq(&fsg->lock);
  44853. + if (!exception_in_progress(fsg))
  44854. + fsg->state = FSG_STATE_DATA_PHASE;
  44855. + spin_unlock_irq(&fsg->lock);
  44856. +
  44857. + if (do_scsi_command(fsg) || finish_reply(fsg))
  44858. + continue;
  44859. +
  44860. + spin_lock_irq(&fsg->lock);
  44861. + if (!exception_in_progress(fsg))
  44862. + fsg->state = FSG_STATE_STATUS_PHASE;
  44863. + spin_unlock_irq(&fsg->lock);
  44864. +
  44865. + if (send_status(fsg))
  44866. + continue;
  44867. +
  44868. + spin_lock_irq(&fsg->lock);
  44869. + if (!exception_in_progress(fsg))
  44870. + fsg->state = FSG_STATE_IDLE;
  44871. + spin_unlock_irq(&fsg->lock);
  44872. + }
  44873. +
  44874. + spin_lock_irq(&fsg->lock);
  44875. + fsg->thread_task = NULL;
  44876. + spin_unlock_irq(&fsg->lock);
  44877. +
  44878. + /* If we are exiting because of a signal, unregister the
  44879. + * gadget driver. */
  44880. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  44881. + usb_gadget_unregister_driver(&fsg_driver);
  44882. +
  44883. + /* Let the unbind and cleanup routines know the thread has exited */
  44884. + complete_and_exit(&fsg->thread_notifier, 0);
  44885. +}
  44886. +
  44887. +
  44888. +/*-------------------------------------------------------------------------*/
  44889. +
  44890. +
  44891. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  44892. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  44893. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  44894. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  44895. +
  44896. +
  44897. +/*-------------------------------------------------------------------------*/
  44898. +
  44899. +static void fsg_release(struct kref *ref)
  44900. +{
  44901. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  44902. +
  44903. + kfree(fsg->luns);
  44904. + kfree(fsg);
  44905. +}
  44906. +
  44907. +static void lun_release(struct device *dev)
  44908. +{
  44909. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  44910. + struct fsg_dev *fsg =
  44911. + container_of(filesem, struct fsg_dev, filesem);
  44912. +
  44913. + kref_put(&fsg->ref, fsg_release);
  44914. +}
  44915. +
  44916. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  44917. +{
  44918. + struct fsg_dev *fsg = get_gadget_data(gadget);
  44919. + int i;
  44920. + struct fsg_lun *curlun;
  44921. + struct usb_request *req = fsg->ep0req;
  44922. +
  44923. + DBG(fsg, "unbind\n");
  44924. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  44925. +
  44926. + /* If the thread isn't already dead, tell it to exit now */
  44927. + if (fsg->state != FSG_STATE_TERMINATED) {
  44928. + raise_exception(fsg, FSG_STATE_EXIT);
  44929. + wait_for_completion(&fsg->thread_notifier);
  44930. +
  44931. + /* The cleanup routine waits for this completion also */
  44932. + complete(&fsg->thread_notifier);
  44933. + }
  44934. +
  44935. + /* Unregister the sysfs attribute files and the LUNs */
  44936. + for (i = 0; i < fsg->nluns; ++i) {
  44937. + curlun = &fsg->luns[i];
  44938. + if (curlun->registered) {
  44939. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  44940. + device_remove_file(&curlun->dev, &dev_attr_ro);
  44941. + device_remove_file(&curlun->dev, &dev_attr_file);
  44942. + fsg_lun_close(curlun);
  44943. + device_unregister(&curlun->dev);
  44944. + curlun->registered = 0;
  44945. + }
  44946. + }
  44947. +
  44948. + /* Free the data buffers */
  44949. + for (i = 0; i < fsg_num_buffers; ++i)
  44950. + kfree(fsg->buffhds[i].buf);
  44951. +
  44952. + /* Free the request and buffer for endpoint 0 */
  44953. + if (req) {
  44954. + kfree(req->buf);
  44955. + usb_ep_free_request(fsg->ep0, req);
  44956. + }
  44957. +
  44958. + set_gadget_data(gadget, NULL);
  44959. +}
  44960. +
  44961. +
  44962. +static int __init check_parameters(struct fsg_dev *fsg)
  44963. +{
  44964. + int prot;
  44965. + int gcnum;
  44966. +
  44967. + /* Store the default values */
  44968. + mod_data.transport_type = USB_PR_BULK;
  44969. + mod_data.transport_name = "Bulk-only";
  44970. + mod_data.protocol_type = USB_SC_SCSI;
  44971. + mod_data.protocol_name = "Transparent SCSI";
  44972. +
  44973. + /* Some peripheral controllers are known not to be able to
  44974. + * halt bulk endpoints correctly. If one of them is present,
  44975. + * disable stalls.
  44976. + */
  44977. + if (gadget_is_at91(fsg->gadget))
  44978. + mod_data.can_stall = 0;
  44979. +
  44980. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  44981. + gcnum = usb_gadget_controller_number(fsg->gadget);
  44982. + if (gcnum >= 0)
  44983. + mod_data.release = 0x0300 + gcnum;
  44984. + else {
  44985. + WARNING(fsg, "controller '%s' not recognized\n",
  44986. + fsg->gadget->name);
  44987. + mod_data.release = 0x0399;
  44988. + }
  44989. + }
  44990. +
  44991. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  44992. +
  44993. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  44994. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  44995. + ; // Use default setting
  44996. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  44997. + mod_data.transport_type = USB_PR_CB;
  44998. + mod_data.transport_name = "Control-Bulk";
  44999. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  45000. + mod_data.transport_type = USB_PR_CBI;
  45001. + mod_data.transport_name = "Control-Bulk-Interrupt";
  45002. + } else {
  45003. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  45004. + return -EINVAL;
  45005. + }
  45006. +
  45007. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  45008. + prot == USB_SC_SCSI) {
  45009. + ; // Use default setting
  45010. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  45011. + prot == USB_SC_RBC) {
  45012. + mod_data.protocol_type = USB_SC_RBC;
  45013. + mod_data.protocol_name = "RBC";
  45014. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  45015. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  45016. + prot == USB_SC_8020) {
  45017. + mod_data.protocol_type = USB_SC_8020;
  45018. + mod_data.protocol_name = "8020i (ATAPI)";
  45019. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  45020. + prot == USB_SC_QIC) {
  45021. + mod_data.protocol_type = USB_SC_QIC;
  45022. + mod_data.protocol_name = "QIC-157";
  45023. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  45024. + prot == USB_SC_UFI) {
  45025. + mod_data.protocol_type = USB_SC_UFI;
  45026. + mod_data.protocol_name = "UFI";
  45027. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  45028. + prot == USB_SC_8070) {
  45029. + mod_data.protocol_type = USB_SC_8070;
  45030. + mod_data.protocol_name = "8070i";
  45031. + } else {
  45032. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  45033. + return -EINVAL;
  45034. + }
  45035. +
  45036. + mod_data.buflen &= PAGE_CACHE_MASK;
  45037. + if (mod_data.buflen <= 0) {
  45038. + ERROR(fsg, "invalid buflen\n");
  45039. + return -ETOOSMALL;
  45040. + }
  45041. +
  45042. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  45043. +
  45044. + /* Serial string handling.
  45045. + * On a real device, the serial string would be loaded
  45046. + * from permanent storage. */
  45047. + if (mod_data.serial) {
  45048. + const char *ch;
  45049. + unsigned len = 0;
  45050. +
  45051. + /* Sanity check :
  45052. + * The CB[I] specification limits the serial string to
  45053. + * 12 uppercase hexadecimal characters.
  45054. + * BBB need at least 12 uppercase hexadecimal characters,
  45055. + * with a maximum of 126. */
  45056. + for (ch = mod_data.serial; *ch; ++ch) {
  45057. + ++len;
  45058. + if ((*ch < '0' || *ch > '9') &&
  45059. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  45060. + WARNING(fsg,
  45061. + "Invalid serial string character: %c\n",
  45062. + *ch);
  45063. + goto no_serial;
  45064. + }
  45065. + }
  45066. + if (len > 126 ||
  45067. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  45068. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  45069. + WARNING(fsg, "Invalid serial string length!\n");
  45070. + goto no_serial;
  45071. + }
  45072. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  45073. + } else {
  45074. + WARNING(fsg, "No serial-number string provided!\n");
  45075. + no_serial:
  45076. + device_desc.iSerialNumber = 0;
  45077. + }
  45078. +
  45079. + return 0;
  45080. +}
  45081. +
  45082. +
  45083. +static int __init fsg_bind(struct usb_gadget *gadget)
  45084. +{
  45085. + struct fsg_dev *fsg = the_fsg;
  45086. + int rc;
  45087. + int i;
  45088. + struct fsg_lun *curlun;
  45089. + struct usb_ep *ep;
  45090. + struct usb_request *req;
  45091. + char *pathbuf, *p;
  45092. +
  45093. + fsg->gadget = gadget;
  45094. + set_gadget_data(gadget, fsg);
  45095. + fsg->ep0 = gadget->ep0;
  45096. + fsg->ep0->driver_data = fsg;
  45097. +
  45098. + if ((rc = check_parameters(fsg)) != 0)
  45099. + goto out;
  45100. +
  45101. + if (mod_data.removable) { // Enable the store_xxx attributes
  45102. + dev_attr_file.attr.mode = 0644;
  45103. + dev_attr_file.store = fsg_store_file;
  45104. + if (!mod_data.cdrom) {
  45105. + dev_attr_ro.attr.mode = 0644;
  45106. + dev_attr_ro.store = fsg_store_ro;
  45107. + }
  45108. + }
  45109. +
  45110. + /* Only for removable media? */
  45111. + dev_attr_nofua.attr.mode = 0644;
  45112. + dev_attr_nofua.store = fsg_store_nofua;
  45113. +
  45114. + /* Find out how many LUNs there should be */
  45115. + i = mod_data.nluns;
  45116. + if (i == 0)
  45117. + i = max(mod_data.num_filenames, 1u);
  45118. + if (i > FSG_MAX_LUNS) {
  45119. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  45120. + rc = -EINVAL;
  45121. + goto out;
  45122. + }
  45123. +
  45124. + /* Create the LUNs, open their backing files, and register the
  45125. + * LUN devices in sysfs. */
  45126. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  45127. + if (!fsg->luns) {
  45128. + rc = -ENOMEM;
  45129. + goto out;
  45130. + }
  45131. + fsg->nluns = i;
  45132. +
  45133. + for (i = 0; i < fsg->nluns; ++i) {
  45134. + curlun = &fsg->luns[i];
  45135. + curlun->cdrom = !!mod_data.cdrom;
  45136. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  45137. + curlun->initially_ro = curlun->ro;
  45138. + curlun->removable = mod_data.removable;
  45139. + curlun->nofua = mod_data.nofua[i];
  45140. + curlun->dev.release = lun_release;
  45141. + curlun->dev.parent = &gadget->dev;
  45142. + curlun->dev.driver = &fsg_driver.driver;
  45143. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  45144. + dev_set_name(&curlun->dev,"%s-lun%d",
  45145. + dev_name(&gadget->dev), i);
  45146. +
  45147. + kref_get(&fsg->ref);
  45148. + rc = device_register(&curlun->dev);
  45149. + if (rc) {
  45150. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  45151. + put_device(&curlun->dev);
  45152. + goto out;
  45153. + }
  45154. + curlun->registered = 1;
  45155. +
  45156. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  45157. + if (rc)
  45158. + goto out;
  45159. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  45160. + if (rc)
  45161. + goto out;
  45162. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  45163. + if (rc)
  45164. + goto out;
  45165. +
  45166. + if (mod_data.file[i] && *mod_data.file[i]) {
  45167. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  45168. + if (rc)
  45169. + goto out;
  45170. + } else if (!mod_data.removable) {
  45171. + ERROR(fsg, "no file given for LUN%d\n", i);
  45172. + rc = -EINVAL;
  45173. + goto out;
  45174. + }
  45175. + }
  45176. +
  45177. + /* Find all the endpoints we will use */
  45178. + usb_ep_autoconfig_reset(gadget);
  45179. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  45180. + if (!ep)
  45181. + goto autoconf_fail;
  45182. + ep->driver_data = fsg; // claim the endpoint
  45183. + fsg->bulk_in = ep;
  45184. +
  45185. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  45186. + if (!ep)
  45187. + goto autoconf_fail;
  45188. + ep->driver_data = fsg; // claim the endpoint
  45189. + fsg->bulk_out = ep;
  45190. +
  45191. + if (transport_is_cbi()) {
  45192. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  45193. + if (!ep)
  45194. + goto autoconf_fail;
  45195. + ep->driver_data = fsg; // claim the endpoint
  45196. + fsg->intr_in = ep;
  45197. + }
  45198. +
  45199. + /* Fix up the descriptors */
  45200. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  45201. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  45202. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  45203. +
  45204. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  45205. + fsg_intf_desc.bNumEndpoints = i;
  45206. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  45207. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  45208. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  45209. +
  45210. + if (gadget_is_dualspeed(gadget)) {
  45211. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  45212. +
  45213. + /* Assume endpoint addresses are the same for both speeds */
  45214. + fsg_hs_bulk_in_desc.bEndpointAddress =
  45215. + fsg_fs_bulk_in_desc.bEndpointAddress;
  45216. + fsg_hs_bulk_out_desc.bEndpointAddress =
  45217. + fsg_fs_bulk_out_desc.bEndpointAddress;
  45218. + fsg_hs_intr_in_desc.bEndpointAddress =
  45219. + fsg_fs_intr_in_desc.bEndpointAddress;
  45220. + }
  45221. +
  45222. + if (gadget_is_superspeed(gadget)) {
  45223. + unsigned max_burst;
  45224. +
  45225. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  45226. +
  45227. + /* Calculate bMaxBurst, we know packet size is 1024 */
  45228. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  45229. +
  45230. + /* Assume endpoint addresses are the same for both speeds */
  45231. + fsg_ss_bulk_in_desc.bEndpointAddress =
  45232. + fsg_fs_bulk_in_desc.bEndpointAddress;
  45233. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  45234. +
  45235. + fsg_ss_bulk_out_desc.bEndpointAddress =
  45236. + fsg_fs_bulk_out_desc.bEndpointAddress;
  45237. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  45238. + }
  45239. +
  45240. + if (gadget_is_otg(gadget))
  45241. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  45242. +
  45243. + rc = -ENOMEM;
  45244. +
  45245. + /* Allocate the request and buffer for endpoint 0 */
  45246. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  45247. + if (!req)
  45248. + goto out;
  45249. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  45250. + if (!req->buf)
  45251. + goto out;
  45252. + req->complete = ep0_complete;
  45253. +
  45254. + /* Allocate the data buffers */
  45255. + for (i = 0; i < fsg_num_buffers; ++i) {
  45256. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  45257. +
  45258. + /* Allocate for the bulk-in endpoint. We assume that
  45259. + * the buffer will also work with the bulk-out (and
  45260. + * interrupt-in) endpoint. */
  45261. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  45262. + if (!bh->buf)
  45263. + goto out;
  45264. + bh->next = bh + 1;
  45265. + }
  45266. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  45267. +
  45268. + /* This should reflect the actual gadget power source */
  45269. + usb_gadget_set_selfpowered(gadget);
  45270. +
  45271. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  45272. + "%s %s with %s",
  45273. + init_utsname()->sysname, init_utsname()->release,
  45274. + gadget->name);
  45275. +
  45276. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  45277. + "file-storage-gadget");
  45278. + if (IS_ERR(fsg->thread_task)) {
  45279. + rc = PTR_ERR(fsg->thread_task);
  45280. + goto out;
  45281. + }
  45282. +
  45283. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  45284. + INFO(fsg, "NOTE: This driver is deprecated. "
  45285. + "Consider using g_mass_storage instead.\n");
  45286. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  45287. +
  45288. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  45289. + for (i = 0; i < fsg->nluns; ++i) {
  45290. + curlun = &fsg->luns[i];
  45291. + if (fsg_lun_is_open(curlun)) {
  45292. + p = NULL;
  45293. + if (pathbuf) {
  45294. + p = d_path(&curlun->filp->f_path,
  45295. + pathbuf, PATH_MAX);
  45296. + if (IS_ERR(p))
  45297. + p = NULL;
  45298. + }
  45299. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  45300. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  45301. + }
  45302. + }
  45303. + kfree(pathbuf);
  45304. +
  45305. + DBG(fsg, "transport=%s (x%02x)\n",
  45306. + mod_data.transport_name, mod_data.transport_type);
  45307. + DBG(fsg, "protocol=%s (x%02x)\n",
  45308. + mod_data.protocol_name, mod_data.protocol_type);
  45309. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  45310. + mod_data.vendor, mod_data.product, mod_data.release);
  45311. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  45312. + mod_data.removable, mod_data.can_stall,
  45313. + mod_data.cdrom, mod_data.buflen);
  45314. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  45315. +
  45316. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  45317. +
  45318. + /* Tell the thread to start working */
  45319. + wake_up_process(fsg->thread_task);
  45320. + return 0;
  45321. +
  45322. +autoconf_fail:
  45323. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  45324. + rc = -ENOTSUPP;
  45325. +
  45326. +out:
  45327. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  45328. + fsg_unbind(gadget);
  45329. + complete(&fsg->thread_notifier);
  45330. + return rc;
  45331. +}
  45332. +
  45333. +
  45334. +/*-------------------------------------------------------------------------*/
  45335. +
  45336. +static void fsg_suspend(struct usb_gadget *gadget)
  45337. +{
  45338. + struct fsg_dev *fsg = get_gadget_data(gadget);
  45339. +
  45340. + DBG(fsg, "suspend\n");
  45341. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  45342. +}
  45343. +
  45344. +static void fsg_resume(struct usb_gadget *gadget)
  45345. +{
  45346. + struct fsg_dev *fsg = get_gadget_data(gadget);
  45347. +
  45348. + DBG(fsg, "resume\n");
  45349. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  45350. +}
  45351. +
  45352. +
  45353. +/*-------------------------------------------------------------------------*/
  45354. +
  45355. +static struct usb_gadget_driver fsg_driver = {
  45356. + .max_speed = USB_SPEED_SUPER,
  45357. + .function = (char *) fsg_string_product,
  45358. + .unbind = fsg_unbind,
  45359. + .disconnect = fsg_disconnect,
  45360. + .setup = fsg_setup,
  45361. + .suspend = fsg_suspend,
  45362. + .resume = fsg_resume,
  45363. +
  45364. + .driver = {
  45365. + .name = DRIVER_NAME,
  45366. + .owner = THIS_MODULE,
  45367. + // .release = ...
  45368. + // .suspend = ...
  45369. + // .resume = ...
  45370. + },
  45371. +};
  45372. +
  45373. +
  45374. +static int __init fsg_alloc(void)
  45375. +{
  45376. + struct fsg_dev *fsg;
  45377. +
  45378. + fsg = kzalloc(sizeof *fsg +
  45379. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  45380. +
  45381. + if (!fsg)
  45382. + return -ENOMEM;
  45383. + spin_lock_init(&fsg->lock);
  45384. + init_rwsem(&fsg->filesem);
  45385. + kref_init(&fsg->ref);
  45386. + init_completion(&fsg->thread_notifier);
  45387. +
  45388. + the_fsg = fsg;
  45389. + return 0;
  45390. +}
  45391. +
  45392. +
  45393. +static int __init fsg_init(void)
  45394. +{
  45395. + int rc;
  45396. + struct fsg_dev *fsg;
  45397. +
  45398. + rc = fsg_num_buffers_validate();
  45399. + if (rc != 0)
  45400. + return rc;
  45401. +
  45402. + if ((rc = fsg_alloc()) != 0)
  45403. + return rc;
  45404. + fsg = the_fsg;
  45405. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  45406. + kref_put(&fsg->ref, fsg_release);
  45407. + return rc;
  45408. +}
  45409. +module_init(fsg_init);
  45410. +
  45411. +
  45412. +static void __exit fsg_cleanup(void)
  45413. +{
  45414. + struct fsg_dev *fsg = the_fsg;
  45415. +
  45416. + /* Unregister the driver iff the thread hasn't already done so */
  45417. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  45418. + usb_gadget_unregister_driver(&fsg_driver);
  45419. +
  45420. + /* Wait for the thread to finish up */
  45421. + wait_for_completion(&fsg->thread_notifier);
  45422. +
  45423. + kref_put(&fsg->ref, fsg_release);
  45424. +}
  45425. +module_exit(fsg_cleanup);
  45426. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/changes.txt linux-3.16-rpi/drivers/usb/host/dwc_common_port/changes.txt
  45427. --- linux-3.16.2/drivers/usb/host/dwc_common_port/changes.txt 1970-01-01 01:00:00.000000000 +0100
  45428. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/changes.txt 2014-04-13 17:33:11.000000000 +0200
  45429. @@ -0,0 +1,174 @@
  45430. +
  45431. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  45432. +IO context struct. The IO context struct should live in an os-dependent struct
  45433. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  45434. +named 'os_dep' embedded in the main device struct. So there these calls look
  45435. +like this:
  45436. +
  45437. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  45438. +
  45439. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  45440. + &pcd->dev_global_regs->dcfg, 0);
  45441. +
  45442. +Note that for the existing Linux driver ports, it is not necessary to actually
  45443. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  45444. +require an IO context, its macros for dwc_read_reg32() and friends do not
  45445. +use the context pointer, so it is optimized away by the compiler. But it is
  45446. +necessary to add the pointer parameter to all of the call sites, to be ready
  45447. +for any future ports (such as FreeBSD) which do require an IO context.
  45448. +
  45449. +
  45450. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  45451. +take an additional parameter, a pointer to a memory context. Examples:
  45452. +
  45453. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  45454. +
  45455. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  45456. +
  45457. +Again, for the Linux ports, it is not necessary to actually define the memctx
  45458. +member, but it is necessary to add the pointer parameter to all of the call
  45459. +sites.
  45460. +
  45461. +
  45462. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  45463. +
  45464. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  45465. +
  45466. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  45467. +
  45468. +
  45469. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  45470. +
  45471. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  45472. +
  45473. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  45474. +
  45475. +
  45476. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  45477. +
  45478. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  45479. +
  45480. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  45481. +
  45482. +
  45483. +Same for dwc_timer_alloc(). Example:
  45484. +
  45485. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  45486. + cb_func, cb_data);
  45487. +
  45488. +
  45489. +Same for dwc_waitq_alloc(). Example:
  45490. +
  45491. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  45492. +
  45493. +
  45494. +Same for dwc_thread_run(). Example:
  45495. +
  45496. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  45497. + "dwc_usb3_thd1", data);
  45498. +
  45499. +
  45500. +Same for dwc_workq_alloc(). Example:
  45501. +
  45502. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  45503. +
  45504. +
  45505. +Same for dwc_task_alloc(). Example:
  45506. +
  45507. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  45508. + cb_func, cb_data);
  45509. +
  45510. +
  45511. +In addition to the context pointer additions, a few core functions have had
  45512. +other changes made to their parameters:
  45513. +
  45514. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  45515. +has been changed from a uint64_t to a dwc_irqflags_t.
  45516. +
  45517. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  45518. +FreeBSD equivalent of that function requires it.
  45519. +
  45520. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  45521. +'char *name' parameter, to be consistent with dwc_thread_run() and
  45522. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  45523. +requires a unique name.
  45524. +
  45525. +
  45526. +Here is a complete list of the core functions that now take a pointer to a
  45527. +context as their first parameter:
  45528. +
  45529. + dwc_read_reg32
  45530. + dwc_read_reg64
  45531. + dwc_write_reg32
  45532. + dwc_write_reg64
  45533. + dwc_modify_reg32
  45534. + dwc_modify_reg64
  45535. + dwc_alloc
  45536. + dwc_alloc_atomic
  45537. + dwc_strdup
  45538. + dwc_free
  45539. + dwc_dma_alloc
  45540. + dwc_dma_free
  45541. + dwc_mutex_alloc
  45542. + dwc_mutex_free
  45543. + dwc_spinlock_alloc
  45544. + dwc_spinlock_free
  45545. + dwc_timer_alloc
  45546. + dwc_waitq_alloc
  45547. + dwc_thread_run
  45548. + dwc_workq_alloc
  45549. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  45550. +
  45551. +And here are the core functions that have other changes to their parameters:
  45552. +
  45553. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  45554. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  45555. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  45556. +
  45557. +
  45558. +
  45559. +The changes to the core functions also require some of the other library
  45560. +functions to change:
  45561. +
  45562. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  45563. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  45564. + (for mutex allocation) as the 2nd param.
  45565. +
  45566. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  45567. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  45568. + 'void *memctx' as the 1st param.
  45569. +
  45570. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  45571. + 'void *memctx' as the 1st param.
  45572. +
  45573. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  45574. +
  45575. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  45576. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  45577. + param, and also now returns an integer value that is non-zero if
  45578. + allocation of its data structures or work queue fails.
  45579. +
  45580. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  45581. +
  45582. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  45583. + param, and also now returns an integer value that is non-zero if
  45584. + allocation of its data structures fails.
  45585. +
  45586. +
  45587. +
  45588. +Other miscellaneous changes:
  45589. +
  45590. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  45591. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  45592. +
  45593. +The following #define's have been added to allow selectively compiling library
  45594. +features:
  45595. +
  45596. + DWC_CCLIB
  45597. + DWC_CRYPTOLIB
  45598. + DWC_NOTIFYLIB
  45599. + DWC_UTFLIB
  45600. +
  45601. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  45602. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  45603. +library code directly into a driver module, instead of as a standalone module.
  45604. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/doc/doxygen.cfg linux-3.16-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  45605. --- linux-3.16.2/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  45606. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/doc/doxygen.cfg 2014-09-14 19:04:13.000000000 +0200
  45607. @@ -0,0 +1,270 @@
  45608. +# Doxyfile 1.4.5
  45609. +
  45610. +#---------------------------------------------------------------------------
  45611. +# Project related configuration options
  45612. +#---------------------------------------------------------------------------
  45613. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  45614. +PROJECT_NUMBER =
  45615. +OUTPUT_DIRECTORY = doc
  45616. +CREATE_SUBDIRS = NO
  45617. +OUTPUT_LANGUAGE = English
  45618. +BRIEF_MEMBER_DESC = YES
  45619. +REPEAT_BRIEF = YES
  45620. +ABBREVIATE_BRIEF = "The $name class" \
  45621. + "The $name widget" \
  45622. + "The $name file" \
  45623. + is \
  45624. + provides \
  45625. + specifies \
  45626. + contains \
  45627. + represents \
  45628. + a \
  45629. + an \
  45630. + the
  45631. +ALWAYS_DETAILED_SEC = YES
  45632. +INLINE_INHERITED_MEMB = NO
  45633. +FULL_PATH_NAMES = NO
  45634. +STRIP_FROM_PATH = ..
  45635. +STRIP_FROM_INC_PATH =
  45636. +SHORT_NAMES = NO
  45637. +JAVADOC_AUTOBRIEF = YES
  45638. +MULTILINE_CPP_IS_BRIEF = NO
  45639. +DETAILS_AT_TOP = YES
  45640. +INHERIT_DOCS = YES
  45641. +SEPARATE_MEMBER_PAGES = NO
  45642. +TAB_SIZE = 8
  45643. +ALIASES =
  45644. +OPTIMIZE_OUTPUT_FOR_C = YES
  45645. +OPTIMIZE_OUTPUT_JAVA = NO
  45646. +BUILTIN_STL_SUPPORT = NO
  45647. +DISTRIBUTE_GROUP_DOC = NO
  45648. +SUBGROUPING = NO
  45649. +#---------------------------------------------------------------------------
  45650. +# Build related configuration options
  45651. +#---------------------------------------------------------------------------
  45652. +EXTRACT_ALL = NO
  45653. +EXTRACT_PRIVATE = NO
  45654. +EXTRACT_STATIC = YES
  45655. +EXTRACT_LOCAL_CLASSES = NO
  45656. +EXTRACT_LOCAL_METHODS = NO
  45657. +HIDE_UNDOC_MEMBERS = NO
  45658. +HIDE_UNDOC_CLASSES = NO
  45659. +HIDE_FRIEND_COMPOUNDS = NO
  45660. +HIDE_IN_BODY_DOCS = NO
  45661. +INTERNAL_DOCS = NO
  45662. +CASE_SENSE_NAMES = YES
  45663. +HIDE_SCOPE_NAMES = NO
  45664. +SHOW_INCLUDE_FILES = NO
  45665. +INLINE_INFO = YES
  45666. +SORT_MEMBER_DOCS = NO
  45667. +SORT_BRIEF_DOCS = NO
  45668. +SORT_BY_SCOPE_NAME = NO
  45669. +GENERATE_TODOLIST = YES
  45670. +GENERATE_TESTLIST = YES
  45671. +GENERATE_BUGLIST = YES
  45672. +GENERATE_DEPRECATEDLIST= YES
  45673. +ENABLED_SECTIONS =
  45674. +MAX_INITIALIZER_LINES = 30
  45675. +SHOW_USED_FILES = YES
  45676. +SHOW_DIRECTORIES = YES
  45677. +FILE_VERSION_FILTER =
  45678. +#---------------------------------------------------------------------------
  45679. +# configuration options related to warning and progress messages
  45680. +#---------------------------------------------------------------------------
  45681. +QUIET = YES
  45682. +WARNINGS = YES
  45683. +WARN_IF_UNDOCUMENTED = NO
  45684. +WARN_IF_DOC_ERROR = YES
  45685. +WARN_NO_PARAMDOC = YES
  45686. +WARN_FORMAT = "$file:$line: $text"
  45687. +WARN_LOGFILE =
  45688. +#---------------------------------------------------------------------------
  45689. +# configuration options related to the input files
  45690. +#---------------------------------------------------------------------------
  45691. +INPUT = .
  45692. +FILE_PATTERNS = *.c \
  45693. + *.cc \
  45694. + *.cxx \
  45695. + *.cpp \
  45696. + *.c++ \
  45697. + *.d \
  45698. + *.java \
  45699. + *.ii \
  45700. + *.ixx \
  45701. + *.ipp \
  45702. + *.i++ \
  45703. + *.inl \
  45704. + *.h \
  45705. + *.hh \
  45706. + *.hxx \
  45707. + *.hpp \
  45708. + *.h++ \
  45709. + *.idl \
  45710. + *.odl \
  45711. + *.cs \
  45712. + *.php \
  45713. + *.php3 \
  45714. + *.inc \
  45715. + *.m \
  45716. + *.mm \
  45717. + *.dox \
  45718. + *.py \
  45719. + *.C \
  45720. + *.CC \
  45721. + *.C++ \
  45722. + *.II \
  45723. + *.I++ \
  45724. + *.H \
  45725. + *.HH \
  45726. + *.H++ \
  45727. + *.CS \
  45728. + *.PHP \
  45729. + *.PHP3 \
  45730. + *.M \
  45731. + *.MM \
  45732. + *.PY
  45733. +RECURSIVE = NO
  45734. +EXCLUDE =
  45735. +EXCLUDE_SYMLINKS = NO
  45736. +EXCLUDE_PATTERNS =
  45737. +EXAMPLE_PATH =
  45738. +EXAMPLE_PATTERNS = *
  45739. +EXAMPLE_RECURSIVE = NO
  45740. +IMAGE_PATH =
  45741. +INPUT_FILTER =
  45742. +FILTER_PATTERNS =
  45743. +FILTER_SOURCE_FILES = NO
  45744. +#---------------------------------------------------------------------------
  45745. +# configuration options related to source browsing
  45746. +#---------------------------------------------------------------------------
  45747. +SOURCE_BROWSER = NO
  45748. +INLINE_SOURCES = NO
  45749. +STRIP_CODE_COMMENTS = YES
  45750. +REFERENCED_BY_RELATION = YES
  45751. +REFERENCES_RELATION = YES
  45752. +USE_HTAGS = NO
  45753. +VERBATIM_HEADERS = NO
  45754. +#---------------------------------------------------------------------------
  45755. +# configuration options related to the alphabetical class index
  45756. +#---------------------------------------------------------------------------
  45757. +ALPHABETICAL_INDEX = NO
  45758. +COLS_IN_ALPHA_INDEX = 5
  45759. +IGNORE_PREFIX =
  45760. +#---------------------------------------------------------------------------
  45761. +# configuration options related to the HTML output
  45762. +#---------------------------------------------------------------------------
  45763. +GENERATE_HTML = YES
  45764. +HTML_OUTPUT = html
  45765. +HTML_FILE_EXTENSION = .html
  45766. +HTML_HEADER =
  45767. +HTML_FOOTER =
  45768. +HTML_STYLESHEET =
  45769. +HTML_ALIGN_MEMBERS = YES
  45770. +GENERATE_HTMLHELP = NO
  45771. +CHM_FILE =
  45772. +HHC_LOCATION =
  45773. +GENERATE_CHI = NO
  45774. +BINARY_TOC = NO
  45775. +TOC_EXPAND = NO
  45776. +DISABLE_INDEX = NO
  45777. +ENUM_VALUES_PER_LINE = 4
  45778. +GENERATE_TREEVIEW = YES
  45779. +TREEVIEW_WIDTH = 250
  45780. +#---------------------------------------------------------------------------
  45781. +# configuration options related to the LaTeX output
  45782. +#---------------------------------------------------------------------------
  45783. +GENERATE_LATEX = NO
  45784. +LATEX_OUTPUT = latex
  45785. +LATEX_CMD_NAME = latex
  45786. +MAKEINDEX_CMD_NAME = makeindex
  45787. +COMPACT_LATEX = NO
  45788. +PAPER_TYPE = a4wide
  45789. +EXTRA_PACKAGES =
  45790. +LATEX_HEADER =
  45791. +PDF_HYPERLINKS = NO
  45792. +USE_PDFLATEX = NO
  45793. +LATEX_BATCHMODE = NO
  45794. +LATEX_HIDE_INDICES = NO
  45795. +#---------------------------------------------------------------------------
  45796. +# configuration options related to the RTF output
  45797. +#---------------------------------------------------------------------------
  45798. +GENERATE_RTF = NO
  45799. +RTF_OUTPUT = rtf
  45800. +COMPACT_RTF = NO
  45801. +RTF_HYPERLINKS = NO
  45802. +RTF_STYLESHEET_FILE =
  45803. +RTF_EXTENSIONS_FILE =
  45804. +#---------------------------------------------------------------------------
  45805. +# configuration options related to the man page output
  45806. +#---------------------------------------------------------------------------
  45807. +GENERATE_MAN = NO
  45808. +MAN_OUTPUT = man
  45809. +MAN_EXTENSION = .3
  45810. +MAN_LINKS = NO
  45811. +#---------------------------------------------------------------------------
  45812. +# configuration options related to the XML output
  45813. +#---------------------------------------------------------------------------
  45814. +GENERATE_XML = NO
  45815. +XML_OUTPUT = xml
  45816. +XML_SCHEMA =
  45817. +XML_DTD =
  45818. +XML_PROGRAMLISTING = YES
  45819. +#---------------------------------------------------------------------------
  45820. +# configuration options for the AutoGen Definitions output
  45821. +#---------------------------------------------------------------------------
  45822. +GENERATE_AUTOGEN_DEF = NO
  45823. +#---------------------------------------------------------------------------
  45824. +# configuration options related to the Perl module output
  45825. +#---------------------------------------------------------------------------
  45826. +GENERATE_PERLMOD = NO
  45827. +PERLMOD_LATEX = NO
  45828. +PERLMOD_PRETTY = YES
  45829. +PERLMOD_MAKEVAR_PREFIX =
  45830. +#---------------------------------------------------------------------------
  45831. +# Configuration options related to the preprocessor
  45832. +#---------------------------------------------------------------------------
  45833. +ENABLE_PREPROCESSING = YES
  45834. +MACRO_EXPANSION = NO
  45835. +EXPAND_ONLY_PREDEF = NO
  45836. +SEARCH_INCLUDES = YES
  45837. +INCLUDE_PATH =
  45838. +INCLUDE_FILE_PATTERNS =
  45839. +PREDEFINED = DEBUG DEBUG_MEMORY
  45840. +EXPAND_AS_DEFINED =
  45841. +SKIP_FUNCTION_MACROS = YES
  45842. +#---------------------------------------------------------------------------
  45843. +# Configuration::additions related to external references
  45844. +#---------------------------------------------------------------------------
  45845. +TAGFILES =
  45846. +GENERATE_TAGFILE =
  45847. +ALLEXTERNALS = NO
  45848. +EXTERNAL_GROUPS = YES
  45849. +PERL_PATH = /usr/bin/perl
  45850. +#---------------------------------------------------------------------------
  45851. +# Configuration options related to the dot tool
  45852. +#---------------------------------------------------------------------------
  45853. +CLASS_DIAGRAMS = YES
  45854. +HIDE_UNDOC_RELATIONS = YES
  45855. +HAVE_DOT = NO
  45856. +CLASS_GRAPH = YES
  45857. +COLLABORATION_GRAPH = YES
  45858. +GROUP_GRAPHS = YES
  45859. +UML_LOOK = NO
  45860. +TEMPLATE_RELATIONS = NO
  45861. +INCLUDE_GRAPH = NO
  45862. +INCLUDED_BY_GRAPH = YES
  45863. +CALL_GRAPH = NO
  45864. +GRAPHICAL_HIERARCHY = YES
  45865. +DIRECTORY_GRAPH = YES
  45866. +DOT_IMAGE_FORMAT = png
  45867. +DOT_PATH =
  45868. +DOTFILE_DIRS =
  45869. +MAX_DOT_GRAPH_DEPTH = 1000
  45870. +DOT_TRANSPARENT = NO
  45871. +DOT_MULTI_TARGETS = NO
  45872. +GENERATE_LEGEND = YES
  45873. +DOT_CLEANUP = YES
  45874. +#---------------------------------------------------------------------------
  45875. +# Configuration::additions related to the search engine
  45876. +#---------------------------------------------------------------------------
  45877. +SEARCHENGINE = NO
  45878. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_cc.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c
  45879. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_cc.c 1970-01-01 01:00:00.000000000 +0100
  45880. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_cc.c 2014-09-14 19:04:13.000000000 +0200
  45881. @@ -0,0 +1,532 @@
  45882. +/* =========================================================================
  45883. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  45884. + * $Revision: #4 $
  45885. + * $Date: 2010/11/04 $
  45886. + * $Change: 1621692 $
  45887. + *
  45888. + * Synopsys Portability Library Software and documentation
  45889. + * (hereinafter, "Software") is an Unsupported proprietary work of
  45890. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  45891. + * between Synopsys and you.
  45892. + *
  45893. + * The Software IS NOT an item of Licensed Software or Licensed Product
  45894. + * under any End User Software License Agreement or Agreement for
  45895. + * Licensed Product with Synopsys or any supplement thereto. You are
  45896. + * permitted to use and redistribute this Software in source and binary
  45897. + * forms, with or without modification, provided that redistributions
  45898. + * of source code must retain this notice. You may not view, use,
  45899. + * disclose, copy or distribute this file or any information contained
  45900. + * herein except pursuant to this license grant from Synopsys. If you
  45901. + * do not agree with this notice, including the disclaimer below, then
  45902. + * you are not authorized to use the Software.
  45903. + *
  45904. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45905. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  45906. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  45907. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  45908. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  45909. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  45910. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  45911. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  45912. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  45913. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  45914. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  45915. + * DAMAGE.
  45916. + * ========================================================================= */
  45917. +#ifdef DWC_CCLIB
  45918. +
  45919. +#include "dwc_cc.h"
  45920. +
  45921. +typedef struct dwc_cc
  45922. +{
  45923. + uint32_t uid;
  45924. + uint8_t chid[16];
  45925. + uint8_t cdid[16];
  45926. + uint8_t ck[16];
  45927. + uint8_t *name;
  45928. + uint8_t length;
  45929. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  45930. +} dwc_cc_t;
  45931. +
  45932. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  45933. +
  45934. +/** The main structure for CC management. */
  45935. +struct dwc_cc_if
  45936. +{
  45937. + dwc_mutex_t *mutex;
  45938. + char *filename;
  45939. +
  45940. + unsigned is_host:1;
  45941. +
  45942. + dwc_notifier_t *notifier;
  45943. +
  45944. + struct context_list list;
  45945. +};
  45946. +
  45947. +#ifdef DEBUG
  45948. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  45949. +{
  45950. + int i;
  45951. + DWC_PRINTF("%s: ", name);
  45952. + for (i=0; i<len; i++) {
  45953. + DWC_PRINTF("%02x ", bytes[i]);
  45954. + }
  45955. + DWC_PRINTF("\n");
  45956. +}
  45957. +#else
  45958. +#define dump_bytes(x...)
  45959. +#endif
  45960. +
  45961. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  45962. +{
  45963. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  45964. + if (!cc) {
  45965. + return NULL;
  45966. + }
  45967. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  45968. +
  45969. + if (name) {
  45970. + cc->length = length;
  45971. + cc->name = dwc_alloc(mem_ctx, length);
  45972. + if (!cc->name) {
  45973. + dwc_free(mem_ctx, cc);
  45974. + return NULL;
  45975. + }
  45976. +
  45977. + DWC_MEMCPY(cc->name, name, length);
  45978. + }
  45979. +
  45980. + return cc;
  45981. +}
  45982. +
  45983. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  45984. +{
  45985. + if (cc->name) {
  45986. + dwc_free(mem_ctx, cc->name);
  45987. + }
  45988. + dwc_free(mem_ctx, cc);
  45989. +}
  45990. +
  45991. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  45992. +{
  45993. + uint32_t uid = 0;
  45994. + dwc_cc_t *cc;
  45995. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  45996. + if (cc->uid > uid) {
  45997. + uid = cc->uid;
  45998. + }
  45999. + }
  46000. +
  46001. + if (uid == 0) {
  46002. + uid = 255;
  46003. + }
  46004. +
  46005. + return uid + 1;
  46006. +}
  46007. +
  46008. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  46009. +{
  46010. + dwc_cc_t *cc;
  46011. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  46012. + if (cc->uid == uid) {
  46013. + return cc;
  46014. + }
  46015. + }
  46016. + return NULL;
  46017. +}
  46018. +
  46019. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  46020. +{
  46021. + unsigned int size = 0;
  46022. + dwc_cc_t *cc;
  46023. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  46024. + size += (48 + 1);
  46025. + if (cc->name) {
  46026. + size += cc->length;
  46027. + }
  46028. + }
  46029. + return size;
  46030. +}
  46031. +
  46032. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  46033. +{
  46034. + uint32_t uid = 0;
  46035. + dwc_cc_t *cc;
  46036. +
  46037. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  46038. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  46039. + uid = cc->uid;
  46040. + break;
  46041. + }
  46042. + }
  46043. + return uid;
  46044. +}
  46045. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  46046. +{
  46047. + uint32_t uid = 0;
  46048. + dwc_cc_t *cc;
  46049. +
  46050. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  46051. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  46052. + uid = cc->uid;
  46053. + break;
  46054. + }
  46055. + }
  46056. + return uid;
  46057. +}
  46058. +
  46059. +/* Internal cc_add */
  46060. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  46061. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  46062. +{
  46063. + dwc_cc_t *cc;
  46064. + uint32_t uid;
  46065. +
  46066. + if (cc_if->is_host) {
  46067. + uid = cc_match_cdid(cc_if, cdid);
  46068. + }
  46069. + else {
  46070. + uid = cc_match_chid(cc_if, chid);
  46071. + }
  46072. +
  46073. + if (uid) {
  46074. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  46075. + cc = cc_find(cc_if, uid);
  46076. + }
  46077. + else {
  46078. + cc = alloc_cc(mem_ctx, name, length);
  46079. + cc->uid = next_uid(cc_if);
  46080. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  46081. + }
  46082. +
  46083. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  46084. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  46085. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  46086. +
  46087. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  46088. + dump_bytes("CHID", cc->chid, 16);
  46089. + dump_bytes("CDID", cc->cdid, 16);
  46090. + dump_bytes("CK", cc->ck, 16);
  46091. + return cc->uid;
  46092. +}
  46093. +
  46094. +/* Internal cc_clear */
  46095. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  46096. +{
  46097. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  46098. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  46099. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  46100. + free_cc(mem_ctx, cc);
  46101. + }
  46102. +}
  46103. +
  46104. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  46105. + dwc_notifier_t *notifier, unsigned is_host)
  46106. +{
  46107. + dwc_cc_if_t *cc_if = NULL;
  46108. +
  46109. + /* Allocate a common_cc_if structure */
  46110. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  46111. +
  46112. + if (!cc_if)
  46113. + return NULL;
  46114. +
  46115. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  46116. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  46117. +#else
  46118. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  46119. +#endif
  46120. + if (!cc_if->mutex) {
  46121. + dwc_free(mem_ctx, cc_if);
  46122. + return NULL;
  46123. + }
  46124. +
  46125. + DWC_CIRCLEQ_INIT(&cc_if->list);
  46126. + cc_if->is_host = is_host;
  46127. + cc_if->notifier = notifier;
  46128. + return cc_if;
  46129. +}
  46130. +
  46131. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  46132. +{
  46133. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  46134. + DWC_MUTEX_FREE(cc_if->mutex);
  46135. +#else
  46136. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  46137. +#endif
  46138. + cc_clear(mem_ctx, cc_if);
  46139. + dwc_free(mem_ctx, cc_if);
  46140. +}
  46141. +
  46142. +static void cc_changed(dwc_cc_if_t *cc_if)
  46143. +{
  46144. + if (cc_if->notifier) {
  46145. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  46146. + }
  46147. +}
  46148. +
  46149. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  46150. +{
  46151. + DWC_MUTEX_LOCK(cc_if->mutex);
  46152. + cc_clear(mem_ctx, cc_if);
  46153. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46154. + cc_changed(cc_if);
  46155. +}
  46156. +
  46157. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  46158. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  46159. +{
  46160. + uint32_t uid;
  46161. +
  46162. + DWC_MUTEX_LOCK(cc_if->mutex);
  46163. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  46164. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46165. + cc_changed(cc_if);
  46166. +
  46167. + return uid;
  46168. +}
  46169. +
  46170. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  46171. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  46172. +{
  46173. + dwc_cc_t* cc;
  46174. +
  46175. + DWC_DEBUGC("Change connection context %d", id);
  46176. +
  46177. + DWC_MUTEX_LOCK(cc_if->mutex);
  46178. + cc = cc_find(cc_if, id);
  46179. + if (!cc) {
  46180. + DWC_ERROR("Uid %d not found in cc list\n", id);
  46181. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46182. + return;
  46183. + }
  46184. +
  46185. + if (chid) {
  46186. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  46187. + }
  46188. + if (cdid) {
  46189. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  46190. + }
  46191. + if (ck) {
  46192. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  46193. + }
  46194. +
  46195. + if (name) {
  46196. + if (cc->name) {
  46197. + dwc_free(mem_ctx, cc->name);
  46198. + }
  46199. + cc->name = dwc_alloc(mem_ctx, length);
  46200. + if (!cc->name) {
  46201. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  46202. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46203. + return;
  46204. + }
  46205. + cc->length = length;
  46206. + DWC_MEMCPY(cc->name, name, length);
  46207. + }
  46208. +
  46209. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46210. +
  46211. + cc_changed(cc_if);
  46212. +
  46213. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  46214. + dump_bytes("New CHID", cc->chid, 16);
  46215. + dump_bytes("New CDID", cc->cdid, 16);
  46216. + dump_bytes("New CK", cc->ck, 16);
  46217. +}
  46218. +
  46219. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  46220. +{
  46221. + dwc_cc_t *cc;
  46222. +
  46223. + DWC_DEBUGC("Removing connection context %d", id);
  46224. +
  46225. + DWC_MUTEX_LOCK(cc_if->mutex);
  46226. + cc = cc_find(cc_if, id);
  46227. + if (!cc) {
  46228. + DWC_ERROR("Uid %d not found in cc list\n", id);
  46229. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46230. + return;
  46231. + }
  46232. +
  46233. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  46234. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46235. + free_cc(mem_ctx, cc);
  46236. +
  46237. + cc_changed(cc_if);
  46238. +}
  46239. +
  46240. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  46241. +{
  46242. + uint8_t *buf, *x;
  46243. + uint8_t zero = 0;
  46244. + dwc_cc_t *cc;
  46245. +
  46246. + DWC_MUTEX_LOCK(cc_if->mutex);
  46247. + *length = cc_data_size(cc_if);
  46248. + if (!(*length)) {
  46249. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46250. + return NULL;
  46251. + }
  46252. +
  46253. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  46254. +
  46255. + buf = dwc_alloc(mem_ctx, *length);
  46256. + if (!buf) {
  46257. + *length = 0;
  46258. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46259. + return NULL;
  46260. + }
  46261. +
  46262. + x = buf;
  46263. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  46264. + DWC_MEMCPY(x, cc->chid, 16);
  46265. + x += 16;
  46266. + DWC_MEMCPY(x, cc->cdid, 16);
  46267. + x += 16;
  46268. + DWC_MEMCPY(x, cc->ck, 16);
  46269. + x += 16;
  46270. + if (cc->name) {
  46271. + DWC_MEMCPY(x, &cc->length, 1);
  46272. + x += 1;
  46273. + DWC_MEMCPY(x, cc->name, cc->length);
  46274. + x += cc->length;
  46275. + }
  46276. + else {
  46277. + DWC_MEMCPY(x, &zero, 1);
  46278. + x += 1;
  46279. + }
  46280. + }
  46281. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46282. +
  46283. + return buf;
  46284. +}
  46285. +
  46286. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  46287. +{
  46288. + uint8_t name_length;
  46289. + uint8_t *name;
  46290. + uint8_t *chid;
  46291. + uint8_t *cdid;
  46292. + uint8_t *ck;
  46293. + uint32_t i = 0;
  46294. +
  46295. + DWC_MUTEX_LOCK(cc_if->mutex);
  46296. + cc_clear(mem_ctx, cc_if);
  46297. +
  46298. + while (i < length) {
  46299. + chid = &data[i];
  46300. + i += 16;
  46301. + cdid = &data[i];
  46302. + i += 16;
  46303. + ck = &data[i];
  46304. + i += 16;
  46305. +
  46306. + name_length = data[i];
  46307. + i ++;
  46308. +
  46309. + if (name_length) {
  46310. + name = &data[i];
  46311. + i += name_length;
  46312. + }
  46313. + else {
  46314. + name = NULL;
  46315. + }
  46316. +
  46317. + /* check to see if we haven't overflown the buffer */
  46318. + if (i > length) {
  46319. + DWC_ERROR("Data format error while attempting to load CCs "
  46320. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  46321. + break;
  46322. + }
  46323. +
  46324. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  46325. + }
  46326. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46327. +
  46328. + cc_changed(cc_if);
  46329. +}
  46330. +
  46331. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  46332. +{
  46333. + uint32_t uid = 0;
  46334. +
  46335. + DWC_MUTEX_LOCK(cc_if->mutex);
  46336. + uid = cc_match_chid(cc_if, chid);
  46337. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46338. + return uid;
  46339. +}
  46340. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  46341. +{
  46342. + uint32_t uid = 0;
  46343. +
  46344. + DWC_MUTEX_LOCK(cc_if->mutex);
  46345. + uid = cc_match_cdid(cc_if, cdid);
  46346. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46347. + return uid;
  46348. +}
  46349. +
  46350. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  46351. +{
  46352. + uint8_t *ck = NULL;
  46353. + dwc_cc_t *cc;
  46354. +
  46355. + DWC_MUTEX_LOCK(cc_if->mutex);
  46356. + cc = cc_find(cc_if, id);
  46357. + if (cc) {
  46358. + ck = cc->ck;
  46359. + }
  46360. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46361. +
  46362. + return ck;
  46363. +
  46364. +}
  46365. +
  46366. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  46367. +{
  46368. + uint8_t *retval = NULL;
  46369. + dwc_cc_t *cc;
  46370. +
  46371. + DWC_MUTEX_LOCK(cc_if->mutex);
  46372. + cc = cc_find(cc_if, id);
  46373. + if (cc) {
  46374. + retval = cc->chid;
  46375. + }
  46376. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46377. +
  46378. + return retval;
  46379. +}
  46380. +
  46381. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  46382. +{
  46383. + uint8_t *retval = NULL;
  46384. + dwc_cc_t *cc;
  46385. +
  46386. + DWC_MUTEX_LOCK(cc_if->mutex);
  46387. + cc = cc_find(cc_if, id);
  46388. + if (cc) {
  46389. + retval = cc->cdid;
  46390. + }
  46391. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46392. +
  46393. + return retval;
  46394. +}
  46395. +
  46396. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  46397. +{
  46398. + uint8_t *retval = NULL;
  46399. + dwc_cc_t *cc;
  46400. +
  46401. + DWC_MUTEX_LOCK(cc_if->mutex);
  46402. + *length = 0;
  46403. + cc = cc_find(cc_if, id);
  46404. + if (cc) {
  46405. + *length = cc->length;
  46406. + retval = cc->name;
  46407. + }
  46408. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  46409. +
  46410. + return retval;
  46411. +}
  46412. +
  46413. +#endif /* DWC_CCLIB */
  46414. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_cc.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h
  46415. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_cc.h 1970-01-01 01:00:00.000000000 +0100
  46416. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_cc.h 2014-09-14 19:04:13.000000000 +0200
  46417. @@ -0,0 +1,224 @@
  46418. +/* =========================================================================
  46419. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  46420. + * $Revision: #4 $
  46421. + * $Date: 2010/09/28 $
  46422. + * $Change: 1596182 $
  46423. + *
  46424. + * Synopsys Portability Library Software and documentation
  46425. + * (hereinafter, "Software") is an Unsupported proprietary work of
  46426. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  46427. + * between Synopsys and you.
  46428. + *
  46429. + * The Software IS NOT an item of Licensed Software or Licensed Product
  46430. + * under any End User Software License Agreement or Agreement for
  46431. + * Licensed Product with Synopsys or any supplement thereto. You are
  46432. + * permitted to use and redistribute this Software in source and binary
  46433. + * forms, with or without modification, provided that redistributions
  46434. + * of source code must retain this notice. You may not view, use,
  46435. + * disclose, copy or distribute this file or any information contained
  46436. + * herein except pursuant to this license grant from Synopsys. If you
  46437. + * do not agree with this notice, including the disclaimer below, then
  46438. + * you are not authorized to use the Software.
  46439. + *
  46440. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  46441. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46442. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  46443. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  46444. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  46445. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  46446. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  46447. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  46448. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  46449. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  46450. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46451. + * DAMAGE.
  46452. + * ========================================================================= */
  46453. +#ifndef _DWC_CC_H_
  46454. +#define _DWC_CC_H_
  46455. +
  46456. +#ifdef __cplusplus
  46457. +extern "C" {
  46458. +#endif
  46459. +
  46460. +/** @file
  46461. + *
  46462. + * This file defines the Context Context library.
  46463. + *
  46464. + * The main data structure is dwc_cc_if_t which is returned by either the
  46465. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  46466. + * function. The data structure is opaque and should only be manipulated via the
  46467. + * functions provied in this API.
  46468. + *
  46469. + * It manages a list of connection contexts and operations can be performed to
  46470. + * add, remove, query, search, and change, those contexts. Additionally,
  46471. + * a dwc_notifier_t object can be requested from the manager so that
  46472. + * the user can be notified whenever the context list has changed.
  46473. + */
  46474. +
  46475. +#include "dwc_os.h"
  46476. +#include "dwc_list.h"
  46477. +#include "dwc_notifier.h"
  46478. +
  46479. +
  46480. +/* Notifications */
  46481. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  46482. +
  46483. +struct dwc_cc_if;
  46484. +typedef struct dwc_cc_if dwc_cc_if_t;
  46485. +
  46486. +
  46487. +/** @name Connection Context Operations */
  46488. +/** @{ */
  46489. +
  46490. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  46491. + * fields to default values, and returns a pointer to the structure or NULL on
  46492. + * error. */
  46493. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  46494. + dwc_notifier_t *notifier, unsigned is_host);
  46495. +
  46496. +/** Frees the memory for the specified CC structure allocated from
  46497. + * dwc_cc_if_alloc(). */
  46498. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  46499. +
  46500. +/** Removes all contexts from the connection context list */
  46501. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  46502. +
  46503. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  46504. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  46505. + * not overwritten.
  46506. + *
  46507. + * @param cc_if The cc_if structure.
  46508. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  46509. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  46510. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  46511. + * @param name An optional host friendly name as defined in the association model
  46512. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  46513. + * @param length The length othe unicode string.
  46514. + * @return A unique identifier used to refer to this context that is valid for
  46515. + * as long as this context is still in the list. */
  46516. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  46517. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  46518. + uint8_t length);
  46519. +
  46520. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  46521. + * list, preserving any accumulated statistics. This would typically be called
  46522. + * if the host decideds to change the context with a SET_CONNECTION request.
  46523. + *
  46524. + * @param cc_if The cc_if structure.
  46525. + * @param id The identifier of the connection context.
  46526. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  46527. + * indicates no change.
  46528. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  46529. + * indicates no change.
  46530. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  46531. + * indicates no change.
  46532. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  46533. + * @param length Length of name. */
  46534. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  46535. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  46536. + uint8_t *name, uint8_t length);
  46537. +
  46538. +/** Remove the specified connection context.
  46539. + * @param cc_if The cc_if structure.
  46540. + * @param id The identifier of the connection context to remove. */
  46541. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  46542. +
  46543. +/** Get a binary block of data for the connection context list and attributes.
  46544. + * This data can be used by the OS specific driver to save the connection
  46545. + * context list into non-volatile memory.
  46546. + *
  46547. + * @param cc_if The cc_if structure.
  46548. + * @param length Return the length of the data buffer.
  46549. + * @return A pointer to the data buffer. The memory for this buffer should be
  46550. + * freed with DWC_FREE() after use. */
  46551. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  46552. + unsigned int *length);
  46553. +
  46554. +/** Restore the connection context list from the binary data that was previously
  46555. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  46556. + * driver to load a connection context list from non-volatile memory.
  46557. + *
  46558. + * @param cc_if The cc_if structure.
  46559. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  46560. + * @param length The length of the data. */
  46561. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  46562. + uint8_t *data, unsigned int length);
  46563. +
  46564. +/** Find the connection context from the specified CHID.
  46565. + *
  46566. + * @param cc_if The cc_if structure.
  46567. + * @param chid A pointer to the CHID data.
  46568. + * @return A non-zero identifier of the connection context if the CHID matches.
  46569. + * Otherwise returns 0. */
  46570. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  46571. +
  46572. +/** Find the connection context from the specified CDID.
  46573. + *
  46574. + * @param cc_if The cc_if structure.
  46575. + * @param cdid A pointer to the CDID data.
  46576. + * @return A non-zero identifier of the connection context if the CHID matches.
  46577. + * Otherwise returns 0. */
  46578. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  46579. +
  46580. +/** Retrieve the CK from the specified connection context.
  46581. + *
  46582. + * @param cc_if The cc_if structure.
  46583. + * @param id The identifier of the connection context.
  46584. + * @return A pointer to the CK data. The memory does not need to be freed. */
  46585. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  46586. +
  46587. +/** Retrieve the CHID from the specified connection context.
  46588. + *
  46589. + * @param cc_if The cc_if structure.
  46590. + * @param id The identifier of the connection context.
  46591. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  46592. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  46593. +
  46594. +/** Retrieve the CDID from the specified connection context.
  46595. + *
  46596. + * @param cc_if The cc_if structure.
  46597. + * @param id The identifier of the connection context.
  46598. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  46599. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  46600. +
  46601. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  46602. +
  46603. +/** Checks a buffer for non-zero.
  46604. + * @param id A pointer to a 16 byte buffer.
  46605. + * @return true if the 16 byte value is non-zero. */
  46606. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  46607. + int i;
  46608. + for (i=0; i<16; i++) {
  46609. + if (id[i]) return 1;
  46610. + }
  46611. + return 0;
  46612. +}
  46613. +
  46614. +/** Checks a buffer for zero.
  46615. + * @param id A pointer to a 16 byte buffer.
  46616. + * @return true if the 16 byte value is zero. */
  46617. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  46618. + return !dwc_assoc_is_not_zero_id(id);
  46619. +}
  46620. +
  46621. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  46622. + * buffer. */
  46623. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  46624. + char *ptr = buffer;
  46625. + int i;
  46626. + for (i=0; i<16; i++) {
  46627. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  46628. + if (i < 15) {
  46629. + ptr += DWC_SPRINTF(ptr, " ");
  46630. + }
  46631. + }
  46632. + return ptr - buffer;
  46633. +}
  46634. +
  46635. +/** @} */
  46636. +
  46637. +#ifdef __cplusplus
  46638. +}
  46639. +#endif
  46640. +
  46641. +#endif /* _DWC_CC_H_ */
  46642. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  46643. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 1970-01-01 01:00:00.000000000 +0100
  46644. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c 2014-04-13 17:33:11.000000000 +0200
  46645. @@ -0,0 +1,1308 @@
  46646. +#include "dwc_os.h"
  46647. +#include "dwc_list.h"
  46648. +
  46649. +#ifdef DWC_CCLIB
  46650. +# include "dwc_cc.h"
  46651. +#endif
  46652. +
  46653. +#ifdef DWC_CRYPTOLIB
  46654. +# include "dwc_modpow.h"
  46655. +# include "dwc_dh.h"
  46656. +# include "dwc_crypto.h"
  46657. +#endif
  46658. +
  46659. +#ifdef DWC_NOTIFYLIB
  46660. +# include "dwc_notifier.h"
  46661. +#endif
  46662. +
  46663. +/* OS-Level Implementations */
  46664. +
  46665. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  46666. +
  46667. +
  46668. +/* MISC */
  46669. +
  46670. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  46671. +{
  46672. + return memset(dest, byte, size);
  46673. +}
  46674. +
  46675. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  46676. +{
  46677. + return memcpy(dest, src, size);
  46678. +}
  46679. +
  46680. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  46681. +{
  46682. + bcopy(src, dest, size);
  46683. + return dest;
  46684. +}
  46685. +
  46686. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  46687. +{
  46688. + return memcmp(m1, m2, size);
  46689. +}
  46690. +
  46691. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  46692. +{
  46693. + return strncmp(s1, s2, size);
  46694. +}
  46695. +
  46696. +int DWC_STRCMP(void *s1, void *s2)
  46697. +{
  46698. + return strcmp(s1, s2);
  46699. +}
  46700. +
  46701. +int DWC_STRLEN(char const *str)
  46702. +{
  46703. + return strlen(str);
  46704. +}
  46705. +
  46706. +char *DWC_STRCPY(char *to, char const *from)
  46707. +{
  46708. + return strcpy(to, from);
  46709. +}
  46710. +
  46711. +char *DWC_STRDUP(char const *str)
  46712. +{
  46713. + int len = DWC_STRLEN(str) + 1;
  46714. + char *new = DWC_ALLOC_ATOMIC(len);
  46715. +
  46716. + if (!new) {
  46717. + return NULL;
  46718. + }
  46719. +
  46720. + DWC_MEMCPY(new, str, len);
  46721. + return new;
  46722. +}
  46723. +
  46724. +int DWC_ATOI(char *str, int32_t *value)
  46725. +{
  46726. + char *end = NULL;
  46727. +
  46728. + *value = strtol(str, &end, 0);
  46729. + if (*end == '\0') {
  46730. + return 0;
  46731. + }
  46732. +
  46733. + return -1;
  46734. +}
  46735. +
  46736. +int DWC_ATOUI(char *str, uint32_t *value)
  46737. +{
  46738. + char *end = NULL;
  46739. +
  46740. + *value = strtoul(str, &end, 0);
  46741. + if (*end == '\0') {
  46742. + return 0;
  46743. + }
  46744. +
  46745. + return -1;
  46746. +}
  46747. +
  46748. +
  46749. +#ifdef DWC_UTFLIB
  46750. +/* From usbstring.c */
  46751. +
  46752. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  46753. +{
  46754. + int count = 0;
  46755. + u8 c;
  46756. + u16 uchar;
  46757. +
  46758. + /* this insists on correct encodings, though not minimal ones.
  46759. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  46760. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  46761. + */
  46762. + while (len != 0 && (c = (u8) *s++) != 0) {
  46763. + if (unlikely(c & 0x80)) {
  46764. + // 2-byte sequence:
  46765. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  46766. + if ((c & 0xe0) == 0xc0) {
  46767. + uchar = (c & 0x1f) << 6;
  46768. +
  46769. + c = (u8) *s++;
  46770. + if ((c & 0xc0) != 0xc0)
  46771. + goto fail;
  46772. + c &= 0x3f;
  46773. + uchar |= c;
  46774. +
  46775. + // 3-byte sequence (most CJKV characters):
  46776. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  46777. + } else if ((c & 0xf0) == 0xe0) {
  46778. + uchar = (c & 0x0f) << 12;
  46779. +
  46780. + c = (u8) *s++;
  46781. + if ((c & 0xc0) != 0xc0)
  46782. + goto fail;
  46783. + c &= 0x3f;
  46784. + uchar |= c << 6;
  46785. +
  46786. + c = (u8) *s++;
  46787. + if ((c & 0xc0) != 0xc0)
  46788. + goto fail;
  46789. + c &= 0x3f;
  46790. + uchar |= c;
  46791. +
  46792. + /* no bogus surrogates */
  46793. + if (0xd800 <= uchar && uchar <= 0xdfff)
  46794. + goto fail;
  46795. +
  46796. + // 4-byte sequence (surrogate pairs, currently rare):
  46797. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  46798. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  46799. + // (uuuuu = wwww + 1)
  46800. + // FIXME accept the surrogate code points (only)
  46801. + } else
  46802. + goto fail;
  46803. + } else
  46804. + uchar = c;
  46805. + put_unaligned (cpu_to_le16 (uchar), cp++);
  46806. + count++;
  46807. + len--;
  46808. + }
  46809. + return count;
  46810. +fail:
  46811. + return -1;
  46812. +}
  46813. +
  46814. +#endif /* DWC_UTFLIB */
  46815. +
  46816. +
  46817. +/* dwc_debug.h */
  46818. +
  46819. +dwc_bool_t DWC_IN_IRQ(void)
  46820. +{
  46821. +// return in_irq();
  46822. + return 0;
  46823. +}
  46824. +
  46825. +dwc_bool_t DWC_IN_BH(void)
  46826. +{
  46827. +// return in_softirq();
  46828. + return 0;
  46829. +}
  46830. +
  46831. +void DWC_VPRINTF(char *format, va_list args)
  46832. +{
  46833. + vprintf(format, args);
  46834. +}
  46835. +
  46836. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  46837. +{
  46838. + return vsnprintf(str, size, format, args);
  46839. +}
  46840. +
  46841. +void DWC_PRINTF(char *format, ...)
  46842. +{
  46843. + va_list args;
  46844. +
  46845. + va_start(args, format);
  46846. + DWC_VPRINTF(format, args);
  46847. + va_end(args);
  46848. +}
  46849. +
  46850. +int DWC_SPRINTF(char *buffer, char *format, ...)
  46851. +{
  46852. + int retval;
  46853. + va_list args;
  46854. +
  46855. + va_start(args, format);
  46856. + retval = vsprintf(buffer, format, args);
  46857. + va_end(args);
  46858. + return retval;
  46859. +}
  46860. +
  46861. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  46862. +{
  46863. + int retval;
  46864. + va_list args;
  46865. +
  46866. + va_start(args, format);
  46867. + retval = vsnprintf(buffer, size, format, args);
  46868. + va_end(args);
  46869. + return retval;
  46870. +}
  46871. +
  46872. +void __DWC_WARN(char *format, ...)
  46873. +{
  46874. + va_list args;
  46875. +
  46876. + va_start(args, format);
  46877. + DWC_VPRINTF(format, args);
  46878. + va_end(args);
  46879. +}
  46880. +
  46881. +void __DWC_ERROR(char *format, ...)
  46882. +{
  46883. + va_list args;
  46884. +
  46885. + va_start(args, format);
  46886. + DWC_VPRINTF(format, args);
  46887. + va_end(args);
  46888. +}
  46889. +
  46890. +void DWC_EXCEPTION(char *format, ...)
  46891. +{
  46892. + va_list args;
  46893. +
  46894. + va_start(args, format);
  46895. + DWC_VPRINTF(format, args);
  46896. + va_end(args);
  46897. +// BUG_ON(1); ???
  46898. +}
  46899. +
  46900. +#ifdef DEBUG
  46901. +void __DWC_DEBUG(char *format, ...)
  46902. +{
  46903. + va_list args;
  46904. +
  46905. + va_start(args, format);
  46906. + DWC_VPRINTF(format, args);
  46907. + va_end(args);
  46908. +}
  46909. +#endif
  46910. +
  46911. +
  46912. +/* dwc_mem.h */
  46913. +
  46914. +#if 0
  46915. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  46916. + uint32_t align,
  46917. + uint32_t alloc)
  46918. +{
  46919. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  46920. + size, align, alloc);
  46921. + return (dwc_pool_t *)pool;
  46922. +}
  46923. +
  46924. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  46925. +{
  46926. + dma_pool_destroy((struct dma_pool *)pool);
  46927. +}
  46928. +
  46929. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  46930. +{
  46931. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  46932. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  46933. +}
  46934. +
  46935. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  46936. +{
  46937. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  46938. + memset(..);
  46939. +}
  46940. +
  46941. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  46942. +{
  46943. + dma_pool_free(pool, vaddr, daddr);
  46944. +}
  46945. +#endif
  46946. +
  46947. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  46948. +{
  46949. + if (error)
  46950. + return;
  46951. + *(bus_addr_t *)arg = segs[0].ds_addr;
  46952. +}
  46953. +
  46954. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  46955. +{
  46956. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  46957. + int error;
  46958. +
  46959. + error = bus_dma_tag_create(
  46960. +#if __FreeBSD_version >= 700000
  46961. + bus_get_dma_tag(dma->dev), /* parent */
  46962. +#else
  46963. + NULL, /* parent */
  46964. +#endif
  46965. + 4, 0, /* alignment, bounds */
  46966. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  46967. + BUS_SPACE_MAXADDR, /* highaddr */
  46968. + NULL, NULL, /* filter, filterarg */
  46969. + size, /* maxsize */
  46970. + 1, /* nsegments */
  46971. + size, /* maxsegsize */
  46972. + 0, /* flags */
  46973. + NULL, /* lockfunc */
  46974. + NULL, /* lockarg */
  46975. + &dma->dma_tag);
  46976. + if (error) {
  46977. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  46978. + __func__, error);
  46979. + goto fail_0;
  46980. + }
  46981. +
  46982. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  46983. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  46984. + if (error) {
  46985. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  46986. + __func__, (uintmax_t)size, error);
  46987. + goto fail_1;
  46988. + }
  46989. +
  46990. + dma->dma_paddr = 0;
  46991. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  46992. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  46993. + if (error || dma->dma_paddr == 0) {
  46994. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  46995. + __func__, error);
  46996. + goto fail_2;
  46997. + }
  46998. +
  46999. + *dma_addr = dma->dma_paddr;
  47000. + return dma->dma_vaddr;
  47001. +
  47002. +fail_2:
  47003. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  47004. +fail_1:
  47005. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  47006. + bus_dma_tag_destroy(dma->dma_tag);
  47007. +fail_0:
  47008. + dma->dma_map = NULL;
  47009. + dma->dma_tag = NULL;
  47010. +
  47011. + return NULL;
  47012. +}
  47013. +
  47014. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  47015. +{
  47016. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  47017. +
  47018. + if (dma->dma_tag == NULL)
  47019. + return;
  47020. + if (dma->dma_map != NULL) {
  47021. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  47022. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  47023. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  47024. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  47025. + dma->dma_map = NULL;
  47026. + }
  47027. +
  47028. + bus_dma_tag_destroy(dma->dma_tag);
  47029. + dma->dma_tag = NULL;
  47030. +}
  47031. +
  47032. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  47033. +{
  47034. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  47035. +}
  47036. +
  47037. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  47038. +{
  47039. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  47040. +}
  47041. +
  47042. +void __DWC_FREE(void *mem_ctx, void *addr)
  47043. +{
  47044. + free(addr, M_DEVBUF);
  47045. +}
  47046. +
  47047. +
  47048. +#ifdef DWC_CRYPTOLIB
  47049. +/* dwc_crypto.h */
  47050. +
  47051. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  47052. +{
  47053. + get_random_bytes(buffer, length);
  47054. +}
  47055. +
  47056. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  47057. +{
  47058. + struct crypto_blkcipher *tfm;
  47059. + struct blkcipher_desc desc;
  47060. + struct scatterlist sgd;
  47061. + struct scatterlist sgs;
  47062. +
  47063. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  47064. + if (tfm == NULL) {
  47065. + printk("failed to load transform for aes CBC\n");
  47066. + return -1;
  47067. + }
  47068. +
  47069. + crypto_blkcipher_setkey(tfm, key, keylen);
  47070. + crypto_blkcipher_set_iv(tfm, iv, 16);
  47071. +
  47072. + sg_init_one(&sgd, out, messagelen);
  47073. + sg_init_one(&sgs, message, messagelen);
  47074. +
  47075. + desc.tfm = tfm;
  47076. + desc.flags = 0;
  47077. +
  47078. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  47079. + crypto_free_blkcipher(tfm);
  47080. + DWC_ERROR("AES CBC encryption failed");
  47081. + return -1;
  47082. + }
  47083. +
  47084. + crypto_free_blkcipher(tfm);
  47085. + return 0;
  47086. +}
  47087. +
  47088. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  47089. +{
  47090. + struct crypto_hash *tfm;
  47091. + struct hash_desc desc;
  47092. + struct scatterlist sg;
  47093. +
  47094. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  47095. + if (IS_ERR(tfm)) {
  47096. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  47097. + return 0;
  47098. + }
  47099. + desc.tfm = tfm;
  47100. + desc.flags = 0;
  47101. +
  47102. + sg_init_one(&sg, message, len);
  47103. + crypto_hash_digest(&desc, &sg, len, out);
  47104. + crypto_free_hash(tfm);
  47105. +
  47106. + return 1;
  47107. +}
  47108. +
  47109. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  47110. + uint8_t *key, uint32_t keylen, uint8_t *out)
  47111. +{
  47112. + struct crypto_hash *tfm;
  47113. + struct hash_desc desc;
  47114. + struct scatterlist sg;
  47115. +
  47116. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  47117. + if (IS_ERR(tfm)) {
  47118. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  47119. + return 0;
  47120. + }
  47121. + desc.tfm = tfm;
  47122. + desc.flags = 0;
  47123. +
  47124. + sg_init_one(&sg, message, messagelen);
  47125. + crypto_hash_setkey(tfm, key, keylen);
  47126. + crypto_hash_digest(&desc, &sg, messagelen, out);
  47127. + crypto_free_hash(tfm);
  47128. +
  47129. + return 1;
  47130. +}
  47131. +
  47132. +#endif /* DWC_CRYPTOLIB */
  47133. +
  47134. +
  47135. +/* Byte Ordering Conversions */
  47136. +
  47137. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  47138. +{
  47139. +#ifdef __LITTLE_ENDIAN
  47140. + return *p;
  47141. +#else
  47142. + uint8_t *u_p = (uint8_t *)p;
  47143. +
  47144. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47145. +#endif
  47146. +}
  47147. +
  47148. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  47149. +{
  47150. +#ifdef __BIG_ENDIAN
  47151. + return *p;
  47152. +#else
  47153. + uint8_t *u_p = (uint8_t *)p;
  47154. +
  47155. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47156. +#endif
  47157. +}
  47158. +
  47159. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  47160. +{
  47161. +#ifdef __LITTLE_ENDIAN
  47162. + return *p;
  47163. +#else
  47164. + uint8_t *u_p = (uint8_t *)p;
  47165. +
  47166. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47167. +#endif
  47168. +}
  47169. +
  47170. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  47171. +{
  47172. +#ifdef __BIG_ENDIAN
  47173. + return *p;
  47174. +#else
  47175. + uint8_t *u_p = (uint8_t *)p;
  47176. +
  47177. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  47178. +#endif
  47179. +}
  47180. +
  47181. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  47182. +{
  47183. +#ifdef __LITTLE_ENDIAN
  47184. + return *p;
  47185. +#else
  47186. + uint8_t *u_p = (uint8_t *)p;
  47187. + return (u_p[1] | (u_p[0] << 8));
  47188. +#endif
  47189. +}
  47190. +
  47191. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  47192. +{
  47193. +#ifdef __BIG_ENDIAN
  47194. + return *p;
  47195. +#else
  47196. + uint8_t *u_p = (uint8_t *)p;
  47197. + return (u_p[1] | (u_p[0] << 8));
  47198. +#endif
  47199. +}
  47200. +
  47201. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  47202. +{
  47203. +#ifdef __LITTLE_ENDIAN
  47204. + return *p;
  47205. +#else
  47206. + uint8_t *u_p = (uint8_t *)p;
  47207. + return (u_p[1] | (u_p[0] << 8));
  47208. +#endif
  47209. +}
  47210. +
  47211. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  47212. +{
  47213. +#ifdef __BIG_ENDIAN
  47214. + return *p;
  47215. +#else
  47216. + uint8_t *u_p = (uint8_t *)p;
  47217. + return (u_p[1] | (u_p[0] << 8));
  47218. +#endif
  47219. +}
  47220. +
  47221. +
  47222. +/* Registers */
  47223. +
  47224. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  47225. +{
  47226. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  47227. + bus_size_t ior = (bus_size_t)reg;
  47228. +
  47229. + return bus_space_read_4(io->iot, io->ioh, ior);
  47230. +}
  47231. +
  47232. +#if 0
  47233. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  47234. +{
  47235. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  47236. + bus_size_t ior = (bus_size_t)reg;
  47237. +
  47238. + return bus_space_read_8(io->iot, io->ioh, ior);
  47239. +}
  47240. +#endif
  47241. +
  47242. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  47243. +{
  47244. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  47245. + bus_size_t ior = (bus_size_t)reg;
  47246. +
  47247. + bus_space_write_4(io->iot, io->ioh, ior, value);
  47248. +}
  47249. +
  47250. +#if 0
  47251. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  47252. +{
  47253. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  47254. + bus_size_t ior = (bus_size_t)reg;
  47255. +
  47256. + bus_space_write_8(io->iot, io->ioh, ior, value);
  47257. +}
  47258. +#endif
  47259. +
  47260. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  47261. + uint32_t set_mask)
  47262. +{
  47263. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  47264. + bus_size_t ior = (bus_size_t)reg;
  47265. +
  47266. + bus_space_write_4(io->iot, io->ioh, ior,
  47267. + (bus_space_read_4(io->iot, io->ioh, ior) &
  47268. + ~clear_mask) | set_mask);
  47269. +}
  47270. +
  47271. +#if 0
  47272. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  47273. + uint64_t set_mask)
  47274. +{
  47275. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  47276. + bus_size_t ior = (bus_size_t)reg;
  47277. +
  47278. + bus_space_write_8(io->iot, io->ioh, ior,
  47279. + (bus_space_read_8(io->iot, io->ioh, ior) &
  47280. + ~clear_mask) | set_mask);
  47281. +}
  47282. +#endif
  47283. +
  47284. +
  47285. +/* Locking */
  47286. +
  47287. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  47288. +{
  47289. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  47290. +
  47291. + if (!sl) {
  47292. + DWC_ERROR("Cannot allocate memory for spinlock");
  47293. + return NULL;
  47294. + }
  47295. +
  47296. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  47297. + return (dwc_spinlock_t *)sl;
  47298. +}
  47299. +
  47300. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  47301. +{
  47302. + struct mtx *sl = (struct mtx *)lock;
  47303. +
  47304. + mtx_destroy(sl);
  47305. + DWC_FREE(sl);
  47306. +}
  47307. +
  47308. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  47309. +{
  47310. + mtx_lock_spin((struct mtx *)lock); // ???
  47311. +}
  47312. +
  47313. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  47314. +{
  47315. + mtx_unlock_spin((struct mtx *)lock); // ???
  47316. +}
  47317. +
  47318. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  47319. +{
  47320. + mtx_lock_spin((struct mtx *)lock);
  47321. +}
  47322. +
  47323. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  47324. +{
  47325. + mtx_unlock_spin((struct mtx *)lock);
  47326. +}
  47327. +
  47328. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  47329. +{
  47330. + struct mtx *m;
  47331. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  47332. +
  47333. + if (!mutex) {
  47334. + DWC_ERROR("Cannot allocate memory for mutex");
  47335. + return NULL;
  47336. + }
  47337. +
  47338. + m = (struct mtx *)mutex;
  47339. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  47340. + return mutex;
  47341. +}
  47342. +
  47343. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  47344. +#else
  47345. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  47346. +{
  47347. + mtx_destroy((struct mtx *)mutex);
  47348. + DWC_FREE(mutex);
  47349. +}
  47350. +#endif
  47351. +
  47352. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  47353. +{
  47354. + struct mtx *m = (struct mtx *)mutex;
  47355. +
  47356. + mtx_lock(m);
  47357. +}
  47358. +
  47359. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  47360. +{
  47361. + struct mtx *m = (struct mtx *)mutex;
  47362. +
  47363. + return mtx_trylock(m);
  47364. +}
  47365. +
  47366. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  47367. +{
  47368. + struct mtx *m = (struct mtx *)mutex;
  47369. +
  47370. + mtx_unlock(m);
  47371. +}
  47372. +
  47373. +
  47374. +/* Timing */
  47375. +
  47376. +void DWC_UDELAY(uint32_t usecs)
  47377. +{
  47378. + DELAY(usecs);
  47379. +}
  47380. +
  47381. +void DWC_MDELAY(uint32_t msecs)
  47382. +{
  47383. + do {
  47384. + DELAY(1000);
  47385. + } while (--msecs);
  47386. +}
  47387. +
  47388. +void DWC_MSLEEP(uint32_t msecs)
  47389. +{
  47390. + struct timeval tv;
  47391. +
  47392. + tv.tv_sec = msecs / 1000;
  47393. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  47394. + pause("dw3slp", tvtohz(&tv));
  47395. +}
  47396. +
  47397. +uint32_t DWC_TIME(void)
  47398. +{
  47399. + struct timeval tv;
  47400. +
  47401. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  47402. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  47403. +}
  47404. +
  47405. +
  47406. +/* Timers */
  47407. +
  47408. +struct dwc_timer {
  47409. + struct callout t;
  47410. + char *name;
  47411. + dwc_spinlock_t *lock;
  47412. + dwc_timer_callback_t cb;
  47413. + void *data;
  47414. +};
  47415. +
  47416. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  47417. +{
  47418. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  47419. +
  47420. + if (!t) {
  47421. + DWC_ERROR("Cannot allocate memory for timer");
  47422. + return NULL;
  47423. + }
  47424. +
  47425. + callout_init(&t->t, 1);
  47426. +
  47427. + t->name = DWC_STRDUP(name);
  47428. + if (!t->name) {
  47429. + DWC_ERROR("Cannot allocate memory for timer->name");
  47430. + goto no_name;
  47431. + }
  47432. +
  47433. + t->lock = DWC_SPINLOCK_ALLOC();
  47434. + if (!t->lock) {
  47435. + DWC_ERROR("Cannot allocate memory for lock");
  47436. + goto no_lock;
  47437. + }
  47438. +
  47439. + t->cb = cb;
  47440. + t->data = data;
  47441. +
  47442. + return t;
  47443. +
  47444. + no_lock:
  47445. + DWC_FREE(t->name);
  47446. + no_name:
  47447. + DWC_FREE(t);
  47448. +
  47449. + return NULL;
  47450. +}
  47451. +
  47452. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  47453. +{
  47454. + callout_stop(&timer->t);
  47455. + DWC_SPINLOCK_FREE(timer->lock);
  47456. + DWC_FREE(timer->name);
  47457. + DWC_FREE(timer);
  47458. +}
  47459. +
  47460. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  47461. +{
  47462. + struct timeval tv;
  47463. +
  47464. + tv.tv_sec = time / 1000;
  47465. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  47466. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  47467. +}
  47468. +
  47469. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  47470. +{
  47471. + callout_stop(&timer->t);
  47472. +}
  47473. +
  47474. +
  47475. +/* Wait Queues */
  47476. +
  47477. +struct dwc_waitq {
  47478. + struct mtx lock;
  47479. + int abort;
  47480. +};
  47481. +
  47482. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  47483. +{
  47484. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  47485. +
  47486. + if (!wq) {
  47487. + DWC_ERROR("Cannot allocate memory for waitqueue");
  47488. + return NULL;
  47489. + }
  47490. +
  47491. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  47492. + wq->abort = 0;
  47493. +
  47494. + return wq;
  47495. +}
  47496. +
  47497. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  47498. +{
  47499. + mtx_destroy(&wq->lock);
  47500. + DWC_FREE(wq);
  47501. +}
  47502. +
  47503. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  47504. +{
  47505. +// intrmask_t ipl;
  47506. + int result = 0;
  47507. +
  47508. + mtx_lock(&wq->lock);
  47509. +// ipl = splbio();
  47510. +
  47511. + /* Skip the sleep if already aborted or triggered */
  47512. + if (!wq->abort && !cond(data)) {
  47513. +// splx(ipl);
  47514. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  47515. +// ipl = splbio();
  47516. + }
  47517. +
  47518. + if (result == ERESTART) { // signaled - restart
  47519. + result = -DWC_E_RESTART;
  47520. +
  47521. + } else if (result == EINTR) { // signaled - interrupt
  47522. + result = -DWC_E_ABORT;
  47523. +
  47524. + } else if (wq->abort) {
  47525. + result = -DWC_E_ABORT;
  47526. +
  47527. + } else {
  47528. + result = 0;
  47529. + }
  47530. +
  47531. + wq->abort = 0;
  47532. +// splx(ipl);
  47533. + mtx_unlock(&wq->lock);
  47534. + return result;
  47535. +}
  47536. +
  47537. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  47538. + void *data, int32_t msecs)
  47539. +{
  47540. + struct timeval tv, tv1, tv2;
  47541. +// intrmask_t ipl;
  47542. + int result = 0;
  47543. +
  47544. + tv.tv_sec = msecs / 1000;
  47545. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  47546. +
  47547. + mtx_lock(&wq->lock);
  47548. +// ipl = splbio();
  47549. +
  47550. + /* Skip the sleep if already aborted or triggered */
  47551. + if (!wq->abort && !cond(data)) {
  47552. +// splx(ipl);
  47553. + getmicrouptime(&tv1);
  47554. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  47555. + getmicrouptime(&tv2);
  47556. +// ipl = splbio();
  47557. + }
  47558. +
  47559. + if (result == 0) { // awoken
  47560. + if (wq->abort) {
  47561. + result = -DWC_E_ABORT;
  47562. + } else {
  47563. + tv2.tv_usec -= tv1.tv_usec;
  47564. + if (tv2.tv_usec < 0) {
  47565. + tv2.tv_usec += 1000000;
  47566. + tv2.tv_sec--;
  47567. + }
  47568. +
  47569. + tv2.tv_sec -= tv1.tv_sec;
  47570. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  47571. + result = msecs - result;
  47572. + if (result <= 0)
  47573. + result = 1;
  47574. + }
  47575. + } else if (result == ERESTART) { // signaled - restart
  47576. + result = -DWC_E_RESTART;
  47577. +
  47578. + } else if (result == EINTR) { // signaled - interrupt
  47579. + result = -DWC_E_ABORT;
  47580. +
  47581. + } else { // timed out
  47582. + result = -DWC_E_TIMEOUT;
  47583. + }
  47584. +
  47585. + wq->abort = 0;
  47586. +// splx(ipl);
  47587. + mtx_unlock(&wq->lock);
  47588. + return result;
  47589. +}
  47590. +
  47591. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  47592. +{
  47593. + wakeup(wq);
  47594. +}
  47595. +
  47596. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  47597. +{
  47598. +// intrmask_t ipl;
  47599. +
  47600. + mtx_lock(&wq->lock);
  47601. +// ipl = splbio();
  47602. + wq->abort = 1;
  47603. + wakeup(wq);
  47604. +// splx(ipl);
  47605. + mtx_unlock(&wq->lock);
  47606. +}
  47607. +
  47608. +
  47609. +/* Threading */
  47610. +
  47611. +struct dwc_thread {
  47612. + struct proc *proc;
  47613. + int abort;
  47614. +};
  47615. +
  47616. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  47617. +{
  47618. + int retval;
  47619. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  47620. +
  47621. + if (!thread) {
  47622. + return NULL;
  47623. + }
  47624. +
  47625. + thread->abort = 0;
  47626. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  47627. + RFPROC | RFNOWAIT, 0, "%s", name);
  47628. + if (retval) {
  47629. + DWC_FREE(thread);
  47630. + return NULL;
  47631. + }
  47632. +
  47633. + return thread;
  47634. +}
  47635. +
  47636. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  47637. +{
  47638. + int retval;
  47639. +
  47640. + thread->abort = 1;
  47641. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  47642. +
  47643. + if (retval == 0) {
  47644. + /* DWC_THREAD_EXIT() will free the thread struct */
  47645. + return 0;
  47646. + }
  47647. +
  47648. + /* NOTE: We leak the thread struct if thread doesn't die */
  47649. +
  47650. + if (retval == EWOULDBLOCK) {
  47651. + return -DWC_E_TIMEOUT;
  47652. + }
  47653. +
  47654. + return -DWC_E_UNKNOWN;
  47655. +}
  47656. +
  47657. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  47658. +{
  47659. + return thread->abort;
  47660. +}
  47661. +
  47662. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  47663. +{
  47664. + wakeup(&thread->abort);
  47665. + DWC_FREE(thread);
  47666. + kthread_exit(0);
  47667. +}
  47668. +
  47669. +
  47670. +/* tasklets
  47671. + - Runs in interrupt context (cannot sleep)
  47672. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  47673. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  47674. + */
  47675. +struct dwc_tasklet {
  47676. + struct task t;
  47677. + dwc_tasklet_callback_t cb;
  47678. + void *data;
  47679. +};
  47680. +
  47681. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  47682. +{
  47683. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  47684. +
  47685. + task->cb(task->data);
  47686. +}
  47687. +
  47688. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  47689. +{
  47690. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  47691. +
  47692. + if (task) {
  47693. + task->cb = cb;
  47694. + task->data = data;
  47695. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  47696. + } else {
  47697. + DWC_ERROR("Cannot allocate memory for tasklet");
  47698. + }
  47699. +
  47700. + return task;
  47701. +}
  47702. +
  47703. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  47704. +{
  47705. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  47706. + DWC_FREE(task);
  47707. +}
  47708. +
  47709. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  47710. +{
  47711. + /* Uses predefined system queue */
  47712. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  47713. +}
  47714. +
  47715. +
  47716. +/* workqueues
  47717. + - Runs in process context (can sleep)
  47718. + */
  47719. +typedef struct work_container {
  47720. + dwc_work_callback_t cb;
  47721. + void *data;
  47722. + dwc_workq_t *wq;
  47723. + char *name;
  47724. + int hz;
  47725. +
  47726. +#ifdef DEBUG
  47727. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  47728. +#endif
  47729. + struct task task;
  47730. +} work_container_t;
  47731. +
  47732. +#ifdef DEBUG
  47733. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  47734. +#endif
  47735. +
  47736. +struct dwc_workq {
  47737. + struct taskqueue *taskq;
  47738. + dwc_spinlock_t *lock;
  47739. + dwc_waitq_t *waitq;
  47740. + int pending;
  47741. +
  47742. +#ifdef DEBUG
  47743. + struct work_container_queue entries;
  47744. +#endif
  47745. +};
  47746. +
  47747. +static void do_work(void *data, int pending) // what to do with pending ???
  47748. +{
  47749. + work_container_t *container = (work_container_t *)data;
  47750. + dwc_workq_t *wq = container->wq;
  47751. + dwc_irqflags_t flags;
  47752. +
  47753. + if (container->hz) {
  47754. + pause("dw3wrk", container->hz);
  47755. + }
  47756. +
  47757. + container->cb(container->data);
  47758. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  47759. +
  47760. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  47761. +
  47762. +#ifdef DEBUG
  47763. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  47764. +#endif
  47765. + if (container->name)
  47766. + DWC_FREE(container->name);
  47767. + DWC_FREE(container);
  47768. + wq->pending--;
  47769. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  47770. + DWC_WAITQ_TRIGGER(wq->waitq);
  47771. +}
  47772. +
  47773. +static int work_done(void *data)
  47774. +{
  47775. + dwc_workq_t *workq = (dwc_workq_t *)data;
  47776. +
  47777. + return workq->pending == 0;
  47778. +}
  47779. +
  47780. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  47781. +{
  47782. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  47783. +}
  47784. +
  47785. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  47786. +{
  47787. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  47788. +
  47789. + if (!wq) {
  47790. + DWC_ERROR("Cannot allocate memory for workqueue");
  47791. + return NULL;
  47792. + }
  47793. +
  47794. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  47795. + if (!wq->taskq) {
  47796. + DWC_ERROR("Cannot allocate memory for taskqueue");
  47797. + goto no_taskq;
  47798. + }
  47799. +
  47800. + wq->pending = 0;
  47801. +
  47802. + wq->lock = DWC_SPINLOCK_ALLOC();
  47803. + if (!wq->lock) {
  47804. + DWC_ERROR("Cannot allocate memory for spinlock");
  47805. + goto no_lock;
  47806. + }
  47807. +
  47808. + wq->waitq = DWC_WAITQ_ALLOC();
  47809. + if (!wq->waitq) {
  47810. + DWC_ERROR("Cannot allocate memory for waitqueue");
  47811. + goto no_waitq;
  47812. + }
  47813. +
  47814. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  47815. +
  47816. +#ifdef DEBUG
  47817. + DWC_CIRCLEQ_INIT(&wq->entries);
  47818. +#endif
  47819. + return wq;
  47820. +
  47821. + no_waitq:
  47822. + DWC_SPINLOCK_FREE(wq->lock);
  47823. + no_lock:
  47824. + taskqueue_free(wq->taskq);
  47825. + no_taskq:
  47826. + DWC_FREE(wq);
  47827. +
  47828. + return NULL;
  47829. +}
  47830. +
  47831. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  47832. +{
  47833. +#ifdef DEBUG
  47834. + dwc_irqflags_t flags;
  47835. +
  47836. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  47837. +
  47838. + if (wq->pending != 0) {
  47839. + struct work_container *container;
  47840. +
  47841. + DWC_ERROR("Destroying work queue with pending work");
  47842. +
  47843. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  47844. + DWC_ERROR("Work %s still pending", container->name);
  47845. + }
  47846. + }
  47847. +
  47848. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  47849. +#endif
  47850. + DWC_WAITQ_FREE(wq->waitq);
  47851. + DWC_SPINLOCK_FREE(wq->lock);
  47852. + taskqueue_free(wq->taskq);
  47853. + DWC_FREE(wq);
  47854. +}
  47855. +
  47856. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  47857. + char *format, ...)
  47858. +{
  47859. + dwc_irqflags_t flags;
  47860. + work_container_t *container;
  47861. + static char name[128];
  47862. + va_list args;
  47863. +
  47864. + va_start(args, format);
  47865. + DWC_VSNPRINTF(name, 128, format, args);
  47866. + va_end(args);
  47867. +
  47868. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  47869. + wq->pending++;
  47870. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  47871. + DWC_WAITQ_TRIGGER(wq->waitq);
  47872. +
  47873. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  47874. + if (!container) {
  47875. + DWC_ERROR("Cannot allocate memory for container");
  47876. + return;
  47877. + }
  47878. +
  47879. + container->name = DWC_STRDUP(name);
  47880. + if (!container->name) {
  47881. + DWC_ERROR("Cannot allocate memory for container->name");
  47882. + DWC_FREE(container);
  47883. + return;
  47884. + }
  47885. +
  47886. + container->cb = cb;
  47887. + container->data = data;
  47888. + container->wq = wq;
  47889. + container->hz = 0;
  47890. +
  47891. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  47892. +
  47893. + TASK_INIT(&container->task, 0, do_work, container);
  47894. +
  47895. +#ifdef DEBUG
  47896. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  47897. +#endif
  47898. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  47899. +}
  47900. +
  47901. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  47902. + void *data, uint32_t time, char *format, ...)
  47903. +{
  47904. + dwc_irqflags_t flags;
  47905. + work_container_t *container;
  47906. + static char name[128];
  47907. + struct timeval tv;
  47908. + va_list args;
  47909. +
  47910. + va_start(args, format);
  47911. + DWC_VSNPRINTF(name, 128, format, args);
  47912. + va_end(args);
  47913. +
  47914. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  47915. + wq->pending++;
  47916. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  47917. + DWC_WAITQ_TRIGGER(wq->waitq);
  47918. +
  47919. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  47920. + if (!container) {
  47921. + DWC_ERROR("Cannot allocate memory for container");
  47922. + return;
  47923. + }
  47924. +
  47925. + container->name = DWC_STRDUP(name);
  47926. + if (!container->name) {
  47927. + DWC_ERROR("Cannot allocate memory for container->name");
  47928. + DWC_FREE(container);
  47929. + return;
  47930. + }
  47931. +
  47932. + container->cb = cb;
  47933. + container->data = data;
  47934. + container->wq = wq;
  47935. +
  47936. + tv.tv_sec = time / 1000;
  47937. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  47938. + container->hz = tvtohz(&tv);
  47939. +
  47940. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  47941. +
  47942. + TASK_INIT(&container->task, 0, do_work, container);
  47943. +
  47944. +#ifdef DEBUG
  47945. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  47946. +#endif
  47947. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  47948. +}
  47949. +
  47950. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  47951. +{
  47952. + return wq->pending;
  47953. +}
  47954. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_common_linux.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  47955. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_common_linux.c 1970-01-01 01:00:00.000000000 +0100
  47956. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_common_linux.c 2014-09-14 19:04:13.000000000 +0200
  47957. @@ -0,0 +1,1440 @@
  47958. +#include <linux/kernel.h>
  47959. +#include <linux/init.h>
  47960. +#include <linux/module.h>
  47961. +#include <linux/kthread.h>
  47962. +
  47963. +#ifdef DWC_CCLIB
  47964. +# include "dwc_cc.h"
  47965. +#endif
  47966. +
  47967. +#ifdef DWC_CRYPTOLIB
  47968. +# include "dwc_modpow.h"
  47969. +# include "dwc_dh.h"
  47970. +# include "dwc_crypto.h"
  47971. +#endif
  47972. +
  47973. +#ifdef DWC_NOTIFYLIB
  47974. +# include "dwc_notifier.h"
  47975. +#endif
  47976. +
  47977. +/* OS-Level Implementations */
  47978. +
  47979. +/* This is the Linux kernel implementation of the DWC platform library. */
  47980. +#include <linux/moduleparam.h>
  47981. +#include <linux/ctype.h>
  47982. +#include <linux/crypto.h>
  47983. +#include <linux/delay.h>
  47984. +#include <linux/device.h>
  47985. +#include <linux/dma-mapping.h>
  47986. +#include <linux/cdev.h>
  47987. +#include <linux/errno.h>
  47988. +#include <linux/interrupt.h>
  47989. +#include <linux/jiffies.h>
  47990. +#include <linux/list.h>
  47991. +#include <linux/pci.h>
  47992. +#include <linux/random.h>
  47993. +#include <linux/scatterlist.h>
  47994. +#include <linux/slab.h>
  47995. +#include <linux/stat.h>
  47996. +#include <linux/string.h>
  47997. +#include <linux/timer.h>
  47998. +#include <linux/usb.h>
  47999. +
  48000. +#include <linux/version.h>
  48001. +
  48002. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  48003. +# include <linux/usb/gadget.h>
  48004. +#else
  48005. +# include <linux/usb_gadget.h>
  48006. +#endif
  48007. +
  48008. +#include <asm/io.h>
  48009. +#include <asm/page.h>
  48010. +#include <asm/uaccess.h>
  48011. +#include <asm/unaligned.h>
  48012. +
  48013. +#include "dwc_os.h"
  48014. +#include "dwc_list.h"
  48015. +
  48016. +
  48017. +/* MISC */
  48018. +
  48019. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  48020. +{
  48021. + return memset(dest, byte, size);
  48022. +}
  48023. +
  48024. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  48025. +{
  48026. + return memcpy(dest, src, size);
  48027. +}
  48028. +
  48029. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  48030. +{
  48031. + return memmove(dest, src, size);
  48032. +}
  48033. +
  48034. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  48035. +{
  48036. + return memcmp(m1, m2, size);
  48037. +}
  48038. +
  48039. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  48040. +{
  48041. + return strncmp(s1, s2, size);
  48042. +}
  48043. +
  48044. +int DWC_STRCMP(void *s1, void *s2)
  48045. +{
  48046. + return strcmp(s1, s2);
  48047. +}
  48048. +
  48049. +int DWC_STRLEN(char const *str)
  48050. +{
  48051. + return strlen(str);
  48052. +}
  48053. +
  48054. +char *DWC_STRCPY(char *to, char const *from)
  48055. +{
  48056. + return strcpy(to, from);
  48057. +}
  48058. +
  48059. +char *DWC_STRDUP(char const *str)
  48060. +{
  48061. + int len = DWC_STRLEN(str) + 1;
  48062. + char *new = DWC_ALLOC_ATOMIC(len);
  48063. +
  48064. + if (!new) {
  48065. + return NULL;
  48066. + }
  48067. +
  48068. + DWC_MEMCPY(new, str, len);
  48069. + return new;
  48070. +}
  48071. +
  48072. +int DWC_ATOI(const char *str, int32_t *value)
  48073. +{
  48074. + char *end = NULL;
  48075. +
  48076. + *value = simple_strtol(str, &end, 0);
  48077. + if (*end == '\0') {
  48078. + return 0;
  48079. + }
  48080. +
  48081. + return -1;
  48082. +}
  48083. +
  48084. +int DWC_ATOUI(const char *str, uint32_t *value)
  48085. +{
  48086. + char *end = NULL;
  48087. +
  48088. + *value = simple_strtoul(str, &end, 0);
  48089. + if (*end == '\0') {
  48090. + return 0;
  48091. + }
  48092. +
  48093. + return -1;
  48094. +}
  48095. +
  48096. +
  48097. +#ifdef DWC_UTFLIB
  48098. +/* From usbstring.c */
  48099. +
  48100. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  48101. +{
  48102. + int count = 0;
  48103. + u8 c;
  48104. + u16 uchar;
  48105. +
  48106. + /* this insists on correct encodings, though not minimal ones.
  48107. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  48108. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  48109. + */
  48110. + while (len != 0 && (c = (u8) *s++) != 0) {
  48111. + if (unlikely(c & 0x80)) {
  48112. + // 2-byte sequence:
  48113. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  48114. + if ((c & 0xe0) == 0xc0) {
  48115. + uchar = (c & 0x1f) << 6;
  48116. +
  48117. + c = (u8) *s++;
  48118. + if ((c & 0xc0) != 0xc0)
  48119. + goto fail;
  48120. + c &= 0x3f;
  48121. + uchar |= c;
  48122. +
  48123. + // 3-byte sequence (most CJKV characters):
  48124. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  48125. + } else if ((c & 0xf0) == 0xe0) {
  48126. + uchar = (c & 0x0f) << 12;
  48127. +
  48128. + c = (u8) *s++;
  48129. + if ((c & 0xc0) != 0xc0)
  48130. + goto fail;
  48131. + c &= 0x3f;
  48132. + uchar |= c << 6;
  48133. +
  48134. + c = (u8) *s++;
  48135. + if ((c & 0xc0) != 0xc0)
  48136. + goto fail;
  48137. + c &= 0x3f;
  48138. + uchar |= c;
  48139. +
  48140. + /* no bogus surrogates */
  48141. + if (0xd800 <= uchar && uchar <= 0xdfff)
  48142. + goto fail;
  48143. +
  48144. + // 4-byte sequence (surrogate pairs, currently rare):
  48145. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  48146. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  48147. + // (uuuuu = wwww + 1)
  48148. + // FIXME accept the surrogate code points (only)
  48149. + } else
  48150. + goto fail;
  48151. + } else
  48152. + uchar = c;
  48153. + put_unaligned (cpu_to_le16 (uchar), cp++);
  48154. + count++;
  48155. + len--;
  48156. + }
  48157. + return count;
  48158. +fail:
  48159. + return -1;
  48160. +}
  48161. +#endif /* DWC_UTFLIB */
  48162. +
  48163. +
  48164. +/* dwc_debug.h */
  48165. +
  48166. +dwc_bool_t DWC_IN_IRQ(void)
  48167. +{
  48168. + return in_irq();
  48169. +}
  48170. +
  48171. +dwc_bool_t DWC_IN_BH(void)
  48172. +{
  48173. + return in_softirq();
  48174. +}
  48175. +
  48176. +void DWC_VPRINTF(char *format, va_list args)
  48177. +{
  48178. + vprintk(format, args);
  48179. +}
  48180. +
  48181. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  48182. +{
  48183. + return vsnprintf(str, size, format, args);
  48184. +}
  48185. +
  48186. +void DWC_PRINTF(char *format, ...)
  48187. +{
  48188. + va_list args;
  48189. +
  48190. + va_start(args, format);
  48191. + DWC_VPRINTF(format, args);
  48192. + va_end(args);
  48193. +}
  48194. +
  48195. +int DWC_SPRINTF(char *buffer, char *format, ...)
  48196. +{
  48197. + int retval;
  48198. + va_list args;
  48199. +
  48200. + va_start(args, format);
  48201. + retval = vsprintf(buffer, format, args);
  48202. + va_end(args);
  48203. + return retval;
  48204. +}
  48205. +
  48206. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  48207. +{
  48208. + int retval;
  48209. + va_list args;
  48210. +
  48211. + va_start(args, format);
  48212. + retval = vsnprintf(buffer, size, format, args);
  48213. + va_end(args);
  48214. + return retval;
  48215. +}
  48216. +
  48217. +void __DWC_WARN(char *format, ...)
  48218. +{
  48219. + va_list args;
  48220. +
  48221. + va_start(args, format);
  48222. + DWC_PRINTF(KERN_WARNING);
  48223. + DWC_VPRINTF(format, args);
  48224. + va_end(args);
  48225. +}
  48226. +
  48227. +void __DWC_ERROR(char *format, ...)
  48228. +{
  48229. + va_list args;
  48230. +
  48231. + va_start(args, format);
  48232. + DWC_PRINTF(KERN_ERR);
  48233. + DWC_VPRINTF(format, args);
  48234. + va_end(args);
  48235. +}
  48236. +
  48237. +void DWC_EXCEPTION(char *format, ...)
  48238. +{
  48239. + va_list args;
  48240. +
  48241. + va_start(args, format);
  48242. + DWC_PRINTF(KERN_ERR);
  48243. + DWC_VPRINTF(format, args);
  48244. + va_end(args);
  48245. + BUG_ON(1);
  48246. +}
  48247. +
  48248. +#ifdef DEBUG
  48249. +void __DWC_DEBUG(char *format, ...)
  48250. +{
  48251. + va_list args;
  48252. +
  48253. + va_start(args, format);
  48254. + DWC_PRINTF(KERN_DEBUG);
  48255. + DWC_VPRINTF(format, args);
  48256. + va_end(args);
  48257. +}
  48258. +#endif
  48259. +
  48260. +
  48261. +/* dwc_mem.h */
  48262. +
  48263. +#if 0
  48264. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  48265. + uint32_t align,
  48266. + uint32_t alloc)
  48267. +{
  48268. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  48269. + size, align, alloc);
  48270. + return (dwc_pool_t *)pool;
  48271. +}
  48272. +
  48273. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  48274. +{
  48275. + dma_pool_destroy((struct dma_pool *)pool);
  48276. +}
  48277. +
  48278. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  48279. +{
  48280. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  48281. +}
  48282. +
  48283. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  48284. +{
  48285. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  48286. + memset(..);
  48287. +}
  48288. +
  48289. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  48290. +{
  48291. + dma_pool_free(pool, vaddr, daddr);
  48292. +}
  48293. +#endif
  48294. +
  48295. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  48296. +{
  48297. +#ifdef xxCOSIM /* Only works for 32-bit cosim */
  48298. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL);
  48299. +#else
  48300. + void *buf = dma_alloc_coherent(dma_ctx, (size_t)size, dma_addr, GFP_KERNEL | GFP_DMA32);
  48301. +#endif
  48302. + if (!buf) {
  48303. + return NULL;
  48304. + }
  48305. +
  48306. + memset(buf, 0, (size_t)size);
  48307. + return buf;
  48308. +}
  48309. +
  48310. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  48311. +{
  48312. + void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_ATOMIC);
  48313. + if (!buf) {
  48314. + return NULL;
  48315. + }
  48316. + memset(buf, 0, (size_t)size);
  48317. + return buf;
  48318. +}
  48319. +
  48320. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  48321. +{
  48322. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  48323. +}
  48324. +
  48325. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  48326. +{
  48327. + return kzalloc(size, GFP_KERNEL);
  48328. +}
  48329. +
  48330. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  48331. +{
  48332. + return kzalloc(size, GFP_ATOMIC);
  48333. +}
  48334. +
  48335. +void __DWC_FREE(void *mem_ctx, void *addr)
  48336. +{
  48337. + kfree(addr);
  48338. +}
  48339. +
  48340. +
  48341. +#ifdef DWC_CRYPTOLIB
  48342. +/* dwc_crypto.h */
  48343. +
  48344. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  48345. +{
  48346. + get_random_bytes(buffer, length);
  48347. +}
  48348. +
  48349. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  48350. +{
  48351. + struct crypto_blkcipher *tfm;
  48352. + struct blkcipher_desc desc;
  48353. + struct scatterlist sgd;
  48354. + struct scatterlist sgs;
  48355. +
  48356. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  48357. + if (tfm == NULL) {
  48358. + printk("failed to load transform for aes CBC\n");
  48359. + return -1;
  48360. + }
  48361. +
  48362. + crypto_blkcipher_setkey(tfm, key, keylen);
  48363. + crypto_blkcipher_set_iv(tfm, iv, 16);
  48364. +
  48365. + sg_init_one(&sgd, out, messagelen);
  48366. + sg_init_one(&sgs, message, messagelen);
  48367. +
  48368. + desc.tfm = tfm;
  48369. + desc.flags = 0;
  48370. +
  48371. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  48372. + crypto_free_blkcipher(tfm);
  48373. + DWC_ERROR("AES CBC encryption failed");
  48374. + return -1;
  48375. + }
  48376. +
  48377. + crypto_free_blkcipher(tfm);
  48378. + return 0;
  48379. +}
  48380. +
  48381. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  48382. +{
  48383. + struct crypto_hash *tfm;
  48384. + struct hash_desc desc;
  48385. + struct scatterlist sg;
  48386. +
  48387. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  48388. + if (IS_ERR(tfm)) {
  48389. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  48390. + return 0;
  48391. + }
  48392. + desc.tfm = tfm;
  48393. + desc.flags = 0;
  48394. +
  48395. + sg_init_one(&sg, message, len);
  48396. + crypto_hash_digest(&desc, &sg, len, out);
  48397. + crypto_free_hash(tfm);
  48398. +
  48399. + return 1;
  48400. +}
  48401. +
  48402. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  48403. + uint8_t *key, uint32_t keylen, uint8_t *out)
  48404. +{
  48405. + struct crypto_hash *tfm;
  48406. + struct hash_desc desc;
  48407. + struct scatterlist sg;
  48408. +
  48409. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  48410. + if (IS_ERR(tfm)) {
  48411. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  48412. + return 0;
  48413. + }
  48414. + desc.tfm = tfm;
  48415. + desc.flags = 0;
  48416. +
  48417. + sg_init_one(&sg, message, messagelen);
  48418. + crypto_hash_setkey(tfm, key, keylen);
  48419. + crypto_hash_digest(&desc, &sg, messagelen, out);
  48420. + crypto_free_hash(tfm);
  48421. +
  48422. + return 1;
  48423. +}
  48424. +#endif /* DWC_CRYPTOLIB */
  48425. +
  48426. +
  48427. +/* Byte Ordering Conversions */
  48428. +
  48429. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  48430. +{
  48431. +#ifdef __LITTLE_ENDIAN
  48432. + return *p;
  48433. +#else
  48434. + uint8_t *u_p = (uint8_t *)p;
  48435. +
  48436. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48437. +#endif
  48438. +}
  48439. +
  48440. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  48441. +{
  48442. +#ifdef __BIG_ENDIAN
  48443. + return *p;
  48444. +#else
  48445. + uint8_t *u_p = (uint8_t *)p;
  48446. +
  48447. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48448. +#endif
  48449. +}
  48450. +
  48451. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  48452. +{
  48453. +#ifdef __LITTLE_ENDIAN
  48454. + return *p;
  48455. +#else
  48456. + uint8_t *u_p = (uint8_t *)p;
  48457. +
  48458. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48459. +#endif
  48460. +}
  48461. +
  48462. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  48463. +{
  48464. +#ifdef __BIG_ENDIAN
  48465. + return *p;
  48466. +#else
  48467. + uint8_t *u_p = (uint8_t *)p;
  48468. +
  48469. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  48470. +#endif
  48471. +}
  48472. +
  48473. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  48474. +{
  48475. +#ifdef __LITTLE_ENDIAN
  48476. + return *p;
  48477. +#else
  48478. + uint8_t *u_p = (uint8_t *)p;
  48479. + return (u_p[1] | (u_p[0] << 8));
  48480. +#endif
  48481. +}
  48482. +
  48483. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  48484. +{
  48485. +#ifdef __BIG_ENDIAN
  48486. + return *p;
  48487. +#else
  48488. + uint8_t *u_p = (uint8_t *)p;
  48489. + return (u_p[1] | (u_p[0] << 8));
  48490. +#endif
  48491. +}
  48492. +
  48493. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  48494. +{
  48495. +#ifdef __LITTLE_ENDIAN
  48496. + return *p;
  48497. +#else
  48498. + uint8_t *u_p = (uint8_t *)p;
  48499. + return (u_p[1] | (u_p[0] << 8));
  48500. +#endif
  48501. +}
  48502. +
  48503. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  48504. +{
  48505. +#ifdef __BIG_ENDIAN
  48506. + return *p;
  48507. +#else
  48508. + uint8_t *u_p = (uint8_t *)p;
  48509. + return (u_p[1] | (u_p[0] << 8));
  48510. +#endif
  48511. +}
  48512. +
  48513. +
  48514. +/* Registers */
  48515. +
  48516. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  48517. +{
  48518. + return readl(reg);
  48519. +}
  48520. +
  48521. +#if 0
  48522. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  48523. +{
  48524. +}
  48525. +#endif
  48526. +
  48527. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  48528. +{
  48529. + writel(value, reg);
  48530. +}
  48531. +
  48532. +#if 0
  48533. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  48534. +{
  48535. +}
  48536. +#endif
  48537. +
  48538. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  48539. +{
  48540. + unsigned long flags;
  48541. +
  48542. + local_irq_save(flags);
  48543. + local_fiq_disable();
  48544. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  48545. + local_fiq_enable();
  48546. + local_irq_restore(flags);
  48547. +}
  48548. +
  48549. +#if 0
  48550. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  48551. +{
  48552. +}
  48553. +#endif
  48554. +
  48555. +
  48556. +/* Locking */
  48557. +
  48558. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  48559. +{
  48560. + spinlock_t *sl = (spinlock_t *)1;
  48561. +
  48562. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  48563. + sl = DWC_ALLOC(sizeof(*sl));
  48564. + if (!sl) {
  48565. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  48566. + return NULL;
  48567. + }
  48568. +
  48569. + spin_lock_init(sl);
  48570. +#endif
  48571. + return (dwc_spinlock_t *)sl;
  48572. +}
  48573. +
  48574. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  48575. +{
  48576. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  48577. + DWC_FREE(lock);
  48578. +#endif
  48579. +}
  48580. +
  48581. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  48582. +{
  48583. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  48584. + spin_lock((spinlock_t *)lock);
  48585. +#endif
  48586. +}
  48587. +
  48588. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  48589. +{
  48590. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  48591. + spin_unlock((spinlock_t *)lock);
  48592. +#endif
  48593. +}
  48594. +
  48595. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  48596. +{
  48597. + dwc_irqflags_t f;
  48598. +
  48599. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  48600. + spin_lock_irqsave((spinlock_t *)lock, f);
  48601. +#else
  48602. + local_irq_save(f);
  48603. +#endif
  48604. + *flags = f;
  48605. +}
  48606. +
  48607. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  48608. +{
  48609. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  48610. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  48611. +#else
  48612. + local_irq_restore(flags);
  48613. +#endif
  48614. +}
  48615. +
  48616. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  48617. +{
  48618. + struct mutex *m;
  48619. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  48620. +
  48621. + if (!mutex) {
  48622. + DWC_ERROR("Cannot allocate memory for mutex\n");
  48623. + return NULL;
  48624. + }
  48625. +
  48626. + m = (struct mutex *)mutex;
  48627. + mutex_init(m);
  48628. + return mutex;
  48629. +}
  48630. +
  48631. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  48632. +#else
  48633. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  48634. +{
  48635. + mutex_destroy((struct mutex *)mutex);
  48636. + DWC_FREE(mutex);
  48637. +}
  48638. +#endif
  48639. +
  48640. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  48641. +{
  48642. + struct mutex *m = (struct mutex *)mutex;
  48643. + mutex_lock(m);
  48644. +}
  48645. +
  48646. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  48647. +{
  48648. + struct mutex *m = (struct mutex *)mutex;
  48649. + return mutex_trylock(m);
  48650. +}
  48651. +
  48652. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  48653. +{
  48654. + struct mutex *m = (struct mutex *)mutex;
  48655. + mutex_unlock(m);
  48656. +}
  48657. +
  48658. +
  48659. +/* Timing */
  48660. +
  48661. +void DWC_UDELAY(uint32_t usecs)
  48662. +{
  48663. + udelay(usecs);
  48664. +}
  48665. +
  48666. +void DWC_MDELAY(uint32_t msecs)
  48667. +{
  48668. + mdelay(msecs);
  48669. +}
  48670. +
  48671. +void DWC_MSLEEP(uint32_t msecs)
  48672. +{
  48673. + msleep(msecs);
  48674. +}
  48675. +
  48676. +uint32_t DWC_TIME(void)
  48677. +{
  48678. + return jiffies_to_msecs(jiffies);
  48679. +}
  48680. +
  48681. +
  48682. +/* Timers */
  48683. +
  48684. +struct dwc_timer {
  48685. + struct timer_list *t;
  48686. + char *name;
  48687. + dwc_timer_callback_t cb;
  48688. + void *data;
  48689. + uint8_t scheduled;
  48690. + dwc_spinlock_t *lock;
  48691. +};
  48692. +
  48693. +static void timer_callback(unsigned long data)
  48694. +{
  48695. + dwc_timer_t *timer = (dwc_timer_t *)data;
  48696. + dwc_irqflags_t flags;
  48697. +
  48698. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  48699. + timer->scheduled = 0;
  48700. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  48701. + DWC_DEBUGC("Timer %s callback", timer->name);
  48702. + timer->cb(timer->data);
  48703. +}
  48704. +
  48705. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  48706. +{
  48707. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  48708. +
  48709. + if (!t) {
  48710. + DWC_ERROR("Cannot allocate memory for timer");
  48711. + return NULL;
  48712. + }
  48713. +
  48714. + t->t = DWC_ALLOC(sizeof(*t->t));
  48715. + if (!t->t) {
  48716. + DWC_ERROR("Cannot allocate memory for timer->t");
  48717. + goto no_timer;
  48718. + }
  48719. +
  48720. + t->name = DWC_STRDUP(name);
  48721. + if (!t->name) {
  48722. + DWC_ERROR("Cannot allocate memory for timer->name");
  48723. + goto no_name;
  48724. + }
  48725. +
  48726. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  48727. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
  48728. +#else
  48729. + t->lock = DWC_SPINLOCK_ALLOC();
  48730. +#endif
  48731. + if (!t->lock) {
  48732. + DWC_ERROR("Cannot allocate memory for lock");
  48733. + goto no_lock;
  48734. + }
  48735. +
  48736. + t->scheduled = 0;
  48737. + t->t->base = &boot_tvec_bases;
  48738. + t->t->expires = jiffies;
  48739. + setup_timer(t->t, timer_callback, (unsigned long)t);
  48740. +
  48741. + t->cb = cb;
  48742. + t->data = data;
  48743. +
  48744. + return t;
  48745. +
  48746. + no_lock:
  48747. + DWC_FREE(t->name);
  48748. + no_name:
  48749. + DWC_FREE(t->t);
  48750. + no_timer:
  48751. + DWC_FREE(t);
  48752. + return NULL;
  48753. +}
  48754. +
  48755. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  48756. +{
  48757. + dwc_irqflags_t flags;
  48758. +
  48759. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  48760. +
  48761. + if (timer->scheduled) {
  48762. + del_timer(timer->t);
  48763. + timer->scheduled = 0;
  48764. + }
  48765. +
  48766. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  48767. + DWC_SPINLOCK_FREE(timer->lock);
  48768. + DWC_FREE(timer->t);
  48769. + DWC_FREE(timer->name);
  48770. + DWC_FREE(timer);
  48771. +}
  48772. +
  48773. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  48774. +{
  48775. + dwc_irqflags_t flags;
  48776. +
  48777. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  48778. +
  48779. + if (!timer->scheduled) {
  48780. + timer->scheduled = 1;
  48781. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  48782. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  48783. + add_timer(timer->t);
  48784. + } else {
  48785. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  48786. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  48787. + }
  48788. +
  48789. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  48790. +}
  48791. +
  48792. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  48793. +{
  48794. + del_timer(timer->t);
  48795. +}
  48796. +
  48797. +
  48798. +/* Wait Queues */
  48799. +
  48800. +struct dwc_waitq {
  48801. + wait_queue_head_t queue;
  48802. + int abort;
  48803. +};
  48804. +
  48805. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  48806. +{
  48807. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  48808. +
  48809. + if (!wq) {
  48810. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  48811. + return NULL;
  48812. + }
  48813. +
  48814. + init_waitqueue_head(&wq->queue);
  48815. + wq->abort = 0;
  48816. + return wq;
  48817. +}
  48818. +
  48819. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  48820. +{
  48821. + DWC_FREE(wq);
  48822. +}
  48823. +
  48824. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  48825. +{
  48826. + int result = wait_event_interruptible(wq->queue,
  48827. + cond(data) || wq->abort);
  48828. + if (result == -ERESTARTSYS) {
  48829. + wq->abort = 0;
  48830. + return -DWC_E_RESTART;
  48831. + }
  48832. +
  48833. + if (wq->abort == 1) {
  48834. + wq->abort = 0;
  48835. + return -DWC_E_ABORT;
  48836. + }
  48837. +
  48838. + wq->abort = 0;
  48839. +
  48840. + if (result == 0) {
  48841. + return 0;
  48842. + }
  48843. +
  48844. + return -DWC_E_UNKNOWN;
  48845. +}
  48846. +
  48847. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  48848. + void *data, int32_t msecs)
  48849. +{
  48850. + int32_t tmsecs;
  48851. + int result = wait_event_interruptible_timeout(wq->queue,
  48852. + cond(data) || wq->abort,
  48853. + msecs_to_jiffies(msecs));
  48854. + if (result == -ERESTARTSYS) {
  48855. + wq->abort = 0;
  48856. + return -DWC_E_RESTART;
  48857. + }
  48858. +
  48859. + if (wq->abort == 1) {
  48860. + wq->abort = 0;
  48861. + return -DWC_E_ABORT;
  48862. + }
  48863. +
  48864. + wq->abort = 0;
  48865. +
  48866. + if (result > 0) {
  48867. + tmsecs = jiffies_to_msecs(result);
  48868. + if (!tmsecs) {
  48869. + return 1;
  48870. + }
  48871. +
  48872. + return tmsecs;
  48873. + }
  48874. +
  48875. + if (result == 0) {
  48876. + return -DWC_E_TIMEOUT;
  48877. + }
  48878. +
  48879. + return -DWC_E_UNKNOWN;
  48880. +}
  48881. +
  48882. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  48883. +{
  48884. + wq->abort = 0;
  48885. + wake_up_interruptible(&wq->queue);
  48886. +}
  48887. +
  48888. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  48889. +{
  48890. + wq->abort = 1;
  48891. + wake_up_interruptible(&wq->queue);
  48892. +}
  48893. +
  48894. +
  48895. +/* Threading */
  48896. +
  48897. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  48898. +{
  48899. + struct task_struct *thread = kthread_run(func, data, name);
  48900. +
  48901. + if (thread == ERR_PTR(-ENOMEM)) {
  48902. + return NULL;
  48903. + }
  48904. +
  48905. + return (dwc_thread_t *)thread;
  48906. +}
  48907. +
  48908. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  48909. +{
  48910. + return kthread_stop((struct task_struct *)thread);
  48911. +}
  48912. +
  48913. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  48914. +{
  48915. + return kthread_should_stop();
  48916. +}
  48917. +
  48918. +
  48919. +/* tasklets
  48920. + - run in interrupt context (cannot sleep)
  48921. + - each tasklet runs on a single CPU
  48922. + - different tasklets can be running simultaneously on different CPUs
  48923. + */
  48924. +struct dwc_tasklet {
  48925. + struct tasklet_struct t;
  48926. + dwc_tasklet_callback_t cb;
  48927. + void *data;
  48928. +};
  48929. +
  48930. +static void tasklet_callback(unsigned long data)
  48931. +{
  48932. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  48933. + t->cb(t->data);
  48934. +}
  48935. +
  48936. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  48937. +{
  48938. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  48939. +
  48940. + if (t) {
  48941. + t->cb = cb;
  48942. + t->data = data;
  48943. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  48944. + } else {
  48945. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  48946. + }
  48947. +
  48948. + return t;
  48949. +}
  48950. +
  48951. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  48952. +{
  48953. + DWC_FREE(task);
  48954. +}
  48955. +
  48956. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  48957. +{
  48958. + tasklet_schedule(&task->t);
  48959. +}
  48960. +
  48961. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  48962. +{
  48963. + tasklet_hi_schedule(&task->t);
  48964. +}
  48965. +
  48966. +
  48967. +/* workqueues
  48968. + - run in process context (can sleep)
  48969. + */
  48970. +typedef struct work_container {
  48971. + dwc_work_callback_t cb;
  48972. + void *data;
  48973. + dwc_workq_t *wq;
  48974. + char *name;
  48975. +
  48976. +#ifdef DEBUG
  48977. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  48978. +#endif
  48979. + struct delayed_work work;
  48980. +} work_container_t;
  48981. +
  48982. +#ifdef DEBUG
  48983. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  48984. +#endif
  48985. +
  48986. +struct dwc_workq {
  48987. + struct workqueue_struct *wq;
  48988. + dwc_spinlock_t *lock;
  48989. + dwc_waitq_t *waitq;
  48990. + int pending;
  48991. +
  48992. +#ifdef DEBUG
  48993. + struct work_container_queue entries;
  48994. +#endif
  48995. +};
  48996. +
  48997. +static void do_work(struct work_struct *work)
  48998. +{
  48999. + dwc_irqflags_t flags;
  49000. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  49001. + work_container_t *container = container_of(dw, struct work_container, work);
  49002. + dwc_workq_t *wq = container->wq;
  49003. +
  49004. + container->cb(container->data);
  49005. +
  49006. +#ifdef DEBUG
  49007. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  49008. +#endif
  49009. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  49010. + if (container->name) {
  49011. + DWC_FREE(container->name);
  49012. + }
  49013. + DWC_FREE(container);
  49014. +
  49015. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49016. + wq->pending--;
  49017. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49018. + DWC_WAITQ_TRIGGER(wq->waitq);
  49019. +}
  49020. +
  49021. +static int work_done(void *data)
  49022. +{
  49023. + dwc_workq_t *workq = (dwc_workq_t *)data;
  49024. + return workq->pending == 0;
  49025. +}
  49026. +
  49027. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  49028. +{
  49029. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  49030. +}
  49031. +
  49032. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  49033. +{
  49034. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  49035. +
  49036. + if (!wq) {
  49037. + return NULL;
  49038. + }
  49039. +
  49040. + wq->wq = create_singlethread_workqueue(name);
  49041. + if (!wq->wq) {
  49042. + goto no_wq;
  49043. + }
  49044. +
  49045. + wq->pending = 0;
  49046. +
  49047. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  49048. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
  49049. +#else
  49050. + wq->lock = DWC_SPINLOCK_ALLOC();
  49051. +#endif
  49052. + if (!wq->lock) {
  49053. + goto no_lock;
  49054. + }
  49055. +
  49056. + wq->waitq = DWC_WAITQ_ALLOC();
  49057. + if (!wq->waitq) {
  49058. + goto no_waitq;
  49059. + }
  49060. +
  49061. +#ifdef DEBUG
  49062. + DWC_CIRCLEQ_INIT(&wq->entries);
  49063. +#endif
  49064. + return wq;
  49065. +
  49066. + no_waitq:
  49067. + DWC_SPINLOCK_FREE(wq->lock);
  49068. + no_lock:
  49069. + destroy_workqueue(wq->wq);
  49070. + no_wq:
  49071. + DWC_FREE(wq);
  49072. +
  49073. + return NULL;
  49074. +}
  49075. +
  49076. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  49077. +{
  49078. +#ifdef DEBUG
  49079. + if (wq->pending != 0) {
  49080. + struct work_container *wc;
  49081. + DWC_ERROR("Destroying work queue with pending work");
  49082. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  49083. + DWC_ERROR("Work %s still pending", wc->name);
  49084. + }
  49085. + }
  49086. +#endif
  49087. + destroy_workqueue(wq->wq);
  49088. + DWC_SPINLOCK_FREE(wq->lock);
  49089. + DWC_WAITQ_FREE(wq->waitq);
  49090. + DWC_FREE(wq);
  49091. +}
  49092. +
  49093. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  49094. + char *format, ...)
  49095. +{
  49096. + dwc_irqflags_t flags;
  49097. + work_container_t *container;
  49098. + static char name[128];
  49099. + va_list args;
  49100. +
  49101. + va_start(args, format);
  49102. + DWC_VSNPRINTF(name, 128, format, args);
  49103. + va_end(args);
  49104. +
  49105. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49106. + wq->pending++;
  49107. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49108. + DWC_WAITQ_TRIGGER(wq->waitq);
  49109. +
  49110. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  49111. + if (!container) {
  49112. + DWC_ERROR("Cannot allocate memory for container\n");
  49113. + return;
  49114. + }
  49115. +
  49116. + container->name = DWC_STRDUP(name);
  49117. + if (!container->name) {
  49118. + DWC_ERROR("Cannot allocate memory for container->name\n");
  49119. + DWC_FREE(container);
  49120. + return;
  49121. + }
  49122. +
  49123. + container->cb = cb;
  49124. + container->data = data;
  49125. + container->wq = wq;
  49126. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  49127. + INIT_WORK(&container->work.work, do_work);
  49128. +
  49129. +#ifdef DEBUG
  49130. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  49131. +#endif
  49132. + queue_work(wq->wq, &container->work.work);
  49133. +}
  49134. +
  49135. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  49136. + void *data, uint32_t time, char *format, ...)
  49137. +{
  49138. + dwc_irqflags_t flags;
  49139. + work_container_t *container;
  49140. + static char name[128];
  49141. + va_list args;
  49142. +
  49143. + va_start(args, format);
  49144. + DWC_VSNPRINTF(name, 128, format, args);
  49145. + va_end(args);
  49146. +
  49147. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  49148. + wq->pending++;
  49149. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  49150. + DWC_WAITQ_TRIGGER(wq->waitq);
  49151. +
  49152. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  49153. + if (!container) {
  49154. + DWC_ERROR("Cannot allocate memory for container\n");
  49155. + return;
  49156. + }
  49157. +
  49158. + container->name = DWC_STRDUP(name);
  49159. + if (!container->name) {
  49160. + DWC_ERROR("Cannot allocate memory for container->name\n");
  49161. + DWC_FREE(container);
  49162. + return;
  49163. + }
  49164. +
  49165. + container->cb = cb;
  49166. + container->data = data;
  49167. + container->wq = wq;
  49168. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  49169. + INIT_DELAYED_WORK(&container->work, do_work);
  49170. +
  49171. +#ifdef DEBUG
  49172. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  49173. +#endif
  49174. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  49175. +}
  49176. +
  49177. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  49178. +{
  49179. + return wq->pending;
  49180. +}
  49181. +
  49182. +
  49183. +#ifdef DWC_LIBMODULE
  49184. +
  49185. +#ifdef DWC_CCLIB
  49186. +/* CC */
  49187. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  49188. +EXPORT_SYMBOL(dwc_cc_if_free);
  49189. +EXPORT_SYMBOL(dwc_cc_clear);
  49190. +EXPORT_SYMBOL(dwc_cc_add);
  49191. +EXPORT_SYMBOL(dwc_cc_remove);
  49192. +EXPORT_SYMBOL(dwc_cc_change);
  49193. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  49194. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  49195. +EXPORT_SYMBOL(dwc_cc_match_chid);
  49196. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  49197. +EXPORT_SYMBOL(dwc_cc_ck);
  49198. +EXPORT_SYMBOL(dwc_cc_chid);
  49199. +EXPORT_SYMBOL(dwc_cc_cdid);
  49200. +EXPORT_SYMBOL(dwc_cc_name);
  49201. +#endif /* DWC_CCLIB */
  49202. +
  49203. +#ifdef DWC_CRYPTOLIB
  49204. +# ifndef CONFIG_MACH_IPMATE
  49205. +/* Modpow */
  49206. +EXPORT_SYMBOL(dwc_modpow);
  49207. +
  49208. +/* DH */
  49209. +EXPORT_SYMBOL(dwc_dh_modpow);
  49210. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  49211. +EXPORT_SYMBOL(dwc_dh_pk);
  49212. +# endif /* CONFIG_MACH_IPMATE */
  49213. +
  49214. +/* Crypto */
  49215. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  49216. +EXPORT_SYMBOL(dwc_wusb_cmf);
  49217. +EXPORT_SYMBOL(dwc_wusb_prf);
  49218. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  49219. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  49220. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  49221. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  49222. +#endif /* DWC_CRYPTOLIB */
  49223. +
  49224. +/* Notification */
  49225. +#ifdef DWC_NOTIFYLIB
  49226. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  49227. +EXPORT_SYMBOL(dwc_free_notification_manager);
  49228. +EXPORT_SYMBOL(dwc_register_notifier);
  49229. +EXPORT_SYMBOL(dwc_unregister_notifier);
  49230. +EXPORT_SYMBOL(dwc_add_observer);
  49231. +EXPORT_SYMBOL(dwc_remove_observer);
  49232. +EXPORT_SYMBOL(dwc_notify);
  49233. +#endif
  49234. +
  49235. +/* Memory Debugging Routines */
  49236. +#ifdef DWC_DEBUG_MEMORY
  49237. +EXPORT_SYMBOL(dwc_alloc_debug);
  49238. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  49239. +EXPORT_SYMBOL(dwc_free_debug);
  49240. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  49241. +EXPORT_SYMBOL(dwc_dma_free_debug);
  49242. +#endif
  49243. +
  49244. +EXPORT_SYMBOL(DWC_MEMSET);
  49245. +EXPORT_SYMBOL(DWC_MEMCPY);
  49246. +EXPORT_SYMBOL(DWC_MEMMOVE);
  49247. +EXPORT_SYMBOL(DWC_MEMCMP);
  49248. +EXPORT_SYMBOL(DWC_STRNCMP);
  49249. +EXPORT_SYMBOL(DWC_STRCMP);
  49250. +EXPORT_SYMBOL(DWC_STRLEN);
  49251. +EXPORT_SYMBOL(DWC_STRCPY);
  49252. +EXPORT_SYMBOL(DWC_STRDUP);
  49253. +EXPORT_SYMBOL(DWC_ATOI);
  49254. +EXPORT_SYMBOL(DWC_ATOUI);
  49255. +
  49256. +#ifdef DWC_UTFLIB
  49257. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  49258. +#endif /* DWC_UTFLIB */
  49259. +
  49260. +EXPORT_SYMBOL(DWC_IN_IRQ);
  49261. +EXPORT_SYMBOL(DWC_IN_BH);
  49262. +EXPORT_SYMBOL(DWC_VPRINTF);
  49263. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  49264. +EXPORT_SYMBOL(DWC_PRINTF);
  49265. +EXPORT_SYMBOL(DWC_SPRINTF);
  49266. +EXPORT_SYMBOL(DWC_SNPRINTF);
  49267. +EXPORT_SYMBOL(__DWC_WARN);
  49268. +EXPORT_SYMBOL(__DWC_ERROR);
  49269. +EXPORT_SYMBOL(DWC_EXCEPTION);
  49270. +
  49271. +#ifdef DEBUG
  49272. +EXPORT_SYMBOL(__DWC_DEBUG);
  49273. +#endif
  49274. +
  49275. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  49276. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  49277. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  49278. +EXPORT_SYMBOL(__DWC_ALLOC);
  49279. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  49280. +EXPORT_SYMBOL(__DWC_FREE);
  49281. +
  49282. +#ifdef DWC_CRYPTOLIB
  49283. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  49284. +EXPORT_SYMBOL(DWC_AES_CBC);
  49285. +EXPORT_SYMBOL(DWC_SHA256);
  49286. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  49287. +#endif
  49288. +
  49289. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  49290. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  49291. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  49292. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  49293. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  49294. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  49295. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  49296. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  49297. +EXPORT_SYMBOL(DWC_READ_REG32);
  49298. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  49299. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  49300. +
  49301. +#if 0
  49302. +EXPORT_SYMBOL(DWC_READ_REG64);
  49303. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  49304. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  49305. +#endif
  49306. +
  49307. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  49308. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  49309. +EXPORT_SYMBOL(DWC_SPINLOCK);
  49310. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  49311. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  49312. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  49313. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  49314. +
  49315. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  49316. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  49317. +#endif
  49318. +
  49319. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  49320. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  49321. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  49322. +EXPORT_SYMBOL(DWC_UDELAY);
  49323. +EXPORT_SYMBOL(DWC_MDELAY);
  49324. +EXPORT_SYMBOL(DWC_MSLEEP);
  49325. +EXPORT_SYMBOL(DWC_TIME);
  49326. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  49327. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  49328. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  49329. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  49330. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  49331. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  49332. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  49333. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  49334. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  49335. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  49336. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  49337. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  49338. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  49339. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  49340. +EXPORT_SYMBOL(DWC_TASK_FREE);
  49341. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  49342. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  49343. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  49344. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  49345. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  49346. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  49347. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  49348. +
  49349. +static int dwc_common_port_init_module(void)
  49350. +{
  49351. + int result = 0;
  49352. +
  49353. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  49354. +
  49355. +#ifdef DWC_DEBUG_MEMORY
  49356. + result = dwc_memory_debug_start(NULL);
  49357. + if (result) {
  49358. + printk(KERN_ERR
  49359. + "dwc_memory_debug_start() failed with error %d\n",
  49360. + result);
  49361. + return result;
  49362. + }
  49363. +#endif
  49364. +
  49365. +#ifdef DWC_NOTIFYLIB
  49366. + result = dwc_alloc_notification_manager(NULL, NULL);
  49367. + if (result) {
  49368. + printk(KERN_ERR
  49369. + "dwc_alloc_notification_manager() failed with error %d\n",
  49370. + result);
  49371. + return result;
  49372. + }
  49373. +#endif
  49374. + return result;
  49375. +}
  49376. +
  49377. +static void dwc_common_port_exit_module(void)
  49378. +{
  49379. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  49380. +
  49381. +#ifdef DWC_NOTIFYLIB
  49382. + dwc_free_notification_manager();
  49383. +#endif
  49384. +
  49385. +#ifdef DWC_DEBUG_MEMORY
  49386. + dwc_memory_debug_stop();
  49387. +#endif
  49388. +}
  49389. +
  49390. +module_init(dwc_common_port_init_module);
  49391. +module_exit(dwc_common_port_exit_module);
  49392. +
  49393. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  49394. +MODULE_AUTHOR("Synopsys Inc.");
  49395. +MODULE_LICENSE ("GPL");
  49396. +
  49397. +#endif /* DWC_LIBMODULE */
  49398. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  49399. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 1970-01-01 01:00:00.000000000 +0100
  49400. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c 2014-04-13 17:33:11.000000000 +0200
  49401. @@ -0,0 +1,1275 @@
  49402. +#include "dwc_os.h"
  49403. +#include "dwc_list.h"
  49404. +
  49405. +#ifdef DWC_CCLIB
  49406. +# include "dwc_cc.h"
  49407. +#endif
  49408. +
  49409. +#ifdef DWC_CRYPTOLIB
  49410. +# include "dwc_modpow.h"
  49411. +# include "dwc_dh.h"
  49412. +# include "dwc_crypto.h"
  49413. +#endif
  49414. +
  49415. +#ifdef DWC_NOTIFYLIB
  49416. +# include "dwc_notifier.h"
  49417. +#endif
  49418. +
  49419. +/* OS-Level Implementations */
  49420. +
  49421. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  49422. +
  49423. +
  49424. +/* MISC */
  49425. +
  49426. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  49427. +{
  49428. + return memset(dest, byte, size);
  49429. +}
  49430. +
  49431. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  49432. +{
  49433. + return memcpy(dest, src, size);
  49434. +}
  49435. +
  49436. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  49437. +{
  49438. + bcopy(src, dest, size);
  49439. + return dest;
  49440. +}
  49441. +
  49442. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  49443. +{
  49444. + return memcmp(m1, m2, size);
  49445. +}
  49446. +
  49447. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  49448. +{
  49449. + return strncmp(s1, s2, size);
  49450. +}
  49451. +
  49452. +int DWC_STRCMP(void *s1, void *s2)
  49453. +{
  49454. + return strcmp(s1, s2);
  49455. +}
  49456. +
  49457. +int DWC_STRLEN(char const *str)
  49458. +{
  49459. + return strlen(str);
  49460. +}
  49461. +
  49462. +char *DWC_STRCPY(char *to, char const *from)
  49463. +{
  49464. + return strcpy(to, from);
  49465. +}
  49466. +
  49467. +char *DWC_STRDUP(char const *str)
  49468. +{
  49469. + int len = DWC_STRLEN(str) + 1;
  49470. + char *new = DWC_ALLOC_ATOMIC(len);
  49471. +
  49472. + if (!new) {
  49473. + return NULL;
  49474. + }
  49475. +
  49476. + DWC_MEMCPY(new, str, len);
  49477. + return new;
  49478. +}
  49479. +
  49480. +int DWC_ATOI(char *str, int32_t *value)
  49481. +{
  49482. + char *end = NULL;
  49483. +
  49484. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  49485. + * should be equivalent on 2's complement machines
  49486. + */
  49487. + *value = strtoul(str, &end, 0);
  49488. + if (*end == '\0') {
  49489. + return 0;
  49490. + }
  49491. +
  49492. + return -1;
  49493. +}
  49494. +
  49495. +int DWC_ATOUI(char *str, uint32_t *value)
  49496. +{
  49497. + char *end = NULL;
  49498. +
  49499. + *value = strtoul(str, &end, 0);
  49500. + if (*end == '\0') {
  49501. + return 0;
  49502. + }
  49503. +
  49504. + return -1;
  49505. +}
  49506. +
  49507. +
  49508. +#ifdef DWC_UTFLIB
  49509. +/* From usbstring.c */
  49510. +
  49511. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  49512. +{
  49513. + int count = 0;
  49514. + u8 c;
  49515. + u16 uchar;
  49516. +
  49517. + /* this insists on correct encodings, though not minimal ones.
  49518. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  49519. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  49520. + */
  49521. + while (len != 0 && (c = (u8) *s++) != 0) {
  49522. + if (unlikely(c & 0x80)) {
  49523. + // 2-byte sequence:
  49524. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  49525. + if ((c & 0xe0) == 0xc0) {
  49526. + uchar = (c & 0x1f) << 6;
  49527. +
  49528. + c = (u8) *s++;
  49529. + if ((c & 0xc0) != 0xc0)
  49530. + goto fail;
  49531. + c &= 0x3f;
  49532. + uchar |= c;
  49533. +
  49534. + // 3-byte sequence (most CJKV characters):
  49535. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  49536. + } else if ((c & 0xf0) == 0xe0) {
  49537. + uchar = (c & 0x0f) << 12;
  49538. +
  49539. + c = (u8) *s++;
  49540. + if ((c & 0xc0) != 0xc0)
  49541. + goto fail;
  49542. + c &= 0x3f;
  49543. + uchar |= c << 6;
  49544. +
  49545. + c = (u8) *s++;
  49546. + if ((c & 0xc0) != 0xc0)
  49547. + goto fail;
  49548. + c &= 0x3f;
  49549. + uchar |= c;
  49550. +
  49551. + /* no bogus surrogates */
  49552. + if (0xd800 <= uchar && uchar <= 0xdfff)
  49553. + goto fail;
  49554. +
  49555. + // 4-byte sequence (surrogate pairs, currently rare):
  49556. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  49557. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  49558. + // (uuuuu = wwww + 1)
  49559. + // FIXME accept the surrogate code points (only)
  49560. + } else
  49561. + goto fail;
  49562. + } else
  49563. + uchar = c;
  49564. + put_unaligned (cpu_to_le16 (uchar), cp++);
  49565. + count++;
  49566. + len--;
  49567. + }
  49568. + return count;
  49569. +fail:
  49570. + return -1;
  49571. +}
  49572. +
  49573. +#endif /* DWC_UTFLIB */
  49574. +
  49575. +
  49576. +/* dwc_debug.h */
  49577. +
  49578. +dwc_bool_t DWC_IN_IRQ(void)
  49579. +{
  49580. +// return in_irq();
  49581. + return 0;
  49582. +}
  49583. +
  49584. +dwc_bool_t DWC_IN_BH(void)
  49585. +{
  49586. +// return in_softirq();
  49587. + return 0;
  49588. +}
  49589. +
  49590. +void DWC_VPRINTF(char *format, va_list args)
  49591. +{
  49592. + vprintf(format, args);
  49593. +}
  49594. +
  49595. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  49596. +{
  49597. + return vsnprintf(str, size, format, args);
  49598. +}
  49599. +
  49600. +void DWC_PRINTF(char *format, ...)
  49601. +{
  49602. + va_list args;
  49603. +
  49604. + va_start(args, format);
  49605. + DWC_VPRINTF(format, args);
  49606. + va_end(args);
  49607. +}
  49608. +
  49609. +int DWC_SPRINTF(char *buffer, char *format, ...)
  49610. +{
  49611. + int retval;
  49612. + va_list args;
  49613. +
  49614. + va_start(args, format);
  49615. + retval = vsprintf(buffer, format, args);
  49616. + va_end(args);
  49617. + return retval;
  49618. +}
  49619. +
  49620. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  49621. +{
  49622. + int retval;
  49623. + va_list args;
  49624. +
  49625. + va_start(args, format);
  49626. + retval = vsnprintf(buffer, size, format, args);
  49627. + va_end(args);
  49628. + return retval;
  49629. +}
  49630. +
  49631. +void __DWC_WARN(char *format, ...)
  49632. +{
  49633. + va_list args;
  49634. +
  49635. + va_start(args, format);
  49636. + DWC_VPRINTF(format, args);
  49637. + va_end(args);
  49638. +}
  49639. +
  49640. +void __DWC_ERROR(char *format, ...)
  49641. +{
  49642. + va_list args;
  49643. +
  49644. + va_start(args, format);
  49645. + DWC_VPRINTF(format, args);
  49646. + va_end(args);
  49647. +}
  49648. +
  49649. +void DWC_EXCEPTION(char *format, ...)
  49650. +{
  49651. + va_list args;
  49652. +
  49653. + va_start(args, format);
  49654. + DWC_VPRINTF(format, args);
  49655. + va_end(args);
  49656. +// BUG_ON(1); ???
  49657. +}
  49658. +
  49659. +#ifdef DEBUG
  49660. +void __DWC_DEBUG(char *format, ...)
  49661. +{
  49662. + va_list args;
  49663. +
  49664. + va_start(args, format);
  49665. + DWC_VPRINTF(format, args);
  49666. + va_end(args);
  49667. +}
  49668. +#endif
  49669. +
  49670. +
  49671. +/* dwc_mem.h */
  49672. +
  49673. +#if 0
  49674. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  49675. + uint32_t align,
  49676. + uint32_t alloc)
  49677. +{
  49678. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  49679. + size, align, alloc);
  49680. + return (dwc_pool_t *)pool;
  49681. +}
  49682. +
  49683. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  49684. +{
  49685. + dma_pool_destroy((struct dma_pool *)pool);
  49686. +}
  49687. +
  49688. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  49689. +{
  49690. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  49691. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  49692. +}
  49693. +
  49694. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  49695. +{
  49696. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  49697. + memset(..);
  49698. +}
  49699. +
  49700. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  49701. +{
  49702. + dma_pool_free(pool, vaddr, daddr);
  49703. +}
  49704. +#endif
  49705. +
  49706. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  49707. +{
  49708. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  49709. + int error;
  49710. +
  49711. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  49712. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  49713. + &dma->nsegs, BUS_DMA_NOWAIT);
  49714. + if (error) {
  49715. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  49716. + (uintmax_t)size, error);
  49717. + goto fail_0;
  49718. + }
  49719. +
  49720. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  49721. + (caddr_t *)&dma->dma_vaddr,
  49722. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  49723. + if (error) {
  49724. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  49725. + goto fail_1;
  49726. + }
  49727. +
  49728. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  49729. + BUS_DMA_NOWAIT, &dma->dma_map);
  49730. + if (error) {
  49731. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  49732. + goto fail_2;
  49733. + }
  49734. +
  49735. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  49736. + size, NULL, BUS_DMA_NOWAIT);
  49737. + if (error) {
  49738. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  49739. + goto fail_3;
  49740. + }
  49741. +
  49742. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  49743. + *dma_addr = dma->dma_paddr;
  49744. + return dma->dma_vaddr;
  49745. +
  49746. +fail_3:
  49747. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  49748. +fail_2:
  49749. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  49750. +fail_1:
  49751. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  49752. +fail_0:
  49753. + dma->dma_map = NULL;
  49754. + dma->dma_vaddr = NULL;
  49755. + dma->nsegs = 0;
  49756. +
  49757. + return NULL;
  49758. +}
  49759. +
  49760. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  49761. +{
  49762. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  49763. +
  49764. + if (dma->dma_map != NULL) {
  49765. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  49766. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  49767. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  49768. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  49769. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  49770. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  49771. + dma->dma_paddr = 0;
  49772. + dma->dma_map = NULL;
  49773. + dma->dma_vaddr = NULL;
  49774. + dma->nsegs = 0;
  49775. + }
  49776. +}
  49777. +
  49778. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  49779. +{
  49780. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  49781. +}
  49782. +
  49783. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  49784. +{
  49785. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  49786. +}
  49787. +
  49788. +void __DWC_FREE(void *mem_ctx, void *addr)
  49789. +{
  49790. + free(addr, M_DEVBUF);
  49791. +}
  49792. +
  49793. +
  49794. +#ifdef DWC_CRYPTOLIB
  49795. +/* dwc_crypto.h */
  49796. +
  49797. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  49798. +{
  49799. + get_random_bytes(buffer, length);
  49800. +}
  49801. +
  49802. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  49803. +{
  49804. + struct crypto_blkcipher *tfm;
  49805. + struct blkcipher_desc desc;
  49806. + struct scatterlist sgd;
  49807. + struct scatterlist sgs;
  49808. +
  49809. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  49810. + if (tfm == NULL) {
  49811. + printk("failed to load transform for aes CBC\n");
  49812. + return -1;
  49813. + }
  49814. +
  49815. + crypto_blkcipher_setkey(tfm, key, keylen);
  49816. + crypto_blkcipher_set_iv(tfm, iv, 16);
  49817. +
  49818. + sg_init_one(&sgd, out, messagelen);
  49819. + sg_init_one(&sgs, message, messagelen);
  49820. +
  49821. + desc.tfm = tfm;
  49822. + desc.flags = 0;
  49823. +
  49824. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  49825. + crypto_free_blkcipher(tfm);
  49826. + DWC_ERROR("AES CBC encryption failed");
  49827. + return -1;
  49828. + }
  49829. +
  49830. + crypto_free_blkcipher(tfm);
  49831. + return 0;
  49832. +}
  49833. +
  49834. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  49835. +{
  49836. + struct crypto_hash *tfm;
  49837. + struct hash_desc desc;
  49838. + struct scatterlist sg;
  49839. +
  49840. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  49841. + if (IS_ERR(tfm)) {
  49842. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  49843. + return 0;
  49844. + }
  49845. + desc.tfm = tfm;
  49846. + desc.flags = 0;
  49847. +
  49848. + sg_init_one(&sg, message, len);
  49849. + crypto_hash_digest(&desc, &sg, len, out);
  49850. + crypto_free_hash(tfm);
  49851. +
  49852. + return 1;
  49853. +}
  49854. +
  49855. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  49856. + uint8_t *key, uint32_t keylen, uint8_t *out)
  49857. +{
  49858. + struct crypto_hash *tfm;
  49859. + struct hash_desc desc;
  49860. + struct scatterlist sg;
  49861. +
  49862. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  49863. + if (IS_ERR(tfm)) {
  49864. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  49865. + return 0;
  49866. + }
  49867. + desc.tfm = tfm;
  49868. + desc.flags = 0;
  49869. +
  49870. + sg_init_one(&sg, message, messagelen);
  49871. + crypto_hash_setkey(tfm, key, keylen);
  49872. + crypto_hash_digest(&desc, &sg, messagelen, out);
  49873. + crypto_free_hash(tfm);
  49874. +
  49875. + return 1;
  49876. +}
  49877. +
  49878. +#endif /* DWC_CRYPTOLIB */
  49879. +
  49880. +
  49881. +/* Byte Ordering Conversions */
  49882. +
  49883. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  49884. +{
  49885. +#ifdef __LITTLE_ENDIAN
  49886. + return *p;
  49887. +#else
  49888. + uint8_t *u_p = (uint8_t *)p;
  49889. +
  49890. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49891. +#endif
  49892. +}
  49893. +
  49894. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  49895. +{
  49896. +#ifdef __BIG_ENDIAN
  49897. + return *p;
  49898. +#else
  49899. + uint8_t *u_p = (uint8_t *)p;
  49900. +
  49901. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49902. +#endif
  49903. +}
  49904. +
  49905. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  49906. +{
  49907. +#ifdef __LITTLE_ENDIAN
  49908. + return *p;
  49909. +#else
  49910. + uint8_t *u_p = (uint8_t *)p;
  49911. +
  49912. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49913. +#endif
  49914. +}
  49915. +
  49916. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  49917. +{
  49918. +#ifdef __BIG_ENDIAN
  49919. + return *p;
  49920. +#else
  49921. + uint8_t *u_p = (uint8_t *)p;
  49922. +
  49923. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  49924. +#endif
  49925. +}
  49926. +
  49927. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  49928. +{
  49929. +#ifdef __LITTLE_ENDIAN
  49930. + return *p;
  49931. +#else
  49932. + uint8_t *u_p = (uint8_t *)p;
  49933. + return (u_p[1] | (u_p[0] << 8));
  49934. +#endif
  49935. +}
  49936. +
  49937. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  49938. +{
  49939. +#ifdef __BIG_ENDIAN
  49940. + return *p;
  49941. +#else
  49942. + uint8_t *u_p = (uint8_t *)p;
  49943. + return (u_p[1] | (u_p[0] << 8));
  49944. +#endif
  49945. +}
  49946. +
  49947. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  49948. +{
  49949. +#ifdef __LITTLE_ENDIAN
  49950. + return *p;
  49951. +#else
  49952. + uint8_t *u_p = (uint8_t *)p;
  49953. + return (u_p[1] | (u_p[0] << 8));
  49954. +#endif
  49955. +}
  49956. +
  49957. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  49958. +{
  49959. +#ifdef __BIG_ENDIAN
  49960. + return *p;
  49961. +#else
  49962. + uint8_t *u_p = (uint8_t *)p;
  49963. + return (u_p[1] | (u_p[0] << 8));
  49964. +#endif
  49965. +}
  49966. +
  49967. +
  49968. +/* Registers */
  49969. +
  49970. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  49971. +{
  49972. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49973. + bus_size_t ior = (bus_size_t)reg;
  49974. +
  49975. + return bus_space_read_4(io->iot, io->ioh, ior);
  49976. +}
  49977. +
  49978. +#if 0
  49979. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  49980. +{
  49981. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49982. + bus_size_t ior = (bus_size_t)reg;
  49983. +
  49984. + return bus_space_read_8(io->iot, io->ioh, ior);
  49985. +}
  49986. +#endif
  49987. +
  49988. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  49989. +{
  49990. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  49991. + bus_size_t ior = (bus_size_t)reg;
  49992. +
  49993. + bus_space_write_4(io->iot, io->ioh, ior, value);
  49994. +}
  49995. +
  49996. +#if 0
  49997. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  49998. +{
  49999. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  50000. + bus_size_t ior = (bus_size_t)reg;
  50001. +
  50002. + bus_space_write_8(io->iot, io->ioh, ior, value);
  50003. +}
  50004. +#endif
  50005. +
  50006. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  50007. + uint32_t set_mask)
  50008. +{
  50009. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  50010. + bus_size_t ior = (bus_size_t)reg;
  50011. +
  50012. + bus_space_write_4(io->iot, io->ioh, ior,
  50013. + (bus_space_read_4(io->iot, io->ioh, ior) &
  50014. + ~clear_mask) | set_mask);
  50015. +}
  50016. +
  50017. +#if 0
  50018. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  50019. + uint64_t set_mask)
  50020. +{
  50021. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  50022. + bus_size_t ior = (bus_size_t)reg;
  50023. +
  50024. + bus_space_write_8(io->iot, io->ioh, ior,
  50025. + (bus_space_read_8(io->iot, io->ioh, ior) &
  50026. + ~clear_mask) | set_mask);
  50027. +}
  50028. +#endif
  50029. +
  50030. +
  50031. +/* Locking */
  50032. +
  50033. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  50034. +{
  50035. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  50036. +
  50037. + if (!sl) {
  50038. + DWC_ERROR("Cannot allocate memory for spinlock");
  50039. + return NULL;
  50040. + }
  50041. +
  50042. + simple_lock_init(sl);
  50043. + return (dwc_spinlock_t *)sl;
  50044. +}
  50045. +
  50046. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  50047. +{
  50048. + struct simplelock *sl = (struct simplelock *)lock;
  50049. +
  50050. + DWC_FREE(sl);
  50051. +}
  50052. +
  50053. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  50054. +{
  50055. + simple_lock((struct simplelock *)lock);
  50056. +}
  50057. +
  50058. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  50059. +{
  50060. + simple_unlock((struct simplelock *)lock);
  50061. +}
  50062. +
  50063. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  50064. +{
  50065. + simple_lock((struct simplelock *)lock);
  50066. + *flags = splbio();
  50067. +}
  50068. +
  50069. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  50070. +{
  50071. + splx(flags);
  50072. + simple_unlock((struct simplelock *)lock);
  50073. +}
  50074. +
  50075. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  50076. +{
  50077. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  50078. +
  50079. + if (!mutex) {
  50080. + DWC_ERROR("Cannot allocate memory for mutex");
  50081. + return NULL;
  50082. + }
  50083. +
  50084. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  50085. + return mutex;
  50086. +}
  50087. +
  50088. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  50089. +#else
  50090. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  50091. +{
  50092. + DWC_FREE(mutex);
  50093. +}
  50094. +#endif
  50095. +
  50096. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  50097. +{
  50098. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  50099. +}
  50100. +
  50101. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  50102. +{
  50103. + int status;
  50104. +
  50105. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  50106. + return status == 0;
  50107. +}
  50108. +
  50109. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  50110. +{
  50111. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  50112. +}
  50113. +
  50114. +
  50115. +/* Timing */
  50116. +
  50117. +void DWC_UDELAY(uint32_t usecs)
  50118. +{
  50119. + DELAY(usecs);
  50120. +}
  50121. +
  50122. +void DWC_MDELAY(uint32_t msecs)
  50123. +{
  50124. + do {
  50125. + DELAY(1000);
  50126. + } while (--msecs);
  50127. +}
  50128. +
  50129. +void DWC_MSLEEP(uint32_t msecs)
  50130. +{
  50131. + struct timeval tv;
  50132. +
  50133. + tv.tv_sec = msecs / 1000;
  50134. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  50135. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  50136. +}
  50137. +
  50138. +uint32_t DWC_TIME(void)
  50139. +{
  50140. + struct timeval tv;
  50141. +
  50142. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  50143. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  50144. +}
  50145. +
  50146. +
  50147. +/* Timers */
  50148. +
  50149. +struct dwc_timer {
  50150. + struct callout t;
  50151. + char *name;
  50152. + dwc_spinlock_t *lock;
  50153. + dwc_timer_callback_t cb;
  50154. + void *data;
  50155. +};
  50156. +
  50157. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  50158. +{
  50159. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  50160. +
  50161. + if (!t) {
  50162. + DWC_ERROR("Cannot allocate memory for timer");
  50163. + return NULL;
  50164. + }
  50165. +
  50166. + callout_init(&t->t);
  50167. +
  50168. + t->name = DWC_STRDUP(name);
  50169. + if (!t->name) {
  50170. + DWC_ERROR("Cannot allocate memory for timer->name");
  50171. + goto no_name;
  50172. + }
  50173. +
  50174. + t->lock = DWC_SPINLOCK_ALLOC();
  50175. + if (!t->lock) {
  50176. + DWC_ERROR("Cannot allocate memory for timer->lock");
  50177. + goto no_lock;
  50178. + }
  50179. +
  50180. + t->cb = cb;
  50181. + t->data = data;
  50182. +
  50183. + return t;
  50184. +
  50185. + no_lock:
  50186. + DWC_FREE(t->name);
  50187. + no_name:
  50188. + DWC_FREE(t);
  50189. +
  50190. + return NULL;
  50191. +}
  50192. +
  50193. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  50194. +{
  50195. + callout_stop(&timer->t);
  50196. + DWC_SPINLOCK_FREE(timer->lock);
  50197. + DWC_FREE(timer->name);
  50198. + DWC_FREE(timer);
  50199. +}
  50200. +
  50201. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  50202. +{
  50203. + struct timeval tv;
  50204. +
  50205. + tv.tv_sec = time / 1000;
  50206. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  50207. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  50208. +}
  50209. +
  50210. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  50211. +{
  50212. + callout_stop(&timer->t);
  50213. +}
  50214. +
  50215. +
  50216. +/* Wait Queues */
  50217. +
  50218. +struct dwc_waitq {
  50219. + struct simplelock lock;
  50220. + int abort;
  50221. +};
  50222. +
  50223. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  50224. +{
  50225. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  50226. +
  50227. + if (!wq) {
  50228. + DWC_ERROR("Cannot allocate memory for waitqueue");
  50229. + return NULL;
  50230. + }
  50231. +
  50232. + simple_lock_init(&wq->lock);
  50233. + wq->abort = 0;
  50234. +
  50235. + return wq;
  50236. +}
  50237. +
  50238. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  50239. +{
  50240. + DWC_FREE(wq);
  50241. +}
  50242. +
  50243. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  50244. +{
  50245. + int ipl;
  50246. + int result = 0;
  50247. +
  50248. + simple_lock(&wq->lock);
  50249. + ipl = splbio();
  50250. +
  50251. + /* Skip the sleep if already aborted or triggered */
  50252. + if (!wq->abort && !cond(data)) {
  50253. + splx(ipl);
  50254. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  50255. + ipl = splbio();
  50256. + }
  50257. +
  50258. + if (result == 0) { // awoken
  50259. + if (wq->abort) {
  50260. + wq->abort = 0;
  50261. + result = -DWC_E_ABORT;
  50262. + } else {
  50263. + result = 0;
  50264. + }
  50265. +
  50266. + splx(ipl);
  50267. + simple_unlock(&wq->lock);
  50268. + } else {
  50269. + wq->abort = 0;
  50270. + splx(ipl);
  50271. + simple_unlock(&wq->lock);
  50272. +
  50273. + if (result == ERESTART) { // signaled - restart
  50274. + result = -DWC_E_RESTART;
  50275. + } else { // signaled - must be EINTR
  50276. + result = -DWC_E_ABORT;
  50277. + }
  50278. + }
  50279. +
  50280. + return result;
  50281. +}
  50282. +
  50283. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  50284. + void *data, int32_t msecs)
  50285. +{
  50286. + struct timeval tv, tv1, tv2;
  50287. + int ipl;
  50288. + int result = 0;
  50289. +
  50290. + tv.tv_sec = msecs / 1000;
  50291. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  50292. +
  50293. + simple_lock(&wq->lock);
  50294. + ipl = splbio();
  50295. +
  50296. + /* Skip the sleep if already aborted or triggered */
  50297. + if (!wq->abort && !cond(data)) {
  50298. + splx(ipl);
  50299. + getmicrouptime(&tv1);
  50300. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  50301. + getmicrouptime(&tv2);
  50302. + ipl = splbio();
  50303. + }
  50304. +
  50305. + if (result == 0) { // awoken
  50306. + if (wq->abort) {
  50307. + wq->abort = 0;
  50308. + splx(ipl);
  50309. + simple_unlock(&wq->lock);
  50310. + result = -DWC_E_ABORT;
  50311. + } else {
  50312. + splx(ipl);
  50313. + simple_unlock(&wq->lock);
  50314. +
  50315. + tv2.tv_usec -= tv1.tv_usec;
  50316. + if (tv2.tv_usec < 0) {
  50317. + tv2.tv_usec += 1000000;
  50318. + tv2.tv_sec--;
  50319. + }
  50320. +
  50321. + tv2.tv_sec -= tv1.tv_sec;
  50322. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  50323. + result = msecs - result;
  50324. + if (result <= 0)
  50325. + result = 1;
  50326. + }
  50327. + } else {
  50328. + wq->abort = 0;
  50329. + splx(ipl);
  50330. + simple_unlock(&wq->lock);
  50331. +
  50332. + if (result == ERESTART) { // signaled - restart
  50333. + result = -DWC_E_RESTART;
  50334. +
  50335. + } else if (result == EINTR) { // signaled - interrupt
  50336. + result = -DWC_E_ABORT;
  50337. +
  50338. + } else { // timed out
  50339. + result = -DWC_E_TIMEOUT;
  50340. + }
  50341. + }
  50342. +
  50343. + return result;
  50344. +}
  50345. +
  50346. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  50347. +{
  50348. + wakeup(wq);
  50349. +}
  50350. +
  50351. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  50352. +{
  50353. + int ipl;
  50354. +
  50355. + simple_lock(&wq->lock);
  50356. + ipl = splbio();
  50357. + wq->abort = 1;
  50358. + wakeup(wq);
  50359. + splx(ipl);
  50360. + simple_unlock(&wq->lock);
  50361. +}
  50362. +
  50363. +
  50364. +/* Threading */
  50365. +
  50366. +struct dwc_thread {
  50367. + struct proc *proc;
  50368. + int abort;
  50369. +};
  50370. +
  50371. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  50372. +{
  50373. + int retval;
  50374. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  50375. +
  50376. + if (!thread) {
  50377. + return NULL;
  50378. + }
  50379. +
  50380. + thread->abort = 0;
  50381. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  50382. + "%s", name);
  50383. + if (retval) {
  50384. + DWC_FREE(thread);
  50385. + return NULL;
  50386. + }
  50387. +
  50388. + return thread;
  50389. +}
  50390. +
  50391. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  50392. +{
  50393. + int retval;
  50394. +
  50395. + thread->abort = 1;
  50396. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  50397. +
  50398. + if (retval == 0) {
  50399. + /* DWC_THREAD_EXIT() will free the thread struct */
  50400. + return 0;
  50401. + }
  50402. +
  50403. + /* NOTE: We leak the thread struct if thread doesn't die */
  50404. +
  50405. + if (retval == EWOULDBLOCK) {
  50406. + return -DWC_E_TIMEOUT;
  50407. + }
  50408. +
  50409. + return -DWC_E_UNKNOWN;
  50410. +}
  50411. +
  50412. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  50413. +{
  50414. + return thread->abort;
  50415. +}
  50416. +
  50417. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  50418. +{
  50419. + wakeup(&thread->abort);
  50420. + DWC_FREE(thread);
  50421. + kthread_exit(0);
  50422. +}
  50423. +
  50424. +/* tasklets
  50425. + - Runs in interrupt context (cannot sleep)
  50426. + - Each tasklet runs on a single CPU
  50427. + - Different tasklets can be running simultaneously on different CPUs
  50428. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  50429. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  50430. + */
  50431. +struct dwc_tasklet {
  50432. + dwc_tasklet_callback_t cb;
  50433. + void *data;
  50434. +};
  50435. +
  50436. +static void tasklet_callback(void *data)
  50437. +{
  50438. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  50439. +
  50440. + task->cb(task->data);
  50441. +}
  50442. +
  50443. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  50444. +{
  50445. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  50446. +
  50447. + if (task) {
  50448. + task->cb = cb;
  50449. + task->data = data;
  50450. + } else {
  50451. + DWC_ERROR("Cannot allocate memory for tasklet");
  50452. + }
  50453. +
  50454. + return task;
  50455. +}
  50456. +
  50457. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  50458. +{
  50459. + DWC_FREE(task);
  50460. +}
  50461. +
  50462. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  50463. +{
  50464. + tasklet_callback(task);
  50465. +}
  50466. +
  50467. +
  50468. +/* workqueues
  50469. + - Runs in process context (can sleep)
  50470. + */
  50471. +typedef struct work_container {
  50472. + dwc_work_callback_t cb;
  50473. + void *data;
  50474. + dwc_workq_t *wq;
  50475. + char *name;
  50476. + int hz;
  50477. + struct work task;
  50478. +} work_container_t;
  50479. +
  50480. +struct dwc_workq {
  50481. + struct workqueue *taskq;
  50482. + dwc_spinlock_t *lock;
  50483. + dwc_waitq_t *waitq;
  50484. + int pending;
  50485. + struct work_container *container;
  50486. +};
  50487. +
  50488. +static void do_work(struct work *task, void *data)
  50489. +{
  50490. + dwc_workq_t *wq = (dwc_workq_t *)data;
  50491. + work_container_t *container = wq->container;
  50492. + dwc_irqflags_t flags;
  50493. +
  50494. + if (container->hz) {
  50495. + tsleep(container, 0, "dw3wrk", container->hz);
  50496. + }
  50497. +
  50498. + container->cb(container->data);
  50499. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  50500. +
  50501. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  50502. + if (container->name)
  50503. + DWC_FREE(container->name);
  50504. + DWC_FREE(container);
  50505. + wq->pending--;
  50506. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  50507. + DWC_WAITQ_TRIGGER(wq->waitq);
  50508. +}
  50509. +
  50510. +static int work_done(void *data)
  50511. +{
  50512. + dwc_workq_t *workq = (dwc_workq_t *)data;
  50513. +
  50514. + return workq->pending == 0;
  50515. +}
  50516. +
  50517. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  50518. +{
  50519. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  50520. +}
  50521. +
  50522. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  50523. +{
  50524. + int result;
  50525. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  50526. +
  50527. + if (!wq) {
  50528. + DWC_ERROR("Cannot allocate memory for workqueue");
  50529. + return NULL;
  50530. + }
  50531. +
  50532. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  50533. + IPL_BIO, 0);
  50534. + if (result) {
  50535. + DWC_ERROR("Cannot create workqueue");
  50536. + goto no_taskq;
  50537. + }
  50538. +
  50539. + wq->pending = 0;
  50540. +
  50541. + wq->lock = DWC_SPINLOCK_ALLOC();
  50542. + if (!wq->lock) {
  50543. + DWC_ERROR("Cannot allocate memory for spinlock");
  50544. + goto no_lock;
  50545. + }
  50546. +
  50547. + wq->waitq = DWC_WAITQ_ALLOC();
  50548. + if (!wq->waitq) {
  50549. + DWC_ERROR("Cannot allocate memory for waitqueue");
  50550. + goto no_waitq;
  50551. + }
  50552. +
  50553. + return wq;
  50554. +
  50555. + no_waitq:
  50556. + DWC_SPINLOCK_FREE(wq->lock);
  50557. + no_lock:
  50558. + workqueue_destroy(wq->taskq);
  50559. + no_taskq:
  50560. + DWC_FREE(wq);
  50561. +
  50562. + return NULL;
  50563. +}
  50564. +
  50565. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  50566. +{
  50567. +#ifdef DEBUG
  50568. + dwc_irqflags_t flags;
  50569. +
  50570. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  50571. +
  50572. + if (wq->pending != 0) {
  50573. + struct work_container *container = wq->container;
  50574. +
  50575. + DWC_ERROR("Destroying work queue with pending work");
  50576. +
  50577. + if (container && container->name) {
  50578. + DWC_ERROR("Work %s still pending", container->name);
  50579. + }
  50580. + }
  50581. +
  50582. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  50583. +#endif
  50584. + DWC_WAITQ_FREE(wq->waitq);
  50585. + DWC_SPINLOCK_FREE(wq->lock);
  50586. + workqueue_destroy(wq->taskq);
  50587. + DWC_FREE(wq);
  50588. +}
  50589. +
  50590. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  50591. + char *format, ...)
  50592. +{
  50593. + dwc_irqflags_t flags;
  50594. + work_container_t *container;
  50595. + static char name[128];
  50596. + va_list args;
  50597. +
  50598. + va_start(args, format);
  50599. + DWC_VSNPRINTF(name, 128, format, args);
  50600. + va_end(args);
  50601. +
  50602. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  50603. + wq->pending++;
  50604. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  50605. + DWC_WAITQ_TRIGGER(wq->waitq);
  50606. +
  50607. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  50608. + if (!container) {
  50609. + DWC_ERROR("Cannot allocate memory for container");
  50610. + return;
  50611. + }
  50612. +
  50613. + container->name = DWC_STRDUP(name);
  50614. + if (!container->name) {
  50615. + DWC_ERROR("Cannot allocate memory for container->name");
  50616. + DWC_FREE(container);
  50617. + return;
  50618. + }
  50619. +
  50620. + container->cb = cb;
  50621. + container->data = data;
  50622. + container->wq = wq;
  50623. + container->hz = 0;
  50624. + wq->container = container;
  50625. +
  50626. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  50627. + workqueue_enqueue(wq->taskq, &container->task);
  50628. +}
  50629. +
  50630. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  50631. + void *data, uint32_t time, char *format, ...)
  50632. +{
  50633. + dwc_irqflags_t flags;
  50634. + work_container_t *container;
  50635. + static char name[128];
  50636. + struct timeval tv;
  50637. + va_list args;
  50638. +
  50639. + va_start(args, format);
  50640. + DWC_VSNPRINTF(name, 128, format, args);
  50641. + va_end(args);
  50642. +
  50643. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  50644. + wq->pending++;
  50645. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  50646. + DWC_WAITQ_TRIGGER(wq->waitq);
  50647. +
  50648. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  50649. + if (!container) {
  50650. + DWC_ERROR("Cannot allocate memory for container");
  50651. + return;
  50652. + }
  50653. +
  50654. + container->name = DWC_STRDUP(name);
  50655. + if (!container->name) {
  50656. + DWC_ERROR("Cannot allocate memory for container->name");
  50657. + DWC_FREE(container);
  50658. + return;
  50659. + }
  50660. +
  50661. + container->cb = cb;
  50662. + container->data = data;
  50663. + container->wq = wq;
  50664. + tv.tv_sec = time / 1000;
  50665. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  50666. + container->hz = tvtohz(&tv);
  50667. + wq->container = container;
  50668. +
  50669. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  50670. + workqueue_enqueue(wq->taskq, &container->task);
  50671. +}
  50672. +
  50673. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  50674. +{
  50675. + return wq->pending;
  50676. +}
  50677. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_crypto.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c
  50678. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_crypto.c 1970-01-01 01:00:00.000000000 +0100
  50679. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.c 2014-04-13 17:33:11.000000000 +0200
  50680. @@ -0,0 +1,308 @@
  50681. +/* =========================================================================
  50682. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  50683. + * $Revision: #5 $
  50684. + * $Date: 2010/09/28 $
  50685. + * $Change: 1596182 $
  50686. + *
  50687. + * Synopsys Portability Library Software and documentation
  50688. + * (hereinafter, "Software") is an Unsupported proprietary work of
  50689. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  50690. + * between Synopsys and you.
  50691. + *
  50692. + * The Software IS NOT an item of Licensed Software or Licensed Product
  50693. + * under any End User Software License Agreement or Agreement for
  50694. + * Licensed Product with Synopsys or any supplement thereto. You are
  50695. + * permitted to use and redistribute this Software in source and binary
  50696. + * forms, with or without modification, provided that redistributions
  50697. + * of source code must retain this notice. You may not view, use,
  50698. + * disclose, copy or distribute this file or any information contained
  50699. + * herein except pursuant to this license grant from Synopsys. If you
  50700. + * do not agree with this notice, including the disclaimer below, then
  50701. + * you are not authorized to use the Software.
  50702. + *
  50703. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  50704. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  50705. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  50706. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  50707. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  50708. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  50709. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  50710. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  50711. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  50712. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  50713. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50714. + * DAMAGE.
  50715. + * ========================================================================= */
  50716. +
  50717. +/** @file
  50718. + * This file contains the WUSB cryptographic routines.
  50719. + */
  50720. +
  50721. +#ifdef DWC_CRYPTOLIB
  50722. +
  50723. +#include "dwc_crypto.h"
  50724. +#include "usb.h"
  50725. +
  50726. +#ifdef DEBUG
  50727. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  50728. +{
  50729. + int i;
  50730. + DWC_PRINTF("%s: ", name);
  50731. + for (i=0; i<len; i++) {
  50732. + DWC_PRINTF("%02x ", bytes[i]);
  50733. + }
  50734. + DWC_PRINTF("\n");
  50735. +}
  50736. +#else
  50737. +#define dump_bytes(x...)
  50738. +#endif
  50739. +
  50740. +/* Display a block */
  50741. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  50742. +{
  50743. +#ifdef DWC_DEBUG_CRYPTO
  50744. + int i, blksize = 16;
  50745. +
  50746. + DWC_DEBUG("%s", prefix);
  50747. +
  50748. + if (suffix == NULL) {
  50749. + suffix = "\n";
  50750. + blksize = a;
  50751. + }
  50752. +
  50753. + for (i = 0; i < blksize; i++)
  50754. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  50755. + DWC_PRINT(suffix);
  50756. +#endif
  50757. +}
  50758. +
  50759. +/**
  50760. + * Encrypts an array of bytes using the AES encryption engine.
  50761. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  50762. + * in-place.
  50763. + *
  50764. + * @return 0 on success, negative error code on error.
  50765. + */
  50766. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  50767. +{
  50768. + u8 block_t[16];
  50769. + DWC_MEMSET(block_t, 0, 16);
  50770. +
  50771. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  50772. +}
  50773. +
  50774. +/**
  50775. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  50776. + * This function takes a data string and returns the encrypted CBC
  50777. + * Counter-mode MIC.
  50778. + *
  50779. + * @param key The 128-bit symmetric key.
  50780. + * @param nonce The CCM nonce.
  50781. + * @param label The unique 14-byte ASCII text label.
  50782. + * @param bytes The byte array to be encrypted.
  50783. + * @param len Length of the byte array.
  50784. + * @param result Byte array to receive the 8-byte encrypted MIC.
  50785. + */
  50786. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  50787. + char *label, u8 *bytes, int len, u8 *result)
  50788. +{
  50789. + u8 block_m[16];
  50790. + u8 block_x[16];
  50791. + u8 block_t[8];
  50792. + int idx, blkNum;
  50793. + u16 la = (u16)(len + 14);
  50794. +
  50795. + /* Set the AES-128 key */
  50796. + //dwc_aes_setkey(tfm, key, 16);
  50797. +
  50798. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  50799. + block_m[0] = 0x59;
  50800. + for (idx = 0; idx < 13; idx++)
  50801. + block_m[idx + 1] = nonce[idx];
  50802. + block_m[14] = 0;
  50803. + block_m[15] = 0;
  50804. +
  50805. + /* Produce the CBC IV */
  50806. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  50807. + show_block(block_m, "CBC IV in: ", "\n", 0);
  50808. + show_block(block_x, "CBC IV out:", "\n", 0);
  50809. +
  50810. + /* Fill block B1 from l(a) = Blen + 14, and A */
  50811. + block_x[0] ^= (u8)(la >> 8);
  50812. + block_x[1] ^= (u8)la;
  50813. + for (idx = 0; idx < 14; idx++)
  50814. + block_x[idx + 2] ^= label[idx];
  50815. + show_block(block_x, "After xor: ", "b1\n", 16);
  50816. +
  50817. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  50818. + show_block(block_x, "After AES: ", "b1\n", 16);
  50819. +
  50820. + idx = 0;
  50821. + blkNum = 0;
  50822. +
  50823. + /* Fill remaining blocks with B */
  50824. + while (len-- > 0) {
  50825. + block_x[idx] ^= *bytes++;
  50826. + if (++idx >= 16) {
  50827. + idx = 0;
  50828. + show_block(block_x, "After xor: ", "\n", blkNum);
  50829. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  50830. + show_block(block_x, "After AES: ", "\n", blkNum);
  50831. + blkNum++;
  50832. + }
  50833. + }
  50834. +
  50835. + /* Handle partial last block */
  50836. + if (idx > 0) {
  50837. + show_block(block_x, "After xor: ", "\n", blkNum);
  50838. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  50839. + show_block(block_x, "After AES: ", "\n", blkNum);
  50840. + }
  50841. +
  50842. + /* Save the MIC tag */
  50843. + DWC_MEMCPY(block_t, block_x, 8);
  50844. + show_block(block_t, "MIC tag : ", NULL, 8);
  50845. +
  50846. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  50847. + block_m[0] = 0x01;
  50848. + block_m[14] = 0;
  50849. + block_m[15] = 0;
  50850. +
  50851. + /* Encrypt the counter */
  50852. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  50853. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  50854. +
  50855. + /* XOR with MIC tag */
  50856. + for (idx = 0; idx < 8; idx++) {
  50857. + block_t[idx] ^= block_x[idx];
  50858. + }
  50859. +
  50860. + /* Return result to caller */
  50861. + DWC_MEMCPY(result, block_t, 8);
  50862. + show_block(result, "CCM-MIC : ", NULL, 8);
  50863. +
  50864. +}
  50865. +
  50866. +/**
  50867. + * The PRF function described in section 6.5 of the WUSB spec. This function
  50868. + * concatenates MIC values returned from dwc_cmf() to create a value of
  50869. + * the requested length.
  50870. + *
  50871. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  50872. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  50873. + * @param result Byte array to receive the result.
  50874. + */
  50875. +void dwc_wusb_prf(int prf_len, u8 *key,
  50876. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  50877. +{
  50878. + int i;
  50879. +
  50880. + nonce[0] = 0;
  50881. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  50882. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  50883. + result += 8;
  50884. + }
  50885. +}
  50886. +
  50887. +/**
  50888. + * Fills in CCM Nonce per the WUSB spec.
  50889. + *
  50890. + * @param[in] haddr Host address.
  50891. + * @param[in] daddr Device address.
  50892. + * @param[in] tkid Session Key(PTK) identifier.
  50893. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  50894. + */
  50895. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  50896. + uint8_t *nonce)
  50897. +{
  50898. +
  50899. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  50900. +
  50901. + DWC_MEMSET(&nonce[0], 0, 16);
  50902. +
  50903. + DWC_MEMCPY(&nonce[6], tkid, 3);
  50904. + nonce[9] = daddr & 0xFF;
  50905. + nonce[10] = (daddr >> 8) & 0xFF;
  50906. + nonce[11] = haddr & 0xFF;
  50907. + nonce[12] = (haddr >> 8) & 0xFF;
  50908. +
  50909. + dump_bytes("CCM nonce", nonce, 16);
  50910. +}
  50911. +
  50912. +/**
  50913. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  50914. + * Nonce.
  50915. + */
  50916. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  50917. +{
  50918. + uint8_t inonce[16];
  50919. + uint32_t temp[4];
  50920. +
  50921. + /* Fill in the Nonce */
  50922. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  50923. + inonce[9] = addr & 0xFF;
  50924. + inonce[10] = (addr >> 8) & 0xFF;
  50925. + inonce[11] = inonce[9];
  50926. + inonce[12] = inonce[10];
  50927. +
  50928. + /* Collect "randomness samples" */
  50929. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  50930. +
  50931. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  50932. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  50933. + nonce);
  50934. +}
  50935. +
  50936. +/**
  50937. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  50938. + * WUSB spec.
  50939. + *
  50940. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  50941. + * @param[in] mk Master Key to derive the session from
  50942. + * @param[in] hnonce Pointer to Host Nonce.
  50943. + * @param[in] dnonce Pointer to Device Nonce.
  50944. + * @param[out] kck Pointer to where the KCK output is to be written.
  50945. + * @param[out] ptk Pointer to where the PTK output is to be written.
  50946. + */
  50947. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  50948. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  50949. +{
  50950. + uint8_t idata[32];
  50951. + uint8_t odata[32];
  50952. +
  50953. + dump_bytes("ck", mk, 16);
  50954. + dump_bytes("hnonce", hnonce, 16);
  50955. + dump_bytes("dnonce", dnonce, 16);
  50956. +
  50957. + /* The data is the HNonce and DNonce concatenated */
  50958. + DWC_MEMCPY(&idata[0], hnonce, 16);
  50959. + DWC_MEMCPY(&idata[16], dnonce, 16);
  50960. +
  50961. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  50962. +
  50963. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  50964. + DWC_MEMCPY(kck, &odata[0], 16);
  50965. + DWC_MEMCPY(ptk, &odata[16], 16);
  50966. +
  50967. + dump_bytes("kck", kck, 16);
  50968. + dump_bytes("ptk", ptk, 16);
  50969. +}
  50970. +
  50971. +/**
  50972. + * Generates the Message Integrity Code over the Handshake data per the
  50973. + * WUSB spec.
  50974. + *
  50975. + * @param ccm_nonce Pointer to CCM Nonce.
  50976. + * @param kck Pointer to Key Confirmation Key.
  50977. + * @param data Pointer to Handshake data to be checked.
  50978. + * @param mic Pointer to where the MIC output is to be written.
  50979. + */
  50980. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  50981. + uint8_t *data, uint8_t *mic)
  50982. +{
  50983. +
  50984. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  50985. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  50986. +}
  50987. +
  50988. +#endif /* DWC_CRYPTOLIB */
  50989. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_crypto.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h
  50990. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_crypto.h 1970-01-01 01:00:00.000000000 +0100
  50991. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_crypto.h 2014-04-13 17:33:11.000000000 +0200
  50992. @@ -0,0 +1,111 @@
  50993. +/* =========================================================================
  50994. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  50995. + * $Revision: #3 $
  50996. + * $Date: 2010/09/28 $
  50997. + * $Change: 1596182 $
  50998. + *
  50999. + * Synopsys Portability Library Software and documentation
  51000. + * (hereinafter, "Software") is an Unsupported proprietary work of
  51001. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  51002. + * between Synopsys and you.
  51003. + *
  51004. + * The Software IS NOT an item of Licensed Software or Licensed Product
  51005. + * under any End User Software License Agreement or Agreement for
  51006. + * Licensed Product with Synopsys or any supplement thereto. You are
  51007. + * permitted to use and redistribute this Software in source and binary
  51008. + * forms, with or without modification, provided that redistributions
  51009. + * of source code must retain this notice. You may not view, use,
  51010. + * disclose, copy or distribute this file or any information contained
  51011. + * herein except pursuant to this license grant from Synopsys. If you
  51012. + * do not agree with this notice, including the disclaimer below, then
  51013. + * you are not authorized to use the Software.
  51014. + *
  51015. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  51016. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51017. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  51018. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  51019. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  51020. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51021. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  51022. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  51023. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51024. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  51025. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51026. + * DAMAGE.
  51027. + * ========================================================================= */
  51028. +
  51029. +#ifndef _DWC_CRYPTO_H_
  51030. +#define _DWC_CRYPTO_H_
  51031. +
  51032. +#ifdef __cplusplus
  51033. +extern "C" {
  51034. +#endif
  51035. +
  51036. +/** @file
  51037. + *
  51038. + * This file contains declarations for the WUSB Cryptographic routines as
  51039. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  51040. + * modules.
  51041. + */
  51042. +
  51043. +#include "dwc_os.h"
  51044. +
  51045. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  51046. +
  51047. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  51048. + char *label, u8 *bytes, int len, u8 *result);
  51049. +void dwc_wusb_prf(int prf_len, u8 *key,
  51050. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  51051. +
  51052. +/**
  51053. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  51054. + *
  51055. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  51056. + */
  51057. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  51058. + char *label, u8 *bytes, int len, u8 *result)
  51059. +{
  51060. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  51061. +}
  51062. +
  51063. +/**
  51064. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  51065. + *
  51066. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  51067. + */
  51068. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  51069. + char *label, u8 *bytes, int len, u8 *result)
  51070. +{
  51071. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  51072. +}
  51073. +
  51074. +/**
  51075. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  51076. + *
  51077. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  51078. + */
  51079. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  51080. + char *label, u8 *bytes, int len, u8 *result)
  51081. +{
  51082. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  51083. +}
  51084. +
  51085. +
  51086. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  51087. + uint8_t *nonce);
  51088. +void dwc_wusb_gen_nonce(uint16_t addr,
  51089. + uint8_t *nonce);
  51090. +
  51091. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  51092. + uint8_t *hnonce, uint8_t *dnonce,
  51093. + uint8_t *kck, uint8_t *ptk);
  51094. +
  51095. +
  51096. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  51097. + *kck, uint8_t *data, uint8_t *mic);
  51098. +
  51099. +#ifdef __cplusplus
  51100. +}
  51101. +#endif
  51102. +
  51103. +#endif /* _DWC_CRYPTO_H_ */
  51104. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_dh.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c
  51105. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_dh.c 1970-01-01 01:00:00.000000000 +0100
  51106. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_dh.c 2014-09-14 19:04:13.000000000 +0200
  51107. @@ -0,0 +1,291 @@
  51108. +/* =========================================================================
  51109. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  51110. + * $Revision: #3 $
  51111. + * $Date: 2010/09/28 $
  51112. + * $Change: 1596182 $
  51113. + *
  51114. + * Synopsys Portability Library Software and documentation
  51115. + * (hereinafter, "Software") is an Unsupported proprietary work of
  51116. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  51117. + * between Synopsys and you.
  51118. + *
  51119. + * The Software IS NOT an item of Licensed Software or Licensed Product
  51120. + * under any End User Software License Agreement or Agreement for
  51121. + * Licensed Product with Synopsys or any supplement thereto. You are
  51122. + * permitted to use and redistribute this Software in source and binary
  51123. + * forms, with or without modification, provided that redistributions
  51124. + * of source code must retain this notice. You may not view, use,
  51125. + * disclose, copy or distribute this file or any information contained
  51126. + * herein except pursuant to this license grant from Synopsys. If you
  51127. + * do not agree with this notice, including the disclaimer below, then
  51128. + * you are not authorized to use the Software.
  51129. + *
  51130. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  51131. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51132. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  51133. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  51134. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  51135. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51136. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  51137. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  51138. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51139. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  51140. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51141. + * DAMAGE.
  51142. + * ========================================================================= */
  51143. +#ifdef DWC_CRYPTOLIB
  51144. +
  51145. +#ifndef CONFIG_MACH_IPMATE
  51146. +
  51147. +#include "dwc_dh.h"
  51148. +#include "dwc_modpow.h"
  51149. +
  51150. +#ifdef DEBUG
  51151. +/* This function prints out a buffer in the format described in the Association
  51152. + * Model specification. */
  51153. +static void dh_dump(char *str, void *_num, int len)
  51154. +{
  51155. + uint8_t *num = _num;
  51156. + int i;
  51157. + DWC_PRINTF("%s\n", str);
  51158. + for (i = 0; i < len; i ++) {
  51159. + DWC_PRINTF("%02x", num[i]);
  51160. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  51161. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  51162. + }
  51163. +
  51164. + DWC_PRINTF("\n");
  51165. +}
  51166. +#else
  51167. +#define dh_dump(_x...) do {; } while(0)
  51168. +#endif
  51169. +
  51170. +/* Constant g value */
  51171. +static __u32 dh_g[] = {
  51172. + 0x02000000,
  51173. +};
  51174. +
  51175. +/* Constant p value */
  51176. +static __u32 dh_p[] = {
  51177. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  51178. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  51179. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  51180. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  51181. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  51182. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  51183. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  51184. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  51185. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  51186. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  51187. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  51188. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  51189. +};
  51190. +
  51191. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  51192. +{
  51193. + uint8_t *in = _in;
  51194. + uint8_t *out = _out;
  51195. + int i;
  51196. + for (i=0; i<len; i++) {
  51197. + out[i] = in[len-1-i];
  51198. + }
  51199. +}
  51200. +
  51201. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  51202. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  51203. + * of 4. */
  51204. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  51205. + void *exp, uint32_t exp_len,
  51206. + void *mod, uint32_t mod_len,
  51207. + void *out)
  51208. +{
  51209. + /* modpow() takes little endian numbers. AM uses big-endian. This
  51210. + * function swaps bytes of numbers before passing onto modpow. */
  51211. +
  51212. + int retval = 0;
  51213. + uint32_t *result;
  51214. +
  51215. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  51216. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  51217. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  51218. +
  51219. + dh_swap_bytes(num, &bignum_num[1], num_len);
  51220. + bignum_num[0] = num_len / 4;
  51221. +
  51222. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  51223. + bignum_exp[0] = exp_len / 4;
  51224. +
  51225. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  51226. + bignum_mod[0] = mod_len / 4;
  51227. +
  51228. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  51229. + if (!result) {
  51230. + retval = -1;
  51231. + goto dh_modpow_nomem;
  51232. + }
  51233. +
  51234. + dh_swap_bytes(&result[1], out, result[0] * 4);
  51235. + dwc_free(mem_ctx, result);
  51236. +
  51237. + dh_modpow_nomem:
  51238. + dwc_free(mem_ctx, bignum_num);
  51239. + dwc_free(mem_ctx, bignum_exp);
  51240. + dwc_free(mem_ctx, bignum_mod);
  51241. + return retval;
  51242. +}
  51243. +
  51244. +
  51245. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  51246. +{
  51247. + int retval;
  51248. + uint8_t m3[385];
  51249. +
  51250. +#ifndef DH_TEST_VECTORS
  51251. + DWC_RANDOM_BYTES(exp, 32);
  51252. +#endif
  51253. +
  51254. + /* Compute the pkd */
  51255. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  51256. + exp, 32,
  51257. + dh_p, 384, pk))) {
  51258. + return retval;
  51259. + }
  51260. +
  51261. + m3[384] = nd;
  51262. + DWC_MEMCPY(&m3[0], pk, 384);
  51263. + DWC_SHA256(m3, 385, hash);
  51264. +
  51265. + dh_dump("PK", pk, 384);
  51266. + dh_dump("SHA-256(M3)", hash, 32);
  51267. + return 0;
  51268. +}
  51269. +
  51270. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  51271. + uint8_t *exp, int is_host,
  51272. + char *dd, uint8_t *ck, uint8_t *kdk)
  51273. +{
  51274. + int retval;
  51275. + uint8_t mv[784];
  51276. + uint8_t sha_result[32];
  51277. + uint8_t dhkey[384];
  51278. + uint8_t shared_secret[384];
  51279. + char *message;
  51280. + uint32_t vd;
  51281. +
  51282. + uint8_t *pk;
  51283. +
  51284. + if (is_host) {
  51285. + pk = pkd;
  51286. + }
  51287. + else {
  51288. + pk = pkh;
  51289. + }
  51290. +
  51291. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  51292. + exp, 32,
  51293. + dh_p, 384, shared_secret))) {
  51294. + return retval;
  51295. + }
  51296. + dh_dump("Shared Secret", shared_secret, 384);
  51297. +
  51298. + DWC_SHA256(shared_secret, 384, dhkey);
  51299. + dh_dump("DHKEY", dhkey, 384);
  51300. +
  51301. + DWC_MEMCPY(&mv[0], pkd, 384);
  51302. + DWC_MEMCPY(&mv[384], pkh, 384);
  51303. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  51304. + dh_dump("MV", mv, 784);
  51305. +
  51306. + DWC_SHA256(mv, 784, sha_result);
  51307. + dh_dump("SHA-256(MV)", sha_result, 32);
  51308. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  51309. +
  51310. + dh_swap_bytes(sha_result, &vd, 4);
  51311. +#ifdef DEBUG
  51312. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  51313. +#endif
  51314. +
  51315. + switch (nd) {
  51316. + case 2:
  51317. + vd = vd % 100;
  51318. + DWC_SPRINTF(dd, "%02d", vd);
  51319. + break;
  51320. + case 3:
  51321. + vd = vd % 1000;
  51322. + DWC_SPRINTF(dd, "%03d", vd);
  51323. + break;
  51324. + case 4:
  51325. + vd = vd % 10000;
  51326. + DWC_SPRINTF(dd, "%04d", vd);
  51327. + break;
  51328. + }
  51329. +#ifdef DEBUG
  51330. + DWC_PRINTF("Display Digits: %s\n", dd);
  51331. +#endif
  51332. +
  51333. + message = "connection key";
  51334. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  51335. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  51336. + DWC_MEMCPY(ck, sha_result, 16);
  51337. +
  51338. + message = "key derivation key";
  51339. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  51340. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  51341. + DWC_MEMCPY(kdk, sha_result, 32);
  51342. +
  51343. + return 0;
  51344. +}
  51345. +
  51346. +
  51347. +#ifdef DH_TEST_VECTORS
  51348. +
  51349. +static __u8 dh_a[] = {
  51350. + 0x44, 0x00, 0x51, 0xd6,
  51351. + 0xf0, 0xb5, 0x5e, 0xa9,
  51352. + 0x67, 0xab, 0x31, 0xc6,
  51353. + 0x8a, 0x8b, 0x5e, 0x37,
  51354. + 0xd9, 0x10, 0xda, 0xe0,
  51355. + 0xe2, 0xd4, 0x59, 0xa4,
  51356. + 0x86, 0x45, 0x9c, 0xaa,
  51357. + 0xdf, 0x36, 0x75, 0x16,
  51358. +};
  51359. +
  51360. +static __u8 dh_b[] = {
  51361. + 0x5d, 0xae, 0xc7, 0x86,
  51362. + 0x79, 0x80, 0xa3, 0x24,
  51363. + 0x8c, 0xe3, 0x57, 0x8f,
  51364. + 0xc7, 0x5f, 0x1b, 0x0f,
  51365. + 0x2d, 0xf8, 0x9d, 0x30,
  51366. + 0x6f, 0xa4, 0x52, 0xcd,
  51367. + 0xe0, 0x7a, 0x04, 0x8a,
  51368. + 0xde, 0xd9, 0x26, 0x56,
  51369. +};
  51370. +
  51371. +void dwc_run_dh_test_vectors(void *mem_ctx)
  51372. +{
  51373. + uint8_t pkd[384];
  51374. + uint8_t pkh[384];
  51375. + uint8_t hashd[32];
  51376. + uint8_t hashh[32];
  51377. + uint8_t ck[16];
  51378. + uint8_t kdk[32];
  51379. + char dd[5];
  51380. +
  51381. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  51382. +
  51383. + /* compute the PKd and SHA-256(PKd || Nd) */
  51384. + DWC_PRINTF("Computing PKd\n");
  51385. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  51386. +
  51387. + /* compute the PKd and SHA-256(PKh || Nd) */
  51388. + DWC_PRINTF("Computing PKh\n");
  51389. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  51390. +
  51391. + /* compute the dhkey */
  51392. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  51393. +}
  51394. +#endif /* DH_TEST_VECTORS */
  51395. +
  51396. +#endif /* !CONFIG_MACH_IPMATE */
  51397. +
  51398. +#endif /* DWC_CRYPTOLIB */
  51399. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_dh.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h
  51400. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_dh.h 1970-01-01 01:00:00.000000000 +0100
  51401. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_dh.h 2014-04-13 17:33:11.000000000 +0200
  51402. @@ -0,0 +1,106 @@
  51403. +/* =========================================================================
  51404. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  51405. + * $Revision: #4 $
  51406. + * $Date: 2010/09/28 $
  51407. + * $Change: 1596182 $
  51408. + *
  51409. + * Synopsys Portability Library Software and documentation
  51410. + * (hereinafter, "Software") is an Unsupported proprietary work of
  51411. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  51412. + * between Synopsys and you.
  51413. + *
  51414. + * The Software IS NOT an item of Licensed Software or Licensed Product
  51415. + * under any End User Software License Agreement or Agreement for
  51416. + * Licensed Product with Synopsys or any supplement thereto. You are
  51417. + * permitted to use and redistribute this Software in source and binary
  51418. + * forms, with or without modification, provided that redistributions
  51419. + * of source code must retain this notice. You may not view, use,
  51420. + * disclose, copy or distribute this file or any information contained
  51421. + * herein except pursuant to this license grant from Synopsys. If you
  51422. + * do not agree with this notice, including the disclaimer below, then
  51423. + * you are not authorized to use the Software.
  51424. + *
  51425. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  51426. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51427. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  51428. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  51429. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  51430. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51431. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  51432. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  51433. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  51434. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  51435. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51436. + * DAMAGE.
  51437. + * ========================================================================= */
  51438. +#ifndef _DWC_DH_H_
  51439. +#define _DWC_DH_H_
  51440. +
  51441. +#ifdef __cplusplus
  51442. +extern "C" {
  51443. +#endif
  51444. +
  51445. +#include "dwc_os.h"
  51446. +
  51447. +/** @file
  51448. + *
  51449. + * This file defines the common functions on device and host for performing
  51450. + * numeric association as defined in the WUSB spec. They are only to be
  51451. + * used internally by the DWC UWB modules. */
  51452. +
  51453. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  51454. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  51455. + uint8_t *key, uint32_t keylen,
  51456. + uint8_t *out);
  51457. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  51458. + void *exp, uint32_t exp_len,
  51459. + void *mod, uint32_t mod_len,
  51460. + void *out);
  51461. +
  51462. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  51463. + *
  51464. + * PK = g^exp mod p.
  51465. + *
  51466. + * Input:
  51467. + * Nd = Number of digits on the device.
  51468. + *
  51469. + * Output:
  51470. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  51471. + * used as either A or B.
  51472. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  51473. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  51474. + */
  51475. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  51476. +
  51477. +/** Computes the DHKEY, and VD.
  51478. + *
  51479. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  51480. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  51481. + *
  51482. + * Input:
  51483. + * pkd = The PKD value.
  51484. + * pkh = The PKH value.
  51485. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  51486. + * is_host = Set to non zero if a WUSB host is calling this function.
  51487. + *
  51488. + * Output:
  51489. +
  51490. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  51491. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  51492. + * null termination character. This buffer can be used directly for display.
  51493. + * ck = A 16-byte buffer to be filled with the CK.
  51494. + * kdk = A 32-byte buffer to be filled with the KDK.
  51495. + */
  51496. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  51497. + uint8_t *exp, int is_host,
  51498. + char *dd, uint8_t *ck, uint8_t *kdk);
  51499. +
  51500. +#ifdef DH_TEST_VECTORS
  51501. +extern void dwc_run_dh_test_vectors(void);
  51502. +#endif
  51503. +
  51504. +#ifdef __cplusplus
  51505. +}
  51506. +#endif
  51507. +
  51508. +#endif /* _DWC_DH_H_ */
  51509. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_list.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_list.h
  51510. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_list.h 1970-01-01 01:00:00.000000000 +0100
  51511. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_list.h 2014-04-13 17:33:11.000000000 +0200
  51512. @@ -0,0 +1,594 @@
  51513. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  51514. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  51515. +
  51516. +/*
  51517. + * Copyright (c) 1991, 1993
  51518. + * The Regents of the University of California. All rights reserved.
  51519. + *
  51520. + * Redistribution and use in source and binary forms, with or without
  51521. + * modification, are permitted provided that the following conditions
  51522. + * are met:
  51523. + * 1. Redistributions of source code must retain the above copyright
  51524. + * notice, this list of conditions and the following disclaimer.
  51525. + * 2. Redistributions in binary form must reproduce the above copyright
  51526. + * notice, this list of conditions and the following disclaimer in the
  51527. + * documentation and/or other materials provided with the distribution.
  51528. + * 3. Neither the name of the University nor the names of its contributors
  51529. + * may be used to endorse or promote products derived from this software
  51530. + * without specific prior written permission.
  51531. + *
  51532. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  51533. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51534. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51535. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  51536. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  51537. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  51538. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51539. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51540. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51541. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51542. + * SUCH DAMAGE.
  51543. + *
  51544. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  51545. + */
  51546. +
  51547. +#ifndef _DWC_LIST_H_
  51548. +#define _DWC_LIST_H_
  51549. +
  51550. +#ifdef __cplusplus
  51551. +extern "C" {
  51552. +#endif
  51553. +
  51554. +/** @file
  51555. + *
  51556. + * This file defines linked list operations. It is derived from BSD with
  51557. + * only the MACRO names being prefixed with DWC_. This is because a few of
  51558. + * these names conflict with those on Linux. For documentation on use, see the
  51559. + * inline comments in the source code. The original license for this source
  51560. + * code applies and is preserved in the dwc_list.h source file.
  51561. + */
  51562. +
  51563. +/*
  51564. + * This file defines five types of data structures: singly-linked lists,
  51565. + * lists, simple queues, tail queues, and circular queues.
  51566. + *
  51567. + *
  51568. + * A singly-linked list is headed by a single forward pointer. The elements
  51569. + * are singly linked for minimum space and pointer manipulation overhead at
  51570. + * the expense of O(n) removal for arbitrary elements. New elements can be
  51571. + * added to the list after an existing element or at the head of the list.
  51572. + * Elements being removed from the head of the list should use the explicit
  51573. + * macro for this purpose for optimum efficiency. A singly-linked list may
  51574. + * only be traversed in the forward direction. Singly-linked lists are ideal
  51575. + * for applications with large datasets and few or no removals or for
  51576. + * implementing a LIFO queue.
  51577. + *
  51578. + * A list is headed by a single forward pointer (or an array of forward
  51579. + * pointers for a hash table header). The elements are doubly linked
  51580. + * so that an arbitrary element can be removed without a need to
  51581. + * traverse the list. New elements can be added to the list before
  51582. + * or after an existing element or at the head of the list. A list
  51583. + * may only be traversed in the forward direction.
  51584. + *
  51585. + * A simple queue is headed by a pair of pointers, one the head of the
  51586. + * list and the other to the tail of the list. The elements are singly
  51587. + * linked to save space, so elements can only be removed from the
  51588. + * head of the list. New elements can be added to the list before or after
  51589. + * an existing element, at the head of the list, or at the end of the
  51590. + * list. A simple queue may only be traversed in the forward direction.
  51591. + *
  51592. + * A tail queue is headed by a pair of pointers, one to the head of the
  51593. + * list and the other to the tail of the list. The elements are doubly
  51594. + * linked so that an arbitrary element can be removed without a need to
  51595. + * traverse the list. New elements can be added to the list before or
  51596. + * after an existing element, at the head of the list, or at the end of
  51597. + * the list. A tail queue may be traversed in either direction.
  51598. + *
  51599. + * A circle queue is headed by a pair of pointers, one to the head of the
  51600. + * list and the other to the tail of the list. The elements are doubly
  51601. + * linked so that an arbitrary element can be removed without a need to
  51602. + * traverse the list. New elements can be added to the list before or after
  51603. + * an existing element, at the head of the list, or at the end of the list.
  51604. + * A circle queue may be traversed in either direction, but has a more
  51605. + * complex end of list detection.
  51606. + *
  51607. + * For details on the use of these macros, see the queue(3) manual page.
  51608. + */
  51609. +
  51610. +/*
  51611. + * Double-linked List.
  51612. + */
  51613. +
  51614. +typedef struct dwc_list_link {
  51615. + struct dwc_list_link *next;
  51616. + struct dwc_list_link *prev;
  51617. +} dwc_list_link_t;
  51618. +
  51619. +#define DWC_LIST_INIT(link) do { \
  51620. + (link)->next = (link); \
  51621. + (link)->prev = (link); \
  51622. +} while (0)
  51623. +
  51624. +#define DWC_LIST_FIRST(link) ((link)->next)
  51625. +#define DWC_LIST_LAST(link) ((link)->prev)
  51626. +#define DWC_LIST_END(link) (link)
  51627. +#define DWC_LIST_NEXT(link) ((link)->next)
  51628. +#define DWC_LIST_PREV(link) ((link)->prev)
  51629. +#define DWC_LIST_EMPTY(link) \
  51630. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  51631. +#define DWC_LIST_ENTRY(link, type, field) \
  51632. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  51633. +
  51634. +#if 0
  51635. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  51636. + (link)->next = (list)->next; \
  51637. + (link)->prev = (list); \
  51638. + (list)->next->prev = (link); \
  51639. + (list)->next = (link); \
  51640. +} while (0)
  51641. +
  51642. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  51643. + (link)->next = (list); \
  51644. + (link)->prev = (list)->prev; \
  51645. + (list)->prev->next = (link); \
  51646. + (list)->prev = (link); \
  51647. +} while (0)
  51648. +#else
  51649. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  51650. + dwc_list_link_t *__next__ = (list)->next; \
  51651. + __next__->prev = (link); \
  51652. + (link)->next = __next__; \
  51653. + (link)->prev = (list); \
  51654. + (list)->next = (link); \
  51655. +} while (0)
  51656. +
  51657. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  51658. + dwc_list_link_t *__prev__ = (list)->prev; \
  51659. + (list)->prev = (link); \
  51660. + (link)->next = (list); \
  51661. + (link)->prev = __prev__; \
  51662. + __prev__->next = (link); \
  51663. +} while (0)
  51664. +#endif
  51665. +
  51666. +#if 0
  51667. +static inline void __list_add(struct list_head *new,
  51668. + struct list_head *prev,
  51669. + struct list_head *next)
  51670. +{
  51671. + next->prev = new;
  51672. + new->next = next;
  51673. + new->prev = prev;
  51674. + prev->next = new;
  51675. +}
  51676. +
  51677. +static inline void list_add(struct list_head *new, struct list_head *head)
  51678. +{
  51679. + __list_add(new, head, head->next);
  51680. +}
  51681. +
  51682. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  51683. +{
  51684. + __list_add(new, head->prev, head);
  51685. +}
  51686. +
  51687. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  51688. +{
  51689. + next->prev = prev;
  51690. + prev->next = next;
  51691. +}
  51692. +
  51693. +static inline void list_del(struct list_head *entry)
  51694. +{
  51695. + __list_del(entry->prev, entry->next);
  51696. + entry->next = LIST_POISON1;
  51697. + entry->prev = LIST_POISON2;
  51698. +}
  51699. +#endif
  51700. +
  51701. +#define DWC_LIST_REMOVE(link) do { \
  51702. + (link)->next->prev = (link)->prev; \
  51703. + (link)->prev->next = (link)->next; \
  51704. +} while (0)
  51705. +
  51706. +#define DWC_LIST_REMOVE_INIT(link) do { \
  51707. + DWC_LIST_REMOVE(link); \
  51708. + DWC_LIST_INIT(link); \
  51709. +} while (0)
  51710. +
  51711. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  51712. + DWC_LIST_REMOVE(link); \
  51713. + DWC_LIST_INSERT_HEAD(list, link); \
  51714. +} while (0)
  51715. +
  51716. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  51717. + DWC_LIST_REMOVE(link); \
  51718. + DWC_LIST_INSERT_TAIL(list, link); \
  51719. +} while (0)
  51720. +
  51721. +#define DWC_LIST_FOREACH(var, list) \
  51722. + for((var) = DWC_LIST_FIRST(list); \
  51723. + (var) != DWC_LIST_END(list); \
  51724. + (var) = DWC_LIST_NEXT(var))
  51725. +
  51726. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  51727. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  51728. + (var) != DWC_LIST_END(list); \
  51729. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  51730. +
  51731. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  51732. + for((var) = DWC_LIST_LAST(list); \
  51733. + (var) != DWC_LIST_END(list); \
  51734. + (var) = DWC_LIST_PREV(var))
  51735. +
  51736. +/*
  51737. + * Singly-linked List definitions.
  51738. + */
  51739. +#define DWC_SLIST_HEAD(name, type) \
  51740. +struct name { \
  51741. + struct type *slh_first; /* first element */ \
  51742. +}
  51743. +
  51744. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  51745. + { NULL }
  51746. +
  51747. +#define DWC_SLIST_ENTRY(type) \
  51748. +struct { \
  51749. + struct type *sle_next; /* next element */ \
  51750. +}
  51751. +
  51752. +/*
  51753. + * Singly-linked List access methods.
  51754. + */
  51755. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  51756. +#define DWC_SLIST_END(head) NULL
  51757. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  51758. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  51759. +
  51760. +#define DWC_SLIST_FOREACH(var, head, field) \
  51761. + for((var) = SLIST_FIRST(head); \
  51762. + (var) != SLIST_END(head); \
  51763. + (var) = SLIST_NEXT(var, field))
  51764. +
  51765. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  51766. + for((varp) = &SLIST_FIRST((head)); \
  51767. + ((var) = *(varp)) != SLIST_END(head); \
  51768. + (varp) = &SLIST_NEXT((var), field))
  51769. +
  51770. +/*
  51771. + * Singly-linked List functions.
  51772. + */
  51773. +#define DWC_SLIST_INIT(head) { \
  51774. + SLIST_FIRST(head) = SLIST_END(head); \
  51775. +}
  51776. +
  51777. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  51778. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  51779. + (slistelm)->field.sle_next = (elm); \
  51780. +} while (0)
  51781. +
  51782. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  51783. + (elm)->field.sle_next = (head)->slh_first; \
  51784. + (head)->slh_first = (elm); \
  51785. +} while (0)
  51786. +
  51787. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  51788. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  51789. +} while (0)
  51790. +
  51791. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  51792. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  51793. +} while (0)
  51794. +
  51795. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  51796. + if ((head)->slh_first == (elm)) { \
  51797. + SLIST_REMOVE_HEAD((head), field); \
  51798. + } \
  51799. + else { \
  51800. + struct type *curelm = (head)->slh_first; \
  51801. + while( curelm->field.sle_next != (elm) ) \
  51802. + curelm = curelm->field.sle_next; \
  51803. + curelm->field.sle_next = \
  51804. + curelm->field.sle_next->field.sle_next; \
  51805. + } \
  51806. +} while (0)
  51807. +
  51808. +/*
  51809. + * Simple queue definitions.
  51810. + */
  51811. +#define DWC_SIMPLEQ_HEAD(name, type) \
  51812. +struct name { \
  51813. + struct type *sqh_first; /* first element */ \
  51814. + struct type **sqh_last; /* addr of last next element */ \
  51815. +}
  51816. +
  51817. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  51818. + { NULL, &(head).sqh_first }
  51819. +
  51820. +#define DWC_SIMPLEQ_ENTRY(type) \
  51821. +struct { \
  51822. + struct type *sqe_next; /* next element */ \
  51823. +}
  51824. +
  51825. +/*
  51826. + * Simple queue access methods.
  51827. + */
  51828. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  51829. +#define DWC_SIMPLEQ_END(head) NULL
  51830. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  51831. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  51832. +
  51833. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  51834. + for((var) = SIMPLEQ_FIRST(head); \
  51835. + (var) != SIMPLEQ_END(head); \
  51836. + (var) = SIMPLEQ_NEXT(var, field))
  51837. +
  51838. +/*
  51839. + * Simple queue functions.
  51840. + */
  51841. +#define DWC_SIMPLEQ_INIT(head) do { \
  51842. + (head)->sqh_first = NULL; \
  51843. + (head)->sqh_last = &(head)->sqh_first; \
  51844. +} while (0)
  51845. +
  51846. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  51847. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  51848. + (head)->sqh_last = &(elm)->field.sqe_next; \
  51849. + (head)->sqh_first = (elm); \
  51850. +} while (0)
  51851. +
  51852. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  51853. + (elm)->field.sqe_next = NULL; \
  51854. + *(head)->sqh_last = (elm); \
  51855. + (head)->sqh_last = &(elm)->field.sqe_next; \
  51856. +} while (0)
  51857. +
  51858. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  51859. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  51860. + (head)->sqh_last = &(elm)->field.sqe_next; \
  51861. + (listelm)->field.sqe_next = (elm); \
  51862. +} while (0)
  51863. +
  51864. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  51865. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  51866. + (head)->sqh_last = &(head)->sqh_first; \
  51867. +} while (0)
  51868. +
  51869. +/*
  51870. + * Tail queue definitions.
  51871. + */
  51872. +#define DWC_TAILQ_HEAD(name, type) \
  51873. +struct name { \
  51874. + struct type *tqh_first; /* first element */ \
  51875. + struct type **tqh_last; /* addr of last next element */ \
  51876. +}
  51877. +
  51878. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  51879. + { NULL, &(head).tqh_first }
  51880. +
  51881. +#define DWC_TAILQ_ENTRY(type) \
  51882. +struct { \
  51883. + struct type *tqe_next; /* next element */ \
  51884. + struct type **tqe_prev; /* address of previous next element */ \
  51885. +}
  51886. +
  51887. +/*
  51888. + * tail queue access methods
  51889. + */
  51890. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  51891. +#define DWC_TAILQ_END(head) NULL
  51892. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  51893. +#define DWC_TAILQ_LAST(head, headname) \
  51894. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  51895. +/* XXX */
  51896. +#define DWC_TAILQ_PREV(elm, headname, field) \
  51897. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  51898. +#define DWC_TAILQ_EMPTY(head) \
  51899. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  51900. +
  51901. +#define DWC_TAILQ_FOREACH(var, head, field) \
  51902. + for ((var) = DWC_TAILQ_FIRST(head); \
  51903. + (var) != DWC_TAILQ_END(head); \
  51904. + (var) = DWC_TAILQ_NEXT(var, field))
  51905. +
  51906. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  51907. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  51908. + (var) != DWC_TAILQ_END(head); \
  51909. + (var) = DWC_TAILQ_PREV(var, headname, field))
  51910. +
  51911. +/*
  51912. + * Tail queue functions.
  51913. + */
  51914. +#define DWC_TAILQ_INIT(head) do { \
  51915. + (head)->tqh_first = NULL; \
  51916. + (head)->tqh_last = &(head)->tqh_first; \
  51917. +} while (0)
  51918. +
  51919. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  51920. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  51921. + (head)->tqh_first->field.tqe_prev = \
  51922. + &(elm)->field.tqe_next; \
  51923. + else \
  51924. + (head)->tqh_last = &(elm)->field.tqe_next; \
  51925. + (head)->tqh_first = (elm); \
  51926. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  51927. +} while (0)
  51928. +
  51929. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  51930. + (elm)->field.tqe_next = NULL; \
  51931. + (elm)->field.tqe_prev = (head)->tqh_last; \
  51932. + *(head)->tqh_last = (elm); \
  51933. + (head)->tqh_last = &(elm)->field.tqe_next; \
  51934. +} while (0)
  51935. +
  51936. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  51937. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  51938. + (elm)->field.tqe_next->field.tqe_prev = \
  51939. + &(elm)->field.tqe_next; \
  51940. + else \
  51941. + (head)->tqh_last = &(elm)->field.tqe_next; \
  51942. + (listelm)->field.tqe_next = (elm); \
  51943. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  51944. +} while (0)
  51945. +
  51946. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  51947. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  51948. + (elm)->field.tqe_next = (listelm); \
  51949. + *(listelm)->field.tqe_prev = (elm); \
  51950. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  51951. +} while (0)
  51952. +
  51953. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  51954. + if (((elm)->field.tqe_next) != NULL) \
  51955. + (elm)->field.tqe_next->field.tqe_prev = \
  51956. + (elm)->field.tqe_prev; \
  51957. + else \
  51958. + (head)->tqh_last = (elm)->field.tqe_prev; \
  51959. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  51960. +} while (0)
  51961. +
  51962. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  51963. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  51964. + (elm2)->field.tqe_next->field.tqe_prev = \
  51965. + &(elm2)->field.tqe_next; \
  51966. + else \
  51967. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  51968. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  51969. + *(elm2)->field.tqe_prev = (elm2); \
  51970. +} while (0)
  51971. +
  51972. +/*
  51973. + * Circular queue definitions.
  51974. + */
  51975. +#define DWC_CIRCLEQ_HEAD(name, type) \
  51976. +struct name { \
  51977. + struct type *cqh_first; /* first element */ \
  51978. + struct type *cqh_last; /* last element */ \
  51979. +}
  51980. +
  51981. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  51982. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  51983. +
  51984. +#define DWC_CIRCLEQ_ENTRY(type) \
  51985. +struct { \
  51986. + struct type *cqe_next; /* next element */ \
  51987. + struct type *cqe_prev; /* previous element */ \
  51988. +}
  51989. +
  51990. +/*
  51991. + * Circular queue access methods
  51992. + */
  51993. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  51994. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  51995. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  51996. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  51997. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  51998. +#define DWC_CIRCLEQ_EMPTY(head) \
  51999. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  52000. +
  52001. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  52002. +
  52003. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  52004. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  52005. + (var) != DWC_CIRCLEQ_END(head); \
  52006. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  52007. +
  52008. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  52009. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  52010. + (var) != DWC_CIRCLEQ_END(head); \
  52011. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  52012. +
  52013. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  52014. + for((var) = DWC_CIRCLEQ_LAST(head); \
  52015. + (var) != DWC_CIRCLEQ_END(head); \
  52016. + (var) = DWC_CIRCLEQ_PREV(var, field))
  52017. +
  52018. +/*
  52019. + * Circular queue functions.
  52020. + */
  52021. +#define DWC_CIRCLEQ_INIT(head) do { \
  52022. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  52023. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  52024. +} while (0)
  52025. +
  52026. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  52027. + (elm)->field.cqe_next = NULL; \
  52028. + (elm)->field.cqe_prev = NULL; \
  52029. +} while (0)
  52030. +
  52031. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  52032. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  52033. + (elm)->field.cqe_prev = (listelm); \
  52034. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  52035. + (head)->cqh_last = (elm); \
  52036. + else \
  52037. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  52038. + (listelm)->field.cqe_next = (elm); \
  52039. +} while (0)
  52040. +
  52041. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  52042. + (elm)->field.cqe_next = (listelm); \
  52043. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  52044. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  52045. + (head)->cqh_first = (elm); \
  52046. + else \
  52047. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  52048. + (listelm)->field.cqe_prev = (elm); \
  52049. +} while (0)
  52050. +
  52051. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  52052. + (elm)->field.cqe_next = (head)->cqh_first; \
  52053. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  52054. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  52055. + (head)->cqh_last = (elm); \
  52056. + else \
  52057. + (head)->cqh_first->field.cqe_prev = (elm); \
  52058. + (head)->cqh_first = (elm); \
  52059. +} while (0)
  52060. +
  52061. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  52062. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  52063. + (elm)->field.cqe_prev = (head)->cqh_last; \
  52064. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  52065. + (head)->cqh_first = (elm); \
  52066. + else \
  52067. + (head)->cqh_last->field.cqe_next = (elm); \
  52068. + (head)->cqh_last = (elm); \
  52069. +} while (0)
  52070. +
  52071. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  52072. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  52073. + (head)->cqh_last = (elm)->field.cqe_prev; \
  52074. + else \
  52075. + (elm)->field.cqe_next->field.cqe_prev = \
  52076. + (elm)->field.cqe_prev; \
  52077. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  52078. + (head)->cqh_first = (elm)->field.cqe_next; \
  52079. + else \
  52080. + (elm)->field.cqe_prev->field.cqe_next = \
  52081. + (elm)->field.cqe_next; \
  52082. +} while (0)
  52083. +
  52084. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  52085. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  52086. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  52087. +} while (0)
  52088. +
  52089. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  52090. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  52091. + DWC_CIRCLEQ_END(head)) \
  52092. + (head).cqh_last = (elm2); \
  52093. + else \
  52094. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  52095. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  52096. + DWC_CIRCLEQ_END(head)) \
  52097. + (head).cqh_first = (elm2); \
  52098. + else \
  52099. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  52100. +} while (0)
  52101. +
  52102. +#ifdef __cplusplus
  52103. +}
  52104. +#endif
  52105. +
  52106. +#endif /* _DWC_LIST_H_ */
  52107. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_mem.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c
  52108. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_mem.c 1970-01-01 01:00:00.000000000 +0100
  52109. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_mem.c 2014-04-13 17:33:11.000000000 +0200
  52110. @@ -0,0 +1,245 @@
  52111. +/* Memory Debugging */
  52112. +#ifdef DWC_DEBUG_MEMORY
  52113. +
  52114. +#include "dwc_os.h"
  52115. +#include "dwc_list.h"
  52116. +
  52117. +struct allocation {
  52118. + void *addr;
  52119. + void *ctx;
  52120. + char *func;
  52121. + int line;
  52122. + uint32_t size;
  52123. + int dma;
  52124. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  52125. +};
  52126. +
  52127. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  52128. +
  52129. +struct allocation_manager {
  52130. + void *mem_ctx;
  52131. + struct allocation_queue allocations;
  52132. +
  52133. + /* statistics */
  52134. + int num;
  52135. + int num_freed;
  52136. + int num_active;
  52137. + uint32_t total;
  52138. + uint32_t cur;
  52139. + uint32_t max;
  52140. +};
  52141. +
  52142. +static struct allocation_manager *manager = NULL;
  52143. +
  52144. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  52145. + int dma)
  52146. +{
  52147. + struct allocation *a;
  52148. +
  52149. + DWC_ASSERT(manager != NULL, "manager not allocated");
  52150. +
  52151. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  52152. + if (!a) {
  52153. + return -DWC_E_NO_MEMORY;
  52154. + }
  52155. +
  52156. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  52157. + if (!a->func) {
  52158. + __DWC_FREE(manager->mem_ctx, a);
  52159. + return -DWC_E_NO_MEMORY;
  52160. + }
  52161. +
  52162. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  52163. + a->addr = addr;
  52164. + a->ctx = ctx;
  52165. + a->line = line;
  52166. + a->size = size;
  52167. + a->dma = dma;
  52168. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  52169. +
  52170. + /* Update stats */
  52171. + manager->num++;
  52172. + manager->num_active++;
  52173. + manager->total += size;
  52174. + manager->cur += size;
  52175. +
  52176. + if (manager->max < manager->cur) {
  52177. + manager->max = manager->cur;
  52178. + }
  52179. +
  52180. + return 0;
  52181. +}
  52182. +
  52183. +static struct allocation *find_allocation(void *ctx, void *addr)
  52184. +{
  52185. + struct allocation *a;
  52186. +
  52187. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  52188. + if (a->ctx == ctx && a->addr == addr) {
  52189. + return a;
  52190. + }
  52191. + }
  52192. +
  52193. + return NULL;
  52194. +}
  52195. +
  52196. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  52197. +{
  52198. + struct allocation *a = find_allocation(ctx, addr);
  52199. +
  52200. + if (!a) {
  52201. + DWC_ASSERT(0,
  52202. + "Free of address %p that was never allocated or already freed %s:%d",
  52203. + addr, func, line);
  52204. + return;
  52205. + }
  52206. +
  52207. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  52208. +
  52209. + manager->num_active--;
  52210. + manager->num_freed++;
  52211. + manager->cur -= a->size;
  52212. + __DWC_FREE(manager->mem_ctx, a->func);
  52213. + __DWC_FREE(manager->mem_ctx, a);
  52214. +}
  52215. +
  52216. +int dwc_memory_debug_start(void *mem_ctx)
  52217. +{
  52218. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  52219. +
  52220. + if (manager) {
  52221. + return -DWC_E_BUSY;
  52222. + }
  52223. +
  52224. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  52225. + if (!manager) {
  52226. + return -DWC_E_NO_MEMORY;
  52227. + }
  52228. +
  52229. + DWC_CIRCLEQ_INIT(&manager->allocations);
  52230. + manager->mem_ctx = mem_ctx;
  52231. + manager->num = 0;
  52232. + manager->num_freed = 0;
  52233. + manager->num_active = 0;
  52234. + manager->total = 0;
  52235. + manager->cur = 0;
  52236. + manager->max = 0;
  52237. +
  52238. + return 0;
  52239. +}
  52240. +
  52241. +void dwc_memory_debug_stop(void)
  52242. +{
  52243. + struct allocation *a;
  52244. +
  52245. + dwc_memory_debug_report();
  52246. +
  52247. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  52248. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  52249. + free_allocation(a->ctx, a->addr, NULL, -1);
  52250. + }
  52251. +
  52252. + __DWC_FREE(manager->mem_ctx, manager);
  52253. +}
  52254. +
  52255. +void dwc_memory_debug_report(void)
  52256. +{
  52257. + struct allocation *a;
  52258. +
  52259. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  52260. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  52261. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  52262. + DWC_PRINTF("Active = %d\n", manager->num_active);
  52263. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  52264. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  52265. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  52266. + DWC_PRINTF("Unfreed allocations:\n");
  52267. +
  52268. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  52269. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  52270. + a->addr, a->size, a->func, a->line, a->dma);
  52271. + }
  52272. +}
  52273. +
  52274. +/* The replacement functions */
  52275. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  52276. +{
  52277. + void *addr = __DWC_ALLOC(mem_ctx, size);
  52278. +
  52279. + if (!addr) {
  52280. + return NULL;
  52281. + }
  52282. +
  52283. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  52284. + __DWC_FREE(mem_ctx, addr);
  52285. + return NULL;
  52286. + }
  52287. +
  52288. + return addr;
  52289. +}
  52290. +
  52291. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  52292. + int line)
  52293. +{
  52294. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  52295. +
  52296. + if (!addr) {
  52297. + return NULL;
  52298. + }
  52299. +
  52300. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  52301. + __DWC_FREE(mem_ctx, addr);
  52302. + return NULL;
  52303. + }
  52304. +
  52305. + return addr;
  52306. +}
  52307. +
  52308. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  52309. +{
  52310. + free_allocation(mem_ctx, addr, func, line);
  52311. + __DWC_FREE(mem_ctx, addr);
  52312. +}
  52313. +
  52314. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  52315. + char const *func, int line)
  52316. +{
  52317. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  52318. +
  52319. + if (!addr) {
  52320. + return NULL;
  52321. + }
  52322. +
  52323. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  52324. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  52325. + return NULL;
  52326. + }
  52327. +
  52328. + return addr;
  52329. +}
  52330. +
  52331. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  52332. + dwc_dma_t *dma_addr, char const *func, int line)
  52333. +{
  52334. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  52335. +
  52336. + if (!addr) {
  52337. + return NULL;
  52338. + }
  52339. +
  52340. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  52341. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  52342. + return NULL;
  52343. + }
  52344. +
  52345. + return addr;
  52346. +}
  52347. +
  52348. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  52349. + dwc_dma_t dma_addr, char const *func, int line)
  52350. +{
  52351. + free_allocation(dma_ctx, virt_addr, func, line);
  52352. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  52353. +}
  52354. +
  52355. +#endif /* DWC_DEBUG_MEMORY */
  52356. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_modpow.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c
  52357. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_modpow.c 1970-01-01 01:00:00.000000000 +0100
  52358. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.c 2014-09-14 19:04:13.000000000 +0200
  52359. @@ -0,0 +1,636 @@
  52360. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  52361. + *
  52362. + * PuTTY is copyright 1997-2007 Simon Tatham.
  52363. + *
  52364. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  52365. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  52366. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  52367. + * Kuhn, and CORE SDI S.A.
  52368. + *
  52369. + * Permission is hereby granted, free of charge, to any person
  52370. + * obtaining a copy of this software and associated documentation files
  52371. + * (the "Software"), to deal in the Software without restriction,
  52372. + * including without limitation the rights to use, copy, modify, merge,
  52373. + * publish, distribute, sublicense, and/or sell copies of the Software,
  52374. + * and to permit persons to whom the Software is furnished to do so,
  52375. + * subject to the following conditions:
  52376. + *
  52377. + * The above copyright notice and this permission notice shall be
  52378. + * included in all copies or substantial portions of the Software.
  52379. +
  52380. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  52381. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  52382. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  52383. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  52384. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  52385. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  52386. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  52387. + *
  52388. + */
  52389. +#ifdef DWC_CRYPTOLIB
  52390. +
  52391. +#ifndef CONFIG_MACH_IPMATE
  52392. +
  52393. +#include "dwc_modpow.h"
  52394. +
  52395. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  52396. +#define BIGNUM_TOP_BIT 0x80000000UL
  52397. +#define BIGNUM_INT_BITS 32
  52398. +
  52399. +
  52400. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  52401. +{
  52402. + void *p;
  52403. + size *= n;
  52404. + if (size == 0) size = 1;
  52405. + p = dwc_alloc(mem_ctx, size);
  52406. + return p;
  52407. +}
  52408. +
  52409. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  52410. +#define sfree dwc_free
  52411. +
  52412. +/*
  52413. + * Usage notes:
  52414. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  52415. + * subscripts, as some implementations object to this (see below).
  52416. + * * Note that none of the division methods below will cope if the
  52417. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  52418. + * to avoid this case.
  52419. + * If this condition occurs, in the case of the x86 DIV instruction,
  52420. + * an overflow exception will occur, which (according to a correspondent)
  52421. + * will manifest on Windows as something like
  52422. + * 0xC0000095: Integer overflow
  52423. + * The C variant won't give the right answer, either.
  52424. + */
  52425. +
  52426. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  52427. +
  52428. +#if defined __GNUC__ && defined __i386__
  52429. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  52430. + __asm__("div %2" : \
  52431. + "=d" (r), "=a" (q) : \
  52432. + "r" (w), "d" (hi), "a" (lo))
  52433. +#else
  52434. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  52435. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  52436. + q = n / w; \
  52437. + r = n % w; \
  52438. +} while (0)
  52439. +#endif
  52440. +
  52441. +// q = n / w;
  52442. +// r = n % w;
  52443. +
  52444. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  52445. +
  52446. +#define BIGNUM_INTERNAL
  52447. +
  52448. +static Bignum newbn(void *mem_ctx, int length)
  52449. +{
  52450. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  52451. + //if (!b)
  52452. + //abort(); /* FIXME */
  52453. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  52454. + b[0] = length;
  52455. + return b;
  52456. +}
  52457. +
  52458. +void freebn(void *mem_ctx, Bignum b)
  52459. +{
  52460. + /*
  52461. + * Burn the evidence, just in case.
  52462. + */
  52463. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  52464. + sfree(mem_ctx, b);
  52465. +}
  52466. +
  52467. +/*
  52468. + * Compute c = a * b.
  52469. + * Input is in the first len words of a and b.
  52470. + * Result is returned in the first 2*len words of c.
  52471. + */
  52472. +static void internal_mul(BignumInt *a, BignumInt *b,
  52473. + BignumInt *c, int len)
  52474. +{
  52475. + int i, j;
  52476. + BignumDblInt t;
  52477. +
  52478. + for (j = 0; j < 2 * len; j++)
  52479. + c[j] = 0;
  52480. +
  52481. + for (i = len - 1; i >= 0; i--) {
  52482. + t = 0;
  52483. + for (j = len - 1; j >= 0; j--) {
  52484. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  52485. + t += (BignumDblInt) c[i + j + 1];
  52486. + c[i + j + 1] = (BignumInt) t;
  52487. + t = t >> BIGNUM_INT_BITS;
  52488. + }
  52489. + c[i] = (BignumInt) t;
  52490. + }
  52491. +}
  52492. +
  52493. +static void internal_add_shifted(BignumInt *number,
  52494. + unsigned n, int shift)
  52495. +{
  52496. + int word = 1 + (shift / BIGNUM_INT_BITS);
  52497. + int bshift = shift % BIGNUM_INT_BITS;
  52498. + BignumDblInt addend;
  52499. +
  52500. + addend = (BignumDblInt)n << bshift;
  52501. +
  52502. + while (addend) {
  52503. + addend += number[word];
  52504. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  52505. + addend >>= BIGNUM_INT_BITS;
  52506. + word++;
  52507. + }
  52508. +}
  52509. +
  52510. +/*
  52511. + * Compute a = a % m.
  52512. + * Input in first alen words of a and first mlen words of m.
  52513. + * Output in first alen words of a
  52514. + * (of which first alen-mlen words will be zero).
  52515. + * The MSW of m MUST have its high bit set.
  52516. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  52517. + * rather than the internal bigendian format. Quotient parts are shifted
  52518. + * left by `qshift' before adding into quot.
  52519. + */
  52520. +static void internal_mod(BignumInt *a, int alen,
  52521. + BignumInt *m, int mlen,
  52522. + BignumInt *quot, int qshift)
  52523. +{
  52524. + BignumInt m0, m1;
  52525. + unsigned int h;
  52526. + int i, k;
  52527. +
  52528. + m0 = m[0];
  52529. + if (mlen > 1)
  52530. + m1 = m[1];
  52531. + else
  52532. + m1 = 0;
  52533. +
  52534. + for (i = 0; i <= alen - mlen; i++) {
  52535. + BignumDblInt t;
  52536. + unsigned int q, r, c, ai1;
  52537. +
  52538. + if (i == 0) {
  52539. + h = 0;
  52540. + } else {
  52541. + h = a[i - 1];
  52542. + a[i - 1] = 0;
  52543. + }
  52544. +
  52545. + if (i == alen - 1)
  52546. + ai1 = 0;
  52547. + else
  52548. + ai1 = a[i + 1];
  52549. +
  52550. + /* Find q = h:a[i] / m0 */
  52551. + if (h >= m0) {
  52552. + /*
  52553. + * Special case.
  52554. + *
  52555. + * To illustrate it, suppose a BignumInt is 8 bits, and
  52556. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  52557. + * our initial division will be 0xA123 / 0xA1, which
  52558. + * will give a quotient of 0x100 and a divide overflow.
  52559. + * However, the invariants in this division algorithm
  52560. + * are not violated, since the full number A1:23:... is
  52561. + * _less_ than the quotient prefix A1:B2:... and so the
  52562. + * following correction loop would have sorted it out.
  52563. + *
  52564. + * In this situation we set q to be the largest
  52565. + * quotient we _can_ stomach (0xFF, of course).
  52566. + */
  52567. + q = BIGNUM_INT_MASK;
  52568. + } else {
  52569. + /* Macro doesn't want an array subscript expression passed
  52570. + * into it (see definition), so use a temporary. */
  52571. + BignumInt tmplo = a[i];
  52572. + DIVMOD_WORD(q, r, h, tmplo, m0);
  52573. +
  52574. + /* Refine our estimate of q by looking at
  52575. + h:a[i]:a[i+1] / m0:m1 */
  52576. + t = MUL_WORD(m1, q);
  52577. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  52578. + q--;
  52579. + t -= m1;
  52580. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  52581. + if (r >= (BignumDblInt) m0 &&
  52582. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  52583. + }
  52584. + }
  52585. +
  52586. + /* Subtract q * m from a[i...] */
  52587. + c = 0;
  52588. + for (k = mlen - 1; k >= 0; k--) {
  52589. + t = MUL_WORD(q, m[k]);
  52590. + t += c;
  52591. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  52592. + if ((BignumInt) t > a[i + k])
  52593. + c++;
  52594. + a[i + k] -= (BignumInt) t;
  52595. + }
  52596. +
  52597. + /* Add back m in case of borrow */
  52598. + if (c != h) {
  52599. + t = 0;
  52600. + for (k = mlen - 1; k >= 0; k--) {
  52601. + t += m[k];
  52602. + t += a[i + k];
  52603. + a[i + k] = (BignumInt) t;
  52604. + t = t >> BIGNUM_INT_BITS;
  52605. + }
  52606. + q--;
  52607. + }
  52608. + if (quot)
  52609. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  52610. + }
  52611. +}
  52612. +
  52613. +/*
  52614. + * Compute p % mod.
  52615. + * The most significant word of mod MUST be non-zero.
  52616. + * We assume that the result array is the same size as the mod array.
  52617. + * We optionally write out a quotient if `quotient' is non-NULL.
  52618. + * We can avoid writing out the result if `result' is NULL.
  52619. + */
  52620. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  52621. +{
  52622. + BignumInt *n, *m;
  52623. + int mshift;
  52624. + int plen, mlen, i, j;
  52625. +
  52626. + /* Allocate m of size mlen, copy mod to m */
  52627. + /* We use big endian internally */
  52628. + mlen = mod[0];
  52629. + m = snewn(mem_ctx, mlen, BignumInt);
  52630. + //if (!m)
  52631. + //abort(); /* FIXME */
  52632. + for (j = 0; j < mlen; j++)
  52633. + m[j] = mod[mod[0] - j];
  52634. +
  52635. + /* Shift m left to make msb bit set */
  52636. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  52637. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  52638. + break;
  52639. + if (mshift) {
  52640. + for (i = 0; i < mlen - 1; i++)
  52641. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  52642. + m[mlen - 1] = m[mlen - 1] << mshift;
  52643. + }
  52644. +
  52645. + plen = p[0];
  52646. + /* Ensure plen > mlen */
  52647. + if (plen <= mlen)
  52648. + plen = mlen + 1;
  52649. +
  52650. + /* Allocate n of size plen, copy p to n */
  52651. + n = snewn(mem_ctx, plen, BignumInt);
  52652. + //if (!n)
  52653. + //abort(); /* FIXME */
  52654. + for (j = 0; j < plen; j++)
  52655. + n[j] = 0;
  52656. + for (j = 1; j <= (int)p[0]; j++)
  52657. + n[plen - j] = p[j];
  52658. +
  52659. + /* Main computation */
  52660. + internal_mod(n, plen, m, mlen, quotient, mshift);
  52661. +
  52662. + /* Fixup result in case the modulus was shifted */
  52663. + if (mshift) {
  52664. + for (i = plen - mlen - 1; i < plen - 1; i++)
  52665. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  52666. + n[plen - 1] = n[plen - 1] << mshift;
  52667. + internal_mod(n, plen, m, mlen, quotient, 0);
  52668. + for (i = plen - 1; i >= plen - mlen; i--)
  52669. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  52670. + }
  52671. +
  52672. + /* Copy result to buffer */
  52673. + if (result) {
  52674. + for (i = 1; i <= (int)result[0]; i++) {
  52675. + int j = plen - i;
  52676. + result[i] = j >= 0 ? n[j] : 0;
  52677. + }
  52678. + }
  52679. +
  52680. + /* Free temporary arrays */
  52681. + for (i = 0; i < mlen; i++)
  52682. + m[i] = 0;
  52683. + sfree(mem_ctx, m);
  52684. + for (i = 0; i < plen; i++)
  52685. + n[i] = 0;
  52686. + sfree(mem_ctx, n);
  52687. +}
  52688. +
  52689. +/*
  52690. + * Simple remainder.
  52691. + */
  52692. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  52693. +{
  52694. + Bignum r = newbn(mem_ctx, b[0]);
  52695. + bigdivmod(mem_ctx, a, b, r, NULL);
  52696. + return r;
  52697. +}
  52698. +
  52699. +/*
  52700. + * Compute (base ^ exp) % mod.
  52701. + */
  52702. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  52703. +{
  52704. + BignumInt *a, *b, *n, *m;
  52705. + int mshift;
  52706. + int mlen, i, j;
  52707. + Bignum base, result;
  52708. +
  52709. + /*
  52710. + * The most significant word of mod needs to be non-zero. It
  52711. + * should already be, but let's make sure.
  52712. + */
  52713. + //assert(mod[mod[0]] != 0);
  52714. +
  52715. + /*
  52716. + * Make sure the base is smaller than the modulus, by reducing
  52717. + * it modulo the modulus if not.
  52718. + */
  52719. + base = bigmod(mem_ctx, base_in, mod);
  52720. +
  52721. + /* Allocate m of size mlen, copy mod to m */
  52722. + /* We use big endian internally */
  52723. + mlen = mod[0];
  52724. + m = snewn(mem_ctx, mlen, BignumInt);
  52725. + //if (!m)
  52726. + //abort(); /* FIXME */
  52727. + for (j = 0; j < mlen; j++)
  52728. + m[j] = mod[mod[0] - j];
  52729. +
  52730. + /* Shift m left to make msb bit set */
  52731. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  52732. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  52733. + break;
  52734. + if (mshift) {
  52735. + for (i = 0; i < mlen - 1; i++)
  52736. + m[i] =
  52737. + (m[i] << mshift) | (m[i + 1] >>
  52738. + (BIGNUM_INT_BITS - mshift));
  52739. + m[mlen - 1] = m[mlen - 1] << mshift;
  52740. + }
  52741. +
  52742. + /* Allocate n of size mlen, copy base to n */
  52743. + n = snewn(mem_ctx, mlen, BignumInt);
  52744. + //if (!n)
  52745. + //abort(); /* FIXME */
  52746. + i = mlen - base[0];
  52747. + for (j = 0; j < i; j++)
  52748. + n[j] = 0;
  52749. + for (j = 0; j < base[0]; j++)
  52750. + n[i + j] = base[base[0] - j];
  52751. +
  52752. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  52753. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  52754. + //if (!a)
  52755. + //abort(); /* FIXME */
  52756. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  52757. + //if (!b)
  52758. + //abort(); /* FIXME */
  52759. + for (i = 0; i < 2 * mlen; i++)
  52760. + a[i] = 0;
  52761. + a[2 * mlen - 1] = 1;
  52762. +
  52763. + /* Skip leading zero bits of exp. */
  52764. + i = 0;
  52765. + j = BIGNUM_INT_BITS - 1;
  52766. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  52767. + j--;
  52768. + if (j < 0) {
  52769. + i++;
  52770. + j = BIGNUM_INT_BITS - 1;
  52771. + }
  52772. + }
  52773. +
  52774. + /* Main computation */
  52775. + while (i < exp[0]) {
  52776. + while (j >= 0) {
  52777. + internal_mul(a + mlen, a + mlen, b, mlen);
  52778. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  52779. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  52780. + internal_mul(b + mlen, n, a, mlen);
  52781. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  52782. + } else {
  52783. + BignumInt *t;
  52784. + t = a;
  52785. + a = b;
  52786. + b = t;
  52787. + }
  52788. + j--;
  52789. + }
  52790. + i++;
  52791. + j = BIGNUM_INT_BITS - 1;
  52792. + }
  52793. +
  52794. + /* Fixup result in case the modulus was shifted */
  52795. + if (mshift) {
  52796. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  52797. + a[i] =
  52798. + (a[i] << mshift) | (a[i + 1] >>
  52799. + (BIGNUM_INT_BITS - mshift));
  52800. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  52801. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  52802. + for (i = 2 * mlen - 1; i >= mlen; i--)
  52803. + a[i] =
  52804. + (a[i] >> mshift) | (a[i - 1] <<
  52805. + (BIGNUM_INT_BITS - mshift));
  52806. + }
  52807. +
  52808. + /* Copy result to buffer */
  52809. + result = newbn(mem_ctx, mod[0]);
  52810. + for (i = 0; i < mlen; i++)
  52811. + result[result[0] - i] = a[i + mlen];
  52812. + while (result[0] > 1 && result[result[0]] == 0)
  52813. + result[0]--;
  52814. +
  52815. + /* Free temporary arrays */
  52816. + for (i = 0; i < 2 * mlen; i++)
  52817. + a[i] = 0;
  52818. + sfree(mem_ctx, a);
  52819. + for (i = 0; i < 2 * mlen; i++)
  52820. + b[i] = 0;
  52821. + sfree(mem_ctx, b);
  52822. + for (i = 0; i < mlen; i++)
  52823. + m[i] = 0;
  52824. + sfree(mem_ctx, m);
  52825. + for (i = 0; i < mlen; i++)
  52826. + n[i] = 0;
  52827. + sfree(mem_ctx, n);
  52828. +
  52829. + freebn(mem_ctx, base);
  52830. +
  52831. + return result;
  52832. +}
  52833. +
  52834. +
  52835. +#ifdef UNITTEST
  52836. +
  52837. +static __u32 dh_p[] = {
  52838. + 96,
  52839. + 0xFFFFFFFF,
  52840. + 0xFFFFFFFF,
  52841. + 0xA93AD2CA,
  52842. + 0x4B82D120,
  52843. + 0xE0FD108E,
  52844. + 0x43DB5BFC,
  52845. + 0x74E5AB31,
  52846. + 0x08E24FA0,
  52847. + 0xBAD946E2,
  52848. + 0x770988C0,
  52849. + 0x7A615D6C,
  52850. + 0xBBE11757,
  52851. + 0x177B200C,
  52852. + 0x521F2B18,
  52853. + 0x3EC86A64,
  52854. + 0xD8760273,
  52855. + 0xD98A0864,
  52856. + 0xF12FFA06,
  52857. + 0x1AD2EE6B,
  52858. + 0xCEE3D226,
  52859. + 0x4A25619D,
  52860. + 0x1E8C94E0,
  52861. + 0xDB0933D7,
  52862. + 0xABF5AE8C,
  52863. + 0xA6E1E4C7,
  52864. + 0xB3970F85,
  52865. + 0x5D060C7D,
  52866. + 0x8AEA7157,
  52867. + 0x58DBEF0A,
  52868. + 0xECFB8504,
  52869. + 0xDF1CBA64,
  52870. + 0xA85521AB,
  52871. + 0x04507A33,
  52872. + 0xAD33170D,
  52873. + 0x8AAAC42D,
  52874. + 0x15728E5A,
  52875. + 0x98FA0510,
  52876. + 0x15D22618,
  52877. + 0xEA956AE5,
  52878. + 0x3995497C,
  52879. + 0x95581718,
  52880. + 0xDE2BCBF6,
  52881. + 0x6F4C52C9,
  52882. + 0xB5C55DF0,
  52883. + 0xEC07A28F,
  52884. + 0x9B2783A2,
  52885. + 0x180E8603,
  52886. + 0xE39E772C,
  52887. + 0x2E36CE3B,
  52888. + 0x32905E46,
  52889. + 0xCA18217C,
  52890. + 0xF1746C08,
  52891. + 0x4ABC9804,
  52892. + 0x670C354E,
  52893. + 0x7096966D,
  52894. + 0x9ED52907,
  52895. + 0x208552BB,
  52896. + 0x1C62F356,
  52897. + 0xDCA3AD96,
  52898. + 0x83655D23,
  52899. + 0xFD24CF5F,
  52900. + 0x69163FA8,
  52901. + 0x1C55D39A,
  52902. + 0x98DA4836,
  52903. + 0xA163BF05,
  52904. + 0xC2007CB8,
  52905. + 0xECE45B3D,
  52906. + 0x49286651,
  52907. + 0x7C4B1FE6,
  52908. + 0xAE9F2411,
  52909. + 0x5A899FA5,
  52910. + 0xEE386BFB,
  52911. + 0xF406B7ED,
  52912. + 0x0BFF5CB6,
  52913. + 0xA637ED6B,
  52914. + 0xF44C42E9,
  52915. + 0x625E7EC6,
  52916. + 0xE485B576,
  52917. + 0x6D51C245,
  52918. + 0x4FE1356D,
  52919. + 0xF25F1437,
  52920. + 0x302B0A6D,
  52921. + 0xCD3A431B,
  52922. + 0xEF9519B3,
  52923. + 0x8E3404DD,
  52924. + 0x514A0879,
  52925. + 0x3B139B22,
  52926. + 0x020BBEA6,
  52927. + 0x8A67CC74,
  52928. + 0x29024E08,
  52929. + 0x80DC1CD1,
  52930. + 0xC4C6628B,
  52931. + 0x2168C234,
  52932. + 0xC90FDAA2,
  52933. + 0xFFFFFFFF,
  52934. + 0xFFFFFFFF,
  52935. +};
  52936. +
  52937. +static __u32 dh_a[] = {
  52938. + 8,
  52939. + 0xdf367516,
  52940. + 0x86459caa,
  52941. + 0xe2d459a4,
  52942. + 0xd910dae0,
  52943. + 0x8a8b5e37,
  52944. + 0x67ab31c6,
  52945. + 0xf0b55ea9,
  52946. + 0x440051d6,
  52947. +};
  52948. +
  52949. +static __u32 dh_b[] = {
  52950. + 8,
  52951. + 0xded92656,
  52952. + 0xe07a048a,
  52953. + 0x6fa452cd,
  52954. + 0x2df89d30,
  52955. + 0xc75f1b0f,
  52956. + 0x8ce3578f,
  52957. + 0x7980a324,
  52958. + 0x5daec786,
  52959. +};
  52960. +
  52961. +static __u32 dh_g[] = {
  52962. + 1,
  52963. + 2,
  52964. +};
  52965. +
  52966. +int main(void)
  52967. +{
  52968. + int i;
  52969. + __u32 *k;
  52970. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  52971. +
  52972. + printf("\n\n");
  52973. + for (i=0; i<k[0]; i++) {
  52974. + __u32 word32 = k[k[0] - i];
  52975. + __u16 l = word32 & 0xffff;
  52976. + __u16 m = (word32 & 0xffff0000) >> 16;
  52977. + printf("%04x %04x ", m, l);
  52978. + if (!((i + 1)%13)) printf("\n");
  52979. + }
  52980. + printf("\n\n");
  52981. +
  52982. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  52983. + printf("PASS\n\n");
  52984. + }
  52985. + else {
  52986. + printf("FAIL\n\n");
  52987. + }
  52988. +
  52989. +}
  52990. +
  52991. +#endif /* UNITTEST */
  52992. +
  52993. +#endif /* CONFIG_MACH_IPMATE */
  52994. +
  52995. +#endif /*DWC_CRYPTOLIB */
  52996. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_modpow.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h
  52997. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_modpow.h 1970-01-01 01:00:00.000000000 +0100
  52998. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_modpow.h 2014-04-13 17:33:11.000000000 +0200
  52999. @@ -0,0 +1,34 @@
  53000. +/*
  53001. + * dwc_modpow.h
  53002. + * See dwc_modpow.c for license and changes
  53003. + */
  53004. +#ifndef _DWC_MODPOW_H
  53005. +#define _DWC_MODPOW_H
  53006. +
  53007. +#ifdef __cplusplus
  53008. +extern "C" {
  53009. +#endif
  53010. +
  53011. +#include "dwc_os.h"
  53012. +
  53013. +/** @file
  53014. + *
  53015. + * This file defines the module exponentiation function which is only used
  53016. + * internally by the DWC UWB modules for calculation of PKs during numeric
  53017. + * association. The routine is taken from the PUTTY, an open source terminal
  53018. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  53019. + *
  53020. + */
  53021. +
  53022. +typedef uint32_t BignumInt;
  53023. +typedef uint64_t BignumDblInt;
  53024. +typedef BignumInt *Bignum;
  53025. +
  53026. +/* Compute modular exponentiaion */
  53027. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  53028. +
  53029. +#ifdef __cplusplus
  53030. +}
  53031. +#endif
  53032. +
  53033. +#endif /* _LINUX_BIGNUM_H */
  53034. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_notifier.c linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c
  53035. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_notifier.c 1970-01-01 01:00:00.000000000 +0100
  53036. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.c 2014-04-13 17:33:11.000000000 +0200
  53037. @@ -0,0 +1,319 @@
  53038. +#ifdef DWC_NOTIFYLIB
  53039. +
  53040. +#include "dwc_notifier.h"
  53041. +#include "dwc_list.h"
  53042. +
  53043. +typedef struct dwc_observer {
  53044. + void *observer;
  53045. + dwc_notifier_callback_t callback;
  53046. + void *data;
  53047. + char *notification;
  53048. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  53049. +} observer_t;
  53050. +
  53051. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  53052. +
  53053. +typedef struct dwc_notifier {
  53054. + void *mem_ctx;
  53055. + void *object;
  53056. + struct observer_queue observers;
  53057. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  53058. +} notifier_t;
  53059. +
  53060. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  53061. +
  53062. +typedef struct manager {
  53063. + void *mem_ctx;
  53064. + void *wkq_ctx;
  53065. + dwc_workq_t *wq;
  53066. +// dwc_mutex_t *mutex;
  53067. + struct notifier_queue notifiers;
  53068. +} manager_t;
  53069. +
  53070. +static manager_t *manager = NULL;
  53071. +
  53072. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  53073. +{
  53074. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  53075. + if (!manager) {
  53076. + return -DWC_E_NO_MEMORY;
  53077. + }
  53078. +
  53079. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  53080. +
  53081. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  53082. + if (!manager->wq) {
  53083. + return -DWC_E_NO_MEMORY;
  53084. + }
  53085. +
  53086. + return 0;
  53087. +}
  53088. +
  53089. +static void free_manager(void)
  53090. +{
  53091. + dwc_workq_free(manager->wq);
  53092. +
  53093. + /* All notifiers must have unregistered themselves before this module
  53094. + * can be removed. Hitting this assertion indicates a programmer
  53095. + * error. */
  53096. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  53097. + "Notification manager being freed before all notifiers have been removed");
  53098. + dwc_free(manager->mem_ctx, manager);
  53099. +}
  53100. +
  53101. +#ifdef DEBUG
  53102. +static void dump_manager(void)
  53103. +{
  53104. + notifier_t *n;
  53105. + observer_t *o;
  53106. +
  53107. + DWC_ASSERT(manager, "Notification manager not found");
  53108. +
  53109. + DWC_DEBUG("List of all notifiers and observers:\n");
  53110. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  53111. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  53112. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  53113. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  53114. + }
  53115. + }
  53116. +}
  53117. +#else
  53118. +#define dump_manager(...)
  53119. +#endif
  53120. +
  53121. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  53122. + dwc_notifier_callback_t callback, void *data)
  53123. +{
  53124. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  53125. +
  53126. + if (!new_observer) {
  53127. + return NULL;
  53128. + }
  53129. +
  53130. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  53131. + new_observer->observer = observer;
  53132. + new_observer->notification = notification;
  53133. + new_observer->callback = callback;
  53134. + new_observer->data = data;
  53135. + return new_observer;
  53136. +}
  53137. +
  53138. +static void free_observer(void *mem_ctx, observer_t *observer)
  53139. +{
  53140. + dwc_free(mem_ctx, observer);
  53141. +}
  53142. +
  53143. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  53144. +{
  53145. + notifier_t *notifier;
  53146. +
  53147. + if (!object) {
  53148. + return NULL;
  53149. + }
  53150. +
  53151. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  53152. + if (!notifier) {
  53153. + return NULL;
  53154. + }
  53155. +
  53156. + DWC_CIRCLEQ_INIT(&notifier->observers);
  53157. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  53158. +
  53159. + notifier->mem_ctx = mem_ctx;
  53160. + notifier->object = object;
  53161. + return notifier;
  53162. +}
  53163. +
  53164. +static void free_notifier(notifier_t *notifier)
  53165. +{
  53166. + observer_t *observer;
  53167. +
  53168. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  53169. + free_observer(notifier->mem_ctx, observer);
  53170. + }
  53171. +
  53172. + dwc_free(notifier->mem_ctx, notifier);
  53173. +}
  53174. +
  53175. +static notifier_t *find_notifier(void *object)
  53176. +{
  53177. + notifier_t *notifier;
  53178. +
  53179. + DWC_ASSERT(manager, "Notification manager not found");
  53180. +
  53181. + if (!object) {
  53182. + return NULL;
  53183. + }
  53184. +
  53185. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  53186. + if (notifier->object == object) {
  53187. + return notifier;
  53188. + }
  53189. + }
  53190. +
  53191. + return NULL;
  53192. +}
  53193. +
  53194. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  53195. +{
  53196. + return create_manager(mem_ctx, wkq_ctx);
  53197. +}
  53198. +
  53199. +void dwc_free_notification_manager(void)
  53200. +{
  53201. + free_manager();
  53202. +}
  53203. +
  53204. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  53205. +{
  53206. + notifier_t *notifier;
  53207. +
  53208. + DWC_ASSERT(manager, "Notification manager not found");
  53209. +
  53210. + notifier = find_notifier(object);
  53211. + if (notifier) {
  53212. + DWC_ERROR("Notifier %p is already registered\n", object);
  53213. + return NULL;
  53214. + }
  53215. +
  53216. + notifier = alloc_notifier(mem_ctx, object);
  53217. + if (!notifier) {
  53218. + return NULL;
  53219. + }
  53220. +
  53221. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  53222. +
  53223. + DWC_INFO("Notifier %p registered", object);
  53224. + dump_manager();
  53225. +
  53226. + return notifier;
  53227. +}
  53228. +
  53229. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  53230. +{
  53231. + DWC_ASSERT(manager, "Notification manager not found");
  53232. +
  53233. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  53234. + observer_t *o;
  53235. +
  53236. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  53237. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  53238. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  53239. + }
  53240. +
  53241. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  53242. + "Notifier %p has active observers when removing", notifier);
  53243. + }
  53244. +
  53245. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  53246. + free_notifier(notifier);
  53247. +
  53248. + DWC_INFO("Notifier unregistered");
  53249. + dump_manager();
  53250. +}
  53251. +
  53252. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  53253. +int dwc_add_observer(void *observer, void *object, char *notification,
  53254. + dwc_notifier_callback_t callback, void *data)
  53255. +{
  53256. + notifier_t *notifier = find_notifier(object);
  53257. + observer_t *new_observer;
  53258. +
  53259. + if (!notifier) {
  53260. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  53261. + return -DWC_E_INVALID;
  53262. + }
  53263. +
  53264. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  53265. + if (!new_observer) {
  53266. + return -DWC_E_NO_MEMORY;
  53267. + }
  53268. +
  53269. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  53270. +
  53271. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  53272. + observer, object, notification, callback, data);
  53273. +
  53274. + dump_manager();
  53275. + return 0;
  53276. +}
  53277. +
  53278. +int dwc_remove_observer(void *observer)
  53279. +{
  53280. + notifier_t *n;
  53281. +
  53282. + DWC_ASSERT(manager, "Notification manager not found");
  53283. +
  53284. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  53285. + observer_t *o;
  53286. + observer_t *o2;
  53287. +
  53288. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  53289. + if (o->observer == observer) {
  53290. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  53291. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  53292. + o->observer, n->object, o->notification);
  53293. + free_observer(n->mem_ctx, o);
  53294. + }
  53295. + }
  53296. + }
  53297. +
  53298. + dump_manager();
  53299. + return 0;
  53300. +}
  53301. +
  53302. +typedef struct callback_data {
  53303. + void *mem_ctx;
  53304. + dwc_notifier_callback_t cb;
  53305. + void *observer;
  53306. + void *data;
  53307. + void *object;
  53308. + char *notification;
  53309. + void *notification_data;
  53310. +} cb_data_t;
  53311. +
  53312. +static void cb_task(void *data)
  53313. +{
  53314. + cb_data_t *cb = (cb_data_t *)data;
  53315. +
  53316. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  53317. + dwc_free(cb->mem_ctx, cb);
  53318. +}
  53319. +
  53320. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  53321. +{
  53322. + observer_t *o;
  53323. +
  53324. + DWC_ASSERT(manager, "Notification manager not found");
  53325. +
  53326. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  53327. + int len = DWC_STRLEN(notification);
  53328. +
  53329. + if (DWC_STRLEN(o->notification) != len) {
  53330. + continue;
  53331. + }
  53332. +
  53333. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  53334. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  53335. +
  53336. + if (!cb_data) {
  53337. + DWC_ERROR("Failed to allocate callback data\n");
  53338. + return;
  53339. + }
  53340. +
  53341. + cb_data->mem_ctx = notifier->mem_ctx;
  53342. + cb_data->cb = o->callback;
  53343. + cb_data->observer = o->observer;
  53344. + cb_data->data = o->data;
  53345. + cb_data->object = notifier->object;
  53346. + cb_data->notification = notification;
  53347. + cb_data->notification_data = notification_data;
  53348. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  53349. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  53350. + "Notify callback from %p for Notification %s, to observer %p",
  53351. + cb_data->object, notification, cb_data->observer);
  53352. + }
  53353. + }
  53354. +}
  53355. +
  53356. +#endif /* DWC_NOTIFYLIB */
  53357. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_notifier.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h
  53358. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_notifier.h 1970-01-01 01:00:00.000000000 +0100
  53359. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_notifier.h 2014-04-13 17:33:11.000000000 +0200
  53360. @@ -0,0 +1,122 @@
  53361. +
  53362. +#ifndef __DWC_NOTIFIER_H__
  53363. +#define __DWC_NOTIFIER_H__
  53364. +
  53365. +#ifdef __cplusplus
  53366. +extern "C" {
  53367. +#endif
  53368. +
  53369. +#include "dwc_os.h"
  53370. +
  53371. +/** @file
  53372. + *
  53373. + * A simple implementation of the Observer pattern. Any "module" can
  53374. + * register as an observer or notifier. The notion of "module" is abstract and
  53375. + * can mean anything used to identify either an observer or notifier. Usually
  53376. + * it will be a pointer to a data structure which contains some state, ie an
  53377. + * object.
  53378. + *
  53379. + * Before any notifiers can be added, the global notification manager must be
  53380. + * brought up with dwc_alloc_notification_manager().
  53381. + * dwc_free_notification_manager() will bring it down and free all resources.
  53382. + * These would typically be called upon module load and unload. The
  53383. + * notification manager is a single global instance that handles all registered
  53384. + * observable modules and observers so this should be done only once.
  53385. + *
  53386. + * A module can be observable by using Notifications to publicize some general
  53387. + * information about it's state or operation. It does not care who listens, or
  53388. + * even if anyone listens, or what they do with the information. The observable
  53389. + * modules do not need to know any information about it's observers or their
  53390. + * interface, or their state or data.
  53391. + *
  53392. + * Any module can register to emit Notifications. It should publish a list of
  53393. + * notifications that it can emit and their behavior, such as when they will get
  53394. + * triggered, and what information will be provided to the observer. Then it
  53395. + * should register itself as an observable module. See dwc_register_notifier().
  53396. + *
  53397. + * Any module can observe any observable, registered module, provided it has a
  53398. + * handle to the other module and knows what notifications to observe. See
  53399. + * dwc_add_observer().
  53400. + *
  53401. + * A function of type dwc_notifier_callback_t is called whenever a notification
  53402. + * is triggered with one or more observers observing it. This function is
  53403. + * called in it's own process so it may sleep or block if needed. It is
  53404. + * guaranteed to be called sometime after the notification has occurred and will
  53405. + * be called once per each time the notification is triggered. It will NOT be
  53406. + * called in the same process context used to trigger the notification.
  53407. + *
  53408. + * @section Limitiations
  53409. + *
  53410. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  53411. + * schedule too many processes too handle. Be aware of this limitation when
  53412. + * designing to use notifications, and only add notifications for appropriate
  53413. + * observable information.
  53414. + *
  53415. + * Also Notification callbacks are not synchronous. If you need to synchronize
  53416. + * the behavior between module/observer you must use other means. And perhaps
  53417. + * that will mean Notifications are not the proper solution.
  53418. + */
  53419. +
  53420. +struct dwc_notifier;
  53421. +typedef struct dwc_notifier dwc_notifier_t;
  53422. +
  53423. +/** The callback function must be of this type.
  53424. + *
  53425. + * @param object This is the object that is being observed.
  53426. + * @param notification This is the notification that was triggered.
  53427. + * @param observer This is the observer
  53428. + * @param notification_data This is notification-specific data that the notifier
  53429. + * has included in this notification. The value of this should be published in
  53430. + * the documentation of the observable module with the notifications.
  53431. + * @param user_data This is any custom data that the observer provided when
  53432. + * adding itself as an observer to the notification. */
  53433. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  53434. + void *notification_data, void *user_data);
  53435. +
  53436. +/** Brings up the notification manager. */
  53437. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  53438. +/** Brings down the notification manager. */
  53439. +extern void dwc_free_notification_manager(void);
  53440. +
  53441. +/** This function registers an observable module. A dwc_notifier_t object is
  53442. + * returned to the observable module. This is an opaque object that is used by
  53443. + * the observable module to trigger notifications. This object should only be
  53444. + * accessible to functions that are authorized to trigger notifications for this
  53445. + * module. Observers do not need this object. */
  53446. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  53447. +
  53448. +/** This function unregisters an observable module. All observers have to be
  53449. + * removed prior to unregistration. */
  53450. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  53451. +
  53452. +/** Add a module as an observer to the observable module. The observable module
  53453. + * needs to have previously registered with the notification manager.
  53454. + *
  53455. + * @param observer The observer module
  53456. + * @param object The module to observe
  53457. + * @param notification The notification to observe
  53458. + * @param callback The callback function to call
  53459. + * @param user_data Any additional user data to pass into the callback function */
  53460. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  53461. + dwc_notifier_callback_t callback, void *user_data);
  53462. +
  53463. +/** Removes the specified observer from all notifications that it is currently
  53464. + * observing. */
  53465. +extern int dwc_remove_observer(void *observer);
  53466. +
  53467. +/** This function triggers a Notification. It should be called by the
  53468. + * observable module, or any module or library which the observable module
  53469. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  53470. + *
  53471. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  53472. + * their own process context for each trigger. Callbacks can be blocking.
  53473. + * dwc_notify can be called from interrupt context if needed.
  53474. + *
  53475. + */
  53476. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  53477. +
  53478. +#ifdef __cplusplus
  53479. +}
  53480. +#endif
  53481. +
  53482. +#endif /* __DWC_NOTIFIER_H__ */
  53483. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_os.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_os.h
  53484. --- linux-3.16.2/drivers/usb/host/dwc_common_port/dwc_os.h 1970-01-01 01:00:00.000000000 +0100
  53485. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/dwc_os.h 2014-09-14 19:04:13.000000000 +0200
  53486. @@ -0,0 +1,1276 @@
  53487. +/* =========================================================================
  53488. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  53489. + * $Revision: #14 $
  53490. + * $Date: 2010/11/04 $
  53491. + * $Change: 1621695 $
  53492. + *
  53493. + * Synopsys Portability Library Software and documentation
  53494. + * (hereinafter, "Software") is an Unsupported proprietary work of
  53495. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  53496. + * between Synopsys and you.
  53497. + *
  53498. + * The Software IS NOT an item of Licensed Software or Licensed Product
  53499. + * under any End User Software License Agreement or Agreement for
  53500. + * Licensed Product with Synopsys or any supplement thereto. You are
  53501. + * permitted to use and redistribute this Software in source and binary
  53502. + * forms, with or without modification, provided that redistributions
  53503. + * of source code must retain this notice. You may not view, use,
  53504. + * disclose, copy or distribute this file or any information contained
  53505. + * herein except pursuant to this license grant from Synopsys. If you
  53506. + * do not agree with this notice, including the disclaimer below, then
  53507. + * you are not authorized to use the Software.
  53508. + *
  53509. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  53510. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53511. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  53512. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  53513. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  53514. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  53515. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  53516. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  53517. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  53518. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  53519. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  53520. + * DAMAGE.
  53521. + * ========================================================================= */
  53522. +#ifndef _DWC_OS_H_
  53523. +#define _DWC_OS_H_
  53524. +
  53525. +#ifdef __cplusplus
  53526. +extern "C" {
  53527. +#endif
  53528. +
  53529. +/** @file
  53530. + *
  53531. + * DWC portability library, low level os-wrapper functions
  53532. + *
  53533. + */
  53534. +
  53535. +/* These basic types need to be defined by some OS header file or custom header
  53536. + * file for your specific target architecture.
  53537. + *
  53538. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  53539. + *
  53540. + * Any custom or alternate header file must be added and enabled here.
  53541. + */
  53542. +
  53543. +#ifdef DWC_LINUX
  53544. +# include <linux/types.h>
  53545. +# ifdef CONFIG_DEBUG_MUTEXES
  53546. +# include <linux/mutex.h>
  53547. +# endif
  53548. +# include <linux/spinlock.h>
  53549. +# include <linux/errno.h>
  53550. +# include <stdarg.h>
  53551. +#endif
  53552. +
  53553. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53554. +# include <os_dep.h>
  53555. +#endif
  53556. +
  53557. +
  53558. +/** @name Primitive Types and Values */
  53559. +
  53560. +/** We define a boolean type for consistency. Can be either YES or NO */
  53561. +typedef uint8_t dwc_bool_t;
  53562. +#define YES 1
  53563. +#define NO 0
  53564. +
  53565. +#ifdef DWC_LINUX
  53566. +
  53567. +/** @name Error Codes */
  53568. +#define DWC_E_INVALID EINVAL
  53569. +#define DWC_E_NO_MEMORY ENOMEM
  53570. +#define DWC_E_NO_DEVICE ENODEV
  53571. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  53572. +#define DWC_E_TIMEOUT ETIMEDOUT
  53573. +#define DWC_E_BUSY EBUSY
  53574. +#define DWC_E_AGAIN EAGAIN
  53575. +#define DWC_E_RESTART ERESTART
  53576. +#define DWC_E_ABORT ECONNABORTED
  53577. +#define DWC_E_SHUTDOWN ESHUTDOWN
  53578. +#define DWC_E_NO_DATA ENODATA
  53579. +#define DWC_E_DISCONNECT ECONNRESET
  53580. +#define DWC_E_UNKNOWN EINVAL
  53581. +#define DWC_E_NO_STREAM_RES ENOSR
  53582. +#define DWC_E_COMMUNICATION ECOMM
  53583. +#define DWC_E_OVERFLOW EOVERFLOW
  53584. +#define DWC_E_PROTOCOL EPROTO
  53585. +#define DWC_E_IN_PROGRESS EINPROGRESS
  53586. +#define DWC_E_PIPE EPIPE
  53587. +#define DWC_E_IO EIO
  53588. +#define DWC_E_NO_SPACE ENOSPC
  53589. +
  53590. +#else
  53591. +
  53592. +/** @name Error Codes */
  53593. +#define DWC_E_INVALID 1001
  53594. +#define DWC_E_NO_MEMORY 1002
  53595. +#define DWC_E_NO_DEVICE 1003
  53596. +#define DWC_E_NOT_SUPPORTED 1004
  53597. +#define DWC_E_TIMEOUT 1005
  53598. +#define DWC_E_BUSY 1006
  53599. +#define DWC_E_AGAIN 1007
  53600. +#define DWC_E_RESTART 1008
  53601. +#define DWC_E_ABORT 1009
  53602. +#define DWC_E_SHUTDOWN 1010
  53603. +#define DWC_E_NO_DATA 1011
  53604. +#define DWC_E_DISCONNECT 2000
  53605. +#define DWC_E_UNKNOWN 3000
  53606. +#define DWC_E_NO_STREAM_RES 4001
  53607. +#define DWC_E_COMMUNICATION 4002
  53608. +#define DWC_E_OVERFLOW 4003
  53609. +#define DWC_E_PROTOCOL 4004
  53610. +#define DWC_E_IN_PROGRESS 4005
  53611. +#define DWC_E_PIPE 4006
  53612. +#define DWC_E_IO 4007
  53613. +#define DWC_E_NO_SPACE 4008
  53614. +
  53615. +#endif
  53616. +
  53617. +
  53618. +/** @name Tracing/Logging Functions
  53619. + *
  53620. + * These function provide the capability to add tracing, debugging, and error
  53621. + * messages, as well exceptions as assertions. The WUDEV uses these
  53622. + * extensively. These could be logged to the main console, the serial port, an
  53623. + * internal buffer, etc. These functions could also be no-op if they are too
  53624. + * expensive on your system. By default undefining the DEBUG macro already
  53625. + * no-ops some of these functions. */
  53626. +
  53627. +/** Returns non-zero if in interrupt context. */
  53628. +extern dwc_bool_t DWC_IN_IRQ(void);
  53629. +#define dwc_in_irq DWC_IN_IRQ
  53630. +
  53631. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  53632. +static inline char *dwc_irq(void) {
  53633. + return DWC_IN_IRQ() ? "IRQ" : "";
  53634. +}
  53635. +
  53636. +/** Returns non-zero if in bottom-half context. */
  53637. +extern dwc_bool_t DWC_IN_BH(void);
  53638. +#define dwc_in_bh DWC_IN_BH
  53639. +
  53640. +/** Returns "BH" if DWC_IN_BH is true. */
  53641. +static inline char *dwc_bh(void) {
  53642. + return DWC_IN_BH() ? "BH" : "";
  53643. +}
  53644. +
  53645. +/**
  53646. + * A vprintf() clone. Just call vprintf if you've got it.
  53647. + */
  53648. +extern void DWC_VPRINTF(char *format, va_list args);
  53649. +#define dwc_vprintf DWC_VPRINTF
  53650. +
  53651. +/**
  53652. + * A vsnprintf() clone. Just call vprintf if you've got it.
  53653. + */
  53654. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  53655. +#define dwc_vsnprintf DWC_VSNPRINTF
  53656. +
  53657. +/**
  53658. + * printf() clone. Just call printf if you've go it.
  53659. + */
  53660. +extern void DWC_PRINTF(char *format, ...)
  53661. +/* This provides compiler level static checking of the parameters if you're
  53662. + * using GCC. */
  53663. +#ifdef __GNUC__
  53664. + __attribute__ ((format(printf, 1, 2)));
  53665. +#else
  53666. + ;
  53667. +#endif
  53668. +#define dwc_printf DWC_PRINTF
  53669. +
  53670. +/**
  53671. + * sprintf() clone. Just call sprintf if you've got it.
  53672. + */
  53673. +extern int DWC_SPRINTF(char *string, char *format, ...)
  53674. +#ifdef __GNUC__
  53675. + __attribute__ ((format(printf, 2, 3)));
  53676. +#else
  53677. + ;
  53678. +#endif
  53679. +#define dwc_sprintf DWC_SPRINTF
  53680. +
  53681. +/**
  53682. + * snprintf() clone. Just call snprintf if you've got it.
  53683. + */
  53684. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  53685. +#ifdef __GNUC__
  53686. + __attribute__ ((format(printf, 3, 4)));
  53687. +#else
  53688. + ;
  53689. +#endif
  53690. +#define dwc_snprintf DWC_SNPRINTF
  53691. +
  53692. +/**
  53693. + * Prints a WARNING message. On systems that don't differentiate between
  53694. + * warnings and regular log messages, just print it. Indicates that something
  53695. + * may be wrong with the driver. Works like printf().
  53696. + *
  53697. + * Use the DWC_WARN macro to call this function.
  53698. + */
  53699. +extern void __DWC_WARN(char *format, ...)
  53700. +#ifdef __GNUC__
  53701. + __attribute__ ((format(printf, 1, 2)));
  53702. +#else
  53703. + ;
  53704. +#endif
  53705. +
  53706. +/**
  53707. + * Prints an error message. On systems that don't differentiate between errors
  53708. + * and regular log messages, just print it. Indicates that something went wrong
  53709. + * with the driver. Works like printf().
  53710. + *
  53711. + * Use the DWC_ERROR macro to call this function.
  53712. + */
  53713. +extern void __DWC_ERROR(char *format, ...)
  53714. +#ifdef __GNUC__
  53715. + __attribute__ ((format(printf, 1, 2)));
  53716. +#else
  53717. + ;
  53718. +#endif
  53719. +
  53720. +/**
  53721. + * Prints an exception error message and takes some user-defined action such as
  53722. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  53723. + * abnormally wrong with the driver such as programmer error, or other
  53724. + * exceptional condition. It should not be ignored so even on systems without
  53725. + * printing capability, some action should be taken to notify the developer of
  53726. + * it. Works like printf().
  53727. + */
  53728. +extern void DWC_EXCEPTION(char *format, ...)
  53729. +#ifdef __GNUC__
  53730. + __attribute__ ((format(printf, 1, 2)));
  53731. +#else
  53732. + ;
  53733. +#endif
  53734. +#define dwc_exception DWC_EXCEPTION
  53735. +
  53736. +#ifndef DWC_OTG_DEBUG_LEV
  53737. +#define DWC_OTG_DEBUG_LEV 0
  53738. +#endif
  53739. +
  53740. +#ifdef DEBUG
  53741. +/**
  53742. + * Prints out a debug message. Used for logging/trace messages.
  53743. + *
  53744. + * Use the DWC_DEBUG macro to call this function
  53745. + */
  53746. +extern void __DWC_DEBUG(char *format, ...)
  53747. +#ifdef __GNUC__
  53748. + __attribute__ ((format(printf, 1, 2)));
  53749. +#else
  53750. + ;
  53751. +#endif
  53752. +#else
  53753. +#define __DWC_DEBUG printk
  53754. +#endif
  53755. +
  53756. +/**
  53757. + * Prints out a Debug message.
  53758. + */
  53759. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  53760. + __func__, dwc_irq(), ## _args)
  53761. +#define dwc_debug DWC_DEBUG
  53762. +/**
  53763. + * Prints out a Debug message if enabled at compile time.
  53764. + */
  53765. +#if DWC_OTG_DEBUG_LEV > 0
  53766. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  53767. +#else
  53768. +#define DWC_DEBUGC(_format, _args...)
  53769. +#endif
  53770. +#define dwc_debugc DWC_DEBUGC
  53771. +/**
  53772. + * Prints out an informative message.
  53773. + */
  53774. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  53775. + dwc_irq(), ## _args)
  53776. +#define dwc_info DWC_INFO
  53777. +/**
  53778. + * Prints out an informative message if enabled at compile time.
  53779. + */
  53780. +#if DWC_OTG_DEBUG_LEV > 1
  53781. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  53782. +#else
  53783. +#define DWC_INFOC(_format, _args...)
  53784. +#endif
  53785. +#define dwc_infoc DWC_INFOC
  53786. +/**
  53787. + * Prints out a warning message.
  53788. + */
  53789. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  53790. + dwc_irq(), __func__, __LINE__, ## _args)
  53791. +#define dwc_warn DWC_WARN
  53792. +/**
  53793. + * Prints out an error message.
  53794. + */
  53795. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  53796. + dwc_irq(), __func__, __LINE__, ## _args)
  53797. +#define dwc_error DWC_ERROR
  53798. +
  53799. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  53800. + dwc_irq(), __func__, __LINE__, ## _args)
  53801. +#define dwc_proto_error DWC_PROTO_ERROR
  53802. +
  53803. +#ifdef DEBUG
  53804. +/** Prints out a exception error message if the _expr expression fails. Disabled
  53805. + * if DEBUG is not enabled. */
  53806. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  53807. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  53808. + __FILE__, __LINE__, ## _args); } \
  53809. + } while (0)
  53810. +#else
  53811. +#define DWC_ASSERT(_x...)
  53812. +#endif
  53813. +#define dwc_assert DWC_ASSERT
  53814. +
  53815. +
  53816. +/** @name Byte Ordering
  53817. + * The following functions are for conversions between processor's byte ordering
  53818. + * and specific ordering you want.
  53819. + */
  53820. +
  53821. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  53822. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  53823. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  53824. +
  53825. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  53826. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  53827. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  53828. +
  53829. +/** Converts 32 bit little endian data to CPU byte ordering. */
  53830. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  53831. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  53832. +
  53833. +/** Converts 32 bit big endian data to CPU byte ordering. */
  53834. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  53835. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  53836. +
  53837. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  53838. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  53839. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  53840. +
  53841. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  53842. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  53843. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  53844. +
  53845. +/** Converts 16 bit little endian data to CPU byte ordering. */
  53846. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  53847. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  53848. +
  53849. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  53850. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  53851. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  53852. +
  53853. +
  53854. +/** @name Register Read/Write
  53855. + *
  53856. + * The following six functions should be implemented to read/write registers of
  53857. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  53858. + * The reg value is a pointer to the register calculated from the void *base
  53859. + * variable passed into the driver when it is started. */
  53860. +
  53861. +#ifdef DWC_LINUX
  53862. +/* Linux doesn't need any extra parameters for register read/write, so we
  53863. + * just throw away the IO context parameter.
  53864. + */
  53865. +/** Reads the content of a 32-bit register. */
  53866. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  53867. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  53868. +
  53869. +/** Reads the content of a 64-bit register. */
  53870. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  53871. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  53872. +
  53873. +/** Writes to a 32-bit register. */
  53874. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  53875. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  53876. +
  53877. +/** Writes to a 64-bit register. */
  53878. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  53879. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  53880. +
  53881. +/**
  53882. + * Modify bit values in a register. Using the
  53883. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  53884. + */
  53885. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  53886. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  53887. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  53888. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  53889. +
  53890. +#endif /* DWC_LINUX */
  53891. +
  53892. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53893. +typedef struct dwc_ioctx {
  53894. + struct device *dev;
  53895. + bus_space_tag_t iot;
  53896. + bus_space_handle_t ioh;
  53897. +} dwc_ioctx_t;
  53898. +
  53899. +/** BSD needs two extra parameters for register read/write, so we pass
  53900. + * them in using the IO context parameter.
  53901. + */
  53902. +/** Reads the content of a 32-bit register. */
  53903. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  53904. +#define dwc_read_reg32 DWC_READ_REG32
  53905. +
  53906. +/** Reads the content of a 64-bit register. */
  53907. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  53908. +#define dwc_read_reg64 DWC_READ_REG64
  53909. +
  53910. +/** Writes to a 32-bit register. */
  53911. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  53912. +#define dwc_write_reg32 DWC_WRITE_REG32
  53913. +
  53914. +/** Writes to a 64-bit register. */
  53915. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  53916. +#define dwc_write_reg64 DWC_WRITE_REG64
  53917. +
  53918. +/**
  53919. + * Modify bit values in a register. Using the
  53920. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  53921. + */
  53922. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  53923. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  53924. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  53925. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  53926. +
  53927. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  53928. +
  53929. +/** @cond */
  53930. +
  53931. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  53932. + * register writes. */
  53933. +
  53934. +#ifdef DWC_LINUX
  53935. +
  53936. +# ifdef DWC_DEBUG_REGS
  53937. +
  53938. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  53939. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  53940. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  53941. +} \
  53942. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  53943. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  53944. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  53945. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  53946. +}
  53947. +
  53948. +#define dwc_define_read_write_reg(_reg,_container_type) \
  53949. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  53950. + return DWC_READ_REG32(&container->regs->_reg); \
  53951. +} \
  53952. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  53953. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  53954. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  53955. +}
  53956. +
  53957. +# else /* DWC_DEBUG_REGS */
  53958. +
  53959. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  53960. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  53961. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  53962. +} \
  53963. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  53964. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  53965. +}
  53966. +
  53967. +#define dwc_define_read_write_reg(_reg,_container_type) \
  53968. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  53969. + return DWC_READ_REG32(&container->regs->_reg); \
  53970. +} \
  53971. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  53972. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  53973. +}
  53974. +
  53975. +# endif /* DWC_DEBUG_REGS */
  53976. +
  53977. +#endif /* DWC_LINUX */
  53978. +
  53979. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  53980. +
  53981. +# ifdef DWC_DEBUG_REGS
  53982. +
  53983. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  53984. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  53985. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  53986. +} \
  53987. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  53988. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  53989. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  53990. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  53991. +}
  53992. +
  53993. +#define dwc_define_read_write_reg(_reg,_container_type) \
  53994. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  53995. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  53996. +} \
  53997. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  53998. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  53999. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  54000. +}
  54001. +
  54002. +# else /* DWC_DEBUG_REGS */
  54003. +
  54004. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  54005. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  54006. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  54007. +} \
  54008. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  54009. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  54010. +}
  54011. +
  54012. +#define dwc_define_read_write_reg(_reg,_container_type) \
  54013. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  54014. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  54015. +} \
  54016. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  54017. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  54018. +}
  54019. +
  54020. +# endif /* DWC_DEBUG_REGS */
  54021. +
  54022. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  54023. +
  54024. +/** @endcond */
  54025. +
  54026. +
  54027. +#ifdef DWC_CRYPTOLIB
  54028. +/** @name Crypto Functions
  54029. + *
  54030. + * These are the low-level cryptographic functions used by the driver. */
  54031. +
  54032. +/** Perform AES CBC */
  54033. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  54034. +#define dwc_aes_cbc DWC_AES_CBC
  54035. +
  54036. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  54037. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  54038. +#define dwc_random_bytes DWC_RANDOM_BYTES
  54039. +
  54040. +/** Perform the SHA-256 hash function */
  54041. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  54042. +#define dwc_sha256 DWC_SHA256
  54043. +
  54044. +/** Calculated the HMAC-SHA256 */
  54045. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  54046. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  54047. +
  54048. +#endif /* DWC_CRYPTOLIB */
  54049. +
  54050. +
  54051. +/** @name Memory Allocation
  54052. + *
  54053. + * These function provide access to memory allocation. There are only 2 DMA
  54054. + * functions and 3 Regular memory functions that need to be implemented. None
  54055. + * of the memory debugging routines need to be implemented. The allocation
  54056. + * routines all ZERO the contents of the memory.
  54057. + *
  54058. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  54059. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  54060. + * keeps track of how much memory the driver is using at any given time. */
  54061. +
  54062. +#define DWC_PAGE_SIZE 4096
  54063. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  54064. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  54065. +
  54066. +#define DWC_INVALID_DMA_ADDR 0x0
  54067. +
  54068. +#ifdef DWC_LINUX
  54069. +/** Type for a DMA address */
  54070. +typedef dma_addr_t dwc_dma_t;
  54071. +#endif
  54072. +
  54073. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  54074. +typedef bus_addr_t dwc_dma_t;
  54075. +#endif
  54076. +
  54077. +#ifdef DWC_FREEBSD
  54078. +typedef struct dwc_dmactx {
  54079. + struct device *dev;
  54080. + bus_dma_tag_t dma_tag;
  54081. + bus_dmamap_t dma_map;
  54082. + bus_addr_t dma_paddr;
  54083. + void *dma_vaddr;
  54084. +} dwc_dmactx_t;
  54085. +#endif
  54086. +
  54087. +#ifdef DWC_NETBSD
  54088. +typedef struct dwc_dmactx {
  54089. + struct device *dev;
  54090. + bus_dma_tag_t dma_tag;
  54091. + bus_dmamap_t dma_map;
  54092. + bus_dma_segment_t segs[1];
  54093. + int nsegs;
  54094. + bus_addr_t dma_paddr;
  54095. + void *dma_vaddr;
  54096. +} dwc_dmactx_t;
  54097. +#endif
  54098. +
  54099. +/* @todo these functions will be added in the future */
  54100. +#if 0
  54101. +/**
  54102. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  54103. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  54104. + * boundary requirements specified.
  54105. + *
  54106. + * @param[in] size Specifies the size of the buffers that will be allocated from
  54107. + * this pool.
  54108. + * @param[in] align Specifies the byte alignment requirements of the buffers
  54109. + * allocated from this pool. Must be a power of 2.
  54110. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  54111. + * this pool must not cross.
  54112. + *
  54113. + * @returns A pointer to an internal opaque structure which is not to be
  54114. + * accessed outside of these library functions. Use this handle to specify
  54115. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  54116. + * when you are done with it.
  54117. + */
  54118. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  54119. +
  54120. +/**
  54121. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  54122. + */
  54123. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  54124. +
  54125. +/**
  54126. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  54127. + */
  54128. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  54129. +
  54130. +/**
  54131. + * Free a previously allocated buffer from the DMA pool.
  54132. + */
  54133. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  54134. +#endif
  54135. +
  54136. +/** Allocates a DMA capable buffer and zeroes its contents. */
  54137. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  54138. +
  54139. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  54140. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  54141. +
  54142. +/** Frees a previously allocated buffer. */
  54143. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  54144. +
  54145. +/** Allocates a block of memory and zeroes its contents. */
  54146. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  54147. +
  54148. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  54149. + * which can be used inside interrupt context. The size should be sufficiently
  54150. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  54151. + * __DWC_ALLOC if it is atomic. */
  54152. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  54153. +
  54154. +/** Frees a previously allocated buffer. */
  54155. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  54156. +
  54157. +#ifndef DWC_DEBUG_MEMORY
  54158. +
  54159. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  54160. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  54161. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  54162. +
  54163. +# ifdef DWC_LINUX
  54164. +#define DWC_DMA_ALLOC(_size_,_dma_) __DWC_DMA_ALLOC(NULL, _size_, _dma_)
  54165. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) __DWC_DMA_ALLOC_ATOMIC(NULL, _size_,_dma_)
  54166. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) __DWC_DMA_FREE(NULL, _size_, _virt_, _dma_)
  54167. +# endif
  54168. +
  54169. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  54170. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  54171. +#define DWC_DMA_FREE __DWC_DMA_FREE
  54172. +# endif
  54173. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  54174. +
  54175. +#else /* DWC_DEBUG_MEMORY */
  54176. +
  54177. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  54178. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  54179. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  54180. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  54181. + char const *func, int line);
  54182. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  54183. + char const *func, int line);
  54184. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  54185. + dwc_dma_t dma_addr, char const *func, int line);
  54186. +
  54187. +extern int dwc_memory_debug_start(void *mem_ctx);
  54188. +extern void dwc_memory_debug_stop(void);
  54189. +extern void dwc_memory_debug_report(void);
  54190. +
  54191. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  54192. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  54193. + __func__, __LINE__)
  54194. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  54195. +
  54196. +# ifdef DWC_LINUX
  54197. +#define DWC_DMA_ALLOC(_size_,_dma_) dwc_dma_alloc_debug(NULL, _size_, \
  54198. + _dma_, __func__, __LINE__)
  54199. +#define DWC_DMA_ALLOC_ATOMIC(_size_,_dma_) dwc_dma_alloc_atomic_debug(NULL, _size_, \
  54200. + _dma_, __func__, __LINE__)
  54201. +#define DWC_DMA_FREE(_size_,_virt_,_dma_) dwc_dma_free_debug(NULL, _size_, \
  54202. + _virt_, _dma_, __func__, __LINE__)
  54203. +# endif
  54204. +
  54205. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  54206. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  54207. + _dma_, __func__, __LINE__)
  54208. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  54209. + _virt_, _dma_, __func__, __LINE__)
  54210. +# endif
  54211. +
  54212. +#endif /* DWC_DEBUG_MEMORY */
  54213. +
  54214. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  54215. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  54216. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  54217. +
  54218. +#ifdef DWC_LINUX
  54219. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  54220. + * just throw away the DMA context parameter.
  54221. + */
  54222. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  54223. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  54224. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  54225. +#endif
  54226. +
  54227. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  54228. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  54229. + * them in using the DMA context parameter.
  54230. + */
  54231. +#define dwc_dma_alloc DWC_DMA_ALLOC
  54232. +#define dwc_dma_free DWC_DMA_FREE
  54233. +#endif
  54234. +
  54235. +
  54236. +/** @name Memory and String Processing */
  54237. +
  54238. +/** memset() clone */
  54239. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  54240. +#define dwc_memset DWC_MEMSET
  54241. +
  54242. +/** memcpy() clone */
  54243. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  54244. +#define dwc_memcpy DWC_MEMCPY
  54245. +
  54246. +/** memmove() clone */
  54247. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  54248. +#define dwc_memmove DWC_MEMMOVE
  54249. +
  54250. +/** memcmp() clone */
  54251. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  54252. +#define dwc_memcmp DWC_MEMCMP
  54253. +
  54254. +/** strcmp() clone */
  54255. +extern int DWC_STRCMP(void *s1, void *s2);
  54256. +#define dwc_strcmp DWC_STRCMP
  54257. +
  54258. +/** strncmp() clone */
  54259. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  54260. +#define dwc_strncmp DWC_STRNCMP
  54261. +
  54262. +/** strlen() clone, for NULL terminated ASCII strings */
  54263. +extern int DWC_STRLEN(char const *str);
  54264. +#define dwc_strlen DWC_STRLEN
  54265. +
  54266. +/** strcpy() clone, for NULL terminated ASCII strings */
  54267. +extern char *DWC_STRCPY(char *to, const char *from);
  54268. +#define dwc_strcpy DWC_STRCPY
  54269. +
  54270. +/** strdup() clone. If you wish to use memory allocation debugging, this
  54271. + * implementation of strdup should use the DWC_* memory routines instead of
  54272. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  54273. + * will not be seen by the debugging routines. */
  54274. +extern char *DWC_STRDUP(char const *str);
  54275. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  54276. +
  54277. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  54278. + * converted from the string str in base 10 unless the string begins with a "0x"
  54279. + * in which case it is base 16. String must be a NULL terminated sequence of
  54280. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  54281. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  54282. + * the number and end with a NULL character. If any invalid characters are
  54283. + * encountered or it returns with a negative error code and the results of the
  54284. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  54285. + * undefined. An example implementation using atoi() can be referenced from the
  54286. + * Linux implementation. */
  54287. +extern int DWC_ATOI(const char *str, int32_t *value);
  54288. +#define dwc_atoi DWC_ATOI
  54289. +
  54290. +/** Same as above but for unsigned. */
  54291. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  54292. +#define dwc_atoui DWC_ATOUI
  54293. +
  54294. +#ifdef DWC_UTFLIB
  54295. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  54296. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  54297. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  54298. +#endif
  54299. +
  54300. +
  54301. +/** @name Wait queues
  54302. + *
  54303. + * Wait queues provide a means of synchronizing between threads or processes. A
  54304. + * process can block on a waitq if some condition is not true, waiting for it to
  54305. + * become true. When the waitq is triggered all waiting process will get
  54306. + * unblocked and the condition will be check again. Waitqs should be triggered
  54307. + * every time a condition can potentially change.*/
  54308. +struct dwc_waitq;
  54309. +
  54310. +/** Type for a waitq */
  54311. +typedef struct dwc_waitq dwc_waitq_t;
  54312. +
  54313. +/** The type of waitq condition callback function. This is called every time
  54314. + * condition is evaluated. */
  54315. +typedef int (*dwc_waitq_condition_t)(void *data);
  54316. +
  54317. +/** Allocate a waitq */
  54318. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  54319. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  54320. +
  54321. +/** Free a waitq */
  54322. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  54323. +#define dwc_waitq_free DWC_WAITQ_FREE
  54324. +
  54325. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  54326. + * condition again. The function returns when the condition becomes true. The return value
  54327. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  54328. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  54329. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  54330. +
  54331. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  54332. + * check the condition again. The function returns when the condition become
  54333. + * true or the timeout has passed. The return value is 0 on condition true or
  54334. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  54335. + * error. */
  54336. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  54337. + void *data, int32_t msecs);
  54338. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  54339. +
  54340. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  54341. + * has potentially changed. */
  54342. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  54343. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  54344. +
  54345. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  54346. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  54347. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  54348. +
  54349. +
  54350. +/** @name Threads
  54351. + *
  54352. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  54353. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  54354. + * returns the value from the thread.
  54355. + */
  54356. +
  54357. +struct dwc_thread;
  54358. +
  54359. +/** Type for a thread */
  54360. +typedef struct dwc_thread dwc_thread_t;
  54361. +
  54362. +/** The thread function */
  54363. +typedef int (*dwc_thread_function_t)(void *data);
  54364. +
  54365. +/** Create a thread and start it running the thread_function. Returns a handle
  54366. + * to the thread */
  54367. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  54368. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  54369. +
  54370. +/** Stops a thread. Return the value returned by the thread. Or will return
  54371. + * DWC_ABORT if the thread never started. */
  54372. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  54373. +#define dwc_thread_stop DWC_THREAD_STOP
  54374. +
  54375. +/** Signifies to the thread that it must stop. */
  54376. +#ifdef DWC_LINUX
  54377. +/* Linux doesn't need any parameters for kthread_should_stop() */
  54378. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  54379. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  54380. +
  54381. +/* No thread_exit function in Linux */
  54382. +#define dwc_thread_exit(_thrd_)
  54383. +#endif
  54384. +
  54385. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  54386. +/** BSD needs the thread pointer for kthread_suspend_check() */
  54387. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  54388. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  54389. +
  54390. +/** The thread must call this to exit. */
  54391. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  54392. +#define dwc_thread_exit DWC_THREAD_EXIT
  54393. +#endif
  54394. +
  54395. +
  54396. +/** @name Work queues
  54397. + *
  54398. + * Workqs are used to queue a callback function to be called at some later time,
  54399. + * in another thread. */
  54400. +struct dwc_workq;
  54401. +
  54402. +/** Type for a workq */
  54403. +typedef struct dwc_workq dwc_workq_t;
  54404. +
  54405. +/** The type of the callback function to be called. */
  54406. +typedef void (*dwc_work_callback_t)(void *data);
  54407. +
  54408. +/** Allocate a workq */
  54409. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  54410. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  54411. +
  54412. +/** Free a workq. All work must be completed before being freed. */
  54413. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  54414. +#define dwc_workq_free DWC_WORKQ_FREE
  54415. +
  54416. +/** Schedule a callback on the workq, passing in data. The function will be
  54417. + * scheduled at some later time. */
  54418. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  54419. + void *data, char *format, ...)
  54420. +#ifdef __GNUC__
  54421. + __attribute__ ((format(printf, 4, 5)));
  54422. +#else
  54423. + ;
  54424. +#endif
  54425. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  54426. +
  54427. +/** Schedule a callback on the workq, that will be called until at least
  54428. + * given number miliseconds have passed. */
  54429. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  54430. + void *data, uint32_t time, char *format, ...)
  54431. +#ifdef __GNUC__
  54432. + __attribute__ ((format(printf, 5, 6)));
  54433. +#else
  54434. + ;
  54435. +#endif
  54436. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  54437. +
  54438. +/** The number of processes in the workq */
  54439. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  54440. +#define dwc_workq_pending DWC_WORKQ_PENDING
  54441. +
  54442. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  54443. + * 0 on timeout. */
  54444. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  54445. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  54446. +
  54447. +
  54448. +/** @name Tasklets
  54449. + *
  54450. + */
  54451. +struct dwc_tasklet;
  54452. +
  54453. +/** Type for a tasklet */
  54454. +typedef struct dwc_tasklet dwc_tasklet_t;
  54455. +
  54456. +/** The type of the callback function to be called */
  54457. +typedef void (*dwc_tasklet_callback_t)(void *data);
  54458. +
  54459. +/** Allocates a tasklet */
  54460. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  54461. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  54462. +
  54463. +/** Frees a tasklet */
  54464. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  54465. +#define dwc_task_free DWC_TASK_FREE
  54466. +
  54467. +/** Schedules a tasklet to run */
  54468. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  54469. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  54470. +
  54471. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  54472. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  54473. +
  54474. +/** @name Timer
  54475. + *
  54476. + * Callbacks must be small and atomic.
  54477. + */
  54478. +struct dwc_timer;
  54479. +
  54480. +/** Type for a timer */
  54481. +typedef struct dwc_timer dwc_timer_t;
  54482. +
  54483. +/** The type of the callback function to be called */
  54484. +typedef void (*dwc_timer_callback_t)(void *data);
  54485. +
  54486. +/** Allocates a timer */
  54487. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  54488. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  54489. +
  54490. +/** Frees a timer */
  54491. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  54492. +#define dwc_timer_free DWC_TIMER_FREE
  54493. +
  54494. +/** Schedules the timer to run at time ms from now. And will repeat at every
  54495. + * repeat_interval msec therafter
  54496. + *
  54497. + * Modifies a timer that is still awaiting execution to a new expiration time.
  54498. + * The mod_time is added to the old time. */
  54499. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  54500. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  54501. +
  54502. +/** Disables the timer from execution. */
  54503. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  54504. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  54505. +
  54506. +
  54507. +/** @name Spinlocks
  54508. + *
  54509. + * These locks are used when the work between the lock/unlock is atomic and
  54510. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  54511. + * suitable to lock between interrupt/non-interrupt context. They also lock
  54512. + * between processes if you have multiple CPUs or Preemption. If you don't have
  54513. + * multiple CPUS or Preemption, then the you can simply implement the
  54514. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  54515. + * the work between the lock/unlock is atomic, the process context will never
  54516. + * change, and so you never have to lock between processes. */
  54517. +
  54518. +struct dwc_spinlock;
  54519. +
  54520. +/** Type for a spinlock */
  54521. +typedef struct dwc_spinlock dwc_spinlock_t;
  54522. +
  54523. +/** Type for the 'flags' argument to spinlock funtions */
  54524. +typedef unsigned long dwc_irqflags_t;
  54525. +
  54526. +/** Returns an initialized lock variable. This function should allocate and
  54527. + * initialize the OS-specific data structure used for locking. This data
  54528. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  54529. + * be freed by the DWC_FREE_LOCK when it is no longer used.
  54530. + *
  54531. + * For Linux Spinlock Debugging make it macro because the debugging routines use
  54532. + * the symbol name to determine recursive locking. Using a wrapper function
  54533. + * makes it falsely think recursive locking occurs. */
  54534. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
  54535. +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
  54536. + lock = DWC_ALLOC(sizeof(spinlock_t)); \
  54537. + if (lock) { \
  54538. + spin_lock_init((spinlock_t *)lock); \
  54539. + } \
  54540. +})
  54541. +#else
  54542. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  54543. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  54544. +#endif
  54545. +
  54546. +/** Frees an initialized lock variable. */
  54547. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  54548. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  54549. +
  54550. +/** Disables interrupts and blocks until it acquires the lock.
  54551. + *
  54552. + * @param lock Pointer to the spinlock.
  54553. + * @param flags Unsigned long for irq flags storage.
  54554. + */
  54555. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  54556. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  54557. +
  54558. +/** Re-enables the interrupt and releases the lock.
  54559. + *
  54560. + * @param lock Pointer to the spinlock.
  54561. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  54562. + * passed into DWC_LOCK.
  54563. + */
  54564. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  54565. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  54566. +
  54567. +/** Blocks until it acquires the lock.
  54568. + *
  54569. + * @param lock Pointer to the spinlock.
  54570. + */
  54571. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  54572. +#define dwc_spinlock DWC_SPINLOCK
  54573. +
  54574. +/** Releases the lock.
  54575. + *
  54576. + * @param lock Pointer to the spinlock.
  54577. + */
  54578. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  54579. +#define dwc_spinunlock DWC_SPINUNLOCK
  54580. +
  54581. +
  54582. +/** @name Mutexes
  54583. + *
  54584. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  54585. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  54586. + */
  54587. +
  54588. +struct dwc_mutex;
  54589. +
  54590. +/** Type for a mutex */
  54591. +typedef struct dwc_mutex dwc_mutex_t;
  54592. +
  54593. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  54594. + * the symbol to determine recursive locking. This makes it falsely think
  54595. + * recursive locking occurs. */
  54596. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  54597. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  54598. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  54599. + mutex_init((struct mutex *)__mutexp); \
  54600. +})
  54601. +#endif
  54602. +
  54603. +/** Allocate a mutex */
  54604. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  54605. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  54606. +
  54607. +/* For memory leak debugging when using Linux Mutex Debugging */
  54608. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  54609. +#define DWC_MUTEX_FREE(__mutexp) do { \
  54610. + mutex_destroy((struct mutex *)__mutexp); \
  54611. + DWC_FREE(__mutexp); \
  54612. +} while(0)
  54613. +#else
  54614. +/** Free a mutex */
  54615. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  54616. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  54617. +#endif
  54618. +
  54619. +/** Lock a mutex */
  54620. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  54621. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  54622. +
  54623. +/** Non-blocking lock returns 1 on successful lock. */
  54624. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  54625. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  54626. +
  54627. +/** Unlock a mutex */
  54628. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  54629. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  54630. +
  54631. +
  54632. +/** @name Time */
  54633. +
  54634. +/** Microsecond delay.
  54635. + *
  54636. + * @param usecs Microseconds to delay.
  54637. + */
  54638. +extern void DWC_UDELAY(uint32_t usecs);
  54639. +#define dwc_udelay DWC_UDELAY
  54640. +
  54641. +/** Millisecond delay.
  54642. + *
  54643. + * @param msecs Milliseconds to delay.
  54644. + */
  54645. +extern void DWC_MDELAY(uint32_t msecs);
  54646. +#define dwc_mdelay DWC_MDELAY
  54647. +
  54648. +/** Non-busy waiting.
  54649. + * Sleeps for specified number of milliseconds.
  54650. + *
  54651. + * @param msecs Milliseconds to sleep.
  54652. + */
  54653. +extern void DWC_MSLEEP(uint32_t msecs);
  54654. +#define dwc_msleep DWC_MSLEEP
  54655. +
  54656. +/**
  54657. + * Returns number of milliseconds since boot.
  54658. + */
  54659. +extern uint32_t DWC_TIME(void);
  54660. +#define dwc_time DWC_TIME
  54661. +
  54662. +
  54663. +
  54664. +
  54665. +/* @mainpage DWC Portability and Common Library
  54666. + *
  54667. + * This is the documentation for the DWC Portability and Common Library.
  54668. + *
  54669. + * @section intro Introduction
  54670. + *
  54671. + * The DWC Portability library consists of wrapper calls and data structures to
  54672. + * all low-level functions which are typically provided by the OS. The WUDEV
  54673. + * driver uses only these functions. In order to port the WUDEV driver, only
  54674. + * the functions in this library need to be re-implemented, with the same
  54675. + * behavior as documented here.
  54676. + *
  54677. + * The Common library consists of higher level functions, which rely only on
  54678. + * calling the functions from the DWC Portability library. These common
  54679. + * routines are shared across modules. Some of the common libraries need to be
  54680. + * used directly by the driver programmer when porting WUDEV. Such as the
  54681. + * parameter and notification libraries.
  54682. + *
  54683. + * @section low Portability Library OS Wrapper Functions
  54684. + *
  54685. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  54686. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  54687. + * these functions are included in the dwc_os.h file.
  54688. + *
  54689. + * There are many functions here covering a wide array of OS services. Please
  54690. + * see dwc_os.h for details, and implementation notes for each function.
  54691. + *
  54692. + * @section common Common Library Functions
  54693. + *
  54694. + * Any function starting with dwc and in all lowercase is a common library
  54695. + * routine. These functions have a portable implementation and do not need to
  54696. + * be reimplemented when porting. The common routines can be used by any
  54697. + * driver, and some must be used by the end user to control the drivers. For
  54698. + * example, you must use the Parameter common library in order to set the
  54699. + * parameters in the WUDEV module.
  54700. + *
  54701. + * The common libraries consist of the following:
  54702. + *
  54703. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  54704. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  54705. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  54706. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  54707. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  54708. + * - Modpow - Used internally only. See dwc_modpow.h
  54709. + * - DH - Used internally only. See dwc_dh.h
  54710. + * - Crypto - Used internally only. See dwc_crypto.h
  54711. + *
  54712. + *
  54713. + * @section prereq Prerequistes For dwc_os.h
  54714. + * @subsection types Data Types
  54715. + *
  54716. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  54717. + * compilation environment. These data types are:
  54718. + *
  54719. + * - uint8_t - unsigned 8-bit data type
  54720. + * - int8_t - signed 8-bit data type
  54721. + * - uint16_t - unsigned 16-bit data type
  54722. + * - int16_t - signed 16-bit data type
  54723. + * - uint32_t - unsigned 32-bit data type
  54724. + * - int32_t - signed 32-bit data type
  54725. + * - uint64_t - unsigned 64-bit data type
  54726. + * - int64_t - signed 64-bit data type
  54727. + *
  54728. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  54729. + * that is to modify the top of the file to include the appropriate header.
  54730. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  54731. + * defined, the correct header will be added. A standard header <stdint.h> is
  54732. + * also used for environments where standard C headers are available.
  54733. + *
  54734. + * @subsection stdarg Variable Arguments
  54735. + *
  54736. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  54737. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  54738. + * provided in your enviornment in order to use dwc_os.h with the debug and
  54739. + * tracing message functionality.
  54740. + *
  54741. + * @subsection thread Threading
  54742. + *
  54743. + * WUDEV Core must be run on an operating system that provides for multiple
  54744. + * threads/processes. Threading can be implemented in many ways, even in
  54745. + * embedded systems without an operating system. At the bare minimum, the
  54746. + * system should be able to start any number of processes at any time to handle
  54747. + * special work. It need not be a pre-emptive system. Process context can
  54748. + * change upon a call to a blocking function. The hardware interrupt context
  54749. + * that calls the module's ISR() function must be differentiable from process
  54750. + * context, even if your processes are impemented via a hardware interrupt.
  54751. + * Further locking mechanism between process must exist (or be implemented), and
  54752. + * process context must have a way to disable interrupts for a period of time to
  54753. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  54754. + * threading should be able to be implemented with the defined behavior.
  54755. + *
  54756. + */
  54757. +
  54758. +#ifdef __cplusplus
  54759. +}
  54760. +#endif
  54761. +
  54762. +#endif /* _DWC_OS_H_ */
  54763. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/Makefile linux-3.16-rpi/drivers/usb/host/dwc_common_port/Makefile
  54764. --- linux-3.16.2/drivers/usb/host/dwc_common_port/Makefile 1970-01-01 01:00:00.000000000 +0100
  54765. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/Makefile 2014-09-14 19:04:13.000000000 +0200
  54766. @@ -0,0 +1,58 @@
  54767. +#
  54768. +# Makefile for DWC_common library
  54769. +#
  54770. +
  54771. +ifneq ($(KERNELRELEASE),)
  54772. +
  54773. +ccflags-y += -DDWC_LINUX
  54774. +#ccflags-y += -DDEBUG
  54775. +#ccflags-y += -DDWC_DEBUG_REGS
  54776. +#ccflags-y += -DDWC_DEBUG_MEMORY
  54777. +
  54778. +ccflags-y += -DDWC_LIBMODULE
  54779. +ccflags-y += -DDWC_CCLIB
  54780. +#ccflags-y += -DDWC_CRYPTOLIB
  54781. +ccflags-y += -DDWC_NOTIFYLIB
  54782. +ccflags-y += -DDWC_UTFLIB
  54783. +
  54784. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  54785. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  54786. + dwc_crypto.o dwc_notifier.o \
  54787. + dwc_common_linux.o dwc_mem.o
  54788. +
  54789. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  54790. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  54791. +
  54792. +ifneq ($(kernrel3),2.6.20)
  54793. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  54794. +ccflags-y += $(CPPFLAGS)
  54795. +endif
  54796. +
  54797. +else
  54798. +
  54799. +#ifeq ($(KDIR),)
  54800. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  54801. +#endif
  54802. +
  54803. +ifeq ($(ARCH),)
  54804. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  54805. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  54806. +endif
  54807. +
  54808. +ifeq ($(DOXYGEN),)
  54809. +DOXYGEN := doxygen
  54810. +endif
  54811. +
  54812. +default:
  54813. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  54814. +
  54815. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  54816. + $(DOXYGEN) doc/doxygen.cfg
  54817. +
  54818. +tags: $(wildcard *.[hc])
  54819. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  54820. +
  54821. +endif
  54822. +
  54823. +clean:
  54824. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  54825. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/Makefile.fbsd linux-3.16-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd
  54826. --- linux-3.16.2/drivers/usb/host/dwc_common_port/Makefile.fbsd 1970-01-01 01:00:00.000000000 +0100
  54827. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/Makefile.fbsd 2014-04-13 17:33:11.000000000 +0200
  54828. @@ -0,0 +1,17 @@
  54829. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  54830. +CFLAGS += -DDWC_FREEBSD
  54831. +CFLAGS += -DDEBUG
  54832. +#CFLAGS += -DDWC_DEBUG_REGS
  54833. +#CFLAGS += -DDWC_DEBUG_MEMORY
  54834. +
  54835. +#CFLAGS += -DDWC_LIBMODULE
  54836. +#CFLAGS += -DDWC_CCLIB
  54837. +#CFLAGS += -DDWC_CRYPTOLIB
  54838. +#CFLAGS += -DDWC_NOTIFYLIB
  54839. +#CFLAGS += -DDWC_UTFLIB
  54840. +
  54841. +KMOD = dwc_common_port_lib
  54842. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  54843. + dwc_common_fbsd.c dwc_mem.c
  54844. +
  54845. +.include <bsd.kmod.mk>
  54846. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/Makefile.linux linux-3.16-rpi/drivers/usb/host/dwc_common_port/Makefile.linux
  54847. --- linux-3.16.2/drivers/usb/host/dwc_common_port/Makefile.linux 1970-01-01 01:00:00.000000000 +0100
  54848. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/Makefile.linux 2014-04-13 17:33:11.000000000 +0200
  54849. @@ -0,0 +1,49 @@
  54850. +#
  54851. +# Makefile for DWC_common library
  54852. +#
  54853. +ifneq ($(KERNELRELEASE),)
  54854. +
  54855. +ccflags-y += -DDWC_LINUX
  54856. +#ccflags-y += -DDEBUG
  54857. +#ccflags-y += -DDWC_DEBUG_REGS
  54858. +#ccflags-y += -DDWC_DEBUG_MEMORY
  54859. +
  54860. +ccflags-y += -DDWC_LIBMODULE
  54861. +ccflags-y += -DDWC_CCLIB
  54862. +ccflags-y += -DDWC_CRYPTOLIB
  54863. +ccflags-y += -DDWC_NOTIFYLIB
  54864. +ccflags-y += -DDWC_UTFLIB
  54865. +
  54866. +obj-m := dwc_common_port_lib.o
  54867. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  54868. + dwc_crypto.o dwc_notifier.o \
  54869. + dwc_common_linux.o dwc_mem.o
  54870. +
  54871. +else
  54872. +
  54873. +ifeq ($(KDIR),)
  54874. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  54875. +endif
  54876. +
  54877. +ifeq ($(ARCH),)
  54878. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  54879. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  54880. +endif
  54881. +
  54882. +ifeq ($(DOXYGEN),)
  54883. +DOXYGEN := doxygen
  54884. +endif
  54885. +
  54886. +default:
  54887. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  54888. +
  54889. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  54890. + $(DOXYGEN) doc/doxygen.cfg
  54891. +
  54892. +tags: $(wildcard *.[hc])
  54893. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  54894. +
  54895. +endif
  54896. +
  54897. +clean:
  54898. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  54899. diff -Nur linux-3.16.2/drivers/usb/host/dwc_common_port/usb.h linux-3.16-rpi/drivers/usb/host/dwc_common_port/usb.h
  54900. --- linux-3.16.2/drivers/usb/host/dwc_common_port/usb.h 1970-01-01 01:00:00.000000000 +0100
  54901. +++ linux-3.16-rpi/drivers/usb/host/dwc_common_port/usb.h 2014-04-13 17:33:11.000000000 +0200
  54902. @@ -0,0 +1,946 @@
  54903. +/*
  54904. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  54905. + * All rights reserved.
  54906. + *
  54907. + * This code is derived from software contributed to The NetBSD Foundation
  54908. + * by Lennart Augustsson (lennart@augustsson.net) at
  54909. + * Carlstedt Research & Technology.
  54910. + *
  54911. + * Redistribution and use in source and binary forms, with or without
  54912. + * modification, are permitted provided that the following conditions
  54913. + * are met:
  54914. + * 1. Redistributions of source code must retain the above copyright
  54915. + * notice, this list of conditions and the following disclaimer.
  54916. + * 2. Redistributions in binary form must reproduce the above copyright
  54917. + * notice, this list of conditions and the following disclaimer in the
  54918. + * documentation and/or other materials provided with the distribution.
  54919. + * 3. All advertising materials mentioning features or use of this software
  54920. + * must display the following acknowledgement:
  54921. + * This product includes software developed by the NetBSD
  54922. + * Foundation, Inc. and its contributors.
  54923. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  54924. + * contributors may be used to endorse or promote products derived
  54925. + * from this software without specific prior written permission.
  54926. + *
  54927. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  54928. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  54929. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  54930. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  54931. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  54932. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  54933. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54934. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  54935. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  54936. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54937. + * POSSIBILITY OF SUCH DAMAGE.
  54938. + */
  54939. +
  54940. +/* Modified by Synopsys, Inc, 12/12/2007 */
  54941. +
  54942. +
  54943. +#ifndef _USB_H_
  54944. +#define _USB_H_
  54945. +
  54946. +#ifdef __cplusplus
  54947. +extern "C" {
  54948. +#endif
  54949. +
  54950. +/*
  54951. + * The USB records contain some unaligned little-endian word
  54952. + * components. The U[SG]ETW macros take care of both the alignment
  54953. + * and endian problem and should always be used to access non-byte
  54954. + * values.
  54955. + */
  54956. +typedef u_int8_t uByte;
  54957. +typedef u_int8_t uWord[2];
  54958. +typedef u_int8_t uDWord[4];
  54959. +
  54960. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  54961. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  54962. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  54963. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  54964. +
  54965. +#if 1
  54966. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  54967. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  54968. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  54969. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  54970. + (w)[1] = (u_int8_t)((v) >> 8), \
  54971. + (w)[2] = (u_int8_t)((v) >> 16), \
  54972. + (w)[3] = (u_int8_t)((v) >> 24))
  54973. +#else
  54974. +/*
  54975. + * On little-endian machines that can handle unanliged accesses
  54976. + * (e.g. i386) these macros can be replaced by the following.
  54977. + */
  54978. +#define UGETW(w) (*(u_int16_t *)(w))
  54979. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  54980. +#define UGETDW(w) (*(u_int32_t *)(w))
  54981. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  54982. +#endif
  54983. +
  54984. +/*
  54985. + * Macros for accessing UAS IU fields, which are big-endian
  54986. + */
  54987. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  54988. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  54989. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  54990. + ((x) >> 8) & 0xff, (x) & 0xff }
  54991. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  54992. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  54993. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  54994. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  54995. + (w)[1] = (u_int8_t)((v) >> 16), \
  54996. + (w)[2] = (u_int8_t)((v) >> 8), \
  54997. + (w)[3] = (u_int8_t)(v))
  54998. +
  54999. +#define UPACKED __attribute__((__packed__))
  55000. +
  55001. +typedef struct {
  55002. + uByte bmRequestType;
  55003. + uByte bRequest;
  55004. + uWord wValue;
  55005. + uWord wIndex;
  55006. + uWord wLength;
  55007. +} UPACKED usb_device_request_t;
  55008. +
  55009. +#define UT_GET_DIR(a) ((a) & 0x80)
  55010. +#define UT_WRITE 0x00
  55011. +#define UT_READ 0x80
  55012. +
  55013. +#define UT_GET_TYPE(a) ((a) & 0x60)
  55014. +#define UT_STANDARD 0x00
  55015. +#define UT_CLASS 0x20
  55016. +#define UT_VENDOR 0x40
  55017. +
  55018. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  55019. +#define UT_DEVICE 0x00
  55020. +#define UT_INTERFACE 0x01
  55021. +#define UT_ENDPOINT 0x02
  55022. +#define UT_OTHER 0x03
  55023. +
  55024. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  55025. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  55026. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  55027. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  55028. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  55029. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  55030. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  55031. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  55032. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  55033. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  55034. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  55035. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  55036. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  55037. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  55038. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  55039. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  55040. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  55041. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  55042. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  55043. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  55044. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  55045. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  55046. +
  55047. +/* Requests */
  55048. +#define UR_GET_STATUS 0x00
  55049. +#define USTAT_STANDARD_STATUS 0x00
  55050. +#define WUSTAT_WUSB_FEATURE 0x01
  55051. +#define WUSTAT_CHANNEL_INFO 0x02
  55052. +#define WUSTAT_RECEIVED_DATA 0x03
  55053. +#define WUSTAT_MAS_AVAILABILITY 0x04
  55054. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  55055. +#define UR_CLEAR_FEATURE 0x01
  55056. +#define UR_SET_FEATURE 0x03
  55057. +#define UR_SET_AND_TEST_FEATURE 0x0c
  55058. +#define UR_SET_ADDRESS 0x05
  55059. +#define UR_GET_DESCRIPTOR 0x06
  55060. +#define UDESC_DEVICE 0x01
  55061. +#define UDESC_CONFIG 0x02
  55062. +#define UDESC_STRING 0x03
  55063. +#define UDESC_INTERFACE 0x04
  55064. +#define UDESC_ENDPOINT 0x05
  55065. +#define UDESC_SS_USB_COMPANION 0x30
  55066. +#define UDESC_DEVICE_QUALIFIER 0x06
  55067. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  55068. +#define UDESC_INTERFACE_POWER 0x08
  55069. +#define UDESC_OTG 0x09
  55070. +#define WUDESC_SECURITY 0x0c
  55071. +#define WUDESC_KEY 0x0d
  55072. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  55073. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  55074. +#define WUD_KEY_TYPE_ASSOC 0x01
  55075. +#define WUD_KEY_TYPE_GTK 0x02
  55076. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  55077. +#define WUD_KEY_ORIGIN_HOST 0x00
  55078. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  55079. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  55080. +#define WUDESC_BOS 0x0f
  55081. +#define WUDESC_DEVICE_CAPABILITY 0x10
  55082. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  55083. +#define UDESC_BOS 0x0f
  55084. +#define UDESC_DEVICE_CAPABILITY 0x10
  55085. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  55086. +#define UDESC_CS_CONFIG 0x22
  55087. +#define UDESC_CS_STRING 0x23
  55088. +#define UDESC_CS_INTERFACE 0x24
  55089. +#define UDESC_CS_ENDPOINT 0x25
  55090. +#define UDESC_HUB 0x29
  55091. +#define UR_SET_DESCRIPTOR 0x07
  55092. +#define UR_GET_CONFIG 0x08
  55093. +#define UR_SET_CONFIG 0x09
  55094. +#define UR_GET_INTERFACE 0x0a
  55095. +#define UR_SET_INTERFACE 0x0b
  55096. +#define UR_SYNCH_FRAME 0x0c
  55097. +#define WUR_SET_ENCRYPTION 0x0d
  55098. +#define WUR_GET_ENCRYPTION 0x0e
  55099. +#define WUR_SET_HANDSHAKE 0x0f
  55100. +#define WUR_GET_HANDSHAKE 0x10
  55101. +#define WUR_SET_CONNECTION 0x11
  55102. +#define WUR_SET_SECURITY_DATA 0x12
  55103. +#define WUR_GET_SECURITY_DATA 0x13
  55104. +#define WUR_SET_WUSB_DATA 0x14
  55105. +#define WUDATA_DRPIE_INFO 0x01
  55106. +#define WUDATA_TRANSMIT_DATA 0x02
  55107. +#define WUDATA_TRANSMIT_PARAMS 0x03
  55108. +#define WUDATA_RECEIVE_PARAMS 0x04
  55109. +#define WUDATA_TRANSMIT_POWER 0x05
  55110. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  55111. +#define WUR_LOOPBACK_DATA_READ 0x16
  55112. +#define WUR_SET_INTERFACE_DS 0x17
  55113. +
  55114. +/* Feature numbers */
  55115. +#define UF_ENDPOINT_HALT 0
  55116. +#define UF_DEVICE_REMOTE_WAKEUP 1
  55117. +#define UF_TEST_MODE 2
  55118. +#define UF_DEVICE_B_HNP_ENABLE 3
  55119. +#define UF_DEVICE_A_HNP_SUPPORT 4
  55120. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  55121. +#define WUF_WUSB 3
  55122. +#define WUF_TX_DRPIE 0x0
  55123. +#define WUF_DEV_XMIT_PACKET 0x1
  55124. +#define WUF_COUNT_PACKETS 0x2
  55125. +#define WUF_CAPTURE_PACKETS 0x3
  55126. +#define UF_FUNCTION_SUSPEND 0
  55127. +#define UF_U1_ENABLE 48
  55128. +#define UF_U2_ENABLE 49
  55129. +#define UF_LTM_ENABLE 50
  55130. +
  55131. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  55132. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  55133. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  55134. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  55135. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  55136. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  55137. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  55138. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  55139. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  55140. +
  55141. +#ifdef _MSC_VER
  55142. +#include <pshpack1.h>
  55143. +#endif
  55144. +
  55145. +typedef struct {
  55146. + uByte bLength;
  55147. + uByte bDescriptorType;
  55148. + uByte bDescriptorSubtype;
  55149. +} UPACKED usb_descriptor_t;
  55150. +
  55151. +typedef struct {
  55152. + uByte bLength;
  55153. + uByte bDescriptorType;
  55154. +} UPACKED usb_descriptor_header_t;
  55155. +
  55156. +typedef struct {
  55157. + uByte bLength;
  55158. + uByte bDescriptorType;
  55159. + uWord bcdUSB;
  55160. +#define UD_USB_2_0 0x0200
  55161. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  55162. + uByte bDeviceClass;
  55163. + uByte bDeviceSubClass;
  55164. + uByte bDeviceProtocol;
  55165. + uByte bMaxPacketSize;
  55166. + /* The fields below are not part of the initial descriptor. */
  55167. + uWord idVendor;
  55168. + uWord idProduct;
  55169. + uWord bcdDevice;
  55170. + uByte iManufacturer;
  55171. + uByte iProduct;
  55172. + uByte iSerialNumber;
  55173. + uByte bNumConfigurations;
  55174. +} UPACKED usb_device_descriptor_t;
  55175. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  55176. +
  55177. +typedef struct {
  55178. + uByte bLength;
  55179. + uByte bDescriptorType;
  55180. + uWord wTotalLength;
  55181. + uByte bNumInterface;
  55182. + uByte bConfigurationValue;
  55183. + uByte iConfiguration;
  55184. +#define UC_ATT_ONE (1 << 7) /* must be set */
  55185. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  55186. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  55187. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  55188. + uByte bmAttributes;
  55189. +#define UC_BUS_POWERED 0x80
  55190. +#define UC_SELF_POWERED 0x40
  55191. +#define UC_REMOTE_WAKEUP 0x20
  55192. + uByte bMaxPower; /* max current in 2 mA units */
  55193. +#define UC_POWER_FACTOR 2
  55194. +} UPACKED usb_config_descriptor_t;
  55195. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  55196. +
  55197. +typedef struct {
  55198. + uByte bLength;
  55199. + uByte bDescriptorType;
  55200. + uByte bInterfaceNumber;
  55201. + uByte bAlternateSetting;
  55202. + uByte bNumEndpoints;
  55203. + uByte bInterfaceClass;
  55204. + uByte bInterfaceSubClass;
  55205. + uByte bInterfaceProtocol;
  55206. + uByte iInterface;
  55207. +} UPACKED usb_interface_descriptor_t;
  55208. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  55209. +
  55210. +typedef struct {
  55211. + uByte bLength;
  55212. + uByte bDescriptorType;
  55213. + uByte bEndpointAddress;
  55214. +#define UE_GET_DIR(a) ((a) & 0x80)
  55215. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  55216. +#define UE_DIR_IN 0x80
  55217. +#define UE_DIR_OUT 0x00
  55218. +#define UE_ADDR 0x0f
  55219. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  55220. + uByte bmAttributes;
  55221. +#define UE_XFERTYPE 0x03
  55222. +#define UE_CONTROL 0x00
  55223. +#define UE_ISOCHRONOUS 0x01
  55224. +#define UE_BULK 0x02
  55225. +#define UE_INTERRUPT 0x03
  55226. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  55227. +#define UE_ISO_TYPE 0x0c
  55228. +#define UE_ISO_ASYNC 0x04
  55229. +#define UE_ISO_ADAPT 0x08
  55230. +#define UE_ISO_SYNC 0x0c
  55231. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  55232. + uWord wMaxPacketSize;
  55233. + uByte bInterval;
  55234. +} UPACKED usb_endpoint_descriptor_t;
  55235. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  55236. +
  55237. +typedef struct ss_endpoint_companion_descriptor {
  55238. + uByte bLength;
  55239. + uByte bDescriptorType;
  55240. + uByte bMaxBurst;
  55241. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  55242. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  55243. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  55244. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  55245. + uByte bmAttributes;
  55246. + uWord wBytesPerInterval;
  55247. +} UPACKED ss_endpoint_companion_descriptor_t;
  55248. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  55249. +
  55250. +typedef struct {
  55251. + uByte bLength;
  55252. + uByte bDescriptorType;
  55253. + uWord bString[127];
  55254. +} UPACKED usb_string_descriptor_t;
  55255. +#define USB_MAX_STRING_LEN 128
  55256. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  55257. +
  55258. +/* Hub specific request */
  55259. +#define UR_GET_BUS_STATE 0x02
  55260. +#define UR_CLEAR_TT_BUFFER 0x08
  55261. +#define UR_RESET_TT 0x09
  55262. +#define UR_GET_TT_STATE 0x0a
  55263. +#define UR_STOP_TT 0x0b
  55264. +
  55265. +/* Hub features */
  55266. +#define UHF_C_HUB_LOCAL_POWER 0
  55267. +#define UHF_C_HUB_OVER_CURRENT 1
  55268. +#define UHF_PORT_CONNECTION 0
  55269. +#define UHF_PORT_ENABLE 1
  55270. +#define UHF_PORT_SUSPEND 2
  55271. +#define UHF_PORT_OVER_CURRENT 3
  55272. +#define UHF_PORT_RESET 4
  55273. +#define UHF_PORT_L1 5
  55274. +#define UHF_PORT_POWER 8
  55275. +#define UHF_PORT_LOW_SPEED 9
  55276. +#define UHF_PORT_HIGH_SPEED 10
  55277. +#define UHF_C_PORT_CONNECTION 16
  55278. +#define UHF_C_PORT_ENABLE 17
  55279. +#define UHF_C_PORT_SUSPEND 18
  55280. +#define UHF_C_PORT_OVER_CURRENT 19
  55281. +#define UHF_C_PORT_RESET 20
  55282. +#define UHF_C_PORT_L1 23
  55283. +#define UHF_PORT_TEST 21
  55284. +#define UHF_PORT_INDICATOR 22
  55285. +
  55286. +typedef struct {
  55287. + uByte bDescLength;
  55288. + uByte bDescriptorType;
  55289. + uByte bNbrPorts;
  55290. + uWord wHubCharacteristics;
  55291. +#define UHD_PWR 0x0003
  55292. +#define UHD_PWR_GANGED 0x0000
  55293. +#define UHD_PWR_INDIVIDUAL 0x0001
  55294. +#define UHD_PWR_NO_SWITCH 0x0002
  55295. +#define UHD_COMPOUND 0x0004
  55296. +#define UHD_OC 0x0018
  55297. +#define UHD_OC_GLOBAL 0x0000
  55298. +#define UHD_OC_INDIVIDUAL 0x0008
  55299. +#define UHD_OC_NONE 0x0010
  55300. +#define UHD_TT_THINK 0x0060
  55301. +#define UHD_TT_THINK_8 0x0000
  55302. +#define UHD_TT_THINK_16 0x0020
  55303. +#define UHD_TT_THINK_24 0x0040
  55304. +#define UHD_TT_THINK_32 0x0060
  55305. +#define UHD_PORT_IND 0x0080
  55306. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  55307. +#define UHD_PWRON_FACTOR 2
  55308. + uByte bHubContrCurrent;
  55309. + uByte DeviceRemovable[32]; /* max 255 ports */
  55310. +#define UHD_NOT_REMOV(desc, i) \
  55311. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  55312. + /* deprecated */ uByte PortPowerCtrlMask[1];
  55313. +} UPACKED usb_hub_descriptor_t;
  55314. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  55315. +
  55316. +typedef struct {
  55317. + uByte bLength;
  55318. + uByte bDescriptorType;
  55319. + uWord bcdUSB;
  55320. + uByte bDeviceClass;
  55321. + uByte bDeviceSubClass;
  55322. + uByte bDeviceProtocol;
  55323. + uByte bMaxPacketSize0;
  55324. + uByte bNumConfigurations;
  55325. + uByte bReserved;
  55326. +} UPACKED usb_device_qualifier_t;
  55327. +#define USB_DEVICE_QUALIFIER_SIZE 10
  55328. +
  55329. +typedef struct {
  55330. + uByte bLength;
  55331. + uByte bDescriptorType;
  55332. + uByte bmAttributes;
  55333. +#define UOTG_SRP 0x01
  55334. +#define UOTG_HNP 0x02
  55335. +} UPACKED usb_otg_descriptor_t;
  55336. +
  55337. +/* OTG feature selectors */
  55338. +#define UOTG_B_HNP_ENABLE 3
  55339. +#define UOTG_A_HNP_SUPPORT 4
  55340. +#define UOTG_A_ALT_HNP_SUPPORT 5
  55341. +
  55342. +typedef struct {
  55343. + uWord wStatus;
  55344. +/* Device status flags */
  55345. +#define UDS_SELF_POWERED 0x0001
  55346. +#define UDS_REMOTE_WAKEUP 0x0002
  55347. +/* Endpoint status flags */
  55348. +#define UES_HALT 0x0001
  55349. +} UPACKED usb_status_t;
  55350. +
  55351. +typedef struct {
  55352. + uWord wHubStatus;
  55353. +#define UHS_LOCAL_POWER 0x0001
  55354. +#define UHS_OVER_CURRENT 0x0002
  55355. + uWord wHubChange;
  55356. +} UPACKED usb_hub_status_t;
  55357. +
  55358. +typedef struct {
  55359. + uWord wPortStatus;
  55360. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  55361. +#define UPS_PORT_ENABLED 0x0002
  55362. +#define UPS_SUSPEND 0x0004
  55363. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  55364. +#define UPS_RESET 0x0010
  55365. +#define UPS_PORT_POWER 0x0100
  55366. +#define UPS_LOW_SPEED 0x0200
  55367. +#define UPS_HIGH_SPEED 0x0400
  55368. +#define UPS_PORT_TEST 0x0800
  55369. +#define UPS_PORT_INDICATOR 0x1000
  55370. + uWord wPortChange;
  55371. +#define UPS_C_CONNECT_STATUS 0x0001
  55372. +#define UPS_C_PORT_ENABLED 0x0002
  55373. +#define UPS_C_SUSPEND 0x0004
  55374. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  55375. +#define UPS_C_PORT_RESET 0x0010
  55376. +} UPACKED usb_port_status_t;
  55377. +
  55378. +#ifdef _MSC_VER
  55379. +#include <poppack.h>
  55380. +#endif
  55381. +
  55382. +/* Device class codes */
  55383. +#define UDCLASS_IN_INTERFACE 0x00
  55384. +#define UDCLASS_COMM 0x02
  55385. +#define UDCLASS_HUB 0x09
  55386. +#define UDSUBCLASS_HUB 0x00
  55387. +#define UDPROTO_FSHUB 0x00
  55388. +#define UDPROTO_HSHUBSTT 0x01
  55389. +#define UDPROTO_HSHUBMTT 0x02
  55390. +#define UDCLASS_DIAGNOSTIC 0xdc
  55391. +#define UDCLASS_WIRELESS 0xe0
  55392. +#define UDSUBCLASS_RF 0x01
  55393. +#define UDPROTO_BLUETOOTH 0x01
  55394. +#define UDCLASS_VENDOR 0xff
  55395. +
  55396. +/* Interface class codes */
  55397. +#define UICLASS_UNSPEC 0x00
  55398. +
  55399. +#define UICLASS_AUDIO 0x01
  55400. +#define UISUBCLASS_AUDIOCONTROL 1
  55401. +#define UISUBCLASS_AUDIOSTREAM 2
  55402. +#define UISUBCLASS_MIDISTREAM 3
  55403. +
  55404. +#define UICLASS_CDC 0x02 /* communication */
  55405. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  55406. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  55407. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  55408. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  55409. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  55410. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  55411. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  55412. +#define UIPROTO_CDC_AT 1
  55413. +
  55414. +#define UICLASS_HID 0x03
  55415. +#define UISUBCLASS_BOOT 1
  55416. +#define UIPROTO_BOOT_KEYBOARD 1
  55417. +
  55418. +#define UICLASS_PHYSICAL 0x05
  55419. +
  55420. +#define UICLASS_IMAGE 0x06
  55421. +
  55422. +#define UICLASS_PRINTER 0x07
  55423. +#define UISUBCLASS_PRINTER 1
  55424. +#define UIPROTO_PRINTER_UNI 1
  55425. +#define UIPROTO_PRINTER_BI 2
  55426. +#define UIPROTO_PRINTER_1284 3
  55427. +
  55428. +#define UICLASS_MASS 0x08
  55429. +#define UISUBCLASS_RBC 1
  55430. +#define UISUBCLASS_SFF8020I 2
  55431. +#define UISUBCLASS_QIC157 3
  55432. +#define UISUBCLASS_UFI 4
  55433. +#define UISUBCLASS_SFF8070I 5
  55434. +#define UISUBCLASS_SCSI 6
  55435. +#define UIPROTO_MASS_CBI_I 0
  55436. +#define UIPROTO_MASS_CBI 1
  55437. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  55438. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  55439. +
  55440. +#define UICLASS_HUB 0x09
  55441. +#define UISUBCLASS_HUB 0
  55442. +#define UIPROTO_FSHUB 0
  55443. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  55444. +#define UIPROTO_HSHUBMTT 1
  55445. +
  55446. +#define UICLASS_CDC_DATA 0x0a
  55447. +#define UISUBCLASS_DATA 0
  55448. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  55449. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  55450. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  55451. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  55452. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  55453. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  55454. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  55455. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  55456. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  55457. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  55458. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  55459. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  55460. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  55461. +
  55462. +#define UICLASS_SMARTCARD 0x0b
  55463. +
  55464. +/*#define UICLASS_FIRM_UPD 0x0c*/
  55465. +
  55466. +#define UICLASS_SECURITY 0x0d
  55467. +
  55468. +#define UICLASS_DIAGNOSTIC 0xdc
  55469. +
  55470. +#define UICLASS_WIRELESS 0xe0
  55471. +#define UISUBCLASS_RF 0x01
  55472. +#define UIPROTO_BLUETOOTH 0x01
  55473. +
  55474. +#define UICLASS_APPL_SPEC 0xfe
  55475. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  55476. +#define UISUBCLASS_IRDA 2
  55477. +#define UIPROTO_IRDA 0
  55478. +
  55479. +#define UICLASS_VENDOR 0xff
  55480. +
  55481. +#define USB_HUB_MAX_DEPTH 5
  55482. +
  55483. +/*
  55484. + * Minimum time a device needs to be powered down to go through
  55485. + * a power cycle. XXX Are these time in the spec?
  55486. + */
  55487. +#define USB_POWER_DOWN_TIME 200 /* ms */
  55488. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  55489. +
  55490. +#if 0
  55491. +/* These are the values from the spec. */
  55492. +#define USB_PORT_RESET_DELAY 10 /* ms */
  55493. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  55494. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  55495. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  55496. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  55497. +#define USB_RESUME_DELAY (20*5) /* ms */
  55498. +#define USB_RESUME_WAIT 10 /* ms */
  55499. +#define USB_RESUME_RECOVERY 10 /* ms */
  55500. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  55501. +#else
  55502. +/* Allow for marginal (i.e. non-conforming) devices. */
  55503. +#define USB_PORT_RESET_DELAY 50 /* ms */
  55504. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  55505. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  55506. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  55507. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  55508. +#define USB_RESUME_DELAY (50*5) /* ms */
  55509. +#define USB_RESUME_WAIT 50 /* ms */
  55510. +#define USB_RESUME_RECOVERY 50 /* ms */
  55511. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  55512. +#endif
  55513. +
  55514. +#define USB_MIN_POWER 100 /* mA */
  55515. +#define USB_MAX_POWER 500 /* mA */
  55516. +
  55517. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  55518. +
  55519. +#define USB_UNCONFIG_NO 0
  55520. +#define USB_UNCONFIG_INDEX (-1)
  55521. +
  55522. +/*** ioctl() related stuff ***/
  55523. +
  55524. +struct usb_ctl_request {
  55525. + int ucr_addr;
  55526. + usb_device_request_t ucr_request;
  55527. + void *ucr_data;
  55528. + int ucr_flags;
  55529. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  55530. + int ucr_actlen; /* actual length transferred */
  55531. +};
  55532. +
  55533. +struct usb_alt_interface {
  55534. + int uai_config_index;
  55535. + int uai_interface_index;
  55536. + int uai_alt_no;
  55537. +};
  55538. +
  55539. +#define USB_CURRENT_CONFIG_INDEX (-1)
  55540. +#define USB_CURRENT_ALT_INDEX (-1)
  55541. +
  55542. +struct usb_config_desc {
  55543. + int ucd_config_index;
  55544. + usb_config_descriptor_t ucd_desc;
  55545. +};
  55546. +
  55547. +struct usb_interface_desc {
  55548. + int uid_config_index;
  55549. + int uid_interface_index;
  55550. + int uid_alt_index;
  55551. + usb_interface_descriptor_t uid_desc;
  55552. +};
  55553. +
  55554. +struct usb_endpoint_desc {
  55555. + int ued_config_index;
  55556. + int ued_interface_index;
  55557. + int ued_alt_index;
  55558. + int ued_endpoint_index;
  55559. + usb_endpoint_descriptor_t ued_desc;
  55560. +};
  55561. +
  55562. +struct usb_full_desc {
  55563. + int ufd_config_index;
  55564. + u_int ufd_size;
  55565. + u_char *ufd_data;
  55566. +};
  55567. +
  55568. +struct usb_string_desc {
  55569. + int usd_string_index;
  55570. + int usd_language_id;
  55571. + usb_string_descriptor_t usd_desc;
  55572. +};
  55573. +
  55574. +struct usb_ctl_report_desc {
  55575. + int ucrd_size;
  55576. + u_char ucrd_data[1024]; /* filled data size will vary */
  55577. +};
  55578. +
  55579. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  55580. +
  55581. +#define USB_MAX_DEVNAMES 4
  55582. +#define USB_MAX_DEVNAMELEN 16
  55583. +struct usb_device_info {
  55584. + u_int8_t udi_bus;
  55585. + u_int8_t udi_addr; /* device address */
  55586. + usb_event_cookie_t udi_cookie;
  55587. + char udi_product[USB_MAX_STRING_LEN];
  55588. + char udi_vendor[USB_MAX_STRING_LEN];
  55589. + char udi_release[8];
  55590. + u_int16_t udi_productNo;
  55591. + u_int16_t udi_vendorNo;
  55592. + u_int16_t udi_releaseNo;
  55593. + u_int8_t udi_class;
  55594. + u_int8_t udi_subclass;
  55595. + u_int8_t udi_protocol;
  55596. + u_int8_t udi_config;
  55597. + u_int8_t udi_speed;
  55598. +#define USB_SPEED_UNKNOWN 0
  55599. +#define USB_SPEED_LOW 1
  55600. +#define USB_SPEED_FULL 2
  55601. +#define USB_SPEED_HIGH 3
  55602. +#define USB_SPEED_VARIABLE 4
  55603. +#define USB_SPEED_SUPER 5
  55604. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  55605. + int udi_nports;
  55606. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  55607. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  55608. +#define USB_PORT_ENABLED 0xff
  55609. +#define USB_PORT_SUSPENDED 0xfe
  55610. +#define USB_PORT_POWERED 0xfd
  55611. +#define USB_PORT_DISABLED 0xfc
  55612. +};
  55613. +
  55614. +struct usb_ctl_report {
  55615. + int ucr_report;
  55616. + u_char ucr_data[1024]; /* filled data size will vary */
  55617. +};
  55618. +
  55619. +struct usb_device_stats {
  55620. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  55621. +};
  55622. +
  55623. +#define WUSB_MIN_IE 0x80
  55624. +#define WUSB_WCTA_IE 0x80
  55625. +#define WUSB_WCONNECTACK_IE 0x81
  55626. +#define WUSB_WHOSTINFO_IE 0x82
  55627. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  55628. +#define WUHI_CA_RECONN 0x00
  55629. +#define WUHI_CA_LIMITED 0x01
  55630. +#define WUHI_CA_ALL 0x03
  55631. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  55632. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  55633. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  55634. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  55635. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  55636. +#define WUSB_WWORK_IE 0x87
  55637. +#define WUSB_WCHANNEL_STOP_IE 0x88
  55638. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  55639. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  55640. +#define WUSB_WRESETDEVICE_IE 0x8B
  55641. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  55642. +#define WUSB_MAX_IE 0x8C
  55643. +
  55644. +/* Device Notification Types */
  55645. +
  55646. +#define WUSB_DN_MIN 0x01
  55647. +#define WUSB_DN_CONNECT 0x01
  55648. +# define WUSB_DA_OLDCONN 0x00
  55649. +# define WUSB_DA_NEWCONN 0x01
  55650. +# define WUSB_DA_SELF_BEACON 0x02
  55651. +# define WUSB_DA_DIR_BEACON 0x04
  55652. +# define WUSB_DA_NO_BEACON 0x06
  55653. +#define WUSB_DN_DISCONNECT 0x02
  55654. +#define WUSB_DN_EPRDY 0x03
  55655. +#define WUSB_DN_MASAVAILCHANGED 0x04
  55656. +#define WUSB_DN_REMOTEWAKEUP 0x05
  55657. +#define WUSB_DN_SLEEP 0x06
  55658. +#define WUSB_DN_ALIVE 0x07
  55659. +#define WUSB_DN_MAX 0x07
  55660. +
  55661. +#ifdef _MSC_VER
  55662. +#include <pshpack1.h>
  55663. +#endif
  55664. +
  55665. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  55666. +typedef struct wusb_hndshk_data {
  55667. + uByte bMessageNumber;
  55668. + uByte bStatus;
  55669. + uByte tTKID[3];
  55670. + uByte bReserved;
  55671. + uByte CDID[16];
  55672. + uByte Nonce[16];
  55673. + uByte MIC[8];
  55674. +} UPACKED wusb_hndshk_data_t;
  55675. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  55676. +
  55677. +/* WUSB Connection Context */
  55678. +typedef struct wusb_conn_context {
  55679. + uByte CHID [16];
  55680. + uByte CDID [16];
  55681. + uByte CK [16];
  55682. +} UPACKED wusb_conn_context_t;
  55683. +
  55684. +/* WUSB Security Descriptor */
  55685. +typedef struct wusb_security_desc {
  55686. + uByte bLength;
  55687. + uByte bDescriptorType;
  55688. + uWord wTotalLength;
  55689. + uByte bNumEncryptionTypes;
  55690. +} UPACKED wusb_security_desc_t;
  55691. +
  55692. +/* WUSB Encryption Type Descriptor */
  55693. +typedef struct wusb_encrypt_type_desc {
  55694. + uByte bLength;
  55695. + uByte bDescriptorType;
  55696. +
  55697. + uByte bEncryptionType;
  55698. +#define WUETD_UNSECURE 0
  55699. +#define WUETD_WIRED 1
  55700. +#define WUETD_CCM_1 2
  55701. +#define WUETD_RSA_1 3
  55702. +
  55703. + uByte bEncryptionValue;
  55704. + uByte bAuthKeyIndex;
  55705. +} UPACKED wusb_encrypt_type_desc_t;
  55706. +
  55707. +/* WUSB Key Descriptor */
  55708. +typedef struct wusb_key_desc {
  55709. + uByte bLength;
  55710. + uByte bDescriptorType;
  55711. + uByte tTKID[3];
  55712. + uByte bReserved;
  55713. + uByte KeyData[1]; /* variable length */
  55714. +} UPACKED wusb_key_desc_t;
  55715. +
  55716. +/* WUSB BOS Descriptor (Binary device Object Store) */
  55717. +typedef struct wusb_bos_desc {
  55718. + uByte bLength;
  55719. + uByte bDescriptorType;
  55720. + uWord wTotalLength;
  55721. + uByte bNumDeviceCaps;
  55722. +} UPACKED wusb_bos_desc_t;
  55723. +
  55724. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  55725. +typedef struct usb_dev_cap_20_ext_desc {
  55726. + uByte bLength;
  55727. + uByte bDescriptorType;
  55728. + uByte bDevCapabilityType;
  55729. +#define USB_20_EXT_LPM 0x02
  55730. + uDWord bmAttributes;
  55731. +} UPACKED usb_dev_cap_20_ext_desc_t;
  55732. +
  55733. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  55734. +typedef struct usb_dev_cap_ss_usb {
  55735. + uByte bLength;
  55736. + uByte bDescriptorType;
  55737. + uByte bDevCapabilityType;
  55738. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  55739. + uByte bmAttributes;
  55740. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  55741. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  55742. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  55743. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  55744. + uWord wSpeedsSupported;
  55745. + uByte bFunctionalitySupport;
  55746. + uByte bU1DevExitLat;
  55747. + uWord wU2DevExitLat;
  55748. +} UPACKED usb_dev_cap_ss_usb_t;
  55749. +
  55750. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  55751. +typedef struct usb_dev_cap_container_id {
  55752. + uByte bLength;
  55753. + uByte bDescriptorType;
  55754. + uByte bDevCapabilityType;
  55755. + uByte bReserved;
  55756. + uByte containerID[16];
  55757. +} UPACKED usb_dev_cap_container_id_t;
  55758. +
  55759. +/* Device Capability Type Codes */
  55760. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  55761. +
  55762. +/* Device Capability Descriptor */
  55763. +typedef struct wusb_dev_cap_desc {
  55764. + uByte bLength;
  55765. + uByte bDescriptorType;
  55766. + uByte bDevCapabilityType;
  55767. + uByte caps[1]; /* Variable length */
  55768. +} UPACKED wusb_dev_cap_desc_t;
  55769. +
  55770. +/* Device Capability Descriptor */
  55771. +typedef struct wusb_dev_cap_uwb_desc {
  55772. + uByte bLength;
  55773. + uByte bDescriptorType;
  55774. + uByte bDevCapabilityType;
  55775. + uByte bmAttributes;
  55776. + uWord wPHYRates; /* Bitmap */
  55777. + uByte bmTFITXPowerInfo;
  55778. + uByte bmFFITXPowerInfo;
  55779. + uWord bmBandGroup;
  55780. + uByte bReserved;
  55781. +} UPACKED wusb_dev_cap_uwb_desc_t;
  55782. +
  55783. +/* Wireless USB Endpoint Companion Descriptor */
  55784. +typedef struct wusb_endpoint_companion_desc {
  55785. + uByte bLength;
  55786. + uByte bDescriptorType;
  55787. + uByte bMaxBurst;
  55788. + uByte bMaxSequence;
  55789. + uWord wMaxStreamDelay;
  55790. + uWord wOverTheAirPacketSize;
  55791. + uByte bOverTheAirInterval;
  55792. + uByte bmCompAttributes;
  55793. +} UPACKED wusb_endpoint_companion_desc_t;
  55794. +
  55795. +/* Wireless USB Numeric Association M1 Data Structure */
  55796. +typedef struct wusb_m1_data {
  55797. + uByte version;
  55798. + uWord langId;
  55799. + uByte deviceFriendlyNameLength;
  55800. + uByte sha_256_m3[32];
  55801. + uByte deviceFriendlyName[256];
  55802. +} UPACKED wusb_m1_data_t;
  55803. +
  55804. +typedef struct wusb_m2_data {
  55805. + uByte version;
  55806. + uWord langId;
  55807. + uByte hostFriendlyNameLength;
  55808. + uByte pkh[384];
  55809. + uByte hostFriendlyName[256];
  55810. +} UPACKED wusb_m2_data_t;
  55811. +
  55812. +typedef struct wusb_m3_data {
  55813. + uByte pkd[384];
  55814. + uByte nd;
  55815. +} UPACKED wusb_m3_data_t;
  55816. +
  55817. +typedef struct wusb_m4_data {
  55818. + uDWord _attributeTypeIdAndLength_1;
  55819. + uWord associationTypeId;
  55820. +
  55821. + uDWord _attributeTypeIdAndLength_2;
  55822. + uWord associationSubTypeId;
  55823. +
  55824. + uDWord _attributeTypeIdAndLength_3;
  55825. + uDWord length;
  55826. +
  55827. + uDWord _attributeTypeIdAndLength_4;
  55828. + uDWord associationStatus;
  55829. +
  55830. + uDWord _attributeTypeIdAndLength_5;
  55831. + uByte chid[16];
  55832. +
  55833. + uDWord _attributeTypeIdAndLength_6;
  55834. + uByte cdid[16];
  55835. +
  55836. + uDWord _attributeTypeIdAndLength_7;
  55837. + uByte bandGroups[2];
  55838. +} UPACKED wusb_m4_data_t;
  55839. +
  55840. +#ifdef _MSC_VER
  55841. +#include <poppack.h>
  55842. +#endif
  55843. +
  55844. +#ifdef __cplusplus
  55845. +}
  55846. +#endif
  55847. +
  55848. +#endif /* _USB_H_ */
  55849. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/doc/doxygen.cfg linux-3.16-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  55850. --- linux-3.16.2/drivers/usb/host/dwc_otg/doc/doxygen.cfg 1970-01-01 01:00:00.000000000 +0100
  55851. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/doc/doxygen.cfg 2014-09-14 19:04:13.000000000 +0200
  55852. @@ -0,0 +1,224 @@
  55853. +# Doxyfile 1.3.9.1
  55854. +
  55855. +#---------------------------------------------------------------------------
  55856. +# Project related configuration options
  55857. +#---------------------------------------------------------------------------
  55858. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  55859. +PROJECT_NUMBER = v3.00a
  55860. +OUTPUT_DIRECTORY = ./doc/
  55861. +CREATE_SUBDIRS = NO
  55862. +OUTPUT_LANGUAGE = English
  55863. +BRIEF_MEMBER_DESC = YES
  55864. +REPEAT_BRIEF = YES
  55865. +ABBREVIATE_BRIEF = "The $name class" \
  55866. + "The $name widget" \
  55867. + "The $name file" \
  55868. + is \
  55869. + provides \
  55870. + specifies \
  55871. + contains \
  55872. + represents \
  55873. + a \
  55874. + an \
  55875. + the
  55876. +ALWAYS_DETAILED_SEC = NO
  55877. +INLINE_INHERITED_MEMB = NO
  55878. +FULL_PATH_NAMES = NO
  55879. +STRIP_FROM_PATH =
  55880. +STRIP_FROM_INC_PATH =
  55881. +SHORT_NAMES = NO
  55882. +JAVADOC_AUTOBRIEF = YES
  55883. +MULTILINE_CPP_IS_BRIEF = NO
  55884. +INHERIT_DOCS = YES
  55885. +DISTRIBUTE_GROUP_DOC = NO
  55886. +TAB_SIZE = 8
  55887. +ALIASES =
  55888. +OPTIMIZE_OUTPUT_FOR_C = YES
  55889. +OPTIMIZE_OUTPUT_JAVA = NO
  55890. +SUBGROUPING = YES
  55891. +#---------------------------------------------------------------------------
  55892. +# Build related configuration options
  55893. +#---------------------------------------------------------------------------
  55894. +EXTRACT_ALL = NO
  55895. +EXTRACT_PRIVATE = YES
  55896. +EXTRACT_STATIC = YES
  55897. +EXTRACT_LOCAL_CLASSES = YES
  55898. +EXTRACT_LOCAL_METHODS = NO
  55899. +HIDE_UNDOC_MEMBERS = NO
  55900. +HIDE_UNDOC_CLASSES = NO
  55901. +HIDE_FRIEND_COMPOUNDS = NO
  55902. +HIDE_IN_BODY_DOCS = NO
  55903. +INTERNAL_DOCS = NO
  55904. +CASE_SENSE_NAMES = NO
  55905. +HIDE_SCOPE_NAMES = NO
  55906. +SHOW_INCLUDE_FILES = YES
  55907. +INLINE_INFO = YES
  55908. +SORT_MEMBER_DOCS = NO
  55909. +SORT_BRIEF_DOCS = NO
  55910. +SORT_BY_SCOPE_NAME = NO
  55911. +GENERATE_TODOLIST = YES
  55912. +GENERATE_TESTLIST = YES
  55913. +GENERATE_BUGLIST = YES
  55914. +GENERATE_DEPRECATEDLIST= YES
  55915. +ENABLED_SECTIONS =
  55916. +MAX_INITIALIZER_LINES = 30
  55917. +SHOW_USED_FILES = YES
  55918. +SHOW_DIRECTORIES = YES
  55919. +#---------------------------------------------------------------------------
  55920. +# configuration options related to warning and progress messages
  55921. +#---------------------------------------------------------------------------
  55922. +QUIET = YES
  55923. +WARNINGS = YES
  55924. +WARN_IF_UNDOCUMENTED = NO
  55925. +WARN_IF_DOC_ERROR = YES
  55926. +WARN_FORMAT = "$file:$line: $text"
  55927. +WARN_LOGFILE =
  55928. +#---------------------------------------------------------------------------
  55929. +# configuration options related to the input files
  55930. +#---------------------------------------------------------------------------
  55931. +INPUT = .
  55932. +FILE_PATTERNS = *.c \
  55933. + *.h \
  55934. + ./linux/*.c \
  55935. + ./linux/*.h
  55936. +RECURSIVE = NO
  55937. +EXCLUDE = ./test/ \
  55938. + ./dwc_otg/.AppleDouble/
  55939. +EXCLUDE_SYMLINKS = YES
  55940. +EXCLUDE_PATTERNS = *.mod.*
  55941. +EXAMPLE_PATH =
  55942. +EXAMPLE_PATTERNS = *
  55943. +EXAMPLE_RECURSIVE = NO
  55944. +IMAGE_PATH =
  55945. +INPUT_FILTER =
  55946. +FILTER_PATTERNS =
  55947. +FILTER_SOURCE_FILES = NO
  55948. +#---------------------------------------------------------------------------
  55949. +# configuration options related to source browsing
  55950. +#---------------------------------------------------------------------------
  55951. +SOURCE_BROWSER = YES
  55952. +INLINE_SOURCES = NO
  55953. +STRIP_CODE_COMMENTS = YES
  55954. +REFERENCED_BY_RELATION = NO
  55955. +REFERENCES_RELATION = NO
  55956. +VERBATIM_HEADERS = NO
  55957. +#---------------------------------------------------------------------------
  55958. +# configuration options related to the alphabetical class index
  55959. +#---------------------------------------------------------------------------
  55960. +ALPHABETICAL_INDEX = NO
  55961. +COLS_IN_ALPHA_INDEX = 5
  55962. +IGNORE_PREFIX =
  55963. +#---------------------------------------------------------------------------
  55964. +# configuration options related to the HTML output
  55965. +#---------------------------------------------------------------------------
  55966. +GENERATE_HTML = YES
  55967. +HTML_OUTPUT = html
  55968. +HTML_FILE_EXTENSION = .html
  55969. +HTML_HEADER =
  55970. +HTML_FOOTER =
  55971. +HTML_STYLESHEET =
  55972. +HTML_ALIGN_MEMBERS = YES
  55973. +GENERATE_HTMLHELP = NO
  55974. +CHM_FILE =
  55975. +HHC_LOCATION =
  55976. +GENERATE_CHI = NO
  55977. +BINARY_TOC = NO
  55978. +TOC_EXPAND = NO
  55979. +DISABLE_INDEX = NO
  55980. +ENUM_VALUES_PER_LINE = 4
  55981. +GENERATE_TREEVIEW = YES
  55982. +TREEVIEW_WIDTH = 250
  55983. +#---------------------------------------------------------------------------
  55984. +# configuration options related to the LaTeX output
  55985. +#---------------------------------------------------------------------------
  55986. +GENERATE_LATEX = NO
  55987. +LATEX_OUTPUT = latex
  55988. +LATEX_CMD_NAME = latex
  55989. +MAKEINDEX_CMD_NAME = makeindex
  55990. +COMPACT_LATEX = NO
  55991. +PAPER_TYPE = a4wide
  55992. +EXTRA_PACKAGES =
  55993. +LATEX_HEADER =
  55994. +PDF_HYPERLINKS = NO
  55995. +USE_PDFLATEX = NO
  55996. +LATEX_BATCHMODE = NO
  55997. +LATEX_HIDE_INDICES = NO
  55998. +#---------------------------------------------------------------------------
  55999. +# configuration options related to the RTF output
  56000. +#---------------------------------------------------------------------------
  56001. +GENERATE_RTF = NO
  56002. +RTF_OUTPUT = rtf
  56003. +COMPACT_RTF = NO
  56004. +RTF_HYPERLINKS = NO
  56005. +RTF_STYLESHEET_FILE =
  56006. +RTF_EXTENSIONS_FILE =
  56007. +#---------------------------------------------------------------------------
  56008. +# configuration options related to the man page output
  56009. +#---------------------------------------------------------------------------
  56010. +GENERATE_MAN = NO
  56011. +MAN_OUTPUT = man
  56012. +MAN_EXTENSION = .3
  56013. +MAN_LINKS = NO
  56014. +#---------------------------------------------------------------------------
  56015. +# configuration options related to the XML output
  56016. +#---------------------------------------------------------------------------
  56017. +GENERATE_XML = NO
  56018. +XML_OUTPUT = xml
  56019. +XML_SCHEMA =
  56020. +XML_DTD =
  56021. +XML_PROGRAMLISTING = YES
  56022. +#---------------------------------------------------------------------------
  56023. +# configuration options for the AutoGen Definitions output
  56024. +#---------------------------------------------------------------------------
  56025. +GENERATE_AUTOGEN_DEF = NO
  56026. +#---------------------------------------------------------------------------
  56027. +# configuration options related to the Perl module output
  56028. +#---------------------------------------------------------------------------
  56029. +GENERATE_PERLMOD = NO
  56030. +PERLMOD_LATEX = NO
  56031. +PERLMOD_PRETTY = YES
  56032. +PERLMOD_MAKEVAR_PREFIX =
  56033. +#---------------------------------------------------------------------------
  56034. +# Configuration options related to the preprocessor
  56035. +#---------------------------------------------------------------------------
  56036. +ENABLE_PREPROCESSING = YES
  56037. +MACRO_EXPANSION = YES
  56038. +EXPAND_ONLY_PREDEF = YES
  56039. +SEARCH_INCLUDES = YES
  56040. +INCLUDE_PATH =
  56041. +INCLUDE_FILE_PATTERNS =
  56042. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  56043. +EXPAND_AS_DEFINED = DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW DWC_OTG_DEVICE_ATTR_BITFIELD_STORE DWC_OTG_DEVICE_ATTR_BITFIELD_RW DWC_OTG_DEVICE_ATTR_BITFIELD_RO DWC_OTG_DEVICE_ATTR_REG_SHOW DWC_OTG_DEVICE_ATTR_REG_STORE DWC_OTG_DEVICE_ATTR_REG32_RW DWC_OTG_DEVICE_ATTR_REG32_RO DWC_EN_ISOC
  56044. +SKIP_FUNCTION_MACROS = NO
  56045. +#---------------------------------------------------------------------------
  56046. +# Configuration::additions related to external references
  56047. +#---------------------------------------------------------------------------
  56048. +TAGFILES =
  56049. +GENERATE_TAGFILE =
  56050. +ALLEXTERNALS = NO
  56051. +EXTERNAL_GROUPS = YES
  56052. +PERL_PATH = /usr/bin/perl
  56053. +#---------------------------------------------------------------------------
  56054. +# Configuration options related to the dot tool
  56055. +#---------------------------------------------------------------------------
  56056. +CLASS_DIAGRAMS = YES
  56057. +HIDE_UNDOC_RELATIONS = YES
  56058. +HAVE_DOT = NO
  56059. +CLASS_GRAPH = YES
  56060. +COLLABORATION_GRAPH = YES
  56061. +UML_LOOK = NO
  56062. +TEMPLATE_RELATIONS = NO
  56063. +INCLUDE_GRAPH = YES
  56064. +INCLUDED_BY_GRAPH = YES
  56065. +CALL_GRAPH = NO
  56066. +GRAPHICAL_HIERARCHY = YES
  56067. +DOT_IMAGE_FORMAT = png
  56068. +DOT_PATH =
  56069. +DOTFILE_DIRS =
  56070. +MAX_DOT_GRAPH_DEPTH = 1000
  56071. +GENERATE_LEGEND = YES
  56072. +DOT_CLEANUP = YES
  56073. +#---------------------------------------------------------------------------
  56074. +# Configuration::additions related to the search engine
  56075. +#---------------------------------------------------------------------------
  56076. +SEARCHENGINE = NO
  56077. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dummy_audio.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dummy_audio.c
  56078. --- linux-3.16.2/drivers/usb/host/dwc_otg/dummy_audio.c 1970-01-01 01:00:00.000000000 +0100
  56079. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dummy_audio.c 2014-09-14 19:04:13.000000000 +0200
  56080. @@ -0,0 +1,1575 @@
  56081. +/*
  56082. + * zero.c -- Gadget Zero, for USB development
  56083. + *
  56084. + * Copyright (C) 2003-2004 David Brownell
  56085. + * All rights reserved.
  56086. + *
  56087. + * Redistribution and use in source and binary forms, with or without
  56088. + * modification, are permitted provided that the following conditions
  56089. + * are met:
  56090. + * 1. Redistributions of source code must retain the above copyright
  56091. + * notice, this list of conditions, and the following disclaimer,
  56092. + * without modification.
  56093. + * 2. Redistributions in binary form must reproduce the above copyright
  56094. + * notice, this list of conditions and the following disclaimer in the
  56095. + * documentation and/or other materials provided with the distribution.
  56096. + * 3. The names of the above-listed copyright holders may not be used
  56097. + * to endorse or promote products derived from this software without
  56098. + * specific prior written permission.
  56099. + *
  56100. + * ALTERNATIVELY, this software may be distributed under the terms of the
  56101. + * GNU General Public License ("GPL") as published by the Free Software
  56102. + * Foundation, either version 2 of that License or (at your option) any
  56103. + * later version.
  56104. + *
  56105. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  56106. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  56107. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  56108. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  56109. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  56110. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56111. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  56112. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  56113. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  56114. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  56115. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56116. + */
  56117. +
  56118. +
  56119. +/*
  56120. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  56121. + * can write a hardware-agnostic gadget driver running inside a USB device.
  56122. + *
  56123. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  56124. + * affect most of the driver.
  56125. + *
  56126. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  56127. + * functional test of your device-side usb stack, or with "usb-skeleton".
  56128. + *
  56129. + * It supports two similar configurations. One sinks whatever the usb host
  56130. + * writes, and in return sources zeroes. The other loops whatever the host
  56131. + * writes back, so the host can read it. Module options include:
  56132. + *
  56133. + * buflen=N default N=4096, buffer size used
  56134. + * qlen=N default N=32, how many buffers in the loopback queue
  56135. + * loopdefault default false, list loopback config first
  56136. + *
  56137. + * Many drivers will only have one configuration, letting them be much
  56138. + * simpler if they also don't support high speed operation (like this
  56139. + * driver does).
  56140. + */
  56141. +
  56142. +#include <linux/config.h>
  56143. +#include <linux/module.h>
  56144. +#include <linux/kernel.h>
  56145. +#include <linux/delay.h>
  56146. +#include <linux/ioport.h>
  56147. +#include <linux/sched.h>
  56148. +#include <linux/slab.h>
  56149. +#include <linux/smp_lock.h>
  56150. +#include <linux/errno.h>
  56151. +#include <linux/init.h>
  56152. +#include <linux/timer.h>
  56153. +#include <linux/list.h>
  56154. +#include <linux/interrupt.h>
  56155. +#include <linux/uts.h>
  56156. +#include <linux/version.h>
  56157. +#include <linux/device.h>
  56158. +#include <linux/moduleparam.h>
  56159. +#include <linux/proc_fs.h>
  56160. +
  56161. +#include <asm/byteorder.h>
  56162. +#include <asm/io.h>
  56163. +#include <asm/irq.h>
  56164. +#include <asm/system.h>
  56165. +#include <asm/unaligned.h>
  56166. +
  56167. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  56168. +# include <linux/usb/ch9.h>
  56169. +#else
  56170. +# include <linux/usb_ch9.h>
  56171. +#endif
  56172. +
  56173. +#include <linux/usb_gadget.h>
  56174. +
  56175. +
  56176. +/*-------------------------------------------------------------------------*/
  56177. +/*-------------------------------------------------------------------------*/
  56178. +
  56179. +
  56180. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  56181. +{
  56182. + int count = 0;
  56183. + u8 c;
  56184. + u16 uchar;
  56185. +
  56186. + /* this insists on correct encodings, though not minimal ones.
  56187. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  56188. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  56189. + */
  56190. + while (len != 0 && (c = (u8) *s++) != 0) {
  56191. + if (unlikely(c & 0x80)) {
  56192. + // 2-byte sequence:
  56193. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  56194. + if ((c & 0xe0) == 0xc0) {
  56195. + uchar = (c & 0x1f) << 6;
  56196. +
  56197. + c = (u8) *s++;
  56198. + if ((c & 0xc0) != 0xc0)
  56199. + goto fail;
  56200. + c &= 0x3f;
  56201. + uchar |= c;
  56202. +
  56203. + // 3-byte sequence (most CJKV characters):
  56204. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  56205. + } else if ((c & 0xf0) == 0xe0) {
  56206. + uchar = (c & 0x0f) << 12;
  56207. +
  56208. + c = (u8) *s++;
  56209. + if ((c & 0xc0) != 0xc0)
  56210. + goto fail;
  56211. + c &= 0x3f;
  56212. + uchar |= c << 6;
  56213. +
  56214. + c = (u8) *s++;
  56215. + if ((c & 0xc0) != 0xc0)
  56216. + goto fail;
  56217. + c &= 0x3f;
  56218. + uchar |= c;
  56219. +
  56220. + /* no bogus surrogates */
  56221. + if (0xd800 <= uchar && uchar <= 0xdfff)
  56222. + goto fail;
  56223. +
  56224. + // 4-byte sequence (surrogate pairs, currently rare):
  56225. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  56226. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  56227. + // (uuuuu = wwww + 1)
  56228. + // FIXME accept the surrogate code points (only)
  56229. +
  56230. + } else
  56231. + goto fail;
  56232. + } else
  56233. + uchar = c;
  56234. + put_unaligned (cpu_to_le16 (uchar), cp++);
  56235. + count++;
  56236. + len--;
  56237. + }
  56238. + return count;
  56239. +fail:
  56240. + return -1;
  56241. +}
  56242. +
  56243. +
  56244. +/**
  56245. + * usb_gadget_get_string - fill out a string descriptor
  56246. + * @table: of c strings encoded using UTF-8
  56247. + * @id: string id, from low byte of wValue in get string descriptor
  56248. + * @buf: at least 256 bytes
  56249. + *
  56250. + * Finds the UTF-8 string matching the ID, and converts it into a
  56251. + * string descriptor in utf16-le.
  56252. + * Returns length of descriptor (always even) or negative errno
  56253. + *
  56254. + * If your driver needs stings in multiple languages, you'll probably
  56255. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  56256. + * using this routine after choosing which set of UTF-8 strings to use.
  56257. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  56258. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  56259. + * characters (which are also widely used in C strings).
  56260. + */
  56261. +int
  56262. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  56263. +{
  56264. + struct usb_string *s;
  56265. + int len;
  56266. +
  56267. + /* descriptor 0 has the language id */
  56268. + if (id == 0) {
  56269. + buf [0] = 4;
  56270. + buf [1] = USB_DT_STRING;
  56271. + buf [2] = (u8) table->language;
  56272. + buf [3] = (u8) (table->language >> 8);
  56273. + return 4;
  56274. + }
  56275. + for (s = table->strings; s && s->s; s++)
  56276. + if (s->id == id)
  56277. + break;
  56278. +
  56279. + /* unrecognized: stall. */
  56280. + if (!s || !s->s)
  56281. + return -EINVAL;
  56282. +
  56283. + /* string descriptors have length, tag, then UTF16-LE text */
  56284. + len = min ((size_t) 126, strlen (s->s));
  56285. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  56286. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  56287. + if (len < 0)
  56288. + return -EINVAL;
  56289. + buf [0] = (len + 1) * 2;
  56290. + buf [1] = USB_DT_STRING;
  56291. + return buf [0];
  56292. +}
  56293. +
  56294. +
  56295. +/*-------------------------------------------------------------------------*/
  56296. +/*-------------------------------------------------------------------------*/
  56297. +
  56298. +
  56299. +/**
  56300. + * usb_descriptor_fillbuf - fill buffer with descriptors
  56301. + * @buf: Buffer to be filled
  56302. + * @buflen: Size of buf
  56303. + * @src: Array of descriptor pointers, terminated by null pointer.
  56304. + *
  56305. + * Copies descriptors into the buffer, returning the length or a
  56306. + * negative error code if they can't all be copied. Useful when
  56307. + * assembling descriptors for an associated set of interfaces used
  56308. + * as part of configuring a composite device; or in other cases where
  56309. + * sets of descriptors need to be marshaled.
  56310. + */
  56311. +int
  56312. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  56313. + const struct usb_descriptor_header **src)
  56314. +{
  56315. + u8 *dest = buf;
  56316. +
  56317. + if (!src)
  56318. + return -EINVAL;
  56319. +
  56320. + /* fill buffer from src[] until null descriptor ptr */
  56321. + for (; 0 != *src; src++) {
  56322. + unsigned len = (*src)->bLength;
  56323. +
  56324. + if (len > buflen)
  56325. + return -EINVAL;
  56326. + memcpy(dest, *src, len);
  56327. + buflen -= len;
  56328. + dest += len;
  56329. + }
  56330. + return dest - (u8 *)buf;
  56331. +}
  56332. +
  56333. +
  56334. +/**
  56335. + * usb_gadget_config_buf - builts a complete configuration descriptor
  56336. + * @config: Header for the descriptor, including characteristics such
  56337. + * as power requirements and number of interfaces.
  56338. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  56339. + * endpoint, etc) defining all functions in this device configuration.
  56340. + * @buf: Buffer for the resulting configuration descriptor.
  56341. + * @length: Length of buffer. If this is not big enough to hold the
  56342. + * entire configuration descriptor, an error code will be returned.
  56343. + *
  56344. + * This copies descriptors into the response buffer, building a descriptor
  56345. + * for that configuration. It returns the buffer length or a negative
  56346. + * status code. The config.wTotalLength field is set to match the length
  56347. + * of the result, but other descriptor fields (including power usage and
  56348. + * interface count) must be set by the caller.
  56349. + *
  56350. + * Gadget drivers could use this when constructing a config descriptor
  56351. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  56352. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  56353. + */
  56354. +int usb_gadget_config_buf(
  56355. + const struct usb_config_descriptor *config,
  56356. + void *buf,
  56357. + unsigned length,
  56358. + const struct usb_descriptor_header **desc
  56359. +)
  56360. +{
  56361. + struct usb_config_descriptor *cp = buf;
  56362. + int len;
  56363. +
  56364. + /* config descriptor first */
  56365. + if (length < USB_DT_CONFIG_SIZE || !desc)
  56366. + return -EINVAL;
  56367. + *cp = *config;
  56368. +
  56369. + /* then interface/endpoint/class/vendor/... */
  56370. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  56371. + length - USB_DT_CONFIG_SIZE, desc);
  56372. + if (len < 0)
  56373. + return len;
  56374. + len += USB_DT_CONFIG_SIZE;
  56375. + if (len > 0xffff)
  56376. + return -EINVAL;
  56377. +
  56378. + /* patch up the config descriptor */
  56379. + cp->bLength = USB_DT_CONFIG_SIZE;
  56380. + cp->bDescriptorType = USB_DT_CONFIG;
  56381. + cp->wTotalLength = cpu_to_le16(len);
  56382. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  56383. + return len;
  56384. +}
  56385. +
  56386. +/*-------------------------------------------------------------------------*/
  56387. +/*-------------------------------------------------------------------------*/
  56388. +
  56389. +
  56390. +#define RBUF_LEN (1024*1024)
  56391. +static int rbuf_start;
  56392. +static int rbuf_len;
  56393. +static __u8 rbuf[RBUF_LEN];
  56394. +
  56395. +/*-------------------------------------------------------------------------*/
  56396. +
  56397. +#define DRIVER_VERSION "St Patrick's Day 2004"
  56398. +
  56399. +static const char shortname [] = "zero";
  56400. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  56401. +
  56402. +static const char source_sink [] = "source and sink data";
  56403. +static const char loopback [] = "loop input to output";
  56404. +
  56405. +/*-------------------------------------------------------------------------*/
  56406. +
  56407. +/*
  56408. + * driver assumes self-powered hardware, and
  56409. + * has no way for users to trigger remote wakeup.
  56410. + *
  56411. + * this version autoconfigures as much as possible,
  56412. + * which is reasonable for most "bulk-only" drivers.
  56413. + */
  56414. +static const char *EP_IN_NAME; /* source */
  56415. +static const char *EP_OUT_NAME; /* sink */
  56416. +
  56417. +/*-------------------------------------------------------------------------*/
  56418. +
  56419. +/* big enough to hold our biggest descriptor */
  56420. +#define USB_BUFSIZ 512
  56421. +
  56422. +struct zero_dev {
  56423. + spinlock_t lock;
  56424. + struct usb_gadget *gadget;
  56425. + struct usb_request *req; /* for control responses */
  56426. +
  56427. + /* when configured, we have one of two configs:
  56428. + * - source data (in to host) and sink it (out from host)
  56429. + * - or loop it back (out from host back in to host)
  56430. + */
  56431. + u8 config;
  56432. + struct usb_ep *in_ep, *out_ep;
  56433. +
  56434. + /* autoresume timer */
  56435. + struct timer_list resume;
  56436. +};
  56437. +
  56438. +#define xprintk(d,level,fmt,args...) \
  56439. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  56440. +
  56441. +#ifdef DEBUG
  56442. +#define DBG(dev,fmt,args...) \
  56443. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  56444. +#else
  56445. +#define DBG(dev,fmt,args...) \
  56446. + do { } while (0)
  56447. +#endif /* DEBUG */
  56448. +
  56449. +#ifdef VERBOSE
  56450. +#define VDBG DBG
  56451. +#else
  56452. +#define VDBG(dev,fmt,args...) \
  56453. + do { } while (0)
  56454. +#endif /* VERBOSE */
  56455. +
  56456. +#define ERROR(dev,fmt,args...) \
  56457. + xprintk(dev , KERN_ERR , fmt , ## args)
  56458. +#define WARN(dev,fmt,args...) \
  56459. + xprintk(dev , KERN_WARNING , fmt , ## args)
  56460. +#define INFO(dev,fmt,args...) \
  56461. + xprintk(dev , KERN_INFO , fmt , ## args)
  56462. +
  56463. +/*-------------------------------------------------------------------------*/
  56464. +
  56465. +static unsigned buflen = 4096;
  56466. +static unsigned qlen = 32;
  56467. +static unsigned pattern = 0;
  56468. +
  56469. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  56470. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  56471. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  56472. +
  56473. +/*
  56474. + * if it's nonzero, autoresume says how many seconds to wait
  56475. + * before trying to wake up the host after suspend.
  56476. + */
  56477. +static unsigned autoresume = 0;
  56478. +module_param (autoresume, uint, 0);
  56479. +
  56480. +/*
  56481. + * Normally the "loopback" configuration is second (index 1) so
  56482. + * it's not the default. Here's where to change that order, to
  56483. + * work better with hosts where config changes are problematic.
  56484. + * Or controllers (like superh) that only support one config.
  56485. + */
  56486. +static int loopdefault = 0;
  56487. +
  56488. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  56489. +
  56490. +/*-------------------------------------------------------------------------*/
  56491. +
  56492. +/* Thanks to NetChip Technologies for donating this product ID.
  56493. + *
  56494. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  56495. + * Instead: allocate your own, using normal USB-IF procedures.
  56496. + */
  56497. +#ifndef CONFIG_USB_ZERO_HNPTEST
  56498. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  56499. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  56500. +#else
  56501. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  56502. +#define DRIVER_PRODUCT_NUM 0xbadd
  56503. +#endif
  56504. +
  56505. +/*-------------------------------------------------------------------------*/
  56506. +
  56507. +/*
  56508. + * DESCRIPTORS ... most are static, but strings and (full)
  56509. + * configuration descriptors are built on demand.
  56510. + */
  56511. +
  56512. +/*
  56513. +#define STRING_MANUFACTURER 25
  56514. +#define STRING_PRODUCT 42
  56515. +#define STRING_SERIAL 101
  56516. +*/
  56517. +#define STRING_MANUFACTURER 1
  56518. +#define STRING_PRODUCT 2
  56519. +#define STRING_SERIAL 3
  56520. +
  56521. +#define STRING_SOURCE_SINK 250
  56522. +#define STRING_LOOPBACK 251
  56523. +
  56524. +/*
  56525. + * This device advertises two configurations; these numbers work
  56526. + * on a pxa250 as well as more flexible hardware.
  56527. + */
  56528. +#define CONFIG_SOURCE_SINK 3
  56529. +#define CONFIG_LOOPBACK 2
  56530. +
  56531. +/*
  56532. +static struct usb_device_descriptor
  56533. +device_desc = {
  56534. + .bLength = sizeof device_desc,
  56535. + .bDescriptorType = USB_DT_DEVICE,
  56536. +
  56537. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  56538. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  56539. +
  56540. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  56541. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  56542. + .iManufacturer = STRING_MANUFACTURER,
  56543. + .iProduct = STRING_PRODUCT,
  56544. + .iSerialNumber = STRING_SERIAL,
  56545. + .bNumConfigurations = 2,
  56546. +};
  56547. +*/
  56548. +static struct usb_device_descriptor
  56549. +device_desc = {
  56550. + .bLength = sizeof device_desc,
  56551. + .bDescriptorType = USB_DT_DEVICE,
  56552. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  56553. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  56554. + .bDeviceSubClass = 0,
  56555. + .bDeviceProtocol = 0,
  56556. + .bMaxPacketSize0 = 64,
  56557. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  56558. + .idVendor = __constant_cpu_to_le16 (0x0499),
  56559. + .idProduct = __constant_cpu_to_le16 (0x3002),
  56560. + .iManufacturer = STRING_MANUFACTURER,
  56561. + .iProduct = STRING_PRODUCT,
  56562. + .iSerialNumber = STRING_SERIAL,
  56563. + .bNumConfigurations = 1,
  56564. +};
  56565. +
  56566. +static struct usb_config_descriptor
  56567. +z_config = {
  56568. + .bLength = sizeof z_config,
  56569. + .bDescriptorType = USB_DT_CONFIG,
  56570. +
  56571. + /* compute wTotalLength on the fly */
  56572. + .bNumInterfaces = 2,
  56573. + .bConfigurationValue = 1,
  56574. + .iConfiguration = 0,
  56575. + .bmAttributes = 0x40,
  56576. + .bMaxPower = 0, /* self-powered */
  56577. +};
  56578. +
  56579. +
  56580. +static struct usb_otg_descriptor
  56581. +otg_descriptor = {
  56582. + .bLength = sizeof otg_descriptor,
  56583. + .bDescriptorType = USB_DT_OTG,
  56584. +
  56585. + .bmAttributes = USB_OTG_SRP,
  56586. +};
  56587. +
  56588. +/* one interface in each configuration */
  56589. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  56590. +
  56591. +/*
  56592. + * usb 2.0 devices need to expose both high speed and full speed
  56593. + * descriptors, unless they only run at full speed.
  56594. + *
  56595. + * that means alternate endpoint descriptors (bigger packets)
  56596. + * and a "device qualifier" ... plus more construction options
  56597. + * for the config descriptor.
  56598. + */
  56599. +
  56600. +static struct usb_qualifier_descriptor
  56601. +dev_qualifier = {
  56602. + .bLength = sizeof dev_qualifier,
  56603. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  56604. +
  56605. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  56606. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  56607. +
  56608. + .bNumConfigurations = 2,
  56609. +};
  56610. +
  56611. +
  56612. +struct usb_cs_as_general_descriptor {
  56613. + __u8 bLength;
  56614. + __u8 bDescriptorType;
  56615. +
  56616. + __u8 bDescriptorSubType;
  56617. + __u8 bTerminalLink;
  56618. + __u8 bDelay;
  56619. + __u16 wFormatTag;
  56620. +} __attribute__ ((packed));
  56621. +
  56622. +struct usb_cs_as_format_descriptor {
  56623. + __u8 bLength;
  56624. + __u8 bDescriptorType;
  56625. +
  56626. + __u8 bDescriptorSubType;
  56627. + __u8 bFormatType;
  56628. + __u8 bNrChannels;
  56629. + __u8 bSubframeSize;
  56630. + __u8 bBitResolution;
  56631. + __u8 bSamfreqType;
  56632. + __u8 tLowerSamFreq[3];
  56633. + __u8 tUpperSamFreq[3];
  56634. +} __attribute__ ((packed));
  56635. +
  56636. +static const struct usb_interface_descriptor
  56637. +z_audio_control_if_desc = {
  56638. + .bLength = sizeof z_audio_control_if_desc,
  56639. + .bDescriptorType = USB_DT_INTERFACE,
  56640. + .bInterfaceNumber = 0,
  56641. + .bAlternateSetting = 0,
  56642. + .bNumEndpoints = 0,
  56643. + .bInterfaceClass = USB_CLASS_AUDIO,
  56644. + .bInterfaceSubClass = 0x1,
  56645. + .bInterfaceProtocol = 0,
  56646. + .iInterface = 0,
  56647. +};
  56648. +
  56649. +static const struct usb_interface_descriptor
  56650. +z_audio_if_desc = {
  56651. + .bLength = sizeof z_audio_if_desc,
  56652. + .bDescriptorType = USB_DT_INTERFACE,
  56653. + .bInterfaceNumber = 1,
  56654. + .bAlternateSetting = 0,
  56655. + .bNumEndpoints = 0,
  56656. + .bInterfaceClass = USB_CLASS_AUDIO,
  56657. + .bInterfaceSubClass = 0x2,
  56658. + .bInterfaceProtocol = 0,
  56659. + .iInterface = 0,
  56660. +};
  56661. +
  56662. +static const struct usb_interface_descriptor
  56663. +z_audio_if_desc2 = {
  56664. + .bLength = sizeof z_audio_if_desc,
  56665. + .bDescriptorType = USB_DT_INTERFACE,
  56666. + .bInterfaceNumber = 1,
  56667. + .bAlternateSetting = 1,
  56668. + .bNumEndpoints = 1,
  56669. + .bInterfaceClass = USB_CLASS_AUDIO,
  56670. + .bInterfaceSubClass = 0x2,
  56671. + .bInterfaceProtocol = 0,
  56672. + .iInterface = 0,
  56673. +};
  56674. +
  56675. +static const struct usb_cs_as_general_descriptor
  56676. +z_audio_cs_as_if_desc = {
  56677. + .bLength = 7,
  56678. + .bDescriptorType = 0x24,
  56679. +
  56680. + .bDescriptorSubType = 0x01,
  56681. + .bTerminalLink = 0x01,
  56682. + .bDelay = 0x0,
  56683. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  56684. +};
  56685. +
  56686. +
  56687. +static const struct usb_cs_as_format_descriptor
  56688. +z_audio_cs_as_format_desc = {
  56689. + .bLength = 0xe,
  56690. + .bDescriptorType = 0x24,
  56691. +
  56692. + .bDescriptorSubType = 2,
  56693. + .bFormatType = 1,
  56694. + .bNrChannels = 1,
  56695. + .bSubframeSize = 1,
  56696. + .bBitResolution = 8,
  56697. + .bSamfreqType = 0,
  56698. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  56699. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  56700. +};
  56701. +
  56702. +static const struct usb_endpoint_descriptor
  56703. +z_iso_ep = {
  56704. + .bLength = 0x09,
  56705. + .bDescriptorType = 0x05,
  56706. + .bEndpointAddress = 0x04,
  56707. + .bmAttributes = 0x09,
  56708. + .wMaxPacketSize = 0x0038,
  56709. + .bInterval = 0x01,
  56710. + .bRefresh = 0x00,
  56711. + .bSynchAddress = 0x00,
  56712. +};
  56713. +
  56714. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  56715. +
  56716. +// 9 bytes
  56717. +static char z_ac_interface_header_desc[] =
  56718. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  56719. +
  56720. +// 12 bytes
  56721. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  56722. + 0x03, 0x00, 0x00, 0x00};
  56723. +// 13 bytes
  56724. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  56725. + 0x02, 0x00, 0x02, 0x00, 0x00};
  56726. +// 9 bytes
  56727. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  56728. + 0x00};
  56729. +
  56730. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  56731. + 0x00};
  56732. +
  56733. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  56734. +
  56735. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  56736. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  56737. +
  56738. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  56739. + 0x00};
  56740. +
  56741. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  56742. +
  56743. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  56744. + 0x00};
  56745. +
  56746. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  56747. +
  56748. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  56749. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  56750. +
  56751. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  56752. + 0x00};
  56753. +
  56754. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  56755. +
  56756. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  56757. + 0x00};
  56758. +
  56759. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  56760. +
  56761. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  56762. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  56763. +
  56764. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  56765. + 0x00};
  56766. +
  56767. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  56768. +
  56769. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  56770. + 0x00};
  56771. +
  56772. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  56773. +
  56774. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  56775. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  56776. +
  56777. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  56778. + 0x00};
  56779. +
  56780. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  56781. +
  56782. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  56783. + 0x00};
  56784. +
  56785. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  56786. +
  56787. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  56788. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  56789. +
  56790. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  56791. + 0x00};
  56792. +
  56793. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  56794. +
  56795. +
  56796. +
  56797. +static const struct usb_descriptor_header *z_function [] = {
  56798. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  56799. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  56800. + (struct usb_descriptor_header *) &z_0,
  56801. + (struct usb_descriptor_header *) &z_1,
  56802. + (struct usb_descriptor_header *) &z_2,
  56803. + (struct usb_descriptor_header *) &z_audio_if_desc,
  56804. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  56805. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  56806. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  56807. + (struct usb_descriptor_header *) &z_iso_ep,
  56808. + (struct usb_descriptor_header *) &z_iso_ep2,
  56809. + (struct usb_descriptor_header *) &za_0,
  56810. + (struct usb_descriptor_header *) &za_1,
  56811. + (struct usb_descriptor_header *) &za_2,
  56812. + (struct usb_descriptor_header *) &za_3,
  56813. + (struct usb_descriptor_header *) &za_4,
  56814. + (struct usb_descriptor_header *) &za_5,
  56815. + (struct usb_descriptor_header *) &za_6,
  56816. + (struct usb_descriptor_header *) &za_7,
  56817. + (struct usb_descriptor_header *) &za_8,
  56818. + (struct usb_descriptor_header *) &za_9,
  56819. + (struct usb_descriptor_header *) &za_10,
  56820. + (struct usb_descriptor_header *) &za_11,
  56821. + (struct usb_descriptor_header *) &za_12,
  56822. + (struct usb_descriptor_header *) &za_13,
  56823. + (struct usb_descriptor_header *) &za_14,
  56824. + (struct usb_descriptor_header *) &za_15,
  56825. + (struct usb_descriptor_header *) &za_16,
  56826. + (struct usb_descriptor_header *) &za_17,
  56827. + (struct usb_descriptor_header *) &za_18,
  56828. + (struct usb_descriptor_header *) &za_19,
  56829. + (struct usb_descriptor_header *) &za_20,
  56830. + (struct usb_descriptor_header *) &za_21,
  56831. + (struct usb_descriptor_header *) &za_22,
  56832. + (struct usb_descriptor_header *) &za_23,
  56833. + (struct usb_descriptor_header *) &za_24,
  56834. + NULL,
  56835. +};
  56836. +
  56837. +/* maxpacket and other transfer characteristics vary by speed. */
  56838. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  56839. +
  56840. +#else
  56841. +
  56842. +/* if there's no high speed support, maxpacket doesn't change. */
  56843. +#define ep_desc(g,hs,fs) fs
  56844. +
  56845. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  56846. +
  56847. +static char manufacturer [40];
  56848. +//static char serial [40];
  56849. +static char serial [] = "Ser 00 em";
  56850. +
  56851. +/* static strings, in UTF-8 */
  56852. +static struct usb_string strings [] = {
  56853. + { STRING_MANUFACTURER, manufacturer, },
  56854. + { STRING_PRODUCT, longname, },
  56855. + { STRING_SERIAL, serial, },
  56856. + { STRING_LOOPBACK, loopback, },
  56857. + { STRING_SOURCE_SINK, source_sink, },
  56858. + { } /* end of list */
  56859. +};
  56860. +
  56861. +static struct usb_gadget_strings stringtab = {
  56862. + .language = 0x0409, /* en-us */
  56863. + .strings = strings,
  56864. +};
  56865. +
  56866. +/*
  56867. + * config descriptors are also handcrafted. these must agree with code
  56868. + * that sets configurations, and with code managing interfaces and their
  56869. + * altsettings. other complexity may come from:
  56870. + *
  56871. + * - high speed support, including "other speed config" rules
  56872. + * - multiple configurations
  56873. + * - interfaces with alternate settings
  56874. + * - embedded class or vendor-specific descriptors
  56875. + *
  56876. + * this handles high speed, and has a second config that could as easily
  56877. + * have been an alternate interface setting (on most hardware).
  56878. + *
  56879. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  56880. + * should include an altsetting to test interrupt transfers, including
  56881. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  56882. + * device?)
  56883. + */
  56884. +static int
  56885. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  56886. +{
  56887. + int len;
  56888. + const struct usb_descriptor_header **function;
  56889. +
  56890. + function = z_function;
  56891. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  56892. + if (len < 0)
  56893. + return len;
  56894. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  56895. + return len;
  56896. +}
  56897. +
  56898. +/*-------------------------------------------------------------------------*/
  56899. +
  56900. +static struct usb_request *
  56901. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  56902. +{
  56903. + struct usb_request *req;
  56904. +
  56905. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  56906. + if (req) {
  56907. + req->length = length;
  56908. + req->buf = usb_ep_alloc_buffer (ep, length,
  56909. + &req->dma, GFP_ATOMIC);
  56910. + if (!req->buf) {
  56911. + usb_ep_free_request (ep, req);
  56912. + req = NULL;
  56913. + }
  56914. + }
  56915. + return req;
  56916. +}
  56917. +
  56918. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  56919. +{
  56920. + if (req->buf)
  56921. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  56922. + usb_ep_free_request (ep, req);
  56923. +}
  56924. +
  56925. +/*-------------------------------------------------------------------------*/
  56926. +
  56927. +/* optionally require specific source/sink data patterns */
  56928. +
  56929. +static int
  56930. +check_read_data (
  56931. + struct zero_dev *dev,
  56932. + struct usb_ep *ep,
  56933. + struct usb_request *req
  56934. +)
  56935. +{
  56936. + unsigned i;
  56937. + u8 *buf = req->buf;
  56938. +
  56939. + for (i = 0; i < req->actual; i++, buf++) {
  56940. + switch (pattern) {
  56941. + /* all-zeroes has no synchronization issues */
  56942. + case 0:
  56943. + if (*buf == 0)
  56944. + continue;
  56945. + break;
  56946. + /* mod63 stays in sync with short-terminated transfers,
  56947. + * or otherwise when host and gadget agree on how large
  56948. + * each usb transfer request should be. resync is done
  56949. + * with set_interface or set_config.
  56950. + */
  56951. + case 1:
  56952. + if (*buf == (u8)(i % 63))
  56953. + continue;
  56954. + break;
  56955. + }
  56956. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  56957. + usb_ep_set_halt (ep);
  56958. + return -EINVAL;
  56959. + }
  56960. + return 0;
  56961. +}
  56962. +
  56963. +/*-------------------------------------------------------------------------*/
  56964. +
  56965. +static void zero_reset_config (struct zero_dev *dev)
  56966. +{
  56967. + if (dev->config == 0)
  56968. + return;
  56969. +
  56970. + DBG (dev, "reset config\n");
  56971. +
  56972. + /* just disable endpoints, forcing completion of pending i/o.
  56973. + * all our completion handlers free their requests in this case.
  56974. + */
  56975. + if (dev->in_ep) {
  56976. + usb_ep_disable (dev->in_ep);
  56977. + dev->in_ep = NULL;
  56978. + }
  56979. + if (dev->out_ep) {
  56980. + usb_ep_disable (dev->out_ep);
  56981. + dev->out_ep = NULL;
  56982. + }
  56983. + dev->config = 0;
  56984. + del_timer (&dev->resume);
  56985. +}
  56986. +
  56987. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  56988. +
  56989. +static void
  56990. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  56991. +{
  56992. + struct zero_dev *dev = ep->driver_data;
  56993. + int status = req->status;
  56994. + int i, j;
  56995. +
  56996. + switch (status) {
  56997. +
  56998. + case 0: /* normal completion? */
  56999. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  57000. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  57001. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  57002. + rbuf[j] = ((__u8*)req->buf)[i];
  57003. + j++;
  57004. + if (j >= RBUF_LEN) j=0;
  57005. + }
  57006. + rbuf_start = j;
  57007. + //printk ("\n\n");
  57008. +
  57009. + if (rbuf_len < RBUF_LEN) {
  57010. + rbuf_len += req->actual;
  57011. + if (rbuf_len > RBUF_LEN) {
  57012. + rbuf_len = RBUF_LEN;
  57013. + }
  57014. + }
  57015. +
  57016. + break;
  57017. +
  57018. + /* this endpoint is normally active while we're configured */
  57019. + case -ECONNABORTED: /* hardware forced ep reset */
  57020. + case -ECONNRESET: /* request dequeued */
  57021. + case -ESHUTDOWN: /* disconnect from host */
  57022. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  57023. + req->actual, req->length);
  57024. + if (ep == dev->out_ep)
  57025. + check_read_data (dev, ep, req);
  57026. + free_ep_req (ep, req);
  57027. + return;
  57028. +
  57029. + case -EOVERFLOW: /* buffer overrun on read means that
  57030. + * we didn't provide a big enough
  57031. + * buffer.
  57032. + */
  57033. + default:
  57034. +#if 1
  57035. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  57036. + status, req->actual, req->length);
  57037. +#endif
  57038. + case -EREMOTEIO: /* short read */
  57039. + break;
  57040. + }
  57041. +
  57042. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  57043. + if (status) {
  57044. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  57045. + ep->name, req->length, status);
  57046. + usb_ep_set_halt (ep);
  57047. + /* FIXME recover later ... somehow */
  57048. + }
  57049. +}
  57050. +
  57051. +static struct usb_request *
  57052. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  57053. +{
  57054. + struct usb_request *req;
  57055. + int status;
  57056. +
  57057. + req = alloc_ep_req (ep, 512);
  57058. + if (!req)
  57059. + return NULL;
  57060. +
  57061. + req->complete = zero_isoc_complete;
  57062. +
  57063. + status = usb_ep_queue (ep, req, gfp_flags);
  57064. + if (status) {
  57065. + struct zero_dev *dev = ep->driver_data;
  57066. +
  57067. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  57068. + free_ep_req (ep, req);
  57069. + req = NULL;
  57070. + }
  57071. +
  57072. + return req;
  57073. +}
  57074. +
  57075. +/* change our operational config. this code must agree with the code
  57076. + * that returns config descriptors, and altsetting code.
  57077. + *
  57078. + * it's also responsible for power management interactions. some
  57079. + * configurations might not work with our current power sources.
  57080. + *
  57081. + * note that some device controller hardware will constrain what this
  57082. + * code can do, perhaps by disallowing more than one configuration or
  57083. + * by limiting configuration choices (like the pxa2xx).
  57084. + */
  57085. +static int
  57086. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  57087. +{
  57088. + int result = 0;
  57089. + struct usb_gadget *gadget = dev->gadget;
  57090. + const struct usb_endpoint_descriptor *d;
  57091. + struct usb_ep *ep;
  57092. +
  57093. + if (number == dev->config)
  57094. + return 0;
  57095. +
  57096. + zero_reset_config (dev);
  57097. +
  57098. + gadget_for_each_ep (ep, gadget) {
  57099. +
  57100. + if (strcmp (ep->name, "ep4") == 0) {
  57101. +
  57102. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  57103. + result = usb_ep_enable (ep, d);
  57104. +
  57105. + if (result == 0) {
  57106. + ep->driver_data = dev;
  57107. + dev->in_ep = ep;
  57108. +
  57109. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  57110. +
  57111. + dev->in_ep = ep;
  57112. + continue;
  57113. + }
  57114. +
  57115. + usb_ep_disable (ep);
  57116. + result = -EIO;
  57117. + }
  57118. + }
  57119. +
  57120. + }
  57121. +
  57122. + dev->config = number;
  57123. + return result;
  57124. +}
  57125. +
  57126. +/*-------------------------------------------------------------------------*/
  57127. +
  57128. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  57129. +{
  57130. + if (req->status || req->actual != req->length)
  57131. + DBG ((struct zero_dev *) ep->driver_data,
  57132. + "setup complete --> %d, %d/%d\n",
  57133. + req->status, req->actual, req->length);
  57134. +}
  57135. +
  57136. +/*
  57137. + * The setup() callback implements all the ep0 functionality that's
  57138. + * not handled lower down, in hardware or the hardware driver (like
  57139. + * device and endpoint feature flags, and their status). It's all
  57140. + * housekeeping for the gadget function we're implementing. Most of
  57141. + * the work is in config-specific setup.
  57142. + */
  57143. +static int
  57144. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  57145. +{
  57146. + struct zero_dev *dev = get_gadget_data (gadget);
  57147. + struct usb_request *req = dev->req;
  57148. + int value = -EOPNOTSUPP;
  57149. +
  57150. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  57151. + * but config change events will reconfigure hardware.
  57152. + */
  57153. + req->zero = 0;
  57154. + switch (ctrl->bRequest) {
  57155. +
  57156. + case USB_REQ_GET_DESCRIPTOR:
  57157. +
  57158. + switch (ctrl->wValue >> 8) {
  57159. +
  57160. + case USB_DT_DEVICE:
  57161. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  57162. + memcpy (req->buf, &device_desc, value);
  57163. + break;
  57164. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  57165. + case USB_DT_DEVICE_QUALIFIER:
  57166. + if (!gadget->is_dualspeed)
  57167. + break;
  57168. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  57169. + memcpy (req->buf, &dev_qualifier, value);
  57170. + break;
  57171. +
  57172. + case USB_DT_OTHER_SPEED_CONFIG:
  57173. + if (!gadget->is_dualspeed)
  57174. + break;
  57175. + // FALLTHROUGH
  57176. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  57177. + case USB_DT_CONFIG:
  57178. + value = config_buf (gadget, req->buf,
  57179. + ctrl->wValue >> 8,
  57180. + ctrl->wValue & 0xff);
  57181. + if (value >= 0)
  57182. + value = min (ctrl->wLength, (u16) value);
  57183. + break;
  57184. +
  57185. + case USB_DT_STRING:
  57186. + /* wIndex == language code.
  57187. + * this driver only handles one language, you can
  57188. + * add string tables for other languages, using
  57189. + * any UTF-8 characters
  57190. + */
  57191. + value = usb_gadget_get_string (&stringtab,
  57192. + ctrl->wValue & 0xff, req->buf);
  57193. + if (value >= 0) {
  57194. + value = min (ctrl->wLength, (u16) value);
  57195. + }
  57196. + break;
  57197. + }
  57198. + break;
  57199. +
  57200. + /* currently two configs, two speeds */
  57201. + case USB_REQ_SET_CONFIGURATION:
  57202. + if (ctrl->bRequestType != 0)
  57203. + goto unknown;
  57204. +
  57205. + spin_lock (&dev->lock);
  57206. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  57207. + spin_unlock (&dev->lock);
  57208. + break;
  57209. + case USB_REQ_GET_CONFIGURATION:
  57210. + if (ctrl->bRequestType != USB_DIR_IN)
  57211. + goto unknown;
  57212. + *(u8 *)req->buf = dev->config;
  57213. + value = min (ctrl->wLength, (u16) 1);
  57214. + break;
  57215. +
  57216. + /* until we add altsetting support, or other interfaces,
  57217. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  57218. + * and already killed pending endpoint I/O.
  57219. + */
  57220. + case USB_REQ_SET_INTERFACE:
  57221. +
  57222. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  57223. + goto unknown;
  57224. + spin_lock (&dev->lock);
  57225. + if (dev->config) {
  57226. + u8 config = dev->config;
  57227. +
  57228. + /* resets interface configuration, forgets about
  57229. + * previous transaction state (queued bufs, etc)
  57230. + * and re-inits endpoint state (toggle etc)
  57231. + * no response queued, just zero status == success.
  57232. + * if we had more than one interface we couldn't
  57233. + * use this "reset the config" shortcut.
  57234. + */
  57235. + zero_reset_config (dev);
  57236. + zero_set_config (dev, config, GFP_ATOMIC);
  57237. + value = 0;
  57238. + }
  57239. + spin_unlock (&dev->lock);
  57240. + break;
  57241. + case USB_REQ_GET_INTERFACE:
  57242. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  57243. + value = ctrl->wLength;
  57244. + break;
  57245. + }
  57246. + else {
  57247. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  57248. + goto unknown;
  57249. + if (!dev->config)
  57250. + break;
  57251. + if (ctrl->wIndex != 0) {
  57252. + value = -EDOM;
  57253. + break;
  57254. + }
  57255. + *(u8 *)req->buf = 0;
  57256. + value = min (ctrl->wLength, (u16) 1);
  57257. + }
  57258. + break;
  57259. +
  57260. + /*
  57261. + * These are the same vendor-specific requests supported by
  57262. + * Intel's USB 2.0 compliance test devices. We exceed that
  57263. + * device spec by allowing multiple-packet requests.
  57264. + */
  57265. + case 0x5b: /* control WRITE test -- fill the buffer */
  57266. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  57267. + goto unknown;
  57268. + if (ctrl->wValue || ctrl->wIndex)
  57269. + break;
  57270. + /* just read that many bytes into the buffer */
  57271. + if (ctrl->wLength > USB_BUFSIZ)
  57272. + break;
  57273. + value = ctrl->wLength;
  57274. + break;
  57275. + case 0x5c: /* control READ test -- return the buffer */
  57276. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  57277. + goto unknown;
  57278. + if (ctrl->wValue || ctrl->wIndex)
  57279. + break;
  57280. + /* expect those bytes are still in the buffer; send back */
  57281. + if (ctrl->wLength > USB_BUFSIZ
  57282. + || ctrl->wLength != req->length)
  57283. + break;
  57284. + value = ctrl->wLength;
  57285. + break;
  57286. +
  57287. + case 0x01: // SET_CUR
  57288. + case 0x02:
  57289. + case 0x03:
  57290. + case 0x04:
  57291. + case 0x05:
  57292. + value = ctrl->wLength;
  57293. + break;
  57294. + case 0x81:
  57295. + switch (ctrl->wValue) {
  57296. + case 0x0201:
  57297. + case 0x0202:
  57298. + ((u8*)req->buf)[0] = 0x00;
  57299. + ((u8*)req->buf)[1] = 0xe3;
  57300. + break;
  57301. + case 0x0300:
  57302. + case 0x0500:
  57303. + ((u8*)req->buf)[0] = 0x00;
  57304. + break;
  57305. + }
  57306. + //((u8*)req->buf)[0] = 0x81;
  57307. + //((u8*)req->buf)[1] = 0x81;
  57308. + value = ctrl->wLength;
  57309. + break;
  57310. + case 0x82:
  57311. + switch (ctrl->wValue) {
  57312. + case 0x0201:
  57313. + case 0x0202:
  57314. + ((u8*)req->buf)[0] = 0x00;
  57315. + ((u8*)req->buf)[1] = 0xc3;
  57316. + break;
  57317. + case 0x0300:
  57318. + case 0x0500:
  57319. + ((u8*)req->buf)[0] = 0x00;
  57320. + break;
  57321. + }
  57322. + //((u8*)req->buf)[0] = 0x82;
  57323. + //((u8*)req->buf)[1] = 0x82;
  57324. + value = ctrl->wLength;
  57325. + break;
  57326. + case 0x83:
  57327. + switch (ctrl->wValue) {
  57328. + case 0x0201:
  57329. + case 0x0202:
  57330. + ((u8*)req->buf)[0] = 0x00;
  57331. + ((u8*)req->buf)[1] = 0x00;
  57332. + break;
  57333. + case 0x0300:
  57334. + ((u8*)req->buf)[0] = 0x60;
  57335. + break;
  57336. + case 0x0500:
  57337. + ((u8*)req->buf)[0] = 0x18;
  57338. + break;
  57339. + }
  57340. + //((u8*)req->buf)[0] = 0x83;
  57341. + //((u8*)req->buf)[1] = 0x83;
  57342. + value = ctrl->wLength;
  57343. + break;
  57344. + case 0x84:
  57345. + switch (ctrl->wValue) {
  57346. + case 0x0201:
  57347. + case 0x0202:
  57348. + ((u8*)req->buf)[0] = 0x00;
  57349. + ((u8*)req->buf)[1] = 0x01;
  57350. + break;
  57351. + case 0x0300:
  57352. + case 0x0500:
  57353. + ((u8*)req->buf)[0] = 0x08;
  57354. + break;
  57355. + }
  57356. + //((u8*)req->buf)[0] = 0x84;
  57357. + //((u8*)req->buf)[1] = 0x84;
  57358. + value = ctrl->wLength;
  57359. + break;
  57360. + case 0x85:
  57361. + ((u8*)req->buf)[0] = 0x85;
  57362. + ((u8*)req->buf)[1] = 0x85;
  57363. + value = ctrl->wLength;
  57364. + break;
  57365. +
  57366. +
  57367. + default:
  57368. +unknown:
  57369. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  57370. + ctrl->bRequestType, ctrl->bRequest,
  57371. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  57372. + }
  57373. +
  57374. + /* respond with data transfer before status phase? */
  57375. + if (value >= 0) {
  57376. + req->length = value;
  57377. + req->zero = value < ctrl->wLength
  57378. + && (value % gadget->ep0->maxpacket) == 0;
  57379. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  57380. + if (value < 0) {
  57381. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  57382. + req->status = 0;
  57383. + zero_setup_complete (gadget->ep0, req);
  57384. + }
  57385. + }
  57386. +
  57387. + /* device either stalls (value < 0) or reports success */
  57388. + return value;
  57389. +}
  57390. +
  57391. +static void
  57392. +zero_disconnect (struct usb_gadget *gadget)
  57393. +{
  57394. + struct zero_dev *dev = get_gadget_data (gadget);
  57395. + unsigned long flags;
  57396. +
  57397. + spin_lock_irqsave (&dev->lock, flags);
  57398. + zero_reset_config (dev);
  57399. +
  57400. + /* a more significant application might have some non-usb
  57401. + * activities to quiesce here, saving resources like power
  57402. + * or pushing the notification up a network stack.
  57403. + */
  57404. + spin_unlock_irqrestore (&dev->lock, flags);
  57405. +
  57406. + /* next we may get setup() calls to enumerate new connections;
  57407. + * or an unbind() during shutdown (including removing module).
  57408. + */
  57409. +}
  57410. +
  57411. +static void
  57412. +zero_autoresume (unsigned long _dev)
  57413. +{
  57414. + struct zero_dev *dev = (struct zero_dev *) _dev;
  57415. + int status;
  57416. +
  57417. + /* normally the host would be woken up for something
  57418. + * more significant than just a timer firing...
  57419. + */
  57420. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  57421. + status = usb_gadget_wakeup (dev->gadget);
  57422. + DBG (dev, "wakeup --> %d\n", status);
  57423. + }
  57424. +}
  57425. +
  57426. +/*-------------------------------------------------------------------------*/
  57427. +
  57428. +static void
  57429. +zero_unbind (struct usb_gadget *gadget)
  57430. +{
  57431. + struct zero_dev *dev = get_gadget_data (gadget);
  57432. +
  57433. + DBG (dev, "unbind\n");
  57434. +
  57435. + /* we've already been disconnected ... no i/o is active */
  57436. + if (dev->req)
  57437. + free_ep_req (gadget->ep0, dev->req);
  57438. + del_timer_sync (&dev->resume);
  57439. + kfree (dev);
  57440. + set_gadget_data (gadget, NULL);
  57441. +}
  57442. +
  57443. +static int
  57444. +zero_bind (struct usb_gadget *gadget)
  57445. +{
  57446. + struct zero_dev *dev;
  57447. + //struct usb_ep *ep;
  57448. +
  57449. + printk("binding\n");
  57450. + /*
  57451. + * DRIVER POLICY CHOICE: you may want to do this differently.
  57452. + * One thing to avoid is reusing a bcdDevice revision code
  57453. + * with different host-visible configurations or behavior
  57454. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  57455. + */
  57456. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  57457. +
  57458. +
  57459. + /* ok, we made sense of the hardware ... */
  57460. + dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  57461. + if (!dev)
  57462. + return -ENOMEM;
  57463. + memset (dev, 0, sizeof *dev);
  57464. + spin_lock_init (&dev->lock);
  57465. + dev->gadget = gadget;
  57466. + set_gadget_data (gadget, dev);
  57467. +
  57468. + /* preallocate control response and buffer */
  57469. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  57470. + if (!dev->req)
  57471. + goto enomem;
  57472. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  57473. + &dev->req->dma, GFP_KERNEL);
  57474. + if (!dev->req->buf)
  57475. + goto enomem;
  57476. +
  57477. + dev->req->complete = zero_setup_complete;
  57478. +
  57479. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  57480. +
  57481. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  57482. + /* assume ep0 uses the same value for both speeds ... */
  57483. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  57484. +
  57485. + /* and that all endpoints are dual-speed */
  57486. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  57487. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  57488. +#endif
  57489. +
  57490. + usb_gadget_set_selfpowered (gadget);
  57491. +
  57492. + init_timer (&dev->resume);
  57493. + dev->resume.function = zero_autoresume;
  57494. + dev->resume.data = (unsigned long) dev;
  57495. +
  57496. + gadget->ep0->driver_data = dev;
  57497. +
  57498. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  57499. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  57500. + EP_OUT_NAME, EP_IN_NAME);
  57501. +
  57502. + snprintf (manufacturer, sizeof manufacturer,
  57503. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  57504. + gadget->name);
  57505. +
  57506. + return 0;
  57507. +
  57508. +enomem:
  57509. + zero_unbind (gadget);
  57510. + return -ENOMEM;
  57511. +}
  57512. +
  57513. +/*-------------------------------------------------------------------------*/
  57514. +
  57515. +static void
  57516. +zero_suspend (struct usb_gadget *gadget)
  57517. +{
  57518. + struct zero_dev *dev = get_gadget_data (gadget);
  57519. +
  57520. + if (gadget->speed == USB_SPEED_UNKNOWN)
  57521. + return;
  57522. +
  57523. + if (autoresume) {
  57524. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  57525. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  57526. + } else
  57527. + DBG (dev, "suspend\n");
  57528. +}
  57529. +
  57530. +static void
  57531. +zero_resume (struct usb_gadget *gadget)
  57532. +{
  57533. + struct zero_dev *dev = get_gadget_data (gadget);
  57534. +
  57535. + DBG (dev, "resume\n");
  57536. + del_timer (&dev->resume);
  57537. +}
  57538. +
  57539. +
  57540. +/*-------------------------------------------------------------------------*/
  57541. +
  57542. +static struct usb_gadget_driver zero_driver = {
  57543. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  57544. + .speed = USB_SPEED_HIGH,
  57545. +#else
  57546. + .speed = USB_SPEED_FULL,
  57547. +#endif
  57548. + .function = (char *) longname,
  57549. + .bind = zero_bind,
  57550. + .unbind = zero_unbind,
  57551. +
  57552. + .setup = zero_setup,
  57553. + .disconnect = zero_disconnect,
  57554. +
  57555. + .suspend = zero_suspend,
  57556. + .resume = zero_resume,
  57557. +
  57558. + .driver = {
  57559. + .name = (char *) shortname,
  57560. + // .shutdown = ...
  57561. + // .suspend = ...
  57562. + // .resume = ...
  57563. + },
  57564. +};
  57565. +
  57566. +MODULE_AUTHOR ("David Brownell");
  57567. +MODULE_LICENSE ("Dual BSD/GPL");
  57568. +
  57569. +static struct proc_dir_entry *pdir, *pfile;
  57570. +
  57571. +static int isoc_read_data (char *page, char **start,
  57572. + off_t off, int count,
  57573. + int *eof, void *data)
  57574. +{
  57575. + int i;
  57576. + static int c = 0;
  57577. + static int done = 0;
  57578. + static int s = 0;
  57579. +
  57580. +/*
  57581. + printk ("\ncount: %d\n", count);
  57582. + printk ("rbuf_start: %d\n", rbuf_start);
  57583. + printk ("rbuf_len: %d\n", rbuf_len);
  57584. + printk ("off: %d\n", off);
  57585. + printk ("start: %p\n\n", *start);
  57586. +*/
  57587. + if (done) {
  57588. + c = 0;
  57589. + done = 0;
  57590. + *eof = 1;
  57591. + return 0;
  57592. + }
  57593. +
  57594. + if (c == 0) {
  57595. + if (rbuf_len == RBUF_LEN)
  57596. + s = rbuf_start;
  57597. + else s = 0;
  57598. + }
  57599. +
  57600. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  57601. + page[i] = rbuf[(c+s) % RBUF_LEN];
  57602. + }
  57603. + *start = page;
  57604. +
  57605. + if (c >= rbuf_len) {
  57606. + *eof = 1;
  57607. + done = 1;
  57608. + }
  57609. +
  57610. +
  57611. + return i;
  57612. +}
  57613. +
  57614. +static int __init init (void)
  57615. +{
  57616. +
  57617. + int retval = 0;
  57618. +
  57619. + pdir = proc_mkdir("isoc_test", NULL);
  57620. + if(pdir == NULL) {
  57621. + retval = -ENOMEM;
  57622. + printk("Error creating dir\n");
  57623. + goto done;
  57624. + }
  57625. + pdir->owner = THIS_MODULE;
  57626. +
  57627. + pfile = create_proc_read_entry("isoc_data",
  57628. + 0444, pdir,
  57629. + isoc_read_data,
  57630. + NULL);
  57631. + if (pfile == NULL) {
  57632. + retval = -ENOMEM;
  57633. + printk("Error creating file\n");
  57634. + goto no_file;
  57635. + }
  57636. + pfile->owner = THIS_MODULE;
  57637. +
  57638. + return usb_gadget_register_driver (&zero_driver);
  57639. +
  57640. + no_file:
  57641. + remove_proc_entry("isoc_data", NULL);
  57642. + done:
  57643. + return retval;
  57644. +}
  57645. +module_init (init);
  57646. +
  57647. +static void __exit cleanup (void)
  57648. +{
  57649. +
  57650. + usb_gadget_unregister_driver (&zero_driver);
  57651. +
  57652. + remove_proc_entry("isoc_data", pdir);
  57653. + remove_proc_entry("isoc_test", NULL);
  57654. +}
  57655. +module_exit (cleanup);
  57656. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_cfi_common.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  57657. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_cfi_common.h 1970-01-01 01:00:00.000000000 +0100
  57658. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_cfi_common.h 2014-09-14 19:04:13.000000000 +0200
  57659. @@ -0,0 +1,142 @@
  57660. +/* ==========================================================================
  57661. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57662. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57663. + * otherwise expressly agreed to in writing between Synopsys and you.
  57664. + *
  57665. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57666. + * any End User Software License Agreement or Agreement for Licensed Product
  57667. + * with Synopsys or any supplement thereto. You are permitted to use and
  57668. + * redistribute this Software in source and binary forms, with or without
  57669. + * modification, provided that redistributions of source code must retain this
  57670. + * notice. You may not view, use, disclose, copy or distribute this file or
  57671. + * any information contained herein except pursuant to this license grant from
  57672. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57673. + * below, then you are not authorized to use the Software.
  57674. + *
  57675. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57676. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57677. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57678. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57679. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57680. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57681. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57682. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57683. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57684. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57685. + * DAMAGE.
  57686. + * ========================================================================== */
  57687. +
  57688. +#if !defined(__DWC_CFI_COMMON_H__)
  57689. +#define __DWC_CFI_COMMON_H__
  57690. +
  57691. +//#include <linux/types.h>
  57692. +
  57693. +/**
  57694. + * @file
  57695. + *
  57696. + * This file contains the CFI specific common constants, interfaces
  57697. + * (functions and macros) and structures for Linux. No PCD specific
  57698. + * data structure or definition is to be included in this file.
  57699. + *
  57700. + */
  57701. +
  57702. +/** This is a request for all Core Features */
  57703. +#define VEN_CORE_GET_FEATURES 0xB1
  57704. +
  57705. +/** This is a request to get the value of a specific Core Feature */
  57706. +#define VEN_CORE_GET_FEATURE 0xB2
  57707. +
  57708. +/** This command allows the host to set the value of a specific Core Feature */
  57709. +#define VEN_CORE_SET_FEATURE 0xB3
  57710. +
  57711. +/** This command allows the host to set the default values of
  57712. + * either all or any specific Core Feature
  57713. + */
  57714. +#define VEN_CORE_RESET_FEATURES 0xB4
  57715. +
  57716. +/** This command forces the PCD to write the deferred values of a Core Features */
  57717. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  57718. +
  57719. +/** This request reads a DWORD value from a register at the specified offset */
  57720. +#define VEN_CORE_READ_REGISTER 0xB6
  57721. +
  57722. +/** This request writes a DWORD value into a register at the specified offset */
  57723. +#define VEN_CORE_WRITE_REGISTER 0xB7
  57724. +
  57725. +/** This structure is the header of the Core Features dataset returned to
  57726. + * the Host
  57727. + */
  57728. +struct cfi_all_features_header {
  57729. +/** The features header structure length is */
  57730. +#define CFI_ALL_FEATURES_HDR_LEN 8
  57731. + /**
  57732. + * The total length of the features dataset returned to the Host
  57733. + */
  57734. + uint16_t wTotalLen;
  57735. +
  57736. + /**
  57737. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  57738. + * This field identifies the version of the CFI Specification with which
  57739. + * the device is compliant.
  57740. + */
  57741. + uint16_t wVersion;
  57742. +
  57743. + /** The ID of the Core */
  57744. + uint16_t wCoreID;
  57745. +#define CFI_CORE_ID_UDC 1
  57746. +#define CFI_CORE_ID_OTG 2
  57747. +#define CFI_CORE_ID_WUDEV 3
  57748. +
  57749. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  57750. + uint16_t wNumFeatures;
  57751. +} UPACKED;
  57752. +
  57753. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  57754. +
  57755. +/** This structure is a header of the Core Feature descriptor dataset returned to
  57756. + * the Host after the VEN_CORE_GET_FEATURES request
  57757. + */
  57758. +struct cfi_feature_desc_header {
  57759. +#define CFI_FEATURE_DESC_HDR_LEN 8
  57760. +
  57761. + /** The feature ID */
  57762. + uint16_t wFeatureID;
  57763. +
  57764. + /** Length of this feature descriptor in bytes - including the
  57765. + * length of the feature name string
  57766. + */
  57767. + uint16_t wLength;
  57768. +
  57769. + /** The data length of this feature in bytes */
  57770. + uint16_t wDataLength;
  57771. +
  57772. + /**
  57773. + * Attributes of this features
  57774. + * D0: Access rights
  57775. + * 0 - Read/Write
  57776. + * 1 - Read only
  57777. + */
  57778. + uint8_t bmAttributes;
  57779. +#define CFI_FEATURE_ATTR_RO 1
  57780. +#define CFI_FEATURE_ATTR_RW 0
  57781. +
  57782. + /** Length of the feature name in bytes */
  57783. + uint8_t bNameLen;
  57784. +
  57785. + /** The feature name buffer */
  57786. + //uint8_t *name;
  57787. +} UPACKED;
  57788. +
  57789. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  57790. +
  57791. +/**
  57792. + * This structure describes a NULL terminated string referenced by its id field.
  57793. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  57794. + */
  57795. +struct cfi_string {
  57796. + uint16_t id;
  57797. + const uint8_t *s;
  57798. +};
  57799. +typedef struct cfi_string cfi_string_t;
  57800. +
  57801. +#endif
  57802. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_adp.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  57803. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_adp.c 1970-01-01 01:00:00.000000000 +0100
  57804. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.c 2014-09-14 19:04:13.000000000 +0200
  57805. @@ -0,0 +1,854 @@
  57806. +/* ==========================================================================
  57807. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  57808. + * $Revision: #12 $
  57809. + * $Date: 2011/10/26 $
  57810. + * $Change: 1873028 $
  57811. + *
  57812. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57813. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57814. + * otherwise expressly agreed to in writing between Synopsys and you.
  57815. + *
  57816. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57817. + * any End User Software License Agreement or Agreement for Licensed Product
  57818. + * with Synopsys or any supplement thereto. You are permitted to use and
  57819. + * redistribute this Software in source and binary forms, with or without
  57820. + * modification, provided that redistributions of source code must retain this
  57821. + * notice. You may not view, use, disclose, copy or distribute this file or
  57822. + * any information contained herein except pursuant to this license grant from
  57823. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57824. + * below, then you are not authorized to use the Software.
  57825. + *
  57826. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57827. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57828. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57829. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57830. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57831. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57832. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57833. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57834. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57835. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57836. + * DAMAGE.
  57837. + * ========================================================================== */
  57838. +
  57839. +#include "dwc_os.h"
  57840. +#include "dwc_otg_regs.h"
  57841. +#include "dwc_otg_cil.h"
  57842. +#include "dwc_otg_adp.h"
  57843. +
  57844. +/** @file
  57845. + *
  57846. + * This file contains the most of the Attach Detect Protocol implementation for
  57847. + * the driver to support OTG Rev2.0.
  57848. + *
  57849. + */
  57850. +
  57851. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  57852. +{
  57853. + adpctl_data_t adpctl;
  57854. +
  57855. + adpctl.d32 = value;
  57856. + adpctl.b.ar = 0x2;
  57857. +
  57858. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  57859. +
  57860. + while (adpctl.b.ar) {
  57861. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  57862. + }
  57863. +
  57864. +}
  57865. +
  57866. +/**
  57867. + * Function is called to read ADP registers
  57868. + */
  57869. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  57870. +{
  57871. + adpctl_data_t adpctl;
  57872. +
  57873. + adpctl.d32 = 0;
  57874. + adpctl.b.ar = 0x1;
  57875. +
  57876. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  57877. +
  57878. + while (adpctl.b.ar) {
  57879. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  57880. + }
  57881. +
  57882. + return adpctl.d32;
  57883. +}
  57884. +
  57885. +/**
  57886. + * Function is called to read ADPCTL register and filter Write-clear bits
  57887. + */
  57888. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  57889. +{
  57890. + adpctl_data_t adpctl;
  57891. +
  57892. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  57893. + adpctl.b.adp_tmout_int = 0;
  57894. + adpctl.b.adp_prb_int = 0;
  57895. + adpctl.b.adp_tmout_int = 0;
  57896. +
  57897. + return adpctl.d32;
  57898. +}
  57899. +
  57900. +/**
  57901. + * Function is called to write ADP registers
  57902. + */
  57903. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  57904. + uint32_t set)
  57905. +{
  57906. + dwc_otg_adp_write_reg(core_if,
  57907. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  57908. +}
  57909. +
  57910. +static void adp_sense_timeout(void *ptr)
  57911. +{
  57912. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  57913. + core_if->adp.sense_timer_started = 0;
  57914. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  57915. + if (core_if->adp_enable) {
  57916. + dwc_otg_adp_sense_stop(core_if);
  57917. + dwc_otg_adp_probe_start(core_if);
  57918. + }
  57919. +}
  57920. +
  57921. +/**
  57922. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  57923. + */
  57924. +static void adp_vbuson_timeout(void *ptr)
  57925. +{
  57926. + gpwrdn_data_t gpwrdn;
  57927. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  57928. + hprt0_data_t hprt0 = {.d32 = 0 };
  57929. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  57930. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  57931. + if (core_if) {
  57932. + core_if->adp.vbuson_timer_started = 0;
  57933. + /* Turn off vbus */
  57934. + hprt0.b.prtpwr = 1;
  57935. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  57936. + gpwrdn.d32 = 0;
  57937. +
  57938. + /* Power off the core */
  57939. + if (core_if->power_down == 2) {
  57940. + /* Enable Wakeup Logic */
  57941. +// gpwrdn.b.wkupactiv = 1;
  57942. + gpwrdn.b.pmuactv = 0;
  57943. + gpwrdn.b.pwrdnrstn = 1;
  57944. + gpwrdn.b.pwrdnclmp = 1;
  57945. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  57946. + gpwrdn.d32);
  57947. +
  57948. + /* Suspend the Phy Clock */
  57949. + pcgcctl.b.stoppclk = 1;
  57950. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  57951. +
  57952. + /* Switch on VDD */
  57953. +// gpwrdn.b.wkupactiv = 1;
  57954. + gpwrdn.b.pmuactv = 1;
  57955. + gpwrdn.b.pwrdnrstn = 1;
  57956. + gpwrdn.b.pwrdnclmp = 1;
  57957. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  57958. + gpwrdn.d32);
  57959. + } else {
  57960. + /* Enable Power Down Logic */
  57961. + gpwrdn.b.pmuintsel = 1;
  57962. + gpwrdn.b.pmuactv = 1;
  57963. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  57964. + }
  57965. +
  57966. + /* Power off the core */
  57967. + if (core_if->power_down == 2) {
  57968. + gpwrdn.d32 = 0;
  57969. + gpwrdn.b.pwrdnswtch = 1;
  57970. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  57971. + gpwrdn.d32, 0);
  57972. + }
  57973. +
  57974. + /* Unmask SRP detected interrupt from Power Down Logic */
  57975. + gpwrdn.d32 = 0;
  57976. + gpwrdn.b.srp_det_msk = 1;
  57977. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  57978. +
  57979. + dwc_otg_adp_probe_start(core_if);
  57980. + dwc_otg_dump_global_registers(core_if);
  57981. + dwc_otg_dump_host_registers(core_if);
  57982. + }
  57983. +
  57984. +}
  57985. +
  57986. +/**
  57987. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  57988. + * not asserted within 1.1 seconds.
  57989. + *
  57990. + * @param core_if the pointer to core_if strucure.
  57991. + */
  57992. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  57993. +{
  57994. + core_if->adp.vbuson_timer_started = 1;
  57995. + if (core_if->adp.vbuson_timer)
  57996. + {
  57997. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  57998. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  57999. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  58000. + } else {
  58001. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  58002. + }
  58003. +}
  58004. +
  58005. +#if 0
  58006. +/**
  58007. + * Masks all DWC OTG core interrupts
  58008. + *
  58009. + */
  58010. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  58011. +{
  58012. + int i;
  58013. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  58014. +
  58015. + /* Mask Host Interrupts */
  58016. +
  58017. + /* Clear and disable HCINTs */
  58018. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  58019. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  58020. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  58021. +
  58022. + }
  58023. +
  58024. + /* Clear and disable HAINT */
  58025. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  58026. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  58027. +
  58028. + /* Mask Device Interrupts */
  58029. + if (!core_if->multiproc_int_enable) {
  58030. + /* Clear and disable IN Endpoint interrupts */
  58031. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  58032. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  58033. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  58034. + diepint, 0xFFFFFFFF);
  58035. + }
  58036. +
  58037. + /* Clear and disable OUT Endpoint interrupts */
  58038. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  58039. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  58040. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  58041. + doepint, 0xFFFFFFFF);
  58042. + }
  58043. +
  58044. + /* Clear and disable DAINT */
  58045. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  58046. + 0xFFFFFFFF);
  58047. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  58048. + } else {
  58049. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  58050. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  58051. + diepeachintmsk[i], 0);
  58052. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  58053. + diepint, 0xFFFFFFFF);
  58054. + }
  58055. +
  58056. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  58057. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  58058. + doepeachintmsk[i], 0);
  58059. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  58060. + doepint, 0xFFFFFFFF);
  58061. + }
  58062. +
  58063. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  58064. + 0);
  58065. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  58066. + 0xFFFFFFFF);
  58067. +
  58068. + }
  58069. +
  58070. + /* Disable interrupts */
  58071. + ahbcfg.b.glblintrmsk = 1;
  58072. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  58073. +
  58074. + /* Disable all interrupts. */
  58075. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  58076. +
  58077. + /* Clear any pending interrupts */
  58078. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  58079. +
  58080. + /* Clear any pending OTG Interrupts */
  58081. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  58082. +}
  58083. +
  58084. +/**
  58085. + * Unmask Port Connection Detected interrupt
  58086. + *
  58087. + */
  58088. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  58089. +{
  58090. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  58091. +
  58092. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  58093. +}
  58094. +#endif
  58095. +
  58096. +/**
  58097. + * Starts the ADP Probing
  58098. + *
  58099. + * @param core_if the pointer to core_if structure.
  58100. + */
  58101. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  58102. +{
  58103. +
  58104. + adpctl_data_t adpctl = {.d32 = 0};
  58105. + gpwrdn_data_t gpwrdn;
  58106. +#if 0
  58107. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  58108. + .b.adp_sns_int = 1, b.adp_tmout_int};
  58109. +#endif
  58110. + dwc_otg_disable_global_interrupts(core_if);
  58111. + DWC_PRINTF("ADP Probe Start\n");
  58112. + core_if->adp.probe_enabled = 1;
  58113. +
  58114. + adpctl.b.adpres = 1;
  58115. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58116. +
  58117. + while (adpctl.b.adpres) {
  58118. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  58119. + }
  58120. +
  58121. + adpctl.d32 = 0;
  58122. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  58123. +
  58124. + /* In Host mode unmask SRP detected interrupt */
  58125. + gpwrdn.d32 = 0;
  58126. + gpwrdn.b.sts_chngint_msk = 1;
  58127. + if (!gpwrdn.b.idsts) {
  58128. + gpwrdn.b.srp_det_msk = 1;
  58129. + }
  58130. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  58131. +
  58132. + adpctl.b.adp_tmout_int_msk = 1;
  58133. + adpctl.b.adp_prb_int_msk = 1;
  58134. + adpctl.b.prb_dschg = 1;
  58135. + adpctl.b.prb_delta = 1;
  58136. + adpctl.b.prb_per = 1;
  58137. + adpctl.b.adpen = 1;
  58138. + adpctl.b.enaprb = 1;
  58139. +
  58140. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58141. + DWC_PRINTF("ADP Probe Finish\n");
  58142. + return 0;
  58143. +}
  58144. +
  58145. +/**
  58146. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  58147. + * within 3 seconds.
  58148. + *
  58149. + * @param core_if the pointer to core_if strucure.
  58150. + */
  58151. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  58152. +{
  58153. + core_if->adp.sense_timer_started = 1;
  58154. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  58155. +}
  58156. +
  58157. +/**
  58158. + * Starts the ADP Sense
  58159. + *
  58160. + * @param core_if the pointer to core_if strucure.
  58161. + */
  58162. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  58163. +{
  58164. + adpctl_data_t adpctl;
  58165. +
  58166. + DWC_PRINTF("ADP Sense Start\n");
  58167. +
  58168. + /* Unmask ADP sense interrupt and mask all other from the core */
  58169. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  58170. + adpctl.b.adp_sns_int_msk = 1;
  58171. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58172. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  58173. +
  58174. + /* Set ADP reset bit*/
  58175. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  58176. + adpctl.b.adpres = 1;
  58177. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58178. +
  58179. + while (adpctl.b.adpres) {
  58180. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  58181. + }
  58182. +
  58183. + adpctl.b.adpres = 0;
  58184. + adpctl.b.adpen = 1;
  58185. + adpctl.b.enasns = 1;
  58186. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58187. +
  58188. + dwc_otg_adp_sense_timer_start(core_if);
  58189. +
  58190. + return 0;
  58191. +}
  58192. +
  58193. +/**
  58194. + * Stops the ADP Probing
  58195. + *
  58196. + * @param core_if the pointer to core_if strucure.
  58197. + */
  58198. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  58199. +{
  58200. +
  58201. + adpctl_data_t adpctl;
  58202. + DWC_PRINTF("Stop ADP probe\n");
  58203. + core_if->adp.probe_enabled = 0;
  58204. + core_if->adp.probe_counter = 0;
  58205. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  58206. +
  58207. + adpctl.b.adpen = 0;
  58208. + adpctl.b.adp_prb_int = 1;
  58209. + adpctl.b.adp_tmout_int = 1;
  58210. + adpctl.b.adp_sns_int = 1;
  58211. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58212. +
  58213. + return 0;
  58214. +}
  58215. +
  58216. +/**
  58217. + * Stops the ADP Sensing
  58218. + *
  58219. + * @param core_if the pointer to core_if strucure.
  58220. + */
  58221. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  58222. +{
  58223. + adpctl_data_t adpctl;
  58224. +
  58225. + core_if->adp.sense_enabled = 0;
  58226. +
  58227. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  58228. + adpctl.b.enasns = 0;
  58229. + adpctl.b.adp_sns_int = 1;
  58230. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58231. +
  58232. + return 0;
  58233. +}
  58234. +
  58235. +/**
  58236. + * Called to turn on the VBUS after initial ADP probe in host mode.
  58237. + * If port power was already enabled in cil_hcd_start function then
  58238. + * only schedule a timer.
  58239. + *
  58240. + * @param core_if the pointer to core_if structure.
  58241. + */
  58242. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  58243. +{
  58244. + hprt0_data_t hprt0 = {.d32 = 0 };
  58245. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  58246. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  58247. +
  58248. + if (hprt0.b.prtpwr == 0) {
  58249. + hprt0.b.prtpwr = 1;
  58250. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  58251. + }
  58252. +
  58253. + dwc_otg_adp_vbuson_timer_start(core_if);
  58254. +}
  58255. +
  58256. +/**
  58257. + * Called right after driver is loaded
  58258. + * to perform initial actions for ADP
  58259. + *
  58260. + * @param core_if the pointer to core_if structure.
  58261. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  58262. + */
  58263. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  58264. +{
  58265. + gpwrdn_data_t gpwrdn;
  58266. +
  58267. + DWC_PRINTF("ADP Initial Start\n");
  58268. + core_if->adp.adp_started = 1;
  58269. +
  58270. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  58271. + dwc_otg_disable_global_interrupts(core_if);
  58272. + if (is_host) {
  58273. + DWC_PRINTF("HOST MODE\n");
  58274. + /* Enable Power Down Logic Interrupt*/
  58275. + gpwrdn.d32 = 0;
  58276. + gpwrdn.b.pmuintsel = 1;
  58277. + gpwrdn.b.pmuactv = 1;
  58278. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  58279. + /* Initialize first ADP probe to obtain Ramp Time value */
  58280. + core_if->adp.initial_probe = 1;
  58281. + dwc_otg_adp_probe_start(core_if);
  58282. + } else {
  58283. + gotgctl_data_t gotgctl;
  58284. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  58285. + DWC_PRINTF("DEVICE MODE\n");
  58286. + if (gotgctl.b.bsesvld == 0) {
  58287. + /* Enable Power Down Logic Interrupt*/
  58288. + gpwrdn.d32 = 0;
  58289. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  58290. + gpwrdn.b.pmuintsel = 1;
  58291. + gpwrdn.b.pmuactv = 1;
  58292. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  58293. + core_if->adp.initial_probe = 1;
  58294. + dwc_otg_adp_probe_start(core_if);
  58295. + } else {
  58296. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  58297. + core_if->op_state = B_PERIPHERAL;
  58298. + dwc_otg_core_init(core_if);
  58299. + dwc_otg_enable_global_interrupts(core_if);
  58300. + cil_pcd_start(core_if);
  58301. + dwc_otg_dump_global_registers(core_if);
  58302. + dwc_otg_dump_dev_registers(core_if);
  58303. + }
  58304. + }
  58305. +}
  58306. +
  58307. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  58308. +{
  58309. + core_if->adp.adp_started = 0;
  58310. + core_if->adp.initial_probe = 0;
  58311. + core_if->adp.probe_timer_values[0] = -1;
  58312. + core_if->adp.probe_timer_values[1] = -1;
  58313. + core_if->adp.probe_enabled = 0;
  58314. + core_if->adp.sense_enabled = 0;
  58315. + core_if->adp.sense_timer_started = 0;
  58316. + core_if->adp.vbuson_timer_started = 0;
  58317. + core_if->adp.probe_counter = 0;
  58318. + core_if->adp.gpwrdn = 0;
  58319. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  58320. + /* Initialize timers */
  58321. + core_if->adp.sense_timer =
  58322. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  58323. + core_if->adp.vbuson_timer =
  58324. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  58325. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  58326. + {
  58327. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  58328. + }
  58329. +}
  58330. +
  58331. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  58332. +{
  58333. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  58334. + gpwrdn.b.pmuintsel = 1;
  58335. + gpwrdn.b.pmuactv = 1;
  58336. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  58337. +
  58338. + if (core_if->adp.probe_enabled)
  58339. + dwc_otg_adp_probe_stop(core_if);
  58340. + if (core_if->adp.sense_enabled)
  58341. + dwc_otg_adp_sense_stop(core_if);
  58342. + if (core_if->adp.sense_timer_started)
  58343. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  58344. + if (core_if->adp.vbuson_timer_started)
  58345. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  58346. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  58347. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  58348. +}
  58349. +
  58350. +/////////////////////////////////////////////////////////////////////
  58351. +////////////// ADP Interrupt Handlers ///////////////////////////////
  58352. +/////////////////////////////////////////////////////////////////////
  58353. +/**
  58354. + * This function sets Ramp Timer values
  58355. + */
  58356. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  58357. +{
  58358. + if (core_if->adp.probe_timer_values[0] == -1) {
  58359. + core_if->adp.probe_timer_values[0] = val;
  58360. + core_if->adp.probe_timer_values[1] = -1;
  58361. + return 1;
  58362. + } else {
  58363. + core_if->adp.probe_timer_values[1] =
  58364. + core_if->adp.probe_timer_values[0];
  58365. + core_if->adp.probe_timer_values[0] = val;
  58366. + return 0;
  58367. + }
  58368. +}
  58369. +
  58370. +/**
  58371. + * This function compares Ramp Timer values
  58372. + */
  58373. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  58374. +{
  58375. + uint32_t diff;
  58376. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  58377. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  58378. + else
  58379. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  58380. + if(diff < 2) {
  58381. + return 0;
  58382. + } else {
  58383. + return 1;
  58384. + }
  58385. +}
  58386. +
  58387. +/**
  58388. + * This function handles ADP Probe Interrupts
  58389. + */
  58390. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  58391. + uint32_t val)
  58392. +{
  58393. + adpctl_data_t adpctl = {.d32 = 0 };
  58394. + gpwrdn_data_t gpwrdn, temp;
  58395. + adpctl.d32 = val;
  58396. +
  58397. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  58398. + core_if->adp.probe_counter++;
  58399. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  58400. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  58401. + DWC_PRINTF("RTIM value is 0\n");
  58402. + goto exit;
  58403. + }
  58404. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  58405. + core_if->adp.initial_probe) {
  58406. + core_if->adp.initial_probe = 0;
  58407. + dwc_otg_adp_probe_stop(core_if);
  58408. + gpwrdn.d32 = 0;
  58409. + gpwrdn.b.pmuactv = 1;
  58410. + gpwrdn.b.pmuintsel = 1;
  58411. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  58412. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  58413. +
  58414. + /* check which value is for device mode and which for Host mode */
  58415. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  58416. + /*
  58417. + * Turn on VBUS after initial ADP probe.
  58418. + */
  58419. + core_if->op_state = A_HOST;
  58420. + dwc_otg_enable_global_interrupts(core_if);
  58421. + DWC_SPINUNLOCK(core_if->lock);
  58422. + cil_hcd_start(core_if);
  58423. + dwc_otg_adp_turnon_vbus(core_if);
  58424. + DWC_SPINLOCK(core_if->lock);
  58425. + } else {
  58426. + /*
  58427. + * Initiate SRP after initial ADP probe.
  58428. + */
  58429. + dwc_otg_enable_global_interrupts(core_if);
  58430. + dwc_otg_initiate_srp(core_if);
  58431. + }
  58432. + } else if (core_if->adp.probe_counter > 2){
  58433. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  58434. + if (compare_timer_values(core_if)) {
  58435. + DWC_PRINTF("Difference in timer values !!! \n");
  58436. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  58437. + dwc_otg_adp_probe_stop(core_if);
  58438. +
  58439. + /* Power on the core */
  58440. + if (core_if->power_down == 2) {
  58441. + gpwrdn.b.pwrdnswtch = 1;
  58442. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  58443. + gpwrdn, 0, gpwrdn.d32);
  58444. + }
  58445. +
  58446. + /* check which value is for device mode and which for Host mode */
  58447. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  58448. + /* Disable Interrupt from Power Down Logic */
  58449. + gpwrdn.d32 = 0;
  58450. + gpwrdn.b.pmuintsel = 1;
  58451. + gpwrdn.b.pmuactv = 1;
  58452. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  58453. + gpwrdn, gpwrdn.d32, 0);
  58454. +
  58455. + /*
  58456. + * Initialize the Core for Host mode.
  58457. + */
  58458. + core_if->op_state = A_HOST;
  58459. + dwc_otg_core_init(core_if);
  58460. + dwc_otg_enable_global_interrupts(core_if);
  58461. + cil_hcd_start(core_if);
  58462. + } else {
  58463. + gotgctl_data_t gotgctl;
  58464. + /* Mask SRP detected interrupt from Power Down Logic */
  58465. + gpwrdn.d32 = 0;
  58466. + gpwrdn.b.srp_det_msk = 1;
  58467. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  58468. + gpwrdn, gpwrdn.d32, 0);
  58469. +
  58470. + /* Disable Power Down Logic */
  58471. + gpwrdn.d32 = 0;
  58472. + gpwrdn.b.pmuintsel = 1;
  58473. + gpwrdn.b.pmuactv = 1;
  58474. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  58475. + gpwrdn, gpwrdn.d32, 0);
  58476. +
  58477. + /*
  58478. + * Initialize the Core for Device mode.
  58479. + */
  58480. + core_if->op_state = B_PERIPHERAL;
  58481. + dwc_otg_core_init(core_if);
  58482. + dwc_otg_enable_global_interrupts(core_if);
  58483. + cil_pcd_start(core_if);
  58484. +
  58485. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  58486. + if (!gotgctl.b.bsesvld) {
  58487. + dwc_otg_initiate_srp(core_if);
  58488. + }
  58489. + }
  58490. + }
  58491. + if (core_if->power_down == 2) {
  58492. + if (gpwrdn.b.bsessvld) {
  58493. + /* Mask SRP detected interrupt from Power Down Logic */
  58494. + gpwrdn.d32 = 0;
  58495. + gpwrdn.b.srp_det_msk = 1;
  58496. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  58497. +
  58498. + /* Disable Power Down Logic */
  58499. + gpwrdn.d32 = 0;
  58500. + gpwrdn.b.pmuactv = 1;
  58501. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  58502. +
  58503. + /*
  58504. + * Initialize the Core for Device mode.
  58505. + */
  58506. + core_if->op_state = B_PERIPHERAL;
  58507. + dwc_otg_core_init(core_if);
  58508. + dwc_otg_enable_global_interrupts(core_if);
  58509. + cil_pcd_start(core_if);
  58510. + }
  58511. + }
  58512. + }
  58513. +exit:
  58514. + /* Clear interrupt */
  58515. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  58516. + adpctl.b.adp_prb_int = 1;
  58517. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58518. +
  58519. + return 0;
  58520. +}
  58521. +
  58522. +/**
  58523. + * This function hadles ADP Sense Interrupt
  58524. + */
  58525. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  58526. +{
  58527. + adpctl_data_t adpctl;
  58528. + /* Stop ADP Sense timer */
  58529. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  58530. +
  58531. + /* Restart ADP Sense timer */
  58532. + dwc_otg_adp_sense_timer_start(core_if);
  58533. +
  58534. + /* Clear interrupt */
  58535. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  58536. + adpctl.b.adp_sns_int = 1;
  58537. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58538. +
  58539. + return 0;
  58540. +}
  58541. +
  58542. +/**
  58543. + * This function handles ADP Probe Interrupts
  58544. + */
  58545. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  58546. + uint32_t val)
  58547. +{
  58548. + adpctl_data_t adpctl = {.d32 = 0 };
  58549. + adpctl.d32 = val;
  58550. + set_timer_value(core_if, adpctl.b.rtim);
  58551. +
  58552. + /* Clear interrupt */
  58553. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  58554. + adpctl.b.adp_tmout_int = 1;
  58555. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58556. +
  58557. + return 0;
  58558. +}
  58559. +
  58560. +/**
  58561. + * ADP Interrupt handler.
  58562. + *
  58563. + */
  58564. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  58565. +{
  58566. + int retval = 0;
  58567. + adpctl_data_t adpctl = {.d32 = 0};
  58568. +
  58569. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  58570. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  58571. +
  58572. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  58573. + DWC_PRINTF("ADP Sense interrupt\n");
  58574. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  58575. + }
  58576. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  58577. + DWC_PRINTF("ADP timeout interrupt\n");
  58578. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  58579. + }
  58580. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  58581. + DWC_PRINTF("ADP Probe interrupt\n");
  58582. + adpctl.b.adp_prb_int = 1;
  58583. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  58584. + }
  58585. +
  58586. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  58587. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  58588. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  58589. +
  58590. + return retval;
  58591. +}
  58592. +
  58593. +/**
  58594. + *
  58595. + * @param core_if Programming view of DWC_otg controller.
  58596. + */
  58597. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  58598. +{
  58599. +
  58600. +#ifndef DWC_HOST_ONLY
  58601. + hprt0_data_t hprt0;
  58602. + gpwrdn_data_t gpwrdn;
  58603. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  58604. +
  58605. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  58606. + /* check which value is for device mode and which for Host mode */
  58607. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  58608. + DWC_PRINTF("SRP: Host mode\n");
  58609. +
  58610. + if (core_if->adp_enable) {
  58611. + dwc_otg_adp_probe_stop(core_if);
  58612. +
  58613. + /* Power on the core */
  58614. + if (core_if->power_down == 2) {
  58615. + gpwrdn.b.pwrdnswtch = 1;
  58616. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  58617. + gpwrdn, 0, gpwrdn.d32);
  58618. + }
  58619. +
  58620. + core_if->op_state = A_HOST;
  58621. + dwc_otg_core_init(core_if);
  58622. + dwc_otg_enable_global_interrupts(core_if);
  58623. + cil_hcd_start(core_if);
  58624. + }
  58625. +
  58626. + /* Turn on the port power bit. */
  58627. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  58628. + hprt0.b.prtpwr = 1;
  58629. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  58630. +
  58631. + /* Start the Connection timer. So a message can be displayed
  58632. + * if connect does not occur within 10 seconds. */
  58633. + cil_hcd_session_start(core_if);
  58634. + } else {
  58635. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  58636. + if (core_if->adp_enable) {
  58637. + dwc_otg_adp_probe_stop(core_if);
  58638. +
  58639. + /* Power on the core */
  58640. + if (core_if->power_down == 2) {
  58641. + gpwrdn.b.pwrdnswtch = 1;
  58642. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  58643. + gpwrdn, 0, gpwrdn.d32);
  58644. + }
  58645. +
  58646. + gpwrdn.d32 = 0;
  58647. + gpwrdn.b.pmuactv = 0;
  58648. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  58649. + gpwrdn.d32);
  58650. +
  58651. + core_if->op_state = B_PERIPHERAL;
  58652. + dwc_otg_core_init(core_if);
  58653. + dwc_otg_enable_global_interrupts(core_if);
  58654. + cil_pcd_start(core_if);
  58655. + }
  58656. + }
  58657. +#endif
  58658. + return 1;
  58659. +}
  58660. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_adp.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  58661. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_adp.h 1970-01-01 01:00:00.000000000 +0100
  58662. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_adp.h 2014-09-14 19:04:13.000000000 +0200
  58663. @@ -0,0 +1,80 @@
  58664. +/* ==========================================================================
  58665. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  58666. + * $Revision: #7 $
  58667. + * $Date: 2011/10/24 $
  58668. + * $Change: 1871159 $
  58669. + *
  58670. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  58671. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  58672. + * otherwise expressly agreed to in writing between Synopsys and you.
  58673. + *
  58674. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  58675. + * any End User Software License Agreement or Agreement for Licensed Product
  58676. + * with Synopsys or any supplement thereto. You are permitted to use and
  58677. + * redistribute this Software in source and binary forms, with or without
  58678. + * modification, provided that redistributions of source code must retain this
  58679. + * notice. You may not view, use, disclose, copy or distribute this file or
  58680. + * any information contained herein except pursuant to this license grant from
  58681. + * Synopsys. If you do not agree with this notice, including the disclaimer
  58682. + * below, then you are not authorized to use the Software.
  58683. + *
  58684. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  58685. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58686. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58687. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  58688. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  58689. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  58690. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  58691. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58692. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58693. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  58694. + * DAMAGE.
  58695. + * ========================================================================== */
  58696. +
  58697. +#ifndef __DWC_OTG_ADP_H__
  58698. +#define __DWC_OTG_ADP_H__
  58699. +
  58700. +/**
  58701. + * @file
  58702. + *
  58703. + * This file contains the Attach Detect Protocol interfaces and defines
  58704. + * (functions) and structures for Linux.
  58705. + *
  58706. + */
  58707. +
  58708. +#define DWC_OTG_ADP_UNATTACHED 0
  58709. +#define DWC_OTG_ADP_ATTACHED 1
  58710. +#define DWC_OTG_ADP_UNKOWN 2
  58711. +
  58712. +typedef struct dwc_otg_adp {
  58713. + uint32_t adp_started;
  58714. + uint32_t initial_probe;
  58715. + int32_t probe_timer_values[2];
  58716. + uint32_t probe_enabled;
  58717. + uint32_t sense_enabled;
  58718. + dwc_timer_t *sense_timer;
  58719. + uint32_t sense_timer_started;
  58720. + dwc_timer_t *vbuson_timer;
  58721. + uint32_t vbuson_timer_started;
  58722. + uint32_t attached;
  58723. + uint32_t probe_counter;
  58724. + uint32_t gpwrdn;
  58725. +} dwc_otg_adp_t;
  58726. +
  58727. +/**
  58728. + * Attach Detect Protocol functions
  58729. + */
  58730. +
  58731. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  58732. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  58733. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  58734. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  58735. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  58736. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  58737. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  58738. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  58739. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  58740. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  58741. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  58742. +
  58743. +#endif //__DWC_OTG_ADP_H__
  58744. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_attr.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  58745. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_attr.c 1970-01-01 01:00:00.000000000 +0100
  58746. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.c 2014-09-14 19:04:13.000000000 +0200
  58747. @@ -0,0 +1,1210 @@
  58748. +/* ==========================================================================
  58749. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  58750. + * $Revision: #44 $
  58751. + * $Date: 2010/11/29 $
  58752. + * $Change: 1636033 $
  58753. + *
  58754. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  58755. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  58756. + * otherwise expressly agreed to in writing between Synopsys and you.
  58757. + *
  58758. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  58759. + * any End User Software License Agreement or Agreement for Licensed Product
  58760. + * with Synopsys or any supplement thereto. You are permitted to use and
  58761. + * redistribute this Software in source and binary forms, with or without
  58762. + * modification, provided that redistributions of source code must retain this
  58763. + * notice. You may not view, use, disclose, copy or distribute this file or
  58764. + * any information contained herein except pursuant to this license grant from
  58765. + * Synopsys. If you do not agree with this notice, including the disclaimer
  58766. + * below, then you are not authorized to use the Software.
  58767. + *
  58768. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  58769. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  58770. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  58771. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  58772. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  58773. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  58774. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  58775. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  58776. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58777. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  58778. + * DAMAGE.
  58779. + * ========================================================================== */
  58780. +
  58781. +/** @file
  58782. + *
  58783. + * The diagnostic interface will provide access to the controller for
  58784. + * bringing up the hardware and testing. The Linux driver attributes
  58785. + * feature will be used to provide the Linux Diagnostic
  58786. + * Interface. These attributes are accessed through sysfs.
  58787. + */
  58788. +
  58789. +/** @page "Linux Module Attributes"
  58790. + *
  58791. + * The Linux module attributes feature is used to provide the Linux
  58792. + * Diagnostic Interface. These attributes are accessed through sysfs.
  58793. + * The diagnostic interface will provide access to the controller for
  58794. + * bringing up the hardware and testing.
  58795. +
  58796. + The following table shows the attributes.
  58797. + <table>
  58798. + <tr>
  58799. + <td><b> Name</b></td>
  58800. + <td><b> Description</b></td>
  58801. + <td><b> Access</b></td>
  58802. + </tr>
  58803. +
  58804. + <tr>
  58805. + <td> mode </td>
  58806. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  58807. + <td> Read</td>
  58808. + </tr>
  58809. +
  58810. + <tr>
  58811. + <td> hnpcapable </td>
  58812. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  58813. + Read returns the current value.</td>
  58814. + <td> Read/Write</td>
  58815. + </tr>
  58816. +
  58817. + <tr>
  58818. + <td> srpcapable </td>
  58819. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  58820. + Read returns the current value.</td>
  58821. + <td> Read/Write</td>
  58822. + </tr>
  58823. +
  58824. + <tr>
  58825. + <td> hsic_connect </td>
  58826. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  58827. + Read returns the current value.</td>
  58828. + <td> Read/Write</td>
  58829. + </tr>
  58830. +
  58831. + <tr>
  58832. + <td> inv_sel_hsic </td>
  58833. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  58834. + Read returns the current value.</td>
  58835. + <td> Read/Write</td>
  58836. + </tr>
  58837. +
  58838. + <tr>
  58839. + <td> hnp </td>
  58840. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  58841. + <td> Read/Write</td>
  58842. + </tr>
  58843. +
  58844. + <tr>
  58845. + <td> srp </td>
  58846. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  58847. + <td> Read/Write</td>
  58848. + </tr>
  58849. +
  58850. + <tr>
  58851. + <td> buspower </td>
  58852. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  58853. + <td> Read/Write</td>
  58854. + </tr>
  58855. +
  58856. + <tr>
  58857. + <td> bussuspend </td>
  58858. + <td> Suspends the USB bus.</td>
  58859. + <td> Read/Write</td>
  58860. + </tr>
  58861. +
  58862. + <tr>
  58863. + <td> busconnected </td>
  58864. + <td> Gets the connection status of the bus</td>
  58865. + <td> Read</td>
  58866. + </tr>
  58867. +
  58868. + <tr>
  58869. + <td> gotgctl </td>
  58870. + <td> Gets or sets the Core Control Status Register.</td>
  58871. + <td> Read/Write</td>
  58872. + </tr>
  58873. +
  58874. + <tr>
  58875. + <td> gusbcfg </td>
  58876. + <td> Gets or sets the Core USB Configuration Register</td>
  58877. + <td> Read/Write</td>
  58878. + </tr>
  58879. +
  58880. + <tr>
  58881. + <td> grxfsiz </td>
  58882. + <td> Gets or sets the Receive FIFO Size Register</td>
  58883. + <td> Read/Write</td>
  58884. + </tr>
  58885. +
  58886. + <tr>
  58887. + <td> gnptxfsiz </td>
  58888. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  58889. + <td> Read/Write</td>
  58890. + </tr>
  58891. +
  58892. + <tr>
  58893. + <td> gpvndctl </td>
  58894. + <td> Gets or sets the PHY Vendor Control Register</td>
  58895. + <td> Read/Write</td>
  58896. + </tr>
  58897. +
  58898. + <tr>
  58899. + <td> ggpio </td>
  58900. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  58901. + or sets the upper 16 bits.</td>
  58902. + <td> Read/Write</td>
  58903. + </tr>
  58904. +
  58905. + <tr>
  58906. + <td> guid </td>
  58907. + <td> Gets or sets the value of the User ID Register</td>
  58908. + <td> Read/Write</td>
  58909. + </tr>
  58910. +
  58911. + <tr>
  58912. + <td> gsnpsid </td>
  58913. + <td> Gets the value of the Synopsys ID Regester</td>
  58914. + <td> Read</td>
  58915. + </tr>
  58916. +
  58917. + <tr>
  58918. + <td> devspeed </td>
  58919. + <td> Gets or sets the device speed setting in the DCFG register</td>
  58920. + <td> Read/Write</td>
  58921. + </tr>
  58922. +
  58923. + <tr>
  58924. + <td> enumspeed </td>
  58925. + <td> Gets the device enumeration Speed.</td>
  58926. + <td> Read</td>
  58927. + </tr>
  58928. +
  58929. + <tr>
  58930. + <td> hptxfsiz </td>
  58931. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  58932. + <td> Read</td>
  58933. + </tr>
  58934. +
  58935. + <tr>
  58936. + <td> hprt0 </td>
  58937. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  58938. + <td> Read/Write</td>
  58939. + </tr>
  58940. +
  58941. + <tr>
  58942. + <td> regoffset </td>
  58943. + <td> Sets the register offset for the next Register Access</td>
  58944. + <td> Read/Write</td>
  58945. + </tr>
  58946. +
  58947. + <tr>
  58948. + <td> regvalue </td>
  58949. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  58950. + <td> Read/Write</td>
  58951. + </tr>
  58952. +
  58953. + <tr>
  58954. + <td> remote_wakeup </td>
  58955. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  58956. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  58957. + Wakeup signalling bit in the Device Control Register is set for 1
  58958. + milli-second.</td>
  58959. + <td> Read/Write</td>
  58960. + </tr>
  58961. +
  58962. + <tr>
  58963. + <td> rem_wakeup_pwrdn </td>
  58964. + <td> On read, shows the status core - hibernated or not. On write, initiates
  58965. + a remote wakeup of the device from Hibernation. </td>
  58966. + <td> Read/Write</td>
  58967. + </tr>
  58968. +
  58969. + <tr>
  58970. + <td> mode_ch_tim_en </td>
  58971. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  58972. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  58973. + after Suspend or LPM. </td>
  58974. + <td> Read/Write</td>
  58975. + </tr>
  58976. +
  58977. + <tr>
  58978. + <td> fr_interval </td>
  58979. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  58980. + reload HFIR register during runtime. The application can write a value to this
  58981. + register only after the Port Enable bit of the Host Port Control and Status
  58982. + register (HPRT.PrtEnaPort) has been set </td>
  58983. + <td> Read/Write</td>
  58984. + </tr>
  58985. +
  58986. + <tr>
  58987. + <td> disconnect_us </td>
  58988. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  58989. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  58990. + <td> Read/Write</td>
  58991. + </tr>
  58992. +
  58993. + <tr>
  58994. + <td> regdump </td>
  58995. + <td> Dumps the contents of core registers.</td>
  58996. + <td> Read</td>
  58997. + </tr>
  58998. +
  58999. + <tr>
  59000. + <td> spramdump </td>
  59001. + <td> Dumps the contents of core registers.</td>
  59002. + <td> Read</td>
  59003. + </tr>
  59004. +
  59005. + <tr>
  59006. + <td> hcddump </td>
  59007. + <td> Dumps the current HCD state.</td>
  59008. + <td> Read</td>
  59009. + </tr>
  59010. +
  59011. + <tr>
  59012. + <td> hcd_frrem </td>
  59013. + <td> Shows the average value of the Frame Remaining
  59014. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  59015. + occurs. This can be used to determine the average interrupt latency. Also
  59016. + shows the average Frame Remaining value for start_transfer and the "a" and
  59017. + "b" sample points. The "a" and "b" sample points may be used during debugging
  59018. + bto determine how long it takes to execute a section of the HCD code.</td>
  59019. + <td> Read</td>
  59020. + </tr>
  59021. +
  59022. + <tr>
  59023. + <td> rd_reg_test </td>
  59024. + <td> Displays the time required to read the GNPTXFSIZ register many times
  59025. + (the output shows the number of times the register is read).
  59026. + <td> Read</td>
  59027. + </tr>
  59028. +
  59029. + <tr>
  59030. + <td> wr_reg_test </td>
  59031. + <td> Displays the time required to write the GNPTXFSIZ register many times
  59032. + (the output shows the number of times the register is written).
  59033. + <td> Read</td>
  59034. + </tr>
  59035. +
  59036. + <tr>
  59037. + <td> lpm_response </td>
  59038. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  59039. + <td> Write</td>
  59040. + </tr>
  59041. +
  59042. + <tr>
  59043. + <td> sleep_status </td>
  59044. + <td> Shows sleep status of device.
  59045. + <td> Read</td>
  59046. + </tr>
  59047. +
  59048. + </table>
  59049. +
  59050. + Example usage:
  59051. + To get the current mode:
  59052. + cat /sys/devices/lm0/mode
  59053. +
  59054. + To power down the USB:
  59055. + echo 0 > /sys/devices/lm0/buspower
  59056. + */
  59057. +
  59058. +#include "dwc_otg_os_dep.h"
  59059. +#include "dwc_os.h"
  59060. +#include "dwc_otg_driver.h"
  59061. +#include "dwc_otg_attr.h"
  59062. +#include "dwc_otg_core_if.h"
  59063. +#include "dwc_otg_pcd_if.h"
  59064. +#include "dwc_otg_hcd_if.h"
  59065. +
  59066. +/*
  59067. + * MACROs for defining sysfs attribute
  59068. + */
  59069. +#ifdef LM_INTERFACE
  59070. +
  59071. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  59072. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  59073. +{ \
  59074. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  59075. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  59076. + uint32_t val; \
  59077. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  59078. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  59079. +}
  59080. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  59081. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  59082. + const char *buf, size_t count) \
  59083. +{ \
  59084. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  59085. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  59086. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  59087. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  59088. + return count; \
  59089. +}
  59090. +
  59091. +#elif defined(PCI_INTERFACE)
  59092. +
  59093. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  59094. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  59095. +{ \
  59096. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  59097. + uint32_t val; \
  59098. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  59099. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  59100. +}
  59101. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  59102. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  59103. + const char *buf, size_t count) \
  59104. +{ \
  59105. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  59106. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  59107. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  59108. + return count; \
  59109. +}
  59110. +
  59111. +#elif defined(PLATFORM_INTERFACE)
  59112. +
  59113. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  59114. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  59115. +{ \
  59116. + struct platform_device *platform_dev = \
  59117. + container_of(_dev, struct platform_device, dev); \
  59118. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  59119. + uint32_t val; \
  59120. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  59121. + __func__, _dev, platform_dev, otg_dev); \
  59122. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  59123. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  59124. +}
  59125. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  59126. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  59127. + const char *buf, size_t count) \
  59128. +{ \
  59129. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  59130. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  59131. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  59132. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  59133. + return count; \
  59134. +}
  59135. +#endif
  59136. +
  59137. +/*
  59138. + * MACROs for defining sysfs attribute for 32-bit registers
  59139. + */
  59140. +#ifdef LM_INTERFACE
  59141. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  59142. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  59143. +{ \
  59144. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  59145. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  59146. + uint32_t val; \
  59147. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  59148. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  59149. +}
  59150. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  59151. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  59152. + const char *buf, size_t count) \
  59153. +{ \
  59154. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  59155. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  59156. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  59157. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  59158. + return count; \
  59159. +}
  59160. +#elif defined(PCI_INTERFACE)
  59161. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  59162. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  59163. +{ \
  59164. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  59165. + uint32_t val; \
  59166. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  59167. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  59168. +}
  59169. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  59170. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  59171. + const char *buf, size_t count) \
  59172. +{ \
  59173. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  59174. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  59175. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  59176. + return count; \
  59177. +}
  59178. +
  59179. +#elif defined(PLATFORM_INTERFACE)
  59180. +#include "dwc_otg_dbg.h"
  59181. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  59182. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  59183. +{ \
  59184. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  59185. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  59186. + uint32_t val; \
  59187. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  59188. + __func__, _dev, platform_dev, otg_dev); \
  59189. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  59190. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  59191. +}
  59192. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  59193. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  59194. + const char *buf, size_t count) \
  59195. +{ \
  59196. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  59197. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  59198. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  59199. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  59200. + return count; \
  59201. +}
  59202. +
  59203. +#endif
  59204. +
  59205. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  59206. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  59207. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  59208. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  59209. +
  59210. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  59211. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  59212. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  59213. +
  59214. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  59215. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  59216. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  59217. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  59218. +
  59219. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  59220. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  59221. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  59222. +
  59223. +/** @name Functions for Show/Store of Attributes */
  59224. +/**@{*/
  59225. +
  59226. +/**
  59227. + * Helper function returning the otg_device structure of the given device
  59228. + */
  59229. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  59230. +{
  59231. + dwc_otg_device_t *otg_dev;
  59232. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  59233. + return otg_dev;
  59234. +}
  59235. +
  59236. +/**
  59237. + * Show the register offset of the Register Access.
  59238. + */
  59239. +static ssize_t regoffset_show(struct device *_dev,
  59240. + struct device_attribute *attr, char *buf)
  59241. +{
  59242. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59243. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  59244. + otg_dev->os_dep.reg_offset);
  59245. +}
  59246. +
  59247. +/**
  59248. + * Set the register offset for the next Register Access Read/Write
  59249. + */
  59250. +static ssize_t regoffset_store(struct device *_dev,
  59251. + struct device_attribute *attr,
  59252. + const char *buf, size_t count)
  59253. +{
  59254. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59255. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  59256. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  59257. + if (offset < SZ_256K) {
  59258. +#elif defined(PCI_INTERFACE)
  59259. + if (offset < 0x00040000) {
  59260. +#endif
  59261. + otg_dev->os_dep.reg_offset = offset;
  59262. + } else {
  59263. + dev_err(_dev, "invalid offset\n");
  59264. + }
  59265. +
  59266. + return count;
  59267. +}
  59268. +
  59269. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  59270. +
  59271. +/**
  59272. + * Show the value of the register at the offset in the reg_offset
  59273. + * attribute.
  59274. + */
  59275. +static ssize_t regvalue_show(struct device *_dev,
  59276. + struct device_attribute *attr, char *buf)
  59277. +{
  59278. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59279. + uint32_t val;
  59280. + volatile uint32_t *addr;
  59281. +
  59282. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  59283. + /* Calculate the address */
  59284. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  59285. + (uint8_t *) otg_dev->os_dep.base);
  59286. + val = DWC_READ_REG32(addr);
  59287. + return snprintf(buf,
  59288. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  59289. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  59290. + val);
  59291. + } else {
  59292. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  59293. + return sprintf(buf, "invalid offset\n");
  59294. + }
  59295. +}
  59296. +
  59297. +/**
  59298. + * Store the value in the register at the offset in the reg_offset
  59299. + * attribute.
  59300. + *
  59301. + */
  59302. +static ssize_t regvalue_store(struct device *_dev,
  59303. + struct device_attribute *attr,
  59304. + const char *buf, size_t count)
  59305. +{
  59306. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59307. + volatile uint32_t *addr;
  59308. + uint32_t val = simple_strtoul(buf, NULL, 16);
  59309. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  59310. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  59311. + /* Calculate the address */
  59312. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  59313. + (uint8_t *) otg_dev->os_dep.base);
  59314. + DWC_WRITE_REG32(addr, val);
  59315. + } else {
  59316. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  59317. + otg_dev->os_dep.reg_offset);
  59318. + }
  59319. + return count;
  59320. +}
  59321. +
  59322. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  59323. +
  59324. +/*
  59325. + * Attributes
  59326. + */
  59327. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  59328. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  59329. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  59330. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  59331. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  59332. +
  59333. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  59334. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  59335. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  59336. +
  59337. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  59338. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  59339. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  59340. + "GUSBCFG");
  59341. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  59342. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  59343. + "GRXFSIZ");
  59344. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  59345. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  59346. + "GNPTXFSIZ");
  59347. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  59348. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  59349. + "GPVNDCTL");
  59350. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  59351. + &(otg_dev->core_if->core_global_regs->ggpio),
  59352. + "GGPIO");
  59353. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  59354. + "GUID");
  59355. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  59356. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  59357. + "GSNPSID");
  59358. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  59359. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  59360. +
  59361. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  59362. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  59363. + "HPTXFSIZ");
  59364. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  59365. +
  59366. +/**
  59367. + * @todo Add code to initiate the HNP.
  59368. + */
  59369. +/**
  59370. + * Show the HNP status bit
  59371. + */
  59372. +static ssize_t hnp_show(struct device *_dev,
  59373. + struct device_attribute *attr, char *buf)
  59374. +{
  59375. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59376. + return sprintf(buf, "HstNegScs = 0x%x\n",
  59377. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  59378. +}
  59379. +
  59380. +/**
  59381. + * Set the HNP Request bit
  59382. + */
  59383. +static ssize_t hnp_store(struct device *_dev,
  59384. + struct device_attribute *attr,
  59385. + const char *buf, size_t count)
  59386. +{
  59387. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59388. + uint32_t in = simple_strtoul(buf, NULL, 16);
  59389. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  59390. + return count;
  59391. +}
  59392. +
  59393. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  59394. +
  59395. +/**
  59396. + * @todo Add code to initiate the SRP.
  59397. + */
  59398. +/**
  59399. + * Show the SRP status bit
  59400. + */
  59401. +static ssize_t srp_show(struct device *_dev,
  59402. + struct device_attribute *attr, char *buf)
  59403. +{
  59404. +#ifndef DWC_HOST_ONLY
  59405. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59406. + return sprintf(buf, "SesReqScs = 0x%x\n",
  59407. + dwc_otg_get_srpstatus(otg_dev->core_if));
  59408. +#else
  59409. + return sprintf(buf, "Host Only Mode!\n");
  59410. +#endif
  59411. +}
  59412. +
  59413. +/**
  59414. + * Set the SRP Request bit
  59415. + */
  59416. +static ssize_t srp_store(struct device *_dev,
  59417. + struct device_attribute *attr,
  59418. + const char *buf, size_t count)
  59419. +{
  59420. +#ifndef DWC_HOST_ONLY
  59421. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59422. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  59423. +#endif
  59424. + return count;
  59425. +}
  59426. +
  59427. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  59428. +
  59429. +/**
  59430. + * @todo Need to do more for power on/off?
  59431. + */
  59432. +/**
  59433. + * Show the Bus Power status
  59434. + */
  59435. +static ssize_t buspower_show(struct device *_dev,
  59436. + struct device_attribute *attr, char *buf)
  59437. +{
  59438. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59439. + return sprintf(buf, "Bus Power = 0x%x\n",
  59440. + dwc_otg_get_prtpower(otg_dev->core_if));
  59441. +}
  59442. +
  59443. +/**
  59444. + * Set the Bus Power status
  59445. + */
  59446. +static ssize_t buspower_store(struct device *_dev,
  59447. + struct device_attribute *attr,
  59448. + const char *buf, size_t count)
  59449. +{
  59450. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59451. + uint32_t on = simple_strtoul(buf, NULL, 16);
  59452. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  59453. + return count;
  59454. +}
  59455. +
  59456. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  59457. +
  59458. +/**
  59459. + * @todo Need to do more for suspend?
  59460. + */
  59461. +/**
  59462. + * Show the Bus Suspend status
  59463. + */
  59464. +static ssize_t bussuspend_show(struct device *_dev,
  59465. + struct device_attribute *attr, char *buf)
  59466. +{
  59467. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59468. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  59469. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  59470. +}
  59471. +
  59472. +/**
  59473. + * Set the Bus Suspend status
  59474. + */
  59475. +static ssize_t bussuspend_store(struct device *_dev,
  59476. + struct device_attribute *attr,
  59477. + const char *buf, size_t count)
  59478. +{
  59479. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59480. + uint32_t in = simple_strtoul(buf, NULL, 16);
  59481. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  59482. + return count;
  59483. +}
  59484. +
  59485. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  59486. +
  59487. +/**
  59488. + * Show the Mode Change Ready Timer status
  59489. + */
  59490. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  59491. + struct device_attribute *attr, char *buf)
  59492. +{
  59493. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59494. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  59495. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  59496. +}
  59497. +
  59498. +/**
  59499. + * Set the Mode Change Ready Timer status
  59500. + */
  59501. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  59502. + struct device_attribute *attr,
  59503. + const char *buf, size_t count)
  59504. +{
  59505. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59506. + uint32_t in = simple_strtoul(buf, NULL, 16);
  59507. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  59508. + return count;
  59509. +}
  59510. +
  59511. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  59512. +
  59513. +/**
  59514. + * Show the value of HFIR Frame Interval bitfield
  59515. + */
  59516. +static ssize_t fr_interval_show(struct device *_dev,
  59517. + struct device_attribute *attr, char *buf)
  59518. +{
  59519. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59520. + return sprintf(buf, "Frame Interval = 0x%x\n",
  59521. + dwc_otg_get_fr_interval(otg_dev->core_if));
  59522. +}
  59523. +
  59524. +/**
  59525. + * Set the HFIR Frame Interval value
  59526. + */
  59527. +static ssize_t fr_interval_store(struct device *_dev,
  59528. + struct device_attribute *attr,
  59529. + const char *buf, size_t count)
  59530. +{
  59531. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59532. + uint32_t in = simple_strtoul(buf, NULL, 10);
  59533. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  59534. + return count;
  59535. +}
  59536. +
  59537. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  59538. +
  59539. +/**
  59540. + * Show the status of Remote Wakeup.
  59541. + */
  59542. +static ssize_t remote_wakeup_show(struct device *_dev,
  59543. + struct device_attribute *attr, char *buf)
  59544. +{
  59545. +#ifndef DWC_HOST_ONLY
  59546. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59547. +
  59548. + return sprintf(buf,
  59549. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  59550. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  59551. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  59552. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  59553. +#else
  59554. + return sprintf(buf, "Host Only Mode!\n");
  59555. +#endif /* DWC_HOST_ONLY */
  59556. +}
  59557. +
  59558. +/**
  59559. + * Initiate a remote wakeup of the host. The Device control register
  59560. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  59561. + * flag is set.
  59562. + *
  59563. + */
  59564. +static ssize_t remote_wakeup_store(struct device *_dev,
  59565. + struct device_attribute *attr,
  59566. + const char *buf, size_t count)
  59567. +{
  59568. +#ifndef DWC_HOST_ONLY
  59569. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59570. + uint32_t val = simple_strtoul(buf, NULL, 16);
  59571. +
  59572. + if (val & 1) {
  59573. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  59574. + } else {
  59575. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  59576. + }
  59577. +#endif /* DWC_HOST_ONLY */
  59578. + return count;
  59579. +}
  59580. +
  59581. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  59582. + remote_wakeup_store);
  59583. +
  59584. +/**
  59585. + * Show the whether core is hibernated or not.
  59586. + */
  59587. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  59588. + struct device_attribute *attr, char *buf)
  59589. +{
  59590. +#ifndef DWC_HOST_ONLY
  59591. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59592. +
  59593. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  59594. + DWC_PRINTF("Core is in hibernation\n");
  59595. + } else {
  59596. + DWC_PRINTF("Core is not in hibernation\n");
  59597. + }
  59598. +#endif /* DWC_HOST_ONLY */
  59599. + return 0;
  59600. +}
  59601. +
  59602. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  59603. + int rem_wakeup, int reset);
  59604. +
  59605. +/**
  59606. + * Initiate a remote wakeup of the device to exit from hibernation.
  59607. + */
  59608. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  59609. + struct device_attribute *attr,
  59610. + const char *buf, size_t count)
  59611. +{
  59612. +#ifndef DWC_HOST_ONLY
  59613. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59614. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  59615. +#endif
  59616. + return count;
  59617. +}
  59618. +
  59619. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  59620. + rem_wakeup_pwrdn_store);
  59621. +
  59622. +static ssize_t disconnect_us(struct device *_dev,
  59623. + struct device_attribute *attr,
  59624. + const char *buf, size_t count)
  59625. +{
  59626. +
  59627. +#ifndef DWC_HOST_ONLY
  59628. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59629. + uint32_t val = simple_strtoul(buf, NULL, 16);
  59630. + DWC_PRINTF("The Passed value is %04x\n", val);
  59631. +
  59632. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  59633. +
  59634. +#endif /* DWC_HOST_ONLY */
  59635. + return count;
  59636. +}
  59637. +
  59638. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  59639. +
  59640. +/**
  59641. + * Dump global registers and either host or device registers (depending on the
  59642. + * current mode of the core).
  59643. + */
  59644. +static ssize_t regdump_show(struct device *_dev,
  59645. + struct device_attribute *attr, char *buf)
  59646. +{
  59647. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59648. +
  59649. + dwc_otg_dump_global_registers(otg_dev->core_if);
  59650. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  59651. + dwc_otg_dump_host_registers(otg_dev->core_if);
  59652. + } else {
  59653. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  59654. +
  59655. + }
  59656. + return sprintf(buf, "Register Dump\n");
  59657. +}
  59658. +
  59659. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  59660. +
  59661. +/**
  59662. + * Dump global registers and either host or device registers (depending on the
  59663. + * current mode of the core).
  59664. + */
  59665. +static ssize_t spramdump_show(struct device *_dev,
  59666. + struct device_attribute *attr, char *buf)
  59667. +{
  59668. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59669. +
  59670. + //dwc_otg_dump_spram(otg_dev->core_if);
  59671. +
  59672. + return sprintf(buf, "SPRAM Dump\n");
  59673. +}
  59674. +
  59675. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  59676. +
  59677. +/**
  59678. + * Dump the current hcd state.
  59679. + */
  59680. +static ssize_t hcddump_show(struct device *_dev,
  59681. + struct device_attribute *attr, char *buf)
  59682. +{
  59683. +#ifndef DWC_DEVICE_ONLY
  59684. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59685. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  59686. +#endif /* DWC_DEVICE_ONLY */
  59687. + return sprintf(buf, "HCD Dump\n");
  59688. +}
  59689. +
  59690. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  59691. +
  59692. +/**
  59693. + * Dump the average frame remaining at SOF. This can be used to
  59694. + * determine average interrupt latency. Frame remaining is also shown for
  59695. + * start transfer and two additional sample points.
  59696. + */
  59697. +static ssize_t hcd_frrem_show(struct device *_dev,
  59698. + struct device_attribute *attr, char *buf)
  59699. +{
  59700. +#ifndef DWC_DEVICE_ONLY
  59701. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59702. +
  59703. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  59704. +#endif /* DWC_DEVICE_ONLY */
  59705. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  59706. +}
  59707. +
  59708. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  59709. +
  59710. +/**
  59711. + * Displays the time required to read the GNPTXFSIZ register many times (the
  59712. + * output shows the number of times the register is read).
  59713. + */
  59714. +#define RW_REG_COUNT 10000000
  59715. +#define MSEC_PER_JIFFIE 1000/HZ
  59716. +static ssize_t rd_reg_test_show(struct device *_dev,
  59717. + struct device_attribute *attr, char *buf)
  59718. +{
  59719. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59720. + int i;
  59721. + int time;
  59722. + int start_jiffies;
  59723. +
  59724. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  59725. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  59726. + start_jiffies = jiffies;
  59727. + for (i = 0; i < RW_REG_COUNT; i++) {
  59728. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  59729. + }
  59730. + time = jiffies - start_jiffies;
  59731. + return sprintf(buf,
  59732. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  59733. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  59734. +}
  59735. +
  59736. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  59737. +
  59738. +/**
  59739. + * Displays the time required to write the GNPTXFSIZ register many times (the
  59740. + * output shows the number of times the register is written).
  59741. + */
  59742. +static ssize_t wr_reg_test_show(struct device *_dev,
  59743. + struct device_attribute *attr, char *buf)
  59744. +{
  59745. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59746. + uint32_t reg_val;
  59747. + int i;
  59748. + int time;
  59749. + int start_jiffies;
  59750. +
  59751. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  59752. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  59753. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  59754. + start_jiffies = jiffies;
  59755. + for (i = 0; i < RW_REG_COUNT; i++) {
  59756. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  59757. + }
  59758. + time = jiffies - start_jiffies;
  59759. + return sprintf(buf,
  59760. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  59761. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  59762. +}
  59763. +
  59764. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  59765. +
  59766. +#ifdef CONFIG_USB_DWC_OTG_LPM
  59767. +
  59768. +/**
  59769. +* Show the lpm_response attribute.
  59770. +*/
  59771. +static ssize_t lpmresp_show(struct device *_dev,
  59772. + struct device_attribute *attr, char *buf)
  59773. +{
  59774. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59775. +
  59776. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  59777. + return sprintf(buf, "** LPM is DISABLED **\n");
  59778. +
  59779. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  59780. + return sprintf(buf, "** Current mode is not device mode\n");
  59781. + }
  59782. + return sprintf(buf, "lpm_response = %d\n",
  59783. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  59784. +}
  59785. +
  59786. +/**
  59787. +* Store the lpm_response attribute.
  59788. +*/
  59789. +static ssize_t lpmresp_store(struct device *_dev,
  59790. + struct device_attribute *attr,
  59791. + const char *buf, size_t count)
  59792. +{
  59793. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59794. + uint32_t val = simple_strtoul(buf, NULL, 16);
  59795. +
  59796. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  59797. + return 0;
  59798. + }
  59799. +
  59800. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  59801. + return 0;
  59802. + }
  59803. +
  59804. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  59805. + return count;
  59806. +}
  59807. +
  59808. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  59809. +
  59810. +/**
  59811. +* Show the sleep_status attribute.
  59812. +*/
  59813. +static ssize_t sleepstatus_show(struct device *_dev,
  59814. + struct device_attribute *attr, char *buf)
  59815. +{
  59816. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59817. + return sprintf(buf, "Sleep Status = %d\n",
  59818. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  59819. +}
  59820. +
  59821. +/**
  59822. + * Store the sleep_status attribure.
  59823. + */
  59824. +static ssize_t sleepstatus_store(struct device *_dev,
  59825. + struct device_attribute *attr,
  59826. + const char *buf, size_t count)
  59827. +{
  59828. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  59829. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  59830. +
  59831. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  59832. + if (dwc_otg_is_host_mode(core_if)) {
  59833. +
  59834. + DWC_PRINTF("Host initiated resume\n");
  59835. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  59836. + }
  59837. + }
  59838. +
  59839. + return count;
  59840. +}
  59841. +
  59842. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  59843. + sleepstatus_store);
  59844. +
  59845. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  59846. +
  59847. +/**@}*/
  59848. +
  59849. +/**
  59850. + * Create the device files
  59851. + */
  59852. +void dwc_otg_attr_create(
  59853. +#ifdef LM_INTERFACE
  59854. + struct lm_device *dev
  59855. +#elif defined(PCI_INTERFACE)
  59856. + struct pci_dev *dev
  59857. +#elif defined(PLATFORM_INTERFACE)
  59858. + struct platform_device *dev
  59859. +#endif
  59860. + )
  59861. +{
  59862. + int error;
  59863. +
  59864. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  59865. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  59866. + error = device_create_file(&dev->dev, &dev_attr_mode);
  59867. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  59868. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  59869. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  59870. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  59871. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  59872. + error = device_create_file(&dev->dev, &dev_attr_srp);
  59873. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  59874. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  59875. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  59876. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  59877. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  59878. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  59879. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  59880. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  59881. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  59882. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  59883. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  59884. + error = device_create_file(&dev->dev, &dev_attr_guid);
  59885. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  59886. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  59887. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  59888. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  59889. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  59890. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  59891. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  59892. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  59893. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  59894. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  59895. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  59896. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  59897. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  59898. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  59899. +#ifdef CONFIG_USB_DWC_OTG_LPM
  59900. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  59901. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  59902. +#endif
  59903. +}
  59904. +
  59905. +/**
  59906. + * Remove the device files
  59907. + */
  59908. +void dwc_otg_attr_remove(
  59909. +#ifdef LM_INTERFACE
  59910. + struct lm_device *dev
  59911. +#elif defined(PCI_INTERFACE)
  59912. + struct pci_dev *dev
  59913. +#elif defined(PLATFORM_INTERFACE)
  59914. + struct platform_device *dev
  59915. +#endif
  59916. + )
  59917. +{
  59918. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  59919. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  59920. + device_remove_file(&dev->dev, &dev_attr_mode);
  59921. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  59922. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  59923. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  59924. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  59925. + device_remove_file(&dev->dev, &dev_attr_hnp);
  59926. + device_remove_file(&dev->dev, &dev_attr_srp);
  59927. + device_remove_file(&dev->dev, &dev_attr_buspower);
  59928. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  59929. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  59930. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  59931. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  59932. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  59933. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  59934. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  59935. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  59936. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  59937. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  59938. + device_remove_file(&dev->dev, &dev_attr_guid);
  59939. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  59940. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  59941. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  59942. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  59943. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  59944. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  59945. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  59946. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  59947. + device_remove_file(&dev->dev, &dev_attr_regdump);
  59948. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  59949. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  59950. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  59951. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  59952. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  59953. +#ifdef CONFIG_USB_DWC_OTG_LPM
  59954. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  59955. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  59956. +#endif
  59957. +}
  59958. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_attr.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  59959. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_attr.h 1970-01-01 01:00:00.000000000 +0100
  59960. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_attr.h 2014-09-14 19:04:13.000000000 +0200
  59961. @@ -0,0 +1,89 @@
  59962. +/* ==========================================================================
  59963. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  59964. + * $Revision: #13 $
  59965. + * $Date: 2010/06/21 $
  59966. + * $Change: 1532021 $
  59967. + *
  59968. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  59969. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  59970. + * otherwise expressly agreed to in writing between Synopsys and you.
  59971. + *
  59972. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  59973. + * any End User Software License Agreement or Agreement for Licensed Product
  59974. + * with Synopsys or any supplement thereto. You are permitted to use and
  59975. + * redistribute this Software in source and binary forms, with or without
  59976. + * modification, provided that redistributions of source code must retain this
  59977. + * notice. You may not view, use, disclose, copy or distribute this file or
  59978. + * any information contained herein except pursuant to this license grant from
  59979. + * Synopsys. If you do not agree with this notice, including the disclaimer
  59980. + * below, then you are not authorized to use the Software.
  59981. + *
  59982. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  59983. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  59984. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  59985. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  59986. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  59987. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  59988. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  59989. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  59990. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  59991. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  59992. + * DAMAGE.
  59993. + * ========================================================================== */
  59994. +
  59995. +#if !defined(__DWC_OTG_ATTR_H__)
  59996. +#define __DWC_OTG_ATTR_H__
  59997. +
  59998. +/** @file
  59999. + * This file contains the interface to the Linux device attributes.
  60000. + */
  60001. +extern struct device_attribute dev_attr_regoffset;
  60002. +extern struct device_attribute dev_attr_regvalue;
  60003. +
  60004. +extern struct device_attribute dev_attr_mode;
  60005. +extern struct device_attribute dev_attr_hnpcapable;
  60006. +extern struct device_attribute dev_attr_srpcapable;
  60007. +extern struct device_attribute dev_attr_hnp;
  60008. +extern struct device_attribute dev_attr_srp;
  60009. +extern struct device_attribute dev_attr_buspower;
  60010. +extern struct device_attribute dev_attr_bussuspend;
  60011. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  60012. +extern struct device_attribute dev_attr_fr_interval;
  60013. +extern struct device_attribute dev_attr_busconnected;
  60014. +extern struct device_attribute dev_attr_gotgctl;
  60015. +extern struct device_attribute dev_attr_gusbcfg;
  60016. +extern struct device_attribute dev_attr_grxfsiz;
  60017. +extern struct device_attribute dev_attr_gnptxfsiz;
  60018. +extern struct device_attribute dev_attr_gpvndctl;
  60019. +extern struct device_attribute dev_attr_ggpio;
  60020. +extern struct device_attribute dev_attr_guid;
  60021. +extern struct device_attribute dev_attr_gsnpsid;
  60022. +extern struct device_attribute dev_attr_devspeed;
  60023. +extern struct device_attribute dev_attr_enumspeed;
  60024. +extern struct device_attribute dev_attr_hptxfsiz;
  60025. +extern struct device_attribute dev_attr_hprt0;
  60026. +#ifdef CONFIG_USB_DWC_OTG_LPM
  60027. +extern struct device_attribute dev_attr_lpm_response;
  60028. +extern struct device_attribute devi_attr_sleep_status;
  60029. +#endif
  60030. +
  60031. +void dwc_otg_attr_create(
  60032. +#ifdef LM_INTERFACE
  60033. + struct lm_device *dev
  60034. +#elif defined(PCI_INTERFACE)
  60035. + struct pci_dev *dev
  60036. +#elif defined(PLATFORM_INTERFACE)
  60037. + struct platform_device *dev
  60038. +#endif
  60039. + );
  60040. +
  60041. +void dwc_otg_attr_remove(
  60042. +#ifdef LM_INTERFACE
  60043. + struct lm_device *dev
  60044. +#elif defined(PCI_INTERFACE)
  60045. + struct pci_dev *dev
  60046. +#elif defined(PLATFORM_INTERFACE)
  60047. + struct platform_device *dev
  60048. +#endif
  60049. + );
  60050. +#endif
  60051. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cfi.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  60052. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 1970-01-01 01:00:00.000000000 +0100
  60053. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.c 2014-09-14 19:04:13.000000000 +0200
  60054. @@ -0,0 +1,1876 @@
  60055. +/* ==========================================================================
  60056. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  60057. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  60058. + * otherwise expressly agreed to in writing between Synopsys and you.
  60059. + *
  60060. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  60061. + * any End User Software License Agreement or Agreement for Licensed Product
  60062. + * with Synopsys or any supplement thereto. You are permitted to use and
  60063. + * redistribute this Software in source and binary forms, with or without
  60064. + * modification, provided that redistributions of source code must retain this
  60065. + * notice. You may not view, use, disclose, copy or distribute this file or
  60066. + * any information contained herein except pursuant to this license grant from
  60067. + * Synopsys. If you do not agree with this notice, including the disclaimer
  60068. + * below, then you are not authorized to use the Software.
  60069. + *
  60070. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  60071. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60072. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  60073. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  60074. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  60075. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  60076. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  60077. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  60078. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  60079. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  60080. + * DAMAGE.
  60081. + * ========================================================================== */
  60082. +
  60083. +/** @file
  60084. + *
  60085. + * This file contains the most of the CFI(Core Feature Interface)
  60086. + * implementation for the OTG.
  60087. + */
  60088. +
  60089. +#ifdef DWC_UTE_CFI
  60090. +
  60091. +#include "dwc_otg_pcd.h"
  60092. +#include "dwc_otg_cfi.h"
  60093. +
  60094. +/** This definition should actually migrate to the Portability Library */
  60095. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  60096. +
  60097. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  60098. +
  60099. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  60100. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  60101. + struct dwc_otg_pcd *pcd,
  60102. + struct cfi_usb_ctrlrequest *ctrl_req);
  60103. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  60104. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  60105. + struct cfi_usb_ctrlrequest *req);
  60106. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  60107. + struct cfi_usb_ctrlrequest *req);
  60108. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  60109. + struct cfi_usb_ctrlrequest *req);
  60110. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  60111. + struct cfi_usb_ctrlrequest *req);
  60112. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  60113. +
  60114. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  60115. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  60116. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  60117. +
  60118. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  60119. +
  60120. +/** This is the header of the all features descriptor */
  60121. +static cfi_all_features_header_t all_props_desc_header = {
  60122. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  60123. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  60124. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  60125. +};
  60126. +
  60127. +/** This is an array of statically allocated feature descriptors */
  60128. +static cfi_feature_desc_header_t prop_descs[] = {
  60129. +
  60130. + /* FT_ID_DMA_MODE */
  60131. + {
  60132. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  60133. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60134. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  60135. + },
  60136. +
  60137. + /* FT_ID_DMA_BUFFER_SETUP */
  60138. + {
  60139. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  60140. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60141. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  60142. + },
  60143. +
  60144. + /* FT_ID_DMA_BUFF_ALIGN */
  60145. + {
  60146. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  60147. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60148. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  60149. + },
  60150. +
  60151. + /* FT_ID_DMA_CONCAT_SETUP */
  60152. + {
  60153. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  60154. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60155. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  60156. + },
  60157. +
  60158. + /* FT_ID_DMA_CIRCULAR */
  60159. + {
  60160. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  60161. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60162. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  60163. + },
  60164. +
  60165. + /* FT_ID_THRESHOLD_SETUP */
  60166. + {
  60167. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  60168. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60169. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  60170. + },
  60171. +
  60172. + /* FT_ID_DFIFO_DEPTH */
  60173. + {
  60174. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  60175. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  60176. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  60177. + },
  60178. +
  60179. + /* FT_ID_TX_FIFO_DEPTH */
  60180. + {
  60181. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  60182. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60183. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  60184. + },
  60185. +
  60186. + /* FT_ID_RX_FIFO_DEPTH */
  60187. + {
  60188. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  60189. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  60190. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  60191. + }
  60192. +};
  60193. +
  60194. +/** The table of feature names */
  60195. +cfi_string_t prop_name_table[] = {
  60196. + {FT_ID_DMA_MODE, "dma_mode"},
  60197. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  60198. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  60199. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  60200. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  60201. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  60202. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  60203. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  60204. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  60205. + {}
  60206. +};
  60207. +
  60208. +/************************************************************************/
  60209. +
  60210. +/**
  60211. + * Returns the name of the feature by its ID
  60212. + * or NULL if no featute ID matches.
  60213. + *
  60214. + */
  60215. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  60216. +{
  60217. + cfi_string_t *pstr;
  60218. + *len = 0;
  60219. +
  60220. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  60221. + if (pstr->id == prop_id) {
  60222. + *len = DWC_STRLEN(pstr->s);
  60223. + return pstr->s;
  60224. + }
  60225. + }
  60226. + return NULL;
  60227. +}
  60228. +
  60229. +/**
  60230. + * This function handles all CFI specific control requests.
  60231. + *
  60232. + * Return a negative value to stall the DCE.
  60233. + */
  60234. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  60235. +{
  60236. + int retval = 0;
  60237. + dwc_otg_pcd_ep_t *ep = NULL;
  60238. + cfiobject_t *cfi = pcd->cfi;
  60239. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  60240. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  60241. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  60242. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  60243. + uint32_t regaddr = 0;
  60244. + uint32_t regval = 0;
  60245. +
  60246. + /* Save this Control Request in the CFI object.
  60247. + * The data field will be assigned in the data stage completion CB function.
  60248. + */
  60249. + cfi->ctrl_req = *ctrl;
  60250. + cfi->ctrl_req.data = NULL;
  60251. +
  60252. + cfi->need_gadget_att = 0;
  60253. + cfi->need_status_in_complete = 0;
  60254. +
  60255. + switch (ctrl->bRequest) {
  60256. + case VEN_CORE_GET_FEATURES:
  60257. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  60258. + if (retval >= 0) {
  60259. + //dump_msg(cfi->buf_in.buf, retval);
  60260. + ep = &pcd->ep0;
  60261. +
  60262. + retval = min((uint16_t) retval, wLen);
  60263. + /* Transfer this buffer to the host through the EP0-IN EP */
  60264. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  60265. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  60266. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  60267. + ep->dwc_ep.xfer_len = retval;
  60268. + ep->dwc_ep.xfer_count = 0;
  60269. + ep->dwc_ep.sent_zlp = 0;
  60270. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  60271. +
  60272. + pcd->ep0_pending = 1;
  60273. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  60274. + }
  60275. + retval = 0;
  60276. + break;
  60277. +
  60278. + case VEN_CORE_GET_FEATURE:
  60279. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  60280. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  60281. + pcd, ctrl);
  60282. + if (retval >= 0) {
  60283. + ep = &pcd->ep0;
  60284. +
  60285. + retval = min((uint16_t) retval, wLen);
  60286. + /* Transfer this buffer to the host through the EP0-IN EP */
  60287. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  60288. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  60289. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  60290. + ep->dwc_ep.xfer_len = retval;
  60291. + ep->dwc_ep.xfer_count = 0;
  60292. + ep->dwc_ep.sent_zlp = 0;
  60293. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  60294. +
  60295. + pcd->ep0_pending = 1;
  60296. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  60297. + }
  60298. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  60299. + dump_msg(cfi->buf_in.buf, retval);
  60300. + break;
  60301. +
  60302. + case VEN_CORE_SET_FEATURE:
  60303. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  60304. + /* Set up an XFER to get the data stage of the control request,
  60305. + * which is the new value of the feature to be modified.
  60306. + */
  60307. + ep = &pcd->ep0;
  60308. + ep->dwc_ep.is_in = 0;
  60309. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  60310. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  60311. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  60312. + ep->dwc_ep.xfer_len = wLen;
  60313. + ep->dwc_ep.xfer_count = 0;
  60314. + ep->dwc_ep.sent_zlp = 0;
  60315. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  60316. +
  60317. + pcd->ep0_pending = 1;
  60318. + /* Read the control write's data stage */
  60319. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  60320. + retval = 0;
  60321. + break;
  60322. +
  60323. + case VEN_CORE_RESET_FEATURES:
  60324. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  60325. + cfi->need_gadget_att = 1;
  60326. + cfi->need_status_in_complete = 1;
  60327. + retval = cfi_preproc_reset(pcd, ctrl);
  60328. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  60329. + break;
  60330. +
  60331. + case VEN_CORE_ACTIVATE_FEATURES:
  60332. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  60333. + break;
  60334. +
  60335. + case VEN_CORE_READ_REGISTER:
  60336. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  60337. + /* wValue optionally contains the HI WORD of the register offset and
  60338. + * wIndex contains the LOW WORD of the register offset
  60339. + */
  60340. + if (wValue == 0) {
  60341. + /* @TODO - MAS - fix the access to the base field */
  60342. + regaddr = 0;
  60343. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  60344. + //GET_CORE_IF(pcd)->co
  60345. + regaddr |= wIndex;
  60346. + } else {
  60347. + regaddr = (wValue << 16) | wIndex;
  60348. + }
  60349. +
  60350. + /* Read a 32-bit value of the memory at the regaddr */
  60351. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  60352. +
  60353. + ep = &pcd->ep0;
  60354. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  60355. + ep->dwc_ep.is_in = 1;
  60356. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  60357. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  60358. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  60359. + ep->dwc_ep.xfer_len = wLen;
  60360. + ep->dwc_ep.xfer_count = 0;
  60361. + ep->dwc_ep.sent_zlp = 0;
  60362. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  60363. +
  60364. + pcd->ep0_pending = 1;
  60365. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  60366. + cfi->need_gadget_att = 0;
  60367. + retval = 0;
  60368. + break;
  60369. +
  60370. + case VEN_CORE_WRITE_REGISTER:
  60371. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  60372. + /* Set up an XFER to get the data stage of the control request,
  60373. + * which is the new value of the register to be modified.
  60374. + */
  60375. + ep = &pcd->ep0;
  60376. + ep->dwc_ep.is_in = 0;
  60377. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  60378. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  60379. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  60380. + ep->dwc_ep.xfer_len = wLen;
  60381. + ep->dwc_ep.xfer_count = 0;
  60382. + ep->dwc_ep.sent_zlp = 0;
  60383. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  60384. +
  60385. + pcd->ep0_pending = 1;
  60386. + /* Read the control write's data stage */
  60387. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  60388. + retval = 0;
  60389. + break;
  60390. +
  60391. + default:
  60392. + retval = -DWC_E_NOT_SUPPORTED;
  60393. + break;
  60394. + }
  60395. +
  60396. + return retval;
  60397. +}
  60398. +
  60399. +/**
  60400. + * This function prepares the core features descriptors and copies its
  60401. + * raw representation into the buffer <buf>.
  60402. + *
  60403. + * The buffer structure is as follows:
  60404. + * all_features_header (8 bytes)
  60405. + * features_#1 (8 bytes + feature name string length)
  60406. + * features_#2 (8 bytes + feature name string length)
  60407. + * .....
  60408. + * features_#n - where n=the total count of feature descriptors
  60409. + */
  60410. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  60411. +{
  60412. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  60413. + cfi_feature_desc_header_t *prop;
  60414. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  60415. + cfi_all_features_header_t *tmp;
  60416. + uint8_t *tmpbuf = buf;
  60417. + const uint8_t *pname = NULL;
  60418. + int i, j, namelen = 0, totlen;
  60419. +
  60420. + /* Prepare and copy the core features into the buffer */
  60421. + CFI_INFO("%s:\n", __func__);
  60422. +
  60423. + tmp = (cfi_all_features_header_t *) tmpbuf;
  60424. + *tmp = *all_props_hdr;
  60425. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  60426. +
  60427. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  60428. + for (i = 0; i < j; i++, prop_hdr++) {
  60429. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  60430. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  60431. + *prop = *prop_hdr;
  60432. +
  60433. + prop->bNameLen = namelen;
  60434. + prop->wLength =
  60435. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  60436. + namelen);
  60437. +
  60438. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  60439. + dwc_memcpy(tmpbuf, pname, namelen);
  60440. + tmpbuf += namelen;
  60441. + }
  60442. +
  60443. + totlen = tmpbuf - buf;
  60444. +
  60445. + if (totlen > 0) {
  60446. + tmp = (cfi_all_features_header_t *) buf;
  60447. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  60448. + }
  60449. +
  60450. + return totlen;
  60451. +}
  60452. +
  60453. +/**
  60454. + * This function releases all the dynamic memory in the CFI object.
  60455. + */
  60456. +static void cfi_release(cfiobject_t * cfiobj)
  60457. +{
  60458. + cfi_ep_t *cfiep;
  60459. + dwc_list_link_t *tmp;
  60460. +
  60461. + CFI_INFO("%s\n", __func__);
  60462. +
  60463. + if (cfiobj->buf_in.buf) {
  60464. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  60465. + cfiobj->buf_in.addr);
  60466. + cfiobj->buf_in.buf = NULL;
  60467. + }
  60468. +
  60469. + if (cfiobj->buf_out.buf) {
  60470. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  60471. + cfiobj->buf_out.addr);
  60472. + cfiobj->buf_out.buf = NULL;
  60473. + }
  60474. +
  60475. + /* Free the Buffer Setup values for each EP */
  60476. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  60477. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  60478. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  60479. + cfi_free_ep_bs_dyn_data(cfiep);
  60480. + }
  60481. +}
  60482. +
  60483. +/**
  60484. + * This function frees the dynamically allocated EP buffer setup data.
  60485. + */
  60486. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  60487. +{
  60488. + if (cfiep->bm_sg) {
  60489. + DWC_FREE(cfiep->bm_sg);
  60490. + cfiep->bm_sg = NULL;
  60491. + }
  60492. +
  60493. + if (cfiep->bm_align) {
  60494. + DWC_FREE(cfiep->bm_align);
  60495. + cfiep->bm_align = NULL;
  60496. + }
  60497. +
  60498. + if (cfiep->bm_concat) {
  60499. + if (NULL != cfiep->bm_concat->wTxBytes) {
  60500. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  60501. + cfiep->bm_concat->wTxBytes = NULL;
  60502. + }
  60503. + DWC_FREE(cfiep->bm_concat);
  60504. + cfiep->bm_concat = NULL;
  60505. + }
  60506. +}
  60507. +
  60508. +/**
  60509. + * This function initializes the default values of the features
  60510. + * for a specific endpoint and should be called only once when
  60511. + * the EP is enabled first time.
  60512. + */
  60513. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  60514. +{
  60515. + int retval = 0;
  60516. +
  60517. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  60518. + if (NULL == cfiep->bm_sg) {
  60519. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  60520. + return -DWC_E_NO_MEMORY;
  60521. + }
  60522. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  60523. +
  60524. + /* For the Concatenation feature's default value we do not allocate
  60525. + * memory for the wTxBytes field - it will be done in the set_feature_value
  60526. + * request handler.
  60527. + */
  60528. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  60529. + if (NULL == cfiep->bm_concat) {
  60530. + CFI_INFO
  60531. + ("Failed to allocate memory for CONCATENATION feature value\n");
  60532. + DWC_FREE(cfiep->bm_sg);
  60533. + return -DWC_E_NO_MEMORY;
  60534. + }
  60535. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  60536. +
  60537. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  60538. + if (NULL == cfiep->bm_align) {
  60539. + CFI_INFO
  60540. + ("Failed to allocate memory for Alignment feature value\n");
  60541. + DWC_FREE(cfiep->bm_sg);
  60542. + DWC_FREE(cfiep->bm_concat);
  60543. + return -DWC_E_NO_MEMORY;
  60544. + }
  60545. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  60546. +
  60547. + return retval;
  60548. +}
  60549. +
  60550. +/**
  60551. + * The callback function that notifies the CFI on the activation of
  60552. + * an endpoint in the PCD. The following steps are done in this function:
  60553. + *
  60554. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  60555. + * active endpoint)
  60556. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  60557. + * Set the Buffer Mode to standard
  60558. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  60559. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  60560. + */
  60561. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  60562. + struct dwc_otg_pcd_ep *ep)
  60563. +{
  60564. + cfi_ep_t *cfiep;
  60565. + int retval = -DWC_E_NOT_SUPPORTED;
  60566. +
  60567. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  60568. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  60569. + /* MAS - Check whether this endpoint already is in the list */
  60570. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  60571. +
  60572. + if (NULL == cfiep) {
  60573. + /* Allocate a cfi_ep_t object */
  60574. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  60575. + if (NULL == cfiep) {
  60576. + CFI_INFO
  60577. + ("Unable to allocate memory for <cfiep> in function %s\n",
  60578. + __func__);
  60579. + return -DWC_E_NO_MEMORY;
  60580. + }
  60581. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  60582. +
  60583. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  60584. + cfiep->ep = ep;
  60585. +
  60586. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  60587. + ep->dwc_ep.descs =
  60588. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  60589. + sizeof(dwc_otg_dma_desc_t),
  60590. + &ep->dwc_ep.descs_dma_addr);
  60591. +
  60592. + if (NULL == ep->dwc_ep.descs) {
  60593. + DWC_FREE(cfiep);
  60594. + return -DWC_E_NO_MEMORY;
  60595. + }
  60596. +
  60597. + DWC_LIST_INIT(&cfiep->lh);
  60598. +
  60599. + /* Set the buffer mode to BM_STANDARD. It will be modified
  60600. + * when building descriptors for a specific buffer mode */
  60601. + ep->dwc_ep.buff_mode = BM_STANDARD;
  60602. +
  60603. + /* Create and initialize the default values for this EP's Buffer modes */
  60604. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  60605. + return retval;
  60606. +
  60607. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  60608. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  60609. + retval = 0;
  60610. + } else { /* The sought EP already is in the list */
  60611. + CFI_INFO("%s: The sought EP already is in the list\n",
  60612. + __func__);
  60613. + }
  60614. +
  60615. + return retval;
  60616. +}
  60617. +
  60618. +/**
  60619. + * This function is called when the data stage of a 3-stage Control Write request
  60620. + * is complete.
  60621. + *
  60622. + */
  60623. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  60624. + struct dwc_otg_pcd *pcd)
  60625. +{
  60626. + uint32_t addr, reg_value;
  60627. + uint16_t wIndex, wValue;
  60628. + uint8_t bRequest;
  60629. + uint8_t *buf = cfi->buf_out.buf;
  60630. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  60631. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  60632. + int retval = -DWC_E_NOT_SUPPORTED;
  60633. +
  60634. + CFI_INFO("%s\n", __func__);
  60635. +
  60636. + bRequest = ctrl_req->bRequest;
  60637. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  60638. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  60639. +
  60640. + /*
  60641. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  60642. + * The request should be already saved in the command stage by now.
  60643. + */
  60644. + ctrl_req->data = cfi->buf_out.buf;
  60645. + cfi->need_status_in_complete = 0;
  60646. + cfi->need_gadget_att = 0;
  60647. +
  60648. + switch (bRequest) {
  60649. + case VEN_CORE_WRITE_REGISTER:
  60650. + /* The buffer contains raw data of the new value for the register */
  60651. + reg_value = *((uint32_t *) buf);
  60652. + if (wValue == 0) {
  60653. + addr = 0;
  60654. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  60655. + addr += wIndex;
  60656. + } else {
  60657. + addr = (wValue << 16) | wIndex;
  60658. + }
  60659. +
  60660. + //writel(reg_value, addr);
  60661. +
  60662. + retval = 0;
  60663. + cfi->need_status_in_complete = 1;
  60664. + break;
  60665. +
  60666. + case VEN_CORE_SET_FEATURE:
  60667. + /* The buffer contains raw data of the new value of the feature */
  60668. + retval = cfi_set_feature_value(pcd);
  60669. + if (retval < 0)
  60670. + return retval;
  60671. +
  60672. + cfi->need_status_in_complete = 1;
  60673. + break;
  60674. +
  60675. + default:
  60676. + break;
  60677. + }
  60678. +
  60679. + return retval;
  60680. +}
  60681. +
  60682. +/**
  60683. + * This function builds the DMA descriptors for the SG buffer mode.
  60684. + */
  60685. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  60686. + dwc_otg_pcd_request_t * req)
  60687. +{
  60688. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  60689. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  60690. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  60691. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  60692. + dma_addr_t buff_addr = req->dma;
  60693. + int i;
  60694. + uint32_t txsize, off;
  60695. +
  60696. + txsize = sgval->wSize;
  60697. + off = sgval->bOffset;
  60698. +
  60699. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  60700. +// __func__, cfiep->ep->ep.name, txsize, off);
  60701. +
  60702. + for (i = 0; i < sgval->bCount; i++) {
  60703. + desc->status.b.bs = BS_HOST_BUSY;
  60704. + desc->buf = buff_addr;
  60705. + desc->status.b.l = 0;
  60706. + desc->status.b.ioc = 0;
  60707. + desc->status.b.sp = 0;
  60708. + desc->status.b.bytes = txsize;
  60709. + desc->status.b.bs = BS_HOST_READY;
  60710. +
  60711. + /* Set the next address of the buffer */
  60712. + buff_addr += txsize + off;
  60713. + desc_last = desc;
  60714. + desc++;
  60715. + }
  60716. +
  60717. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  60718. + desc_last->status.b.l = 1;
  60719. + desc_last->status.b.ioc = 1;
  60720. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  60721. + /* Save the last DMA descriptor pointer */
  60722. + cfiep->dma_desc_last = desc_last;
  60723. + cfiep->desc_count = sgval->bCount;
  60724. +}
  60725. +
  60726. +/**
  60727. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  60728. + */
  60729. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  60730. + dwc_otg_pcd_request_t * req)
  60731. +{
  60732. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  60733. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  60734. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  60735. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  60736. + dma_addr_t buff_addr = req->dma;
  60737. + int i;
  60738. + uint16_t *txsize;
  60739. +
  60740. + txsize = concatval->wTxBytes;
  60741. +
  60742. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  60743. + desc->buf = buff_addr;
  60744. + desc->status.b.bs = BS_HOST_BUSY;
  60745. + desc->status.b.l = 0;
  60746. + desc->status.b.ioc = 0;
  60747. + desc->status.b.sp = 0;
  60748. + desc->status.b.bytes = *txsize;
  60749. + desc->status.b.bs = BS_HOST_READY;
  60750. +
  60751. + txsize++;
  60752. + /* Set the next address of the buffer */
  60753. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  60754. + desc_last = desc;
  60755. + desc++;
  60756. + }
  60757. +
  60758. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  60759. + desc_last->status.b.l = 1;
  60760. + desc_last->status.b.ioc = 1;
  60761. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  60762. + cfiep->dma_desc_last = desc_last;
  60763. + cfiep->desc_count = concatval->hdr.bDescCount;
  60764. +}
  60765. +
  60766. +/**
  60767. + * This function builds the DMA descriptors for the Circular buffer mode
  60768. + */
  60769. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  60770. + dwc_otg_pcd_request_t * req)
  60771. +{
  60772. + /* @todo: MAS - add implementation when this feature needs to be tested */
  60773. +}
  60774. +
  60775. +/**
  60776. + * This function builds the DMA descriptors for the Alignment buffer mode
  60777. + */
  60778. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  60779. + dwc_otg_pcd_request_t * req)
  60780. +{
  60781. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  60782. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  60783. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  60784. + dma_addr_t buff_addr = req->dma;
  60785. +
  60786. + desc->status.b.bs = BS_HOST_BUSY;
  60787. + desc->status.b.l = 1;
  60788. + desc->status.b.ioc = 1;
  60789. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  60790. + desc->status.b.bytes = req->length;
  60791. + /* Adjust the buffer alignment */
  60792. + desc->buf = (buff_addr + alignval->bAlign);
  60793. + desc->status.b.bs = BS_HOST_READY;
  60794. + cfiep->dma_desc_last = desc;
  60795. + cfiep->desc_count = 1;
  60796. +}
  60797. +
  60798. +/**
  60799. + * This function builds the DMA descriptors chain for different modes of the
  60800. + * buffer setup of an endpoint.
  60801. + */
  60802. +static void cfi_build_descriptors(struct cfiobject *cfi,
  60803. + struct dwc_otg_pcd *pcd,
  60804. + struct dwc_otg_pcd_ep *ep,
  60805. + dwc_otg_pcd_request_t * req)
  60806. +{
  60807. + cfi_ep_t *cfiep;
  60808. +
  60809. + /* Get the cfiep by the dwc_otg_pcd_ep */
  60810. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  60811. + if (NULL == cfiep) {
  60812. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  60813. + __func__);
  60814. + return;
  60815. + }
  60816. +
  60817. + cfiep->xfer_len = req->length;
  60818. +
  60819. + /* Iterate through all the DMA descriptors */
  60820. + switch (cfiep->ep->dwc_ep.buff_mode) {
  60821. + case BM_SG:
  60822. + cfi_build_sg_descs(cfi, cfiep, req);
  60823. + break;
  60824. +
  60825. + case BM_CONCAT:
  60826. + cfi_build_concat_descs(cfi, cfiep, req);
  60827. + break;
  60828. +
  60829. + case BM_CIRCULAR:
  60830. + cfi_build_circ_descs(cfi, cfiep, req);
  60831. + break;
  60832. +
  60833. + case BM_ALIGN:
  60834. + cfi_build_align_descs(cfi, cfiep, req);
  60835. + break;
  60836. +
  60837. + default:
  60838. + break;
  60839. + }
  60840. +}
  60841. +
  60842. +/**
  60843. + * Allocate DMA buffer for different Buffer modes.
  60844. + */
  60845. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  60846. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  60847. + unsigned size, gfp_t flags)
  60848. +{
  60849. + return DWC_DMA_ALLOC(size, dma);
  60850. +}
  60851. +
  60852. +/**
  60853. + * This function initializes the CFI object.
  60854. + */
  60855. +int init_cfi(cfiobject_t * cfiobj)
  60856. +{
  60857. + CFI_INFO("%s\n", __func__);
  60858. +
  60859. + /* Allocate a buffer for IN XFERs */
  60860. + cfiobj->buf_in.buf =
  60861. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  60862. + if (NULL == cfiobj->buf_in.buf) {
  60863. + CFI_INFO("Unable to allocate buffer for INs\n");
  60864. + return -DWC_E_NO_MEMORY;
  60865. + }
  60866. +
  60867. + /* Allocate a buffer for OUT XFERs */
  60868. + cfiobj->buf_out.buf =
  60869. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  60870. + if (NULL == cfiobj->buf_out.buf) {
  60871. + CFI_INFO("Unable to allocate buffer for OUT\n");
  60872. + return -DWC_E_NO_MEMORY;
  60873. + }
  60874. +
  60875. + /* Initialize the callback function pointers */
  60876. + cfiobj->ops.release = cfi_release;
  60877. + cfiobj->ops.ep_enable = cfi_ep_enable;
  60878. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  60879. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  60880. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  60881. +
  60882. + /* Initialize the list of active endpoints in the CFI object */
  60883. + DWC_LIST_INIT(&cfiobj->active_eps);
  60884. +
  60885. + return 0;
  60886. +}
  60887. +
  60888. +/**
  60889. + * This function reads the required feature's current value into the buffer
  60890. + *
  60891. + * @retval: Returns negative as error, or the data length of the feature
  60892. + */
  60893. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  60894. + struct dwc_otg_pcd *pcd,
  60895. + struct cfi_usb_ctrlrequest *ctrl_req)
  60896. +{
  60897. + int retval = -DWC_E_NOT_SUPPORTED;
  60898. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  60899. + uint16_t dfifo, rxfifo, txfifo;
  60900. +
  60901. + switch (ctrl_req->wIndex) {
  60902. + /* Whether the DDMA is enabled or not */
  60903. + case FT_ID_DMA_MODE:
  60904. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  60905. + retval = 1;
  60906. + break;
  60907. +
  60908. + case FT_ID_DMA_BUFFER_SETUP:
  60909. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  60910. + break;
  60911. +
  60912. + case FT_ID_DMA_BUFF_ALIGN:
  60913. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  60914. + break;
  60915. +
  60916. + case FT_ID_DMA_CONCAT_SETUP:
  60917. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  60918. + break;
  60919. +
  60920. + case FT_ID_DMA_CIRCULAR:
  60921. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  60922. + break;
  60923. +
  60924. + case FT_ID_THRESHOLD_SETUP:
  60925. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  60926. + break;
  60927. +
  60928. + case FT_ID_DFIFO_DEPTH:
  60929. + dfifo = get_dfifo_size(coreif);
  60930. + *((uint16_t *) buf) = dfifo;
  60931. + retval = sizeof(uint16_t);
  60932. + break;
  60933. +
  60934. + case FT_ID_TX_FIFO_DEPTH:
  60935. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  60936. + if (retval >= 0) {
  60937. + txfifo = retval;
  60938. + *((uint16_t *) buf) = txfifo;
  60939. + retval = sizeof(uint16_t);
  60940. + }
  60941. + break;
  60942. +
  60943. + case FT_ID_RX_FIFO_DEPTH:
  60944. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  60945. + if (retval >= 0) {
  60946. + rxfifo = retval;
  60947. + *((uint16_t *) buf) = rxfifo;
  60948. + retval = sizeof(uint16_t);
  60949. + }
  60950. + break;
  60951. + }
  60952. +
  60953. + return retval;
  60954. +}
  60955. +
  60956. +/**
  60957. + * This function resets the SG for the specified EP to its default value
  60958. + */
  60959. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  60960. +{
  60961. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  60962. + return 0;
  60963. +}
  60964. +
  60965. +/**
  60966. + * This function resets the Alignment for the specified EP to its default value
  60967. + */
  60968. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  60969. +{
  60970. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  60971. + return 0;
  60972. +}
  60973. +
  60974. +/**
  60975. + * This function resets the Concatenation for the specified EP to its default value
  60976. + * This function will also set the value of the wTxBytes field to NULL after
  60977. + * freeing the memory previously allocated for this field.
  60978. + */
  60979. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  60980. +{
  60981. + /* First we need to free the wTxBytes field */
  60982. + if (cfiep->bm_concat->wTxBytes) {
  60983. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  60984. + cfiep->bm_concat->wTxBytes = NULL;
  60985. + }
  60986. +
  60987. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  60988. + return 0;
  60989. +}
  60990. +
  60991. +/**
  60992. + * This function resets all the buffer setups of the specified endpoint
  60993. + */
  60994. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  60995. +{
  60996. + cfi_reset_sg_val(cfiep);
  60997. + cfi_reset_align_val(cfiep);
  60998. + cfi_reset_concat_val(cfiep);
  60999. + return 0;
  61000. +}
  61001. +
  61002. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  61003. + uint8_t rx_rst, uint8_t tx_rst)
  61004. +{
  61005. + int retval = -DWC_E_INVALID;
  61006. + uint16_t tx_siz[15];
  61007. + uint16_t rx_siz = 0;
  61008. + dwc_otg_pcd_ep_t *ep = NULL;
  61009. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  61010. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  61011. +
  61012. + if (rx_rst) {
  61013. + rx_siz = params->dev_rx_fifo_size;
  61014. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  61015. + }
  61016. +
  61017. + if (tx_rst) {
  61018. + if (ep_addr == 0) {
  61019. + int i;
  61020. +
  61021. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61022. + tx_siz[i] =
  61023. + core_if->core_params->dev_tx_fifo_size[i];
  61024. + core_if->core_params->dev_tx_fifo_size[i] =
  61025. + core_if->init_txfsiz[i];
  61026. + }
  61027. + } else {
  61028. +
  61029. + ep = get_ep_by_addr(pcd, ep_addr);
  61030. +
  61031. + if (NULL == ep) {
  61032. + CFI_INFO
  61033. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  61034. + __func__, ep_addr);
  61035. + return -DWC_E_INVALID;
  61036. + }
  61037. +
  61038. + tx_siz[0] =
  61039. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  61040. + 1];
  61041. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  61042. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  61043. + dwc_ep.tx_fifo_num -
  61044. + 1];
  61045. + }
  61046. + }
  61047. +
  61048. + if (resize_fifos(GET_CORE_IF(pcd))) {
  61049. + retval = 0;
  61050. + } else {
  61051. + CFI_INFO
  61052. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  61053. + __func__);
  61054. + if (rx_rst) {
  61055. + params->dev_rx_fifo_size = rx_siz;
  61056. + }
  61057. +
  61058. + if (tx_rst) {
  61059. + if (ep_addr == 0) {
  61060. + int i;
  61061. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  61062. + i++) {
  61063. + core_if->
  61064. + core_params->dev_tx_fifo_size[i] =
  61065. + tx_siz[i];
  61066. + }
  61067. + } else {
  61068. + params->dev_tx_fifo_size[ep->
  61069. + dwc_ep.tx_fifo_num -
  61070. + 1] = tx_siz[0];
  61071. + }
  61072. + }
  61073. + retval = -DWC_E_INVALID;
  61074. + }
  61075. + return retval;
  61076. +}
  61077. +
  61078. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  61079. +{
  61080. + int retval = 0;
  61081. + cfi_ep_t *cfiep;
  61082. + cfiobject_t *cfi = pcd->cfi;
  61083. + dwc_list_link_t *tmp;
  61084. +
  61085. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  61086. + if (retval < 0) {
  61087. + return retval;
  61088. + }
  61089. +
  61090. + /* If the EP address is known then reset the features for only that EP */
  61091. + if (addr) {
  61092. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61093. + if (NULL == cfiep) {
  61094. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  61095. + __func__, addr);
  61096. + return -DWC_E_INVALID;
  61097. + }
  61098. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  61099. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  61100. + }
  61101. + /* Otherwise (wValue == 0), reset all features of all EP's */
  61102. + else {
  61103. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  61104. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  61105. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  61106. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  61107. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  61108. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  61109. + if (retval < 0) {
  61110. + CFI_INFO
  61111. + ("%s: Error resetting the feature Reset All\n",
  61112. + __func__);
  61113. + return retval;
  61114. + }
  61115. + }
  61116. + }
  61117. + return retval;
  61118. +}
  61119. +
  61120. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  61121. + uint8_t addr)
  61122. +{
  61123. + int retval = 0;
  61124. + cfi_ep_t *cfiep;
  61125. + cfiobject_t *cfi = pcd->cfi;
  61126. + dwc_list_link_t *tmp;
  61127. +
  61128. + /* If the EP address is known then reset the features for only that EP */
  61129. + if (addr) {
  61130. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61131. + if (NULL == cfiep) {
  61132. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  61133. + __func__, addr);
  61134. + return -DWC_E_INVALID;
  61135. + }
  61136. + retval = cfi_reset_sg_val(cfiep);
  61137. + }
  61138. + /* Otherwise (wValue == 0), reset all features of all EP's */
  61139. + else {
  61140. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  61141. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  61142. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  61143. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  61144. + retval = cfi_reset_sg_val(cfiep);
  61145. + if (retval < 0) {
  61146. + CFI_INFO
  61147. + ("%s: Error resetting the feature Buffer Setup\n",
  61148. + __func__);
  61149. + return retval;
  61150. + }
  61151. + }
  61152. + }
  61153. + return retval;
  61154. +}
  61155. +
  61156. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  61157. +{
  61158. + int retval = 0;
  61159. + cfi_ep_t *cfiep;
  61160. + cfiobject_t *cfi = pcd->cfi;
  61161. + dwc_list_link_t *tmp;
  61162. +
  61163. + /* If the EP address is known then reset the features for only that EP */
  61164. + if (addr) {
  61165. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61166. + if (NULL == cfiep) {
  61167. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  61168. + __func__, addr);
  61169. + return -DWC_E_INVALID;
  61170. + }
  61171. + retval = cfi_reset_concat_val(cfiep);
  61172. + }
  61173. + /* Otherwise (wValue == 0), reset all features of all EP's */
  61174. + else {
  61175. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  61176. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  61177. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  61178. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  61179. + retval = cfi_reset_concat_val(cfiep);
  61180. + if (retval < 0) {
  61181. + CFI_INFO
  61182. + ("%s: Error resetting the feature Concatenation Value\n",
  61183. + __func__);
  61184. + return retval;
  61185. + }
  61186. + }
  61187. + }
  61188. + return retval;
  61189. +}
  61190. +
  61191. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  61192. +{
  61193. + int retval = 0;
  61194. + cfi_ep_t *cfiep;
  61195. + cfiobject_t *cfi = pcd->cfi;
  61196. + dwc_list_link_t *tmp;
  61197. +
  61198. + /* If the EP address is known then reset the features for only that EP */
  61199. + if (addr) {
  61200. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61201. + if (NULL == cfiep) {
  61202. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  61203. + __func__, addr);
  61204. + return -DWC_E_INVALID;
  61205. + }
  61206. + retval = cfi_reset_align_val(cfiep);
  61207. + }
  61208. + /* Otherwise (wValue == 0), reset all features of all EP's */
  61209. + else {
  61210. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  61211. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  61212. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  61213. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  61214. + retval = cfi_reset_align_val(cfiep);
  61215. + if (retval < 0) {
  61216. + CFI_INFO
  61217. + ("%s: Error resetting the feature Aliignment Value\n",
  61218. + __func__);
  61219. + return retval;
  61220. + }
  61221. + }
  61222. + }
  61223. + return retval;
  61224. +
  61225. +}
  61226. +
  61227. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  61228. + struct cfi_usb_ctrlrequest *req)
  61229. +{
  61230. + int retval = 0;
  61231. +
  61232. + switch (req->wIndex) {
  61233. + case 0:
  61234. + /* Reset all features */
  61235. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  61236. + break;
  61237. +
  61238. + case FT_ID_DMA_BUFFER_SETUP:
  61239. + /* Reset the SG buffer setup */
  61240. + retval =
  61241. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  61242. + break;
  61243. +
  61244. + case FT_ID_DMA_CONCAT_SETUP:
  61245. + /* Reset the Concatenation buffer setup */
  61246. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  61247. + break;
  61248. +
  61249. + case FT_ID_DMA_BUFF_ALIGN:
  61250. + /* Reset the Alignment buffer setup */
  61251. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  61252. + break;
  61253. +
  61254. + case FT_ID_TX_FIFO_DEPTH:
  61255. + retval =
  61256. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  61257. + pcd->cfi->need_gadget_att = 0;
  61258. + break;
  61259. +
  61260. + case FT_ID_RX_FIFO_DEPTH:
  61261. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  61262. + pcd->cfi->need_gadget_att = 0;
  61263. + break;
  61264. + default:
  61265. + break;
  61266. + }
  61267. + return retval;
  61268. +}
  61269. +
  61270. +/**
  61271. + * This function sets a new value for the SG buffer setup.
  61272. + */
  61273. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  61274. +{
  61275. + uint8_t inaddr, outaddr;
  61276. + cfi_ep_t *epin, *epout;
  61277. + ddma_sg_buffer_setup_t *psgval;
  61278. + uint32_t desccount, size;
  61279. +
  61280. + CFI_INFO("%s\n", __func__);
  61281. +
  61282. + psgval = (ddma_sg_buffer_setup_t *) buf;
  61283. + desccount = (uint32_t) psgval->bCount;
  61284. + size = (uint32_t) psgval->wSize;
  61285. +
  61286. + /* Check the DMA descriptor count */
  61287. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  61288. + CFI_INFO
  61289. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  61290. + __func__, MAX_DMA_DESCS_PER_EP);
  61291. + return -DWC_E_INVALID;
  61292. + }
  61293. +
  61294. + /* Check the DMA descriptor count */
  61295. +
  61296. + if (size == 0) {
  61297. +
  61298. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  61299. + __func__);
  61300. +
  61301. + return -DWC_E_INVALID;
  61302. +
  61303. + }
  61304. +
  61305. + inaddr = psgval->bInEndpointAddress;
  61306. + outaddr = psgval->bOutEndpointAddress;
  61307. +
  61308. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  61309. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  61310. +
  61311. + if (NULL == epin || NULL == epout) {
  61312. + CFI_INFO
  61313. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  61314. + __func__, inaddr, outaddr);
  61315. + return -DWC_E_INVALID;
  61316. + }
  61317. +
  61318. + epin->ep->dwc_ep.buff_mode = BM_SG;
  61319. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  61320. +
  61321. + epout->ep->dwc_ep.buff_mode = BM_SG;
  61322. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  61323. +
  61324. + return 0;
  61325. +}
  61326. +
  61327. +/**
  61328. + * This function sets a new value for the buffer Alignment setup.
  61329. + */
  61330. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  61331. +{
  61332. + cfi_ep_t *ep;
  61333. + uint8_t addr;
  61334. + ddma_align_buffer_setup_t *palignval;
  61335. +
  61336. + palignval = (ddma_align_buffer_setup_t *) buf;
  61337. + addr = palignval->bEndpointAddress;
  61338. +
  61339. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61340. +
  61341. + if (NULL == ep) {
  61342. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  61343. + __func__, addr);
  61344. + return -DWC_E_INVALID;
  61345. + }
  61346. +
  61347. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  61348. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  61349. +
  61350. + return 0;
  61351. +}
  61352. +
  61353. +/**
  61354. + * This function sets a new value for the Concatenation buffer setup.
  61355. + */
  61356. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  61357. +{
  61358. + uint8_t addr;
  61359. + cfi_ep_t *ep;
  61360. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  61361. + uint16_t *pVals;
  61362. + uint32_t desccount;
  61363. + int i;
  61364. + uint16_t mps;
  61365. +
  61366. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  61367. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  61368. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  61369. +
  61370. + /* Check the DMA descriptor count */
  61371. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  61372. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  61373. + __func__, MAX_DMA_DESCS_PER_EP);
  61374. + return -DWC_E_INVALID;
  61375. + }
  61376. +
  61377. + addr = pConcatValHdr->bEndpointAddress;
  61378. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61379. + if (NULL == ep) {
  61380. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  61381. + __func__, addr);
  61382. + return -DWC_E_INVALID;
  61383. + }
  61384. +
  61385. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  61386. +
  61387. +#if 0
  61388. + for (i = 0; i < desccount; i++) {
  61389. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  61390. + }
  61391. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  61392. +#endif
  61393. +
  61394. + /* Check the wTxSizes to be less than or equal to the mps */
  61395. + for (i = 0; i < desccount; i++) {
  61396. + if (pVals[i] > mps) {
  61397. + CFI_INFO
  61398. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  61399. + __func__, i, pVals[i]);
  61400. + return -DWC_E_INVALID;
  61401. + }
  61402. + }
  61403. +
  61404. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  61405. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  61406. +
  61407. + /* Free the previously allocated storage for the wTxBytes */
  61408. + if (ep->bm_concat->wTxBytes) {
  61409. + DWC_FREE(ep->bm_concat->wTxBytes);
  61410. + }
  61411. +
  61412. + /* Allocate a new storage for the wTxBytes field */
  61413. + ep->bm_concat->wTxBytes =
  61414. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  61415. + if (NULL == ep->bm_concat->wTxBytes) {
  61416. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  61417. + return -DWC_E_NO_MEMORY;
  61418. + }
  61419. +
  61420. + /* Copy the new values into the wTxBytes filed */
  61421. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  61422. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  61423. +
  61424. + return 0;
  61425. +}
  61426. +
  61427. +/**
  61428. + * This function calculates the total of all FIFO sizes
  61429. + *
  61430. + * @param core_if Programming view of DWC_otg controller
  61431. + *
  61432. + * @return The total of data FIFO sizes.
  61433. + *
  61434. + */
  61435. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  61436. +{
  61437. + dwc_otg_core_params_t *params = core_if->core_params;
  61438. + uint16_t dfifo_total = 0;
  61439. + int i;
  61440. +
  61441. + /* The shared RxFIFO size */
  61442. + dfifo_total =
  61443. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  61444. +
  61445. + /* Add up each TxFIFO size to the total */
  61446. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61447. + dfifo_total += params->dev_tx_fifo_size[i];
  61448. + }
  61449. +
  61450. + return dfifo_total;
  61451. +}
  61452. +
  61453. +/**
  61454. + * This function returns Rx FIFO size
  61455. + *
  61456. + * @param core_if Programming view of DWC_otg controller
  61457. + *
  61458. + * @return The total of data FIFO sizes.
  61459. + *
  61460. + */
  61461. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  61462. +{
  61463. + switch (wValue >> 8) {
  61464. + case 0:
  61465. + return (core_if->pwron_rxfsiz <
  61466. + 32768) ? core_if->pwron_rxfsiz : 32768;
  61467. + break;
  61468. + case 1:
  61469. + return core_if->core_params->dev_rx_fifo_size;
  61470. + break;
  61471. + default:
  61472. + return -DWC_E_INVALID;
  61473. + break;
  61474. + }
  61475. +}
  61476. +
  61477. +/**
  61478. + * This function returns Tx FIFO size for IN EP
  61479. + *
  61480. + * @param core_if Programming view of DWC_otg controller
  61481. + *
  61482. + * @return The total of data FIFO sizes.
  61483. + *
  61484. + */
  61485. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  61486. +{
  61487. + dwc_otg_pcd_ep_t *ep;
  61488. +
  61489. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  61490. +
  61491. + if (NULL == ep) {
  61492. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  61493. + __func__, wValue & 0xff);
  61494. + return -DWC_E_INVALID;
  61495. + }
  61496. +
  61497. + if (!ep->dwc_ep.is_in) {
  61498. + CFI_INFO
  61499. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  61500. + __func__, wValue & 0xff);
  61501. + return -DWC_E_INVALID;
  61502. + }
  61503. +
  61504. + switch (wValue >> 8) {
  61505. + case 0:
  61506. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  61507. + [ep->dwc_ep.tx_fifo_num - 1] <
  61508. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  61509. + dwc_ep.tx_fifo_num
  61510. + - 1] : 32768;
  61511. + break;
  61512. + case 1:
  61513. + return GET_CORE_IF(pcd)->core_params->
  61514. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  61515. + break;
  61516. + default:
  61517. + return -DWC_E_INVALID;
  61518. + break;
  61519. + }
  61520. +}
  61521. +
  61522. +/**
  61523. + * This function checks if the submitted combination of
  61524. + * device mode FIFO sizes is possible or not.
  61525. + *
  61526. + * @param core_if Programming view of DWC_otg controller
  61527. + *
  61528. + * @return 1 if possible, 0 otherwise.
  61529. + *
  61530. + */
  61531. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  61532. +{
  61533. + uint16_t dfifo_actual = 0;
  61534. + dwc_otg_core_params_t *params = core_if->core_params;
  61535. + uint16_t start_addr = 0;
  61536. + int i;
  61537. +
  61538. + dfifo_actual =
  61539. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  61540. +
  61541. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61542. + dfifo_actual += params->dev_tx_fifo_size[i];
  61543. + }
  61544. +
  61545. + if (dfifo_actual > core_if->total_fifo_size) {
  61546. + return 0;
  61547. + }
  61548. +
  61549. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  61550. + return 0;
  61551. +
  61552. + if (params->dev_nperio_tx_fifo_size > 32768
  61553. + || params->dev_nperio_tx_fifo_size < 16)
  61554. + return 0;
  61555. +
  61556. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61557. +
  61558. + if (params->dev_tx_fifo_size[i] > 768
  61559. + || params->dev_tx_fifo_size[i] < 4)
  61560. + return 0;
  61561. + }
  61562. +
  61563. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  61564. + return 0;
  61565. + start_addr = params->dev_rx_fifo_size;
  61566. +
  61567. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  61568. + return 0;
  61569. + start_addr += params->dev_nperio_tx_fifo_size;
  61570. +
  61571. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61572. +
  61573. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  61574. + return 0;
  61575. + start_addr += params->dev_tx_fifo_size[i];
  61576. + }
  61577. +
  61578. + return 1;
  61579. +}
  61580. +
  61581. +/**
  61582. + * This function resizes Device mode FIFOs
  61583. + *
  61584. + * @param core_if Programming view of DWC_otg controller
  61585. + *
  61586. + * @return 1 if successful, 0 otherwise
  61587. + *
  61588. + */
  61589. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  61590. +{
  61591. + int i = 0;
  61592. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  61593. + dwc_otg_core_params_t *params = core_if->core_params;
  61594. + uint32_t rx_fifo_size;
  61595. + fifosize_data_t nptxfifosize;
  61596. + fifosize_data_t txfifosize[15];
  61597. +
  61598. + uint32_t rx_fsz_bak;
  61599. + uint32_t nptxfsz_bak;
  61600. + uint32_t txfsz_bak[15];
  61601. +
  61602. + uint16_t start_address;
  61603. + uint8_t retval = 1;
  61604. +
  61605. + if (!check_fifo_sizes(core_if)) {
  61606. + return 0;
  61607. + }
  61608. +
  61609. + /* Configure data FIFO sizes */
  61610. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  61611. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  61612. + rx_fifo_size = params->dev_rx_fifo_size;
  61613. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  61614. +
  61615. + /*
  61616. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  61617. + * Indexes of the FIFO size module parameters in the
  61618. + * dev_tx_fifo_size array and the FIFO size registers in
  61619. + * the dtxfsiz array run from 0 to 14.
  61620. + */
  61621. +
  61622. + /* Non-periodic Tx FIFO */
  61623. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  61624. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  61625. + start_address = params->dev_rx_fifo_size;
  61626. + nptxfifosize.b.startaddr = start_address;
  61627. +
  61628. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  61629. +
  61630. + start_address += nptxfifosize.b.depth;
  61631. +
  61632. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61633. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  61634. +
  61635. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  61636. + txfifosize[i].b.startaddr = start_address;
  61637. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  61638. + txfifosize[i].d32);
  61639. +
  61640. + start_address += txfifosize[i].b.depth;
  61641. + }
  61642. +
  61643. + /** Check if register values are set correctly */
  61644. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  61645. + retval = 0;
  61646. + }
  61647. +
  61648. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  61649. + retval = 0;
  61650. + }
  61651. +
  61652. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61653. + if (txfifosize[i].d32 !=
  61654. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  61655. + retval = 0;
  61656. + }
  61657. + }
  61658. +
  61659. + /** If register values are not set correctly, reset old values */
  61660. + if (retval == 0) {
  61661. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  61662. +
  61663. + /* Non-periodic Tx FIFO */
  61664. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  61665. +
  61666. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  61667. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  61668. + txfsz_bak[i]);
  61669. + }
  61670. + }
  61671. + } else {
  61672. + return 0;
  61673. + }
  61674. +
  61675. + /* Flush the FIFOs */
  61676. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  61677. + dwc_otg_flush_rx_fifo(core_if);
  61678. +
  61679. + return retval;
  61680. +}
  61681. +
  61682. +/**
  61683. + * This function sets a new value for the buffer Alignment setup.
  61684. + */
  61685. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  61686. +{
  61687. + int retval;
  61688. + uint32_t fsiz;
  61689. + uint16_t size;
  61690. + uint16_t ep_addr;
  61691. + dwc_otg_pcd_ep_t *ep;
  61692. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  61693. + tx_fifo_size_setup_t *ptxfifoval;
  61694. +
  61695. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  61696. + ep_addr = ptxfifoval->bEndpointAddress;
  61697. + size = ptxfifoval->wDepth;
  61698. +
  61699. + ep = get_ep_by_addr(pcd, ep_addr);
  61700. +
  61701. + CFI_INFO
  61702. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  61703. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  61704. +
  61705. + if (NULL == ep) {
  61706. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  61707. + __func__, ep_addr);
  61708. + return -DWC_E_INVALID;
  61709. + }
  61710. +
  61711. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  61712. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  61713. +
  61714. + if (resize_fifos(GET_CORE_IF(pcd))) {
  61715. + retval = 0;
  61716. + } else {
  61717. + CFI_INFO
  61718. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  61719. + __func__, ep_addr);
  61720. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  61721. + retval = -DWC_E_INVALID;
  61722. + }
  61723. +
  61724. + return retval;
  61725. +}
  61726. +
  61727. +/**
  61728. + * This function sets a new value for the buffer Alignment setup.
  61729. + */
  61730. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  61731. +{
  61732. + int retval;
  61733. + uint32_t fsiz;
  61734. + uint16_t size;
  61735. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  61736. + rx_fifo_size_setup_t *prxfifoval;
  61737. +
  61738. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  61739. + size = prxfifoval->wDepth;
  61740. +
  61741. + fsiz = params->dev_rx_fifo_size;
  61742. + params->dev_rx_fifo_size = size;
  61743. +
  61744. + if (resize_fifos(GET_CORE_IF(pcd))) {
  61745. + retval = 0;
  61746. + } else {
  61747. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  61748. + __func__);
  61749. + params->dev_rx_fifo_size = fsiz;
  61750. + retval = -DWC_E_INVALID;
  61751. + }
  61752. +
  61753. + return retval;
  61754. +}
  61755. +
  61756. +/**
  61757. + * This function reads the SG of an EP's buffer setup into the buffer buf
  61758. + */
  61759. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  61760. + struct cfi_usb_ctrlrequest *req)
  61761. +{
  61762. + int retval = -DWC_E_INVALID;
  61763. + uint8_t addr;
  61764. + cfi_ep_t *ep;
  61765. +
  61766. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  61767. + addr = req->wValue & 0xFF;
  61768. + if (addr == 0) /* The address should be non-zero */
  61769. + return retval;
  61770. +
  61771. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61772. + if (NULL == ep) {
  61773. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  61774. + __func__, addr);
  61775. + return retval;
  61776. + }
  61777. +
  61778. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  61779. + retval = BS_SG_VAL_DESC_LEN;
  61780. + return retval;
  61781. +}
  61782. +
  61783. +/**
  61784. + * This function reads the Concatenation value of an EP's buffer mode into
  61785. + * the buffer buf
  61786. + */
  61787. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  61788. + struct cfi_usb_ctrlrequest *req)
  61789. +{
  61790. + int retval = -DWC_E_INVALID;
  61791. + uint8_t addr;
  61792. + cfi_ep_t *ep;
  61793. + uint8_t desc_count;
  61794. +
  61795. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  61796. + addr = req->wValue & 0xFF;
  61797. + if (addr == 0) /* The address should be non-zero */
  61798. + return retval;
  61799. +
  61800. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61801. + if (NULL == ep) {
  61802. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  61803. + __func__, addr);
  61804. + return retval;
  61805. + }
  61806. +
  61807. + /* Copy the header to the buffer */
  61808. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  61809. + /* Advance the buffer pointer by the header size */
  61810. + buf += BS_CONCAT_VAL_HDR_LEN;
  61811. +
  61812. + desc_count = ep->bm_concat->hdr.bDescCount;
  61813. + /* Copy alll the wTxBytes to the buffer */
  61814. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  61815. +
  61816. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  61817. + return retval;
  61818. +}
  61819. +
  61820. +/**
  61821. + * This function reads the buffer Alignment value of an EP's buffer mode into
  61822. + * the buffer buf
  61823. + *
  61824. + * @return The total number of bytes copied to the buffer or negative error code.
  61825. + */
  61826. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  61827. + struct cfi_usb_ctrlrequest *req)
  61828. +{
  61829. + int retval = -DWC_E_INVALID;
  61830. + uint8_t addr;
  61831. + cfi_ep_t *ep;
  61832. +
  61833. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  61834. + addr = req->wValue & 0xFF;
  61835. + if (addr == 0) /* The address should be non-zero */
  61836. + return retval;
  61837. +
  61838. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  61839. + if (NULL == ep) {
  61840. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  61841. + __func__, addr);
  61842. + return retval;
  61843. + }
  61844. +
  61845. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  61846. + retval = BS_ALIGN_VAL_HDR_LEN;
  61847. +
  61848. + return retval;
  61849. +}
  61850. +
  61851. +/**
  61852. + * This function sets a new value for the specified feature
  61853. + *
  61854. + * @param pcd A pointer to the PCD object
  61855. + *
  61856. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  61857. + */
  61858. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  61859. +{
  61860. + int retval = -DWC_E_NOT_SUPPORTED;
  61861. + uint16_t wIndex, wValue;
  61862. + uint8_t bRequest;
  61863. + struct dwc_otg_core_if *coreif;
  61864. + cfiobject_t *cfi = pcd->cfi;
  61865. + struct cfi_usb_ctrlrequest *ctrl_req;
  61866. + uint8_t *buf;
  61867. + ctrl_req = &cfi->ctrl_req;
  61868. +
  61869. + buf = pcd->cfi->ctrl_req.data;
  61870. +
  61871. + coreif = GET_CORE_IF(pcd);
  61872. + bRequest = ctrl_req->bRequest;
  61873. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  61874. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  61875. +
  61876. + /* See which feature is to be modified */
  61877. + switch (wIndex) {
  61878. + case FT_ID_DMA_BUFFER_SETUP:
  61879. + /* Modify the feature */
  61880. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  61881. + return retval;
  61882. +
  61883. + /* And send this request to the gadget */
  61884. + cfi->need_gadget_att = 1;
  61885. + break;
  61886. +
  61887. + case FT_ID_DMA_BUFF_ALIGN:
  61888. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  61889. + return retval;
  61890. + cfi->need_gadget_att = 1;
  61891. + break;
  61892. +
  61893. + case FT_ID_DMA_CONCAT_SETUP:
  61894. + /* Modify the feature */
  61895. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  61896. + return retval;
  61897. + cfi->need_gadget_att = 1;
  61898. + break;
  61899. +
  61900. + case FT_ID_DMA_CIRCULAR:
  61901. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  61902. + break;
  61903. +
  61904. + case FT_ID_THRESHOLD_SETUP:
  61905. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  61906. + break;
  61907. +
  61908. + case FT_ID_DFIFO_DEPTH:
  61909. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  61910. + break;
  61911. +
  61912. + case FT_ID_TX_FIFO_DEPTH:
  61913. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  61914. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  61915. + return retval;
  61916. + cfi->need_gadget_att = 0;
  61917. + break;
  61918. +
  61919. + case FT_ID_RX_FIFO_DEPTH:
  61920. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  61921. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  61922. + return retval;
  61923. + cfi->need_gadget_att = 0;
  61924. + break;
  61925. + }
  61926. +
  61927. + return retval;
  61928. +}
  61929. +
  61930. +#endif //DWC_UTE_CFI
  61931. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cfi.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  61932. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 1970-01-01 01:00:00.000000000 +0100
  61933. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cfi.h 2014-09-14 19:04:13.000000000 +0200
  61934. @@ -0,0 +1,320 @@
  61935. +/* ==========================================================================
  61936. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  61937. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  61938. + * otherwise expressly agreed to in writing between Synopsys and you.
  61939. + *
  61940. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  61941. + * any End User Software License Agreement or Agreement for Licensed Product
  61942. + * with Synopsys or any supplement thereto. You are permitted to use and
  61943. + * redistribute this Software in source and binary forms, with or without
  61944. + * modification, provided that redistributions of source code must retain this
  61945. + * notice. You may not view, use, disclose, copy or distribute this file or
  61946. + * any information contained herein except pursuant to this license grant from
  61947. + * Synopsys. If you do not agree with this notice, including the disclaimer
  61948. + * below, then you are not authorized to use the Software.
  61949. + *
  61950. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  61951. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61952. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61953. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  61954. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  61955. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  61956. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  61957. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  61958. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  61959. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  61960. + * DAMAGE.
  61961. + * ========================================================================== */
  61962. +
  61963. +#if !defined(__DWC_OTG_CFI_H__)
  61964. +#define __DWC_OTG_CFI_H__
  61965. +
  61966. +#include "dwc_otg_pcd.h"
  61967. +#include "dwc_cfi_common.h"
  61968. +
  61969. +/**
  61970. + * @file
  61971. + * This file contains the CFI related OTG PCD specific common constants,
  61972. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  61973. + * optional interface for internal testing purposes that a DUT may implement to
  61974. + * support testing of configurable features.
  61975. + *
  61976. + */
  61977. +
  61978. +struct dwc_otg_pcd;
  61979. +struct dwc_otg_pcd_ep;
  61980. +
  61981. +/** OTG CFI Features (properties) ID constants */
  61982. +/** This is a request for all Core Features */
  61983. +#define FT_ID_DMA_MODE 0x0001
  61984. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  61985. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  61986. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  61987. +#define FT_ID_DMA_CIRCULAR 0x0005
  61988. +#define FT_ID_THRESHOLD_SETUP 0x0006
  61989. +#define FT_ID_DFIFO_DEPTH 0x0007
  61990. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  61991. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  61992. +
  61993. +/**********************************************************/
  61994. +#define CFI_INFO_DEF
  61995. +
  61996. +#ifdef CFI_INFO_DEF
  61997. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  61998. +#else
  61999. +#define CFI_INFO(fmt...)
  62000. +#endif
  62001. +
  62002. +#define min(x,y) ({ \
  62003. + x < y ? x : y; })
  62004. +
  62005. +#define max(x,y) ({ \
  62006. + x > y ? x : y; })
  62007. +
  62008. +/**
  62009. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  62010. + * also used for setting up a buffer for Circular DDMA.
  62011. + */
  62012. +struct _ddma_sg_buffer_setup {
  62013. +#define BS_SG_VAL_DESC_LEN 6
  62014. + /* The OUT EP address */
  62015. + uint8_t bOutEndpointAddress;
  62016. + /* The IN EP address */
  62017. + uint8_t bInEndpointAddress;
  62018. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  62019. + uint8_t bOffset;
  62020. + /* The number of transfer segments (a DMA descriptors per each segment) */
  62021. + uint8_t bCount;
  62022. + /* Size (in byte) of each transfer segment */
  62023. + uint16_t wSize;
  62024. +} __attribute__ ((packed));
  62025. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  62026. +
  62027. +/** Descriptor DMA Concatenation Buffer setup structure */
  62028. +struct _ddma_concat_buffer_setup_hdr {
  62029. +#define BS_CONCAT_VAL_HDR_LEN 4
  62030. + /* The endpoint for which the buffer is to be set up */
  62031. + uint8_t bEndpointAddress;
  62032. + /* The count of descriptors to be used */
  62033. + uint8_t bDescCount;
  62034. + /* The total size of the transfer */
  62035. + uint16_t wSize;
  62036. +} __attribute__ ((packed));
  62037. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  62038. +
  62039. +/** Descriptor DMA Concatenation Buffer setup structure */
  62040. +struct _ddma_concat_buffer_setup {
  62041. + /* The SG header */
  62042. + ddma_concat_buffer_setup_hdr_t hdr;
  62043. +
  62044. + /* The XFER sizes pointer (allocated dynamically) */
  62045. + uint16_t *wTxBytes;
  62046. +} __attribute__ ((packed));
  62047. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  62048. +
  62049. +/** Descriptor DMA Alignment Buffer setup structure */
  62050. +struct _ddma_align_buffer_setup {
  62051. +#define BS_ALIGN_VAL_HDR_LEN 2
  62052. + uint8_t bEndpointAddress;
  62053. + uint8_t bAlign;
  62054. +} __attribute__ ((packed));
  62055. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  62056. +
  62057. +/** Transmit FIFO Size setup structure */
  62058. +struct _tx_fifo_size_setup {
  62059. + uint8_t bEndpointAddress;
  62060. + uint16_t wDepth;
  62061. +} __attribute__ ((packed));
  62062. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  62063. +
  62064. +/** Transmit FIFO Size setup structure */
  62065. +struct _rx_fifo_size_setup {
  62066. + uint16_t wDepth;
  62067. +} __attribute__ ((packed));
  62068. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  62069. +
  62070. +/**
  62071. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  62072. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  62073. + * to the data returned in the data stage of a 3-stage Control Write requests.
  62074. + */
  62075. +struct cfi_usb_ctrlrequest {
  62076. + uint8_t bRequestType;
  62077. + uint8_t bRequest;
  62078. + uint16_t wValue;
  62079. + uint16_t wIndex;
  62080. + uint16_t wLength;
  62081. + uint8_t *data;
  62082. +} UPACKED;
  62083. +
  62084. +/*---------------------------------------------------------------------------*/
  62085. +
  62086. +/**
  62087. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  62088. + * This structure is used to store the buffer setup data for any
  62089. + * enabled endpoint in the PCD.
  62090. + */
  62091. +struct cfi_ep {
  62092. + /* Entry for the list container */
  62093. + dwc_list_link_t lh;
  62094. + /* Pointer to the active PCD endpoint structure */
  62095. + struct dwc_otg_pcd_ep *ep;
  62096. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  62097. + struct dwc_otg_dma_desc *dma_desc_last;
  62098. + /* The SG feature value */
  62099. + ddma_sg_buffer_setup_t *bm_sg;
  62100. + /* The Circular feature value */
  62101. + ddma_sg_buffer_setup_t *bm_circ;
  62102. + /* The Concatenation feature value */
  62103. + ddma_concat_buffer_setup_t *bm_concat;
  62104. + /* The Alignment feature value */
  62105. + ddma_align_buffer_setup_t *bm_align;
  62106. + /* XFER length */
  62107. + uint32_t xfer_len;
  62108. + /*
  62109. + * Count of DMA descriptors currently used.
  62110. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  62111. + * defined in the dwc_otg_cil.h
  62112. + */
  62113. + uint32_t desc_count;
  62114. +};
  62115. +typedef struct cfi_ep cfi_ep_t;
  62116. +
  62117. +typedef struct cfi_dma_buff {
  62118. +#define CFI_IN_BUF_LEN 1024
  62119. +#define CFI_OUT_BUF_LEN 1024
  62120. + dma_addr_t addr;
  62121. + uint8_t *buf;
  62122. +} cfi_dma_buff_t;
  62123. +
  62124. +struct cfiobject;
  62125. +
  62126. +/**
  62127. + * This is the interface for the CFI operations.
  62128. + *
  62129. + * @param ep_enable Called when any endpoint is enabled and activated.
  62130. + * @param release Called when the CFI object is released and it needs to correctly
  62131. + * deallocate the dynamic memory
  62132. + * @param ctrl_write_complete Called when the data stage of the request is complete
  62133. + */
  62134. +typedef struct cfi_ops {
  62135. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  62136. + struct dwc_otg_pcd_ep * ep);
  62137. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  62138. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  62139. + unsigned size, gfp_t flags);
  62140. + void (*release) (struct cfiobject * cfi);
  62141. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  62142. + struct dwc_otg_pcd * pcd);
  62143. + void (*build_descriptors) (struct cfiobject * cfi,
  62144. + struct dwc_otg_pcd * pcd,
  62145. + struct dwc_otg_pcd_ep * ep,
  62146. + dwc_otg_pcd_request_t * req);
  62147. +} cfi_ops_t;
  62148. +
  62149. +struct cfiobject {
  62150. + cfi_ops_t ops;
  62151. + struct dwc_otg_pcd *pcd;
  62152. + struct usb_gadget *gadget;
  62153. +
  62154. + /* Buffers used to send/receive CFI-related request data */
  62155. + cfi_dma_buff_t buf_in;
  62156. + cfi_dma_buff_t buf_out;
  62157. +
  62158. + /* CFI specific Control request wrapper */
  62159. + struct cfi_usb_ctrlrequest ctrl_req;
  62160. +
  62161. + /* The list of active EP's in the PCD of type cfi_ep_t */
  62162. + dwc_list_link_t active_eps;
  62163. +
  62164. + /* This flag shall control the propagation of a specific request
  62165. + * to the gadget's processing routines.
  62166. + * 0 - no gadget handling
  62167. + * 1 - the gadget needs to know about this request (w/o completing a status
  62168. + * phase - just return a 0 to the _setup callback)
  62169. + */
  62170. + uint8_t need_gadget_att;
  62171. +
  62172. + /* Flag indicating whether the status IN phase needs to be
  62173. + * completed by the PCD
  62174. + */
  62175. + uint8_t need_status_in_complete;
  62176. +};
  62177. +typedef struct cfiobject cfiobject_t;
  62178. +
  62179. +#define DUMP_MSG
  62180. +
  62181. +#if defined(DUMP_MSG)
  62182. +static inline void dump_msg(const u8 * buf, unsigned int length)
  62183. +{
  62184. + unsigned int start, num, i;
  62185. + char line[52], *p;
  62186. +
  62187. + if (length >= 512)
  62188. + return;
  62189. +
  62190. + start = 0;
  62191. + while (length > 0) {
  62192. + num = min(length, 16u);
  62193. + p = line;
  62194. + for (i = 0; i < num; ++i) {
  62195. + if (i == 8)
  62196. + *p++ = ' ';
  62197. + DWC_SPRINTF(p, " %02x", buf[i]);
  62198. + p += 3;
  62199. + }
  62200. + *p = 0;
  62201. + DWC_DEBUG("%6x: %s\n", start, line);
  62202. + buf += num;
  62203. + start += num;
  62204. + length -= num;
  62205. + }
  62206. +}
  62207. +#else
  62208. +static inline void dump_msg(const u8 * buf, unsigned int length)
  62209. +{
  62210. +}
  62211. +#endif
  62212. +
  62213. +/**
  62214. + * This function returns a pointer to cfi_ep_t object with the addr address.
  62215. + */
  62216. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  62217. + uint8_t addr)
  62218. +{
  62219. + struct cfi_ep *pcfiep;
  62220. + dwc_list_link_t *tmp;
  62221. +
  62222. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  62223. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  62224. +
  62225. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  62226. + return pcfiep;
  62227. + }
  62228. + }
  62229. +
  62230. + return NULL;
  62231. +}
  62232. +
  62233. +/**
  62234. + * This function returns a pointer to cfi_ep_t object that matches
  62235. + * the dwc_otg_pcd_ep object.
  62236. + */
  62237. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  62238. + struct dwc_otg_pcd_ep *ep)
  62239. +{
  62240. + struct cfi_ep *pcfiep = NULL;
  62241. + dwc_list_link_t *tmp;
  62242. +
  62243. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  62244. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  62245. + if (pcfiep->ep == ep) {
  62246. + return pcfiep;
  62247. + }
  62248. + }
  62249. + return NULL;
  62250. +}
  62251. +
  62252. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  62253. +
  62254. +#endif /* (__DWC_OTG_CFI_H__) */
  62255. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cil.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  62256. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cil.c 1970-01-01 01:00:00.000000000 +0100
  62257. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.c 2014-09-14 19:04:13.000000000 +0200
  62258. @@ -0,0 +1,7151 @@
  62259. +/* ==========================================================================
  62260. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  62261. + * $Revision: #191 $
  62262. + * $Date: 2012/08/10 $
  62263. + * $Change: 2047372 $
  62264. + *
  62265. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  62266. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  62267. + * otherwise expressly agreed to in writing between Synopsys and you.
  62268. + *
  62269. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  62270. + * any End User Software License Agreement or Agreement for Licensed Product
  62271. + * with Synopsys or any supplement thereto. You are permitted to use and
  62272. + * redistribute this Software in source and binary forms, with or without
  62273. + * modification, provided that redistributions of source code must retain this
  62274. + * notice. You may not view, use, disclose, copy or distribute this file or
  62275. + * any information contained herein except pursuant to this license grant from
  62276. + * Synopsys. If you do not agree with this notice, including the disclaimer
  62277. + * below, then you are not authorized to use the Software.
  62278. + *
  62279. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  62280. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  62281. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  62282. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  62283. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  62284. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  62285. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  62286. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  62287. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  62288. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  62289. + * DAMAGE.
  62290. + * ========================================================================== */
  62291. +
  62292. +/** @file
  62293. + *
  62294. + * The Core Interface Layer provides basic services for accessing and
  62295. + * managing the DWC_otg hardware. These services are used by both the
  62296. + * Host Controller Driver and the Peripheral Controller Driver.
  62297. + *
  62298. + * The CIL manages the memory map for the core so that the HCD and PCD
  62299. + * don't have to do this separately. It also handles basic tasks like
  62300. + * reading/writing the registers and data FIFOs in the controller.
  62301. + * Some of the data access functions provide encapsulation of several
  62302. + * operations required to perform a task, such as writing multiple
  62303. + * registers to start a transfer. Finally, the CIL performs basic
  62304. + * services that are not specific to either the host or device modes
  62305. + * of operation. These services include management of the OTG Host
  62306. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  62307. + * Diagnostic API is also provided to allow testing of the controller
  62308. + * hardware.
  62309. + *
  62310. + * The Core Interface Layer has the following requirements:
  62311. + * - Provides basic controller operations.
  62312. + * - Minimal use of OS services.
  62313. + * - The OS services used will be abstracted by using inline functions
  62314. + * or macros.
  62315. + *
  62316. + */
  62317. +
  62318. +#include "dwc_os.h"
  62319. +#include "dwc_otg_regs.h"
  62320. +#include "dwc_otg_cil.h"
  62321. +
  62322. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  62323. +
  62324. +/**
  62325. + * This function is called to initialize the DWC_otg CSR data
  62326. + * structures. The register addresses in the device and host
  62327. + * structures are initialized from the base address supplied by the
  62328. + * caller. The calling function must make the OS calls to get the
  62329. + * base address of the DWC_otg controller registers. The core_params
  62330. + * argument holds the parameters that specify how the core should be
  62331. + * configured.
  62332. + *
  62333. + * @param reg_base_addr Base address of DWC_otg core registers
  62334. + *
  62335. + */
  62336. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  62337. +{
  62338. + dwc_otg_core_if_t *core_if = 0;
  62339. + dwc_otg_dev_if_t *dev_if = 0;
  62340. + dwc_otg_host_if_t *host_if = 0;
  62341. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  62342. + int i = 0;
  62343. +
  62344. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  62345. +
  62346. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  62347. +
  62348. + if (core_if == NULL) {
  62349. + DWC_DEBUGPL(DBG_CIL,
  62350. + "Allocation of dwc_otg_core_if_t failed\n");
  62351. + return 0;
  62352. + }
  62353. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  62354. +
  62355. + /*
  62356. + * Allocate the Device Mode structures.
  62357. + */
  62358. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  62359. +
  62360. + if (dev_if == NULL) {
  62361. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  62362. + DWC_FREE(core_if);
  62363. + return 0;
  62364. + }
  62365. +
  62366. + dev_if->dev_global_regs =
  62367. + (dwc_otg_device_global_regs_t *) (reg_base +
  62368. + DWC_DEV_GLOBAL_REG_OFFSET);
  62369. +
  62370. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  62371. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  62372. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  62373. + (i * DWC_EP_REG_OFFSET));
  62374. +
  62375. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  62376. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  62377. + (i * DWC_EP_REG_OFFSET));
  62378. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  62379. + i, &dev_if->in_ep_regs[i]->diepctl);
  62380. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  62381. + i, &dev_if->out_ep_regs[i]->doepctl);
  62382. + }
  62383. +
  62384. + dev_if->speed = 0; // unknown
  62385. +
  62386. + core_if->dev_if = dev_if;
  62387. +
  62388. + /*
  62389. + * Allocate the Host Mode structures.
  62390. + */
  62391. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  62392. +
  62393. + if (host_if == NULL) {
  62394. + DWC_DEBUGPL(DBG_CIL,
  62395. + "Allocation of dwc_otg_host_if_t failed\n");
  62396. + DWC_FREE(dev_if);
  62397. + DWC_FREE(core_if);
  62398. + return 0;
  62399. + }
  62400. +
  62401. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  62402. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  62403. +
  62404. + host_if->hprt0 =
  62405. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  62406. +
  62407. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  62408. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  62409. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  62410. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  62411. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  62412. + i, &host_if->hc_regs[i]->hcchar);
  62413. + }
  62414. +
  62415. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  62416. + core_if->host_if = host_if;
  62417. +
  62418. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  62419. + core_if->data_fifo[i] =
  62420. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  62421. + (i * DWC_OTG_DATA_FIFO_SIZE));
  62422. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  62423. + i, (unsigned long)core_if->data_fifo[i]);
  62424. + }
  62425. +
  62426. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  62427. +
  62428. + /* Initiate lx_state to L3 disconnected state */
  62429. + core_if->lx_state = DWC_OTG_L3;
  62430. + /*
  62431. + * Store the contents of the hardware configuration registers here for
  62432. + * easy access later.
  62433. + */
  62434. + core_if->hwcfg1.d32 =
  62435. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  62436. + core_if->hwcfg2.d32 =
  62437. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  62438. + core_if->hwcfg3.d32 =
  62439. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  62440. + core_if->hwcfg4.d32 =
  62441. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  62442. +
  62443. + /* Force host mode to get HPTXFSIZ exact power on value */
  62444. + {
  62445. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  62446. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62447. + gusbcfg.b.force_host_mode = 1;
  62448. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  62449. + dwc_mdelay(100);
  62450. + core_if->hptxfsiz.d32 =
  62451. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  62452. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  62453. + gusbcfg.b.force_host_mode = 0;
  62454. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  62455. + dwc_mdelay(100);
  62456. + }
  62457. +
  62458. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  62459. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  62460. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  62461. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  62462. +
  62463. + core_if->hcfg.d32 =
  62464. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  62465. + core_if->dcfg.d32 =
  62466. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  62467. +
  62468. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  62469. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  62470. +
  62471. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  62472. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  62473. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  62474. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  62475. + core_if->hwcfg2.b.num_host_chan);
  62476. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  62477. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  62478. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  62479. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  62480. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  62481. + core_if->hwcfg2.b.dev_token_q_depth);
  62482. +
  62483. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  62484. + core_if->hwcfg3.b.dfifo_depth);
  62485. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  62486. + core_if->hwcfg3.b.xfer_size_cntr_width);
  62487. +
  62488. + /*
  62489. + * Set the SRP sucess bit for FS-I2c
  62490. + */
  62491. + core_if->srp_success = 0;
  62492. + core_if->srp_timer_started = 0;
  62493. +
  62494. + /*
  62495. + * Create new workqueue and init works
  62496. + */
  62497. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  62498. + if (core_if->wq_otg == 0) {
  62499. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  62500. + DWC_FREE(host_if);
  62501. + DWC_FREE(dev_if);
  62502. + DWC_FREE(core_if);
  62503. + return 0;
  62504. + }
  62505. +
  62506. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  62507. +
  62508. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  62509. + (core_if->snpsid >> 12 & 0xF),
  62510. + (core_if->snpsid >> 8 & 0xF),
  62511. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  62512. +
  62513. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  62514. + w_wakeup_detected, core_if);
  62515. + if (core_if->wkp_timer == 0) {
  62516. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  62517. + DWC_FREE(host_if);
  62518. + DWC_FREE(dev_if);
  62519. + DWC_WORKQ_FREE(core_if->wq_otg);
  62520. + DWC_FREE(core_if);
  62521. + return 0;
  62522. + }
  62523. +
  62524. + if (dwc_otg_setup_params(core_if)) {
  62525. + DWC_WARN("Error while setting core params\n");
  62526. + }
  62527. +
  62528. + core_if->hibernation_suspend = 0;
  62529. +
  62530. + /** ADP initialization */
  62531. + dwc_otg_adp_init(core_if);
  62532. +
  62533. + return core_if;
  62534. +}
  62535. +
  62536. +/**
  62537. + * This function frees the structures allocated by dwc_otg_cil_init().
  62538. + *
  62539. + * @param core_if The core interface pointer returned from
  62540. + * dwc_otg_cil_init().
  62541. + *
  62542. + */
  62543. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  62544. +{
  62545. + dctl_data_t dctl = {.d32 = 0 };
  62546. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  62547. +
  62548. + /* Disable all interrupts */
  62549. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  62550. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  62551. +
  62552. + dctl.b.sftdiscon = 1;
  62553. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  62554. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  62555. + dctl.d32);
  62556. + }
  62557. +
  62558. + if (core_if->wq_otg) {
  62559. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  62560. + DWC_WORKQ_FREE(core_if->wq_otg);
  62561. + }
  62562. + if (core_if->dev_if) {
  62563. + DWC_FREE(core_if->dev_if);
  62564. + }
  62565. + if (core_if->host_if) {
  62566. + DWC_FREE(core_if->host_if);
  62567. + }
  62568. +
  62569. + /** Remove ADP Stuff */
  62570. + dwc_otg_adp_remove(core_if);
  62571. + if (core_if->core_params) {
  62572. + DWC_FREE(core_if->core_params);
  62573. + }
  62574. + if (core_if->wkp_timer) {
  62575. + DWC_TIMER_FREE(core_if->wkp_timer);
  62576. + }
  62577. + if (core_if->srp_timer) {
  62578. + DWC_TIMER_FREE(core_if->srp_timer);
  62579. + }
  62580. + DWC_FREE(core_if);
  62581. +}
  62582. +
  62583. +/**
  62584. + * This function enables the controller's Global Interrupt in the AHB Config
  62585. + * register.
  62586. + *
  62587. + * @param core_if Programming view of DWC_otg controller.
  62588. + */
  62589. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  62590. +{
  62591. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  62592. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  62593. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  62594. +}
  62595. +
  62596. +/**
  62597. + * This function disables the controller's Global Interrupt in the AHB Config
  62598. + * register.
  62599. + *
  62600. + * @param core_if Programming view of DWC_otg controller.
  62601. + */
  62602. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  62603. +{
  62604. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  62605. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  62606. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  62607. +}
  62608. +
  62609. +/**
  62610. + * This function initializes the commmon interrupts, used in both
  62611. + * device and host modes.
  62612. + *
  62613. + * @param core_if Programming view of the DWC_otg controller
  62614. + *
  62615. + */
  62616. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  62617. +{
  62618. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  62619. + gintmsk_data_t intr_mask = {.d32 = 0 };
  62620. +
  62621. + /* Clear any pending OTG Interrupts */
  62622. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  62623. +
  62624. + /* Clear any pending interrupts */
  62625. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  62626. +
  62627. + /*
  62628. + * Enable the interrupts in the GINTMSK.
  62629. + */
  62630. + intr_mask.b.modemismatch = 1;
  62631. + intr_mask.b.otgintr = 1;
  62632. +
  62633. + if (!core_if->dma_enable) {
  62634. + intr_mask.b.rxstsqlvl = 1;
  62635. + }
  62636. +
  62637. + intr_mask.b.conidstschng = 1;
  62638. + intr_mask.b.wkupintr = 1;
  62639. + intr_mask.b.disconnect = 0;
  62640. + intr_mask.b.usbsuspend = 1;
  62641. + intr_mask.b.sessreqintr = 1;
  62642. +#ifdef CONFIG_USB_DWC_OTG_LPM
  62643. + if (core_if->core_params->lpm_enable) {
  62644. + intr_mask.b.lpmtranrcvd = 1;
  62645. + }
  62646. +#endif
  62647. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  62648. +}
  62649. +
  62650. +/*
  62651. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  62652. + * Hibernation. This function is for exiting from Device mode hibernation by
  62653. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  62654. + * @param core_if Programming view of DWC_otg controller.
  62655. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  62656. + * @param reset - indicates whether resume is initiated by Reset.
  62657. + */
  62658. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  62659. + int rem_wakeup, int reset)
  62660. +{
  62661. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  62662. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  62663. + dctl_data_t dctl = {.d32 = 0 };
  62664. +
  62665. + int timeout = 2000;
  62666. +
  62667. + if (!core_if->hibernation_suspend) {
  62668. + DWC_PRINTF("Already exited from Hibernation\n");
  62669. + return 1;
  62670. + }
  62671. +
  62672. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  62673. + /* Switch-on voltage to the core */
  62674. + gpwrdn.b.pwrdnswtch = 1;
  62675. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62676. + dwc_udelay(10);
  62677. +
  62678. + /* Reset core */
  62679. + gpwrdn.d32 = 0;
  62680. + gpwrdn.b.pwrdnrstn = 1;
  62681. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62682. + dwc_udelay(10);
  62683. +
  62684. + /* Assert Restore signal */
  62685. + gpwrdn.d32 = 0;
  62686. + gpwrdn.b.restore = 1;
  62687. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  62688. + dwc_udelay(10);
  62689. +
  62690. + /* Disable power clamps */
  62691. + gpwrdn.d32 = 0;
  62692. + gpwrdn.b.pwrdnclmp = 1;
  62693. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62694. +
  62695. + if (rem_wakeup) {
  62696. + dwc_udelay(70);
  62697. + }
  62698. +
  62699. + /* Deassert Reset core */
  62700. + gpwrdn.d32 = 0;
  62701. + gpwrdn.b.pwrdnrstn = 1;
  62702. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  62703. + dwc_udelay(10);
  62704. +
  62705. + /* Disable PMU interrupt */
  62706. + gpwrdn.d32 = 0;
  62707. + gpwrdn.b.pmuintsel = 1;
  62708. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62709. +
  62710. + /* Mask interrupts from gpwrdn */
  62711. + gpwrdn.d32 = 0;
  62712. + gpwrdn.b.connect_det_msk = 1;
  62713. + gpwrdn.b.srp_det_msk = 1;
  62714. + gpwrdn.b.disconn_det_msk = 1;
  62715. + gpwrdn.b.rst_det_msk = 1;
  62716. + gpwrdn.b.lnstchng_msk = 1;
  62717. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62718. +
  62719. + /* Indicates that we are going out from hibernation */
  62720. + core_if->hibernation_suspend = 0;
  62721. +
  62722. + /*
  62723. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  62724. + * indicates restore from remote_wakeup
  62725. + */
  62726. + restore_essential_regs(core_if, rem_wakeup, 0);
  62727. +
  62728. + /*
  62729. + * Wait a little for seeing new value of variable hibernation_suspend if
  62730. + * Restore done interrupt received before polling
  62731. + */
  62732. + dwc_udelay(10);
  62733. +
  62734. + if (core_if->hibernation_suspend == 0) {
  62735. + /*
  62736. + * Wait For Restore_done Interrupt. This mechanism of polling the
  62737. + * interrupt is introduced to avoid any possible race conditions
  62738. + */
  62739. + do {
  62740. + gintsts_data_t gintsts;
  62741. + gintsts.d32 =
  62742. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  62743. + if (gintsts.b.restoredone) {
  62744. + gintsts.d32 = 0;
  62745. + gintsts.b.restoredone = 1;
  62746. + DWC_WRITE_REG32(&core_if->core_global_regs->
  62747. + gintsts, gintsts.d32);
  62748. + DWC_PRINTF("Restore Done Interrupt seen\n");
  62749. + break;
  62750. + }
  62751. + dwc_udelay(10);
  62752. + } while (--timeout);
  62753. + if (!timeout) {
  62754. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  62755. + }
  62756. + }
  62757. + /* Clear all pending interupts */
  62758. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  62759. +
  62760. + /* De-assert Restore */
  62761. + gpwrdn.d32 = 0;
  62762. + gpwrdn.b.restore = 1;
  62763. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62764. + dwc_udelay(10);
  62765. +
  62766. + if (!rem_wakeup) {
  62767. + pcgcctl.d32 = 0;
  62768. + pcgcctl.b.rstpdwnmodule = 1;
  62769. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  62770. + }
  62771. +
  62772. + /* Restore GUSBCFG and DCFG */
  62773. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  62774. + core_if->gr_backup->gusbcfg_local);
  62775. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  62776. + core_if->dr_backup->dcfg);
  62777. +
  62778. + /* De-assert Wakeup Logic */
  62779. + gpwrdn.d32 = 0;
  62780. + gpwrdn.b.pmuactv = 1;
  62781. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62782. + dwc_udelay(10);
  62783. +
  62784. + if (!rem_wakeup) {
  62785. + /* Set Device programming done bit */
  62786. + dctl.b.pwronprgdone = 1;
  62787. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  62788. + } else {
  62789. + /* Start Remote Wakeup Signaling */
  62790. + dctl.d32 = core_if->dr_backup->dctl;
  62791. + dctl.b.rmtwkupsig = 1;
  62792. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  62793. + }
  62794. +
  62795. + dwc_mdelay(2);
  62796. + /* Clear all pending interupts */
  62797. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  62798. +
  62799. + /* Restore global registers */
  62800. + dwc_otg_restore_global_regs(core_if);
  62801. + /* Restore device global registers */
  62802. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  62803. +
  62804. + if (rem_wakeup) {
  62805. + dwc_mdelay(7);
  62806. + dctl.d32 = 0;
  62807. + dctl.b.rmtwkupsig = 1;
  62808. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  62809. + }
  62810. +
  62811. + core_if->hibernation_suspend = 0;
  62812. + /* The core will be in ON STATE */
  62813. + core_if->lx_state = DWC_OTG_L0;
  62814. + DWC_PRINTF("Hibernation recovery completes here\n");
  62815. +
  62816. + return 1;
  62817. +}
  62818. +
  62819. +/*
  62820. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  62821. + * Hibernation. This function is for exiting from Host mode hibernation by
  62822. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  62823. + * @param core_if Programming view of DWC_otg controller.
  62824. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  62825. + * @param reset - indicates whether resume is initiated by Reset.
  62826. + */
  62827. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  62828. + int rem_wakeup, int reset)
  62829. +{
  62830. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  62831. + hprt0_data_t hprt0 = {.d32 = 0 };
  62832. +
  62833. + int timeout = 2000;
  62834. +
  62835. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  62836. + /* Switch-on voltage to the core */
  62837. + gpwrdn.b.pwrdnswtch = 1;
  62838. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62839. + dwc_udelay(10);
  62840. +
  62841. + /* Reset core */
  62842. + gpwrdn.d32 = 0;
  62843. + gpwrdn.b.pwrdnrstn = 1;
  62844. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62845. + dwc_udelay(10);
  62846. +
  62847. + /* Assert Restore signal */
  62848. + gpwrdn.d32 = 0;
  62849. + gpwrdn.b.restore = 1;
  62850. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  62851. + dwc_udelay(10);
  62852. +
  62853. + /* Disable power clamps */
  62854. + gpwrdn.d32 = 0;
  62855. + gpwrdn.b.pwrdnclmp = 1;
  62856. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62857. +
  62858. + if (!rem_wakeup) {
  62859. + dwc_udelay(50);
  62860. + }
  62861. +
  62862. + /* Deassert Reset core */
  62863. + gpwrdn.d32 = 0;
  62864. + gpwrdn.b.pwrdnrstn = 1;
  62865. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  62866. + dwc_udelay(10);
  62867. +
  62868. + /* Disable PMU interrupt */
  62869. + gpwrdn.d32 = 0;
  62870. + gpwrdn.b.pmuintsel = 1;
  62871. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62872. +
  62873. + gpwrdn.d32 = 0;
  62874. + gpwrdn.b.connect_det_msk = 1;
  62875. + gpwrdn.b.srp_det_msk = 1;
  62876. + gpwrdn.b.disconn_det_msk = 1;
  62877. + gpwrdn.b.rst_det_msk = 1;
  62878. + gpwrdn.b.lnstchng_msk = 1;
  62879. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62880. +
  62881. + /* Indicates that we are going out from hibernation */
  62882. + core_if->hibernation_suspend = 0;
  62883. +
  62884. + /* Set Restore Essential Regs bit in PCGCCTL register */
  62885. + restore_essential_regs(core_if, rem_wakeup, 1);
  62886. +
  62887. + /* Wait a little for seeing new value of variable hibernation_suspend if
  62888. + * Restore done interrupt received before polling */
  62889. + dwc_udelay(10);
  62890. +
  62891. + if (core_if->hibernation_suspend == 0) {
  62892. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  62893. + * interrupt is introduced to avoid any possible race conditions
  62894. + */
  62895. + do {
  62896. + gintsts_data_t gintsts;
  62897. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  62898. + if (gintsts.b.restoredone) {
  62899. + gintsts.d32 = 0;
  62900. + gintsts.b.restoredone = 1;
  62901. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  62902. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  62903. + break;
  62904. + }
  62905. + dwc_udelay(10);
  62906. + } while (--timeout);
  62907. + if (!timeout) {
  62908. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  62909. + }
  62910. + }
  62911. +
  62912. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  62913. + core_if->hibernation_suspend = 0;
  62914. +
  62915. + /* This step is not described in functional spec but if not wait for this
  62916. + * delay, mismatch interrupts occurred because just after restore core is
  62917. + * in Device mode(gintsts.curmode == 0) */
  62918. + dwc_mdelay(100);
  62919. +
  62920. + /* Clear all pending interrupts */
  62921. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  62922. +
  62923. + /* De-assert Restore */
  62924. + gpwrdn.d32 = 0;
  62925. + gpwrdn.b.restore = 1;
  62926. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62927. + dwc_udelay(10);
  62928. +
  62929. + /* Restore GUSBCFG and HCFG */
  62930. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  62931. + core_if->gr_backup->gusbcfg_local);
  62932. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  62933. + core_if->hr_backup->hcfg_local);
  62934. +
  62935. + /* De-assert Wakeup Logic */
  62936. + gpwrdn.d32 = 0;
  62937. + gpwrdn.b.pmuactv = 1;
  62938. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  62939. + dwc_udelay(10);
  62940. +
  62941. + /* Start the Resume operation by programming HPRT0 */
  62942. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  62943. + hprt0.b.prtpwr = 1;
  62944. + hprt0.b.prtena = 0;
  62945. + hprt0.b.prtsusp = 0;
  62946. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62947. +
  62948. + DWC_PRINTF("Resume Starts Now\n");
  62949. + if (!reset) { // Indicates it is Resume Operation
  62950. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  62951. + hprt0.b.prtres = 1;
  62952. + hprt0.b.prtpwr = 1;
  62953. + hprt0.b.prtena = 0;
  62954. + hprt0.b.prtsusp = 0;
  62955. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62956. +
  62957. + if (!rem_wakeup)
  62958. + hprt0.b.prtres = 0;
  62959. + /* Wait for Resume time and then program HPRT again */
  62960. + dwc_mdelay(100);
  62961. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62962. +
  62963. + } else { // Indicates it is Reset Operation
  62964. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  62965. + hprt0.b.prtrst = 1;
  62966. + hprt0.b.prtpwr = 1;
  62967. + hprt0.b.prtena = 0;
  62968. + hprt0.b.prtsusp = 0;
  62969. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62970. + /* Wait for Reset time and then program HPRT again */
  62971. + dwc_mdelay(60);
  62972. + hprt0.b.prtrst = 0;
  62973. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62974. + }
  62975. + /* Clear all interrupt status */
  62976. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  62977. + hprt0.b.prtconndet = 1;
  62978. + hprt0.b.prtenchng = 1;
  62979. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  62980. +
  62981. + /* Clear all pending interupts */
  62982. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  62983. +
  62984. + /* Restore global registers */
  62985. + dwc_otg_restore_global_regs(core_if);
  62986. + /* Restore host global registers */
  62987. + dwc_otg_restore_host_regs(core_if, reset);
  62988. +
  62989. + /* The core will be in ON STATE */
  62990. + core_if->lx_state = DWC_OTG_L0;
  62991. + DWC_PRINTF("Hibernation recovery is complete here\n");
  62992. + return 0;
  62993. +}
  62994. +
  62995. +/** Saves some register values into system memory. */
  62996. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  62997. +{
  62998. + struct dwc_otg_global_regs_backup *gr;
  62999. + int i;
  63000. +
  63001. + gr = core_if->gr_backup;
  63002. + if (!gr) {
  63003. + gr = DWC_ALLOC(sizeof(*gr));
  63004. + if (!gr) {
  63005. + return -DWC_E_NO_MEMORY;
  63006. + }
  63007. + core_if->gr_backup = gr;
  63008. + }
  63009. +
  63010. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  63011. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  63012. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  63013. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  63014. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  63015. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  63016. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  63017. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63018. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  63019. +#endif
  63020. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  63021. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  63022. + gr->gdfifocfg_local =
  63023. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  63024. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  63025. + gr->dtxfsiz_local[i] =
  63026. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  63027. + }
  63028. +
  63029. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  63030. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  63031. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  63032. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  63033. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  63034. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  63035. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  63036. + gr->gnptxfsiz_local);
  63037. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  63038. + gr->hptxfsiz_local);
  63039. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63040. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  63041. +#endif
  63042. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  63043. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  63044. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  63045. +
  63046. + return 0;
  63047. +}
  63048. +
  63049. +/** Saves GINTMSK register before setting the msk bits. */
  63050. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  63051. +{
  63052. + struct dwc_otg_global_regs_backup *gr;
  63053. +
  63054. + gr = core_if->gr_backup;
  63055. + if (!gr) {
  63056. + gr = DWC_ALLOC(sizeof(*gr));
  63057. + if (!gr) {
  63058. + return -DWC_E_NO_MEMORY;
  63059. + }
  63060. + core_if->gr_backup = gr;
  63061. + }
  63062. +
  63063. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  63064. +
  63065. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  63066. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  63067. +
  63068. + return 0;
  63069. +}
  63070. +
  63071. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  63072. +{
  63073. + struct dwc_otg_dev_regs_backup *dr;
  63074. + int i;
  63075. +
  63076. + dr = core_if->dr_backup;
  63077. + if (!dr) {
  63078. + dr = DWC_ALLOC(sizeof(*dr));
  63079. + if (!dr) {
  63080. + return -DWC_E_NO_MEMORY;
  63081. + }
  63082. + core_if->dr_backup = dr;
  63083. + }
  63084. +
  63085. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  63086. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  63087. + dr->daintmsk =
  63088. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  63089. + dr->diepmsk =
  63090. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  63091. + dr->doepmsk =
  63092. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  63093. +
  63094. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  63095. + dr->diepctl[i] =
  63096. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  63097. + dr->dieptsiz[i] =
  63098. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  63099. + dr->diepdma[i] =
  63100. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  63101. + }
  63102. +
  63103. + DWC_DEBUGPL(DBG_ANY,
  63104. + "=============Backing Host registers==============\n");
  63105. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  63106. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  63107. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  63108. + dr->daintmsk);
  63109. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  63110. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  63111. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  63112. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  63113. + dr->diepctl[i]);
  63114. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  63115. + i, dr->dieptsiz[i]);
  63116. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  63117. + dr->diepdma[i]);
  63118. + }
  63119. +
  63120. + return 0;
  63121. +}
  63122. +
  63123. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  63124. +{
  63125. + struct dwc_otg_host_regs_backup *hr;
  63126. + int i;
  63127. +
  63128. + hr = core_if->hr_backup;
  63129. + if (!hr) {
  63130. + hr = DWC_ALLOC(sizeof(*hr));
  63131. + if (!hr) {
  63132. + return -DWC_E_NO_MEMORY;
  63133. + }
  63134. + core_if->hr_backup = hr;
  63135. + }
  63136. +
  63137. + hr->hcfg_local =
  63138. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  63139. + hr->haintmsk_local =
  63140. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  63141. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  63142. + hr->hcintmsk_local[i] =
  63143. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  63144. + }
  63145. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  63146. + hr->hfir_local =
  63147. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  63148. +
  63149. + DWC_DEBUGPL(DBG_ANY,
  63150. + "=============Backing Host registers===============\n");
  63151. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  63152. + hr->hcfg_local);
  63153. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  63154. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  63155. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  63156. + hr->hcintmsk_local[i]);
  63157. + }
  63158. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  63159. + hr->hprt0_local);
  63160. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  63161. + hr->hfir_local);
  63162. +
  63163. + return 0;
  63164. +}
  63165. +
  63166. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  63167. +{
  63168. + struct dwc_otg_global_regs_backup *gr;
  63169. + int i;
  63170. +
  63171. + gr = core_if->gr_backup;
  63172. + if (!gr) {
  63173. + return -DWC_E_INVALID;
  63174. + }
  63175. +
  63176. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  63177. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  63178. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  63179. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  63180. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  63181. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  63182. + gr->gnptxfsiz_local);
  63183. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  63184. + gr->hptxfsiz_local);
  63185. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  63186. + gr->gdfifocfg_local);
  63187. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  63188. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  63189. + gr->dtxfsiz_local[i]);
  63190. + }
  63191. +
  63192. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  63193. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  63194. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  63195. + (gr->gahbcfg_local));
  63196. + return 0;
  63197. +}
  63198. +
  63199. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  63200. +{
  63201. + struct dwc_otg_dev_regs_backup *dr;
  63202. + int i;
  63203. +
  63204. + dr = core_if->dr_backup;
  63205. +
  63206. + if (!dr) {
  63207. + return -DWC_E_INVALID;
  63208. + }
  63209. +
  63210. + if (!rem_wakeup) {
  63211. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  63212. + dr->dctl);
  63213. + }
  63214. +
  63215. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  63216. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  63217. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  63218. +
  63219. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  63220. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  63221. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  63222. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  63223. + }
  63224. +
  63225. + return 0;
  63226. +}
  63227. +
  63228. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  63229. +{
  63230. + struct dwc_otg_host_regs_backup *hr;
  63231. + int i;
  63232. + hr = core_if->hr_backup;
  63233. +
  63234. + if (!hr) {
  63235. + return -DWC_E_INVALID;
  63236. + }
  63237. +
  63238. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  63239. + //if (!reset)
  63240. + //{
  63241. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  63242. + //}
  63243. +
  63244. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  63245. + hr->haintmsk_local);
  63246. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  63247. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  63248. + hr->hcintmsk_local[i]);
  63249. + }
  63250. +
  63251. + return 0;
  63252. +}
  63253. +
  63254. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  63255. +{
  63256. + struct dwc_otg_global_regs_backup *gr;
  63257. +
  63258. + gr = core_if->gr_backup;
  63259. +
  63260. + /* Restore values for LPM and I2C */
  63261. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63262. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  63263. +#endif
  63264. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  63265. +
  63266. + return 0;
  63267. +}
  63268. +
  63269. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  63270. +{
  63271. + struct dwc_otg_global_regs_backup *gr;
  63272. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  63273. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  63274. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  63275. + gintmsk_data_t gintmsk = {.d32 = 0 };
  63276. +
  63277. + /* Restore LPM and I2C registers */
  63278. + restore_lpm_i2c_regs(core_if);
  63279. +
  63280. + /* Set PCGCCTL to 0 */
  63281. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  63282. +
  63283. + gr = core_if->gr_backup;
  63284. + /* Load restore values for [31:14] bits */
  63285. + DWC_WRITE_REG32(core_if->pcgcctl,
  63286. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  63287. +
  63288. + /* Umnask global Interrupt in GAHBCFG and restore it */
  63289. + gahbcfg.d32 = gr->gahbcfg_local;
  63290. + gahbcfg.b.glblintrmsk = 1;
  63291. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  63292. +
  63293. + /* Clear all pending interupts */
  63294. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  63295. +
  63296. + /* Unmask restore done interrupt */
  63297. + gintmsk.b.restoredone = 1;
  63298. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  63299. +
  63300. + /* Restore GUSBCFG and HCFG/DCFG */
  63301. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  63302. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  63303. +
  63304. + if (is_host) {
  63305. + hcfg_data_t hcfg = {.d32 = 0 };
  63306. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  63307. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  63308. + hcfg.d32);
  63309. +
  63310. + /* Load restore values for [31:14] bits */
  63311. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  63312. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  63313. +
  63314. + if (rmode)
  63315. + pcgcctl.b.restoremode = 1;
  63316. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  63317. + dwc_udelay(10);
  63318. +
  63319. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  63320. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  63321. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  63322. + pcgcctl.b.ess_reg_restored = 1;
  63323. + if (rmode)
  63324. + pcgcctl.b.restoremode = 1;
  63325. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  63326. + } else {
  63327. + dcfg_data_t dcfg = {.d32 = 0 };
  63328. + dcfg.d32 = core_if->dr_backup->dcfg;
  63329. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  63330. +
  63331. + /* Load restore values for [31:14] bits */
  63332. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  63333. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  63334. + if (!rmode) {
  63335. + pcgcctl.d32 |= 0x208;
  63336. + }
  63337. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  63338. + dwc_udelay(10);
  63339. +
  63340. + /* Load restore values for [31:14] bits */
  63341. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  63342. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  63343. + pcgcctl.b.ess_reg_restored = 1;
  63344. + if (!rmode)
  63345. + pcgcctl.d32 |= 0x208;
  63346. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  63347. + }
  63348. +
  63349. + return 0;
  63350. +}
  63351. +
  63352. +/**
  63353. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  63354. + * type.
  63355. + */
  63356. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  63357. +{
  63358. + uint32_t val;
  63359. + hcfg_data_t hcfg;
  63360. +
  63361. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  63362. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  63363. + (core_if->core_params->ulpi_fs_ls)) ||
  63364. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  63365. + /* Full speed PHY */
  63366. + val = DWC_HCFG_48_MHZ;
  63367. + } else {
  63368. + /* High speed PHY running at full speed or high speed */
  63369. + val = DWC_HCFG_30_60_MHZ;
  63370. + }
  63371. +
  63372. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  63373. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  63374. + hcfg.b.fslspclksel = val;
  63375. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  63376. +}
  63377. +
  63378. +/**
  63379. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  63380. + * and the enumeration speed of the device.
  63381. + */
  63382. +static void init_devspd(dwc_otg_core_if_t * core_if)
  63383. +{
  63384. + uint32_t val;
  63385. + dcfg_data_t dcfg;
  63386. +
  63387. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  63388. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  63389. + (core_if->core_params->ulpi_fs_ls)) ||
  63390. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  63391. + /* Full speed PHY */
  63392. + val = 0x3;
  63393. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  63394. + /* High speed PHY running at full speed */
  63395. + val = 0x1;
  63396. + } else {
  63397. + /* High speed PHY running at high speed */
  63398. + val = 0x0;
  63399. + }
  63400. +
  63401. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  63402. +
  63403. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  63404. + dcfg.b.devspd = val;
  63405. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  63406. +}
  63407. +
  63408. +/**
  63409. + * This function calculates the number of IN EPS
  63410. + * using GHWCFG1 and GHWCFG2 registers values
  63411. + *
  63412. + * @param core_if Programming view of the DWC_otg controller
  63413. + */
  63414. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  63415. +{
  63416. + uint32_t num_in_eps = 0;
  63417. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  63418. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  63419. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  63420. + int i;
  63421. +
  63422. + for (i = 0; i < num_eps; ++i) {
  63423. + if (!(hwcfg1 & 0x1))
  63424. + num_in_eps++;
  63425. +
  63426. + hwcfg1 >>= 2;
  63427. + }
  63428. +
  63429. + if (core_if->hwcfg4.b.ded_fifo_en) {
  63430. + num_in_eps =
  63431. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  63432. + }
  63433. +
  63434. + return num_in_eps;
  63435. +}
  63436. +
  63437. +/**
  63438. + * This function calculates the number of OUT EPS
  63439. + * using GHWCFG1 and GHWCFG2 registers values
  63440. + *
  63441. + * @param core_if Programming view of the DWC_otg controller
  63442. + */
  63443. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  63444. +{
  63445. + uint32_t num_out_eps = 0;
  63446. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  63447. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  63448. + int i;
  63449. +
  63450. + for (i = 0; i < num_eps; ++i) {
  63451. + if (!(hwcfg1 & 0x1))
  63452. + num_out_eps++;
  63453. +
  63454. + hwcfg1 >>= 2;
  63455. + }
  63456. + return num_out_eps;
  63457. +}
  63458. +
  63459. +/**
  63460. + * This function initializes the DWC_otg controller registers and
  63461. + * prepares the core for device mode or host mode operation.
  63462. + *
  63463. + * @param core_if Programming view of the DWC_otg controller
  63464. + *
  63465. + */
  63466. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  63467. +{
  63468. + int i = 0;
  63469. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63470. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  63471. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  63472. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  63473. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  63474. +
  63475. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  63476. + core_if, global_regs);
  63477. +
  63478. + /* Common Initialization */
  63479. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  63480. +
  63481. + /* Program the ULPI External VBUS bit if needed */
  63482. + usbcfg.b.ulpi_ext_vbus_drv =
  63483. + (core_if->core_params->phy_ulpi_ext_vbus ==
  63484. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  63485. +
  63486. + /* Set external TS Dline pulsing */
  63487. + usbcfg.b.term_sel_dl_pulse =
  63488. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  63489. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  63490. +
  63491. + /* Reset the Controller */
  63492. + dwc_otg_core_reset(core_if);
  63493. +
  63494. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  63495. + core_if->power_down = core_if->core_params->power_down;
  63496. + core_if->otg_sts = 0;
  63497. +
  63498. + /* Initialize parameters from Hardware configuration registers. */
  63499. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  63500. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  63501. +
  63502. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  63503. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  63504. +
  63505. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  63506. + dev_if->perio_tx_fifo_size[i] =
  63507. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  63508. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  63509. + i, dev_if->perio_tx_fifo_size[i]);
  63510. + }
  63511. +
  63512. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  63513. + dev_if->tx_fifo_size[i] =
  63514. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  63515. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  63516. + i, dev_if->tx_fifo_size[i]);
  63517. + }
  63518. +
  63519. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  63520. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  63521. + core_if->nperio_tx_fifo_size =
  63522. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  63523. +
  63524. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  63525. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  63526. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  63527. + core_if->nperio_tx_fifo_size);
  63528. +
  63529. + /* This programming sequence needs to happen in FS mode before any other
  63530. + * programming occurs */
  63531. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  63532. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  63533. + /* If FS mode with FS PHY */
  63534. +
  63535. + /* core_init() is now called on every switch so only call the
  63536. + * following for the first time through. */
  63537. + if (!core_if->phy_init_done) {
  63538. + core_if->phy_init_done = 1;
  63539. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  63540. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  63541. + usbcfg.b.physel = 1;
  63542. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  63543. +
  63544. + /* Reset after a PHY select */
  63545. + dwc_otg_core_reset(core_if);
  63546. + }
  63547. +
  63548. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  63549. + * do this on HNP Dev/Host mode switches (done in dev_init and
  63550. + * host_init). */
  63551. + if (dwc_otg_is_host_mode(core_if)) {
  63552. + init_fslspclksel(core_if);
  63553. + } else {
  63554. + init_devspd(core_if);
  63555. + }
  63556. +
  63557. + if (core_if->core_params->i2c_enable) {
  63558. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  63559. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  63560. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  63561. + usbcfg.b.otgutmifssel = 1;
  63562. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  63563. +
  63564. + /* Program GI2CCTL.I2CEn */
  63565. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  63566. + i2cctl.b.i2cdevaddr = 1;
  63567. + i2cctl.b.i2cen = 0;
  63568. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  63569. + i2cctl.b.i2cen = 1;
  63570. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  63571. + }
  63572. +
  63573. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  63574. + else {
  63575. + /* High speed PHY. */
  63576. + if (!core_if->phy_init_done) {
  63577. + core_if->phy_init_done = 1;
  63578. + /* HS PHY parameters. These parameters are preserved
  63579. + * during soft reset so only program the first time. Do
  63580. + * a soft reset immediately after setting phyif. */
  63581. +
  63582. + if (core_if->core_params->phy_type == 2) {
  63583. + /* ULPI interface */
  63584. + usbcfg.b.ulpi_utmi_sel = 1;
  63585. + usbcfg.b.phyif = 0;
  63586. + usbcfg.b.ddrsel =
  63587. + core_if->core_params->phy_ulpi_ddr;
  63588. + } else if (core_if->core_params->phy_type == 1) {
  63589. + /* UTMI+ interface */
  63590. + usbcfg.b.ulpi_utmi_sel = 0;
  63591. + if (core_if->core_params->phy_utmi_width == 16) {
  63592. + usbcfg.b.phyif = 1;
  63593. +
  63594. + } else {
  63595. + usbcfg.b.phyif = 0;
  63596. + }
  63597. + } else {
  63598. + DWC_ERROR("FS PHY TYPE\n");
  63599. + }
  63600. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  63601. + /* Reset after setting the PHY parameters */
  63602. + dwc_otg_core_reset(core_if);
  63603. + }
  63604. + }
  63605. +
  63606. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  63607. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  63608. + (core_if->core_params->ulpi_fs_ls)) {
  63609. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  63610. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  63611. + usbcfg.b.ulpi_fsls = 1;
  63612. + usbcfg.b.ulpi_clk_sus_m = 1;
  63613. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  63614. + } else {
  63615. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  63616. + usbcfg.b.ulpi_fsls = 0;
  63617. + usbcfg.b.ulpi_clk_sus_m = 0;
  63618. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  63619. + }
  63620. +
  63621. + /* Program the GAHBCFG Register. */
  63622. + switch (core_if->hwcfg2.b.architecture) {
  63623. +
  63624. + case DWC_SLAVE_ONLY_ARCH:
  63625. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  63626. + ahbcfg.b.nptxfemplvl_txfemplvl =
  63627. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  63628. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  63629. + core_if->dma_enable = 0;
  63630. + core_if->dma_desc_enable = 0;
  63631. + break;
  63632. +
  63633. + case DWC_EXT_DMA_ARCH:
  63634. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  63635. + {
  63636. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  63637. + ahbcfg.b.hburstlen = 0;
  63638. + while (brst_sz > 1) {
  63639. + ahbcfg.b.hburstlen++;
  63640. + brst_sz >>= 1;
  63641. + }
  63642. + }
  63643. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  63644. + core_if->dma_desc_enable =
  63645. + (core_if->core_params->dma_desc_enable != 0);
  63646. + break;
  63647. +
  63648. + case DWC_INT_DMA_ARCH:
  63649. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  63650. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  63651. + Host mode ISOC in issue fix - vahrama */
  63652. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  63653. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  63654. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  63655. + core_if->dma_desc_enable =
  63656. + (core_if->core_params->dma_desc_enable != 0);
  63657. + break;
  63658. +
  63659. + }
  63660. + if (core_if->dma_enable) {
  63661. + if (core_if->dma_desc_enable) {
  63662. + DWC_PRINTF("Using Descriptor DMA mode\n");
  63663. + } else {
  63664. + DWC_PRINTF("Using Buffer DMA mode\n");
  63665. +
  63666. + }
  63667. + } else {
  63668. + DWC_PRINTF("Using Slave mode\n");
  63669. + core_if->dma_desc_enable = 0;
  63670. + }
  63671. +
  63672. + if (core_if->core_params->ahb_single) {
  63673. + ahbcfg.b.ahbsingle = 1;
  63674. + }
  63675. +
  63676. + ahbcfg.b.dmaenable = core_if->dma_enable;
  63677. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  63678. +
  63679. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  63680. +
  63681. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  63682. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  63683. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  63684. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  63685. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  63686. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  63687. +
  63688. + /*
  63689. + * Program the GUSBCFG register.
  63690. + */
  63691. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  63692. +
  63693. + switch (core_if->hwcfg2.b.op_mode) {
  63694. + case DWC_MODE_HNP_SRP_CAPABLE:
  63695. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  63696. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  63697. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  63698. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  63699. + break;
  63700. +
  63701. + case DWC_MODE_SRP_ONLY_CAPABLE:
  63702. + usbcfg.b.hnpcap = 0;
  63703. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  63704. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  63705. + break;
  63706. +
  63707. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  63708. + usbcfg.b.hnpcap = 0;
  63709. + usbcfg.b.srpcap = 0;
  63710. + break;
  63711. +
  63712. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  63713. + usbcfg.b.hnpcap = 0;
  63714. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  63715. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  63716. + break;
  63717. +
  63718. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  63719. + usbcfg.b.hnpcap = 0;
  63720. + usbcfg.b.srpcap = 0;
  63721. + break;
  63722. +
  63723. + case DWC_MODE_SRP_CAPABLE_HOST:
  63724. + usbcfg.b.hnpcap = 0;
  63725. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  63726. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  63727. + break;
  63728. +
  63729. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  63730. + usbcfg.b.hnpcap = 0;
  63731. + usbcfg.b.srpcap = 0;
  63732. + break;
  63733. + }
  63734. +
  63735. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  63736. +
  63737. +#ifdef CONFIG_USB_DWC_OTG_LPM
  63738. + if (core_if->core_params->lpm_enable) {
  63739. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  63740. +
  63741. + /* To enable LPM support set lpm_cap_en bit */
  63742. + lpmcfg.b.lpm_cap_en = 1;
  63743. +
  63744. + /* Make AppL1Res ACK */
  63745. + lpmcfg.b.appl_resp = 1;
  63746. +
  63747. + /* Retry 3 times */
  63748. + lpmcfg.b.retry_count = 3;
  63749. +
  63750. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  63751. + 0, lpmcfg.d32);
  63752. +
  63753. + }
  63754. +#endif
  63755. + if (core_if->core_params->ic_usb_cap) {
  63756. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  63757. + gusbcfg.b.ic_usb_cap = 1;
  63758. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  63759. + 0, gusbcfg.d32);
  63760. + }
  63761. + {
  63762. + gotgctl_data_t gotgctl = {.d32 = 0 };
  63763. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  63764. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  63765. + gotgctl.d32);
  63766. + /* Set OTG version supported */
  63767. + core_if->otg_ver = core_if->core_params->otg_ver;
  63768. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  63769. + core_if->core_params->otg_ver, core_if->otg_ver);
  63770. + }
  63771. +
  63772. +
  63773. + /* Enable common interrupts */
  63774. + dwc_otg_enable_common_interrupts(core_if);
  63775. +
  63776. + /* Do device or host intialization based on mode during PCD
  63777. + * and HCD initialization */
  63778. + if (dwc_otg_is_host_mode(core_if)) {
  63779. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  63780. + core_if->op_state = A_HOST;
  63781. + } else {
  63782. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  63783. + core_if->op_state = B_PERIPHERAL;
  63784. +#ifdef DWC_DEVICE_ONLY
  63785. + dwc_otg_core_dev_init(core_if);
  63786. +#endif
  63787. + }
  63788. +}
  63789. +
  63790. +/**
  63791. + * This function enables the Device mode interrupts.
  63792. + *
  63793. + * @param core_if Programming view of DWC_otg controller
  63794. + */
  63795. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  63796. +{
  63797. + gintmsk_data_t intr_mask = {.d32 = 0 };
  63798. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63799. +
  63800. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  63801. +
  63802. + /* Disable all interrupts. */
  63803. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  63804. +
  63805. + /* Clear any pending interrupts */
  63806. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  63807. +
  63808. + /* Enable the common interrupts */
  63809. + dwc_otg_enable_common_interrupts(core_if);
  63810. +
  63811. + /* Enable interrupts */
  63812. + intr_mask.b.usbreset = 1;
  63813. + intr_mask.b.enumdone = 1;
  63814. + /* Disable Disconnect interrupt in Device mode */
  63815. + intr_mask.b.disconnect = 0;
  63816. +
  63817. + if (!core_if->multiproc_int_enable) {
  63818. + intr_mask.b.inepintr = 1;
  63819. + intr_mask.b.outepintr = 1;
  63820. + }
  63821. +
  63822. + intr_mask.b.erlysuspend = 1;
  63823. +
  63824. + if (core_if->en_multiple_tx_fifo == 0) {
  63825. + intr_mask.b.epmismatch = 1;
  63826. + }
  63827. +
  63828. + //intr_mask.b.incomplisoout = 1;
  63829. + intr_mask.b.incomplisoin = 1;
  63830. +
  63831. +/* Enable the ignore frame number for ISOC xfers - MAS */
  63832. +/* Disable to support high bandwith ISOC transfers - manukz */
  63833. +#if 0
  63834. +#ifdef DWC_UTE_PER_IO
  63835. + if (core_if->dma_enable) {
  63836. + if (core_if->dma_desc_enable) {
  63837. + dctl_data_t dctl1 = {.d32 = 0 };
  63838. + dctl1.b.ifrmnum = 1;
  63839. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  63840. + dctl, 0, dctl1.d32);
  63841. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  63842. + DWC_READ_REG32(&core_if->dev_if->
  63843. + dev_global_regs->dctl));
  63844. + }
  63845. + }
  63846. +#endif
  63847. +#endif
  63848. +#ifdef DWC_EN_ISOC
  63849. + if (core_if->dma_enable) {
  63850. + if (core_if->dma_desc_enable == 0) {
  63851. + if (core_if->pti_enh_enable) {
  63852. + dctl_data_t dctl = {.d32 = 0 };
  63853. + dctl.b.ifrmnum = 1;
  63854. + DWC_MODIFY_REG32(&core_if->
  63855. + dev_if->dev_global_regs->dctl,
  63856. + 0, dctl.d32);
  63857. + } else {
  63858. + intr_mask.b.incomplisoin = 1;
  63859. + intr_mask.b.incomplisoout = 1;
  63860. + }
  63861. + }
  63862. + } else {
  63863. + intr_mask.b.incomplisoin = 1;
  63864. + intr_mask.b.incomplisoout = 1;
  63865. + }
  63866. +#endif /* DWC_EN_ISOC */
  63867. +
  63868. + /** @todo NGS: Should this be a module parameter? */
  63869. +#ifdef USE_PERIODIC_EP
  63870. + intr_mask.b.isooutdrop = 1;
  63871. + intr_mask.b.eopframe = 1;
  63872. + intr_mask.b.incomplisoin = 1;
  63873. + intr_mask.b.incomplisoout = 1;
  63874. +#endif
  63875. +
  63876. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  63877. +
  63878. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  63879. + DWC_READ_REG32(&global_regs->gintmsk));
  63880. +}
  63881. +
  63882. +/**
  63883. + * This function initializes the DWC_otg controller registers for
  63884. + * device mode.
  63885. + *
  63886. + * @param core_if Programming view of DWC_otg controller
  63887. + *
  63888. + */
  63889. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  63890. +{
  63891. + int i;
  63892. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  63893. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  63894. + dwc_otg_core_params_t *params = core_if->core_params;
  63895. + dcfg_data_t dcfg = {.d32 = 0 };
  63896. + depctl_data_t diepctl = {.d32 = 0 };
  63897. + grstctl_t resetctl = {.d32 = 0 };
  63898. + uint32_t rx_fifo_size;
  63899. + fifosize_data_t nptxfifosize;
  63900. + fifosize_data_t txfifosize;
  63901. + dthrctl_data_t dthrctl;
  63902. + fifosize_data_t ptxfifosize;
  63903. + uint16_t rxfsiz, nptxfsiz;
  63904. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  63905. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  63906. +
  63907. + /* Restart the Phy Clock */
  63908. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  63909. +
  63910. + /* Device configuration register */
  63911. + init_devspd(core_if);
  63912. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  63913. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  63914. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  63915. + /* Enable Device OUT NAK in case of DDMA mode*/
  63916. + if (core_if->core_params->dev_out_nak) {
  63917. + dcfg.b.endevoutnak = 1;
  63918. + }
  63919. +
  63920. + if (core_if->core_params->cont_on_bna) {
  63921. + dctl_data_t dctl = {.d32 = 0 };
  63922. + dctl.b.encontonbna = 1;
  63923. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  63924. + }
  63925. +
  63926. +
  63927. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  63928. +
  63929. + /* Configure data FIFO sizes */
  63930. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  63931. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  63932. + core_if->total_fifo_size);
  63933. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  63934. + params->dev_rx_fifo_size);
  63935. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  63936. + params->dev_nperio_tx_fifo_size);
  63937. +
  63938. + /* Rx FIFO */
  63939. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  63940. + DWC_READ_REG32(&global_regs->grxfsiz));
  63941. +
  63942. +#ifdef DWC_UTE_CFI
  63943. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  63944. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  63945. +#endif
  63946. + rx_fifo_size = params->dev_rx_fifo_size;
  63947. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  63948. +
  63949. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  63950. + DWC_READ_REG32(&global_regs->grxfsiz));
  63951. +
  63952. + /** Set Periodic Tx FIFO Mask all bits 0 */
  63953. + core_if->p_tx_msk = 0;
  63954. +
  63955. + /** Set Tx FIFO Mask all bits 0 */
  63956. + core_if->tx_msk = 0;
  63957. +
  63958. + if (core_if->en_multiple_tx_fifo == 0) {
  63959. + /* Non-periodic Tx FIFO */
  63960. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  63961. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63962. +
  63963. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  63964. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  63965. +
  63966. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  63967. + nptxfifosize.d32);
  63968. +
  63969. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  63970. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  63971. +
  63972. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  63973. + /*
  63974. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  63975. + * Indexes of the FIFO size module parameters in the
  63976. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  63977. + * the dptxfsiz array run from 0 to 14.
  63978. + */
  63979. + /** @todo Finish debug of this */
  63980. + ptxfifosize.b.startaddr =
  63981. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  63982. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  63983. + ptxfifosize.b.depth =
  63984. + params->dev_perio_tx_fifo_size[i];
  63985. + DWC_DEBUGPL(DBG_CIL,
  63986. + "initial dtxfsiz[%d]=%08x\n", i,
  63987. + DWC_READ_REG32(&global_regs->dtxfsiz
  63988. + [i]));
  63989. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  63990. + ptxfifosize.d32);
  63991. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  63992. + i,
  63993. + DWC_READ_REG32(&global_regs->dtxfsiz
  63994. + [i]));
  63995. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  63996. + }
  63997. + } else {
  63998. + /*
  63999. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  64000. + * Indexes of the FIFO size module parameters in the
  64001. + * dev_tx_fifo_size array and the FIFO size registers in
  64002. + * the dtxfsiz array run from 0 to 14.
  64003. + */
  64004. +
  64005. + /* Non-periodic Tx FIFO */
  64006. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  64007. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  64008. +
  64009. +#ifdef DWC_UTE_CFI
  64010. + core_if->pwron_gnptxfsiz =
  64011. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  64012. + core_if->init_gnptxfsiz =
  64013. + params->dev_nperio_tx_fifo_size;
  64014. +#endif
  64015. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  64016. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  64017. +
  64018. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  64019. + nptxfifosize.d32);
  64020. +
  64021. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  64022. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  64023. +
  64024. + txfifosize.b.startaddr =
  64025. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  64026. +
  64027. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  64028. +
  64029. + txfifosize.b.depth =
  64030. + params->dev_tx_fifo_size[i];
  64031. +
  64032. + DWC_DEBUGPL(DBG_CIL,
  64033. + "initial dtxfsiz[%d]=%08x\n",
  64034. + i,
  64035. + DWC_READ_REG32(&global_regs->dtxfsiz
  64036. + [i]));
  64037. +
  64038. +#ifdef DWC_UTE_CFI
  64039. + core_if->pwron_txfsiz[i] =
  64040. + (DWC_READ_REG32
  64041. + (&global_regs->dtxfsiz[i]) >> 16);
  64042. + core_if->init_txfsiz[i] =
  64043. + params->dev_tx_fifo_size[i];
  64044. +#endif
  64045. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  64046. + txfifosize.d32);
  64047. +
  64048. + DWC_DEBUGPL(DBG_CIL,
  64049. + "new dtxfsiz[%d]=%08x\n",
  64050. + i,
  64051. + DWC_READ_REG32(&global_regs->dtxfsiz
  64052. + [i]));
  64053. +
  64054. + txfifosize.b.startaddr += txfifosize.b.depth;
  64055. + }
  64056. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  64057. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  64058. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  64059. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  64060. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  64061. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  64062. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  64063. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  64064. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  64065. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  64066. + }
  64067. + }
  64068. +
  64069. + /* Flush the FIFOs */
  64070. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  64071. + dwc_otg_flush_rx_fifo(core_if);
  64072. +
  64073. + /* Flush the Learning Queue. */
  64074. + resetctl.b.intknqflsh = 1;
  64075. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  64076. +
  64077. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  64078. + core_if->start_predict = 0;
  64079. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  64080. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  64081. + }
  64082. + core_if->nextep_seq[0] = 0;
  64083. + core_if->first_in_nextep_seq = 0;
  64084. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  64085. + diepctl.b.nextep = 0;
  64086. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  64087. +
  64088. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  64089. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  64090. + dcfg.b.epmscnt = 2;
  64091. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  64092. +
  64093. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  64094. + __func__, core_if->first_in_nextep_seq);
  64095. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  64096. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  64097. + }
  64098. + DWC_DEBUGPL(DBG_CILV,"\n");
  64099. + }
  64100. +
  64101. + /* Clear all pending Device Interrupts */
  64102. + /** @todo - if the condition needed to be checked
  64103. + * or in any case all pending interrutps should be cleared?
  64104. + */
  64105. + if (core_if->multiproc_int_enable) {
  64106. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  64107. + DWC_WRITE_REG32(&dev_if->
  64108. + dev_global_regs->diepeachintmsk[i], 0);
  64109. + }
  64110. + }
  64111. +
  64112. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  64113. + DWC_WRITE_REG32(&dev_if->
  64114. + dev_global_regs->doepeachintmsk[i], 0);
  64115. + }
  64116. +
  64117. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  64118. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  64119. + } else {
  64120. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  64121. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  64122. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  64123. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  64124. + }
  64125. +
  64126. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  64127. + depctl_data_t depctl;
  64128. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  64129. + if (depctl.b.epena) {
  64130. + depctl.d32 = 0;
  64131. + depctl.b.epdis = 1;
  64132. + depctl.b.snak = 1;
  64133. + } else {
  64134. + depctl.d32 = 0;
  64135. + }
  64136. +
  64137. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  64138. +
  64139. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  64140. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  64141. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  64142. + }
  64143. +
  64144. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  64145. + depctl_data_t depctl;
  64146. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  64147. + if (depctl.b.epena) {
  64148. + dctl_data_t dctl = {.d32 = 0 };
  64149. + gintmsk_data_t gintsts = {.d32 = 0 };
  64150. + doepint_data_t doepint = {.d32 = 0 };
  64151. + dctl.b.sgoutnak = 1;
  64152. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  64153. + do {
  64154. + dwc_udelay(10);
  64155. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  64156. + } while (!gintsts.b.goutnakeff);
  64157. + gintsts.d32 = 0;
  64158. + gintsts.b.goutnakeff = 1;
  64159. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  64160. +
  64161. + depctl.d32 = 0;
  64162. + depctl.b.epdis = 1;
  64163. + depctl.b.snak = 1;
  64164. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  64165. + do {
  64166. + dwc_udelay(10);
  64167. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  64168. + out_ep_regs[i]->doepint);
  64169. + } while (!doepint.b.epdisabled);
  64170. +
  64171. + doepint.b.epdisabled = 1;
  64172. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  64173. +
  64174. + dctl.d32 = 0;
  64175. + dctl.b.cgoutnak = 1;
  64176. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  64177. + } else {
  64178. + depctl.d32 = 0;
  64179. + }
  64180. +
  64181. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  64182. +
  64183. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  64184. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  64185. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  64186. + }
  64187. +
  64188. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  64189. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  64190. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  64191. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  64192. +
  64193. + dev_if->rx_thr_length = params->rx_thr_length;
  64194. + dev_if->tx_thr_length = params->tx_thr_length;
  64195. +
  64196. + dev_if->setup_desc_index = 0;
  64197. +
  64198. + dthrctl.d32 = 0;
  64199. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  64200. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  64201. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  64202. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  64203. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  64204. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  64205. +
  64206. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  64207. + dthrctl.d32);
  64208. +
  64209. + DWC_DEBUGPL(DBG_CIL,
  64210. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  64211. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  64212. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  64213. + dthrctl.b.rx_thr_len);
  64214. +
  64215. + }
  64216. +
  64217. + dwc_otg_enable_device_interrupts(core_if);
  64218. +
  64219. + {
  64220. + diepmsk_data_t msk = {.d32 = 0 };
  64221. + msk.b.txfifoundrn = 1;
  64222. + if (core_if->multiproc_int_enable) {
  64223. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  64224. + diepeachintmsk[0], msk.d32, msk.d32);
  64225. + } else {
  64226. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  64227. + msk.d32, msk.d32);
  64228. + }
  64229. + }
  64230. +
  64231. + if (core_if->multiproc_int_enable) {
  64232. + /* Set NAK on Babble */
  64233. + dctl_data_t dctl = {.d32 = 0 };
  64234. + dctl.b.nakonbble = 1;
  64235. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  64236. + }
  64237. +
  64238. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  64239. + dctl_data_t dctl = {.d32 = 0 };
  64240. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  64241. + dctl.b.sftdiscon = 0;
  64242. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  64243. + }
  64244. +}
  64245. +
  64246. +/**
  64247. + * This function enables the Host mode interrupts.
  64248. + *
  64249. + * @param core_if Programming view of DWC_otg controller
  64250. + */
  64251. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  64252. +{
  64253. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  64254. + gintmsk_data_t intr_mask = {.d32 = 0 };
  64255. +
  64256. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  64257. +
  64258. + /* Disable all interrupts. */
  64259. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  64260. +
  64261. + /* Clear any pending interrupts. */
  64262. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  64263. +
  64264. + /* Enable the common interrupts */
  64265. + dwc_otg_enable_common_interrupts(core_if);
  64266. +
  64267. + /*
  64268. + * Enable host mode interrupts without disturbing common
  64269. + * interrupts.
  64270. + */
  64271. +
  64272. + intr_mask.b.disconnect = 1;
  64273. + intr_mask.b.portintr = 1;
  64274. + intr_mask.b.hcintr = 1;
  64275. +
  64276. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  64277. +}
  64278. +
  64279. +/**
  64280. + * This function disables the Host Mode interrupts.
  64281. + *
  64282. + * @param core_if Programming view of DWC_otg controller
  64283. + */
  64284. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  64285. +{
  64286. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  64287. + gintmsk_data_t intr_mask = {.d32 = 0 };
  64288. +
  64289. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  64290. +
  64291. + /*
  64292. + * Disable host mode interrupts without disturbing common
  64293. + * interrupts.
  64294. + */
  64295. + intr_mask.b.sofintr = 1;
  64296. + intr_mask.b.portintr = 1;
  64297. + intr_mask.b.hcintr = 1;
  64298. + intr_mask.b.ptxfempty = 1;
  64299. + intr_mask.b.nptxfempty = 1;
  64300. +
  64301. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  64302. +}
  64303. +
  64304. +/**
  64305. + * This function initializes the DWC_otg controller registers for
  64306. + * host mode.
  64307. + *
  64308. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  64309. + * request queues. Host channels are reset to ensure that they are ready for
  64310. + * performing transfers.
  64311. + *
  64312. + * @param core_if Programming view of DWC_otg controller
  64313. + *
  64314. + */
  64315. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  64316. +{
  64317. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  64318. + dwc_otg_host_if_t *host_if = core_if->host_if;
  64319. + dwc_otg_core_params_t *params = core_if->core_params;
  64320. + hprt0_data_t hprt0 = {.d32 = 0 };
  64321. + fifosize_data_t nptxfifosize;
  64322. + fifosize_data_t ptxfifosize;
  64323. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  64324. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  64325. + int i;
  64326. + hcchar_data_t hcchar;
  64327. + hcfg_data_t hcfg;
  64328. + hfir_data_t hfir;
  64329. + dwc_otg_hc_regs_t *hc_regs;
  64330. + int num_channels;
  64331. + gotgctl_data_t gotgctl = {.d32 = 0 };
  64332. +
  64333. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  64334. +
  64335. + /* Restart the Phy Clock */
  64336. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  64337. +
  64338. + /* Initialize Host Configuration Register */
  64339. + init_fslspclksel(core_if);
  64340. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  64341. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  64342. + hcfg.b.fslssupp = 1;
  64343. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  64344. +
  64345. + }
  64346. +
  64347. + /* This bit allows dynamic reloading of the HFIR register
  64348. + * during runtime. This bit needs to be programmed during
  64349. + * initial configuration and its value must not be changed
  64350. + * during runtime.*/
  64351. + if (core_if->core_params->reload_ctl == 1) {
  64352. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  64353. + hfir.b.hfirrldctrl = 1;
  64354. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  64355. + }
  64356. +
  64357. + if (core_if->core_params->dma_desc_enable) {
  64358. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  64359. + if (!
  64360. + (core_if->hwcfg4.b.desc_dma
  64361. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  64362. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  64363. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  64364. + || (op_mode ==
  64365. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  64366. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  64367. + || (op_mode ==
  64368. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  64369. +
  64370. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  64371. + "Either core version is below 2.90a or "
  64372. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  64373. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  64374. + "module parameter to 0.\n");
  64375. + return;
  64376. + }
  64377. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  64378. + hcfg.b.descdma = 1;
  64379. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  64380. + }
  64381. +
  64382. + /* Configure data FIFO sizes */
  64383. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  64384. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  64385. + core_if->total_fifo_size);
  64386. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  64387. + params->host_rx_fifo_size);
  64388. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  64389. + params->host_nperio_tx_fifo_size);
  64390. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  64391. + params->host_perio_tx_fifo_size);
  64392. +
  64393. + /* Rx FIFO */
  64394. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  64395. + DWC_READ_REG32(&global_regs->grxfsiz));
  64396. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  64397. + params->host_rx_fifo_size);
  64398. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  64399. + DWC_READ_REG32(&global_regs->grxfsiz));
  64400. +
  64401. + /* Non-periodic Tx FIFO */
  64402. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  64403. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  64404. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  64405. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  64406. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  64407. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  64408. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  64409. +
  64410. + /* Periodic Tx FIFO */
  64411. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  64412. + DWC_READ_REG32(&global_regs->hptxfsiz));
  64413. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  64414. + ptxfifosize.b.startaddr =
  64415. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  64416. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  64417. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  64418. + DWC_READ_REG32(&global_regs->hptxfsiz));
  64419. +
  64420. + if (core_if->en_multiple_tx_fifo
  64421. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  64422. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  64423. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  64424. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  64425. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  64426. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  64427. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  64428. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  64429. + }
  64430. + }
  64431. +
  64432. + /* TODO - check this */
  64433. + /* Clear Host Set HNP Enable in the OTG Control Register */
  64434. + gotgctl.b.hstsethnpen = 1;
  64435. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  64436. + /* Make sure the FIFOs are flushed. */
  64437. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  64438. + dwc_otg_flush_rx_fifo(core_if);
  64439. +
  64440. + /* Clear Host Set HNP Enable in the OTG Control Register */
  64441. + gotgctl.b.hstsethnpen = 1;
  64442. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  64443. +
  64444. + if (!core_if->core_params->dma_desc_enable) {
  64445. + /* Flush out any leftover queued requests. */
  64446. + num_channels = core_if->core_params->host_channels;
  64447. +
  64448. + for (i = 0; i < num_channels; i++) {
  64449. + hc_regs = core_if->host_if->hc_regs[i];
  64450. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64451. + hcchar.b.chen = 0;
  64452. + hcchar.b.chdis = 1;
  64453. + hcchar.b.epdir = 0;
  64454. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  64455. + }
  64456. +
  64457. + /* Halt all channels to put them into a known state. */
  64458. + for (i = 0; i < num_channels; i++) {
  64459. + int count = 0;
  64460. + hc_regs = core_if->host_if->hc_regs[i];
  64461. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64462. + hcchar.b.chen = 1;
  64463. + hcchar.b.chdis = 1;
  64464. + hcchar.b.epdir = 0;
  64465. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  64466. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  64467. + do {
  64468. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64469. + if (++count > 1000) {
  64470. + DWC_ERROR
  64471. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  64472. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  64473. + break;
  64474. + }
  64475. + dwc_udelay(1);
  64476. + } while (hcchar.b.chen);
  64477. + }
  64478. + }
  64479. +
  64480. + /* Turn on the vbus power. */
  64481. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  64482. + if (core_if->op_state == A_HOST) {
  64483. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  64484. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  64485. + if (hprt0.b.prtpwr == 0) {
  64486. + hprt0.b.prtpwr = 1;
  64487. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  64488. + }
  64489. + }
  64490. +
  64491. + dwc_otg_enable_host_interrupts(core_if);
  64492. +}
  64493. +
  64494. +/**
  64495. + * Prepares a host channel for transferring packets to/from a specific
  64496. + * endpoint. The HCCHARn register is set up with the characteristics specified
  64497. + * in _hc. Host channel interrupts that may need to be serviced while this
  64498. + * transfer is in progress are enabled.
  64499. + *
  64500. + * @param core_if Programming view of DWC_otg controller
  64501. + * @param hc Information needed to initialize the host channel
  64502. + */
  64503. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64504. +{
  64505. + uint32_t intr_enable;
  64506. + hcintmsk_data_t hc_intr_mask;
  64507. + gintmsk_data_t gintmsk = {.d32 = 0 };
  64508. + hcchar_data_t hcchar;
  64509. + hcsplt_data_t hcsplt;
  64510. +
  64511. + uint8_t hc_num = hc->hc_num;
  64512. + dwc_otg_host_if_t *host_if = core_if->host_if;
  64513. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  64514. +
  64515. + /* Clear old interrupt conditions for this host channel. */
  64516. + hc_intr_mask.d32 = 0xFFFFFFFF;
  64517. + hc_intr_mask.b.reserved14_31 = 0;
  64518. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  64519. +
  64520. + /* Enable channel interrupts required for this transfer. */
  64521. + hc_intr_mask.d32 = 0;
  64522. + hc_intr_mask.b.chhltd = 1;
  64523. + if (core_if->dma_enable) {
  64524. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  64525. + if (!core_if->dma_desc_enable)
  64526. + hc_intr_mask.b.ahberr = 1;
  64527. + else {
  64528. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  64529. + hc_intr_mask.b.xfercompl = 1;
  64530. + }
  64531. +
  64532. + if (hc->error_state && !hc->do_split &&
  64533. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  64534. + hc_intr_mask.b.ack = 1;
  64535. + if (hc->ep_is_in) {
  64536. + hc_intr_mask.b.datatglerr = 1;
  64537. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  64538. + hc_intr_mask.b.nak = 1;
  64539. + }
  64540. + }
  64541. + }
  64542. + } else {
  64543. + switch (hc->ep_type) {
  64544. + case DWC_OTG_EP_TYPE_CONTROL:
  64545. + case DWC_OTG_EP_TYPE_BULK:
  64546. + hc_intr_mask.b.xfercompl = 1;
  64547. + hc_intr_mask.b.stall = 1;
  64548. + hc_intr_mask.b.xacterr = 1;
  64549. + hc_intr_mask.b.datatglerr = 1;
  64550. + if (hc->ep_is_in) {
  64551. + hc_intr_mask.b.bblerr = 1;
  64552. + } else {
  64553. + hc_intr_mask.b.nak = 1;
  64554. + hc_intr_mask.b.nyet = 1;
  64555. + if (hc->do_ping) {
  64556. + hc_intr_mask.b.ack = 1;
  64557. + }
  64558. + }
  64559. +
  64560. + if (hc->do_split) {
  64561. + hc_intr_mask.b.nak = 1;
  64562. + if (hc->complete_split) {
  64563. + hc_intr_mask.b.nyet = 1;
  64564. + } else {
  64565. + hc_intr_mask.b.ack = 1;
  64566. + }
  64567. + }
  64568. +
  64569. + if (hc->error_state) {
  64570. + hc_intr_mask.b.ack = 1;
  64571. + }
  64572. + break;
  64573. + case DWC_OTG_EP_TYPE_INTR:
  64574. + hc_intr_mask.b.xfercompl = 1;
  64575. + hc_intr_mask.b.nak = 1;
  64576. + hc_intr_mask.b.stall = 1;
  64577. + hc_intr_mask.b.xacterr = 1;
  64578. + hc_intr_mask.b.datatglerr = 1;
  64579. + hc_intr_mask.b.frmovrun = 1;
  64580. +
  64581. + if (hc->ep_is_in) {
  64582. + hc_intr_mask.b.bblerr = 1;
  64583. + }
  64584. + if (hc->error_state) {
  64585. + hc_intr_mask.b.ack = 1;
  64586. + }
  64587. + if (hc->do_split) {
  64588. + if (hc->complete_split) {
  64589. + hc_intr_mask.b.nyet = 1;
  64590. + } else {
  64591. + hc_intr_mask.b.ack = 1;
  64592. + }
  64593. + }
  64594. + break;
  64595. + case DWC_OTG_EP_TYPE_ISOC:
  64596. + hc_intr_mask.b.xfercompl = 1;
  64597. + hc_intr_mask.b.frmovrun = 1;
  64598. + hc_intr_mask.b.ack = 1;
  64599. +
  64600. + if (hc->ep_is_in) {
  64601. + hc_intr_mask.b.xacterr = 1;
  64602. + hc_intr_mask.b.bblerr = 1;
  64603. + }
  64604. + break;
  64605. + }
  64606. + }
  64607. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  64608. +
  64609. + /* Enable the top level host channel interrupt. */
  64610. + intr_enable = (1 << hc_num);
  64611. + DWC_MODIFY_REG32(&host_if->host_global_regs->haintmsk, 0, intr_enable);
  64612. +
  64613. + /* Make sure host channel interrupts are enabled. */
  64614. + gintmsk.b.hcintr = 1;
  64615. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  64616. +
  64617. + /*
  64618. + * Program the HCCHARn register with the endpoint characteristics for
  64619. + * the current transfer.
  64620. + */
  64621. + hcchar.d32 = 0;
  64622. + hcchar.b.devaddr = hc->dev_addr;
  64623. + hcchar.b.epnum = hc->ep_num;
  64624. + hcchar.b.epdir = hc->ep_is_in;
  64625. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  64626. + hcchar.b.eptype = hc->ep_type;
  64627. + hcchar.b.mps = hc->max_packet;
  64628. +
  64629. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  64630. +
  64631. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  64632. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  64633. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  64634. + "Max Pkt %d, Multi Cnt %d\n",
  64635. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  64636. + hcchar.b.mps, hcchar.b.multicnt);
  64637. +
  64638. + /*
  64639. + * Program the HCSPLIT register for SPLITs
  64640. + */
  64641. + hcsplt.d32 = 0;
  64642. + if (hc->do_split) {
  64643. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  64644. + hc->hc_num,
  64645. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  64646. + hcsplt.b.compsplt = hc->complete_split;
  64647. + hcsplt.b.xactpos = hc->xact_pos;
  64648. + hcsplt.b.hubaddr = hc->hub_addr;
  64649. + hcsplt.b.prtaddr = hc->port_addr;
  64650. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  64651. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  64652. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  64653. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  64654. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  64655. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  64656. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  64657. + }
  64658. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  64659. +
  64660. +}
  64661. +
  64662. +/**
  64663. + * Attempts to halt a host channel. This function should only be called in
  64664. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  64665. + * normal circumstances in DMA mode, the controller halts the channel when the
  64666. + * transfer is complete or a condition occurs that requires application
  64667. + * intervention.
  64668. + *
  64669. + * In slave mode, checks for a free request queue entry, then sets the Channel
  64670. + * Enable and Channel Disable bits of the Host Channel Characteristics
  64671. + * register of the specified channel to intiate the halt. If there is no free
  64672. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  64673. + * register to flush requests for this channel. In the latter case, sets a
  64674. + * flag to indicate that the host channel needs to be halted when a request
  64675. + * queue slot is open.
  64676. + *
  64677. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  64678. + * HCCHARn register. The controller ensures there is space in the request
  64679. + * queue before submitting the halt request.
  64680. + *
  64681. + * Some time may elapse before the core flushes any posted requests for this
  64682. + * host channel and halts. The Channel Halted interrupt handler completes the
  64683. + * deactivation of the host channel.
  64684. + *
  64685. + * @param core_if Controller register interface.
  64686. + * @param hc Host channel to halt.
  64687. + * @param halt_status Reason for halting the channel.
  64688. + */
  64689. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  64690. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  64691. +{
  64692. + gnptxsts_data_t nptxsts;
  64693. + hptxsts_data_t hptxsts;
  64694. + hcchar_data_t hcchar;
  64695. + dwc_otg_hc_regs_t *hc_regs;
  64696. + dwc_otg_core_global_regs_t *global_regs;
  64697. + dwc_otg_host_global_regs_t *host_global_regs;
  64698. +
  64699. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  64700. + global_regs = core_if->core_global_regs;
  64701. + host_global_regs = core_if->host_if->host_global_regs;
  64702. +
  64703. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  64704. + "halt_status = %d\n", halt_status);
  64705. +
  64706. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  64707. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  64708. + /*
  64709. + * Disable all channel interrupts except Ch Halted. The QTD
  64710. + * and QH state associated with this transfer has been cleared
  64711. + * (in the case of URB_DEQUEUE), so the channel needs to be
  64712. + * shut down carefully to prevent crashes.
  64713. + */
  64714. + hcintmsk_data_t hcintmsk;
  64715. + hcintmsk.d32 = 0;
  64716. + hcintmsk.b.chhltd = 1;
  64717. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  64718. +
  64719. + /*
  64720. + * Make sure no other interrupts besides halt are currently
  64721. + * pending. Handling another interrupt could cause a crash due
  64722. + * to the QTD and QH state.
  64723. + */
  64724. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  64725. +
  64726. + /*
  64727. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  64728. + * even if the channel was already halted for some other
  64729. + * reason.
  64730. + */
  64731. + hc->halt_status = halt_status;
  64732. +
  64733. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64734. + if (hcchar.b.chen == 0) {
  64735. + /*
  64736. + * The channel is either already halted or it hasn't
  64737. + * started yet. In DMA mode, the transfer may halt if
  64738. + * it finishes normally or a condition occurs that
  64739. + * requires driver intervention. Don't want to halt
  64740. + * the channel again. In either Slave or DMA mode,
  64741. + * it's possible that the transfer has been assigned
  64742. + * to a channel, but not started yet when an URB is
  64743. + * dequeued. Don't want to halt a channel that hasn't
  64744. + * started yet.
  64745. + */
  64746. + return;
  64747. + }
  64748. + }
  64749. + if (hc->halt_pending) {
  64750. + /*
  64751. + * A halt has already been issued for this channel. This might
  64752. + * happen when a transfer is aborted by a higher level in
  64753. + * the stack.
  64754. + */
  64755. +#ifdef DEBUG
  64756. + DWC_PRINTF
  64757. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  64758. + __func__, hc->hc_num);
  64759. +
  64760. +#endif
  64761. + return;
  64762. + }
  64763. +
  64764. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  64765. +
  64766. + /* No need to set the bit in DDMA for disabling the channel */
  64767. + //TODO check it everywhere channel is disabled
  64768. + if (!core_if->core_params->dma_desc_enable)
  64769. + hcchar.b.chen = 1;
  64770. + hcchar.b.chdis = 1;
  64771. +
  64772. + if (!core_if->dma_enable) {
  64773. + /* Check for space in the request queue to issue the halt. */
  64774. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  64775. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  64776. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  64777. + if (nptxsts.b.nptxqspcavail == 0) {
  64778. + hcchar.b.chen = 0;
  64779. + }
  64780. + } else {
  64781. + hptxsts.d32 =
  64782. + DWC_READ_REG32(&host_global_regs->hptxsts);
  64783. + if ((hptxsts.b.ptxqspcavail == 0)
  64784. + || (core_if->queuing_high_bandwidth)) {
  64785. + hcchar.b.chen = 0;
  64786. + }
  64787. + }
  64788. + }
  64789. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  64790. +
  64791. + hc->halt_status = halt_status;
  64792. +
  64793. + if (hcchar.b.chen) {
  64794. + hc->halt_pending = 1;
  64795. + hc->halt_on_queue = 0;
  64796. + } else {
  64797. + hc->halt_on_queue = 1;
  64798. + }
  64799. +
  64800. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  64801. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  64802. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  64803. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  64804. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  64805. +
  64806. + return;
  64807. +}
  64808. +
  64809. +/**
  64810. + * Clears the transfer state for a host channel. This function is normally
  64811. + * called after a transfer is done and the host channel is being released.
  64812. + *
  64813. + * @param core_if Programming view of DWC_otg controller.
  64814. + * @param hc Identifies the host channel to clean up.
  64815. + */
  64816. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64817. +{
  64818. + dwc_otg_hc_regs_t *hc_regs;
  64819. +
  64820. + hc->xfer_started = 0;
  64821. +
  64822. + /*
  64823. + * Clear channel interrupt enables and any unhandled channel interrupt
  64824. + * conditions.
  64825. + */
  64826. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  64827. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  64828. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  64829. +#ifdef DEBUG
  64830. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  64831. +#endif
  64832. +}
  64833. +
  64834. +/**
  64835. + * Sets the channel property that indicates in which frame a periodic transfer
  64836. + * should occur. This is always set to the _next_ frame. This function has no
  64837. + * effect on non-periodic transfers.
  64838. + *
  64839. + * @param core_if Programming view of DWC_otg controller.
  64840. + * @param hc Identifies the host channel to set up and its properties.
  64841. + * @param hcchar Current value of the HCCHAR register for the specified host
  64842. + * channel.
  64843. + */
  64844. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  64845. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  64846. +{
  64847. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  64848. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  64849. + hfnum_data_t hfnum;
  64850. + hfnum.d32 =
  64851. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  64852. +
  64853. + /* 1 if _next_ frame is odd, 0 if it's even */
  64854. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  64855. +#ifdef DEBUG
  64856. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  64857. + && !hc->complete_split) {
  64858. + switch (hfnum.b.frnum & 0x7) {
  64859. + case 7:
  64860. + core_if->hfnum_7_samples++;
  64861. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  64862. + break;
  64863. + case 0:
  64864. + core_if->hfnum_0_samples++;
  64865. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  64866. + break;
  64867. + default:
  64868. + core_if->hfnum_other_samples++;
  64869. + core_if->hfnum_other_frrem_accum +=
  64870. + hfnum.b.frrem;
  64871. + break;
  64872. + }
  64873. + }
  64874. +#endif
  64875. + }
  64876. +}
  64877. +
  64878. +#ifdef DEBUG
  64879. +void hc_xfer_timeout(void *ptr)
  64880. +{
  64881. + hc_xfer_info_t *xfer_info = NULL;
  64882. + int hc_num = 0;
  64883. +
  64884. + if (ptr)
  64885. + xfer_info = (hc_xfer_info_t *) ptr;
  64886. +
  64887. + if (!xfer_info->hc) {
  64888. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  64889. + return;
  64890. + }
  64891. +
  64892. + hc_num = xfer_info->hc->hc_num;
  64893. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  64894. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  64895. + xfer_info->core_if->start_hcchar_val[hc_num]);
  64896. +}
  64897. +#endif
  64898. +
  64899. +void ep_xfer_timeout(void *ptr)
  64900. +{
  64901. + ep_xfer_info_t *xfer_info = NULL;
  64902. + int ep_num = 0;
  64903. + dctl_data_t dctl = {.d32 = 0 };
  64904. + gintsts_data_t gintsts = {.d32 = 0 };
  64905. + gintmsk_data_t gintmsk = {.d32 = 0 };
  64906. +
  64907. + if (ptr)
  64908. + xfer_info = (ep_xfer_info_t *) ptr;
  64909. +
  64910. + if (!xfer_info->ep) {
  64911. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  64912. + return;
  64913. + }
  64914. +
  64915. + ep_num = xfer_info->ep->num;
  64916. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  64917. + /* Put the sate to 2 as it was time outed */
  64918. + xfer_info->state = 2;
  64919. +
  64920. + dctl.d32 =
  64921. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  64922. + gintsts.d32 =
  64923. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  64924. + gintmsk.d32 =
  64925. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  64926. +
  64927. + if (!gintmsk.b.goutnakeff) {
  64928. + /* Unmask it */
  64929. + gintmsk.b.goutnakeff = 1;
  64930. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  64931. + gintmsk.d32);
  64932. +
  64933. + }
  64934. +
  64935. + if (!gintsts.b.goutnakeff) {
  64936. + dctl.b.sgoutnak = 1;
  64937. + }
  64938. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  64939. + dctl.d32);
  64940. +
  64941. +}
  64942. +
  64943. +void set_pid_isoc(dwc_hc_t * hc)
  64944. +{
  64945. + /* Set up the initial PID for the transfer. */
  64946. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  64947. + if (hc->ep_is_in) {
  64948. + if (hc->multi_count == 1) {
  64949. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  64950. + } else if (hc->multi_count == 2) {
  64951. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  64952. + } else {
  64953. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  64954. + }
  64955. + } else {
  64956. + if (hc->multi_count == 1) {
  64957. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  64958. + } else {
  64959. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  64960. + }
  64961. + }
  64962. + } else {
  64963. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  64964. + }
  64965. +}
  64966. +
  64967. +/**
  64968. + * This function does the setup for a data transfer for a host channel and
  64969. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  64970. + * Slave mode, the caller must ensure that there is sufficient space in the
  64971. + * request queue and Tx Data FIFO.
  64972. + *
  64973. + * For an OUT transfer in Slave mode, it loads a data packet into the
  64974. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  64975. + * the Host ISR.
  64976. + *
  64977. + * For an IN transfer in Slave mode, a data packet is requested. The data
  64978. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  64979. + * additional data packets are requested in the Host ISR.
  64980. + *
  64981. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  64982. + * register along with a packet count of 1 and the channel is enabled. This
  64983. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  64984. + * simply set to 0 since no data transfer occurs in this case.
  64985. + *
  64986. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  64987. + * all the information required to perform the subsequent data transfer. In
  64988. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  64989. + * controller performs the entire PING protocol, then starts the data
  64990. + * transfer.
  64991. + *
  64992. + * @param core_if Programming view of DWC_otg controller.
  64993. + * @param hc Information needed to initialize the host channel. The xfer_len
  64994. + * value may be reduced to accommodate the max widths of the XferSize and
  64995. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  64996. + * to reflect the final xfer_len value.
  64997. + */
  64998. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  64999. +{
  65000. + hcchar_data_t hcchar;
  65001. + hctsiz_data_t hctsiz;
  65002. + uint16_t num_packets;
  65003. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  65004. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  65005. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  65006. +
  65007. + hctsiz.d32 = 0;
  65008. +
  65009. + if (hc->do_ping) {
  65010. + if (!core_if->dma_enable) {
  65011. + dwc_otg_hc_do_ping(core_if, hc);
  65012. + hc->xfer_started = 1;
  65013. + return;
  65014. + } else {
  65015. + hctsiz.b.dopng = 1;
  65016. + }
  65017. + }
  65018. +
  65019. + if (hc->do_split) {
  65020. + num_packets = 1;
  65021. +
  65022. + if (hc->complete_split && !hc->ep_is_in) {
  65023. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  65024. + * core doesn't expect any data written to the FIFO */
  65025. + hc->xfer_len = 0;
  65026. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  65027. + hc->xfer_len = hc->max_packet;
  65028. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  65029. + hc->xfer_len = 188;
  65030. + }
  65031. +
  65032. + hctsiz.b.xfersize = hc->xfer_len;
  65033. + } else {
  65034. + /*
  65035. + * Ensure that the transfer length and packet count will fit
  65036. + * in the widths allocated for them in the HCTSIZn register.
  65037. + */
  65038. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  65039. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  65040. + /*
  65041. + * Make sure the transfer size is no larger than one
  65042. + * (micro)frame's worth of data. (A check was done
  65043. + * when the periodic transfer was accepted to ensure
  65044. + * that a (micro)frame's worth of data can be
  65045. + * programmed into a channel.)
  65046. + */
  65047. + uint32_t max_periodic_len =
  65048. + hc->multi_count * hc->max_packet;
  65049. + if (hc->xfer_len > max_periodic_len) {
  65050. + hc->xfer_len = max_periodic_len;
  65051. + } else {
  65052. + }
  65053. + } else if (hc->xfer_len > max_hc_xfer_size) {
  65054. + /* Make sure that xfer_len is a multiple of max packet size. */
  65055. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  65056. + }
  65057. +
  65058. + if (hc->xfer_len > 0) {
  65059. + num_packets =
  65060. + (hc->xfer_len + hc->max_packet -
  65061. + 1) / hc->max_packet;
  65062. + if (num_packets > max_hc_pkt_count) {
  65063. + num_packets = max_hc_pkt_count;
  65064. + hc->xfer_len = num_packets * hc->max_packet;
  65065. + }
  65066. + } else {
  65067. + /* Need 1 packet for transfer length of 0. */
  65068. + num_packets = 1;
  65069. + }
  65070. +
  65071. + if (hc->ep_is_in) {
  65072. + /* Always program an integral # of max packets for IN transfers. */
  65073. + hc->xfer_len = num_packets * hc->max_packet;
  65074. + }
  65075. +
  65076. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  65077. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  65078. + /*
  65079. + * Make sure that the multi_count field matches the
  65080. + * actual transfer length.
  65081. + */
  65082. + hc->multi_count = num_packets;
  65083. + }
  65084. +
  65085. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  65086. + set_pid_isoc(hc);
  65087. +
  65088. + hctsiz.b.xfersize = hc->xfer_len;
  65089. + }
  65090. +
  65091. + hc->start_pkt_count = num_packets;
  65092. + hctsiz.b.pktcnt = num_packets;
  65093. + hctsiz.b.pid = hc->data_pid_start;
  65094. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  65095. +
  65096. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  65097. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  65098. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  65099. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  65100. +
  65101. + if (core_if->dma_enable) {
  65102. + dwc_dma_t dma_addr;
  65103. + if (hc->align_buff) {
  65104. + dma_addr = hc->align_buff;
  65105. + } else {
  65106. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  65107. + }
  65108. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  65109. + }
  65110. +
  65111. + /* Start the split */
  65112. + if (hc->do_split) {
  65113. + hcsplt_data_t hcsplt;
  65114. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  65115. + hcsplt.b.spltena = 1;
  65116. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  65117. + }
  65118. +
  65119. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65120. + hcchar.b.multicnt = hc->multi_count;
  65121. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  65122. +#ifdef DEBUG
  65123. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  65124. + if (hcchar.b.chdis) {
  65125. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  65126. + __func__, hc->hc_num, hcchar.d32);
  65127. + }
  65128. +#endif
  65129. +
  65130. + /* Set host channel enable after all other setup is complete. */
  65131. + hcchar.b.chen = 1;
  65132. + hcchar.b.chdis = 0;
  65133. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  65134. +
  65135. + hc->xfer_started = 1;
  65136. + hc->requests++;
  65137. +
  65138. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  65139. + /* Load OUT packet into the appropriate Tx FIFO. */
  65140. + dwc_otg_hc_write_packet(core_if, hc);
  65141. + }
  65142. +#ifdef DEBUG
  65143. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  65144. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  65145. + hc->hc_num, core_if);//GRAYG
  65146. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  65147. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  65148. +
  65149. + /* Start a timer for this transfer. */
  65150. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  65151. + }
  65152. +#endif
  65153. +}
  65154. +
  65155. +/**
  65156. + * This function does the setup for a data transfer for a host channel
  65157. + * and starts the transfer in Descriptor DMA mode.
  65158. + *
  65159. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  65160. + * Sets PID and NTD values. For periodic transfers
  65161. + * initializes SCHED_INFO field with micro-frame bitmap.
  65162. + *
  65163. + * Initializes HCDMA register with descriptor list address and CTD value
  65164. + * then starts the transfer via enabling the channel.
  65165. + *
  65166. + * @param core_if Programming view of DWC_otg controller.
  65167. + * @param hc Information needed to initialize the host channel.
  65168. + */
  65169. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  65170. +{
  65171. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  65172. + hcchar_data_t hcchar;
  65173. + hctsiz_data_t hctsiz;
  65174. + hcdma_data_t hcdma;
  65175. +
  65176. + hctsiz.d32 = 0;
  65177. +
  65178. + if (hc->do_ping)
  65179. + hctsiz.b_ddma.dopng = 1;
  65180. +
  65181. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  65182. + set_pid_isoc(hc);
  65183. +
  65184. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  65185. + hctsiz.b_ddma.pid = hc->data_pid_start;
  65186. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  65187. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  65188. +
  65189. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  65190. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  65191. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  65192. +
  65193. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  65194. +
  65195. + hcdma.d32 = 0;
  65196. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  65197. +
  65198. + /* Always start from first descriptor. */
  65199. + hcdma.b.ctd = 0;
  65200. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  65201. +
  65202. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65203. + hcchar.b.multicnt = hc->multi_count;
  65204. +
  65205. +#ifdef DEBUG
  65206. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  65207. + if (hcchar.b.chdis) {
  65208. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  65209. + __func__, hc->hc_num, hcchar.d32);
  65210. + }
  65211. +#endif
  65212. +
  65213. + /* Set host channel enable after all other setup is complete. */
  65214. + hcchar.b.chen = 1;
  65215. + hcchar.b.chdis = 0;
  65216. +
  65217. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  65218. +
  65219. + hc->xfer_started = 1;
  65220. + hc->requests++;
  65221. +
  65222. +#ifdef DEBUG
  65223. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  65224. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  65225. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  65226. + hc->hc_num, core_if);//GRAYG
  65227. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  65228. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  65229. + /* Start a timer for this transfer. */
  65230. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  65231. + }
  65232. +#endif
  65233. +
  65234. +}
  65235. +
  65236. +/**
  65237. + * This function continues a data transfer that was started by previous call
  65238. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  65239. + * sufficient space in the request queue and Tx Data FIFO. This function
  65240. + * should only be called in Slave mode. In DMA mode, the controller acts
  65241. + * autonomously to complete transfers programmed to a host channel.
  65242. + *
  65243. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  65244. + * if there is any data remaining to be queued. For an IN transfer, another
  65245. + * data packet is always requested. For the SETUP phase of a control transfer,
  65246. + * this function does nothing.
  65247. + *
  65248. + * @return 1 if a new request is queued, 0 if no more requests are required
  65249. + * for this transfer.
  65250. + */
  65251. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  65252. +{
  65253. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  65254. +
  65255. + if (hc->do_split) {
  65256. + /* SPLITs always queue just once per channel */
  65257. + return 0;
  65258. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  65259. + /* SETUPs are queued only once since they can't be NAKed. */
  65260. + return 0;
  65261. + } else if (hc->ep_is_in) {
  65262. + /*
  65263. + * Always queue another request for other IN transfers. If
  65264. + * back-to-back INs are issued and NAKs are received for both,
  65265. + * the driver may still be processing the first NAK when the
  65266. + * second NAK is received. When the interrupt handler clears
  65267. + * the NAK interrupt for the first NAK, the second NAK will
  65268. + * not be seen. So we can't depend on the NAK interrupt
  65269. + * handler to requeue a NAKed request. Instead, IN requests
  65270. + * are issued each time this function is called. When the
  65271. + * transfer completes, the extra requests for the channel will
  65272. + * be flushed.
  65273. + */
  65274. + hcchar_data_t hcchar;
  65275. + dwc_otg_hc_regs_t *hc_regs =
  65276. + core_if->host_if->hc_regs[hc->hc_num];
  65277. +
  65278. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65279. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  65280. + hcchar.b.chen = 1;
  65281. + hcchar.b.chdis = 0;
  65282. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  65283. + hcchar.d32);
  65284. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  65285. + hc->requests++;
  65286. + return 1;
  65287. + } else {
  65288. + /* OUT transfers. */
  65289. + if (hc->xfer_count < hc->xfer_len) {
  65290. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  65291. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  65292. + hcchar_data_t hcchar;
  65293. + dwc_otg_hc_regs_t *hc_regs;
  65294. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  65295. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65296. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  65297. + }
  65298. +
  65299. + /* Load OUT packet into the appropriate Tx FIFO. */
  65300. + dwc_otg_hc_write_packet(core_if, hc);
  65301. + hc->requests++;
  65302. + return 1;
  65303. + } else {
  65304. + return 0;
  65305. + }
  65306. + }
  65307. +}
  65308. +
  65309. +/**
  65310. + * Starts a PING transfer. This function should only be called in Slave mode.
  65311. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  65312. + */
  65313. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  65314. +{
  65315. + hcchar_data_t hcchar;
  65316. + hctsiz_data_t hctsiz;
  65317. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  65318. +
  65319. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  65320. +
  65321. + hctsiz.d32 = 0;
  65322. + hctsiz.b.dopng = 1;
  65323. + hctsiz.b.pktcnt = 1;
  65324. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  65325. +
  65326. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  65327. + hcchar.b.chen = 1;
  65328. + hcchar.b.chdis = 0;
  65329. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  65330. +}
  65331. +
  65332. +/*
  65333. + * This function writes a packet into the Tx FIFO associated with the Host
  65334. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  65335. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  65336. + * periodic Tx FIFO is written. This function should only be called in Slave
  65337. + * mode.
  65338. + *
  65339. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  65340. + * then number of bytes written to the Tx FIFO.
  65341. + */
  65342. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  65343. +{
  65344. + uint32_t i;
  65345. + uint32_t remaining_count;
  65346. + uint32_t byte_count;
  65347. + uint32_t dword_count;
  65348. +
  65349. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  65350. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  65351. +
  65352. + remaining_count = hc->xfer_len - hc->xfer_count;
  65353. + if (remaining_count > hc->max_packet) {
  65354. + byte_count = hc->max_packet;
  65355. + } else {
  65356. + byte_count = remaining_count;
  65357. + }
  65358. +
  65359. + dword_count = (byte_count + 3) / 4;
  65360. +
  65361. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  65362. + /* xfer_buff is DWORD aligned. */
  65363. + for (i = 0; i < dword_count; i++, data_buff++) {
  65364. + DWC_WRITE_REG32(data_fifo, *data_buff);
  65365. + }
  65366. + } else {
  65367. + /* xfer_buff is not DWORD aligned. */
  65368. + for (i = 0; i < dword_count; i++, data_buff++) {
  65369. + uint32_t data;
  65370. + data =
  65371. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  65372. + 16 | data_buff[3] << 24);
  65373. + DWC_WRITE_REG32(data_fifo, data);
  65374. + }
  65375. + }
  65376. +
  65377. + hc->xfer_count += byte_count;
  65378. + hc->xfer_buff += byte_count;
  65379. +}
  65380. +
  65381. +/**
  65382. + * Gets the current USB frame number. This is the frame number from the last
  65383. + * SOF packet.
  65384. + */
  65385. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  65386. +{
  65387. + dsts_data_t dsts;
  65388. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  65389. +
  65390. + /* read current frame/microframe number from DSTS register */
  65391. + return dsts.b.soffn;
  65392. +}
  65393. +
  65394. +/**
  65395. + * Calculates and gets the frame Interval value of HFIR register according PHY
  65396. + * type and speed.The application can modify a value of HFIR register only after
  65397. + * the Port Enable bit of the Host Port Control and Status register
  65398. + * (HPRT.PrtEnaPort) has been set.
  65399. +*/
  65400. +
  65401. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  65402. +{
  65403. + gusbcfg_data_t usbcfg;
  65404. + hwcfg2_data_t hwcfg2;
  65405. + hprt0_data_t hprt0;
  65406. + int clock = 60; // default value
  65407. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  65408. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  65409. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  65410. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  65411. + clock = 60;
  65412. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  65413. + clock = 48;
  65414. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  65415. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  65416. + clock = 30;
  65417. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  65418. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  65419. + clock = 60;
  65420. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  65421. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  65422. + clock = 48;
  65423. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  65424. + clock = 48;
  65425. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  65426. + clock = 48;
  65427. + if (hprt0.b.prtspd == 0)
  65428. + /* High speed case */
  65429. + return 125 * clock;
  65430. + else
  65431. + /* FS/LS case */
  65432. + return 1000 * clock;
  65433. +}
  65434. +
  65435. +/**
  65436. + * This function reads a setup packet from the Rx FIFO into the destination
  65437. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  65438. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  65439. + *
  65440. + * @param core_if Programming view of DWC_otg controller.
  65441. + * @param dest Destination buffer for packet data.
  65442. + */
  65443. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  65444. +{
  65445. + device_grxsts_data_t status;
  65446. + /* Get the 8 bytes of a setup transaction data */
  65447. +
  65448. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  65449. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  65450. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  65451. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  65452. + status.d32 =
  65453. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  65454. + DWC_DEBUGPL(DBG_ANY,
  65455. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  65456. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  65457. + status.b.fn, status.b.fn);
  65458. + }
  65459. +}
  65460. +
  65461. +/**
  65462. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  65463. + * IN for transmitting packets. It is normally called when the
  65464. + * "Enumeration Done" interrupt occurs.
  65465. + *
  65466. + * @param core_if Programming view of DWC_otg controller.
  65467. + * @param ep The EP0 data.
  65468. + */
  65469. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65470. +{
  65471. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  65472. + dsts_data_t dsts;
  65473. + depctl_data_t diepctl;
  65474. + depctl_data_t doepctl;
  65475. + dctl_data_t dctl = {.d32 = 0 };
  65476. +
  65477. + ep->stp_rollover = 0;
  65478. + /* Read the Device Status and Endpoint 0 Control registers */
  65479. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  65480. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  65481. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  65482. +
  65483. + /* Set the MPS of the IN EP based on the enumeration speed */
  65484. + switch (dsts.b.enumspd) {
  65485. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  65486. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  65487. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  65488. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  65489. + break;
  65490. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  65491. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  65492. + break;
  65493. + }
  65494. +
  65495. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  65496. +
  65497. + /* Enable OUT EP for receive */
  65498. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  65499. + doepctl.b.epena = 1;
  65500. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  65501. + }
  65502. +#ifdef VERBOSE
  65503. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  65504. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  65505. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  65506. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  65507. +#endif
  65508. + dctl.b.cgnpinnak = 1;
  65509. +
  65510. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  65511. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  65512. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  65513. +
  65514. +}
  65515. +
  65516. +/**
  65517. + * This function activates an EP. The Device EP control register for
  65518. + * the EP is configured as defined in the ep structure. Note: This
  65519. + * function is not used for EP0.
  65520. + *
  65521. + * @param core_if Programming view of DWC_otg controller.
  65522. + * @param ep The EP to activate.
  65523. + */
  65524. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65525. +{
  65526. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  65527. + depctl_data_t depctl;
  65528. + volatile uint32_t *addr;
  65529. + daint_data_t daintmsk = {.d32 = 0 };
  65530. + dcfg_data_t dcfg;
  65531. + uint8_t i;
  65532. +
  65533. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  65534. + (ep->is_in ? "IN" : "OUT"));
  65535. +
  65536. +#ifdef DWC_UTE_PER_IO
  65537. + ep->xiso_frame_num = 0xFFFFFFFF;
  65538. + ep->xiso_active_xfers = 0;
  65539. + ep->xiso_queued_xfers = 0;
  65540. +#endif
  65541. + /* Read DEPCTLn register */
  65542. + if (ep->is_in == 1) {
  65543. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  65544. + daintmsk.ep.in = 1 << ep->num;
  65545. + } else {
  65546. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  65547. + daintmsk.ep.out = 1 << ep->num;
  65548. + }
  65549. +
  65550. + /* If the EP is already active don't change the EP Control
  65551. + * register. */
  65552. + depctl.d32 = DWC_READ_REG32(addr);
  65553. + if (!depctl.b.usbactep) {
  65554. + depctl.b.mps = ep->maxpacket;
  65555. + depctl.b.eptype = ep->type;
  65556. + depctl.b.txfnum = ep->tx_fifo_num;
  65557. +
  65558. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  65559. + depctl.b.setd0pid = 1; // ???
  65560. + } else {
  65561. + depctl.b.setd0pid = 1;
  65562. + }
  65563. + depctl.b.usbactep = 1;
  65564. +
  65565. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  65566. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  65567. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  65568. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  65569. + break;
  65570. + }
  65571. + core_if->nextep_seq[i] = ep->num;
  65572. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  65573. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  65574. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  65575. + dcfg.b.epmscnt++;
  65576. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  65577. +
  65578. + DWC_DEBUGPL(DBG_PCDV,
  65579. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  65580. + __func__, core_if->first_in_nextep_seq);
  65581. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  65582. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  65583. + core_if->nextep_seq[i]);
  65584. + }
  65585. +
  65586. + }
  65587. +
  65588. +
  65589. + DWC_WRITE_REG32(addr, depctl.d32);
  65590. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  65591. + }
  65592. +
  65593. + /* Enable the Interrupt for this EP */
  65594. + if (core_if->multiproc_int_enable) {
  65595. + if (ep->is_in == 1) {
  65596. + diepmsk_data_t diepmsk = {.d32 = 0 };
  65597. + diepmsk.b.xfercompl = 1;
  65598. + diepmsk.b.timeout = 1;
  65599. + diepmsk.b.epdisabled = 1;
  65600. + diepmsk.b.ahberr = 1;
  65601. + diepmsk.b.intknepmis = 1;
  65602. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  65603. + diepmsk.b.intknepmis = 0;
  65604. + diepmsk.b.txfifoundrn = 1; //?????
  65605. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  65606. + diepmsk.b.nak = 1;
  65607. + }
  65608. +
  65609. +
  65610. +
  65611. +/*
  65612. + if (core_if->dma_desc_enable) {
  65613. + diepmsk.b.bna = 1;
  65614. + }
  65615. +*/
  65616. +/*
  65617. + if (core_if->dma_enable) {
  65618. + doepmsk.b.nak = 1;
  65619. + }
  65620. +*/
  65621. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  65622. + diepeachintmsk[ep->num], diepmsk.d32);
  65623. +
  65624. + } else {
  65625. + doepmsk_data_t doepmsk = {.d32 = 0 };
  65626. + doepmsk.b.xfercompl = 1;
  65627. + doepmsk.b.ahberr = 1;
  65628. + doepmsk.b.epdisabled = 1;
  65629. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  65630. + doepmsk.b.outtknepdis = 1;
  65631. +
  65632. +/*
  65633. +
  65634. + if (core_if->dma_desc_enable) {
  65635. + doepmsk.b.bna = 1;
  65636. + }
  65637. +*/
  65638. +/*
  65639. + doepmsk.b.babble = 1;
  65640. + doepmsk.b.nyet = 1;
  65641. + doepmsk.b.nak = 1;
  65642. +*/
  65643. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  65644. + doepeachintmsk[ep->num], doepmsk.d32);
  65645. + }
  65646. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  65647. + 0, daintmsk.d32);
  65648. + } else {
  65649. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  65650. + if (ep->is_in) {
  65651. + diepmsk_data_t diepmsk = {.d32 = 0 };
  65652. + diepmsk.b.nak = 1;
  65653. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  65654. + } else {
  65655. + doepmsk_data_t doepmsk = {.d32 = 0 };
  65656. + doepmsk.b.outtknepdis = 1;
  65657. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  65658. + }
  65659. + }
  65660. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  65661. + 0, daintmsk.d32);
  65662. + }
  65663. +
  65664. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  65665. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  65666. +
  65667. + ep->stall_clear_flag = 0;
  65668. +
  65669. + return;
  65670. +}
  65671. +
  65672. +/**
  65673. + * This function deactivates an EP. This is done by clearing the USB Active
  65674. + * EP bit in the Device EP control register. Note: This function is not used
  65675. + * for EP0. EP0 cannot be deactivated.
  65676. + *
  65677. + * @param core_if Programming view of DWC_otg controller.
  65678. + * @param ep The EP to deactivate.
  65679. + */
  65680. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65681. +{
  65682. + depctl_data_t depctl = {.d32 = 0 };
  65683. + volatile uint32_t *addr;
  65684. + daint_data_t daintmsk = {.d32 = 0 };
  65685. + dcfg_data_t dcfg;
  65686. + uint8_t i = 0;
  65687. +
  65688. +#ifdef DWC_UTE_PER_IO
  65689. + ep->xiso_frame_num = 0xFFFFFFFF;
  65690. + ep->xiso_active_xfers = 0;
  65691. + ep->xiso_queued_xfers = 0;
  65692. +#endif
  65693. +
  65694. + /* Read DEPCTLn register */
  65695. + if (ep->is_in == 1) {
  65696. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  65697. + daintmsk.ep.in = 1 << ep->num;
  65698. + } else {
  65699. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  65700. + daintmsk.ep.out = 1 << ep->num;
  65701. + }
  65702. +
  65703. + depctl.d32 = DWC_READ_REG32(addr);
  65704. +
  65705. + depctl.b.usbactep = 0;
  65706. +
  65707. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  65708. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  65709. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  65710. + if (core_if->nextep_seq[i] == ep->num)
  65711. + break;
  65712. + }
  65713. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  65714. + if (core_if->first_in_nextep_seq == ep->num)
  65715. + core_if->first_in_nextep_seq = i;
  65716. + core_if->nextep_seq[ep->num] = 0xff;
  65717. + depctl.b.nextep = 0;
  65718. + dcfg.d32 =
  65719. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  65720. + dcfg.b.epmscnt--;
  65721. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  65722. + dcfg.d32);
  65723. +
  65724. + DWC_DEBUGPL(DBG_PCDV,
  65725. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  65726. + __func__, core_if->first_in_nextep_seq);
  65727. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  65728. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  65729. + }
  65730. + }
  65731. +
  65732. + if (ep->is_in == 1)
  65733. + depctl.b.txfnum = 0;
  65734. +
  65735. + if (core_if->dma_desc_enable)
  65736. + depctl.b.epdis = 1;
  65737. +
  65738. + DWC_WRITE_REG32(addr, depctl.d32);
  65739. + depctl.d32 = DWC_READ_REG32(addr);
  65740. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  65741. + && depctl.b.epena) {
  65742. + depctl_data_t depctl = {.d32 = 0};
  65743. + if (ep->is_in) {
  65744. + diepint_data_t diepint = {.d32 = 0};
  65745. +
  65746. + depctl.b.snak = 1;
  65747. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  65748. + diepctl, depctl.d32);
  65749. + do {
  65750. + dwc_udelay(10);
  65751. + diepint.d32 =
  65752. + DWC_READ_REG32(&core_if->
  65753. + dev_if->in_ep_regs[ep->num]->
  65754. + diepint);
  65755. + } while (!diepint.b.inepnakeff);
  65756. + diepint.b.inepnakeff = 1;
  65757. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  65758. + diepint, diepint.d32);
  65759. + depctl.d32 = 0;
  65760. + depctl.b.epdis = 1;
  65761. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  65762. + diepctl, depctl.d32);
  65763. + do {
  65764. + dwc_udelay(10);
  65765. + diepint.d32 =
  65766. + DWC_READ_REG32(&core_if->
  65767. + dev_if->in_ep_regs[ep->num]->
  65768. + diepint);
  65769. + } while (!diepint.b.epdisabled);
  65770. + diepint.b.epdisabled = 1;
  65771. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  65772. + diepint, diepint.d32);
  65773. + } else {
  65774. + dctl_data_t dctl = {.d32 = 0};
  65775. + gintmsk_data_t gintsts = {.d32 = 0};
  65776. + doepint_data_t doepint = {.d32 = 0};
  65777. + dctl.b.sgoutnak = 1;
  65778. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  65779. + dctl, 0, dctl.d32);
  65780. + do {
  65781. + dwc_udelay(10);
  65782. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  65783. + } while (!gintsts.b.goutnakeff);
  65784. + gintsts.d32 = 0;
  65785. + gintsts.b.goutnakeff = 1;
  65786. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  65787. +
  65788. + depctl.d32 = 0;
  65789. + depctl.b.epdis = 1;
  65790. + depctl.b.snak = 1;
  65791. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  65792. + do
  65793. + {
  65794. + dwc_udelay(10);
  65795. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  65796. + out_ep_regs[ep->num]->doepint);
  65797. + } while (!doepint.b.epdisabled);
  65798. +
  65799. + doepint.b.epdisabled = 1;
  65800. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  65801. +
  65802. + dctl.d32 = 0;
  65803. + dctl.b.cgoutnak = 1;
  65804. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  65805. + }
  65806. + }
  65807. +
  65808. + /* Disable the Interrupt for this EP */
  65809. + if (core_if->multiproc_int_enable) {
  65810. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  65811. + daintmsk.d32, 0);
  65812. +
  65813. + if (ep->is_in == 1) {
  65814. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  65815. + diepeachintmsk[ep->num], 0);
  65816. + } else {
  65817. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  65818. + doepeachintmsk[ep->num], 0);
  65819. + }
  65820. + } else {
  65821. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  65822. + daintmsk.d32, 0);
  65823. + }
  65824. +
  65825. +}
  65826. +
  65827. +/**
  65828. + * This function initializes dma descriptor chain.
  65829. + *
  65830. + * @param core_if Programming view of DWC_otg controller.
  65831. + * @param ep The EP to start the transfer on.
  65832. + */
  65833. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65834. +{
  65835. + dwc_otg_dev_dma_desc_t *dma_desc;
  65836. + uint32_t offset;
  65837. + uint32_t xfer_est;
  65838. + int i;
  65839. + unsigned maxxfer_local, total_len;
  65840. +
  65841. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  65842. + (ep->maxpacket%4)) {
  65843. + maxxfer_local = ep->maxpacket;
  65844. + total_len = ep->xfer_len;
  65845. + } else {
  65846. + maxxfer_local = ep->maxxfer;
  65847. + total_len = ep->total_len;
  65848. + }
  65849. +
  65850. + ep->desc_cnt = (total_len / maxxfer_local) +
  65851. + ((total_len % maxxfer_local) ? 1 : 0);
  65852. +
  65853. + if (!ep->desc_cnt)
  65854. + ep->desc_cnt = 1;
  65855. +
  65856. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  65857. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  65858. +
  65859. + dma_desc = ep->desc_addr;
  65860. + if (maxxfer_local == ep->maxpacket) {
  65861. + if ((total_len % maxxfer_local) &&
  65862. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  65863. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  65864. + (total_len % maxxfer_local);
  65865. + } else
  65866. + xfer_est = ep->desc_cnt * maxxfer_local;
  65867. + } else
  65868. + xfer_est = total_len;
  65869. + offset = 0;
  65870. + for (i = 0; i < ep->desc_cnt; ++i) {
  65871. + /** DMA Descriptor Setup */
  65872. + if (xfer_est > maxxfer_local) {
  65873. + dma_desc->status.b.bs = BS_HOST_BUSY;
  65874. + dma_desc->status.b.l = 0;
  65875. + dma_desc->status.b.ioc = 0;
  65876. + dma_desc->status.b.sp = 0;
  65877. + dma_desc->status.b.bytes = maxxfer_local;
  65878. + dma_desc->buf = ep->dma_addr + offset;
  65879. + dma_desc->status.b.sts = 0;
  65880. + dma_desc->status.b.bs = BS_HOST_READY;
  65881. +
  65882. + xfer_est -= maxxfer_local;
  65883. + offset += maxxfer_local;
  65884. + } else {
  65885. + dma_desc->status.b.bs = BS_HOST_BUSY;
  65886. + dma_desc->status.b.l = 1;
  65887. + dma_desc->status.b.ioc = 1;
  65888. + if (ep->is_in) {
  65889. + dma_desc->status.b.sp =
  65890. + (xfer_est %
  65891. + ep->maxpacket) ? 1 : ((ep->
  65892. + sent_zlp) ? 1 : 0);
  65893. + dma_desc->status.b.bytes = xfer_est;
  65894. + } else {
  65895. + if (maxxfer_local == ep->maxpacket)
  65896. + dma_desc->status.b.bytes = xfer_est;
  65897. + else
  65898. + dma_desc->status.b.bytes =
  65899. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  65900. + }
  65901. +
  65902. + dma_desc->buf = ep->dma_addr + offset;
  65903. + dma_desc->status.b.sts = 0;
  65904. + dma_desc->status.b.bs = BS_HOST_READY;
  65905. + }
  65906. + dma_desc++;
  65907. + }
  65908. +}
  65909. +/**
  65910. + * This function is called when to write ISOC data into appropriate dedicated
  65911. + * periodic FIFO.
  65912. + */
  65913. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  65914. +{
  65915. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  65916. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  65917. + dtxfsts_data_t txstatus = {.d32 = 0 };
  65918. + uint32_t len = 0;
  65919. + int epnum = dwc_ep->num;
  65920. + int dwords;
  65921. +
  65922. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  65923. +
  65924. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  65925. +
  65926. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  65927. +
  65928. + if (len > dwc_ep->maxpacket) {
  65929. + len = dwc_ep->maxpacket;
  65930. + }
  65931. +
  65932. + dwords = (len + 3) / 4;
  65933. +
  65934. + /* While there is space in the queue and space in the FIFO and
  65935. + * More data to tranfer, Write packets to the Tx FIFO */
  65936. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  65937. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  65938. +
  65939. + while (txstatus.b.txfspcavail > dwords &&
  65940. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  65941. + /* Write the FIFO */
  65942. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  65943. +
  65944. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  65945. + if (len > dwc_ep->maxpacket) {
  65946. + len = dwc_ep->maxpacket;
  65947. + }
  65948. +
  65949. + dwords = (len + 3) / 4;
  65950. + txstatus.d32 =
  65951. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  65952. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  65953. + txstatus.d32);
  65954. + }
  65955. +
  65956. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  65957. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  65958. +
  65959. + return 1;
  65960. +}
  65961. +/**
  65962. + * This function does the setup for a data transfer for an EP and
  65963. + * starts the transfer. For an IN transfer, the packets will be
  65964. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  65965. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  65966. + *
  65967. + * @param core_if Programming view of DWC_otg controller.
  65968. + * @param ep The EP to start the transfer on.
  65969. + */
  65970. +
  65971. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  65972. +{
  65973. + depctl_data_t depctl;
  65974. + deptsiz_data_t deptsiz;
  65975. + gintmsk_data_t intr_mask = {.d32 = 0 };
  65976. +
  65977. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  65978. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  65979. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  65980. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  65981. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  65982. + ep->total_len);
  65983. + /* IN endpoint */
  65984. + if (ep->is_in == 1) {
  65985. + dwc_otg_dev_in_ep_regs_t *in_regs =
  65986. + core_if->dev_if->in_ep_regs[ep->num];
  65987. +
  65988. + gnptxsts_data_t gtxstatus;
  65989. +
  65990. + gtxstatus.d32 =
  65991. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  65992. +
  65993. + if (core_if->en_multiple_tx_fifo == 0
  65994. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  65995. +#ifdef DEBUG
  65996. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  65997. +#endif
  65998. + return;
  65999. + }
  66000. +
  66001. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  66002. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  66003. +
  66004. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  66005. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  66006. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  66007. + else
  66008. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  66009. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  66010. +
  66011. +
  66012. + /* Zero Length Packet? */
  66013. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  66014. + deptsiz.b.xfersize = 0;
  66015. + deptsiz.b.pktcnt = 1;
  66016. + } else {
  66017. + /* Program the transfer size and packet count
  66018. + * as follows: xfersize = N * maxpacket +
  66019. + * short_packet pktcnt = N + (short_packet
  66020. + * exist ? 1 : 0)
  66021. + */
  66022. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  66023. + deptsiz.b.pktcnt =
  66024. + (ep->xfer_len - ep->xfer_count - 1 +
  66025. + ep->maxpacket) / ep->maxpacket;
  66026. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  66027. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  66028. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  66029. + }
  66030. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  66031. + deptsiz.b.mc = deptsiz.b.pktcnt;
  66032. + }
  66033. +
  66034. + /* Write the DMA register */
  66035. + if (core_if->dma_enable) {
  66036. + if (core_if->dma_desc_enable == 0) {
  66037. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  66038. + deptsiz.b.mc = 1;
  66039. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  66040. + deptsiz.d32);
  66041. + DWC_WRITE_REG32(&(in_regs->diepdma),
  66042. + (uint32_t) ep->dma_addr);
  66043. + } else {
  66044. +#ifdef DWC_UTE_CFI
  66045. + /* The descriptor chain should be already initialized by now */
  66046. + if (ep->buff_mode != BM_STANDARD) {
  66047. + DWC_WRITE_REG32(&in_regs->diepdma,
  66048. + ep->descs_dma_addr);
  66049. + } else {
  66050. +#endif
  66051. + init_dma_desc_chain(core_if, ep);
  66052. + /** DIEPDMAn Register write */
  66053. + DWC_WRITE_REG32(&in_regs->diepdma,
  66054. + ep->dma_desc_addr);
  66055. +#ifdef DWC_UTE_CFI
  66056. + }
  66057. +#endif
  66058. + }
  66059. + } else {
  66060. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  66061. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  66062. + /**
  66063. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  66064. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  66065. + * the data will be written into the fifo by the ISR.
  66066. + */
  66067. + if (core_if->en_multiple_tx_fifo == 0) {
  66068. + intr_mask.b.nptxfempty = 1;
  66069. + DWC_MODIFY_REG32
  66070. + (&core_if->core_global_regs->gintmsk,
  66071. + intr_mask.d32, intr_mask.d32);
  66072. + } else {
  66073. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  66074. + if (ep->xfer_len > 0) {
  66075. + uint32_t fifoemptymsk = 0;
  66076. + fifoemptymsk = 1 << ep->num;
  66077. + DWC_MODIFY_REG32
  66078. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  66079. + 0, fifoemptymsk);
  66080. +
  66081. + }
  66082. + }
  66083. + } else {
  66084. + write_isoc_tx_fifo(core_if, ep);
  66085. + }
  66086. + }
  66087. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  66088. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  66089. +
  66090. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  66091. + dsts_data_t dsts = {.d32 = 0};
  66092. + if (ep->bInterval == 1) {
  66093. + dsts.d32 =
  66094. + DWC_READ_REG32(&core_if->dev_if->
  66095. + dev_global_regs->dsts);
  66096. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  66097. + if (ep->frame_num > 0x3FFF) {
  66098. + ep->frm_overrun = 1;
  66099. + ep->frame_num &= 0x3FFF;
  66100. + } else
  66101. + ep->frm_overrun = 0;
  66102. + if (ep->frame_num & 0x1) {
  66103. + depctl.b.setd1pid = 1;
  66104. + } else {
  66105. + depctl.b.setd0pid = 1;
  66106. + }
  66107. + }
  66108. + }
  66109. + /* EP enable, IN data in FIFO */
  66110. + depctl.b.cnak = 1;
  66111. + depctl.b.epena = 1;
  66112. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  66113. +
  66114. + } else {
  66115. + /* OUT endpoint */
  66116. + dwc_otg_dev_out_ep_regs_t *out_regs =
  66117. + core_if->dev_if->out_ep_regs[ep->num];
  66118. +
  66119. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  66120. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  66121. +
  66122. + if (!core_if->dma_desc_enable) {
  66123. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  66124. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  66125. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  66126. + else
  66127. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  66128. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  66129. + }
  66130. +
  66131. + /* Program the transfer size and packet count as follows:
  66132. + *
  66133. + * pktcnt = N
  66134. + * xfersize = N * maxpacket
  66135. + */
  66136. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  66137. + /* Zero Length Packet */
  66138. + deptsiz.b.xfersize = ep->maxpacket;
  66139. + deptsiz.b.pktcnt = 1;
  66140. + } else {
  66141. + deptsiz.b.pktcnt =
  66142. + (ep->xfer_len - ep->xfer_count +
  66143. + (ep->maxpacket - 1)) / ep->maxpacket;
  66144. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  66145. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  66146. + }
  66147. + if (!core_if->dma_desc_enable) {
  66148. + ep->xfer_len =
  66149. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  66150. + }
  66151. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  66152. + }
  66153. +
  66154. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  66155. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  66156. +
  66157. + if (core_if->dma_enable) {
  66158. + if (!core_if->dma_desc_enable) {
  66159. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  66160. + deptsiz.d32);
  66161. +
  66162. + DWC_WRITE_REG32(&(out_regs->doepdma),
  66163. + (uint32_t) ep->dma_addr);
  66164. + } else {
  66165. +#ifdef DWC_UTE_CFI
  66166. + /* The descriptor chain should be already initialized by now */
  66167. + if (ep->buff_mode != BM_STANDARD) {
  66168. + DWC_WRITE_REG32(&out_regs->doepdma,
  66169. + ep->descs_dma_addr);
  66170. + } else {
  66171. +#endif
  66172. + /** This is used for interrupt out transfers*/
  66173. + if (!ep->xfer_len)
  66174. + ep->xfer_len = ep->total_len;
  66175. + init_dma_desc_chain(core_if, ep);
  66176. +
  66177. + if (core_if->core_params->dev_out_nak) {
  66178. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  66179. + deptsiz.b.pktcnt = (ep->total_len +
  66180. + (ep->maxpacket - 1)) / ep->maxpacket;
  66181. + deptsiz.b.xfersize = ep->total_len;
  66182. + /* Remember initial value of doeptsiz */
  66183. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  66184. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  66185. + deptsiz.d32);
  66186. + }
  66187. + }
  66188. + /** DOEPDMAn Register write */
  66189. + DWC_WRITE_REG32(&out_regs->doepdma,
  66190. + ep->dma_desc_addr);
  66191. +#ifdef DWC_UTE_CFI
  66192. + }
  66193. +#endif
  66194. + }
  66195. + } else {
  66196. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  66197. + }
  66198. +
  66199. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  66200. + dsts_data_t dsts = {.d32 = 0};
  66201. + if (ep->bInterval == 1) {
  66202. + dsts.d32 =
  66203. + DWC_READ_REG32(&core_if->dev_if->
  66204. + dev_global_regs->dsts);
  66205. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  66206. + if (ep->frame_num > 0x3FFF) {
  66207. + ep->frm_overrun = 1;
  66208. + ep->frame_num &= 0x3FFF;
  66209. + } else
  66210. + ep->frm_overrun = 0;
  66211. +
  66212. + if (ep->frame_num & 0x1) {
  66213. + depctl.b.setd1pid = 1;
  66214. + } else {
  66215. + depctl.b.setd0pid = 1;
  66216. + }
  66217. + }
  66218. + }
  66219. +
  66220. + /* EP enable */
  66221. + depctl.b.cnak = 1;
  66222. + depctl.b.epena = 1;
  66223. +
  66224. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  66225. +
  66226. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  66227. + DWC_READ_REG32(&out_regs->doepctl),
  66228. + DWC_READ_REG32(&out_regs->doeptsiz));
  66229. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  66230. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  66231. + daintmsk),
  66232. + DWC_READ_REG32(&core_if->core_global_regs->
  66233. + gintmsk));
  66234. +
  66235. + /* Timer is scheduling only for out bulk transfers for
  66236. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  66237. + * about received data payload in case of timeout
  66238. + */
  66239. + if (core_if->core_params->dev_out_nak) {
  66240. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  66241. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  66242. + core_if->ep_xfer_info[ep->num].ep = ep;
  66243. + core_if->ep_xfer_info[ep->num].state = 1;
  66244. +
  66245. + /* Start a timer for this transfer. */
  66246. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  66247. + }
  66248. + }
  66249. + }
  66250. +}
  66251. +
  66252. +/**
  66253. + * This function setup a zero length transfer in Buffer DMA and
  66254. + * Slave modes for usb requests with zero field set
  66255. + *
  66256. + * @param core_if Programming view of DWC_otg controller.
  66257. + * @param ep The EP to start the transfer on.
  66258. + *
  66259. + */
  66260. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66261. +{
  66262. +
  66263. + depctl_data_t depctl;
  66264. + deptsiz_data_t deptsiz;
  66265. + gintmsk_data_t intr_mask = {.d32 = 0 };
  66266. +
  66267. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  66268. + DWC_PRINTF("zero length transfer is called\n");
  66269. +
  66270. + /* IN endpoint */
  66271. + if (ep->is_in == 1) {
  66272. + dwc_otg_dev_in_ep_regs_t *in_regs =
  66273. + core_if->dev_if->in_ep_regs[ep->num];
  66274. +
  66275. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  66276. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  66277. +
  66278. + deptsiz.b.xfersize = 0;
  66279. + deptsiz.b.pktcnt = 1;
  66280. +
  66281. + /* Write the DMA register */
  66282. + if (core_if->dma_enable) {
  66283. + if (core_if->dma_desc_enable == 0) {
  66284. + deptsiz.b.mc = 1;
  66285. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  66286. + deptsiz.d32);
  66287. + DWC_WRITE_REG32(&(in_regs->diepdma),
  66288. + (uint32_t) ep->dma_addr);
  66289. + }
  66290. + } else {
  66291. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  66292. + /**
  66293. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  66294. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  66295. + * the data will be written into the fifo by the ISR.
  66296. + */
  66297. + if (core_if->en_multiple_tx_fifo == 0) {
  66298. + intr_mask.b.nptxfempty = 1;
  66299. + DWC_MODIFY_REG32(&core_if->
  66300. + core_global_regs->gintmsk,
  66301. + intr_mask.d32, intr_mask.d32);
  66302. + } else {
  66303. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  66304. + if (ep->xfer_len > 0) {
  66305. + uint32_t fifoemptymsk = 0;
  66306. + fifoemptymsk = 1 << ep->num;
  66307. + DWC_MODIFY_REG32(&core_if->
  66308. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  66309. + 0, fifoemptymsk);
  66310. + }
  66311. + }
  66312. + }
  66313. +
  66314. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  66315. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  66316. + /* EP enable, IN data in FIFO */
  66317. + depctl.b.cnak = 1;
  66318. + depctl.b.epena = 1;
  66319. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  66320. +
  66321. + } else {
  66322. + /* OUT endpoint */
  66323. + dwc_otg_dev_out_ep_regs_t *out_regs =
  66324. + core_if->dev_if->out_ep_regs[ep->num];
  66325. +
  66326. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  66327. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  66328. +
  66329. + /* Zero Length Packet */
  66330. + deptsiz.b.xfersize = ep->maxpacket;
  66331. + deptsiz.b.pktcnt = 1;
  66332. +
  66333. + if (core_if->dma_enable) {
  66334. + if (!core_if->dma_desc_enable) {
  66335. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  66336. + deptsiz.d32);
  66337. +
  66338. + DWC_WRITE_REG32(&(out_regs->doepdma),
  66339. + (uint32_t) ep->dma_addr);
  66340. + }
  66341. + } else {
  66342. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  66343. + }
  66344. +
  66345. + /* EP enable */
  66346. + depctl.b.cnak = 1;
  66347. + depctl.b.epena = 1;
  66348. +
  66349. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  66350. +
  66351. + }
  66352. +}
  66353. +
  66354. +/**
  66355. + * This function does the setup for a data transfer for EP0 and starts
  66356. + * the transfer. For an IN transfer, the packets will be loaded into
  66357. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  66358. + * unloaded from the Rx FIFO in the ISR.
  66359. + *
  66360. + * @param core_if Programming view of DWC_otg controller.
  66361. + * @param ep The EP0 data.
  66362. + */
  66363. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66364. +{
  66365. + depctl_data_t depctl;
  66366. + deptsiz0_data_t deptsiz;
  66367. + gintmsk_data_t intr_mask = {.d32 = 0 };
  66368. + dwc_otg_dev_dma_desc_t *dma_desc;
  66369. +
  66370. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  66371. + "xfer_buff=%p start_xfer_buff=%p \n",
  66372. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  66373. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  66374. +
  66375. + ep->total_len = ep->xfer_len;
  66376. +
  66377. + /* IN endpoint */
  66378. + if (ep->is_in == 1) {
  66379. + dwc_otg_dev_in_ep_regs_t *in_regs =
  66380. + core_if->dev_if->in_ep_regs[0];
  66381. +
  66382. + gnptxsts_data_t gtxstatus;
  66383. +
  66384. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  66385. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  66386. + if (depctl.b.epena)
  66387. + return;
  66388. + }
  66389. +
  66390. + gtxstatus.d32 =
  66391. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  66392. +
  66393. + /* If dedicated FIFO every time flush fifo before enable ep*/
  66394. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  66395. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  66396. +
  66397. + if (core_if->en_multiple_tx_fifo == 0
  66398. + && gtxstatus.b.nptxqspcavail == 0
  66399. + && !core_if->dma_enable) {
  66400. +#ifdef DEBUG
  66401. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  66402. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  66403. + DWC_READ_REG32(&in_regs->diepctl));
  66404. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  66405. + deptsiz.d32,
  66406. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  66407. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  66408. + gtxstatus.d32);
  66409. +#endif
  66410. + return;
  66411. + }
  66412. +
  66413. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  66414. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  66415. +
  66416. + /* Zero Length Packet? */
  66417. + if (ep->xfer_len == 0) {
  66418. + deptsiz.b.xfersize = 0;
  66419. + deptsiz.b.pktcnt = 1;
  66420. + } else {
  66421. + /* Program the transfer size and packet count
  66422. + * as follows: xfersize = N * maxpacket +
  66423. + * short_packet pktcnt = N + (short_packet
  66424. + * exist ? 1 : 0)
  66425. + */
  66426. + if (ep->xfer_len > ep->maxpacket) {
  66427. + ep->xfer_len = ep->maxpacket;
  66428. + deptsiz.b.xfersize = ep->maxpacket;
  66429. + } else {
  66430. + deptsiz.b.xfersize = ep->xfer_len;
  66431. + }
  66432. + deptsiz.b.pktcnt = 1;
  66433. +
  66434. + }
  66435. + DWC_DEBUGPL(DBG_PCDV,
  66436. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  66437. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  66438. + deptsiz.d32);
  66439. +
  66440. + /* Write the DMA register */
  66441. + if (core_if->dma_enable) {
  66442. + if (core_if->dma_desc_enable == 0) {
  66443. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  66444. + deptsiz.d32);
  66445. +
  66446. + DWC_WRITE_REG32(&(in_regs->diepdma),
  66447. + (uint32_t) ep->dma_addr);
  66448. + } else {
  66449. + dma_desc = core_if->dev_if->in_desc_addr;
  66450. +
  66451. + /** DMA Descriptor Setup */
  66452. + dma_desc->status.b.bs = BS_HOST_BUSY;
  66453. + dma_desc->status.b.l = 1;
  66454. + dma_desc->status.b.ioc = 1;
  66455. + dma_desc->status.b.sp =
  66456. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  66457. + dma_desc->status.b.bytes = ep->xfer_len;
  66458. + dma_desc->buf = ep->dma_addr;
  66459. + dma_desc->status.b.sts = 0;
  66460. + dma_desc->status.b.bs = BS_HOST_READY;
  66461. +
  66462. + /** DIEPDMA0 Register write */
  66463. + DWC_WRITE_REG32(&in_regs->diepdma,
  66464. + core_if->
  66465. + dev_if->dma_in_desc_addr);
  66466. + }
  66467. + } else {
  66468. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  66469. + }
  66470. +
  66471. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  66472. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  66473. + /* EP enable, IN data in FIFO */
  66474. + depctl.b.cnak = 1;
  66475. + depctl.b.epena = 1;
  66476. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  66477. +
  66478. + /**
  66479. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  66480. + * data will be written into the fifo by the ISR.
  66481. + */
  66482. + if (!core_if->dma_enable) {
  66483. + if (core_if->en_multiple_tx_fifo == 0) {
  66484. + intr_mask.b.nptxfempty = 1;
  66485. + DWC_MODIFY_REG32(&core_if->
  66486. + core_global_regs->gintmsk,
  66487. + intr_mask.d32, intr_mask.d32);
  66488. + } else {
  66489. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  66490. + if (ep->xfer_len > 0) {
  66491. + uint32_t fifoemptymsk = 0;
  66492. + fifoemptymsk |= 1 << ep->num;
  66493. + DWC_MODIFY_REG32(&core_if->
  66494. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  66495. + 0, fifoemptymsk);
  66496. + }
  66497. + }
  66498. + }
  66499. + } else {
  66500. + /* OUT endpoint */
  66501. + dwc_otg_dev_out_ep_regs_t *out_regs =
  66502. + core_if->dev_if->out_ep_regs[0];
  66503. +
  66504. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  66505. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  66506. +
  66507. + /* Program the transfer size and packet count as follows:
  66508. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  66509. + * pktcnt = N */
  66510. + /* Zero Length Packet */
  66511. + deptsiz.b.xfersize = ep->maxpacket;
  66512. + deptsiz.b.pktcnt = 1;
  66513. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  66514. + deptsiz.b.supcnt = 3;
  66515. +
  66516. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  66517. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  66518. +
  66519. + if (core_if->dma_enable) {
  66520. + if (!core_if->dma_desc_enable) {
  66521. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  66522. + deptsiz.d32);
  66523. +
  66524. + DWC_WRITE_REG32(&(out_regs->doepdma),
  66525. + (uint32_t) ep->dma_addr);
  66526. + } else {
  66527. + dma_desc = core_if->dev_if->out_desc_addr;
  66528. +
  66529. + /** DMA Descriptor Setup */
  66530. + dma_desc->status.b.bs = BS_HOST_BUSY;
  66531. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  66532. + dma_desc->status.b.mtrf = 0;
  66533. + dma_desc->status.b.sr = 0;
  66534. + }
  66535. + dma_desc->status.b.l = 1;
  66536. + dma_desc->status.b.ioc = 1;
  66537. + dma_desc->status.b.bytes = ep->maxpacket;
  66538. + dma_desc->buf = ep->dma_addr;
  66539. + dma_desc->status.b.sts = 0;
  66540. + dma_desc->status.b.bs = BS_HOST_READY;
  66541. +
  66542. + /** DOEPDMA0 Register write */
  66543. + DWC_WRITE_REG32(&out_regs->doepdma,
  66544. + core_if->dev_if->
  66545. + dma_out_desc_addr);
  66546. + }
  66547. + } else {
  66548. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  66549. + }
  66550. +
  66551. + /* EP enable */
  66552. + depctl.b.cnak = 1;
  66553. + depctl.b.epena = 1;
  66554. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  66555. + }
  66556. +}
  66557. +
  66558. +/**
  66559. + * This function continues control IN transfers started by
  66560. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  66561. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  66562. + * bit for the packet count.
  66563. + *
  66564. + * @param core_if Programming view of DWC_otg controller.
  66565. + * @param ep The EP0 data.
  66566. + */
  66567. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66568. +{
  66569. + depctl_data_t depctl;
  66570. + deptsiz0_data_t deptsiz;
  66571. + gintmsk_data_t intr_mask = {.d32 = 0 };
  66572. + dwc_otg_dev_dma_desc_t *dma_desc;
  66573. +
  66574. + if (ep->is_in == 1) {
  66575. + dwc_otg_dev_in_ep_regs_t *in_regs =
  66576. + core_if->dev_if->in_ep_regs[0];
  66577. + gnptxsts_data_t tx_status = {.d32 = 0 };
  66578. +
  66579. + tx_status.d32 =
  66580. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  66581. + /** @todo Should there be check for room in the Tx
  66582. + * Status Queue. If not remove the code above this comment. */
  66583. +
  66584. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  66585. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  66586. +
  66587. + /* Program the transfer size and packet count
  66588. + * as follows: xfersize = N * maxpacket +
  66589. + * short_packet pktcnt = N + (short_packet
  66590. + * exist ? 1 : 0)
  66591. + */
  66592. +
  66593. + if (core_if->dma_desc_enable == 0) {
  66594. + deptsiz.b.xfersize =
  66595. + (ep->total_len - ep->xfer_count) >
  66596. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  66597. + ep->xfer_count);
  66598. + deptsiz.b.pktcnt = 1;
  66599. + if (core_if->dma_enable == 0) {
  66600. + ep->xfer_len += deptsiz.b.xfersize;
  66601. + } else {
  66602. + ep->xfer_len = deptsiz.b.xfersize;
  66603. + }
  66604. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  66605. + } else {
  66606. + ep->xfer_len =
  66607. + (ep->total_len - ep->xfer_count) >
  66608. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  66609. + ep->xfer_count);
  66610. +
  66611. + dma_desc = core_if->dev_if->in_desc_addr;
  66612. +
  66613. + /** DMA Descriptor Setup */
  66614. + dma_desc->status.b.bs = BS_HOST_BUSY;
  66615. + dma_desc->status.b.l = 1;
  66616. + dma_desc->status.b.ioc = 1;
  66617. + dma_desc->status.b.sp =
  66618. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  66619. + dma_desc->status.b.bytes = ep->xfer_len;
  66620. + dma_desc->buf = ep->dma_addr;
  66621. + dma_desc->status.b.sts = 0;
  66622. + dma_desc->status.b.bs = BS_HOST_READY;
  66623. +
  66624. + /** DIEPDMA0 Register write */
  66625. + DWC_WRITE_REG32(&in_regs->diepdma,
  66626. + core_if->dev_if->dma_in_desc_addr);
  66627. + }
  66628. +
  66629. + DWC_DEBUGPL(DBG_PCDV,
  66630. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  66631. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  66632. + deptsiz.d32);
  66633. +
  66634. + /* Write the DMA register */
  66635. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  66636. + if (core_if->dma_desc_enable == 0)
  66637. + DWC_WRITE_REG32(&(in_regs->diepdma),
  66638. + (uint32_t) ep->dma_addr);
  66639. + }
  66640. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  66641. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  66642. + /* EP enable, IN data in FIFO */
  66643. + depctl.b.cnak = 1;
  66644. + depctl.b.epena = 1;
  66645. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  66646. +
  66647. + /**
  66648. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  66649. + * data will be written into the fifo by the ISR.
  66650. + */
  66651. + if (!core_if->dma_enable) {
  66652. + if (core_if->en_multiple_tx_fifo == 0) {
  66653. + /* First clear it from GINTSTS */
  66654. + intr_mask.b.nptxfempty = 1;
  66655. + DWC_MODIFY_REG32(&core_if->
  66656. + core_global_regs->gintmsk,
  66657. + intr_mask.d32, intr_mask.d32);
  66658. +
  66659. + } else {
  66660. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  66661. + if (ep->xfer_len > 0) {
  66662. + uint32_t fifoemptymsk = 0;
  66663. + fifoemptymsk |= 1 << ep->num;
  66664. + DWC_MODIFY_REG32(&core_if->
  66665. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  66666. + 0, fifoemptymsk);
  66667. + }
  66668. + }
  66669. + }
  66670. + } else {
  66671. + dwc_otg_dev_out_ep_regs_t *out_regs =
  66672. + core_if->dev_if->out_ep_regs[0];
  66673. +
  66674. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  66675. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  66676. +
  66677. + /* Program the transfer size and packet count
  66678. + * as follows: xfersize = N * maxpacket +
  66679. + * short_packet pktcnt = N + (short_packet
  66680. + * exist ? 1 : 0)
  66681. + */
  66682. + deptsiz.b.xfersize = ep->maxpacket;
  66683. + deptsiz.b.pktcnt = 1;
  66684. +
  66685. + if (core_if->dma_desc_enable == 0) {
  66686. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  66687. + } else {
  66688. + dma_desc = core_if->dev_if->out_desc_addr;
  66689. +
  66690. + /** DMA Descriptor Setup */
  66691. + dma_desc->status.b.bs = BS_HOST_BUSY;
  66692. + dma_desc->status.b.l = 1;
  66693. + dma_desc->status.b.ioc = 1;
  66694. + dma_desc->status.b.bytes = ep->maxpacket;
  66695. + dma_desc->buf = ep->dma_addr;
  66696. + dma_desc->status.b.sts = 0;
  66697. + dma_desc->status.b.bs = BS_HOST_READY;
  66698. +
  66699. + /** DOEPDMA0 Register write */
  66700. + DWC_WRITE_REG32(&out_regs->doepdma,
  66701. + core_if->dev_if->dma_out_desc_addr);
  66702. + }
  66703. +
  66704. + DWC_DEBUGPL(DBG_PCDV,
  66705. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  66706. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  66707. + deptsiz.d32);
  66708. +
  66709. + /* Write the DMA register */
  66710. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  66711. + if (core_if->dma_desc_enable == 0)
  66712. + DWC_WRITE_REG32(&(out_regs->doepdma),
  66713. + (uint32_t) ep->dma_addr);
  66714. +
  66715. + }
  66716. +
  66717. + /* EP enable, IN data in FIFO */
  66718. + depctl.b.cnak = 1;
  66719. + depctl.b.epena = 1;
  66720. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  66721. +
  66722. + }
  66723. +}
  66724. +
  66725. +#ifdef DEBUG
  66726. +void dump_msg(const u8 * buf, unsigned int length)
  66727. +{
  66728. + unsigned int start, num, i;
  66729. + char line[52], *p;
  66730. +
  66731. + if (length >= 512)
  66732. + return;
  66733. + start = 0;
  66734. + while (length > 0) {
  66735. + num = length < 16u ? length : 16u;
  66736. + p = line;
  66737. + for (i = 0; i < num; ++i) {
  66738. + if (i == 8)
  66739. + *p++ = ' ';
  66740. + DWC_SPRINTF(p, " %02x", buf[i]);
  66741. + p += 3;
  66742. + }
  66743. + *p = 0;
  66744. + DWC_PRINTF("%6x: %s\n", start, line);
  66745. + buf += num;
  66746. + start += num;
  66747. + length -= num;
  66748. + }
  66749. +}
  66750. +#else
  66751. +static inline void dump_msg(const u8 * buf, unsigned int length)
  66752. +{
  66753. +}
  66754. +#endif
  66755. +
  66756. +/**
  66757. + * This function writes a packet into the Tx FIFO associated with the
  66758. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  66759. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  66760. + * with all packets for the next micro-frame.
  66761. + *
  66762. + * @param core_if Programming view of DWC_otg controller.
  66763. + * @param ep The EP to write packet for.
  66764. + * @param dma Indicates if DMA is being used.
  66765. + */
  66766. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  66767. + int dma)
  66768. +{
  66769. + /**
  66770. + * The buffer is padded to DWORD on a per packet basis in
  66771. + * slave/dma mode if the MPS is not DWORD aligned. The last
  66772. + * packet, if short, is also padded to a multiple of DWORD.
  66773. + *
  66774. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  66775. + * multiple of DWORD in length
  66776. + *
  66777. + * ep->xfer_len can be any number of bytes
  66778. + *
  66779. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  66780. + * packet
  66781. + *
  66782. + * FIFO access is DWORD */
  66783. +
  66784. + uint32_t i;
  66785. + uint32_t byte_count;
  66786. + uint32_t dword_count;
  66787. + uint32_t *fifo;
  66788. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  66789. +
  66790. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  66791. + ep);
  66792. + if (ep->xfer_count >= ep->xfer_len) {
  66793. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  66794. + return;
  66795. + }
  66796. +
  66797. + /* Find the byte length of the packet either short packet or MPS */
  66798. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  66799. + byte_count = ep->xfer_len - ep->xfer_count;
  66800. + } else {
  66801. + byte_count = ep->maxpacket;
  66802. + }
  66803. +
  66804. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  66805. + * is not a multiple of DWORD */
  66806. + dword_count = (byte_count + 3) / 4;
  66807. +
  66808. +#ifdef VERBOSE
  66809. + dump_msg(ep->xfer_buff, byte_count);
  66810. +#endif
  66811. +
  66812. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  66813. + * intialized? What should this be? */
  66814. +
  66815. + fifo = core_if->data_fifo[ep->num];
  66816. +
  66817. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  66818. + fifo, data_buff, *data_buff, byte_count);
  66819. +
  66820. + if (!dma) {
  66821. + for (i = 0; i < dword_count; i++, data_buff++) {
  66822. + DWC_WRITE_REG32(fifo, *data_buff);
  66823. + }
  66824. + }
  66825. +
  66826. + ep->xfer_count += byte_count;
  66827. + ep->xfer_buff += byte_count;
  66828. + ep->dma_addr += byte_count;
  66829. +}
  66830. +
  66831. +/**
  66832. + * Set the EP STALL.
  66833. + *
  66834. + * @param core_if Programming view of DWC_otg controller.
  66835. + * @param ep The EP to set the stall on.
  66836. + */
  66837. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66838. +{
  66839. + depctl_data_t depctl;
  66840. + volatile uint32_t *depctl_addr;
  66841. +
  66842. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  66843. + (ep->is_in ? "IN" : "OUT"));
  66844. +
  66845. + if (ep->is_in == 1) {
  66846. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  66847. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  66848. +
  66849. + /* set the disable and stall bits */
  66850. + if (depctl.b.epena) {
  66851. + depctl.b.epdis = 1;
  66852. + }
  66853. + depctl.b.stall = 1;
  66854. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  66855. + } else {
  66856. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  66857. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  66858. +
  66859. + /* set the stall bit */
  66860. + depctl.b.stall = 1;
  66861. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  66862. + }
  66863. +
  66864. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  66865. +
  66866. + return;
  66867. +}
  66868. +
  66869. +/**
  66870. + * Clear the EP STALL.
  66871. + *
  66872. + * @param core_if Programming view of DWC_otg controller.
  66873. + * @param ep The EP to clear stall from.
  66874. + */
  66875. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  66876. +{
  66877. + depctl_data_t depctl;
  66878. + volatile uint32_t *depctl_addr;
  66879. +
  66880. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  66881. + (ep->is_in ? "IN" : "OUT"));
  66882. +
  66883. + if (ep->is_in == 1) {
  66884. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  66885. + } else {
  66886. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  66887. + }
  66888. +
  66889. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  66890. +
  66891. + /* clear the stall bits */
  66892. + depctl.b.stall = 0;
  66893. +
  66894. + /*
  66895. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  66896. + * of whether an endpoint has the Halt feature set, a
  66897. + * ClearFeature(ENDPOINT_HALT) request always results in the
  66898. + * data toggle being reinitialized to DATA0.
  66899. + */
  66900. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  66901. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  66902. + depctl.b.setd0pid = 1; /* DATA0 */
  66903. + }
  66904. +
  66905. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  66906. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  66907. + return;
  66908. +}
  66909. +
  66910. +/**
  66911. + * This function reads a packet from the Rx FIFO into the destination
  66912. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  66913. + *
  66914. + * @param core_if Programming view of DWC_otg controller.
  66915. + * @param dest Destination buffer for the packet.
  66916. + * @param bytes Number of bytes to copy to the destination.
  66917. + */
  66918. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  66919. + uint8_t * dest, uint16_t bytes)
  66920. +{
  66921. + int i;
  66922. + int word_count = (bytes + 3) / 4;
  66923. +
  66924. + volatile uint32_t *fifo = core_if->data_fifo[0];
  66925. + uint32_t *data_buff = (uint32_t *) dest;
  66926. +
  66927. + /**
  66928. + * @todo Account for the case where _dest is not dword aligned. This
  66929. + * requires reading data from the FIFO into a uint32_t temp buffer,
  66930. + * then moving it into the data buffer.
  66931. + */
  66932. +
  66933. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  66934. + core_if, dest, bytes);
  66935. +
  66936. + for (i = 0; i < word_count; i++, data_buff++) {
  66937. + *data_buff = DWC_READ_REG32(fifo);
  66938. + }
  66939. +
  66940. + return;
  66941. +}
  66942. +
  66943. +/**
  66944. + * This functions reads the device registers and prints them
  66945. + *
  66946. + * @param core_if Programming view of DWC_otg controller.
  66947. + */
  66948. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  66949. +{
  66950. + int i;
  66951. + volatile uint32_t *addr;
  66952. +
  66953. + DWC_PRINTF("Device Global Registers\n");
  66954. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  66955. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  66956. + (unsigned long)addr, DWC_READ_REG32(addr));
  66957. + addr = &core_if->dev_if->dev_global_regs->dctl;
  66958. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  66959. + (unsigned long)addr, DWC_READ_REG32(addr));
  66960. + addr = &core_if->dev_if->dev_global_regs->dsts;
  66961. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  66962. + (unsigned long)addr, DWC_READ_REG32(addr));
  66963. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  66964. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66965. + DWC_READ_REG32(addr));
  66966. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  66967. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66968. + DWC_READ_REG32(addr));
  66969. + addr = &core_if->dev_if->dev_global_regs->daint;
  66970. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66971. + DWC_READ_REG32(addr));
  66972. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  66973. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66974. + DWC_READ_REG32(addr));
  66975. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  66976. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66977. + DWC_READ_REG32(addr));
  66978. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  66979. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  66980. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  66981. + (unsigned long)addr, DWC_READ_REG32(addr));
  66982. + }
  66983. +
  66984. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  66985. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  66986. + DWC_READ_REG32(addr));
  66987. +
  66988. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  66989. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  66990. + (unsigned long)addr, DWC_READ_REG32(addr));
  66991. +
  66992. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  66993. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  66994. + (unsigned long)addr, DWC_READ_REG32(addr));
  66995. +
  66996. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  66997. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  66998. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  66999. + (unsigned long)addr, DWC_READ_REG32(addr));
  67000. + }
  67001. +
  67002. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  67003. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67004. + DWC_READ_REG32(addr));
  67005. +
  67006. + if (core_if->hwcfg2.b.multi_proc_int) {
  67007. +
  67008. + addr = &core_if->dev_if->dev_global_regs->deachint;
  67009. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  67010. + (unsigned long)addr, DWC_READ_REG32(addr));
  67011. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  67012. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  67013. + (unsigned long)addr, DWC_READ_REG32(addr));
  67014. +
  67015. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  67016. + addr =
  67017. + &core_if->dev_if->
  67018. + dev_global_regs->diepeachintmsk[i];
  67019. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  67020. + i, (unsigned long)addr,
  67021. + DWC_READ_REG32(addr));
  67022. + }
  67023. +
  67024. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  67025. + addr =
  67026. + &core_if->dev_if->
  67027. + dev_global_regs->doepeachintmsk[i];
  67028. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  67029. + i, (unsigned long)addr,
  67030. + DWC_READ_REG32(addr));
  67031. + }
  67032. + }
  67033. +
  67034. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  67035. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  67036. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  67037. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  67038. + (unsigned long)addr, DWC_READ_REG32(addr));
  67039. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  67040. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  67041. + (unsigned long)addr, DWC_READ_REG32(addr));
  67042. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  67043. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  67044. + (unsigned long)addr, DWC_READ_REG32(addr));
  67045. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  67046. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  67047. + (unsigned long)addr, DWC_READ_REG32(addr));
  67048. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  67049. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  67050. + (unsigned long)addr, DWC_READ_REG32(addr));
  67051. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  67052. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  67053. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  67054. + }
  67055. +
  67056. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  67057. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  67058. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  67059. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  67060. + (unsigned long)addr, DWC_READ_REG32(addr));
  67061. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  67062. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  67063. + (unsigned long)addr, DWC_READ_REG32(addr));
  67064. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  67065. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  67066. + (unsigned long)addr, DWC_READ_REG32(addr));
  67067. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  67068. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  67069. + (unsigned long)addr, DWC_READ_REG32(addr));
  67070. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  67071. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  67072. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  67073. + (unsigned long)addr, DWC_READ_REG32(addr));
  67074. + }
  67075. +
  67076. + }
  67077. +}
  67078. +
  67079. +/**
  67080. + * This functions reads the SPRAM and prints its content
  67081. + *
  67082. + * @param core_if Programming view of DWC_otg controller.
  67083. + */
  67084. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  67085. +{
  67086. + volatile uint8_t *addr, *start_addr, *end_addr;
  67087. +
  67088. + DWC_PRINTF("SPRAM Data:\n");
  67089. + start_addr = (void *)core_if->core_global_regs;
  67090. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  67091. + start_addr += 0x00028000;
  67092. + end_addr = (void *)core_if->core_global_regs;
  67093. + end_addr += 0x000280e0;
  67094. +
  67095. + for (addr = start_addr; addr < end_addr; addr += 16) {
  67096. + DWC_PRINTF
  67097. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  67098. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  67099. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  67100. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  67101. + );
  67102. + }
  67103. +
  67104. + return;
  67105. +}
  67106. +
  67107. +/**
  67108. + * This function reads the host registers and prints them
  67109. + *
  67110. + * @param core_if Programming view of DWC_otg controller.
  67111. + */
  67112. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  67113. +{
  67114. + int i;
  67115. + volatile uint32_t *addr;
  67116. +
  67117. + DWC_PRINTF("Host Global Registers\n");
  67118. + addr = &core_if->host_if->host_global_regs->hcfg;
  67119. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  67120. + (unsigned long)addr, DWC_READ_REG32(addr));
  67121. + addr = &core_if->host_if->host_global_regs->hfir;
  67122. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  67123. + (unsigned long)addr, DWC_READ_REG32(addr));
  67124. + addr = &core_if->host_if->host_global_regs->hfnum;
  67125. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67126. + DWC_READ_REG32(addr));
  67127. + addr = &core_if->host_if->host_global_regs->hptxsts;
  67128. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67129. + DWC_READ_REG32(addr));
  67130. + addr = &core_if->host_if->host_global_regs->haint;
  67131. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67132. + DWC_READ_REG32(addr));
  67133. + addr = &core_if->host_if->host_global_regs->haintmsk;
  67134. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67135. + DWC_READ_REG32(addr));
  67136. + if (core_if->dma_desc_enable) {
  67137. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  67138. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  67139. + (unsigned long)addr, DWC_READ_REG32(addr));
  67140. + }
  67141. +
  67142. + addr = core_if->host_if->hprt0;
  67143. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67144. + DWC_READ_REG32(addr));
  67145. +
  67146. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  67147. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  67148. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  67149. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  67150. + (unsigned long)addr, DWC_READ_REG32(addr));
  67151. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  67152. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  67153. + (unsigned long)addr, DWC_READ_REG32(addr));
  67154. + addr = &core_if->host_if->hc_regs[i]->hcint;
  67155. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  67156. + (unsigned long)addr, DWC_READ_REG32(addr));
  67157. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  67158. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  67159. + (unsigned long)addr, DWC_READ_REG32(addr));
  67160. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  67161. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  67162. + (unsigned long)addr, DWC_READ_REG32(addr));
  67163. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  67164. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  67165. + (unsigned long)addr, DWC_READ_REG32(addr));
  67166. + if (core_if->dma_desc_enable) {
  67167. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  67168. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  67169. + (unsigned long)addr, DWC_READ_REG32(addr));
  67170. + }
  67171. +
  67172. + }
  67173. + return;
  67174. +}
  67175. +
  67176. +/**
  67177. + * This function reads the core global registers and prints them
  67178. + *
  67179. + * @param core_if Programming view of DWC_otg controller.
  67180. + */
  67181. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  67182. +{
  67183. + int i, ep_num;
  67184. + volatile uint32_t *addr;
  67185. + char *txfsiz;
  67186. +
  67187. + DWC_PRINTF("Core Global Registers\n");
  67188. + addr = &core_if->core_global_regs->gotgctl;
  67189. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67190. + DWC_READ_REG32(addr));
  67191. + addr = &core_if->core_global_regs->gotgint;
  67192. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67193. + DWC_READ_REG32(addr));
  67194. + addr = &core_if->core_global_regs->gahbcfg;
  67195. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67196. + DWC_READ_REG32(addr));
  67197. + addr = &core_if->core_global_regs->gusbcfg;
  67198. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67199. + DWC_READ_REG32(addr));
  67200. + addr = &core_if->core_global_regs->grstctl;
  67201. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67202. + DWC_READ_REG32(addr));
  67203. + addr = &core_if->core_global_regs->gintsts;
  67204. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67205. + DWC_READ_REG32(addr));
  67206. + addr = &core_if->core_global_regs->gintmsk;
  67207. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67208. + DWC_READ_REG32(addr));
  67209. + addr = &core_if->core_global_regs->grxstsr;
  67210. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67211. + DWC_READ_REG32(addr));
  67212. + addr = &core_if->core_global_regs->grxfsiz;
  67213. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67214. + DWC_READ_REG32(addr));
  67215. + addr = &core_if->core_global_regs->gnptxfsiz;
  67216. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67217. + DWC_READ_REG32(addr));
  67218. + addr = &core_if->core_global_regs->gnptxsts;
  67219. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67220. + DWC_READ_REG32(addr));
  67221. + addr = &core_if->core_global_regs->gi2cctl;
  67222. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67223. + DWC_READ_REG32(addr));
  67224. + addr = &core_if->core_global_regs->gpvndctl;
  67225. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67226. + DWC_READ_REG32(addr));
  67227. + addr = &core_if->core_global_regs->ggpio;
  67228. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67229. + DWC_READ_REG32(addr));
  67230. + addr = &core_if->core_global_regs->guid;
  67231. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  67232. + (unsigned long)addr, DWC_READ_REG32(addr));
  67233. + addr = &core_if->core_global_regs->gsnpsid;
  67234. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67235. + DWC_READ_REG32(addr));
  67236. + addr = &core_if->core_global_regs->ghwcfg1;
  67237. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67238. + DWC_READ_REG32(addr));
  67239. + addr = &core_if->core_global_regs->ghwcfg2;
  67240. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67241. + DWC_READ_REG32(addr));
  67242. + addr = &core_if->core_global_regs->ghwcfg3;
  67243. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67244. + DWC_READ_REG32(addr));
  67245. + addr = &core_if->core_global_regs->ghwcfg4;
  67246. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67247. + DWC_READ_REG32(addr));
  67248. + addr = &core_if->core_global_regs->glpmcfg;
  67249. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67250. + DWC_READ_REG32(addr));
  67251. + addr = &core_if->core_global_regs->gpwrdn;
  67252. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67253. + DWC_READ_REG32(addr));
  67254. + addr = &core_if->core_global_regs->gdfifocfg;
  67255. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67256. + DWC_READ_REG32(addr));
  67257. + addr = &core_if->core_global_regs->adpctl;
  67258. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67259. + dwc_otg_adp_read_reg(core_if));
  67260. + addr = &core_if->core_global_regs->hptxfsiz;
  67261. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67262. + DWC_READ_REG32(addr));
  67263. +
  67264. + if (core_if->en_multiple_tx_fifo == 0) {
  67265. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  67266. + txfsiz = "DPTXFSIZ";
  67267. + } else {
  67268. + ep_num = core_if->hwcfg4.b.num_in_eps;
  67269. + txfsiz = "DIENPTXF";
  67270. + }
  67271. + for (i = 0; i < ep_num; i++) {
  67272. + addr = &core_if->core_global_regs->dtxfsiz[i];
  67273. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  67274. + (unsigned long)addr, DWC_READ_REG32(addr));
  67275. + }
  67276. + addr = core_if->pcgcctl;
  67277. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  67278. + DWC_READ_REG32(addr));
  67279. +}
  67280. +
  67281. +/**
  67282. + * Flush a Tx FIFO.
  67283. + *
  67284. + * @param core_if Programming view of DWC_otg controller.
  67285. + * @param num Tx FIFO to flush.
  67286. + */
  67287. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  67288. +{
  67289. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  67290. + volatile grstctl_t greset = {.d32 = 0 };
  67291. + int count = 0;
  67292. +
  67293. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  67294. +
  67295. + greset.b.txfflsh = 1;
  67296. + greset.b.txfnum = num;
  67297. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  67298. +
  67299. + do {
  67300. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  67301. + if (++count > 10000) {
  67302. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  67303. + __func__, greset.d32,
  67304. + DWC_READ_REG32(&global_regs->gnptxsts));
  67305. + break;
  67306. + }
  67307. + dwc_udelay(1);
  67308. + } while (greset.b.txfflsh == 1);
  67309. +
  67310. + /* Wait for 3 PHY Clocks */
  67311. + dwc_udelay(1);
  67312. +}
  67313. +
  67314. +/**
  67315. + * Flush Rx FIFO.
  67316. + *
  67317. + * @param core_if Programming view of DWC_otg controller.
  67318. + */
  67319. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  67320. +{
  67321. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  67322. + volatile grstctl_t greset = {.d32 = 0 };
  67323. + int count = 0;
  67324. +
  67325. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  67326. + /*
  67327. + *
  67328. + */
  67329. + greset.b.rxfflsh = 1;
  67330. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  67331. +
  67332. + do {
  67333. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  67334. + if (++count > 10000) {
  67335. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  67336. + greset.d32);
  67337. + break;
  67338. + }
  67339. + dwc_udelay(1);
  67340. + } while (greset.b.rxfflsh == 1);
  67341. +
  67342. + /* Wait for 3 PHY Clocks */
  67343. + dwc_udelay(1);
  67344. +}
  67345. +
  67346. +/**
  67347. + * Do core a soft reset of the core. Be careful with this because it
  67348. + * resets all the internal state machines of the core.
  67349. + */
  67350. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  67351. +{
  67352. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  67353. + volatile grstctl_t greset = {.d32 = 0 };
  67354. + int count = 0;
  67355. +
  67356. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  67357. + /* Wait for AHB master IDLE state. */
  67358. + do {
  67359. + dwc_udelay(10);
  67360. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  67361. + if (++count > 100000) {
  67362. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  67363. + greset.d32);
  67364. + return;
  67365. + }
  67366. + }
  67367. + while (greset.b.ahbidle == 0);
  67368. +
  67369. + /* Core Soft Reset */
  67370. + count = 0;
  67371. + greset.b.csftrst = 1;
  67372. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  67373. + do {
  67374. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  67375. + if (++count > 10000) {
  67376. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  67377. + __func__, greset.d32);
  67378. + break;
  67379. + }
  67380. + dwc_udelay(1);
  67381. + }
  67382. + while (greset.b.csftrst == 1);
  67383. +
  67384. + /* Wait for 3 PHY Clocks */
  67385. + dwc_mdelay(100);
  67386. +}
  67387. +
  67388. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  67389. +{
  67390. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  67391. +}
  67392. +
  67393. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  67394. +{
  67395. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  67396. +}
  67397. +
  67398. +/**
  67399. + * Register HCD callbacks. The callbacks are used to start and stop
  67400. + * the HCD for interrupt processing.
  67401. + *
  67402. + * @param core_if Programming view of DWC_otg controller.
  67403. + * @param cb the HCD callback structure.
  67404. + * @param p pointer to be passed to callback function (usb_hcd*).
  67405. + */
  67406. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  67407. + dwc_otg_cil_callbacks_t * cb, void *p)
  67408. +{
  67409. + core_if->hcd_cb = cb;
  67410. + cb->p = p;
  67411. +}
  67412. +
  67413. +/**
  67414. + * Register PCD callbacks. The callbacks are used to start and stop
  67415. + * the PCD for interrupt processing.
  67416. + *
  67417. + * @param core_if Programming view of DWC_otg controller.
  67418. + * @param cb the PCD callback structure.
  67419. + * @param p pointer to be passed to callback function (pcd*).
  67420. + */
  67421. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  67422. + dwc_otg_cil_callbacks_t * cb, void *p)
  67423. +{
  67424. + core_if->pcd_cb = cb;
  67425. + cb->p = p;
  67426. +}
  67427. +
  67428. +#ifdef DWC_EN_ISOC
  67429. +
  67430. +/**
  67431. + * This function writes isoc data per 1 (micro)frame into tx fifo
  67432. + *
  67433. + * @param core_if Programming view of DWC_otg controller.
  67434. + * @param ep The EP to start the transfer on.
  67435. + *
  67436. + */
  67437. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  67438. +{
  67439. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  67440. + dtxfsts_data_t txstatus = {.d32 = 0 };
  67441. + uint32_t len = 0;
  67442. + uint32_t dwords;
  67443. +
  67444. + ep->xfer_len = ep->data_per_frame;
  67445. + ep->xfer_count = 0;
  67446. +
  67447. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  67448. +
  67449. + len = ep->xfer_len - ep->xfer_count;
  67450. +
  67451. + if (len > ep->maxpacket) {
  67452. + len = ep->maxpacket;
  67453. + }
  67454. +
  67455. + dwords = (len + 3) / 4;
  67456. +
  67457. + /* While there is space in the queue and space in the FIFO and
  67458. + * More data to tranfer, Write packets to the Tx FIFO */
  67459. + txstatus.d32 =
  67460. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  67461. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  67462. +
  67463. + while (txstatus.b.txfspcavail > dwords &&
  67464. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  67465. + /* Write the FIFO */
  67466. + dwc_otg_ep_write_packet(core_if, ep, 0);
  67467. +
  67468. + len = ep->xfer_len - ep->xfer_count;
  67469. + if (len > ep->maxpacket) {
  67470. + len = ep->maxpacket;
  67471. + }
  67472. +
  67473. + dwords = (len + 3) / 4;
  67474. + txstatus.d32 =
  67475. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  67476. + dtxfsts);
  67477. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  67478. + txstatus.d32);
  67479. + }
  67480. +}
  67481. +
  67482. +/**
  67483. + * This function initializes a descriptor chain for Isochronous transfer
  67484. + *
  67485. + * @param core_if Programming view of DWC_otg controller.
  67486. + * @param ep The EP to start the transfer on.
  67487. + *
  67488. + */
  67489. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  67490. + dwc_ep_t * ep)
  67491. +{
  67492. + deptsiz_data_t deptsiz = {.d32 = 0 };
  67493. + depctl_data_t depctl = {.d32 = 0 };
  67494. + dsts_data_t dsts = {.d32 = 0 };
  67495. + volatile uint32_t *addr;
  67496. +
  67497. + if (ep->is_in) {
  67498. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  67499. + } else {
  67500. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  67501. + }
  67502. +
  67503. + ep->xfer_len = ep->data_per_frame;
  67504. + ep->xfer_count = 0;
  67505. + ep->xfer_buff = ep->cur_pkt_addr;
  67506. + ep->dma_addr = ep->cur_pkt_dma_addr;
  67507. +
  67508. + if (ep->is_in) {
  67509. + /* Program the transfer size and packet count
  67510. + * as follows: xfersize = N * maxpacket +
  67511. + * short_packet pktcnt = N + (short_packet
  67512. + * exist ? 1 : 0)
  67513. + */
  67514. + deptsiz.b.xfersize = ep->xfer_len;
  67515. + deptsiz.b.pktcnt =
  67516. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  67517. + deptsiz.b.mc = deptsiz.b.pktcnt;
  67518. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  67519. + deptsiz.d32);
  67520. +
  67521. + /* Write the DMA register */
  67522. + if (core_if->dma_enable) {
  67523. + DWC_WRITE_REG32(&
  67524. + (core_if->dev_if->in_ep_regs[ep->num]->
  67525. + diepdma), (uint32_t) ep->dma_addr);
  67526. + }
  67527. + } else {
  67528. + deptsiz.b.pktcnt =
  67529. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  67530. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  67531. +
  67532. + DWC_WRITE_REG32(&core_if->dev_if->
  67533. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  67534. +
  67535. + if (core_if->dma_enable) {
  67536. + DWC_WRITE_REG32(&
  67537. + (core_if->dev_if->
  67538. + out_ep_regs[ep->num]->doepdma),
  67539. + (uint32_t) ep->dma_addr);
  67540. + }
  67541. + }
  67542. +
  67543. + /** Enable endpoint, clear nak */
  67544. +
  67545. + depctl.d32 = 0;
  67546. + if (ep->bInterval == 1) {
  67547. + dsts.d32 =
  67548. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  67549. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  67550. +
  67551. + if (ep->next_frame & 0x1) {
  67552. + depctl.b.setd1pid = 1;
  67553. + } else {
  67554. + depctl.b.setd0pid = 1;
  67555. + }
  67556. + } else {
  67557. + ep->next_frame += ep->bInterval;
  67558. +
  67559. + if (ep->next_frame & 0x1) {
  67560. + depctl.b.setd1pid = 1;
  67561. + } else {
  67562. + depctl.b.setd0pid = 1;
  67563. + }
  67564. + }
  67565. + depctl.b.epena = 1;
  67566. + depctl.b.cnak = 1;
  67567. +
  67568. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  67569. + depctl.d32 = DWC_READ_REG32(addr);
  67570. +
  67571. + if (ep->is_in && core_if->dma_enable == 0) {
  67572. + write_isoc_frame_data(core_if, ep);
  67573. + }
  67574. +
  67575. +}
  67576. +#endif /* DWC_EN_ISOC */
  67577. +
  67578. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  67579. +{
  67580. + int i;
  67581. + for (i = 0; i < size; i++) {
  67582. + p[i] = -1;
  67583. + }
  67584. +}
  67585. +
  67586. +static int dwc_otg_param_initialized(int32_t val)
  67587. +{
  67588. + return val != -1;
  67589. +}
  67590. +
  67591. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  67592. +{
  67593. + int i;
  67594. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  67595. + if (!core_if->core_params) {
  67596. + return -DWC_E_NO_MEMORY;
  67597. + }
  67598. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  67599. + sizeof(*core_if->core_params) /
  67600. + sizeof(int32_t));
  67601. + DWC_PRINTF("Setting default values for core params\n");
  67602. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  67603. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  67604. + dwc_otg_set_param_dma_desc_enable(core_if,
  67605. + dwc_param_dma_desc_enable_default);
  67606. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  67607. + dwc_otg_set_param_dma_burst_size(core_if,
  67608. + dwc_param_dma_burst_size_default);
  67609. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  67610. + dwc_param_host_support_fs_ls_low_power_default);
  67611. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  67612. + dwc_param_enable_dynamic_fifo_default);
  67613. + dwc_otg_set_param_data_fifo_size(core_if,
  67614. + dwc_param_data_fifo_size_default);
  67615. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  67616. + dwc_param_dev_rx_fifo_size_default);
  67617. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  67618. + dwc_param_dev_nperio_tx_fifo_size_default);
  67619. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  67620. + dwc_param_host_rx_fifo_size_default);
  67621. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  67622. + dwc_param_host_nperio_tx_fifo_size_default);
  67623. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  67624. + dwc_param_host_perio_tx_fifo_size_default);
  67625. + dwc_otg_set_param_max_transfer_size(core_if,
  67626. + dwc_param_max_transfer_size_default);
  67627. + dwc_otg_set_param_max_packet_count(core_if,
  67628. + dwc_param_max_packet_count_default);
  67629. + dwc_otg_set_param_host_channels(core_if,
  67630. + dwc_param_host_channels_default);
  67631. + dwc_otg_set_param_dev_endpoints(core_if,
  67632. + dwc_param_dev_endpoints_default);
  67633. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  67634. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  67635. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  67636. + dwc_param_host_ls_low_power_phy_clk_default);
  67637. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  67638. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  67639. + dwc_param_phy_ulpi_ext_vbus_default);
  67640. + dwc_otg_set_param_phy_utmi_width(core_if,
  67641. + dwc_param_phy_utmi_width_default);
  67642. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  67643. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  67644. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  67645. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  67646. + dwc_param_en_multiple_tx_fifo_default);
  67647. + for (i = 0; i < 15; i++) {
  67648. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  67649. + dwc_param_dev_perio_tx_fifo_size_default,
  67650. + i);
  67651. + }
  67652. +
  67653. + for (i = 0; i < 15; i++) {
  67654. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  67655. + dwc_param_dev_tx_fifo_size_default,
  67656. + i);
  67657. + }
  67658. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  67659. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  67660. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  67661. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  67662. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  67663. + dwc_otg_set_param_tx_thr_length(core_if,
  67664. + dwc_param_tx_thr_length_default);
  67665. + dwc_otg_set_param_rx_thr_length(core_if,
  67666. + dwc_param_rx_thr_length_default);
  67667. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  67668. + dwc_param_ahb_thr_ratio_default);
  67669. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  67670. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  67671. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  67672. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  67673. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  67674. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  67675. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  67676. + DWC_PRINTF("Finished setting default values for core params\n");
  67677. +
  67678. + return 0;
  67679. +}
  67680. +
  67681. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  67682. +{
  67683. + return core_if->dma_enable;
  67684. +}
  67685. +
  67686. +/* Checks if the parameter is outside of its valid range of values */
  67687. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  67688. + (((_param_) < (_low_)) || \
  67689. + ((_param_) > (_high_)))
  67690. +
  67691. +/* Parameter access functions */
  67692. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  67693. +{
  67694. + int valid;
  67695. + int retval = 0;
  67696. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  67697. + DWC_WARN("Wrong value for otg_cap parameter\n");
  67698. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  67699. + retval = -DWC_E_INVALID;
  67700. + goto out;
  67701. + }
  67702. +
  67703. + valid = 1;
  67704. + switch (val) {
  67705. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  67706. + if (core_if->hwcfg2.b.op_mode !=
  67707. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  67708. + valid = 0;
  67709. + break;
  67710. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  67711. + if ((core_if->hwcfg2.b.op_mode !=
  67712. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  67713. + && (core_if->hwcfg2.b.op_mode !=
  67714. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  67715. + && (core_if->hwcfg2.b.op_mode !=
  67716. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  67717. + && (core_if->hwcfg2.b.op_mode !=
  67718. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  67719. + valid = 0;
  67720. + }
  67721. + break;
  67722. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  67723. + /* always valid */
  67724. + break;
  67725. + }
  67726. + if (!valid) {
  67727. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  67728. + DWC_ERROR
  67729. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  67730. + val);
  67731. + }
  67732. + val =
  67733. + (((core_if->hwcfg2.b.op_mode ==
  67734. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  67735. + || (core_if->hwcfg2.b.op_mode ==
  67736. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  67737. + || (core_if->hwcfg2.b.op_mode ==
  67738. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  67739. + || (core_if->hwcfg2.b.op_mode ==
  67740. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  67741. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  67742. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  67743. + retval = -DWC_E_INVALID;
  67744. + }
  67745. +
  67746. + core_if->core_params->otg_cap = val;
  67747. +out:
  67748. + return retval;
  67749. +}
  67750. +
  67751. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  67752. +{
  67753. + return core_if->core_params->otg_cap;
  67754. +}
  67755. +
  67756. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  67757. +{
  67758. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67759. + DWC_WARN("Wrong value for opt parameter\n");
  67760. + return -DWC_E_INVALID;
  67761. + }
  67762. + core_if->core_params->opt = val;
  67763. + return 0;
  67764. +}
  67765. +
  67766. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  67767. +{
  67768. + return core_if->core_params->opt;
  67769. +}
  67770. +
  67771. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  67772. +{
  67773. + int retval = 0;
  67774. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67775. + DWC_WARN("Wrong value for dma enable\n");
  67776. + return -DWC_E_INVALID;
  67777. + }
  67778. +
  67779. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  67780. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  67781. + DWC_ERROR
  67782. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  67783. + val);
  67784. + }
  67785. + val = 0;
  67786. + retval = -DWC_E_INVALID;
  67787. + }
  67788. +
  67789. + core_if->core_params->dma_enable = val;
  67790. + if (val == 0) {
  67791. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  67792. + }
  67793. + return retval;
  67794. +}
  67795. +
  67796. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  67797. +{
  67798. + return core_if->core_params->dma_enable;
  67799. +}
  67800. +
  67801. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  67802. +{
  67803. + int retval = 0;
  67804. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67805. + DWC_WARN("Wrong value for dma_enable\n");
  67806. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  67807. + return -DWC_E_INVALID;
  67808. + }
  67809. +
  67810. + if ((val == 1)
  67811. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  67812. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  67813. + if (dwc_otg_param_initialized
  67814. + (core_if->core_params->dma_desc_enable)) {
  67815. + DWC_ERROR
  67816. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  67817. + val);
  67818. + }
  67819. + val = 0;
  67820. + retval = -DWC_E_INVALID;
  67821. + }
  67822. + core_if->core_params->dma_desc_enable = val;
  67823. + return retval;
  67824. +}
  67825. +
  67826. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  67827. +{
  67828. + return core_if->core_params->dma_desc_enable;
  67829. +}
  67830. +
  67831. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  67832. + int32_t val)
  67833. +{
  67834. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67835. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  67836. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  67837. + return -DWC_E_INVALID;
  67838. + }
  67839. + core_if->core_params->host_support_fs_ls_low_power = val;
  67840. + return 0;
  67841. +}
  67842. +
  67843. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  67844. + core_if)
  67845. +{
  67846. + return core_if->core_params->host_support_fs_ls_low_power;
  67847. +}
  67848. +
  67849. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  67850. + int32_t val)
  67851. +{
  67852. + int retval = 0;
  67853. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  67854. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  67855. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  67856. + return -DWC_E_INVALID;
  67857. + }
  67858. +
  67859. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  67860. + if (dwc_otg_param_initialized
  67861. + (core_if->core_params->enable_dynamic_fifo)) {
  67862. + DWC_ERROR
  67863. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  67864. + val);
  67865. + }
  67866. + val = 0;
  67867. + retval = -DWC_E_INVALID;
  67868. + }
  67869. + core_if->core_params->enable_dynamic_fifo = val;
  67870. + return retval;
  67871. +}
  67872. +
  67873. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  67874. +{
  67875. + return core_if->core_params->enable_dynamic_fifo;
  67876. +}
  67877. +
  67878. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  67879. +{
  67880. + int retval = 0;
  67881. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  67882. + DWC_WARN("Wrong value for data_fifo_size\n");
  67883. + DWC_WARN("data_fifo_size must be 32-32768\n");
  67884. + return -DWC_E_INVALID;
  67885. + }
  67886. +
  67887. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  67888. + if (dwc_otg_param_initialized
  67889. + (core_if->core_params->data_fifo_size)) {
  67890. + DWC_ERROR
  67891. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  67892. + val);
  67893. + }
  67894. + val = core_if->hwcfg3.b.dfifo_depth;
  67895. + retval = -DWC_E_INVALID;
  67896. + }
  67897. +
  67898. + core_if->core_params->data_fifo_size = val;
  67899. + return retval;
  67900. +}
  67901. +
  67902. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  67903. +{
  67904. + return core_if->core_params->data_fifo_size;
  67905. +}
  67906. +
  67907. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  67908. +{
  67909. + int retval = 0;
  67910. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  67911. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  67912. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  67913. + return -DWC_E_INVALID;
  67914. + }
  67915. +
  67916. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  67917. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  67918. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  67919. + }
  67920. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  67921. + retval = -DWC_E_INVALID;
  67922. + }
  67923. +
  67924. + core_if->core_params->dev_rx_fifo_size = val;
  67925. + return retval;
  67926. +}
  67927. +
  67928. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  67929. +{
  67930. + return core_if->core_params->dev_rx_fifo_size;
  67931. +}
  67932. +
  67933. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  67934. + int32_t val)
  67935. +{
  67936. + int retval = 0;
  67937. +
  67938. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  67939. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  67940. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  67941. + return -DWC_E_INVALID;
  67942. + }
  67943. +
  67944. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  67945. + if (dwc_otg_param_initialized
  67946. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  67947. + DWC_ERROR
  67948. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  67949. + val);
  67950. + }
  67951. + val =
  67952. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  67953. + 16);
  67954. + retval = -DWC_E_INVALID;
  67955. + }
  67956. +
  67957. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  67958. + return retval;
  67959. +}
  67960. +
  67961. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  67962. +{
  67963. + return core_if->core_params->dev_nperio_tx_fifo_size;
  67964. +}
  67965. +
  67966. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  67967. + int32_t val)
  67968. +{
  67969. + int retval = 0;
  67970. +
  67971. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  67972. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  67973. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  67974. + return -DWC_E_INVALID;
  67975. + }
  67976. +
  67977. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  67978. + if (dwc_otg_param_initialized
  67979. + (core_if->core_params->host_rx_fifo_size)) {
  67980. + DWC_ERROR
  67981. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  67982. + val);
  67983. + }
  67984. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  67985. + retval = -DWC_E_INVALID;
  67986. + }
  67987. +
  67988. + core_if->core_params->host_rx_fifo_size = val;
  67989. + return retval;
  67990. +
  67991. +}
  67992. +
  67993. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  67994. +{
  67995. + return core_if->core_params->host_rx_fifo_size;
  67996. +}
  67997. +
  67998. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  67999. + int32_t val)
  68000. +{
  68001. + int retval = 0;
  68002. +
  68003. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  68004. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  68005. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  68006. + return -DWC_E_INVALID;
  68007. + }
  68008. +
  68009. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  68010. + if (dwc_otg_param_initialized
  68011. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  68012. + DWC_ERROR
  68013. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  68014. + val);
  68015. + }
  68016. + val =
  68017. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  68018. + 16);
  68019. + retval = -DWC_E_INVALID;
  68020. + }
  68021. +
  68022. + core_if->core_params->host_nperio_tx_fifo_size = val;
  68023. + return retval;
  68024. +}
  68025. +
  68026. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  68027. +{
  68028. + return core_if->core_params->host_nperio_tx_fifo_size;
  68029. +}
  68030. +
  68031. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  68032. + int32_t val)
  68033. +{
  68034. + int retval = 0;
  68035. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  68036. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  68037. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  68038. + return -DWC_E_INVALID;
  68039. + }
  68040. +
  68041. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  68042. + if (dwc_otg_param_initialized
  68043. + (core_if->core_params->host_perio_tx_fifo_size)) {
  68044. + DWC_ERROR
  68045. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  68046. + val);
  68047. + }
  68048. + val = (core_if->hptxfsiz.d32) >> 16;
  68049. + retval = -DWC_E_INVALID;
  68050. + }
  68051. +
  68052. + core_if->core_params->host_perio_tx_fifo_size = val;
  68053. + return retval;
  68054. +}
  68055. +
  68056. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  68057. +{
  68058. + return core_if->core_params->host_perio_tx_fifo_size;
  68059. +}
  68060. +
  68061. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  68062. + int32_t val)
  68063. +{
  68064. + int retval = 0;
  68065. +
  68066. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  68067. + DWC_WARN("Wrong value for max_transfer_size\n");
  68068. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  68069. + return -DWC_E_INVALID;
  68070. + }
  68071. +
  68072. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  68073. + if (dwc_otg_param_initialized
  68074. + (core_if->core_params->max_transfer_size)) {
  68075. + DWC_ERROR
  68076. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  68077. + val);
  68078. + }
  68079. + val =
  68080. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  68081. + 1);
  68082. + retval = -DWC_E_INVALID;
  68083. + }
  68084. +
  68085. + core_if->core_params->max_transfer_size = val;
  68086. + return retval;
  68087. +}
  68088. +
  68089. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  68090. +{
  68091. + return core_if->core_params->max_transfer_size;
  68092. +}
  68093. +
  68094. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  68095. +{
  68096. + int retval = 0;
  68097. +
  68098. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  68099. + DWC_WARN("Wrong value for max_packet_count\n");
  68100. + DWC_WARN("max_packet_count must be 15-511\n");
  68101. + return -DWC_E_INVALID;
  68102. + }
  68103. +
  68104. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  68105. + if (dwc_otg_param_initialized
  68106. + (core_if->core_params->max_packet_count)) {
  68107. + DWC_ERROR
  68108. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  68109. + val);
  68110. + }
  68111. + val =
  68112. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  68113. + retval = -DWC_E_INVALID;
  68114. + }
  68115. +
  68116. + core_if->core_params->max_packet_count = val;
  68117. + return retval;
  68118. +}
  68119. +
  68120. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  68121. +{
  68122. + return core_if->core_params->max_packet_count;
  68123. +}
  68124. +
  68125. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  68126. +{
  68127. + int retval = 0;
  68128. +
  68129. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  68130. + DWC_WARN("Wrong value for host_channels\n");
  68131. + DWC_WARN("host_channels must be 1-16\n");
  68132. + return -DWC_E_INVALID;
  68133. + }
  68134. +
  68135. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  68136. + if (dwc_otg_param_initialized
  68137. + (core_if->core_params->host_channels)) {
  68138. + DWC_ERROR
  68139. + ("%d invalid for host_channels. Check HW configurations.\n",
  68140. + val);
  68141. + }
  68142. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  68143. + retval = -DWC_E_INVALID;
  68144. + }
  68145. +
  68146. + core_if->core_params->host_channels = val;
  68147. + return retval;
  68148. +}
  68149. +
  68150. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  68151. +{
  68152. + return core_if->core_params->host_channels;
  68153. +}
  68154. +
  68155. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  68156. +{
  68157. + int retval = 0;
  68158. +
  68159. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  68160. + DWC_WARN("Wrong value for dev_endpoints\n");
  68161. + DWC_WARN("dev_endpoints must be 1-15\n");
  68162. + return -DWC_E_INVALID;
  68163. + }
  68164. +
  68165. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  68166. + if (dwc_otg_param_initialized
  68167. + (core_if->core_params->dev_endpoints)) {
  68168. + DWC_ERROR
  68169. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  68170. + val);
  68171. + }
  68172. + val = core_if->hwcfg2.b.num_dev_ep;
  68173. + retval = -DWC_E_INVALID;
  68174. + }
  68175. +
  68176. + core_if->core_params->dev_endpoints = val;
  68177. + return retval;
  68178. +}
  68179. +
  68180. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  68181. +{
  68182. + return core_if->core_params->dev_endpoints;
  68183. +}
  68184. +
  68185. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  68186. +{
  68187. + int retval = 0;
  68188. + int valid = 0;
  68189. +
  68190. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  68191. + DWC_WARN("Wrong value for phy_type\n");
  68192. + DWC_WARN("phy_type must be 0,1 or 2\n");
  68193. + return -DWC_E_INVALID;
  68194. + }
  68195. +#ifndef NO_FS_PHY_HW_CHECKS
  68196. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  68197. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  68198. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  68199. + valid = 1;
  68200. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  68201. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  68202. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  68203. + valid = 1;
  68204. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  68205. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  68206. + valid = 1;
  68207. + }
  68208. + if (!valid) {
  68209. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  68210. + DWC_ERROR
  68211. + ("%d invalid for phy_type. Check HW configurations.\n",
  68212. + val);
  68213. + }
  68214. + if (core_if->hwcfg2.b.hs_phy_type) {
  68215. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  68216. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  68217. + val = DWC_PHY_TYPE_PARAM_UTMI;
  68218. + } else {
  68219. + val = DWC_PHY_TYPE_PARAM_ULPI;
  68220. + }
  68221. + }
  68222. + retval = -DWC_E_INVALID;
  68223. + }
  68224. +#endif
  68225. + core_if->core_params->phy_type = val;
  68226. + return retval;
  68227. +}
  68228. +
  68229. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  68230. +{
  68231. + return core_if->core_params->phy_type;
  68232. +}
  68233. +
  68234. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  68235. +{
  68236. + int retval = 0;
  68237. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68238. + DWC_WARN("Wrong value for speed parameter\n");
  68239. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  68240. + return -DWC_E_INVALID;
  68241. + }
  68242. + if ((val == 0)
  68243. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  68244. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  68245. + DWC_ERROR
  68246. + ("%d invalid for speed paremter. Check HW configuration.\n",
  68247. + val);
  68248. + }
  68249. + val =
  68250. + (dwc_otg_get_param_phy_type(core_if) ==
  68251. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  68252. + retval = -DWC_E_INVALID;
  68253. + }
  68254. + core_if->core_params->speed = val;
  68255. + return retval;
  68256. +}
  68257. +
  68258. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  68259. +{
  68260. + return core_if->core_params->speed;
  68261. +}
  68262. +
  68263. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  68264. + int32_t val)
  68265. +{
  68266. + int retval = 0;
  68267. +
  68268. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68269. + DWC_WARN
  68270. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  68271. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  68272. + return -DWC_E_INVALID;
  68273. + }
  68274. +
  68275. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  68276. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  68277. + if (dwc_otg_param_initialized
  68278. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  68279. + DWC_ERROR
  68280. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  68281. + val);
  68282. + }
  68283. + val =
  68284. + (dwc_otg_get_param_phy_type(core_if) ==
  68285. + DWC_PHY_TYPE_PARAM_FS) ?
  68286. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  68287. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  68288. + retval = -DWC_E_INVALID;
  68289. + }
  68290. +
  68291. + core_if->core_params->host_ls_low_power_phy_clk = val;
  68292. + return retval;
  68293. +}
  68294. +
  68295. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  68296. +{
  68297. + return core_if->core_params->host_ls_low_power_phy_clk;
  68298. +}
  68299. +
  68300. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  68301. +{
  68302. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68303. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  68304. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  68305. + return -DWC_E_INVALID;
  68306. + }
  68307. +
  68308. + core_if->core_params->phy_ulpi_ddr = val;
  68309. + return 0;
  68310. +}
  68311. +
  68312. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  68313. +{
  68314. + return core_if->core_params->phy_ulpi_ddr;
  68315. +}
  68316. +
  68317. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  68318. + int32_t val)
  68319. +{
  68320. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68321. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  68322. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  68323. + return -DWC_E_INVALID;
  68324. + }
  68325. +
  68326. + core_if->core_params->phy_ulpi_ext_vbus = val;
  68327. + return 0;
  68328. +}
  68329. +
  68330. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  68331. +{
  68332. + return core_if->core_params->phy_ulpi_ext_vbus;
  68333. +}
  68334. +
  68335. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  68336. +{
  68337. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  68338. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  68339. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  68340. + return -DWC_E_INVALID;
  68341. + }
  68342. +
  68343. + core_if->core_params->phy_utmi_width = val;
  68344. + return 0;
  68345. +}
  68346. +
  68347. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  68348. +{
  68349. + return core_if->core_params->phy_utmi_width;
  68350. +}
  68351. +
  68352. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  68353. +{
  68354. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68355. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  68356. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  68357. + return -DWC_E_INVALID;
  68358. + }
  68359. +
  68360. + core_if->core_params->ulpi_fs_ls = val;
  68361. + return 0;
  68362. +}
  68363. +
  68364. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  68365. +{
  68366. + return core_if->core_params->ulpi_fs_ls;
  68367. +}
  68368. +
  68369. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  68370. +{
  68371. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68372. + DWC_WARN("Wrong valaue for ts_dline\n");
  68373. + DWC_WARN("ts_dline must be 0 or 1\n");
  68374. + return -DWC_E_INVALID;
  68375. + }
  68376. +
  68377. + core_if->core_params->ts_dline = val;
  68378. + return 0;
  68379. +}
  68380. +
  68381. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  68382. +{
  68383. + return core_if->core_params->ts_dline;
  68384. +}
  68385. +
  68386. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  68387. +{
  68388. + int retval = 0;
  68389. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68390. + DWC_WARN("Wrong valaue for i2c_enable\n");
  68391. + DWC_WARN("i2c_enable must be 0 or 1\n");
  68392. + return -DWC_E_INVALID;
  68393. + }
  68394. +#ifndef NO_FS_PHY_HW_CHECK
  68395. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  68396. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  68397. + DWC_ERROR
  68398. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  68399. + val);
  68400. + }
  68401. + val = 0;
  68402. + retval = -DWC_E_INVALID;
  68403. + }
  68404. +#endif
  68405. +
  68406. + core_if->core_params->i2c_enable = val;
  68407. + return retval;
  68408. +}
  68409. +
  68410. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  68411. +{
  68412. + return core_if->core_params->i2c_enable;
  68413. +}
  68414. +
  68415. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  68416. + int32_t val, int fifo_num)
  68417. +{
  68418. + int retval = 0;
  68419. +
  68420. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  68421. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  68422. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  68423. + return -DWC_E_INVALID;
  68424. + }
  68425. +
  68426. + if (val >
  68427. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  68428. + if (dwc_otg_param_initialized
  68429. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  68430. + DWC_ERROR
  68431. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  68432. + val, fifo_num);
  68433. + }
  68434. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  68435. + retval = -DWC_E_INVALID;
  68436. + }
  68437. +
  68438. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  68439. + return retval;
  68440. +}
  68441. +
  68442. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  68443. + int fifo_num)
  68444. +{
  68445. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  68446. +}
  68447. +
  68448. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  68449. + int32_t val)
  68450. +{
  68451. + int retval = 0;
  68452. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68453. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  68454. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  68455. + return -DWC_E_INVALID;
  68456. + }
  68457. +
  68458. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  68459. + if (dwc_otg_param_initialized
  68460. + (core_if->core_params->en_multiple_tx_fifo)) {
  68461. + DWC_ERROR
  68462. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  68463. + val);
  68464. + }
  68465. + val = 0;
  68466. + retval = -DWC_E_INVALID;
  68467. + }
  68468. +
  68469. + core_if->core_params->en_multiple_tx_fifo = val;
  68470. + return retval;
  68471. +}
  68472. +
  68473. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  68474. +{
  68475. + return core_if->core_params->en_multiple_tx_fifo;
  68476. +}
  68477. +
  68478. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  68479. + int fifo_num)
  68480. +{
  68481. + int retval = 0;
  68482. +
  68483. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  68484. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  68485. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  68486. + return -DWC_E_INVALID;
  68487. + }
  68488. +
  68489. + if (val >
  68490. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  68491. + if (dwc_otg_param_initialized
  68492. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  68493. + DWC_ERROR
  68494. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  68495. + val, fifo_num);
  68496. + }
  68497. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  68498. + retval = -DWC_E_INVALID;
  68499. + }
  68500. +
  68501. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  68502. + return retval;
  68503. +}
  68504. +
  68505. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  68506. + int fifo_num)
  68507. +{
  68508. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  68509. +}
  68510. +
  68511. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  68512. +{
  68513. + int retval = 0;
  68514. +
  68515. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  68516. + DWC_WARN("Wrong value for thr_ctl\n");
  68517. + DWC_WARN("thr_ctl must be 0-7\n");
  68518. + return -DWC_E_INVALID;
  68519. + }
  68520. +
  68521. + if ((val != 0) &&
  68522. + (!dwc_otg_get_param_dma_enable(core_if) ||
  68523. + !core_if->hwcfg4.b.ded_fifo_en)) {
  68524. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  68525. + DWC_ERROR
  68526. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  68527. + val);
  68528. + }
  68529. + val = 0;
  68530. + retval = -DWC_E_INVALID;
  68531. + }
  68532. +
  68533. + core_if->core_params->thr_ctl = val;
  68534. + return retval;
  68535. +}
  68536. +
  68537. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  68538. +{
  68539. + return core_if->core_params->thr_ctl;
  68540. +}
  68541. +
  68542. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  68543. +{
  68544. + int retval = 0;
  68545. +
  68546. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68547. + DWC_WARN("Wrong value for lpm_enable\n");
  68548. + DWC_WARN("lpm_enable must be 0 or 1\n");
  68549. + return -DWC_E_INVALID;
  68550. + }
  68551. +
  68552. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  68553. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  68554. + DWC_ERROR
  68555. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  68556. + val);
  68557. + }
  68558. + val = 0;
  68559. + retval = -DWC_E_INVALID;
  68560. + }
  68561. +
  68562. + core_if->core_params->lpm_enable = val;
  68563. + return retval;
  68564. +}
  68565. +
  68566. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  68567. +{
  68568. + return core_if->core_params->lpm_enable;
  68569. +}
  68570. +
  68571. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  68572. +{
  68573. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  68574. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  68575. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  68576. + return -DWC_E_INVALID;
  68577. + }
  68578. +
  68579. + core_if->core_params->tx_thr_length = val;
  68580. + return 0;
  68581. +}
  68582. +
  68583. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  68584. +{
  68585. + return core_if->core_params->tx_thr_length;
  68586. +}
  68587. +
  68588. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  68589. +{
  68590. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  68591. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  68592. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  68593. + return -DWC_E_INVALID;
  68594. + }
  68595. +
  68596. + core_if->core_params->rx_thr_length = val;
  68597. + return 0;
  68598. +}
  68599. +
  68600. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  68601. +{
  68602. + return core_if->core_params->rx_thr_length;
  68603. +}
  68604. +
  68605. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  68606. +{
  68607. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  68608. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  68609. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  68610. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  68611. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  68612. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  68613. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  68614. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  68615. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  68616. + return -DWC_E_INVALID;
  68617. + }
  68618. + core_if->core_params->dma_burst_size = val;
  68619. + return 0;
  68620. +}
  68621. +
  68622. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  68623. +{
  68624. + return core_if->core_params->dma_burst_size;
  68625. +}
  68626. +
  68627. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  68628. +{
  68629. + int retval = 0;
  68630. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68631. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  68632. + return -DWC_E_INVALID;
  68633. + }
  68634. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  68635. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  68636. + DWC_ERROR
  68637. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  68638. + val);
  68639. + }
  68640. + retval = -DWC_E_INVALID;
  68641. + val = 0;
  68642. + }
  68643. + core_if->core_params->pti_enable = val;
  68644. + return retval;
  68645. +}
  68646. +
  68647. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  68648. +{
  68649. + return core_if->core_params->pti_enable;
  68650. +}
  68651. +
  68652. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  68653. +{
  68654. + int retval = 0;
  68655. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68656. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  68657. + return -DWC_E_INVALID;
  68658. + }
  68659. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  68660. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  68661. + DWC_ERROR
  68662. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  68663. + val);
  68664. + }
  68665. + retval = -DWC_E_INVALID;
  68666. + val = 0;
  68667. + }
  68668. + core_if->core_params->mpi_enable = val;
  68669. + return retval;
  68670. +}
  68671. +
  68672. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  68673. +{
  68674. + return core_if->core_params->mpi_enable;
  68675. +}
  68676. +
  68677. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  68678. +{
  68679. + int retval = 0;
  68680. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68681. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  68682. + return -DWC_E_INVALID;
  68683. + }
  68684. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  68685. + if (dwc_otg_param_initialized
  68686. + (core_if->core_params->adp_supp_enable)) {
  68687. + DWC_ERROR
  68688. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  68689. + val);
  68690. + }
  68691. + retval = -DWC_E_INVALID;
  68692. + val = 0;
  68693. + }
  68694. + core_if->core_params->adp_supp_enable = val;
  68695. + /*Set OTG version 2.0 in case of enabling ADP*/
  68696. + if (val)
  68697. + dwc_otg_set_param_otg_ver(core_if, 1);
  68698. +
  68699. + return retval;
  68700. +}
  68701. +
  68702. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  68703. +{
  68704. + return core_if->core_params->adp_supp_enable;
  68705. +}
  68706. +
  68707. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  68708. +{
  68709. + int retval = 0;
  68710. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68711. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  68712. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  68713. + return -DWC_E_INVALID;
  68714. + }
  68715. +
  68716. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  68717. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  68718. + DWC_ERROR
  68719. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  68720. + val);
  68721. + }
  68722. + retval = -DWC_E_INVALID;
  68723. + val = 0;
  68724. + }
  68725. + core_if->core_params->ic_usb_cap = val;
  68726. + return retval;
  68727. +}
  68728. +
  68729. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  68730. +{
  68731. + return core_if->core_params->ic_usb_cap;
  68732. +}
  68733. +
  68734. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  68735. +{
  68736. + int retval = 0;
  68737. + int valid = 1;
  68738. +
  68739. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  68740. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  68741. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  68742. + return -DWC_E_INVALID;
  68743. + }
  68744. +
  68745. + if (val
  68746. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  68747. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  68748. + valid = 0;
  68749. + } else if (val
  68750. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  68751. + 4)) {
  68752. + valid = 0;
  68753. + }
  68754. + if (valid == 0) {
  68755. + if (dwc_otg_param_initialized
  68756. + (core_if->core_params->ahb_thr_ratio)) {
  68757. + DWC_ERROR
  68758. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  68759. + val);
  68760. + }
  68761. + retval = -DWC_E_INVALID;
  68762. + val = 0;
  68763. + }
  68764. +
  68765. + core_if->core_params->ahb_thr_ratio = val;
  68766. + return retval;
  68767. +}
  68768. +
  68769. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  68770. +{
  68771. + return core_if->core_params->ahb_thr_ratio;
  68772. +}
  68773. +
  68774. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  68775. +{
  68776. + int retval = 0;
  68777. + int valid = 1;
  68778. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  68779. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  68780. +
  68781. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  68782. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  68783. + DWC_WARN("power_down must be 0 - 2\n");
  68784. + return -DWC_E_INVALID;
  68785. + }
  68786. +
  68787. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  68788. + valid = 0;
  68789. + }
  68790. + if ((val == 3)
  68791. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  68792. + || (hwcfg4.b.xhiber == 0))) {
  68793. + valid = 0;
  68794. + }
  68795. + if (valid == 0) {
  68796. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  68797. + DWC_ERROR
  68798. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  68799. + val);
  68800. + }
  68801. + retval = -DWC_E_INVALID;
  68802. + val = 0;
  68803. + }
  68804. + core_if->core_params->power_down = val;
  68805. + return retval;
  68806. +}
  68807. +
  68808. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  68809. +{
  68810. + return core_if->core_params->power_down;
  68811. +}
  68812. +
  68813. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  68814. +{
  68815. + int retval = 0;
  68816. + int valid = 1;
  68817. +
  68818. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68819. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  68820. + DWC_WARN("reload_ctl must be 0 or 1\n");
  68821. + return -DWC_E_INVALID;
  68822. + }
  68823. +
  68824. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  68825. + valid = 0;
  68826. + }
  68827. + if (valid == 0) {
  68828. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  68829. + DWC_ERROR("%d invalid for parameter reload_ctl."
  68830. + "Check HW configuration.\n", val);
  68831. + }
  68832. + retval = -DWC_E_INVALID;
  68833. + val = 0;
  68834. + }
  68835. + core_if->core_params->reload_ctl = val;
  68836. + return retval;
  68837. +}
  68838. +
  68839. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  68840. +{
  68841. + return core_if->core_params->reload_ctl;
  68842. +}
  68843. +
  68844. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  68845. +{
  68846. + int retval = 0;
  68847. + int valid = 1;
  68848. +
  68849. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68850. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  68851. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  68852. + return -DWC_E_INVALID;
  68853. + }
  68854. +
  68855. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  68856. + !(core_if->core_params->dma_desc_enable))) {
  68857. + valid = 0;
  68858. + }
  68859. + if (valid == 0) {
  68860. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  68861. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  68862. + "Check HW configuration.\n", val);
  68863. + }
  68864. + retval = -DWC_E_INVALID;
  68865. + val = 0;
  68866. + }
  68867. + core_if->core_params->dev_out_nak = val;
  68868. + return retval;
  68869. +}
  68870. +
  68871. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  68872. +{
  68873. + return core_if->core_params->dev_out_nak;
  68874. +}
  68875. +
  68876. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  68877. +{
  68878. + int retval = 0;
  68879. + int valid = 1;
  68880. +
  68881. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68882. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  68883. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  68884. + return -DWC_E_INVALID;
  68885. + }
  68886. +
  68887. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  68888. + !(core_if->core_params->dma_desc_enable))) {
  68889. + valid = 0;
  68890. + }
  68891. + if (valid == 0) {
  68892. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  68893. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  68894. + "Check HW configuration.\n", val);
  68895. + }
  68896. + retval = -DWC_E_INVALID;
  68897. + val = 0;
  68898. + }
  68899. + core_if->core_params->cont_on_bna = val;
  68900. + return retval;
  68901. +}
  68902. +
  68903. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  68904. +{
  68905. + return core_if->core_params->cont_on_bna;
  68906. +}
  68907. +
  68908. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  68909. +{
  68910. + int retval = 0;
  68911. + int valid = 1;
  68912. +
  68913. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68914. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  68915. + DWC_WARN("ahb_single must be 0 or 1\n");
  68916. + return -DWC_E_INVALID;
  68917. + }
  68918. +
  68919. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  68920. + valid = 0;
  68921. + }
  68922. + if (valid == 0) {
  68923. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  68924. + DWC_ERROR("%d invalid for parameter ahb_single."
  68925. + "Check HW configuration.\n", val);
  68926. + }
  68927. + retval = -DWC_E_INVALID;
  68928. + val = 0;
  68929. + }
  68930. + core_if->core_params->ahb_single = val;
  68931. + return retval;
  68932. +}
  68933. +
  68934. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  68935. +{
  68936. + return core_if->core_params->ahb_single;
  68937. +}
  68938. +
  68939. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  68940. +{
  68941. + int retval = 0;
  68942. +
  68943. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  68944. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  68945. + DWC_WARN
  68946. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  68947. + return -DWC_E_INVALID;
  68948. + }
  68949. +
  68950. + core_if->core_params->otg_ver = val;
  68951. + return retval;
  68952. +}
  68953. +
  68954. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  68955. +{
  68956. + return core_if->core_params->otg_ver;
  68957. +}
  68958. +
  68959. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  68960. +{
  68961. + gotgctl_data_t otgctl;
  68962. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  68963. + return otgctl.b.hstnegscs;
  68964. +}
  68965. +
  68966. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  68967. +{
  68968. + gotgctl_data_t otgctl;
  68969. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  68970. + return otgctl.b.sesreqscs;
  68971. +}
  68972. +
  68973. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  68974. +{
  68975. + if(core_if->otg_ver == 0) {
  68976. + gotgctl_data_t otgctl;
  68977. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  68978. + otgctl.b.hnpreq = val;
  68979. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  68980. + } else {
  68981. + core_if->otg_sts = val;
  68982. + }
  68983. +}
  68984. +
  68985. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  68986. +{
  68987. + return core_if->snpsid;
  68988. +}
  68989. +
  68990. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  68991. +{
  68992. + gintsts_data_t gintsts;
  68993. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  68994. + return gintsts.b.curmode;
  68995. +}
  68996. +
  68997. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  68998. +{
  68999. + gusbcfg_data_t usbcfg;
  69000. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  69001. + return usbcfg.b.hnpcap;
  69002. +}
  69003. +
  69004. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  69005. +{
  69006. + gusbcfg_data_t usbcfg;
  69007. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  69008. + usbcfg.b.hnpcap = val;
  69009. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  69010. +}
  69011. +
  69012. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  69013. +{
  69014. + gusbcfg_data_t usbcfg;
  69015. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  69016. + return usbcfg.b.srpcap;
  69017. +}
  69018. +
  69019. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  69020. +{
  69021. + gusbcfg_data_t usbcfg;
  69022. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  69023. + usbcfg.b.srpcap = val;
  69024. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  69025. +}
  69026. +
  69027. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  69028. +{
  69029. + dcfg_data_t dcfg;
  69030. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  69031. +
  69032. + dcfg.d32 = -1; //GRAYG
  69033. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  69034. + if (NULL == core_if)
  69035. + DWC_ERROR("reg request with NULL core_if\n");
  69036. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  69037. + core_if, core_if->dev_if);
  69038. + if (NULL == core_if->dev_if)
  69039. + DWC_ERROR("reg request with NULL dev_if\n");
  69040. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  69041. + "dev_global_regs(%p)\n", __func__,
  69042. + core_if, core_if->dev_if,
  69043. + core_if->dev_if->dev_global_regs);
  69044. + if (NULL == core_if->dev_if->dev_global_regs)
  69045. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  69046. + else {
  69047. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  69048. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  69049. + core_if, core_if->dev_if,
  69050. + core_if->dev_if->dev_global_regs,
  69051. + &core_if->dev_if->dev_global_regs->dcfg);
  69052. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  69053. + }
  69054. + return dcfg.b.devspd;
  69055. +}
  69056. +
  69057. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  69058. +{
  69059. + dcfg_data_t dcfg;
  69060. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  69061. + dcfg.b.devspd = val;
  69062. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  69063. +}
  69064. +
  69065. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  69066. +{
  69067. + hprt0_data_t hprt0;
  69068. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  69069. + return hprt0.b.prtconnsts;
  69070. +}
  69071. +
  69072. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  69073. +{
  69074. + dsts_data_t dsts;
  69075. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  69076. + return dsts.b.enumspd;
  69077. +}
  69078. +
  69079. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  69080. +{
  69081. + hprt0_data_t hprt0;
  69082. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  69083. + return hprt0.b.prtpwr;
  69084. +
  69085. +}
  69086. +
  69087. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  69088. +{
  69089. + return core_if->hibernation_suspend;
  69090. +}
  69091. +
  69092. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  69093. +{
  69094. + hprt0_data_t hprt0;
  69095. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  69096. + hprt0.b.prtpwr = val;
  69097. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  69098. +}
  69099. +
  69100. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  69101. +{
  69102. + hprt0_data_t hprt0;
  69103. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  69104. + return hprt0.b.prtsusp;
  69105. +
  69106. +}
  69107. +
  69108. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  69109. +{
  69110. + hprt0_data_t hprt0;
  69111. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  69112. + hprt0.b.prtsusp = val;
  69113. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  69114. +}
  69115. +
  69116. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  69117. +{
  69118. + hfir_data_t hfir;
  69119. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  69120. + return hfir.b.frint;
  69121. +
  69122. +}
  69123. +
  69124. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  69125. +{
  69126. + hfir_data_t hfir;
  69127. + uint32_t fram_int;
  69128. + fram_int = calc_frame_interval(core_if);
  69129. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  69130. + if (!core_if->core_params->reload_ctl) {
  69131. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  69132. + "not set to 1.\nShould load driver with reload_ctl=1"
  69133. + " module parameter\n");
  69134. + return;
  69135. + }
  69136. + switch (fram_int) {
  69137. + case 3750:
  69138. + if ((val < 3350) || (val > 4150)) {
  69139. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  69140. + "clock freq should be from 3350 to 4150\n");
  69141. + return;
  69142. + }
  69143. + break;
  69144. + case 30000:
  69145. + if ((val < 26820) || (val > 33180)) {
  69146. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  69147. + "clock freq should be from 26820 to 33180\n");
  69148. + return;
  69149. + }
  69150. + break;
  69151. + case 6000:
  69152. + if ((val < 5360) || (val > 6640)) {
  69153. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  69154. + "clock freq should be from 5360 to 6640\n");
  69155. + return;
  69156. + }
  69157. + break;
  69158. + case 48000:
  69159. + if ((val < 42912) || (val > 53088)) {
  69160. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  69161. + "clock freq should be from 42912 to 53088\n");
  69162. + return;
  69163. + }
  69164. + break;
  69165. + case 7500:
  69166. + if ((val < 6700) || (val > 8300)) {
  69167. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  69168. + "clock freq should be from 6700 to 8300\n");
  69169. + return;
  69170. + }
  69171. + break;
  69172. + case 60000:
  69173. + if ((val < 53640) || (val > 65536)) {
  69174. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  69175. + "clock freq should be from 53640 to 65536\n");
  69176. + return;
  69177. + }
  69178. + break;
  69179. + default:
  69180. + DWC_WARN("Unknown frame interval\n");
  69181. + return;
  69182. + break;
  69183. +
  69184. + }
  69185. + hfir.b.frint = val;
  69186. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  69187. +}
  69188. +
  69189. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  69190. +{
  69191. + hcfg_data_t hcfg;
  69192. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  69193. + return hcfg.b.modechtimen;
  69194. +
  69195. +}
  69196. +
  69197. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  69198. +{
  69199. + hcfg_data_t hcfg;
  69200. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  69201. + hcfg.b.modechtimen = val;
  69202. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  69203. +}
  69204. +
  69205. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  69206. +{
  69207. + hprt0_data_t hprt0;
  69208. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  69209. + hprt0.b.prtres = val;
  69210. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  69211. +}
  69212. +
  69213. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  69214. +{
  69215. + dctl_data_t dctl;
  69216. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  69217. + return dctl.b.rmtwkupsig;
  69218. +}
  69219. +
  69220. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  69221. +{
  69222. + glpmcfg_data_t lpmcfg;
  69223. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69224. +
  69225. + DWC_ASSERT(!
  69226. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  69227. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  69228. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  69229. +
  69230. + return lpmcfg.b.prt_sleep_sts;
  69231. +}
  69232. +
  69233. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  69234. +{
  69235. + glpmcfg_data_t lpmcfg;
  69236. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69237. + return lpmcfg.b.rem_wkup_en;
  69238. +}
  69239. +
  69240. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  69241. +{
  69242. + glpmcfg_data_t lpmcfg;
  69243. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69244. + return lpmcfg.b.appl_resp;
  69245. +}
  69246. +
  69247. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  69248. +{
  69249. + glpmcfg_data_t lpmcfg;
  69250. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69251. + lpmcfg.b.appl_resp = val;
  69252. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  69253. +}
  69254. +
  69255. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  69256. +{
  69257. + glpmcfg_data_t lpmcfg;
  69258. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69259. + return lpmcfg.b.hsic_connect;
  69260. +}
  69261. +
  69262. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  69263. +{
  69264. + glpmcfg_data_t lpmcfg;
  69265. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69266. + lpmcfg.b.hsic_connect = val;
  69267. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  69268. +}
  69269. +
  69270. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  69271. +{
  69272. + glpmcfg_data_t lpmcfg;
  69273. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69274. + return lpmcfg.b.inv_sel_hsic;
  69275. +
  69276. +}
  69277. +
  69278. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  69279. +{
  69280. + glpmcfg_data_t lpmcfg;
  69281. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  69282. + lpmcfg.b.inv_sel_hsic = val;
  69283. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  69284. +}
  69285. +
  69286. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  69287. +{
  69288. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  69289. +}
  69290. +
  69291. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  69292. +{
  69293. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  69294. +}
  69295. +
  69296. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  69297. +{
  69298. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  69299. +}
  69300. +
  69301. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  69302. +{
  69303. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  69304. +}
  69305. +
  69306. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  69307. +{
  69308. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  69309. +}
  69310. +
  69311. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  69312. +{
  69313. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  69314. +}
  69315. +
  69316. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  69317. +{
  69318. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  69319. +}
  69320. +
  69321. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  69322. +{
  69323. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  69324. +}
  69325. +
  69326. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  69327. +{
  69328. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  69329. +}
  69330. +
  69331. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  69332. +{
  69333. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  69334. +}
  69335. +
  69336. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  69337. +{
  69338. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  69339. +}
  69340. +
  69341. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  69342. +{
  69343. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  69344. +}
  69345. +
  69346. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  69347. +{
  69348. + return DWC_READ_REG32(core_if->host_if->hprt0);
  69349. +
  69350. +}
  69351. +
  69352. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  69353. +{
  69354. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  69355. +}
  69356. +
  69357. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  69358. +{
  69359. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  69360. +}
  69361. +
  69362. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  69363. +{
  69364. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  69365. +}
  69366. +
  69367. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  69368. +{
  69369. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  69370. +}
  69371. +
  69372. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  69373. +{
  69374. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  69375. +}
  69376. +
  69377. +/**
  69378. + * Start the SRP timer to detect when the SRP does not complete within
  69379. + * 6 seconds.
  69380. + *
  69381. + * @param core_if the pointer to core_if strucure.
  69382. + */
  69383. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  69384. +{
  69385. + core_if->srp_timer_started = 1;
  69386. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  69387. +}
  69388. +
  69389. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  69390. +{
  69391. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  69392. + gotgctl_data_t mem;
  69393. + gotgctl_data_t val;
  69394. +
  69395. + val.d32 = DWC_READ_REG32(addr);
  69396. + if (val.b.sesreq) {
  69397. + DWC_ERROR("Session Request Already active!\n");
  69398. + return;
  69399. + }
  69400. +
  69401. + DWC_INFO("Session Request Initated\n"); //NOTICE
  69402. + mem.d32 = DWC_READ_REG32(addr);
  69403. + mem.b.sesreq = 1;
  69404. + DWC_WRITE_REG32(addr, mem.d32);
  69405. +
  69406. + /* Start the SRP timer */
  69407. + dwc_otg_pcd_start_srp_timer(core_if);
  69408. + return;
  69409. +}
  69410. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cil.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  69411. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cil.h 1970-01-01 01:00:00.000000000 +0100
  69412. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil.h 2014-09-14 19:04:13.000000000 +0200
  69413. @@ -0,0 +1,1464 @@
  69414. +/* ==========================================================================
  69415. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  69416. + * $Revision: #123 $
  69417. + * $Date: 2012/08/10 $
  69418. + * $Change: 2047372 $
  69419. + *
  69420. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  69421. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  69422. + * otherwise expressly agreed to in writing between Synopsys and you.
  69423. + *
  69424. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  69425. + * any End User Software License Agreement or Agreement for Licensed Product
  69426. + * with Synopsys or any supplement thereto. You are permitted to use and
  69427. + * redistribute this Software in source and binary forms, with or without
  69428. + * modification, provided that redistributions of source code must retain this
  69429. + * notice. You may not view, use, disclose, copy or distribute this file or
  69430. + * any information contained herein except pursuant to this license grant from
  69431. + * Synopsys. If you do not agree with this notice, including the disclaimer
  69432. + * below, then you are not authorized to use the Software.
  69433. + *
  69434. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  69435. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  69436. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  69437. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  69438. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  69439. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  69440. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  69441. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  69442. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  69443. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  69444. + * DAMAGE.
  69445. + * ========================================================================== */
  69446. +
  69447. +#if !defined(__DWC_CIL_H__)
  69448. +#define __DWC_CIL_H__
  69449. +
  69450. +#include "dwc_list.h"
  69451. +#include "dwc_otg_dbg.h"
  69452. +#include "dwc_otg_regs.h"
  69453. +
  69454. +#include "dwc_otg_core_if.h"
  69455. +#include "dwc_otg_adp.h"
  69456. +
  69457. +/**
  69458. + * @file
  69459. + * This file contains the interface to the Core Interface Layer.
  69460. + */
  69461. +
  69462. +#ifdef DWC_UTE_CFI
  69463. +
  69464. +#define MAX_DMA_DESCS_PER_EP 256
  69465. +
  69466. +/**
  69467. + * Enumeration for the data buffer mode
  69468. + */
  69469. +typedef enum _data_buffer_mode {
  69470. + BM_STANDARD = 0, /* data buffer is in normal mode */
  69471. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  69472. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  69473. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  69474. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  69475. +} data_buffer_mode_e;
  69476. +#endif //DWC_UTE_CFI
  69477. +
  69478. +/** Macros defined for DWC OTG HW Release version */
  69479. +
  69480. +#define OTG_CORE_REV_2_60a 0x4F54260A
  69481. +#define OTG_CORE_REV_2_71a 0x4F54271A
  69482. +#define OTG_CORE_REV_2_72a 0x4F54272A
  69483. +#define OTG_CORE_REV_2_80a 0x4F54280A
  69484. +#define OTG_CORE_REV_2_81a 0x4F54281A
  69485. +#define OTG_CORE_REV_2_90a 0x4F54290A
  69486. +#define OTG_CORE_REV_2_91a 0x4F54291A
  69487. +#define OTG_CORE_REV_2_92a 0x4F54292A
  69488. +#define OTG_CORE_REV_2_93a 0x4F54293A
  69489. +#define OTG_CORE_REV_2_94a 0x4F54294A
  69490. +#define OTG_CORE_REV_3_00a 0x4F54300A
  69491. +
  69492. +/**
  69493. + * Information for each ISOC packet.
  69494. + */
  69495. +typedef struct iso_pkt_info {
  69496. + uint32_t offset;
  69497. + uint32_t length;
  69498. + int32_t status;
  69499. +} iso_pkt_info_t;
  69500. +
  69501. +/**
  69502. + * The <code>dwc_ep</code> structure represents the state of a single
  69503. + * endpoint when acting in device mode. It contains the data items
  69504. + * needed for an endpoint to be activated and transfer packets.
  69505. + */
  69506. +typedef struct dwc_ep {
  69507. + /** EP number used for register address lookup */
  69508. + uint8_t num;
  69509. + /** EP direction 0 = OUT */
  69510. + unsigned is_in:1;
  69511. + /** EP active. */
  69512. + unsigned active:1;
  69513. +
  69514. + /**
  69515. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  69516. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  69517. + unsigned tx_fifo_num:4;
  69518. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  69519. + unsigned type:2;
  69520. +#define DWC_OTG_EP_TYPE_CONTROL 0
  69521. +#define DWC_OTG_EP_TYPE_ISOC 1
  69522. +#define DWC_OTG_EP_TYPE_BULK 2
  69523. +#define DWC_OTG_EP_TYPE_INTR 3
  69524. +
  69525. + /** DATA start PID for INTR and BULK EP */
  69526. + unsigned data_pid_start:1;
  69527. + /** Frame (even/odd) for ISOC EP */
  69528. + unsigned even_odd_frame:1;
  69529. + /** Max Packet bytes */
  69530. + unsigned maxpacket:11;
  69531. +
  69532. + /** Max Transfer size */
  69533. + uint32_t maxxfer;
  69534. +
  69535. + /** @name Transfer state */
  69536. + /** @{ */
  69537. +
  69538. + /**
  69539. + * Pointer to the beginning of the transfer buffer -- do not modify
  69540. + * during transfer.
  69541. + */
  69542. +
  69543. + dwc_dma_t dma_addr;
  69544. +
  69545. + dwc_dma_t dma_desc_addr;
  69546. + dwc_otg_dev_dma_desc_t *desc_addr;
  69547. +
  69548. + uint8_t *start_xfer_buff;
  69549. + /** pointer to the transfer buffer */
  69550. + uint8_t *xfer_buff;
  69551. + /** Number of bytes to transfer */
  69552. + unsigned xfer_len:19;
  69553. + /** Number of bytes transferred. */
  69554. + unsigned xfer_count:19;
  69555. + /** Sent ZLP */
  69556. + unsigned sent_zlp:1;
  69557. + /** Total len for control transfer */
  69558. + unsigned total_len:19;
  69559. +
  69560. + /** stall clear flag */
  69561. + unsigned stall_clear_flag:1;
  69562. +
  69563. + /** SETUP pkt cnt rollover flag for EP0 out*/
  69564. + unsigned stp_rollover;
  69565. +
  69566. +#ifdef DWC_UTE_CFI
  69567. + /* The buffer mode */
  69568. + data_buffer_mode_e buff_mode;
  69569. +
  69570. + /* The chain of DMA descriptors.
  69571. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  69572. + */
  69573. + dwc_otg_dma_desc_t *descs;
  69574. +
  69575. + /* The DMA address of the descriptors chain start */
  69576. + dma_addr_t descs_dma_addr;
  69577. + /** This variable stores the length of the last enqueued request */
  69578. + uint32_t cfi_req_len;
  69579. +#endif //DWC_UTE_CFI
  69580. +
  69581. +/** Max DMA Descriptor count for any EP */
  69582. +#define MAX_DMA_DESC_CNT 256
  69583. + /** Allocated DMA Desc count */
  69584. + uint32_t desc_cnt;
  69585. +
  69586. + /** bInterval */
  69587. + uint32_t bInterval;
  69588. + /** Next frame num to setup next ISOC transfer */
  69589. + uint32_t frame_num;
  69590. + /** Indicates SOF number overrun in DSTS */
  69591. + uint8_t frm_overrun;
  69592. +
  69593. +#ifdef DWC_UTE_PER_IO
  69594. + /** Next frame num for which will be setup DMA Desc */
  69595. + uint32_t xiso_frame_num;
  69596. + /** bInterval */
  69597. + uint32_t xiso_bInterval;
  69598. + /** Count of currently active transfers - shall be either 0 or 1 */
  69599. + int xiso_active_xfers;
  69600. + int xiso_queued_xfers;
  69601. +#endif
  69602. +#ifdef DWC_EN_ISOC
  69603. + /**
  69604. + * Variables specific for ISOC EPs
  69605. + *
  69606. + */
  69607. + /** DMA addresses of ISOC buffers */
  69608. + dwc_dma_t dma_addr0;
  69609. + dwc_dma_t dma_addr1;
  69610. +
  69611. + dwc_dma_t iso_dma_desc_addr;
  69612. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  69613. +
  69614. + /** pointer to the transfer buffers */
  69615. + uint8_t *xfer_buff0;
  69616. + uint8_t *xfer_buff1;
  69617. +
  69618. + /** number of ISOC Buffer is processing */
  69619. + uint32_t proc_buf_num;
  69620. + /** Interval of ISOC Buffer processing */
  69621. + uint32_t buf_proc_intrvl;
  69622. + /** Data size for regular frame */
  69623. + uint32_t data_per_frame;
  69624. +
  69625. + /* todo - pattern data support is to be implemented in the future */
  69626. + /** Data size for pattern frame */
  69627. + uint32_t data_pattern_frame;
  69628. + /** Frame number of pattern data */
  69629. + uint32_t sync_frame;
  69630. +
  69631. + /** bInterval */
  69632. + uint32_t bInterval;
  69633. + /** ISO Packet number per frame */
  69634. + uint32_t pkt_per_frm;
  69635. + /** Next frame num for which will be setup DMA Desc */
  69636. + uint32_t next_frame;
  69637. + /** Number of packets per buffer processing */
  69638. + uint32_t pkt_cnt;
  69639. + /** Info for all isoc packets */
  69640. + iso_pkt_info_t *pkt_info;
  69641. + /** current pkt number */
  69642. + uint32_t cur_pkt;
  69643. + /** current pkt number */
  69644. + uint8_t *cur_pkt_addr;
  69645. + /** current pkt number */
  69646. + uint32_t cur_pkt_dma_addr;
  69647. +#endif /* DWC_EN_ISOC */
  69648. +
  69649. +/** @} */
  69650. +} dwc_ep_t;
  69651. +
  69652. +/*
  69653. + * Reasons for halting a host channel.
  69654. + */
  69655. +typedef enum dwc_otg_halt_status {
  69656. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  69657. + DWC_OTG_HC_XFER_COMPLETE,
  69658. + DWC_OTG_HC_XFER_URB_COMPLETE,
  69659. + DWC_OTG_HC_XFER_ACK,
  69660. + DWC_OTG_HC_XFER_NAK,
  69661. + DWC_OTG_HC_XFER_NYET,
  69662. + DWC_OTG_HC_XFER_STALL,
  69663. + DWC_OTG_HC_XFER_XACT_ERR,
  69664. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  69665. + DWC_OTG_HC_XFER_BABBLE_ERR,
  69666. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  69667. + DWC_OTG_HC_XFER_AHB_ERR,
  69668. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  69669. + DWC_OTG_HC_XFER_URB_DEQUEUE
  69670. +} dwc_otg_halt_status_e;
  69671. +
  69672. +/**
  69673. + * Host channel descriptor. This structure represents the state of a single
  69674. + * host channel when acting in host mode. It contains the data items needed to
  69675. + * transfer packets to an endpoint via a host channel.
  69676. + */
  69677. +typedef struct dwc_hc {
  69678. + /** Host channel number used for register address lookup */
  69679. + uint8_t hc_num;
  69680. +
  69681. + /** Device to access */
  69682. + unsigned dev_addr:7;
  69683. +
  69684. + /** EP to access */
  69685. + unsigned ep_num:4;
  69686. +
  69687. + /** EP direction. 0: OUT, 1: IN */
  69688. + unsigned ep_is_in:1;
  69689. +
  69690. + /**
  69691. + * EP speed.
  69692. + * One of the following values:
  69693. + * - DWC_OTG_EP_SPEED_LOW
  69694. + * - DWC_OTG_EP_SPEED_FULL
  69695. + * - DWC_OTG_EP_SPEED_HIGH
  69696. + */
  69697. + unsigned speed:2;
  69698. +#define DWC_OTG_EP_SPEED_LOW 0
  69699. +#define DWC_OTG_EP_SPEED_FULL 1
  69700. +#define DWC_OTG_EP_SPEED_HIGH 2
  69701. +
  69702. + /**
  69703. + * Endpoint type.
  69704. + * One of the following values:
  69705. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  69706. + * - DWC_OTG_EP_TYPE_ISOC: 1
  69707. + * - DWC_OTG_EP_TYPE_BULK: 2
  69708. + * - DWC_OTG_EP_TYPE_INTR: 3
  69709. + */
  69710. + unsigned ep_type:2;
  69711. +
  69712. + /** Max packet size in bytes */
  69713. + unsigned max_packet:11;
  69714. +
  69715. + /**
  69716. + * PID for initial transaction.
  69717. + * 0: DATA0,<br>
  69718. + * 1: DATA2,<br>
  69719. + * 2: DATA1,<br>
  69720. + * 3: MDATA (non-Control EP),
  69721. + * SETUP (Control EP)
  69722. + */
  69723. + unsigned data_pid_start:2;
  69724. +#define DWC_OTG_HC_PID_DATA0 0
  69725. +#define DWC_OTG_HC_PID_DATA2 1
  69726. +#define DWC_OTG_HC_PID_DATA1 2
  69727. +#define DWC_OTG_HC_PID_MDATA 3
  69728. +#define DWC_OTG_HC_PID_SETUP 3
  69729. +
  69730. + /** Number of periodic transactions per (micro)frame */
  69731. + unsigned multi_count:2;
  69732. +
  69733. + /** @name Transfer State */
  69734. + /** @{ */
  69735. +
  69736. + /** Pointer to the current transfer buffer position. */
  69737. + uint8_t *xfer_buff;
  69738. + /**
  69739. + * In Buffer DMA mode this buffer will be used
  69740. + * if xfer_buff is not DWORD aligned.
  69741. + */
  69742. + dwc_dma_t align_buff;
  69743. + /** Total number of bytes to transfer. */
  69744. + uint32_t xfer_len;
  69745. + /** Number of bytes transferred so far. */
  69746. + uint32_t xfer_count;
  69747. + /** Packet count at start of transfer.*/
  69748. + uint16_t start_pkt_count;
  69749. +
  69750. + /**
  69751. + * Flag to indicate whether the transfer has been started. Set to 1 if
  69752. + * it has been started, 0 otherwise.
  69753. + */
  69754. + uint8_t xfer_started;
  69755. +
  69756. + /**
  69757. + * Set to 1 to indicate that a PING request should be issued on this
  69758. + * channel. If 0, process normally.
  69759. + */
  69760. + uint8_t do_ping;
  69761. +
  69762. + /**
  69763. + * Set to 1 to indicate that the error count for this transaction is
  69764. + * non-zero. Set to 0 if the error count is 0.
  69765. + */
  69766. + uint8_t error_state;
  69767. +
  69768. + /**
  69769. + * Set to 1 to indicate that this channel should be halted the next
  69770. + * time a request is queued for the channel. This is necessary in
  69771. + * slave mode if no request queue space is available when an attempt
  69772. + * is made to halt the channel.
  69773. + */
  69774. + uint8_t halt_on_queue;
  69775. +
  69776. + /**
  69777. + * Set to 1 if the host channel has been halted, but the core is not
  69778. + * finished flushing queued requests. Otherwise 0.
  69779. + */
  69780. + uint8_t halt_pending;
  69781. +
  69782. + /**
  69783. + * Reason for halting the host channel.
  69784. + */
  69785. + dwc_otg_halt_status_e halt_status;
  69786. +
  69787. + /*
  69788. + * Split settings for the host channel
  69789. + */
  69790. + uint8_t do_split; /**< Enable split for the channel */
  69791. + uint8_t complete_split; /**< Enable complete split */
  69792. + uint8_t hub_addr; /**< Address of high speed hub */
  69793. +
  69794. + uint8_t port_addr; /**< Port of the low/full speed device */
  69795. + /** Split transaction position
  69796. + * One of the following values:
  69797. + * - DWC_HCSPLIT_XACTPOS_MID
  69798. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  69799. + * - DWC_HCSPLIT_XACTPOS_END
  69800. + * - DWC_HCSPLIT_XACTPOS_ALL */
  69801. + uint8_t xact_pos;
  69802. +
  69803. + /** Set when the host channel does a short read. */
  69804. + uint8_t short_read;
  69805. +
  69806. + /**
  69807. + * Number of requests issued for this channel since it was assigned to
  69808. + * the current transfer (not counting PINGs).
  69809. + */
  69810. + uint8_t requests;
  69811. +
  69812. + /**
  69813. + * Queue Head for the transfer being processed by this channel.
  69814. + */
  69815. + struct dwc_otg_qh *qh;
  69816. +
  69817. + /** @} */
  69818. +
  69819. + /** Entry in list of host channels. */
  69820. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  69821. +
  69822. + /** @name Descriptor DMA support */
  69823. + /** @{ */
  69824. +
  69825. + /** Number of Transfer Descriptors */
  69826. + uint16_t ntd;
  69827. +
  69828. + /** Descriptor List DMA address */
  69829. + dwc_dma_t desc_list_addr;
  69830. +
  69831. + /** Scheduling micro-frame bitmap. */
  69832. + uint8_t schinfo;
  69833. +
  69834. + /** @} */
  69835. +} dwc_hc_t;
  69836. +
  69837. +/**
  69838. + * The following parameters may be specified when starting the module. These
  69839. + * parameters define how the DWC_otg controller should be configured.
  69840. + */
  69841. +typedef struct dwc_otg_core_params {
  69842. + int32_t opt;
  69843. +
  69844. + /**
  69845. + * Specifies the OTG capabilities. The driver will automatically
  69846. + * detect the value for this parameter if none is specified.
  69847. + * 0 - HNP and SRP capable (default)
  69848. + * 1 - SRP Only capable
  69849. + * 2 - No HNP/SRP capable
  69850. + */
  69851. + int32_t otg_cap;
  69852. +
  69853. + /**
  69854. + * Specifies whether to use slave or DMA mode for accessing the data
  69855. + * FIFOs. The driver will automatically detect the value for this
  69856. + * parameter if none is specified.
  69857. + * 0 - Slave
  69858. + * 1 - DMA (default, if available)
  69859. + */
  69860. + int32_t dma_enable;
  69861. +
  69862. + /**
  69863. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  69864. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  69865. + * will automatically detect the value for this if none is specified.
  69866. + * 0 - address DMA
  69867. + * 1 - DMA Descriptor(default, if available)
  69868. + */
  69869. + int32_t dma_desc_enable;
  69870. + /** The DMA Burst size (applicable only for External DMA
  69871. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  69872. + */
  69873. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  69874. +
  69875. + /**
  69876. + * Specifies the maximum speed of operation in host and device mode.
  69877. + * The actual speed depends on the speed of the attached device and
  69878. + * the value of phy_type. The actual speed depends on the speed of the
  69879. + * attached device.
  69880. + * 0 - High Speed (default)
  69881. + * 1 - Full Speed
  69882. + */
  69883. + int32_t speed;
  69884. + /** Specifies whether low power mode is supported when attached
  69885. + * to a Full Speed or Low Speed device in host mode.
  69886. + * 0 - Don't support low power mode (default)
  69887. + * 1 - Support low power mode
  69888. + */
  69889. + int32_t host_support_fs_ls_low_power;
  69890. +
  69891. + /** Specifies the PHY clock rate in low power mode when connected to a
  69892. + * Low Speed device in host mode. This parameter is applicable only if
  69893. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  69894. + * then defaults to 6 MHZ otherwise 48 MHZ.
  69895. + *
  69896. + * 0 - 48 MHz
  69897. + * 1 - 6 MHz
  69898. + */
  69899. + int32_t host_ls_low_power_phy_clk;
  69900. +
  69901. + /**
  69902. + * 0 - Use cC FIFO size parameters
  69903. + * 1 - Allow dynamic FIFO sizing (default)
  69904. + */
  69905. + int32_t enable_dynamic_fifo;
  69906. +
  69907. + /** Total number of 4-byte words in the data FIFO memory. This
  69908. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  69909. + * Tx FIFOs.
  69910. + * 32 to 32768 (default 8192)
  69911. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  69912. + */
  69913. + int32_t data_fifo_size;
  69914. +
  69915. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  69916. + * FIFO sizing is enabled.
  69917. + * 16 to 32768 (default 1064)
  69918. + */
  69919. + int32_t dev_rx_fifo_size;
  69920. +
  69921. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  69922. + * when dynamic FIFO sizing is enabled.
  69923. + * 16 to 32768 (default 1024)
  69924. + */
  69925. + int32_t dev_nperio_tx_fifo_size;
  69926. +
  69927. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  69928. + * mode when dynamic FIFO sizing is enabled.
  69929. + * 4 to 768 (default 256)
  69930. + */
  69931. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  69932. +
  69933. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  69934. + * FIFO sizing is enabled.
  69935. + * 16 to 32768 (default 1024)
  69936. + */
  69937. + int32_t host_rx_fifo_size;
  69938. +
  69939. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  69940. + * when Dynamic FIFO sizing is enabled in the core.
  69941. + * 16 to 32768 (default 1024)
  69942. + */
  69943. + int32_t host_nperio_tx_fifo_size;
  69944. +
  69945. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  69946. + * FIFO sizing is enabled.
  69947. + * 16 to 32768 (default 1024)
  69948. + */
  69949. + int32_t host_perio_tx_fifo_size;
  69950. +
  69951. + /** The maximum transfer size supported in bytes.
  69952. + * 2047 to 65,535 (default 65,535)
  69953. + */
  69954. + int32_t max_transfer_size;
  69955. +
  69956. + /** The maximum number of packets in a transfer.
  69957. + * 15 to 511 (default 511)
  69958. + */
  69959. + int32_t max_packet_count;
  69960. +
  69961. + /** The number of host channel registers to use.
  69962. + * 1 to 16 (default 12)
  69963. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  69964. + */
  69965. + int32_t host_channels;
  69966. +
  69967. + /** The number of endpoints in addition to EP0 available for device
  69968. + * mode operations.
  69969. + * 1 to 15 (default 6 IN and OUT)
  69970. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  69971. + * endpoints in addition to EP0.
  69972. + */
  69973. + int32_t dev_endpoints;
  69974. +
  69975. + /**
  69976. + * Specifies the type of PHY interface to use. By default, the driver
  69977. + * will automatically detect the phy_type.
  69978. + *
  69979. + * 0 - Full Speed PHY
  69980. + * 1 - UTMI+ (default)
  69981. + * 2 - ULPI
  69982. + */
  69983. + int32_t phy_type;
  69984. +
  69985. + /**
  69986. + * Specifies the UTMI+ Data Width. This parameter is
  69987. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  69988. + * PHY_TYPE, this parameter indicates the data width between
  69989. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  69990. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  69991. + * to "8 and 16 bits", meaning that the core has been
  69992. + * configured to work at either data path width.
  69993. + *
  69994. + * 8 or 16 bits (default 16)
  69995. + */
  69996. + int32_t phy_utmi_width;
  69997. +
  69998. + /**
  69999. + * Specifies whether the ULPI operates at double or single
  70000. + * data rate. This parameter is only applicable if PHY_TYPE is
  70001. + * ULPI.
  70002. + *
  70003. + * 0 - single data rate ULPI interface with 8 bit wide data
  70004. + * bus (default)
  70005. + * 1 - double data rate ULPI interface with 4 bit wide data
  70006. + * bus
  70007. + */
  70008. + int32_t phy_ulpi_ddr;
  70009. +
  70010. + /**
  70011. + * Specifies whether to use the internal or external supply to
  70012. + * drive the vbus with a ULPI phy.
  70013. + */
  70014. + int32_t phy_ulpi_ext_vbus;
  70015. +
  70016. + /**
  70017. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  70018. + * parameter is only applicable if PHY_TYPE is FS.
  70019. + * 0 - No (default)
  70020. + * 1 - Yes
  70021. + */
  70022. + int32_t i2c_enable;
  70023. +
  70024. + int32_t ulpi_fs_ls;
  70025. +
  70026. + int32_t ts_dline;
  70027. +
  70028. + /**
  70029. + * Specifies whether dedicated transmit FIFOs are
  70030. + * enabled for non periodic IN endpoints in device mode
  70031. + * 0 - No
  70032. + * 1 - Yes
  70033. + */
  70034. + int32_t en_multiple_tx_fifo;
  70035. +
  70036. + /** Number of 4-byte words in each of the Tx FIFOs in device
  70037. + * mode when dynamic FIFO sizing is enabled.
  70038. + * 4 to 768 (default 256)
  70039. + */
  70040. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  70041. +
  70042. + /** Thresholding enable flag-
  70043. + * bit 0 - enable non-ISO Tx thresholding
  70044. + * bit 1 - enable ISO Tx thresholding
  70045. + * bit 2 - enable Rx thresholding
  70046. + */
  70047. + uint32_t thr_ctl;
  70048. +
  70049. + /** Thresholding length for Tx
  70050. + * FIFOs in 32 bit DWORDs
  70051. + */
  70052. + uint32_t tx_thr_length;
  70053. +
  70054. + /** Thresholding length for Rx
  70055. + * FIFOs in 32 bit DWORDs
  70056. + */
  70057. + uint32_t rx_thr_length;
  70058. +
  70059. + /**
  70060. + * Specifies whether LPM (Link Power Management) support is enabled
  70061. + */
  70062. + int32_t lpm_enable;
  70063. +
  70064. + /** Per Transfer Interrupt
  70065. + * mode enable flag
  70066. + * 1 - Enabled
  70067. + * 0 - Disabled
  70068. + */
  70069. + int32_t pti_enable;
  70070. +
  70071. + /** Multi Processor Interrupt
  70072. + * mode enable flag
  70073. + * 1 - Enabled
  70074. + * 0 - Disabled
  70075. + */
  70076. + int32_t mpi_enable;
  70077. +
  70078. + /** IS_USB Capability
  70079. + * 1 - Enabled
  70080. + * 0 - Disabled
  70081. + */
  70082. + int32_t ic_usb_cap;
  70083. +
  70084. + /** AHB Threshold Ratio
  70085. + * 2'b00 AHB Threshold = MAC Threshold
  70086. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  70087. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  70088. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  70089. + */
  70090. + int32_t ahb_thr_ratio;
  70091. +
  70092. + /** ADP Support
  70093. + * 1 - Enabled
  70094. + * 0 - Disabled
  70095. + */
  70096. + int32_t adp_supp_enable;
  70097. +
  70098. + /** HFIR Reload Control
  70099. + * 0 - The HFIR cannot be reloaded dynamically.
  70100. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  70101. + */
  70102. + int32_t reload_ctl;
  70103. +
  70104. + /** DCFG: Enable device Out NAK
  70105. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  70106. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  70107. + */
  70108. + int32_t dev_out_nak;
  70109. +
  70110. + /** DCFG: Enable Continue on BNA
  70111. + * After receiving BNA interrupt the core disables the endpoint,when the
  70112. + * endpoint is re-enabled by the application the core starts processing
  70113. + * 0 - from the DOEPDMA descriptor
  70114. + * 1 - from the descriptor which received the BNA.
  70115. + */
  70116. + int32_t cont_on_bna;
  70117. +
  70118. + /** GAHBCFG: AHB Single Support
  70119. + * This bit when programmed supports SINGLE transfers for remainder
  70120. + * data in a transfer for DMA mode of operation.
  70121. + * 0 - in this case the remainder data will be sent using INCR burst size.
  70122. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  70123. + */
  70124. + int32_t ahb_single;
  70125. +
  70126. + /** Core Power down mode
  70127. + * 0 - No Power Down is enabled
  70128. + * 1 - Reserved
  70129. + * 2 - Complete Power Down (Hibernation)
  70130. + */
  70131. + int32_t power_down;
  70132. +
  70133. + /** OTG revision supported
  70134. + * 0 - OTG 1.3 revision
  70135. + * 1 - OTG 2.0 revision
  70136. + */
  70137. + int32_t otg_ver;
  70138. +
  70139. +} dwc_otg_core_params_t;
  70140. +
  70141. +#ifdef DEBUG
  70142. +struct dwc_otg_core_if;
  70143. +typedef struct hc_xfer_info {
  70144. + struct dwc_otg_core_if *core_if;
  70145. + dwc_hc_t *hc;
  70146. +} hc_xfer_info_t;
  70147. +#endif
  70148. +
  70149. +typedef struct ep_xfer_info {
  70150. + struct dwc_otg_core_if *core_if;
  70151. + dwc_ep_t *ep;
  70152. + uint8_t state;
  70153. +} ep_xfer_info_t;
  70154. +/*
  70155. + * Device States
  70156. + */
  70157. +typedef enum dwc_otg_lx_state {
  70158. + /** On state */
  70159. + DWC_OTG_L0,
  70160. + /** LPM sleep state*/
  70161. + DWC_OTG_L1,
  70162. + /** USB suspend state*/
  70163. + DWC_OTG_L2,
  70164. + /** Off state*/
  70165. + DWC_OTG_L3
  70166. +} dwc_otg_lx_state_e;
  70167. +
  70168. +struct dwc_otg_global_regs_backup {
  70169. + uint32_t gotgctl_local;
  70170. + uint32_t gintmsk_local;
  70171. + uint32_t gahbcfg_local;
  70172. + uint32_t gusbcfg_local;
  70173. + uint32_t grxfsiz_local;
  70174. + uint32_t gnptxfsiz_local;
  70175. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70176. + uint32_t glpmcfg_local;
  70177. +#endif
  70178. + uint32_t gi2cctl_local;
  70179. + uint32_t hptxfsiz_local;
  70180. + uint32_t pcgcctl_local;
  70181. + uint32_t gdfifocfg_local;
  70182. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  70183. + uint32_t gpwrdn_local;
  70184. + uint32_t xhib_pcgcctl;
  70185. + uint32_t xhib_gpwrdn;
  70186. +};
  70187. +
  70188. +struct dwc_otg_host_regs_backup {
  70189. + uint32_t hcfg_local;
  70190. + uint32_t haintmsk_local;
  70191. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  70192. + uint32_t hprt0_local;
  70193. + uint32_t hfir_local;
  70194. +};
  70195. +
  70196. +struct dwc_otg_dev_regs_backup {
  70197. + uint32_t dcfg;
  70198. + uint32_t dctl;
  70199. + uint32_t daintmsk;
  70200. + uint32_t diepmsk;
  70201. + uint32_t doepmsk;
  70202. + uint32_t diepctl[MAX_EPS_CHANNELS];
  70203. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  70204. + uint32_t diepdma[MAX_EPS_CHANNELS];
  70205. +};
  70206. +/**
  70207. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  70208. + * the DWC_otg controller acting in either host or device mode. It
  70209. + * represents the programming view of the controller as a whole.
  70210. + */
  70211. +struct dwc_otg_core_if {
  70212. + /** Parameters that define how the core should be configured.*/
  70213. + dwc_otg_core_params_t *core_params;
  70214. +
  70215. + /** Core Global registers starting at offset 000h. */
  70216. + dwc_otg_core_global_regs_t *core_global_regs;
  70217. +
  70218. + /** Device-specific information */
  70219. + dwc_otg_dev_if_t *dev_if;
  70220. + /** Host-specific information */
  70221. + dwc_otg_host_if_t *host_if;
  70222. +
  70223. + /** Value from SNPSID register */
  70224. + uint32_t snpsid;
  70225. +
  70226. + /*
  70227. + * Set to 1 if the core PHY interface bits in USBCFG have been
  70228. + * initialized.
  70229. + */
  70230. + uint8_t phy_init_done;
  70231. +
  70232. + /*
  70233. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  70234. + */
  70235. + uint8_t srp_success;
  70236. + uint8_t srp_timer_started;
  70237. + /** Timer for SRP. If it expires before SRP is successful
  70238. + * clear the SRP. */
  70239. + dwc_timer_t *srp_timer;
  70240. +
  70241. +#ifdef DWC_DEV_SRPCAP
  70242. + /* This timer is needed to power on the hibernated host core if SRP is not
  70243. + * initiated on connected SRP capable device for limited period of time
  70244. + */
  70245. + uint8_t pwron_timer_started;
  70246. + dwc_timer_t *pwron_timer;
  70247. +#endif
  70248. + /* Common configuration information */
  70249. + /** Power and Clock Gating Control Register */
  70250. + volatile uint32_t *pcgcctl;
  70251. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  70252. +
  70253. + /** Push/pop addresses for endpoints or host channels.*/
  70254. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  70255. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  70256. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  70257. +
  70258. + /** Total RAM for FIFOs (Bytes) */
  70259. + uint16_t total_fifo_size;
  70260. + /** Size of Rx FIFO (Bytes) */
  70261. + uint16_t rx_fifo_size;
  70262. + /** Size of Non-periodic Tx FIFO (Bytes) */
  70263. + uint16_t nperio_tx_fifo_size;
  70264. +
  70265. + /** 1 if DMA is enabled, 0 otherwise. */
  70266. + uint8_t dma_enable;
  70267. +
  70268. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  70269. + uint8_t dma_desc_enable;
  70270. +
  70271. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  70272. + uint8_t pti_enh_enable;
  70273. +
  70274. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  70275. + uint8_t multiproc_int_enable;
  70276. +
  70277. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  70278. + uint8_t en_multiple_tx_fifo;
  70279. +
  70280. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  70281. + * process of being queued */
  70282. + uint8_t queuing_high_bandwidth;
  70283. +
  70284. + /** Hardware Configuration -- stored here for convenience.*/
  70285. + hwcfg1_data_t hwcfg1;
  70286. + hwcfg2_data_t hwcfg2;
  70287. + hwcfg3_data_t hwcfg3;
  70288. + hwcfg4_data_t hwcfg4;
  70289. + fifosize_data_t hptxfsiz;
  70290. +
  70291. + /** Host and Device Configuration -- stored here for convenience.*/
  70292. + hcfg_data_t hcfg;
  70293. + dcfg_data_t dcfg;
  70294. +
  70295. + /** The operational State, during transations
  70296. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  70297. + * match the core but allows the software to determine
  70298. + * transitions.
  70299. + */
  70300. + uint8_t op_state;
  70301. +
  70302. + /**
  70303. + * Set to 1 if the HCD needs to be restarted on a session request
  70304. + * interrupt. This is required if no connector ID status change has
  70305. + * occurred since the HCD was last disconnected.
  70306. + */
  70307. + uint8_t restart_hcd_on_session_req;
  70308. +
  70309. + /** HCD callbacks */
  70310. + /** A-Device is a_host */
  70311. +#define A_HOST (1)
  70312. + /** A-Device is a_suspend */
  70313. +#define A_SUSPEND (2)
  70314. + /** A-Device is a_peripherial */
  70315. +#define A_PERIPHERAL (3)
  70316. + /** B-Device is operating as a Peripheral. */
  70317. +#define B_PERIPHERAL (4)
  70318. + /** B-Device is operating as a Host. */
  70319. +#define B_HOST (5)
  70320. +
  70321. + /** HCD callbacks */
  70322. + struct dwc_otg_cil_callbacks *hcd_cb;
  70323. + /** PCD callbacks */
  70324. + struct dwc_otg_cil_callbacks *pcd_cb;
  70325. +
  70326. + /** Device mode Periodic Tx FIFO Mask */
  70327. + uint32_t p_tx_msk;
  70328. + /** Device mode Periodic Tx FIFO Mask */
  70329. + uint32_t tx_msk;
  70330. +
  70331. + /** Workqueue object used for handling several interrupts */
  70332. + dwc_workq_t *wq_otg;
  70333. +
  70334. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  70335. + dwc_timer_t *wkp_timer;
  70336. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  70337. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  70338. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  70339. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  70340. +#ifdef DEBUG
  70341. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  70342. +
  70343. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  70344. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  70345. +
  70346. + uint32_t hfnum_7_samples;
  70347. + uint64_t hfnum_7_frrem_accum;
  70348. + uint32_t hfnum_0_samples;
  70349. + uint64_t hfnum_0_frrem_accum;
  70350. + uint32_t hfnum_other_samples;
  70351. + uint64_t hfnum_other_frrem_accum;
  70352. +#endif
  70353. +
  70354. +#ifdef DWC_UTE_CFI
  70355. + uint16_t pwron_rxfsiz;
  70356. + uint16_t pwron_gnptxfsiz;
  70357. + uint16_t pwron_txfsiz[15];
  70358. +
  70359. + uint16_t init_rxfsiz;
  70360. + uint16_t init_gnptxfsiz;
  70361. + uint16_t init_txfsiz[15];
  70362. +#endif
  70363. +
  70364. + /** Lx state of device */
  70365. + dwc_otg_lx_state_e lx_state;
  70366. +
  70367. + /** Saved Core Global registers */
  70368. + struct dwc_otg_global_regs_backup *gr_backup;
  70369. + /** Saved Host registers */
  70370. + struct dwc_otg_host_regs_backup *hr_backup;
  70371. + /** Saved Device registers */
  70372. + struct dwc_otg_dev_regs_backup *dr_backup;
  70373. +
  70374. + /** Power Down Enable */
  70375. + uint32_t power_down;
  70376. +
  70377. + /** ADP support Enable */
  70378. + uint32_t adp_enable;
  70379. +
  70380. + /** ADP structure object */
  70381. + dwc_otg_adp_t adp;
  70382. +
  70383. + /** hibernation/suspend flag */
  70384. + int hibernation_suspend;
  70385. +
  70386. + /** Device mode extended hibernation flag */
  70387. + int xhib;
  70388. +
  70389. + /** OTG revision supported */
  70390. + uint32_t otg_ver;
  70391. +
  70392. + /** OTG status flag used for HNP polling */
  70393. + uint8_t otg_sts;
  70394. +
  70395. + /** Pointer to either hcd->lock or pcd->lock */
  70396. + dwc_spinlock_t *lock;
  70397. +
  70398. + /** Start predict NextEP based on Learning Queue if equal 1,
  70399. + * also used as counter of disabled NP IN EP's */
  70400. + uint8_t start_predict;
  70401. +
  70402. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  70403. + * active, 0xff otherwise */
  70404. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  70405. +
  70406. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  70407. + uint8_t first_in_nextep_seq;
  70408. +
  70409. + /** Frame number while entering to ISR - needed for ISOCs **/
  70410. + uint32_t frame_num;
  70411. +
  70412. +};
  70413. +
  70414. +#ifdef DEBUG
  70415. +/*
  70416. + * This function is called when transfer is timed out.
  70417. + */
  70418. +extern void hc_xfer_timeout(void *ptr);
  70419. +#endif
  70420. +
  70421. +/*
  70422. + * This function is called when transfer is timed out on endpoint.
  70423. + */
  70424. +extern void ep_xfer_timeout(void *ptr);
  70425. +
  70426. +/*
  70427. + * The following functions are functions for works
  70428. + * using during handling some interrupts
  70429. + */
  70430. +extern void w_conn_id_status_change(void *p);
  70431. +
  70432. +extern void w_wakeup_detected(void *p);
  70433. +
  70434. +/** Saves global register values into system memory. */
  70435. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  70436. +/** Saves device register values into system memory. */
  70437. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  70438. +/** Saves host register values into system memory. */
  70439. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  70440. +/** Restore global register values. */
  70441. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  70442. +/** Restore host register values. */
  70443. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  70444. +/** Restore device register values. */
  70445. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  70446. + int rem_wakeup);
  70447. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  70448. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  70449. + int is_host);
  70450. +
  70451. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  70452. + int restore_mode, int reset);
  70453. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  70454. + int rem_wakeup, int reset);
  70455. +
  70456. +/*
  70457. + * The following functions support initialization of the CIL driver component
  70458. + * and the DWC_otg controller.
  70459. + */
  70460. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  70461. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  70462. +
  70463. +/** @name Device CIL Functions
  70464. + * The following functions support managing the DWC_otg controller in device
  70465. + * mode.
  70466. + */
  70467. +/**@{*/
  70468. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  70469. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  70470. + uint32_t * _dest);
  70471. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  70472. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  70473. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  70474. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  70475. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  70476. + dwc_ep_t * _ep);
  70477. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  70478. + dwc_ep_t * _ep);
  70479. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  70480. + dwc_ep_t * _ep);
  70481. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  70482. + dwc_ep_t * _ep);
  70483. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  70484. + dwc_ep_t * _ep, int _dma);
  70485. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  70486. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  70487. + dwc_ep_t * _ep);
  70488. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  70489. +
  70490. +#ifdef DWC_EN_ISOC
  70491. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  70492. + dwc_ep_t * ep);
  70493. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  70494. + dwc_ep_t * ep);
  70495. +#endif /* DWC_EN_ISOC */
  70496. +/**@}*/
  70497. +
  70498. +/** @name Host CIL Functions
  70499. + * The following functions support managing the DWC_otg controller in host
  70500. + * mode.
  70501. + */
  70502. +/**@{*/
  70503. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  70504. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  70505. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  70506. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  70507. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  70508. + dwc_hc_t * _hc);
  70509. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  70510. + dwc_hc_t * _hc);
  70511. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  70512. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  70513. + dwc_hc_t * _hc);
  70514. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  70515. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  70516. +
  70517. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  70518. + dwc_hc_t * hc);
  70519. +
  70520. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  70521. +
  70522. +/* Macro used to clear one channel interrupt */
  70523. +#define clear_hc_int(_hc_regs_, _intr_) \
  70524. +do { \
  70525. + hcint_data_t hcint_clear = {.d32 = 0}; \
  70526. + hcint_clear.b._intr_ = 1; \
  70527. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  70528. +} while (0)
  70529. +
  70530. +/*
  70531. + * Macro used to disable one channel interrupt. Channel interrupts are
  70532. + * disabled when the channel is halted or released by the interrupt handler.
  70533. + * There is no need to handle further interrupts of that type until the
  70534. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  70535. + * because the channel structures are cleaned up when the channel is released.
  70536. + */
  70537. +#define disable_hc_int(_hc_regs_, _intr_) \
  70538. +do { \
  70539. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  70540. + hcintmsk.b._intr_ = 1; \
  70541. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  70542. +} while (0)
  70543. +
  70544. +/**
  70545. + * This function Reads HPRT0 in preparation to modify. It keeps the
  70546. + * WC bits 0 so that if they are read as 1, they won't clear when you
  70547. + * write it back
  70548. + */
  70549. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  70550. +{
  70551. + hprt0_data_t hprt0;
  70552. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  70553. + hprt0.b.prtena = 0;
  70554. + hprt0.b.prtconndet = 0;
  70555. + hprt0.b.prtenchng = 0;
  70556. + hprt0.b.prtovrcurrchng = 0;
  70557. + return hprt0.d32;
  70558. +}
  70559. +
  70560. +/**@}*/
  70561. +
  70562. +/** @name Common CIL Functions
  70563. + * The following functions support managing the DWC_otg controller in either
  70564. + * device or host mode.
  70565. + */
  70566. +/**@{*/
  70567. +
  70568. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  70569. + uint8_t * dest, uint16_t bytes);
  70570. +
  70571. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  70572. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  70573. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  70574. +
  70575. +/**
  70576. + * This function returns the Core Interrupt register.
  70577. + */
  70578. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  70579. +{
  70580. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  70581. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  70582. +}
  70583. +
  70584. +/**
  70585. + * This function returns the OTG Interrupt register.
  70586. + */
  70587. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  70588. +{
  70589. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  70590. +}
  70591. +
  70592. +/**
  70593. + * This function reads the Device All Endpoints Interrupt register and
  70594. + * returns the IN endpoint interrupt bits.
  70595. + */
  70596. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  70597. + core_if)
  70598. +{
  70599. +
  70600. + uint32_t v;
  70601. +
  70602. + if (core_if->multiproc_int_enable) {
  70603. + v = DWC_READ_REG32(&core_if->dev_if->
  70604. + dev_global_regs->deachint) &
  70605. + DWC_READ_REG32(&core_if->
  70606. + dev_if->dev_global_regs->deachintmsk);
  70607. + } else {
  70608. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  70609. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  70610. + }
  70611. + return (v & 0xffff);
  70612. +}
  70613. +
  70614. +/**
  70615. + * This function reads the Device All Endpoints Interrupt register and
  70616. + * returns the OUT endpoint interrupt bits.
  70617. + */
  70618. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  70619. + core_if)
  70620. +{
  70621. + uint32_t v;
  70622. +
  70623. + if (core_if->multiproc_int_enable) {
  70624. + v = DWC_READ_REG32(&core_if->dev_if->
  70625. + dev_global_regs->deachint) &
  70626. + DWC_READ_REG32(&core_if->
  70627. + dev_if->dev_global_regs->deachintmsk);
  70628. + } else {
  70629. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  70630. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  70631. + }
  70632. +
  70633. + return ((v & 0xffff0000) >> 16);
  70634. +}
  70635. +
  70636. +/**
  70637. + * This function returns the Device IN EP Interrupt register
  70638. + */
  70639. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  70640. + dwc_ep_t * ep)
  70641. +{
  70642. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  70643. + uint32_t v, msk, emp;
  70644. +
  70645. + if (core_if->multiproc_int_enable) {
  70646. + msk =
  70647. + DWC_READ_REG32(&dev_if->
  70648. + dev_global_regs->diepeachintmsk[ep->num]);
  70649. + emp =
  70650. + DWC_READ_REG32(&dev_if->
  70651. + dev_global_regs->dtknqr4_fifoemptymsk);
  70652. + msk |= ((emp >> ep->num) & 0x1) << 7;
  70653. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  70654. + } else {
  70655. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  70656. + emp =
  70657. + DWC_READ_REG32(&dev_if->
  70658. + dev_global_regs->dtknqr4_fifoemptymsk);
  70659. + msk |= ((emp >> ep->num) & 0x1) << 7;
  70660. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  70661. + }
  70662. +
  70663. + return v;
  70664. +}
  70665. +
  70666. +/**
  70667. + * This function returns the Device OUT EP Interrupt register
  70668. + */
  70669. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  70670. + _core_if, dwc_ep_t * _ep)
  70671. +{
  70672. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  70673. + uint32_t v;
  70674. + doepmsk_data_t msk = {.d32 = 0 };
  70675. +
  70676. + if (_core_if->multiproc_int_enable) {
  70677. + msk.d32 =
  70678. + DWC_READ_REG32(&dev_if->
  70679. + dev_global_regs->doepeachintmsk[_ep->num]);
  70680. + if (_core_if->pti_enh_enable) {
  70681. + msk.b.pktdrpsts = 1;
  70682. + }
  70683. + v = DWC_READ_REG32(&dev_if->
  70684. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  70685. + } else {
  70686. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  70687. + if (_core_if->pti_enh_enable) {
  70688. + msk.b.pktdrpsts = 1;
  70689. + }
  70690. + v = DWC_READ_REG32(&dev_if->
  70691. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  70692. + }
  70693. + return v;
  70694. +}
  70695. +
  70696. +/**
  70697. + * This function returns the Host All Channel Interrupt register
  70698. + */
  70699. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  70700. + _core_if)
  70701. +{
  70702. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  70703. +}
  70704. +
  70705. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  70706. + _core_if, dwc_hc_t * _hc)
  70707. +{
  70708. + return (DWC_READ_REG32
  70709. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  70710. +}
  70711. +
  70712. +/**
  70713. + * This function returns the mode of the operation, host or device.
  70714. + *
  70715. + * @return 0 - Device Mode, 1 - Host Mode
  70716. + */
  70717. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  70718. +{
  70719. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  70720. +}
  70721. +
  70722. +/**@}*/
  70723. +
  70724. +/**
  70725. + * DWC_otg CIL callback structure. This structure allows the HCD and
  70726. + * PCD to register functions used for starting and stopping the PCD
  70727. + * and HCD for role change on for a DRD.
  70728. + */
  70729. +typedef struct dwc_otg_cil_callbacks {
  70730. + /** Start function for role change */
  70731. + int (*start) (void *_p);
  70732. + /** Stop Function for role change */
  70733. + int (*stop) (void *_p);
  70734. + /** Disconnect Function for role change */
  70735. + int (*disconnect) (void *_p);
  70736. + /** Resume/Remote wakeup Function */
  70737. + int (*resume_wakeup) (void *_p);
  70738. + /** Suspend function */
  70739. + int (*suspend) (void *_p);
  70740. + /** Session Start (SRP) */
  70741. + int (*session_start) (void *_p);
  70742. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70743. + /** Sleep (switch to L0 state) */
  70744. + int (*sleep) (void *_p);
  70745. +#endif
  70746. + /** Pointer passed to start() and stop() */
  70747. + void *p;
  70748. +} dwc_otg_cil_callbacks_t;
  70749. +
  70750. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  70751. + dwc_otg_cil_callbacks_t * _cb,
  70752. + void *_p);
  70753. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  70754. + dwc_otg_cil_callbacks_t * _cb,
  70755. + void *_p);
  70756. +
  70757. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  70758. +
  70759. +//////////////////////////////////////////////////////////////////////
  70760. +/** Start the HCD. Helper function for using the HCD callbacks.
  70761. + *
  70762. + * @param core_if Programming view of DWC_otg controller.
  70763. + */
  70764. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  70765. +{
  70766. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  70767. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  70768. + }
  70769. +}
  70770. +
  70771. +/** Stop the HCD. Helper function for using the HCD callbacks.
  70772. + *
  70773. + * @param core_if Programming view of DWC_otg controller.
  70774. + */
  70775. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  70776. +{
  70777. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  70778. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  70779. + }
  70780. +}
  70781. +
  70782. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  70783. + *
  70784. + * @param core_if Programming view of DWC_otg controller.
  70785. + */
  70786. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  70787. +{
  70788. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  70789. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  70790. + }
  70791. +}
  70792. +
  70793. +/** Inform the HCD the a New Session has begun. Helper function for
  70794. + * using the HCD callbacks.
  70795. + *
  70796. + * @param core_if Programming view of DWC_otg controller.
  70797. + */
  70798. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  70799. +{
  70800. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  70801. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  70802. + }
  70803. +}
  70804. +
  70805. +#ifdef CONFIG_USB_DWC_OTG_LPM
  70806. +/**
  70807. + * Inform the HCD about LPM sleep.
  70808. + * Helper function for using the HCD callbacks.
  70809. + *
  70810. + * @param core_if Programming view of DWC_otg controller.
  70811. + */
  70812. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  70813. +{
  70814. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  70815. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  70816. + }
  70817. +}
  70818. +#endif
  70819. +
  70820. +/** Resume the HCD. Helper function for using the HCD callbacks.
  70821. + *
  70822. + * @param core_if Programming view of DWC_otg controller.
  70823. + */
  70824. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  70825. +{
  70826. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  70827. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  70828. + }
  70829. +}
  70830. +
  70831. +/** Start the PCD. Helper function for using the PCD callbacks.
  70832. + *
  70833. + * @param core_if Programming view of DWC_otg controller.
  70834. + */
  70835. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  70836. +{
  70837. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  70838. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  70839. + }
  70840. +}
  70841. +
  70842. +/** Stop the PCD. Helper function for using the PCD callbacks.
  70843. + *
  70844. + * @param core_if Programming view of DWC_otg controller.
  70845. + */
  70846. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  70847. +{
  70848. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  70849. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  70850. + }
  70851. +}
  70852. +
  70853. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  70854. + *
  70855. + * @param core_if Programming view of DWC_otg controller.
  70856. + */
  70857. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  70858. +{
  70859. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  70860. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  70861. + }
  70862. +}
  70863. +
  70864. +/** Resume the PCD. Helper function for using the PCD callbacks.
  70865. + *
  70866. + * @param core_if Programming view of DWC_otg controller.
  70867. + */
  70868. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  70869. +{
  70870. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  70871. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  70872. + }
  70873. +}
  70874. +
  70875. +//////////////////////////////////////////////////////////////////////
  70876. +
  70877. +#endif
  70878. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  70879. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 1970-01-01 01:00:00.000000000 +0100
  70880. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c 2014-09-14 19:04:13.000000000 +0200
  70881. @@ -0,0 +1,1594 @@
  70882. +/* ==========================================================================
  70883. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  70884. + * $Revision: #32 $
  70885. + * $Date: 2012/08/10 $
  70886. + * $Change: 2047372 $
  70887. + *
  70888. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  70889. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  70890. + * otherwise expressly agreed to in writing between Synopsys and you.
  70891. + *
  70892. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  70893. + * any End User Software License Agreement or Agreement for Licensed Product
  70894. + * with Synopsys or any supplement thereto. You are permitted to use and
  70895. + * redistribute this Software in source and binary forms, with or without
  70896. + * modification, provided that redistributions of source code must retain this
  70897. + * notice. You may not view, use, disclose, copy or distribute this file or
  70898. + * any information contained herein except pursuant to this license grant from
  70899. + * Synopsys. If you do not agree with this notice, including the disclaimer
  70900. + * below, then you are not authorized to use the Software.
  70901. + *
  70902. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  70903. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70904. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  70905. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  70906. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  70907. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  70908. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  70909. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  70910. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  70911. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  70912. + * DAMAGE.
  70913. + * ========================================================================== */
  70914. +
  70915. +/** @file
  70916. + *
  70917. + * The Core Interface Layer provides basic services for accessing and
  70918. + * managing the DWC_otg hardware. These services are used by both the
  70919. + * Host Controller Driver and the Peripheral Controller Driver.
  70920. + *
  70921. + * This file contains the Common Interrupt handlers.
  70922. + */
  70923. +#include "dwc_os.h"
  70924. +#include "dwc_otg_regs.h"
  70925. +#include "dwc_otg_cil.h"
  70926. +#include "dwc_otg_driver.h"
  70927. +#include "dwc_otg_pcd.h"
  70928. +#include "dwc_otg_hcd.h"
  70929. +
  70930. +#ifdef DEBUG
  70931. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  70932. +{
  70933. + return (core_if->op_state == A_HOST ? "a_host" :
  70934. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  70935. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  70936. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  70937. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  70938. +}
  70939. +#endif
  70940. +
  70941. +/** This function will log a debug message
  70942. + *
  70943. + * @param core_if Programming view of DWC_otg controller.
  70944. + */
  70945. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  70946. +{
  70947. + gintsts_data_t gintsts;
  70948. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  70949. + dwc_otg_mode(core_if) ? "Host" : "Device");
  70950. +
  70951. + /* Clear interrupt */
  70952. + gintsts.d32 = 0;
  70953. + gintsts.b.modemismatch = 1;
  70954. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  70955. + return 1;
  70956. +}
  70957. +
  70958. +/**
  70959. + * This function handles the OTG Interrupts. It reads the OTG
  70960. + * Interrupt Register (GOTGINT) to determine what interrupt has
  70961. + * occurred.
  70962. + *
  70963. + * @param core_if Programming view of DWC_otg controller.
  70964. + */
  70965. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  70966. +{
  70967. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  70968. + gotgint_data_t gotgint;
  70969. + gotgctl_data_t gotgctl;
  70970. + gintmsk_data_t gintmsk;
  70971. + gpwrdn_data_t gpwrdn;
  70972. +
  70973. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  70974. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  70975. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  70976. + op_state_str(core_if));
  70977. +
  70978. + if (gotgint.b.sesenddet) {
  70979. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  70980. + "Session End Detected++ (%s)\n",
  70981. + op_state_str(core_if));
  70982. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  70983. +
  70984. + if (core_if->op_state == B_HOST) {
  70985. + cil_pcd_start(core_if);
  70986. + core_if->op_state = B_PERIPHERAL;
  70987. + } else {
  70988. + /* If not B_HOST and Device HNP still set. HNP
  70989. + * Did not succeed!*/
  70990. + if (gotgctl.b.devhnpen) {
  70991. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  70992. + __DWC_ERROR("Device Not Connected/Responding!\n");
  70993. + }
  70994. +
  70995. + /* If Session End Detected the B-Cable has
  70996. + * been disconnected. */
  70997. + /* Reset PCD and Gadget driver to a
  70998. + * clean state. */
  70999. + core_if->lx_state = DWC_OTG_L0;
  71000. + DWC_SPINUNLOCK(core_if->lock);
  71001. + cil_pcd_stop(core_if);
  71002. + DWC_SPINLOCK(core_if->lock);
  71003. +
  71004. + if (core_if->adp_enable) {
  71005. + if (core_if->power_down == 2) {
  71006. + gpwrdn.d32 = 0;
  71007. + gpwrdn.b.pwrdnswtch = 1;
  71008. + DWC_MODIFY_REG32(&core_if->
  71009. + core_global_regs->
  71010. + gpwrdn, gpwrdn.d32, 0);
  71011. + }
  71012. +
  71013. + gpwrdn.d32 = 0;
  71014. + gpwrdn.b.pmuintsel = 1;
  71015. + gpwrdn.b.pmuactv = 1;
  71016. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71017. + gpwrdn, 0, gpwrdn.d32);
  71018. +
  71019. + dwc_otg_adp_sense_start(core_if);
  71020. + }
  71021. + }
  71022. +
  71023. + gotgctl.d32 = 0;
  71024. + gotgctl.b.devhnpen = 1;
  71025. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  71026. + }
  71027. + if (gotgint.b.sesreqsucstschng) {
  71028. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  71029. + "Session Reqeust Success Status Change++\n");
  71030. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  71031. + if (gotgctl.b.sesreqscs) {
  71032. +
  71033. + if ((core_if->core_params->phy_type ==
  71034. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  71035. + core_if->srp_success = 1;
  71036. + } else {
  71037. + DWC_SPINUNLOCK(core_if->lock);
  71038. + cil_pcd_resume(core_if);
  71039. + DWC_SPINLOCK(core_if->lock);
  71040. + /* Clear Session Request */
  71041. + gotgctl.d32 = 0;
  71042. + gotgctl.b.sesreq = 1;
  71043. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  71044. + gotgctl.d32, 0);
  71045. + }
  71046. + }
  71047. + }
  71048. + if (gotgint.b.hstnegsucstschng) {
  71049. + /* Print statements during the HNP interrupt handling
  71050. + * can cause it to fail.*/
  71051. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  71052. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  71053. + * this does not help*/
  71054. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  71055. + dwc_udelay(100);
  71056. + if (gotgctl.b.hstnegscs) {
  71057. + if (dwc_otg_is_host_mode(core_if)) {
  71058. + core_if->op_state = B_HOST;
  71059. + /*
  71060. + * Need to disable SOF interrupt immediately.
  71061. + * When switching from device to host, the PCD
  71062. + * interrupt handler won't handle the
  71063. + * interrupt if host mode is already set. The
  71064. + * HCD interrupt handler won't get called if
  71065. + * the HCD state is HALT. This means that the
  71066. + * interrupt does not get handled and Linux
  71067. + * complains loudly.
  71068. + */
  71069. + gintmsk.d32 = 0;
  71070. + gintmsk.b.sofintr = 1;
  71071. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  71072. + gintmsk.d32, 0);
  71073. + /* Call callback function with spin lock released */
  71074. + DWC_SPINUNLOCK(core_if->lock);
  71075. + cil_pcd_stop(core_if);
  71076. + /*
  71077. + * Initialize the Core for Host mode.
  71078. + */
  71079. + cil_hcd_start(core_if);
  71080. + DWC_SPINLOCK(core_if->lock);
  71081. + core_if->op_state = B_HOST;
  71082. + }
  71083. + } else {
  71084. + gotgctl.d32 = 0;
  71085. + gotgctl.b.hnpreq = 1;
  71086. + gotgctl.b.devhnpen = 1;
  71087. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  71088. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  71089. + __DWC_ERROR("Device Not Connected/Responding\n");
  71090. + }
  71091. + }
  71092. + if (gotgint.b.hstnegdet) {
  71093. + /* The disconnect interrupt is set at the same time as
  71094. + * Host Negotiation Detected. During the mode
  71095. + * switch all interrupts are cleared so the disconnect
  71096. + * interrupt handler will not get executed.
  71097. + */
  71098. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  71099. + "Host Negotiation Detected++ (%s)\n",
  71100. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  71101. + "Device"));
  71102. + if (dwc_otg_is_device_mode(core_if)) {
  71103. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  71104. + core_if->op_state);
  71105. + DWC_SPINUNLOCK(core_if->lock);
  71106. + cil_hcd_disconnect(core_if);
  71107. + cil_pcd_start(core_if);
  71108. + DWC_SPINLOCK(core_if->lock);
  71109. + core_if->op_state = A_PERIPHERAL;
  71110. + } else {
  71111. + /*
  71112. + * Need to disable SOF interrupt immediately. When
  71113. + * switching from device to host, the PCD interrupt
  71114. + * handler won't handle the interrupt if host mode is
  71115. + * already set. The HCD interrupt handler won't get
  71116. + * called if the HCD state is HALT. This means that
  71117. + * the interrupt does not get handled and Linux
  71118. + * complains loudly.
  71119. + */
  71120. + gintmsk.d32 = 0;
  71121. + gintmsk.b.sofintr = 1;
  71122. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  71123. + DWC_SPINUNLOCK(core_if->lock);
  71124. + cil_pcd_stop(core_if);
  71125. + cil_hcd_start(core_if);
  71126. + DWC_SPINLOCK(core_if->lock);
  71127. + core_if->op_state = A_HOST;
  71128. + }
  71129. + }
  71130. + if (gotgint.b.adevtoutchng) {
  71131. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  71132. + "A-Device Timeout Change++\n");
  71133. + }
  71134. + if (gotgint.b.debdone) {
  71135. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  71136. + }
  71137. +
  71138. + /* Clear GOTGINT */
  71139. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  71140. +
  71141. + return 1;
  71142. +}
  71143. +
  71144. +void w_conn_id_status_change(void *p)
  71145. +{
  71146. + dwc_otg_core_if_t *core_if = p;
  71147. + uint32_t count = 0;
  71148. + gotgctl_data_t gotgctl = {.d32 = 0 };
  71149. +
  71150. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  71151. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  71152. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  71153. +
  71154. + /* B-Device connector (Device Mode) */
  71155. + if (gotgctl.b.conidsts) {
  71156. + /* Wait for switch to device mode. */
  71157. + while (!dwc_otg_is_device_mode(core_if)) {
  71158. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  71159. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  71160. + "Peripheral"));
  71161. + dwc_mdelay(100);
  71162. + if (++count > 10000)
  71163. + break;
  71164. + }
  71165. + DWC_ASSERT(++count < 10000,
  71166. + "Connection id status change timed out");
  71167. + core_if->op_state = B_PERIPHERAL;
  71168. + dwc_otg_core_init(core_if);
  71169. + dwc_otg_enable_global_interrupts(core_if);
  71170. + cil_pcd_start(core_if);
  71171. + } else {
  71172. + /* A-Device connector (Host Mode) */
  71173. + while (!dwc_otg_is_host_mode(core_if)) {
  71174. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  71175. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  71176. + "Peripheral"));
  71177. + dwc_mdelay(100);
  71178. + if (++count > 10000)
  71179. + break;
  71180. + }
  71181. + DWC_ASSERT(++count < 10000,
  71182. + "Connection id status change timed out");
  71183. + core_if->op_state = A_HOST;
  71184. + /*
  71185. + * Initialize the Core for Host mode.
  71186. + */
  71187. + dwc_otg_core_init(core_if);
  71188. + dwc_otg_enable_global_interrupts(core_if);
  71189. + cil_hcd_start(core_if);
  71190. + }
  71191. +}
  71192. +
  71193. +/**
  71194. + * This function handles the Connector ID Status Change Interrupt. It
  71195. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  71196. + * is a Device to Host Mode transition or a Host Mode to Device
  71197. + * Transition.
  71198. + *
  71199. + * This only occurs when the cable is connected/removed from the PHY
  71200. + * connector.
  71201. + *
  71202. + * @param core_if Programming view of DWC_otg controller.
  71203. + */
  71204. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  71205. +{
  71206. +
  71207. + /*
  71208. + * Need to disable SOF interrupt immediately. If switching from device
  71209. + * to host, the PCD interrupt handler won't handle the interrupt if
  71210. + * host mode is already set. The HCD interrupt handler won't get
  71211. + * called if the HCD state is HALT. This means that the interrupt does
  71212. + * not get handled and Linux complains loudly.
  71213. + */
  71214. + gintmsk_data_t gintmsk = {.d32 = 0 };
  71215. + gintsts_data_t gintsts = {.d32 = 0 };
  71216. +
  71217. + gintmsk.b.sofintr = 1;
  71218. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  71219. +
  71220. + DWC_DEBUGPL(DBG_CIL,
  71221. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  71222. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  71223. +
  71224. + DWC_SPINUNLOCK(core_if->lock);
  71225. +
  71226. + /*
  71227. + * Need to schedule a work, as there are possible DELAY function calls
  71228. + * Release lock before scheduling workq as it holds spinlock during scheduling
  71229. + */
  71230. +
  71231. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  71232. + core_if, "connection id status change");
  71233. + DWC_SPINLOCK(core_if->lock);
  71234. +
  71235. + /* Set flag and clear interrupt */
  71236. + gintsts.b.conidstschng = 1;
  71237. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  71238. +
  71239. + return 1;
  71240. +}
  71241. +
  71242. +/**
  71243. + * This interrupt indicates that a device is initiating the Session
  71244. + * Request Protocol to request the host to turn on bus power so a new
  71245. + * session can begin. The handler responds by turning on bus power. If
  71246. + * the DWC_otg controller is in low power mode, the handler brings the
  71247. + * controller out of low power mode before turning on bus power.
  71248. + *
  71249. + * @param core_if Programming view of DWC_otg controller.
  71250. + */
  71251. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  71252. +{
  71253. + gintsts_data_t gintsts;
  71254. +
  71255. +#ifndef DWC_HOST_ONLY
  71256. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  71257. +
  71258. + if (dwc_otg_is_device_mode(core_if)) {
  71259. + DWC_PRINTF("SRP: Device mode\n");
  71260. + } else {
  71261. + hprt0_data_t hprt0;
  71262. + DWC_PRINTF("SRP: Host mode\n");
  71263. +
  71264. + /* Turn on the port power bit. */
  71265. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71266. + hprt0.b.prtpwr = 1;
  71267. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71268. +
  71269. + /* Start the Connection timer. So a message can be displayed
  71270. + * if connect does not occur within 10 seconds. */
  71271. + cil_hcd_session_start(core_if);
  71272. + }
  71273. +#endif
  71274. +
  71275. + /* Clear interrupt */
  71276. + gintsts.d32 = 0;
  71277. + gintsts.b.sessreqintr = 1;
  71278. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  71279. +
  71280. + return 1;
  71281. +}
  71282. +
  71283. +void w_wakeup_detected(void *p)
  71284. +{
  71285. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  71286. + /*
  71287. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  71288. + * so that OPT tests pass with all PHYs).
  71289. + */
  71290. + hprt0_data_t hprt0 = {.d32 = 0 };
  71291. +#if 0
  71292. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71293. + /* Restart the Phy Clock */
  71294. + pcgcctl.b.stoppclk = 1;
  71295. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71296. + dwc_udelay(10);
  71297. +#endif //0
  71298. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  71299. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  71300. +// dwc_mdelay(70);
  71301. + hprt0.b.prtres = 0; /* Resume */
  71302. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  71303. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  71304. + DWC_READ_REG32(core_if->host_if->hprt0));
  71305. +
  71306. + cil_hcd_resume(core_if);
  71307. +
  71308. + /** Change to L0 state*/
  71309. + core_if->lx_state = DWC_OTG_L0;
  71310. +}
  71311. +
  71312. +/**
  71313. + * This interrupt indicates that the DWC_otg controller has detected a
  71314. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  71315. + * low power mode, the handler must brings the controller out of low
  71316. + * power mode. The controller automatically begins resume
  71317. + * signaling. The handler schedules a time to stop resume signaling.
  71318. + */
  71319. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  71320. +{
  71321. + gintsts_data_t gintsts;
  71322. +
  71323. + DWC_DEBUGPL(DBG_ANY,
  71324. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  71325. +
  71326. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  71327. +
  71328. + if (dwc_otg_is_device_mode(core_if)) {
  71329. + dctl_data_t dctl = {.d32 = 0 };
  71330. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  71331. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  71332. + dsts));
  71333. + if (core_if->lx_state == DWC_OTG_L2) {
  71334. +#ifdef PARTIAL_POWER_DOWN
  71335. + if (core_if->hwcfg4.b.power_optimiz) {
  71336. + pcgcctl_data_t power = {.d32 = 0 };
  71337. +
  71338. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  71339. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  71340. + power.d32);
  71341. +
  71342. + power.b.stoppclk = 0;
  71343. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  71344. +
  71345. + power.b.pwrclmp = 0;
  71346. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  71347. +
  71348. + power.b.rstpdwnmodule = 0;
  71349. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  71350. + }
  71351. +#endif
  71352. + /* Clear the Remote Wakeup Signaling */
  71353. + dctl.b.rmtwkupsig = 1;
  71354. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  71355. + dctl, dctl.d32, 0);
  71356. +
  71357. + DWC_SPINUNLOCK(core_if->lock);
  71358. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  71359. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  71360. + }
  71361. + DWC_SPINLOCK(core_if->lock);
  71362. + } else {
  71363. + glpmcfg_data_t lpmcfg;
  71364. + lpmcfg.d32 =
  71365. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  71366. + lpmcfg.b.hird_thres &= (~(1 << 4));
  71367. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  71368. + lpmcfg.d32);
  71369. + }
  71370. + /** Change to L0 state*/
  71371. + core_if->lx_state = DWC_OTG_L0;
  71372. + } else {
  71373. + if (core_if->lx_state != DWC_OTG_L1) {
  71374. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71375. +
  71376. + /* Restart the Phy Clock */
  71377. + pcgcctl.b.stoppclk = 1;
  71378. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  71379. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  71380. + } else {
  71381. + /** Change to L0 state*/
  71382. + core_if->lx_state = DWC_OTG_L0;
  71383. + }
  71384. + }
  71385. +
  71386. + /* Clear interrupt */
  71387. + gintsts.d32 = 0;
  71388. + gintsts.b.wkupintr = 1;
  71389. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  71390. +
  71391. + return 1;
  71392. +}
  71393. +
  71394. +/**
  71395. + * This interrupt indicates that the Wakeup Logic has detected a
  71396. + * Device disconnect.
  71397. + */
  71398. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  71399. +{
  71400. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  71401. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  71402. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71403. +
  71404. + DWC_PRINTF("%s called\n", __FUNCTION__);
  71405. +
  71406. + if (!core_if->hibernation_suspend) {
  71407. + DWC_PRINTF("Already exited from Hibernation\n");
  71408. + return 1;
  71409. + }
  71410. +
  71411. + /* Switch on the voltage to the core */
  71412. + gpwrdn.b.pwrdnswtch = 1;
  71413. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71414. + dwc_udelay(10);
  71415. +
  71416. + /* Reset the core */
  71417. + gpwrdn.d32 = 0;
  71418. + gpwrdn.b.pwrdnrstn = 1;
  71419. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71420. + dwc_udelay(10);
  71421. +
  71422. + /* Disable power clamps*/
  71423. + gpwrdn.d32 = 0;
  71424. + gpwrdn.b.pwrdnclmp = 1;
  71425. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71426. +
  71427. + /* Remove reset the core signal */
  71428. + gpwrdn.d32 = 0;
  71429. + gpwrdn.b.pwrdnrstn = 1;
  71430. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  71431. + dwc_udelay(10);
  71432. +
  71433. + /* Disable PMU interrupt */
  71434. + gpwrdn.d32 = 0;
  71435. + gpwrdn.b.pmuintsel = 1;
  71436. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71437. +
  71438. + core_if->hibernation_suspend = 0;
  71439. +
  71440. + /* Disable PMU */
  71441. + gpwrdn.d32 = 0;
  71442. + gpwrdn.b.pmuactv = 1;
  71443. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71444. + dwc_udelay(10);
  71445. +
  71446. + if (gpwrdn_temp.b.idsts) {
  71447. + core_if->op_state = B_PERIPHERAL;
  71448. + dwc_otg_core_init(core_if);
  71449. + dwc_otg_enable_global_interrupts(core_if);
  71450. + cil_pcd_start(core_if);
  71451. + } else {
  71452. + core_if->op_state = A_HOST;
  71453. + dwc_otg_core_init(core_if);
  71454. + dwc_otg_enable_global_interrupts(core_if);
  71455. + cil_hcd_start(core_if);
  71456. + }
  71457. +
  71458. + return 1;
  71459. +}
  71460. +
  71461. +/**
  71462. + * This interrupt indicates that the Wakeup Logic has detected a
  71463. + * remote wakeup sequence.
  71464. + */
  71465. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  71466. +{
  71467. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71468. + DWC_DEBUGPL(DBG_ANY,
  71469. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  71470. +
  71471. + if (!core_if->hibernation_suspend) {
  71472. + DWC_PRINTF("Already exited from Hibernation\n");
  71473. + return 1;
  71474. + }
  71475. +
  71476. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71477. + if (gpwrdn.b.idsts) { // Device Mode
  71478. + if ((core_if->power_down == 2)
  71479. + && (core_if->hibernation_suspend == 1)) {
  71480. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  71481. + }
  71482. + } else {
  71483. + if ((core_if->power_down == 2)
  71484. + && (core_if->hibernation_suspend == 1)) {
  71485. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  71486. + }
  71487. + }
  71488. + return 1;
  71489. +}
  71490. +
  71491. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  71492. +{
  71493. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71494. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  71495. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  71496. +
  71497. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  71498. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71499. + if (core_if->power_down == 2) {
  71500. + if (!core_if->hibernation_suspend) {
  71501. + DWC_PRINTF("Already exited from Hibernation\n");
  71502. + return 1;
  71503. + }
  71504. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  71505. + /* Switch on the voltage to the core */
  71506. + gpwrdn.b.pwrdnswtch = 1;
  71507. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71508. + dwc_udelay(10);
  71509. +
  71510. + /* Reset the core */
  71511. + gpwrdn.d32 = 0;
  71512. + gpwrdn.b.pwrdnrstn = 1;
  71513. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71514. + dwc_udelay(10);
  71515. +
  71516. + /* Disable power clamps */
  71517. + gpwrdn.d32 = 0;
  71518. + gpwrdn.b.pwrdnclmp = 1;
  71519. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71520. +
  71521. + /* Remove reset the core signal */
  71522. + gpwrdn.d32 = 0;
  71523. + gpwrdn.b.pwrdnrstn = 1;
  71524. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  71525. + dwc_udelay(10);
  71526. +
  71527. + /* Disable PMU interrupt */
  71528. + gpwrdn.d32 = 0;
  71529. + gpwrdn.b.pmuintsel = 1;
  71530. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71531. +
  71532. + /*Indicates that we are exiting from hibernation */
  71533. + core_if->hibernation_suspend = 0;
  71534. +
  71535. + /* Disable PMU */
  71536. + gpwrdn.d32 = 0;
  71537. + gpwrdn.b.pmuactv = 1;
  71538. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71539. + dwc_udelay(10);
  71540. +
  71541. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  71542. + if (gpwrdn.b.dis_vbus == 1) {
  71543. + gpwrdn.d32 = 0;
  71544. + gpwrdn.b.dis_vbus = 1;
  71545. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71546. + }
  71547. +
  71548. + if (gpwrdn_temp.b.idsts) {
  71549. + core_if->op_state = B_PERIPHERAL;
  71550. + dwc_otg_core_init(core_if);
  71551. + dwc_otg_enable_global_interrupts(core_if);
  71552. + cil_pcd_start(core_if);
  71553. + } else {
  71554. + core_if->op_state = A_HOST;
  71555. + dwc_otg_core_init(core_if);
  71556. + dwc_otg_enable_global_interrupts(core_if);
  71557. + cil_hcd_start(core_if);
  71558. + }
  71559. + }
  71560. +
  71561. + if (core_if->adp_enable) {
  71562. + uint8_t is_host = 0;
  71563. + DWC_SPINUNLOCK(core_if->lock);
  71564. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  71565. +#ifndef DWC_HOST_ONLY
  71566. + if (gpwrdn_temp.b.idsts)
  71567. + core_if->lock = otg_dev->pcd->lock;
  71568. +#endif
  71569. +#ifndef DWC_DEVICE_ONLY
  71570. + if (!gpwrdn_temp.b.idsts) {
  71571. + core_if->lock = otg_dev->hcd->lock;
  71572. + is_host = 1;
  71573. + }
  71574. +#endif
  71575. + DWC_PRINTF("RESTART ADP\n");
  71576. + if (core_if->adp.probe_enabled)
  71577. + dwc_otg_adp_probe_stop(core_if);
  71578. + if (core_if->adp.sense_enabled)
  71579. + dwc_otg_adp_sense_stop(core_if);
  71580. + if (core_if->adp.sense_timer_started)
  71581. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  71582. + if (core_if->adp.vbuson_timer_started)
  71583. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  71584. + core_if->adp.probe_timer_values[0] = -1;
  71585. + core_if->adp.probe_timer_values[1] = -1;
  71586. + core_if->adp.sense_timer_started = 0;
  71587. + core_if->adp.vbuson_timer_started = 0;
  71588. + core_if->adp.probe_counter = 0;
  71589. + core_if->adp.gpwrdn = 0;
  71590. +
  71591. + /* Disable PMU and restart ADP */
  71592. + gpwrdn_temp.d32 = 0;
  71593. + gpwrdn_temp.b.pmuactv = 1;
  71594. + gpwrdn_temp.b.pmuintsel = 1;
  71595. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71596. + DWC_PRINTF("Check point 1\n");
  71597. + dwc_mdelay(110);
  71598. + dwc_otg_adp_start(core_if, is_host);
  71599. + DWC_SPINLOCK(core_if->lock);
  71600. + }
  71601. +
  71602. +
  71603. + return 1;
  71604. +}
  71605. +
  71606. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  71607. +{
  71608. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71609. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  71610. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  71611. +
  71612. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71613. + if (core_if->power_down == 2) {
  71614. + if (!core_if->hibernation_suspend) {
  71615. + DWC_PRINTF("Already exited from Hibernation\n");
  71616. + return 1;
  71617. + }
  71618. +
  71619. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  71620. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  71621. + gpwrdn.b.bsessvld == 0) {
  71622. + /* Save gpwrdn register for further usage if stschng interrupt */
  71623. + core_if->gr_backup->gpwrdn_local =
  71624. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71625. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  71626. + return 1;
  71627. + }
  71628. +
  71629. + /* Switch on the voltage to the core */
  71630. + gpwrdn.d32 = 0;
  71631. + gpwrdn.b.pwrdnswtch = 1;
  71632. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71633. + dwc_udelay(10);
  71634. +
  71635. + /* Reset the core */
  71636. + gpwrdn.d32 = 0;
  71637. + gpwrdn.b.pwrdnrstn = 1;
  71638. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71639. + dwc_udelay(10);
  71640. +
  71641. + /* Disable power clamps */
  71642. + gpwrdn.d32 = 0;
  71643. + gpwrdn.b.pwrdnclmp = 1;
  71644. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71645. +
  71646. + /* Remove reset the core signal */
  71647. + gpwrdn.d32 = 0;
  71648. + gpwrdn.b.pwrdnrstn = 1;
  71649. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  71650. + dwc_udelay(10);
  71651. +
  71652. + /* Disable PMU interrupt */
  71653. + gpwrdn.d32 = 0;
  71654. + gpwrdn.b.pmuintsel = 1;
  71655. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71656. + dwc_udelay(10);
  71657. +
  71658. + /*Indicates that we are exiting from hibernation */
  71659. + core_if->hibernation_suspend = 0;
  71660. +
  71661. + /* Disable PMU */
  71662. + gpwrdn.d32 = 0;
  71663. + gpwrdn.b.pmuactv = 1;
  71664. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71665. + dwc_udelay(10);
  71666. +
  71667. + core_if->op_state = B_PERIPHERAL;
  71668. + dwc_otg_core_init(core_if);
  71669. + dwc_otg_enable_global_interrupts(core_if);
  71670. + cil_pcd_start(core_if);
  71671. +
  71672. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  71673. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  71674. + /*
  71675. + * Initiate SRP after initial ADP probe.
  71676. + */
  71677. + dwc_otg_initiate_srp(core_if);
  71678. + }
  71679. + }
  71680. +
  71681. + return 1;
  71682. +}
  71683. +/**
  71684. + * This interrupt indicates that the Wakeup Logic has detected a
  71685. + * status change either on IDDIG or BSessVld.
  71686. + */
  71687. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  71688. +{
  71689. + int retval;
  71690. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71691. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  71692. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  71693. +
  71694. + DWC_PRINTF("%s called\n", __FUNCTION__);
  71695. +
  71696. + if (core_if->power_down == 2) {
  71697. + if (core_if->hibernation_suspend <= 0) {
  71698. + DWC_PRINTF("Already exited from Hibernation\n");
  71699. + return 1;
  71700. + } else
  71701. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  71702. +
  71703. + } else {
  71704. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  71705. + }
  71706. +
  71707. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  71708. +
  71709. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  71710. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  71711. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  71712. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  71713. + }
  71714. +
  71715. + return retval;
  71716. +}
  71717. +
  71718. +/**
  71719. + * This interrupt indicates that the Wakeup Logic has detected a
  71720. + * SRP.
  71721. + */
  71722. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  71723. +{
  71724. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71725. +
  71726. + DWC_PRINTF("%s called\n", __FUNCTION__);
  71727. +
  71728. + if (!core_if->hibernation_suspend) {
  71729. + DWC_PRINTF("Already exited from Hibernation\n");
  71730. + return 1;
  71731. + }
  71732. +#ifdef DWC_DEV_SRPCAP
  71733. + if (core_if->pwron_timer_started) {
  71734. + core_if->pwron_timer_started = 0;
  71735. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  71736. + }
  71737. +#endif
  71738. +
  71739. + /* Switch on the voltage to the core */
  71740. + gpwrdn.b.pwrdnswtch = 1;
  71741. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71742. + dwc_udelay(10);
  71743. +
  71744. + /* Reset the core */
  71745. + gpwrdn.d32 = 0;
  71746. + gpwrdn.b.pwrdnrstn = 1;
  71747. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71748. + dwc_udelay(10);
  71749. +
  71750. + /* Disable power clamps */
  71751. + gpwrdn.d32 = 0;
  71752. + gpwrdn.b.pwrdnclmp = 1;
  71753. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71754. +
  71755. + /* Remove reset the core signal */
  71756. + gpwrdn.d32 = 0;
  71757. + gpwrdn.b.pwrdnrstn = 1;
  71758. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  71759. + dwc_udelay(10);
  71760. +
  71761. + /* Disable PMU interrupt */
  71762. + gpwrdn.d32 = 0;
  71763. + gpwrdn.b.pmuintsel = 1;
  71764. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71765. +
  71766. + /* Indicates that we are exiting from hibernation */
  71767. + core_if->hibernation_suspend = 0;
  71768. +
  71769. + /* Disable PMU */
  71770. + gpwrdn.d32 = 0;
  71771. + gpwrdn.b.pmuactv = 1;
  71772. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71773. + dwc_udelay(10);
  71774. +
  71775. + /* Programm Disable VBUS to 0 */
  71776. + gpwrdn.d32 = 0;
  71777. + gpwrdn.b.dis_vbus = 1;
  71778. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  71779. +
  71780. + /*Initialize the core as Host */
  71781. + core_if->op_state = A_HOST;
  71782. + dwc_otg_core_init(core_if);
  71783. + dwc_otg_enable_global_interrupts(core_if);
  71784. + cil_hcd_start(core_if);
  71785. +
  71786. + return 1;
  71787. +}
  71788. +
  71789. +/** This interrupt indicates that restore command after Hibernation
  71790. + * was completed by the core. */
  71791. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  71792. +{
  71793. + pcgcctl_data_t pcgcctl;
  71794. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  71795. +
  71796. + //TODO De-assert restore signal. 8.a
  71797. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  71798. + if (pcgcctl.b.restoremode == 1) {
  71799. + gintmsk_data_t gintmsk = {.d32 = 0 };
  71800. + /*
  71801. + * If restore mode is Remote Wakeup,
  71802. + * unmask Remote Wakeup interrupt.
  71803. + */
  71804. + gintmsk.b.wkupintr = 1;
  71805. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  71806. + 0, gintmsk.d32);
  71807. + }
  71808. +
  71809. + return 1;
  71810. +}
  71811. +
  71812. +/**
  71813. + * This interrupt indicates that a device has been disconnected from
  71814. + * the root port.
  71815. + */
  71816. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  71817. +{
  71818. + gintsts_data_t gintsts;
  71819. +
  71820. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  71821. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  71822. + op_state_str(core_if));
  71823. +
  71824. +/** @todo Consolidate this if statement. */
  71825. +#ifndef DWC_HOST_ONLY
  71826. + if (core_if->op_state == B_HOST) {
  71827. + /* If in device mode Disconnect and stop the HCD, then
  71828. + * start the PCD. */
  71829. + DWC_SPINUNLOCK(core_if->lock);
  71830. + cil_hcd_disconnect(core_if);
  71831. + cil_pcd_start(core_if);
  71832. + DWC_SPINLOCK(core_if->lock);
  71833. + core_if->op_state = B_PERIPHERAL;
  71834. + } else if (dwc_otg_is_device_mode(core_if)) {
  71835. + gotgctl_data_t gotgctl = {.d32 = 0 };
  71836. + gotgctl.d32 =
  71837. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  71838. + if (gotgctl.b.hstsethnpen == 1) {
  71839. + /* Do nothing, if HNP in process the OTG
  71840. + * interrupt "Host Negotiation Detected"
  71841. + * interrupt will do the mode switch.
  71842. + */
  71843. + } else if (gotgctl.b.devhnpen == 0) {
  71844. + /* If in device mode Disconnect and stop the HCD, then
  71845. + * start the PCD. */
  71846. + DWC_SPINUNLOCK(core_if->lock);
  71847. + cil_hcd_disconnect(core_if);
  71848. + cil_pcd_start(core_if);
  71849. + DWC_SPINLOCK(core_if->lock);
  71850. + core_if->op_state = B_PERIPHERAL;
  71851. + } else {
  71852. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  71853. + }
  71854. + } else {
  71855. + if (core_if->op_state == A_HOST) {
  71856. + /* A-Cable still connected but device disconnected. */
  71857. + cil_hcd_disconnect(core_if);
  71858. + if (core_if->adp_enable) {
  71859. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  71860. + cil_hcd_stop(core_if);
  71861. + /* Enable Power Down Logic */
  71862. + gpwrdn.b.pmuintsel = 1;
  71863. + gpwrdn.b.pmuactv = 1;
  71864. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71865. + gpwrdn, 0, gpwrdn.d32);
  71866. + dwc_otg_adp_probe_start(core_if);
  71867. +
  71868. + /* Power off the core */
  71869. + if (core_if->power_down == 2) {
  71870. + gpwrdn.d32 = 0;
  71871. + gpwrdn.b.pwrdnswtch = 1;
  71872. + DWC_MODIFY_REG32
  71873. + (&core_if->core_global_regs->gpwrdn,
  71874. + gpwrdn.d32, 0);
  71875. + }
  71876. + }
  71877. + }
  71878. + }
  71879. +#endif
  71880. + /* Change to L3(OFF) state */
  71881. + core_if->lx_state = DWC_OTG_L3;
  71882. +
  71883. + gintsts.d32 = 0;
  71884. + gintsts.b.disconnect = 1;
  71885. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  71886. + return 1;
  71887. +}
  71888. +
  71889. +/**
  71890. + * This interrupt indicates that SUSPEND state has been detected on
  71891. + * the USB.
  71892. + *
  71893. + * For HNP the USB Suspend interrupt signals the change from
  71894. + * "a_peripheral" to "a_host".
  71895. + *
  71896. + * When power management is enabled the core will be put in low power
  71897. + * mode.
  71898. + */
  71899. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  71900. +{
  71901. + dsts_data_t dsts;
  71902. + gintsts_data_t gintsts;
  71903. + dcfg_data_t dcfg;
  71904. +
  71905. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  71906. +
  71907. + if (dwc_otg_is_device_mode(core_if)) {
  71908. + /* Check the Device status register to determine if the Suspend
  71909. + * state is active. */
  71910. + dsts.d32 =
  71911. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  71912. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  71913. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  71914. + "HWCFG4.power Optimize=%d\n",
  71915. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  71916. +
  71917. +#ifdef PARTIAL_POWER_DOWN
  71918. +/** @todo Add a module parameter for power management. */
  71919. +
  71920. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  71921. + pcgcctl_data_t power = {.d32 = 0 };
  71922. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  71923. +
  71924. + power.b.pwrclmp = 1;
  71925. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  71926. +
  71927. + power.b.rstpdwnmodule = 1;
  71928. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  71929. +
  71930. + power.b.stoppclk = 1;
  71931. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  71932. +
  71933. + } else {
  71934. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  71935. + }
  71936. +#endif
  71937. + /* PCD callback for suspend. Release the lock inside of callback function */
  71938. + cil_pcd_suspend(core_if);
  71939. + if (core_if->power_down == 2)
  71940. + {
  71941. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  71942. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  71943. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  71944. +
  71945. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  71946. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  71947. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  71948. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  71949. +
  71950. + /* Change to L2(suspend) state */
  71951. + core_if->lx_state = DWC_OTG_L2;
  71952. +
  71953. + /* Clear interrupt in gintsts */
  71954. + gintsts.d32 = 0;
  71955. + gintsts.b.usbsuspend = 1;
  71956. + DWC_WRITE_REG32(&core_if->core_global_regs->
  71957. + gintsts, gintsts.d32);
  71958. + DWC_PRINTF("Start of hibernation completed\n");
  71959. + dwc_otg_save_global_regs(core_if);
  71960. + dwc_otg_save_dev_regs(core_if);
  71961. +
  71962. + gusbcfg.d32 =
  71963. + DWC_READ_REG32(&core_if->core_global_regs->
  71964. + gusbcfg);
  71965. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  71966. + /* ULPI interface */
  71967. + /* Suspend the Phy Clock */
  71968. + pcgcctl.d32 = 0;
  71969. + pcgcctl.b.stoppclk = 1;
  71970. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71971. + pcgcctl.d32);
  71972. + dwc_udelay(10);
  71973. + gpwrdn.b.pmuactv = 1;
  71974. + DWC_MODIFY_REG32(&core_if->
  71975. + core_global_regs->
  71976. + gpwrdn, 0, gpwrdn.d32);
  71977. + } else {
  71978. + /* UTMI+ Interface */
  71979. + gpwrdn.b.pmuactv = 1;
  71980. + DWC_MODIFY_REG32(&core_if->
  71981. + core_global_regs->
  71982. + gpwrdn, 0, gpwrdn.d32);
  71983. + dwc_udelay(10);
  71984. + pcgcctl.b.stoppclk = 1;
  71985. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  71986. + pcgcctl.d32);
  71987. + dwc_udelay(10);
  71988. + }
  71989. +
  71990. + /* Set flag to indicate that we are in hibernation */
  71991. + core_if->hibernation_suspend = 1;
  71992. + /* Enable interrupts from wake up logic */
  71993. + gpwrdn.d32 = 0;
  71994. + gpwrdn.b.pmuintsel = 1;
  71995. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  71996. + gpwrdn, 0, gpwrdn.d32);
  71997. + dwc_udelay(10);
  71998. +
  71999. + /* Unmask device mode interrupts in GPWRDN */
  72000. + gpwrdn.d32 = 0;
  72001. + gpwrdn.b.rst_det_msk = 1;
  72002. + gpwrdn.b.lnstchng_msk = 1;
  72003. + gpwrdn.b.sts_chngint_msk = 1;
  72004. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  72005. + gpwrdn, 0, gpwrdn.d32);
  72006. + dwc_udelay(10);
  72007. +
  72008. + /* Enable Power Down Clamp */
  72009. + gpwrdn.d32 = 0;
  72010. + gpwrdn.b.pwrdnclmp = 1;
  72011. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  72012. + gpwrdn, 0, gpwrdn.d32);
  72013. + dwc_udelay(10);
  72014. +
  72015. + /* Switch off VDD */
  72016. + gpwrdn.d32 = 0;
  72017. + gpwrdn.b.pwrdnswtch = 1;
  72018. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  72019. + gpwrdn, 0, gpwrdn.d32);
  72020. +
  72021. + /* Save gpwrdn register for further usage if stschng interrupt */
  72022. + core_if->gr_backup->gpwrdn_local =
  72023. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72024. + DWC_PRINTF("Hibernation completed\n");
  72025. +
  72026. + return 1;
  72027. + }
  72028. + } else if (core_if->power_down == 3) {
  72029. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  72030. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  72031. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  72032. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  72033. +
  72034. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  72035. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  72036. + core_if->xhib = 1;
  72037. +
  72038. + /* Clear interrupt in gintsts */
  72039. + gintsts.d32 = 0;
  72040. + gintsts.b.usbsuspend = 1;
  72041. + DWC_WRITE_REG32(&core_if->core_global_regs->
  72042. + gintsts, gintsts.d32);
  72043. +
  72044. + dwc_otg_save_global_regs(core_if);
  72045. + dwc_otg_save_dev_regs(core_if);
  72046. +
  72047. + /* Wait for 10 PHY clocks */
  72048. + dwc_udelay(10);
  72049. +
  72050. + /* Program GPIO register while entering to xHib */
  72051. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  72052. +
  72053. + pcgcctl.b.enbl_extnd_hiber = 1;
  72054. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  72055. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  72056. +
  72057. + pcgcctl.d32 = 0;
  72058. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  72059. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  72060. +
  72061. + pcgcctl.d32 = 0;
  72062. + pcgcctl.b.extnd_hiber_switch = 1;
  72063. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72064. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  72065. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  72066. +
  72067. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  72068. +
  72069. + return 1;
  72070. + }
  72071. + }
  72072. + } else {
  72073. + if (core_if->op_state == A_PERIPHERAL) {
  72074. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  72075. + /* Clear the a_peripheral flag, back to a_host. */
  72076. + DWC_SPINUNLOCK(core_if->lock);
  72077. + cil_pcd_stop(core_if);
  72078. + cil_hcd_start(core_if);
  72079. + DWC_SPINLOCK(core_if->lock);
  72080. + core_if->op_state = A_HOST;
  72081. + }
  72082. + }
  72083. +
  72084. + /* Change to L2(suspend) state */
  72085. + core_if->lx_state = DWC_OTG_L2;
  72086. +
  72087. + /* Clear interrupt */
  72088. + gintsts.d32 = 0;
  72089. + gintsts.b.usbsuspend = 1;
  72090. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  72091. +
  72092. + return 1;
  72093. +}
  72094. +
  72095. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  72096. +{
  72097. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  72098. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  72099. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  72100. +
  72101. + dwc_udelay(10);
  72102. +
  72103. + /* Program GPIO register while entering to xHib */
  72104. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  72105. +
  72106. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  72107. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  72108. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  72109. + dwc_udelay(10);
  72110. +
  72111. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  72112. + gpwrdn.b.restore = 1;
  72113. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  72114. + dwc_udelay(10);
  72115. +
  72116. + restore_lpm_i2c_regs(core_if);
  72117. +
  72118. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  72119. + pcgcctl.b.max_xcvrselect = 1;
  72120. + pcgcctl.b.ess_reg_restored = 0;
  72121. + pcgcctl.b.extnd_hiber_switch = 0;
  72122. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  72123. + pcgcctl.b.enbl_extnd_hiber = 1;
  72124. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  72125. +
  72126. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  72127. + gahbcfg.b.glblintrmsk = 1;
  72128. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  72129. +
  72130. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  72131. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  72132. +
  72133. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  72134. + core_if->gr_backup->gusbcfg_local);
  72135. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  72136. + core_if->dr_backup->dcfg);
  72137. +
  72138. + pcgcctl.d32 = 0;
  72139. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  72140. + pcgcctl.b.max_xcvrselect = 1;
  72141. + pcgcctl.d32 |= 0x608;
  72142. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  72143. + dwc_udelay(10);
  72144. +
  72145. + pcgcctl.d32 = 0;
  72146. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  72147. + pcgcctl.b.max_xcvrselect = 1;
  72148. + pcgcctl.b.ess_reg_restored = 1;
  72149. + pcgcctl.b.enbl_extnd_hiber = 1;
  72150. + pcgcctl.b.rstpdwnmodule = 1;
  72151. + pcgcctl.b.restoremode = 1;
  72152. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  72153. +
  72154. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  72155. +
  72156. + return 1;
  72157. +}
  72158. +
  72159. +#ifdef CONFIG_USB_DWC_OTG_LPM
  72160. +/**
  72161. + * This function hadles LPM transaction received interrupt.
  72162. + */
  72163. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  72164. +{
  72165. + glpmcfg_data_t lpmcfg;
  72166. + gintsts_data_t gintsts;
  72167. +
  72168. + if (!core_if->core_params->lpm_enable) {
  72169. + DWC_PRINTF("Unexpected LPM interrupt\n");
  72170. + }
  72171. +
  72172. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  72173. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  72174. +
  72175. + if (dwc_otg_is_host_mode(core_if)) {
  72176. + cil_hcd_sleep(core_if);
  72177. + } else {
  72178. + lpmcfg.b.hird_thres |= (1 << 4);
  72179. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  72180. + lpmcfg.d32);
  72181. + }
  72182. +
  72183. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  72184. + dwc_udelay(10);
  72185. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  72186. + if (lpmcfg.b.prt_sleep_sts) {
  72187. + /* Save the current state */
  72188. + core_if->lx_state = DWC_OTG_L1;
  72189. + }
  72190. +
  72191. + /* Clear interrupt */
  72192. + gintsts.d32 = 0;
  72193. + gintsts.b.lpmtranrcvd = 1;
  72194. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  72195. + return 1;
  72196. +}
  72197. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  72198. +
  72199. +/**
  72200. + * This function returns the Core Interrupt register.
  72201. + */
  72202. +static inline uint32_t dwc_otg_read_common_intr(dwc_otg_core_if_t * core_if, gintmsk_data_t *reenable_gintmsk, dwc_otg_hcd_t *hcd)
  72203. +{
  72204. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  72205. + gintsts_data_t gintsts;
  72206. + gintmsk_data_t gintmsk;
  72207. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  72208. + gintmsk_common.b.wkupintr = 1;
  72209. + gintmsk_common.b.sessreqintr = 1;
  72210. + gintmsk_common.b.conidstschng = 1;
  72211. + gintmsk_common.b.otgintr = 1;
  72212. + gintmsk_common.b.modemismatch = 1;
  72213. + gintmsk_common.b.disconnect = 1;
  72214. + gintmsk_common.b.usbsuspend = 1;
  72215. +#ifdef CONFIG_USB_DWC_OTG_LPM
  72216. + gintmsk_common.b.lpmtranrcvd = 1;
  72217. +#endif
  72218. + gintmsk_common.b.restoredone = 1;
  72219. + if(dwc_otg_is_device_mode(core_if))
  72220. + {
  72221. + /** @todo: The port interrupt occurs while in device
  72222. + * mode. Added code to CIL to clear the interrupt for now!
  72223. + */
  72224. + gintmsk_common.b.portintr = 1;
  72225. + }
  72226. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  72227. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  72228. + if(fiq_enable) {
  72229. + local_fiq_disable();
  72230. + /* Pull in the interrupts that the FIQ has masked */
  72231. + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
  72232. + gintmsk.d32 |= gintmsk_common.d32;
  72233. + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
  72234. + reenable_gintmsk->d32 = gintmsk.d32;
  72235. + local_fiq_enable();
  72236. + }
  72237. +
  72238. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  72239. +
  72240. +#ifdef DEBUG
  72241. + /* if any common interrupts set */
  72242. + if (gintsts.d32 & gintmsk_common.d32) {
  72243. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  72244. + gintsts.d32, gintmsk.d32);
  72245. + }
  72246. +#endif
  72247. + if (!fiq_enable){
  72248. + if (gahbcfg.b.glblintrmsk)
  72249. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  72250. + else
  72251. + return 0;
  72252. + } else {
  72253. + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
  72254. + * Can't trust the global interrupt mask bit in this case.
  72255. + */
  72256. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  72257. + }
  72258. +
  72259. +}
  72260. +
  72261. +/* MACRO for clearing interupt bits in GPWRDN register */
  72262. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  72263. +do { \
  72264. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  72265. + gpwrdn.b.__intr = 1; \
  72266. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  72267. + 0, gpwrdn.d32); \
  72268. +} while (0)
  72269. +
  72270. +/**
  72271. + * Common interrupt handler.
  72272. + *
  72273. + * The common interrupts are those that occur in both Host and Device mode.
  72274. + * This handler handles the following interrupts:
  72275. + * - Mode Mismatch Interrupt
  72276. + * - Disconnect Interrupt
  72277. + * - OTG Interrupt
  72278. + * - Connector ID Status Change Interrupt
  72279. + * - Session Request Interrupt.
  72280. + * - Resume / Remote Wakeup Detected Interrupt.
  72281. + * - LPM Transaction Received Interrupt
  72282. + * - ADP Transaction Received Interrupt
  72283. + *
  72284. + */
  72285. +int32_t dwc_otg_handle_common_intr(void *dev)
  72286. +{
  72287. + int retval = 0;
  72288. + gintsts_data_t gintsts;
  72289. + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
  72290. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  72291. + dwc_otg_device_t *otg_dev = dev;
  72292. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  72293. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  72294. + if (dwc_otg_is_device_mode(core_if))
  72295. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  72296. +
  72297. + if (core_if->lock)
  72298. + DWC_SPINLOCK(core_if->lock);
  72299. +
  72300. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  72301. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  72302. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  72303. + core_if->xhib = 2;
  72304. + if (core_if->lock)
  72305. + DWC_SPINUNLOCK(core_if->lock);
  72306. +
  72307. + return retval;
  72308. + }
  72309. +
  72310. + if (core_if->hibernation_suspend <= 0) {
  72311. + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
  72312. + * of this handler - god only knows why it's done like this
  72313. + */
  72314. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
  72315. +
  72316. + if (gintsts.b.modemismatch) {
  72317. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  72318. + }
  72319. + if (gintsts.b.otgintr) {
  72320. + retval |= dwc_otg_handle_otg_intr(core_if);
  72321. + }
  72322. + if (gintsts.b.conidstschng) {
  72323. + retval |=
  72324. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  72325. + }
  72326. + if (gintsts.b.disconnect) {
  72327. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  72328. + }
  72329. + if (gintsts.b.sessreqintr) {
  72330. + retval |= dwc_otg_handle_session_req_intr(core_if);
  72331. + }
  72332. + if (gintsts.b.wkupintr) {
  72333. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  72334. + }
  72335. + if (gintsts.b.usbsuspend) {
  72336. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  72337. + }
  72338. +#ifdef CONFIG_USB_DWC_OTG_LPM
  72339. + if (gintsts.b.lpmtranrcvd) {
  72340. + retval |= dwc_otg_handle_lpm_intr(core_if);
  72341. + }
  72342. +#endif
  72343. + if (gintsts.b.restoredone) {
  72344. + gintsts.d32 = 0;
  72345. + if (core_if->power_down == 2)
  72346. + core_if->hibernation_suspend = -1;
  72347. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  72348. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  72349. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  72350. + dctl_data_t dctl = {.d32 = 0 };
  72351. +
  72352. + DWC_WRITE_REG32(&core_if->core_global_regs->
  72353. + gintsts, 0xFFFFFFFF);
  72354. +
  72355. + DWC_DEBUGPL(DBG_ANY,
  72356. + "RESTORE DONE generated\n");
  72357. +
  72358. + gpwrdn.b.restore = 1;
  72359. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  72360. + dwc_udelay(10);
  72361. +
  72362. + pcgcctl.b.rstpdwnmodule = 1;
  72363. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  72364. +
  72365. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  72366. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  72367. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  72368. + dwc_udelay(50);
  72369. +
  72370. + dctl.b.pwronprgdone = 1;
  72371. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  72372. + dwc_udelay(10);
  72373. +
  72374. + dwc_otg_restore_global_regs(core_if);
  72375. + dwc_otg_restore_dev_regs(core_if, 0);
  72376. +
  72377. + dctl.d32 = 0;
  72378. + dctl.b.pwronprgdone = 1;
  72379. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  72380. + dwc_udelay(10);
  72381. +
  72382. + pcgcctl.d32 = 0;
  72383. + pcgcctl.b.enbl_extnd_hiber = 1;
  72384. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  72385. +
  72386. + /* The core will be in ON STATE */
  72387. + core_if->lx_state = DWC_OTG_L0;
  72388. + core_if->xhib = 0;
  72389. +
  72390. + DWC_SPINUNLOCK(core_if->lock);
  72391. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  72392. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  72393. + }
  72394. + DWC_SPINLOCK(core_if->lock);
  72395. +
  72396. + }
  72397. +
  72398. + gintsts.b.restoredone = 1;
  72399. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  72400. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  72401. + retval |= 1;
  72402. + }
  72403. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  72404. + /* The port interrupt occurs while in device mode with HPRT0
  72405. + * Port Enable/Disable.
  72406. + */
  72407. + gintsts.d32 = 0;
  72408. + gintsts.b.portintr = 1;
  72409. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  72410. + retval |= 1;
  72411. + gintmsk_reenable.b.portintr = 1;
  72412. +
  72413. + }
  72414. + /* Did we actually handle anything? if so, unmask the interrupt */
  72415. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
  72416. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
  72417. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
  72418. + if (retval && fiq_enable) {
  72419. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
  72420. + }
  72421. +
  72422. + } else {
  72423. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  72424. +
  72425. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  72426. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  72427. + if (gpwrdn.b.linestate == 0) {
  72428. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  72429. + } else {
  72430. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  72431. + }
  72432. +
  72433. + retval |= 1;
  72434. + }
  72435. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  72436. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  72437. + /* remote wakeup from hibernation */
  72438. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  72439. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  72440. + } else {
  72441. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  72442. + }
  72443. + retval |= 1;
  72444. + }
  72445. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  72446. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  72447. + if (gpwrdn.b.linestate == 0) {
  72448. + DWC_PRINTF("Reset detected\n");
  72449. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  72450. + }
  72451. + }
  72452. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  72453. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  72454. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  72455. + retval |= 1;
  72456. + }
  72457. + }
  72458. + /* Handle ADP interrupt here */
  72459. + if (gpwrdn.b.adp_int) {
  72460. + DWC_PRINTF("ADP interrupt\n");
  72461. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  72462. + dwc_otg_adp_handle_intr(core_if);
  72463. + retval |= 1;
  72464. + }
  72465. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  72466. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  72467. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  72468. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  72469. +
  72470. + retval |= 1;
  72471. + }
  72472. + if (core_if->lock)
  72473. + DWC_SPINUNLOCK(core_if->lock);
  72474. + return retval;
  72475. +}
  72476. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_core_if.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  72477. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 1970-01-01 01:00:00.000000000 +0100
  72478. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_core_if.h 2014-09-14 19:04:13.000000000 +0200
  72479. @@ -0,0 +1,705 @@
  72480. +/* ==========================================================================
  72481. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  72482. + * $Revision: #13 $
  72483. + * $Date: 2012/08/10 $
  72484. + * $Change: 2047372 $
  72485. + *
  72486. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  72487. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  72488. + * otherwise expressly agreed to in writing between Synopsys and you.
  72489. + *
  72490. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  72491. + * any End User Software License Agreement or Agreement for Licensed Product
  72492. + * with Synopsys or any supplement thereto. You are permitted to use and
  72493. + * redistribute this Software in source and binary forms, with or without
  72494. + * modification, provided that redistributions of source code must retain this
  72495. + * notice. You may not view, use, disclose, copy or distribute this file or
  72496. + * any information contained herein except pursuant to this license grant from
  72497. + * Synopsys. If you do not agree with this notice, including the disclaimer
  72498. + * below, then you are not authorized to use the Software.
  72499. + *
  72500. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  72501. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  72502. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  72503. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  72504. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  72505. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  72506. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  72507. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  72508. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  72509. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  72510. + * DAMAGE.
  72511. + * ========================================================================== */
  72512. +#if !defined(__DWC_CORE_IF_H__)
  72513. +#define __DWC_CORE_IF_H__
  72514. +
  72515. +#include "dwc_os.h"
  72516. +
  72517. +/** @file
  72518. + * This file defines DWC_OTG Core API
  72519. + */
  72520. +
  72521. +struct dwc_otg_core_if;
  72522. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  72523. +
  72524. +/** Maximum number of Periodic FIFOs */
  72525. +#define MAX_PERIO_FIFOS 15
  72526. +/** Maximum number of Periodic FIFOs */
  72527. +#define MAX_TX_FIFOS 15
  72528. +
  72529. +/** Maximum number of Endpoints/HostChannels */
  72530. +#define MAX_EPS_CHANNELS 16
  72531. +
  72532. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  72533. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  72534. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  72535. +
  72536. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  72537. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  72538. +
  72539. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  72540. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  72541. +
  72542. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  72543. +
  72544. +/** This function should be called on every hardware interrupt. */
  72545. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  72546. +
  72547. +/** @name OTG Core Parameters */
  72548. +/** @{ */
  72549. +
  72550. +/**
  72551. + * Specifies the OTG capabilities. The driver will automatically
  72552. + * detect the value for this parameter if none is specified.
  72553. + * 0 - HNP and SRP capable (default)
  72554. + * 1 - SRP Only capable
  72555. + * 2 - No HNP/SRP capable
  72556. + */
  72557. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  72558. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  72559. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  72560. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  72561. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  72562. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  72563. +
  72564. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  72565. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  72566. +#define dwc_param_opt_default 1
  72567. +
  72568. +/**
  72569. + * Specifies whether to use slave or DMA mode for accessing the data
  72570. + * FIFOs. The driver will automatically detect the value for this
  72571. + * parameter if none is specified.
  72572. + * 0 - Slave
  72573. + * 1 - DMA (default, if available)
  72574. + */
  72575. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  72576. + int32_t val);
  72577. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  72578. +#define dwc_param_dma_enable_default 1
  72579. +
  72580. +/**
  72581. + * When DMA mode is enabled specifies whether to use
  72582. + * address DMA or DMA Descritor mode for accessing the data
  72583. + * FIFOs in device mode. The driver will automatically detect
  72584. + * the value for this parameter if none is specified.
  72585. + * 0 - address DMA
  72586. + * 1 - DMA Descriptor(default, if available)
  72587. + */
  72588. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  72589. + int32_t val);
  72590. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  72591. +//#define dwc_param_dma_desc_enable_default 1
  72592. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  72593. +
  72594. +/** The DMA Burst size (applicable only for External DMA
  72595. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  72596. + */
  72597. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  72598. + int32_t val);
  72599. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  72600. +#define dwc_param_dma_burst_size_default 32
  72601. +
  72602. +/**
  72603. + * Specifies the maximum speed of operation in host and device mode.
  72604. + * The actual speed depends on the speed of the attached device and
  72605. + * the value of phy_type. The actual speed depends on the speed of the
  72606. + * attached device.
  72607. + * 0 - High Speed (default)
  72608. + * 1 - Full Speed
  72609. + */
  72610. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  72611. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  72612. +#define dwc_param_speed_default 0
  72613. +#define DWC_SPEED_PARAM_HIGH 0
  72614. +#define DWC_SPEED_PARAM_FULL 1
  72615. +
  72616. +/** Specifies whether low power mode is supported when attached
  72617. + * to a Full Speed or Low Speed device in host mode.
  72618. + * 0 - Don't support low power mode (default)
  72619. + * 1 - Support low power mode
  72620. + */
  72621. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  72622. + core_if, int32_t val);
  72623. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  72624. + * core_if);
  72625. +#define dwc_param_host_support_fs_ls_low_power_default 0
  72626. +
  72627. +/** Specifies the PHY clock rate in low power mode when connected to a
  72628. + * Low Speed device in host mode. This parameter is applicable only if
  72629. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  72630. + * then defaults to 6 MHZ otherwise 48 MHZ.
  72631. + *
  72632. + * 0 - 48 MHz
  72633. + * 1 - 6 MHz
  72634. + */
  72635. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  72636. + core_if, int32_t val);
  72637. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  72638. + core_if);
  72639. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  72640. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  72641. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  72642. +
  72643. +/**
  72644. + * 0 - Use cC FIFO size parameters
  72645. + * 1 - Allow dynamic FIFO sizing (default)
  72646. + */
  72647. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  72648. + int32_t val);
  72649. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  72650. + core_if);
  72651. +#define dwc_param_enable_dynamic_fifo_default 1
  72652. +
  72653. +/** Total number of 4-byte words in the data FIFO memory. This
  72654. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  72655. + * Tx FIFOs.
  72656. + * 32 to 32768 (default 8192)
  72657. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  72658. + */
  72659. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  72660. + int32_t val);
  72661. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  72662. +//#define dwc_param_data_fifo_size_default 8192
  72663. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  72664. +
  72665. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  72666. + * FIFO sizing is enabled.
  72667. + * 16 to 32768 (default 1064)
  72668. + */
  72669. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  72670. + int32_t val);
  72671. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  72672. +#define dwc_param_dev_rx_fifo_size_default 1064
  72673. +
  72674. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  72675. + * when dynamic FIFO sizing is enabled.
  72676. + * 16 to 32768 (default 1024)
  72677. + */
  72678. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  72679. + core_if, int32_t val);
  72680. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  72681. + core_if);
  72682. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  72683. +
  72684. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  72685. + * mode when dynamic FIFO sizing is enabled.
  72686. + * 4 to 768 (default 256)
  72687. + */
  72688. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  72689. + int32_t val, int fifo_num);
  72690. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  72691. + core_if, int fifo_num);
  72692. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  72693. +
  72694. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  72695. + * FIFO sizing is enabled.
  72696. + * 16 to 32768 (default 1024)
  72697. + */
  72698. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  72699. + int32_t val);
  72700. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  72701. +//#define dwc_param_host_rx_fifo_size_default 1024
  72702. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  72703. +
  72704. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  72705. + * when Dynamic FIFO sizing is enabled in the core.
  72706. + * 16 to 32768 (default 1024)
  72707. + */
  72708. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  72709. + core_if, int32_t val);
  72710. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  72711. + core_if);
  72712. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  72713. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  72714. +
  72715. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  72716. + * FIFO sizing is enabled.
  72717. + * 16 to 32768 (default 1024)
  72718. + */
  72719. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  72720. + core_if, int32_t val);
  72721. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  72722. + core_if);
  72723. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  72724. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  72725. +
  72726. +/** The maximum transfer size supported in bytes.
  72727. + * 2047 to 65,535 (default 65,535)
  72728. + */
  72729. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  72730. + int32_t val);
  72731. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  72732. +#define dwc_param_max_transfer_size_default 65535
  72733. +
  72734. +/** The maximum number of packets in a transfer.
  72735. + * 15 to 511 (default 511)
  72736. + */
  72737. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  72738. + int32_t val);
  72739. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  72740. +#define dwc_param_max_packet_count_default 511
  72741. +
  72742. +/** The number of host channel registers to use.
  72743. + * 1 to 16 (default 12)
  72744. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  72745. + */
  72746. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  72747. + int32_t val);
  72748. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  72749. +//#define dwc_param_host_channels_default 12
  72750. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  72751. +
  72752. +/** The number of endpoints in addition to EP0 available for device
  72753. + * mode operations.
  72754. + * 1 to 15 (default 6 IN and OUT)
  72755. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  72756. + * endpoints in addition to EP0.
  72757. + */
  72758. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  72759. + int32_t val);
  72760. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  72761. +#define dwc_param_dev_endpoints_default 6
  72762. +
  72763. +/**
  72764. + * Specifies the type of PHY interface to use. By default, the driver
  72765. + * will automatically detect the phy_type.
  72766. + *
  72767. + * 0 - Full Speed PHY
  72768. + * 1 - UTMI+ (default)
  72769. + * 2 - ULPI
  72770. + */
  72771. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  72772. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  72773. +#define DWC_PHY_TYPE_PARAM_FS 0
  72774. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  72775. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  72776. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  72777. +
  72778. +/**
  72779. + * Specifies the UTMI+ Data Width. This parameter is
  72780. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  72781. + * PHY_TYPE, this parameter indicates the data width between
  72782. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  72783. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  72784. + * to "8 and 16 bits", meaning that the core has been
  72785. + * configured to work at either data path width.
  72786. + *
  72787. + * 8 or 16 bits (default 16)
  72788. + */
  72789. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  72790. + int32_t val);
  72791. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  72792. +//#define dwc_param_phy_utmi_width_default 16
  72793. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  72794. +
  72795. +/**
  72796. + * Specifies whether the ULPI operates at double or single
  72797. + * data rate. This parameter is only applicable if PHY_TYPE is
  72798. + * ULPI.
  72799. + *
  72800. + * 0 - single data rate ULPI interface with 8 bit wide data
  72801. + * bus (default)
  72802. + * 1 - double data rate ULPI interface with 4 bit wide data
  72803. + * bus
  72804. + */
  72805. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  72806. + int32_t val);
  72807. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  72808. +#define dwc_param_phy_ulpi_ddr_default 0
  72809. +
  72810. +/**
  72811. + * Specifies whether to use the internal or external supply to
  72812. + * drive the vbus with a ULPI phy.
  72813. + */
  72814. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  72815. + int32_t val);
  72816. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  72817. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  72818. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  72819. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  72820. +
  72821. +/**
  72822. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  72823. + * parameter is only applicable if PHY_TYPE is FS.
  72824. + * 0 - No (default)
  72825. + * 1 - Yes
  72826. + */
  72827. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  72828. + int32_t val);
  72829. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  72830. +#define dwc_param_i2c_enable_default 0
  72831. +
  72832. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  72833. + int32_t val);
  72834. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  72835. +#define dwc_param_ulpi_fs_ls_default 0
  72836. +
  72837. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  72838. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  72839. +#define dwc_param_ts_dline_default 0
  72840. +
  72841. +/**
  72842. + * Specifies whether dedicated transmit FIFOs are
  72843. + * enabled for non periodic IN endpoints in device mode
  72844. + * 0 - No
  72845. + * 1 - Yes
  72846. + */
  72847. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  72848. + int32_t val);
  72849. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  72850. + core_if);
  72851. +#define dwc_param_en_multiple_tx_fifo_default 1
  72852. +
  72853. +/** Number of 4-byte words in each of the Tx FIFOs in device
  72854. + * mode when dynamic FIFO sizing is enabled.
  72855. + * 4 to 768 (default 256)
  72856. + */
  72857. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  72858. + int fifo_num, int32_t val);
  72859. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  72860. + int fifo_num);
  72861. +#define dwc_param_dev_tx_fifo_size_default 768
  72862. +
  72863. +/** Thresholding enable flag-
  72864. + * bit 0 - enable non-ISO Tx thresholding
  72865. + * bit 1 - enable ISO Tx thresholding
  72866. + * bit 2 - enable Rx thresholding
  72867. + */
  72868. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  72869. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  72870. +#define dwc_param_thr_ctl_default 0
  72871. +
  72872. +/** Thresholding length for Tx
  72873. + * FIFOs in 32 bit DWORDs
  72874. + */
  72875. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  72876. + int32_t val);
  72877. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  72878. +#define dwc_param_tx_thr_length_default 64
  72879. +
  72880. +/** Thresholding length for Rx
  72881. + * FIFOs in 32 bit DWORDs
  72882. + */
  72883. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  72884. + int32_t val);
  72885. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  72886. +#define dwc_param_rx_thr_length_default 64
  72887. +
  72888. +/**
  72889. + * Specifies whether LPM (Link Power Management) support is enabled
  72890. + */
  72891. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  72892. + int32_t val);
  72893. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  72894. +#define dwc_param_lpm_enable_default 1
  72895. +
  72896. +/**
  72897. + * Specifies whether PTI enhancement is enabled
  72898. + */
  72899. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  72900. + int32_t val);
  72901. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  72902. +#define dwc_param_pti_enable_default 0
  72903. +
  72904. +/**
  72905. + * Specifies whether MPI enhancement is enabled
  72906. + */
  72907. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  72908. + int32_t val);
  72909. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  72910. +#define dwc_param_mpi_enable_default 0
  72911. +
  72912. +/**
  72913. + * Specifies whether ADP capability is enabled
  72914. + */
  72915. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  72916. + int32_t val);
  72917. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  72918. +#define dwc_param_adp_enable_default 0
  72919. +
  72920. +/**
  72921. + * Specifies whether IC_USB capability is enabled
  72922. + */
  72923. +
  72924. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  72925. + int32_t val);
  72926. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  72927. +#define dwc_param_ic_usb_cap_default 0
  72928. +
  72929. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  72930. + int32_t val);
  72931. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  72932. +#define dwc_param_ahb_thr_ratio_default 0
  72933. +
  72934. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  72935. + int32_t val);
  72936. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  72937. +#define dwc_param_power_down_default 0
  72938. +
  72939. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  72940. + int32_t val);
  72941. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  72942. +#define dwc_param_reload_ctl_default 0
  72943. +
  72944. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  72945. + int32_t val);
  72946. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  72947. +#define dwc_param_dev_out_nak_default 0
  72948. +
  72949. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  72950. + int32_t val);
  72951. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  72952. +#define dwc_param_cont_on_bna_default 0
  72953. +
  72954. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  72955. + int32_t val);
  72956. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  72957. +#define dwc_param_ahb_single_default 0
  72958. +
  72959. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  72960. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  72961. +#define dwc_param_otg_ver_default 0
  72962. +
  72963. +/** @} */
  72964. +
  72965. +/** @name Access to registers and bit-fields */
  72966. +
  72967. +/**
  72968. + * Dump core registers and SPRAM
  72969. + */
  72970. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  72971. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  72972. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  72973. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  72974. +
  72975. +/**
  72976. + * Get host negotiation status.
  72977. + */
  72978. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  72979. +
  72980. +/**
  72981. + * Get srp status
  72982. + */
  72983. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  72984. +
  72985. +/**
  72986. + * Set hnpreq bit in the GOTGCTL register.
  72987. + */
  72988. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  72989. +
  72990. +/**
  72991. + * Get Content of SNPSID register.
  72992. + */
  72993. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  72994. +
  72995. +/**
  72996. + * Get current mode.
  72997. + * Returns 0 if in device mode, and 1 if in host mode.
  72998. + */
  72999. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  73000. +
  73001. +/**
  73002. + * Get value of hnpcapable field in the GUSBCFG register
  73003. + */
  73004. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  73005. +/**
  73006. + * Set value of hnpcapable field in the GUSBCFG register
  73007. + */
  73008. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  73009. +
  73010. +/**
  73011. + * Get value of srpcapable field in the GUSBCFG register
  73012. + */
  73013. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  73014. +/**
  73015. + * Set value of srpcapable field in the GUSBCFG register
  73016. + */
  73017. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  73018. +
  73019. +/**
  73020. + * Get value of devspeed field in the DCFG register
  73021. + */
  73022. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  73023. +/**
  73024. + * Set value of devspeed field in the DCFG register
  73025. + */
  73026. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  73027. +
  73028. +/**
  73029. + * Get the value of busconnected field from the HPRT0 register
  73030. + */
  73031. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  73032. +
  73033. +/**
  73034. + * Gets the device enumeration Speed.
  73035. + */
  73036. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  73037. +
  73038. +/**
  73039. + * Get value of prtpwr field from the HPRT0 register
  73040. + */
  73041. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  73042. +
  73043. +/**
  73044. + * Get value of flag indicating core state - hibernated or not
  73045. + */
  73046. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  73047. +
  73048. +/**
  73049. + * Set value of prtpwr field from the HPRT0 register
  73050. + */
  73051. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  73052. +
  73053. +/**
  73054. + * Get value of prtsusp field from the HPRT0 regsiter
  73055. + */
  73056. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  73057. +/**
  73058. + * Set value of prtpwr field from the HPRT0 register
  73059. + */
  73060. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  73061. +
  73062. +/**
  73063. + * Get value of ModeChTimEn field from the HCFG regsiter
  73064. + */
  73065. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  73066. +/**
  73067. + * Set value of ModeChTimEn field from the HCFG regsiter
  73068. + */
  73069. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  73070. +
  73071. +/**
  73072. + * Get value of Fram Interval field from the HFIR regsiter
  73073. + */
  73074. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  73075. +/**
  73076. + * Set value of Frame Interval field from the HFIR regsiter
  73077. + */
  73078. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  73079. +
  73080. +/**
  73081. + * Set value of prtres field from the HPRT0 register
  73082. + *FIXME Remove?
  73083. + */
  73084. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  73085. +
  73086. +/**
  73087. + * Get value of rmtwkupsig bit in DCTL register
  73088. + */
  73089. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  73090. +
  73091. +/**
  73092. + * Get value of prt_sleep_sts field from the GLPMCFG register
  73093. + */
  73094. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  73095. +
  73096. +/**
  73097. + * Get value of rem_wkup_en field from the GLPMCFG register
  73098. + */
  73099. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  73100. +
  73101. +/**
  73102. + * Get value of appl_resp field from the GLPMCFG register
  73103. + */
  73104. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  73105. +/**
  73106. + * Set value of appl_resp field from the GLPMCFG register
  73107. + */
  73108. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  73109. +
  73110. +/**
  73111. + * Get value of hsic_connect field from the GLPMCFG register
  73112. + */
  73113. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  73114. +/**
  73115. + * Set value of hsic_connect field from the GLPMCFG register
  73116. + */
  73117. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  73118. +
  73119. +/**
  73120. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  73121. + */
  73122. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  73123. +/**
  73124. + * Set value of inv_sel_hsic field from the GLPMFG register.
  73125. + */
  73126. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  73127. +
  73128. +/*
  73129. + * Some functions for accessing registers
  73130. + */
  73131. +
  73132. +/**
  73133. + * GOTGCTL register
  73134. + */
  73135. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  73136. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  73137. +
  73138. +/**
  73139. + * GUSBCFG register
  73140. + */
  73141. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  73142. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  73143. +
  73144. +/**
  73145. + * GRXFSIZ register
  73146. + */
  73147. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  73148. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  73149. +
  73150. +/**
  73151. + * GNPTXFSIZ register
  73152. + */
  73153. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  73154. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  73155. +
  73156. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  73157. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  73158. +
  73159. +/**
  73160. + * GGPIO register
  73161. + */
  73162. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  73163. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  73164. +
  73165. +/**
  73166. + * GUID register
  73167. + */
  73168. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  73169. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  73170. +
  73171. +/**
  73172. + * HPRT0 register
  73173. + */
  73174. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  73175. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  73176. +
  73177. +/**
  73178. + * GHPTXFSIZE
  73179. + */
  73180. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  73181. +
  73182. +/** @} */
  73183. +
  73184. +#endif /* __DWC_CORE_IF_H__ */
  73185. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_dbg.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  73186. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 1970-01-01 01:00:00.000000000 +0100
  73187. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_dbg.h 2014-09-14 19:04:13.000000000 +0200
  73188. @@ -0,0 +1,117 @@
  73189. +/* ==========================================================================
  73190. + *
  73191. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  73192. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  73193. + * otherwise expressly agreed to in writing between Synopsys and you.
  73194. + *
  73195. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  73196. + * any End User Software License Agreement or Agreement for Licensed Product
  73197. + * with Synopsys or any supplement thereto. You are permitted to use and
  73198. + * redistribute this Software in source and binary forms, with or without
  73199. + * modification, provided that redistributions of source code must retain this
  73200. + * notice. You may not view, use, disclose, copy or distribute this file or
  73201. + * any information contained herein except pursuant to this license grant from
  73202. + * Synopsys. If you do not agree with this notice, including the disclaimer
  73203. + * below, then you are not authorized to use the Software.
  73204. + *
  73205. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  73206. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73207. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  73208. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  73209. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73210. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73211. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73212. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73213. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73214. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  73215. + * DAMAGE.
  73216. + * ========================================================================== */
  73217. +
  73218. +#ifndef __DWC_OTG_DBG_H__
  73219. +#define __DWC_OTG_DBG_H__
  73220. +
  73221. +/** @file
  73222. + * This file defines debug levels.
  73223. + * Debugging support vanishes in non-debug builds.
  73224. + */
  73225. +
  73226. +/**
  73227. + * The Debug Level bit-mask variable.
  73228. + */
  73229. +extern uint32_t g_dbg_lvl;
  73230. +/**
  73231. + * Set the Debug Level variable.
  73232. + */
  73233. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  73234. +{
  73235. + uint32_t old = g_dbg_lvl;
  73236. + g_dbg_lvl = new;
  73237. + return old;
  73238. +}
  73239. +
  73240. +#define DBG_USER (0x1)
  73241. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  73242. +#define DBG_CIL (0x2)
  73243. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  73244. + * messages */
  73245. +#define DBG_CILV (0x20)
  73246. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  73247. + * messages */
  73248. +#define DBG_PCD (0x4)
  73249. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  73250. + * messages */
  73251. +#define DBG_PCDV (0x40)
  73252. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  73253. +#define DBG_HCD (0x8)
  73254. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  73255. + * messages */
  73256. +#define DBG_HCDV (0x80)
  73257. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  73258. + * mode. */
  73259. +#define DBG_HCD_URB (0x800)
  73260. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  73261. + * messages. */
  73262. +#define DBG_HCDI (0x1000)
  73263. +
  73264. +/** When debug level has any bit set, display debug messages */
  73265. +#define DBG_ANY (0xFF)
  73266. +
  73267. +/** All debug messages off */
  73268. +#define DBG_OFF 0
  73269. +
  73270. +/** Prefix string for DWC_DEBUG print macros. */
  73271. +#define USB_DWC "DWC_otg: "
  73272. +
  73273. +/**
  73274. + * Print a debug message when the Global debug level variable contains
  73275. + * the bit defined in <code>lvl</code>.
  73276. + *
  73277. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  73278. + * @param[in] x - like printf
  73279. + *
  73280. + * Example:<p>
  73281. + * <code>
  73282. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  73283. + * </code>
  73284. + * <br>
  73285. + * results in:<br>
  73286. + * <code>
  73287. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  73288. + * </code>
  73289. + */
  73290. +#ifdef DEBUG
  73291. +
  73292. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  73293. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  73294. +
  73295. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  73296. +
  73297. +#else
  73298. +
  73299. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  73300. +# define DWC_DEBUGP(x...)
  73301. +
  73302. +# define CHK_DEBUG_LEVEL(level) (0)
  73303. +
  73304. +#endif /*DEBUG*/
  73305. +#endif
  73306. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_driver.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  73307. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_driver.c 1970-01-01 01:00:00.000000000 +0100
  73308. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.c 2014-09-14 19:04:13.000000000 +0200
  73309. @@ -0,0 +1,1749 @@
  73310. +/* ==========================================================================
  73311. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  73312. + * $Revision: #92 $
  73313. + * $Date: 2012/08/10 $
  73314. + * $Change: 2047372 $
  73315. + *
  73316. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  73317. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  73318. + * otherwise expressly agreed to in writing between Synopsys and you.
  73319. + *
  73320. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  73321. + * any End User Software License Agreement or Agreement for Licensed Product
  73322. + * with Synopsys or any supplement thereto. You are permitted to use and
  73323. + * redistribute this Software in source and binary forms, with or without
  73324. + * modification, provided that redistributions of source code must retain this
  73325. + * notice. You may not view, use, disclose, copy or distribute this file or
  73326. + * any information contained herein except pursuant to this license grant from
  73327. + * Synopsys. If you do not agree with this notice, including the disclaimer
  73328. + * below, then you are not authorized to use the Software.
  73329. + *
  73330. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  73331. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  73332. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  73333. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  73334. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  73335. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73336. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  73337. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73338. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  73339. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  73340. + * DAMAGE.
  73341. + * ========================================================================== */
  73342. +
  73343. +/** @file
  73344. + * The dwc_otg_driver module provides the initialization and cleanup entry
  73345. + * points for the DWC_otg driver. This module will be dynamically installed
  73346. + * after Linux is booted using the insmod command. When the module is
  73347. + * installed, the dwc_otg_driver_init function is called. When the module is
  73348. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  73349. + *
  73350. + * This module also defines a data structure for the dwc_otg_driver, which is
  73351. + * used in conjunction with the standard ARM lm_device structure. These
  73352. + * structures allow the OTG driver to comply with the standard Linux driver
  73353. + * model in which devices and drivers are registered with a bus driver. This
  73354. + * has the benefit that Linux can expose attributes of the driver and device
  73355. + * in its special sysfs file system. Users can then read or write files in
  73356. + * this file system to perform diagnostics on the driver components or the
  73357. + * device.
  73358. + */
  73359. +
  73360. +#include "dwc_otg_os_dep.h"
  73361. +#include "dwc_os.h"
  73362. +#include "dwc_otg_dbg.h"
  73363. +#include "dwc_otg_driver.h"
  73364. +#include "dwc_otg_attr.h"
  73365. +#include "dwc_otg_core_if.h"
  73366. +#include "dwc_otg_pcd_if.h"
  73367. +#include "dwc_otg_hcd_if.h"
  73368. +#include "dwc_otg_fiq_fsm.h"
  73369. +
  73370. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  73371. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  73372. +
  73373. +bool microframe_schedule=true;
  73374. +
  73375. +static const char dwc_driver_name[] = "dwc_otg";
  73376. +
  73377. +
  73378. +extern int pcd_init(
  73379. +#ifdef LM_INTERFACE
  73380. + struct lm_device *_dev
  73381. +#elif defined(PCI_INTERFACE)
  73382. + struct pci_dev *_dev
  73383. +#elif defined(PLATFORM_INTERFACE)
  73384. + struct platform_device *dev
  73385. +#endif
  73386. + );
  73387. +extern int hcd_init(
  73388. +#ifdef LM_INTERFACE
  73389. + struct lm_device *_dev
  73390. +#elif defined(PCI_INTERFACE)
  73391. + struct pci_dev *_dev
  73392. +#elif defined(PLATFORM_INTERFACE)
  73393. + struct platform_device *dev
  73394. +#endif
  73395. + );
  73396. +
  73397. +extern int pcd_remove(
  73398. +#ifdef LM_INTERFACE
  73399. + struct lm_device *_dev
  73400. +#elif defined(PCI_INTERFACE)
  73401. + struct pci_dev *_dev
  73402. +#elif defined(PLATFORM_INTERFACE)
  73403. + struct platform_device *_dev
  73404. +#endif
  73405. + );
  73406. +
  73407. +extern void hcd_remove(
  73408. +#ifdef LM_INTERFACE
  73409. + struct lm_device *_dev
  73410. +#elif defined(PCI_INTERFACE)
  73411. + struct pci_dev *_dev
  73412. +#elif defined(PLATFORM_INTERFACE)
  73413. + struct platform_device *_dev
  73414. +#endif
  73415. + );
  73416. +
  73417. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  73418. +
  73419. +/*-------------------------------------------------------------------------*/
  73420. +/* Encapsulate the module parameter settings */
  73421. +
  73422. +struct dwc_otg_driver_module_params {
  73423. + int32_t opt;
  73424. + int32_t otg_cap;
  73425. + int32_t dma_enable;
  73426. + int32_t dma_desc_enable;
  73427. + int32_t dma_burst_size;
  73428. + int32_t speed;
  73429. + int32_t host_support_fs_ls_low_power;
  73430. + int32_t host_ls_low_power_phy_clk;
  73431. + int32_t enable_dynamic_fifo;
  73432. + int32_t data_fifo_size;
  73433. + int32_t dev_rx_fifo_size;
  73434. + int32_t dev_nperio_tx_fifo_size;
  73435. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  73436. + int32_t host_rx_fifo_size;
  73437. + int32_t host_nperio_tx_fifo_size;
  73438. + int32_t host_perio_tx_fifo_size;
  73439. + int32_t max_transfer_size;
  73440. + int32_t max_packet_count;
  73441. + int32_t host_channels;
  73442. + int32_t dev_endpoints;
  73443. + int32_t phy_type;
  73444. + int32_t phy_utmi_width;
  73445. + int32_t phy_ulpi_ddr;
  73446. + int32_t phy_ulpi_ext_vbus;
  73447. + int32_t i2c_enable;
  73448. + int32_t ulpi_fs_ls;
  73449. + int32_t ts_dline;
  73450. + int32_t en_multiple_tx_fifo;
  73451. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  73452. + uint32_t thr_ctl;
  73453. + uint32_t tx_thr_length;
  73454. + uint32_t rx_thr_length;
  73455. + int32_t pti_enable;
  73456. + int32_t mpi_enable;
  73457. + int32_t lpm_enable;
  73458. + int32_t ic_usb_cap;
  73459. + int32_t ahb_thr_ratio;
  73460. + int32_t power_down;
  73461. + int32_t reload_ctl;
  73462. + int32_t dev_out_nak;
  73463. + int32_t cont_on_bna;
  73464. + int32_t ahb_single;
  73465. + int32_t otg_ver;
  73466. + int32_t adp_enable;
  73467. +};
  73468. +
  73469. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  73470. + .opt = -1,
  73471. + .otg_cap = -1,
  73472. + .dma_enable = -1,
  73473. + .dma_desc_enable = -1,
  73474. + .dma_burst_size = -1,
  73475. + .speed = -1,
  73476. + .host_support_fs_ls_low_power = -1,
  73477. + .host_ls_low_power_phy_clk = -1,
  73478. + .enable_dynamic_fifo = -1,
  73479. + .data_fifo_size = -1,
  73480. + .dev_rx_fifo_size = -1,
  73481. + .dev_nperio_tx_fifo_size = -1,
  73482. + .dev_perio_tx_fifo_size = {
  73483. + /* dev_perio_tx_fifo_size_1 */
  73484. + -1,
  73485. + -1,
  73486. + -1,
  73487. + -1,
  73488. + -1,
  73489. + -1,
  73490. + -1,
  73491. + -1,
  73492. + -1,
  73493. + -1,
  73494. + -1,
  73495. + -1,
  73496. + -1,
  73497. + -1,
  73498. + -1
  73499. + /* 15 */
  73500. + },
  73501. + .host_rx_fifo_size = -1,
  73502. + .host_nperio_tx_fifo_size = -1,
  73503. + .host_perio_tx_fifo_size = -1,
  73504. + .max_transfer_size = -1,
  73505. + .max_packet_count = -1,
  73506. + .host_channels = -1,
  73507. + .dev_endpoints = -1,
  73508. + .phy_type = -1,
  73509. + .phy_utmi_width = -1,
  73510. + .phy_ulpi_ddr = -1,
  73511. + .phy_ulpi_ext_vbus = -1,
  73512. + .i2c_enable = -1,
  73513. + .ulpi_fs_ls = -1,
  73514. + .ts_dline = -1,
  73515. + .en_multiple_tx_fifo = -1,
  73516. + .dev_tx_fifo_size = {
  73517. + /* dev_tx_fifo_size */
  73518. + -1,
  73519. + -1,
  73520. + -1,
  73521. + -1,
  73522. + -1,
  73523. + -1,
  73524. + -1,
  73525. + -1,
  73526. + -1,
  73527. + -1,
  73528. + -1,
  73529. + -1,
  73530. + -1,
  73531. + -1,
  73532. + -1
  73533. + /* 15 */
  73534. + },
  73535. + .thr_ctl = -1,
  73536. + .tx_thr_length = -1,
  73537. + .rx_thr_length = -1,
  73538. + .pti_enable = -1,
  73539. + .mpi_enable = -1,
  73540. + .lpm_enable = 0,
  73541. + .ic_usb_cap = -1,
  73542. + .ahb_thr_ratio = -1,
  73543. + .power_down = -1,
  73544. + .reload_ctl = -1,
  73545. + .dev_out_nak = -1,
  73546. + .cont_on_bna = -1,
  73547. + .ahb_single = -1,
  73548. + .otg_ver = -1,
  73549. + .adp_enable = -1,
  73550. +};
  73551. +
  73552. +//Global variable to switch the fiq fix on or off
  73553. +bool fiq_enable = 1;
  73554. +// Global variable to enable the split transaction fix
  73555. +bool fiq_fsm_enable = true;
  73556. +//Bulk split-transaction NAK holdoff in microframes
  73557. +uint16_t nak_holdoff = 8;
  73558. +
  73559. +unsigned short fiq_fsm_mask = 0x07;
  73560. +
  73561. +/**
  73562. + * This function shows the Driver Version.
  73563. + */
  73564. +static ssize_t version_show(struct device_driver *dev, char *buf)
  73565. +{
  73566. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  73567. + DWC_DRIVER_VERSION);
  73568. +}
  73569. +
  73570. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  73571. +
  73572. +/**
  73573. + * Global Debug Level Mask.
  73574. + */
  73575. +uint32_t g_dbg_lvl = 0; /* OFF */
  73576. +
  73577. +/**
  73578. + * This function shows the driver Debug Level.
  73579. + */
  73580. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  73581. +{
  73582. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  73583. +}
  73584. +
  73585. +/**
  73586. + * This function stores the driver Debug Level.
  73587. + */
  73588. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  73589. + size_t count)
  73590. +{
  73591. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  73592. + return count;
  73593. +}
  73594. +
  73595. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  73596. + dbg_level_store);
  73597. +
  73598. +/**
  73599. + * This function is called during module intialization
  73600. + * to pass module parameters to the DWC_OTG CORE.
  73601. + */
  73602. +static int set_parameters(dwc_otg_core_if_t * core_if)
  73603. +{
  73604. + int retval = 0;
  73605. + int i;
  73606. +
  73607. + if (dwc_otg_module_params.otg_cap != -1) {
  73608. + retval +=
  73609. + dwc_otg_set_param_otg_cap(core_if,
  73610. + dwc_otg_module_params.otg_cap);
  73611. + }
  73612. + if (dwc_otg_module_params.dma_enable != -1) {
  73613. + retval +=
  73614. + dwc_otg_set_param_dma_enable(core_if,
  73615. + dwc_otg_module_params.
  73616. + dma_enable);
  73617. + }
  73618. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  73619. + retval +=
  73620. + dwc_otg_set_param_dma_desc_enable(core_if,
  73621. + dwc_otg_module_params.
  73622. + dma_desc_enable);
  73623. + }
  73624. + if (dwc_otg_module_params.opt != -1) {
  73625. + retval +=
  73626. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  73627. + }
  73628. + if (dwc_otg_module_params.dma_burst_size != -1) {
  73629. + retval +=
  73630. + dwc_otg_set_param_dma_burst_size(core_if,
  73631. + dwc_otg_module_params.
  73632. + dma_burst_size);
  73633. + }
  73634. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  73635. + retval +=
  73636. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  73637. + dwc_otg_module_params.
  73638. + host_support_fs_ls_low_power);
  73639. + }
  73640. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  73641. + retval +=
  73642. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  73643. + dwc_otg_module_params.
  73644. + enable_dynamic_fifo);
  73645. + }
  73646. + if (dwc_otg_module_params.data_fifo_size != -1) {
  73647. + retval +=
  73648. + dwc_otg_set_param_data_fifo_size(core_if,
  73649. + dwc_otg_module_params.
  73650. + data_fifo_size);
  73651. + }
  73652. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  73653. + retval +=
  73654. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  73655. + dwc_otg_module_params.
  73656. + dev_rx_fifo_size);
  73657. + }
  73658. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  73659. + retval +=
  73660. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  73661. + dwc_otg_module_params.
  73662. + dev_nperio_tx_fifo_size);
  73663. + }
  73664. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  73665. + retval +=
  73666. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  73667. + dwc_otg_module_params.host_rx_fifo_size);
  73668. + }
  73669. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  73670. + retval +=
  73671. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  73672. + dwc_otg_module_params.
  73673. + host_nperio_tx_fifo_size);
  73674. + }
  73675. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  73676. + retval +=
  73677. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  73678. + dwc_otg_module_params.
  73679. + host_perio_tx_fifo_size);
  73680. + }
  73681. + if (dwc_otg_module_params.max_transfer_size != -1) {
  73682. + retval +=
  73683. + dwc_otg_set_param_max_transfer_size(core_if,
  73684. + dwc_otg_module_params.
  73685. + max_transfer_size);
  73686. + }
  73687. + if (dwc_otg_module_params.max_packet_count != -1) {
  73688. + retval +=
  73689. + dwc_otg_set_param_max_packet_count(core_if,
  73690. + dwc_otg_module_params.
  73691. + max_packet_count);
  73692. + }
  73693. + if (dwc_otg_module_params.host_channels != -1) {
  73694. + retval +=
  73695. + dwc_otg_set_param_host_channels(core_if,
  73696. + dwc_otg_module_params.
  73697. + host_channels);
  73698. + }
  73699. + if (dwc_otg_module_params.dev_endpoints != -1) {
  73700. + retval +=
  73701. + dwc_otg_set_param_dev_endpoints(core_if,
  73702. + dwc_otg_module_params.
  73703. + dev_endpoints);
  73704. + }
  73705. + if (dwc_otg_module_params.phy_type != -1) {
  73706. + retval +=
  73707. + dwc_otg_set_param_phy_type(core_if,
  73708. + dwc_otg_module_params.phy_type);
  73709. + }
  73710. + if (dwc_otg_module_params.speed != -1) {
  73711. + retval +=
  73712. + dwc_otg_set_param_speed(core_if,
  73713. + dwc_otg_module_params.speed);
  73714. + }
  73715. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  73716. + retval +=
  73717. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  73718. + dwc_otg_module_params.
  73719. + host_ls_low_power_phy_clk);
  73720. + }
  73721. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  73722. + retval +=
  73723. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  73724. + dwc_otg_module_params.
  73725. + phy_ulpi_ddr);
  73726. + }
  73727. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  73728. + retval +=
  73729. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  73730. + dwc_otg_module_params.
  73731. + phy_ulpi_ext_vbus);
  73732. + }
  73733. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  73734. + retval +=
  73735. + dwc_otg_set_param_phy_utmi_width(core_if,
  73736. + dwc_otg_module_params.
  73737. + phy_utmi_width);
  73738. + }
  73739. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  73740. + retval +=
  73741. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  73742. + dwc_otg_module_params.ulpi_fs_ls);
  73743. + }
  73744. + if (dwc_otg_module_params.ts_dline != -1) {
  73745. + retval +=
  73746. + dwc_otg_set_param_ts_dline(core_if,
  73747. + dwc_otg_module_params.ts_dline);
  73748. + }
  73749. + if (dwc_otg_module_params.i2c_enable != -1) {
  73750. + retval +=
  73751. + dwc_otg_set_param_i2c_enable(core_if,
  73752. + dwc_otg_module_params.
  73753. + i2c_enable);
  73754. + }
  73755. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  73756. + retval +=
  73757. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  73758. + dwc_otg_module_params.
  73759. + en_multiple_tx_fifo);
  73760. + }
  73761. + for (i = 0; i < 15; i++) {
  73762. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  73763. + retval +=
  73764. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  73765. + dwc_otg_module_params.
  73766. + dev_perio_tx_fifo_size
  73767. + [i], i);
  73768. + }
  73769. + }
  73770. +
  73771. + for (i = 0; i < 15; i++) {
  73772. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  73773. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  73774. + dwc_otg_module_params.
  73775. + dev_tx_fifo_size
  73776. + [i], i);
  73777. + }
  73778. + }
  73779. + if (dwc_otg_module_params.thr_ctl != -1) {
  73780. + retval +=
  73781. + dwc_otg_set_param_thr_ctl(core_if,
  73782. + dwc_otg_module_params.thr_ctl);
  73783. + }
  73784. + if (dwc_otg_module_params.mpi_enable != -1) {
  73785. + retval +=
  73786. + dwc_otg_set_param_mpi_enable(core_if,
  73787. + dwc_otg_module_params.
  73788. + mpi_enable);
  73789. + }
  73790. + if (dwc_otg_module_params.pti_enable != -1) {
  73791. + retval +=
  73792. + dwc_otg_set_param_pti_enable(core_if,
  73793. + dwc_otg_module_params.
  73794. + pti_enable);
  73795. + }
  73796. + if (dwc_otg_module_params.lpm_enable != -1) {
  73797. + retval +=
  73798. + dwc_otg_set_param_lpm_enable(core_if,
  73799. + dwc_otg_module_params.
  73800. + lpm_enable);
  73801. + }
  73802. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  73803. + retval +=
  73804. + dwc_otg_set_param_ic_usb_cap(core_if,
  73805. + dwc_otg_module_params.
  73806. + ic_usb_cap);
  73807. + }
  73808. + if (dwc_otg_module_params.tx_thr_length != -1) {
  73809. + retval +=
  73810. + dwc_otg_set_param_tx_thr_length(core_if,
  73811. + dwc_otg_module_params.tx_thr_length);
  73812. + }
  73813. + if (dwc_otg_module_params.rx_thr_length != -1) {
  73814. + retval +=
  73815. + dwc_otg_set_param_rx_thr_length(core_if,
  73816. + dwc_otg_module_params.
  73817. + rx_thr_length);
  73818. + }
  73819. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  73820. + retval +=
  73821. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  73822. + dwc_otg_module_params.ahb_thr_ratio);
  73823. + }
  73824. + if (dwc_otg_module_params.power_down != -1) {
  73825. + retval +=
  73826. + dwc_otg_set_param_power_down(core_if,
  73827. + dwc_otg_module_params.power_down);
  73828. + }
  73829. + if (dwc_otg_module_params.reload_ctl != -1) {
  73830. + retval +=
  73831. + dwc_otg_set_param_reload_ctl(core_if,
  73832. + dwc_otg_module_params.reload_ctl);
  73833. + }
  73834. +
  73835. + if (dwc_otg_module_params.dev_out_nak != -1) {
  73836. + retval +=
  73837. + dwc_otg_set_param_dev_out_nak(core_if,
  73838. + dwc_otg_module_params.dev_out_nak);
  73839. + }
  73840. +
  73841. + if (dwc_otg_module_params.cont_on_bna != -1) {
  73842. + retval +=
  73843. + dwc_otg_set_param_cont_on_bna(core_if,
  73844. + dwc_otg_module_params.cont_on_bna);
  73845. + }
  73846. +
  73847. + if (dwc_otg_module_params.ahb_single != -1) {
  73848. + retval +=
  73849. + dwc_otg_set_param_ahb_single(core_if,
  73850. + dwc_otg_module_params.ahb_single);
  73851. + }
  73852. +
  73853. + if (dwc_otg_module_params.otg_ver != -1) {
  73854. + retval +=
  73855. + dwc_otg_set_param_otg_ver(core_if,
  73856. + dwc_otg_module_params.otg_ver);
  73857. + }
  73858. + if (dwc_otg_module_params.adp_enable != -1) {
  73859. + retval +=
  73860. + dwc_otg_set_param_adp_enable(core_if,
  73861. + dwc_otg_module_params.
  73862. + adp_enable);
  73863. + }
  73864. + return retval;
  73865. +}
  73866. +
  73867. +/**
  73868. + * This function is the top level interrupt handler for the Common
  73869. + * (Device and host modes) interrupts.
  73870. + */
  73871. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  73872. +{
  73873. + int32_t retval = IRQ_NONE;
  73874. +
  73875. + retval = dwc_otg_handle_common_intr(dev);
  73876. + if (retval != 0) {
  73877. + S3C2410X_CLEAR_EINTPEND();
  73878. + }
  73879. + return IRQ_RETVAL(retval);
  73880. +}
  73881. +
  73882. +/**
  73883. + * This function is called when a lm_device is unregistered with the
  73884. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  73885. + * executed. The device may or may not be electrically present. If it is
  73886. + * present, the driver stops device processing. Any resources used on behalf
  73887. + * of this device are freed.
  73888. + *
  73889. + * @param _dev
  73890. + */
  73891. +#ifdef LM_INTERFACE
  73892. +#define REM_RETVAL(n)
  73893. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  73894. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  73895. +#elif defined(PCI_INTERFACE)
  73896. +#define REM_RETVAL(n)
  73897. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  73898. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  73899. +#elif defined(PLATFORM_INTERFACE)
  73900. +#define REM_RETVAL(n) n
  73901. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  73902. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  73903. +#endif
  73904. +
  73905. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  73906. +
  73907. + if (!otg_dev) {
  73908. + /* Memory allocation for the dwc_otg_device failed. */
  73909. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  73910. + return REM_RETVAL(-ENOMEM);
  73911. + }
  73912. +#ifndef DWC_DEVICE_ONLY
  73913. + if (otg_dev->hcd) {
  73914. + hcd_remove(_dev);
  73915. + } else {
  73916. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  73917. + return REM_RETVAL(-EINVAL);
  73918. + }
  73919. +#endif
  73920. +
  73921. +#ifndef DWC_HOST_ONLY
  73922. + if (otg_dev->pcd) {
  73923. + pcd_remove(_dev);
  73924. + } else {
  73925. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  73926. + return REM_RETVAL(-EINVAL);
  73927. + }
  73928. +#endif
  73929. + /*
  73930. + * Free the IRQ
  73931. + */
  73932. + if (otg_dev->common_irq_installed) {
  73933. +#ifdef PLATFORM_INTERFACE
  73934. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  73935. +#else
  73936. + free_irq(_dev->irq, otg_dev);
  73937. +#endif
  73938. + } else {
  73939. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  73940. + return REM_RETVAL(-ENXIO);
  73941. + }
  73942. +
  73943. + if (otg_dev->core_if) {
  73944. + dwc_otg_cil_remove(otg_dev->core_if);
  73945. + } else {
  73946. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  73947. + return REM_RETVAL(-ENXIO);
  73948. + }
  73949. +
  73950. + /*
  73951. + * Remove the device attributes
  73952. + */
  73953. + dwc_otg_attr_remove(_dev);
  73954. +
  73955. + /*
  73956. + * Return the memory.
  73957. + */
  73958. + if (otg_dev->os_dep.base) {
  73959. + iounmap(otg_dev->os_dep.base);
  73960. + }
  73961. + DWC_FREE(otg_dev);
  73962. +
  73963. + /*
  73964. + * Clear the drvdata pointer.
  73965. + */
  73966. +#ifdef LM_INTERFACE
  73967. + lm_set_drvdata(_dev, 0);
  73968. +#elif defined(PCI_INTERFACE)
  73969. + release_mem_region(otg_dev->os_dep.rsrc_start,
  73970. + otg_dev->os_dep.rsrc_len);
  73971. + pci_set_drvdata(_dev, 0);
  73972. +#elif defined(PLATFORM_INTERFACE)
  73973. + platform_set_drvdata(_dev, 0);
  73974. +#endif
  73975. + return REM_RETVAL(0);
  73976. +}
  73977. +
  73978. +/**
  73979. + * This function is called when an lm_device is bound to a
  73980. + * dwc_otg_driver. It creates the driver components required to
  73981. + * control the device (CIL, HCD, and PCD) and it initializes the
  73982. + * device. The driver components are stored in a dwc_otg_device
  73983. + * structure. A reference to the dwc_otg_device is saved in the
  73984. + * lm_device. This allows the driver to access the dwc_otg_device
  73985. + * structure on subsequent calls to driver methods for this device.
  73986. + *
  73987. + * @param _dev Bus device
  73988. + */
  73989. +static int dwc_otg_driver_probe(
  73990. +#ifdef LM_INTERFACE
  73991. + struct lm_device *_dev
  73992. +#elif defined(PCI_INTERFACE)
  73993. + struct pci_dev *_dev,
  73994. + const struct pci_device_id *id
  73995. +#elif defined(PLATFORM_INTERFACE)
  73996. + struct platform_device *_dev
  73997. +#endif
  73998. + )
  73999. +{
  74000. + int retval = 0;
  74001. + dwc_otg_device_t *dwc_otg_device;
  74002. + int devirq;
  74003. +
  74004. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  74005. +#ifdef LM_INTERFACE
  74006. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  74007. +#elif defined(PCI_INTERFACE)
  74008. + if (!id) {
  74009. + DWC_ERROR("Invalid pci_device_id %p", id);
  74010. + return -EINVAL;
  74011. + }
  74012. +
  74013. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  74014. + DWC_ERROR("Invalid pci_device %p", _dev);
  74015. + return -ENODEV;
  74016. + }
  74017. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  74018. + /* other stuff needed as well? */
  74019. +
  74020. +#elif defined(PLATFORM_INTERFACE)
  74021. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  74022. + (unsigned)_dev->resource->start,
  74023. + (unsigned)(_dev->resource->end - _dev->resource->start));
  74024. +#endif
  74025. +
  74026. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  74027. +
  74028. + if (!dwc_otg_device) {
  74029. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  74030. + return -ENOMEM;
  74031. + }
  74032. +
  74033. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  74034. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  74035. +
  74036. + /*
  74037. + * Map the DWC_otg Core memory into virtual address space.
  74038. + */
  74039. +#ifdef LM_INTERFACE
  74040. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  74041. +
  74042. + if (!dwc_otg_device->os_dep.base) {
  74043. + dev_err(&_dev->dev, "ioremap() failed\n");
  74044. + DWC_FREE(dwc_otg_device);
  74045. + return -ENOMEM;
  74046. + }
  74047. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  74048. + (unsigned)dwc_otg_device->os_dep.base);
  74049. +#elif defined(PCI_INTERFACE)
  74050. + _dev->current_state = PCI_D0;
  74051. + _dev->dev.power.power_state = PMSG_ON;
  74052. +
  74053. + if (!_dev->irq) {
  74054. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  74055. + pci_name(_dev));
  74056. + iounmap(dwc_otg_device->os_dep.base);
  74057. + DWC_FREE(dwc_otg_device);
  74058. + return -ENODEV;
  74059. + }
  74060. +
  74061. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  74062. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  74063. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  74064. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  74065. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  74066. + if (!request_mem_region
  74067. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  74068. + "dwc_otg")) {
  74069. + dev_dbg(&_dev->dev, "error requesting memory\n");
  74070. + iounmap(dwc_otg_device->os_dep.base);
  74071. + DWC_FREE(dwc_otg_device);
  74072. + return -EFAULT;
  74073. + }
  74074. +
  74075. + dwc_otg_device->os_dep.base =
  74076. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  74077. + dwc_otg_device->os_dep.rsrc_len);
  74078. + if (dwc_otg_device->os_dep.base == NULL) {
  74079. + dev_dbg(&_dev->dev, "error mapping memory\n");
  74080. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  74081. + dwc_otg_device->os_dep.rsrc_len);
  74082. + iounmap(dwc_otg_device->os_dep.base);
  74083. + DWC_FREE(dwc_otg_device);
  74084. + return -EFAULT;
  74085. + }
  74086. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  74087. + dwc_otg_device->os_dep.base);
  74088. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  74089. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  74090. + dwc_otg_device->os_dep.base);
  74091. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  74092. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  74093. + dwc_otg_device->os_dep.base);
  74094. +
  74095. + pci_set_master(_dev);
  74096. + pci_set_drvdata(_dev, dwc_otg_device);
  74097. +#elif defined(PLATFORM_INTERFACE)
  74098. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  74099. + _dev->resource->start,
  74100. + _dev->resource->end - _dev->resource->start + 1);
  74101. +#if 1
  74102. + if (!request_mem_region(_dev->resource[0].start,
  74103. + _dev->resource[0].end - _dev->resource[0].start + 1,
  74104. + "dwc_otg")) {
  74105. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  74106. + retval = -EFAULT;
  74107. + goto fail;
  74108. + }
  74109. +
  74110. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  74111. + _dev->resource[0].end -
  74112. + _dev->resource[0].start+1);
  74113. + if (fiq_enable)
  74114. + {
  74115. + if (!request_mem_region(_dev->resource[1].start,
  74116. + _dev->resource[1].end - _dev->resource[1].start + 1,
  74117. + "dwc_otg")) {
  74118. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  74119. + retval = -EFAULT;
  74120. + goto fail;
  74121. + }
  74122. +
  74123. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  74124. + _dev->resource[1].end -
  74125. + _dev->resource[1].start + 1);
  74126. + }
  74127. +
  74128. +#else
  74129. + {
  74130. + struct map_desc desc = {
  74131. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  74132. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  74133. + .length = SZ_128K,
  74134. + .type = MT_DEVICE
  74135. + };
  74136. + iotable_init(&desc, 1);
  74137. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  74138. + }
  74139. +#endif
  74140. + if (!dwc_otg_device->os_dep.base) {
  74141. + dev_err(&_dev->dev, "ioremap() failed\n");
  74142. + retval = -ENOMEM;
  74143. + goto fail;
  74144. + }
  74145. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  74146. + (unsigned)dwc_otg_device->os_dep.base);
  74147. +#endif
  74148. +
  74149. + /*
  74150. + * Initialize driver data to point to the global DWC_otg
  74151. + * Device structure.
  74152. + */
  74153. +#ifdef LM_INTERFACE
  74154. + lm_set_drvdata(_dev, dwc_otg_device);
  74155. +#elif defined(PLATFORM_INTERFACE)
  74156. + platform_set_drvdata(_dev, dwc_otg_device);
  74157. +#endif
  74158. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  74159. +
  74160. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  74161. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  74162. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  74163. +
  74164. + if (!dwc_otg_device->core_if) {
  74165. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  74166. + retval = -ENOMEM;
  74167. + goto fail;
  74168. + }
  74169. +
  74170. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  74171. + /*
  74172. + * Attempt to ensure this device is really a DWC_otg Controller.
  74173. + * Read and verify the SNPSID register contents. The value should be
  74174. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  74175. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  74176. + */
  74177. +
  74178. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  74179. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  74180. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  74181. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  74182. + retval = -EINVAL;
  74183. + goto fail;
  74184. + }
  74185. +
  74186. + /*
  74187. + * Validate parameter values.
  74188. + */
  74189. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  74190. + if (set_parameters(dwc_otg_device->core_if)) {
  74191. + retval = -EINVAL;
  74192. + goto fail;
  74193. + }
  74194. +
  74195. + /*
  74196. + * Create Device Attributes in sysfs
  74197. + */
  74198. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  74199. + dwc_otg_attr_create(_dev);
  74200. +
  74201. + /*
  74202. + * Disable the global interrupt until all the interrupt
  74203. + * handlers are installed.
  74204. + */
  74205. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  74206. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  74207. +
  74208. + /*
  74209. + * Install the interrupt handler for the common interrupts before
  74210. + * enabling common interrupts in core_init below.
  74211. + */
  74212. +
  74213. +#if defined(PLATFORM_INTERFACE)
  74214. + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
  74215. +#else
  74216. + devirq = _dev->irq;
  74217. +#endif
  74218. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  74219. + devirq);
  74220. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  74221. + retval = request_irq(devirq, dwc_otg_common_irq,
  74222. + IRQF_SHARED,
  74223. + "dwc_otg", dwc_otg_device);
  74224. + if (retval) {
  74225. + DWC_ERROR("request of irq%d failed\n", devirq);
  74226. + retval = -EBUSY;
  74227. + goto fail;
  74228. + } else {
  74229. + dwc_otg_device->common_irq_installed = 1;
  74230. + }
  74231. +
  74232. +#ifndef IRQF_TRIGGER_LOW
  74233. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  74234. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  74235. + set_irq_type(devirq,
  74236. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  74237. + IRQT_LOW
  74238. +#else
  74239. + IRQ_TYPE_LEVEL_LOW
  74240. +#endif
  74241. + );
  74242. +#endif
  74243. +#endif /*IRQF_TRIGGER_LOW*/
  74244. +
  74245. + /*
  74246. + * Initialize the DWC_otg core.
  74247. + */
  74248. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  74249. + dwc_otg_core_init(dwc_otg_device->core_if);
  74250. +
  74251. +#ifndef DWC_HOST_ONLY
  74252. + /*
  74253. + * Initialize the PCD
  74254. + */
  74255. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  74256. + retval = pcd_init(_dev);
  74257. + if (retval != 0) {
  74258. + DWC_ERROR("pcd_init failed\n");
  74259. + dwc_otg_device->pcd = NULL;
  74260. + goto fail;
  74261. + }
  74262. +#endif
  74263. +#ifndef DWC_DEVICE_ONLY
  74264. + /*
  74265. + * Initialize the HCD
  74266. + */
  74267. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  74268. + retval = hcd_init(_dev);
  74269. + if (retval != 0) {
  74270. + DWC_ERROR("hcd_init failed\n");
  74271. + dwc_otg_device->hcd = NULL;
  74272. + goto fail;
  74273. + }
  74274. +#endif
  74275. + /* Recover from drvdata having been overwritten by hcd_init() */
  74276. +#ifdef LM_INTERFACE
  74277. + lm_set_drvdata(_dev, dwc_otg_device);
  74278. +#elif defined(PLATFORM_INTERFACE)
  74279. + platform_set_drvdata(_dev, dwc_otg_device);
  74280. +#elif defined(PCI_INTERFACE)
  74281. + pci_set_drvdata(_dev, dwc_otg_device);
  74282. + dwc_otg_device->os_dep.pcidev = _dev;
  74283. +#endif
  74284. +
  74285. + /*
  74286. + * Enable the global interrupt after all the interrupt
  74287. + * handlers are installed if there is no ADP support else
  74288. + * perform initial actions required for Internal ADP logic.
  74289. + */
  74290. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  74291. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  74292. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  74293. + dev_dbg(&_dev->dev, "Done\n");
  74294. + } else
  74295. + dwc_otg_adp_start(dwc_otg_device->core_if,
  74296. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  74297. +
  74298. + return 0;
  74299. +
  74300. +fail:
  74301. + dwc_otg_driver_remove(_dev);
  74302. + return retval;
  74303. +}
  74304. +
  74305. +/**
  74306. + * This structure defines the methods to be called by a bus driver
  74307. + * during the lifecycle of a device on that bus. Both drivers and
  74308. + * devices are registered with a bus driver. The bus driver matches
  74309. + * devices to drivers based on information in the device and driver
  74310. + * structures.
  74311. + *
  74312. + * The probe function is called when the bus driver matches a device
  74313. + * to this driver. The remove function is called when a device is
  74314. + * unregistered with the bus driver.
  74315. + */
  74316. +#ifdef LM_INTERFACE
  74317. +static struct lm_driver dwc_otg_driver = {
  74318. + .drv = {.name = (char *)dwc_driver_name,},
  74319. + .probe = dwc_otg_driver_probe,
  74320. + .remove = dwc_otg_driver_remove,
  74321. + // 'suspend' and 'resume' absent
  74322. +};
  74323. +#elif defined(PCI_INTERFACE)
  74324. +static const struct pci_device_id pci_ids[] = { {
  74325. + PCI_DEVICE(0x16c3, 0xabcd),
  74326. + .driver_data =
  74327. + (unsigned long)0xdeadbeef,
  74328. + }, { /* end: all zeroes */ }
  74329. +};
  74330. +
  74331. +MODULE_DEVICE_TABLE(pci, pci_ids);
  74332. +
  74333. +/* pci driver glue; this is a "new style" PCI driver module */
  74334. +static struct pci_driver dwc_otg_driver = {
  74335. + .name = "dwc_otg",
  74336. + .id_table = pci_ids,
  74337. +
  74338. + .probe = dwc_otg_driver_probe,
  74339. + .remove = dwc_otg_driver_remove,
  74340. +
  74341. + .driver = {
  74342. + .name = (char *)dwc_driver_name,
  74343. + },
  74344. +};
  74345. +#elif defined(PLATFORM_INTERFACE)
  74346. +static struct platform_device_id platform_ids[] = {
  74347. + {
  74348. + .name = "bcm2708_usb",
  74349. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  74350. + },
  74351. + { /* end: all zeroes */ }
  74352. +};
  74353. +MODULE_DEVICE_TABLE(platform, platform_ids);
  74354. +
  74355. +static struct platform_driver dwc_otg_driver = {
  74356. + .driver = {
  74357. + .name = (char *)dwc_driver_name,
  74358. + },
  74359. + .id_table = platform_ids,
  74360. +
  74361. + .probe = dwc_otg_driver_probe,
  74362. + .remove = dwc_otg_driver_remove,
  74363. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  74364. +};
  74365. +#endif
  74366. +
  74367. +/**
  74368. + * This function is called when the dwc_otg_driver is installed with the
  74369. + * insmod command. It registers the dwc_otg_driver structure with the
  74370. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  74371. + * to be called. In addition, the bus driver will automatically expose
  74372. + * attributes defined for the device and driver in the special sysfs file
  74373. + * system.
  74374. + *
  74375. + * @return
  74376. + */
  74377. +static int __init dwc_otg_driver_init(void)
  74378. +{
  74379. + int retval = 0;
  74380. + int error;
  74381. + struct device_driver *drv;
  74382. +
  74383. + if(fiq_fsm_enable && !fiq_enable) {
  74384. + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
  74385. + fiq_enable = 1;
  74386. + }
  74387. +
  74388. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  74389. + DWC_DRIVER_VERSION,
  74390. +#ifdef LM_INTERFACE
  74391. + "logicmodule");
  74392. + retval = lm_driver_register(&dwc_otg_driver);
  74393. + drv = &dwc_otg_driver.drv;
  74394. +#elif defined(PCI_INTERFACE)
  74395. + "pci");
  74396. + retval = pci_register_driver(&dwc_otg_driver);
  74397. + drv = &dwc_otg_driver.driver;
  74398. +#elif defined(PLATFORM_INTERFACE)
  74399. + "platform");
  74400. + retval = platform_driver_register(&dwc_otg_driver);
  74401. + drv = &dwc_otg_driver.driver;
  74402. +#endif
  74403. + if (retval < 0) {
  74404. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  74405. + return retval;
  74406. + }
  74407. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
  74408. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
  74409. + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
  74410. +
  74411. + error = driver_create_file(drv, &driver_attr_version);
  74412. +#ifdef DEBUG
  74413. + error = driver_create_file(drv, &driver_attr_debuglevel);
  74414. +#endif
  74415. + return retval;
  74416. +}
  74417. +
  74418. +module_init(dwc_otg_driver_init);
  74419. +
  74420. +/**
  74421. + * This function is called when the driver is removed from the kernel
  74422. + * with the rmmod command. The driver unregisters itself with its bus
  74423. + * driver.
  74424. + *
  74425. + */
  74426. +static void __exit dwc_otg_driver_cleanup(void)
  74427. +{
  74428. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  74429. +
  74430. +#ifdef LM_INTERFACE
  74431. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  74432. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  74433. + lm_driver_unregister(&dwc_otg_driver);
  74434. +#elif defined(PCI_INTERFACE)
  74435. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  74436. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  74437. + pci_unregister_driver(&dwc_otg_driver);
  74438. +#elif defined(PLATFORM_INTERFACE)
  74439. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  74440. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  74441. + platform_driver_unregister(&dwc_otg_driver);
  74442. +#endif
  74443. +
  74444. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  74445. +}
  74446. +
  74447. +module_exit(dwc_otg_driver_cleanup);
  74448. +
  74449. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  74450. +MODULE_AUTHOR("Synopsys Inc.");
  74451. +MODULE_LICENSE("GPL");
  74452. +
  74453. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  74454. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  74455. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  74456. +MODULE_PARM_DESC(opt, "OPT Mode");
  74457. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  74458. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  74459. +
  74460. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  74461. + 0444);
  74462. +MODULE_PARM_DESC(dma_desc_enable,
  74463. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  74464. +
  74465. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  74466. + 0444);
  74467. +MODULE_PARM_DESC(dma_burst_size,
  74468. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  74469. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  74470. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  74471. +module_param_named(host_support_fs_ls_low_power,
  74472. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  74473. + 0444);
  74474. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  74475. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  74476. +module_param_named(host_ls_low_power_phy_clk,
  74477. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  74478. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  74479. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  74480. +module_param_named(enable_dynamic_fifo,
  74481. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  74482. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  74483. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  74484. + 0444);
  74485. +MODULE_PARM_DESC(data_fifo_size,
  74486. + "Total number of words in the data FIFO memory 32-32768");
  74487. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  74488. + int, 0444);
  74489. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  74490. +module_param_named(dev_nperio_tx_fifo_size,
  74491. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  74492. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  74493. + "Number of words in the non-periodic Tx FIFO 16-32768");
  74494. +module_param_named(dev_perio_tx_fifo_size_1,
  74495. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  74496. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  74497. + "Number of words in the periodic Tx FIFO 4-768");
  74498. +module_param_named(dev_perio_tx_fifo_size_2,
  74499. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  74500. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  74501. + "Number of words in the periodic Tx FIFO 4-768");
  74502. +module_param_named(dev_perio_tx_fifo_size_3,
  74503. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  74504. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  74505. + "Number of words in the periodic Tx FIFO 4-768");
  74506. +module_param_named(dev_perio_tx_fifo_size_4,
  74507. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  74508. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  74509. + "Number of words in the periodic Tx FIFO 4-768");
  74510. +module_param_named(dev_perio_tx_fifo_size_5,
  74511. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  74512. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  74513. + "Number of words in the periodic Tx FIFO 4-768");
  74514. +module_param_named(dev_perio_tx_fifo_size_6,
  74515. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  74516. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  74517. + "Number of words in the periodic Tx FIFO 4-768");
  74518. +module_param_named(dev_perio_tx_fifo_size_7,
  74519. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  74520. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  74521. + "Number of words in the periodic Tx FIFO 4-768");
  74522. +module_param_named(dev_perio_tx_fifo_size_8,
  74523. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  74524. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  74525. + "Number of words in the periodic Tx FIFO 4-768");
  74526. +module_param_named(dev_perio_tx_fifo_size_9,
  74527. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  74528. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  74529. + "Number of words in the periodic Tx FIFO 4-768");
  74530. +module_param_named(dev_perio_tx_fifo_size_10,
  74531. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  74532. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  74533. + "Number of words in the periodic Tx FIFO 4-768");
  74534. +module_param_named(dev_perio_tx_fifo_size_11,
  74535. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  74536. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  74537. + "Number of words in the periodic Tx FIFO 4-768");
  74538. +module_param_named(dev_perio_tx_fifo_size_12,
  74539. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  74540. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  74541. + "Number of words in the periodic Tx FIFO 4-768");
  74542. +module_param_named(dev_perio_tx_fifo_size_13,
  74543. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  74544. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  74545. + "Number of words in the periodic Tx FIFO 4-768");
  74546. +module_param_named(dev_perio_tx_fifo_size_14,
  74547. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  74548. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  74549. + "Number of words in the periodic Tx FIFO 4-768");
  74550. +module_param_named(dev_perio_tx_fifo_size_15,
  74551. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  74552. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  74553. + "Number of words in the periodic Tx FIFO 4-768");
  74554. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  74555. + int, 0444);
  74556. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  74557. +module_param_named(host_nperio_tx_fifo_size,
  74558. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  74559. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  74560. + "Number of words in the non-periodic Tx FIFO 16-32768");
  74561. +module_param_named(host_perio_tx_fifo_size,
  74562. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  74563. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  74564. + "Number of words in the host periodic Tx FIFO 16-32768");
  74565. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  74566. + int, 0444);
  74567. +/** @todo Set the max to 512K, modify checks */
  74568. +MODULE_PARM_DESC(max_transfer_size,
  74569. + "The maximum transfer size supported in bytes 2047-65535");
  74570. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  74571. + int, 0444);
  74572. +MODULE_PARM_DESC(max_packet_count,
  74573. + "The maximum number of packets in a transfer 15-511");
  74574. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  74575. + 0444);
  74576. +MODULE_PARM_DESC(host_channels,
  74577. + "The number of host channel registers to use 1-16");
  74578. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  74579. + 0444);
  74580. +MODULE_PARM_DESC(dev_endpoints,
  74581. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  74582. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  74583. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  74584. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  74585. + 0444);
  74586. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  74587. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  74588. +MODULE_PARM_DESC(phy_ulpi_ddr,
  74589. + "ULPI at double or single data rate 0=Single 1=Double");
  74590. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  74591. + int, 0444);
  74592. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  74593. + "ULPI PHY using internal or external vbus 0=Internal");
  74594. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  74595. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  74596. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  74597. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  74598. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  74599. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  74600. +module_param_named(debug, g_dbg_lvl, int, 0444);
  74601. +MODULE_PARM_DESC(debug, "");
  74602. +
  74603. +module_param_named(en_multiple_tx_fifo,
  74604. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  74605. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  74606. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  74607. +module_param_named(dev_tx_fifo_size_1,
  74608. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  74609. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  74610. +module_param_named(dev_tx_fifo_size_2,
  74611. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  74612. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  74613. +module_param_named(dev_tx_fifo_size_3,
  74614. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  74615. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  74616. +module_param_named(dev_tx_fifo_size_4,
  74617. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  74618. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  74619. +module_param_named(dev_tx_fifo_size_5,
  74620. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  74621. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  74622. +module_param_named(dev_tx_fifo_size_6,
  74623. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  74624. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  74625. +module_param_named(dev_tx_fifo_size_7,
  74626. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  74627. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  74628. +module_param_named(dev_tx_fifo_size_8,
  74629. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  74630. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  74631. +module_param_named(dev_tx_fifo_size_9,
  74632. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  74633. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  74634. +module_param_named(dev_tx_fifo_size_10,
  74635. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  74636. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  74637. +module_param_named(dev_tx_fifo_size_11,
  74638. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  74639. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  74640. +module_param_named(dev_tx_fifo_size_12,
  74641. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  74642. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  74643. +module_param_named(dev_tx_fifo_size_13,
  74644. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  74645. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  74646. +module_param_named(dev_tx_fifo_size_14,
  74647. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  74648. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  74649. +module_param_named(dev_tx_fifo_size_15,
  74650. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  74651. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  74652. +
  74653. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  74654. +MODULE_PARM_DESC(thr_ctl,
  74655. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  74656. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  74657. + 0444);
  74658. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  74659. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  74660. + 0444);
  74661. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  74662. +
  74663. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  74664. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  74665. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  74666. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  74667. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  74668. +MODULE_PARM_DESC(ic_usb_cap,
  74669. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  74670. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  74671. + 0444);
  74672. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  74673. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  74674. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  74675. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  74676. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  74677. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  74678. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  74679. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  74680. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  74681. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  74682. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  74683. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  74684. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  74685. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  74686. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  74687. +module_param(microframe_schedule, bool, 0444);
  74688. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  74689. +
  74690. +module_param(fiq_enable, bool, 0444);
  74691. +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
  74692. +module_param(nak_holdoff, ushort, 0644);
  74693. +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
  74694. +module_param(fiq_fsm_enable, bool, 0444);
  74695. +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
  74696. +module_param(fiq_fsm_mask, ushort, 0444);
  74697. +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
  74698. + "Bit 0 : Non-periodic split transactions\n"
  74699. + "Bit 1 : Periodic split transactions\n"
  74700. + "Bit 2 : High-speed multi-transfer isochronous\n"
  74701. + "All other bits should be set 0.");
  74702. +
  74703. +
  74704. +/** @page "Module Parameters"
  74705. + *
  74706. + * The following parameters may be specified when starting the module.
  74707. + * These parameters define how the DWC_otg controller should be
  74708. + * configured. Parameter values are passed to the CIL initialization
  74709. + * function dwc_otg_cil_init
  74710. + *
  74711. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  74712. + *
  74713. +
  74714. + <table>
  74715. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  74716. +
  74717. + <tr>
  74718. + <td>otg_cap</td>
  74719. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  74720. + value for this parameter if none is specified.
  74721. + - 0: HNP and SRP capable (default, if available)
  74722. + - 1: SRP Only capable
  74723. + - 2: No HNP/SRP capable
  74724. + </td></tr>
  74725. +
  74726. + <tr>
  74727. + <td>dma_enable</td>
  74728. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  74729. + The driver will automatically detect the value for this parameter if none is
  74730. + specified.
  74731. + - 0: Slave
  74732. + - 1: DMA (default, if available)
  74733. + </td></tr>
  74734. +
  74735. + <tr>
  74736. + <td>dma_burst_size</td>
  74737. + <td>The DMA Burst size (applicable only for External DMA Mode).
  74738. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  74739. + </td></tr>
  74740. +
  74741. + <tr>
  74742. + <td>speed</td>
  74743. + <td>Specifies the maximum speed of operation in host and device mode. The
  74744. + actual speed depends on the speed of the attached device and the value of
  74745. + phy_type.
  74746. + - 0: High Speed (default)
  74747. + - 1: Full Speed
  74748. + </td></tr>
  74749. +
  74750. + <tr>
  74751. + <td>host_support_fs_ls_low_power</td>
  74752. + <td>Specifies whether low power mode is supported when attached to a Full
  74753. + Speed or Low Speed device in host mode.
  74754. + - 0: Don't support low power mode (default)
  74755. + - 1: Support low power mode
  74756. + </td></tr>
  74757. +
  74758. + <tr>
  74759. + <td>host_ls_low_power_phy_clk</td>
  74760. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  74761. + Speed device in host mode. This parameter is applicable only if
  74762. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  74763. + - 0: 48 MHz (default)
  74764. + - 1: 6 MHz
  74765. + </td></tr>
  74766. +
  74767. + <tr>
  74768. + <td>enable_dynamic_fifo</td>
  74769. + <td> Specifies whether FIFOs may be resized by the driver software.
  74770. + - 0: Use cC FIFO size parameters
  74771. + - 1: Allow dynamic FIFO sizing (default)
  74772. + </td></tr>
  74773. +
  74774. + <tr>
  74775. + <td>data_fifo_size</td>
  74776. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  74777. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  74778. + - Values: 32 to 32768 (default 8192)
  74779. +
  74780. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  74781. + </td></tr>
  74782. +
  74783. + <tr>
  74784. + <td>dev_rx_fifo_size</td>
  74785. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  74786. + FIFO sizing is enabled.
  74787. + - Values: 16 to 32768 (default 1064)
  74788. + </td></tr>
  74789. +
  74790. + <tr>
  74791. + <td>dev_nperio_tx_fifo_size</td>
  74792. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  74793. + dynamic FIFO sizing is enabled.
  74794. + - Values: 16 to 32768 (default 1024)
  74795. + </td></tr>
  74796. +
  74797. + <tr>
  74798. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  74799. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  74800. + when dynamic FIFO sizing is enabled.
  74801. + - Values: 4 to 768 (default 256)
  74802. + </td></tr>
  74803. +
  74804. + <tr>
  74805. + <td>host_rx_fifo_size</td>
  74806. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  74807. + sizing is enabled.
  74808. + - Values: 16 to 32768 (default 1024)
  74809. + </td></tr>
  74810. +
  74811. + <tr>
  74812. + <td>host_nperio_tx_fifo_size</td>
  74813. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  74814. + dynamic FIFO sizing is enabled in the core.
  74815. + - Values: 16 to 32768 (default 1024)
  74816. + </td></tr>
  74817. +
  74818. + <tr>
  74819. + <td>host_perio_tx_fifo_size</td>
  74820. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  74821. + sizing is enabled.
  74822. + - Values: 16 to 32768 (default 1024)
  74823. + </td></tr>
  74824. +
  74825. + <tr>
  74826. + <td>max_transfer_size</td>
  74827. + <td>The maximum transfer size supported in bytes.
  74828. + - Values: 2047 to 65,535 (default 65,535)
  74829. + </td></tr>
  74830. +
  74831. + <tr>
  74832. + <td>max_packet_count</td>
  74833. + <td>The maximum number of packets in a transfer.
  74834. + - Values: 15 to 511 (default 511)
  74835. + </td></tr>
  74836. +
  74837. + <tr>
  74838. + <td>host_channels</td>
  74839. + <td>The number of host channel registers to use.
  74840. + - Values: 1 to 16 (default 12)
  74841. +
  74842. + Note: The FPGA configuration supports a maximum of 12 host channels.
  74843. + </td></tr>
  74844. +
  74845. + <tr>
  74846. + <td>dev_endpoints</td>
  74847. + <td>The number of endpoints in addition to EP0 available for device mode
  74848. + operations.
  74849. + - Values: 1 to 15 (default 6 IN and OUT)
  74850. +
  74851. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  74852. + addition to EP0.
  74853. + </td></tr>
  74854. +
  74855. + <tr>
  74856. + <td>phy_type</td>
  74857. + <td>Specifies the type of PHY interface to use. By default, the driver will
  74858. + automatically detect the phy_type.
  74859. + - 0: Full Speed
  74860. + - 1: UTMI+ (default, if available)
  74861. + - 2: ULPI
  74862. + </td></tr>
  74863. +
  74864. + <tr>
  74865. + <td>phy_utmi_width</td>
  74866. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  74867. + phy_type of UTMI+. Also, this parameter is applicable only if the
  74868. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  74869. + core has been configured to work at either data path width.
  74870. + - Values: 8 or 16 bits (default 16)
  74871. + </td></tr>
  74872. +
  74873. + <tr>
  74874. + <td>phy_ulpi_ddr</td>
  74875. + <td>Specifies whether the ULPI operates at double or single data rate. This
  74876. + parameter is only applicable if phy_type is ULPI.
  74877. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  74878. + - 1: double data rate ULPI interface with 4 bit wide data bus
  74879. + </td></tr>
  74880. +
  74881. + <tr>
  74882. + <td>i2c_enable</td>
  74883. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  74884. + parameter is only applicable if PHY_TYPE is FS.
  74885. + - 0: Disabled (default)
  74886. + - 1: Enabled
  74887. + </td></tr>
  74888. +
  74889. + <tr>
  74890. + <td>ulpi_fs_ls</td>
  74891. + <td>Specifies whether to use ULPI FS/LS mode only.
  74892. + - 0: Disabled (default)
  74893. + - 1: Enabled
  74894. + </td></tr>
  74895. +
  74896. + <tr>
  74897. + <td>ts_dline</td>
  74898. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  74899. + - 0: Disabled (default)
  74900. + - 1: Enabled
  74901. + </td></tr>
  74902. +
  74903. + <tr>
  74904. + <td>en_multiple_tx_fifo</td>
  74905. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  74906. + The driver will automatically detect the value for this parameter if none is
  74907. + specified.
  74908. + - 0: Disabled
  74909. + - 1: Enabled (default, if available)
  74910. + </td></tr>
  74911. +
  74912. + <tr>
  74913. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  74914. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  74915. + when dynamic FIFO sizing is enabled.
  74916. + - Values: 4 to 768 (default 256)
  74917. + </td></tr>
  74918. +
  74919. + <tr>
  74920. + <td>tx_thr_length</td>
  74921. + <td>Transmit Threshold length in 32 bit double words
  74922. + - Values: 8 to 128 (default 64)
  74923. + </td></tr>
  74924. +
  74925. + <tr>
  74926. + <td>rx_thr_length</td>
  74927. + <td>Receive Threshold length in 32 bit double words
  74928. + - Values: 8 to 128 (default 64)
  74929. + </td></tr>
  74930. +
  74931. +<tr>
  74932. + <td>thr_ctl</td>
  74933. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  74934. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  74935. + Rx transfers accordingly.
  74936. + The driver will automatically detect the value for this parameter if none is
  74937. + specified.
  74938. + - Values: 0 to 7 (default 0)
  74939. + Bit values indicate:
  74940. + - 0: Thresholding disabled
  74941. + - 1: Thresholding enabled
  74942. + </td></tr>
  74943. +
  74944. +<tr>
  74945. + <td>dma_desc_enable</td>
  74946. + <td>Specifies whether to enable Descriptor DMA mode.
  74947. + The driver will automatically detect the value for this parameter if none is
  74948. + specified.
  74949. + - 0: Descriptor DMA disabled
  74950. + - 1: Descriptor DMA (default, if available)
  74951. + </td></tr>
  74952. +
  74953. +<tr>
  74954. + <td>mpi_enable</td>
  74955. + <td>Specifies whether to enable MPI enhancement mode.
  74956. + The driver will automatically detect the value for this parameter if none is
  74957. + specified.
  74958. + - 0: MPI disabled (default)
  74959. + - 1: MPI enable
  74960. + </td></tr>
  74961. +
  74962. +<tr>
  74963. + <td>pti_enable</td>
  74964. + <td>Specifies whether to enable PTI enhancement support.
  74965. + The driver will automatically detect the value for this parameter if none is
  74966. + specified.
  74967. + - 0: PTI disabled (default)
  74968. + - 1: PTI enable
  74969. + </td></tr>
  74970. +
  74971. +<tr>
  74972. + <td>lpm_enable</td>
  74973. + <td>Specifies whether to enable LPM support.
  74974. + The driver will automatically detect the value for this parameter if none is
  74975. + specified.
  74976. + - 0: LPM disabled
  74977. + - 1: LPM enable (default, if available)
  74978. + </td></tr>
  74979. +
  74980. +<tr>
  74981. + <td>ic_usb_cap</td>
  74982. + <td>Specifies whether to enable IC_USB capability.
  74983. + The driver will automatically detect the value for this parameter if none is
  74984. + specified.
  74985. + - 0: IC_USB disabled (default, if available)
  74986. + - 1: IC_USB enable
  74987. + </td></tr>
  74988. +
  74989. +<tr>
  74990. + <td>ahb_thr_ratio</td>
  74991. + <td>Specifies AHB Threshold ratio.
  74992. + - Values: 0 to 3 (default 0)
  74993. + </td></tr>
  74994. +
  74995. +<tr>
  74996. + <td>power_down</td>
  74997. + <td>Specifies Power Down(Hibernation) Mode.
  74998. + The driver will automatically detect the value for this parameter if none is
  74999. + specified.
  75000. + - 0: Power Down disabled (default)
  75001. + - 2: Power Down enabled
  75002. + </td></tr>
  75003. +
  75004. + <tr>
  75005. + <td>reload_ctl</td>
  75006. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  75007. + run time. The driver will automatically detect the value for this parameter if
  75008. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  75009. + the core might misbehave.
  75010. + - 0: Reload Control disabled (default)
  75011. + - 1: Reload Control enabled
  75012. + </td></tr>
  75013. +
  75014. + <tr>
  75015. + <td>dev_out_nak</td>
  75016. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  75017. + The driver will automatically detect the value for this parameter if
  75018. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  75019. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  75020. + - 1: The core sets NAK after Bulk OUT transfer complete
  75021. + </td></tr>
  75022. +
  75023. + <tr>
  75024. + <td>cont_on_bna</td>
  75025. + <td>Specifies whether Enable Continue on BNA enabled or no.
  75026. + After receiving BNA interrupt the core disables the endpoint,when the
  75027. + endpoint is re-enabled by the application the
  75028. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  75029. + - 1: Core starts processing from the descriptor which received the BNA.
  75030. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  75031. + </td></tr>
  75032. +
  75033. + <tr>
  75034. + <td>ahb_single</td>
  75035. + <td>This bit when programmed supports SINGLE transfers for remainder data
  75036. + in a transfer for DMA mode of operation.
  75037. + - 0: The remainder data will be sent using INCR burst size (default)
  75038. + - 1: The remainder data will be sent using SINGLE burst size.
  75039. + </td></tr>
  75040. +
  75041. +<tr>
  75042. + <td>adp_enable</td>
  75043. + <td>Specifies whether ADP feature is enabled.
  75044. + The driver will automatically detect the value for this parameter if none is
  75045. + specified.
  75046. + - 0: ADP feature disabled (default)
  75047. + - 1: ADP feature enabled
  75048. + </td></tr>
  75049. +
  75050. + <tr>
  75051. + <td>otg_ver</td>
  75052. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  75053. + USB OTG device.
  75054. + - 0: OTG 2.0 support disabled (default)
  75055. + - 1: OTG 2.0 support enabled
  75056. + </td></tr>
  75057. +
  75058. +*/
  75059. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_driver.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  75060. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_driver.h 1970-01-01 01:00:00.000000000 +0100
  75061. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_driver.h 2014-09-14 19:04:13.000000000 +0200
  75062. @@ -0,0 +1,86 @@
  75063. +/* ==========================================================================
  75064. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  75065. + * $Revision: #19 $
  75066. + * $Date: 2010/11/15 $
  75067. + * $Change: 1627671 $
  75068. + *
  75069. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  75070. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  75071. + * otherwise expressly agreed to in writing between Synopsys and you.
  75072. + *
  75073. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  75074. + * any End User Software License Agreement or Agreement for Licensed Product
  75075. + * with Synopsys or any supplement thereto. You are permitted to use and
  75076. + * redistribute this Software in source and binary forms, with or without
  75077. + * modification, provided that redistributions of source code must retain this
  75078. + * notice. You may not view, use, disclose, copy or distribute this file or
  75079. + * any information contained herein except pursuant to this license grant from
  75080. + * Synopsys. If you do not agree with this notice, including the disclaimer
  75081. + * below, then you are not authorized to use the Software.
  75082. + *
  75083. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  75084. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75085. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  75086. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  75087. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  75088. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  75089. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  75090. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  75091. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  75092. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  75093. + * DAMAGE.
  75094. + * ========================================================================== */
  75095. +
  75096. +#ifndef __DWC_OTG_DRIVER_H__
  75097. +#define __DWC_OTG_DRIVER_H__
  75098. +
  75099. +/** @file
  75100. + * This file contains the interface to the Linux driver.
  75101. + */
  75102. +#include "dwc_otg_os_dep.h"
  75103. +#include "dwc_otg_core_if.h"
  75104. +
  75105. +/* Type declarations */
  75106. +struct dwc_otg_pcd;
  75107. +struct dwc_otg_hcd;
  75108. +
  75109. +/**
  75110. + * This structure is a wrapper that encapsulates the driver components used to
  75111. + * manage a single DWC_otg controller.
  75112. + */
  75113. +typedef struct dwc_otg_device {
  75114. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  75115. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  75116. + * require this. */
  75117. + struct os_dependent os_dep;
  75118. +
  75119. + /** Pointer to the core interface structure. */
  75120. + dwc_otg_core_if_t *core_if;
  75121. +
  75122. + /** Pointer to the PCD structure. */
  75123. + struct dwc_otg_pcd *pcd;
  75124. +
  75125. + /** Pointer to the HCD structure. */
  75126. + struct dwc_otg_hcd *hcd;
  75127. +
  75128. + /** Flag to indicate whether the common IRQ handler is installed. */
  75129. + uint8_t common_irq_installed;
  75130. +
  75131. +} dwc_otg_device_t;
  75132. +
  75133. +/*We must clear S3C24XX_EINTPEND external interrupt register
  75134. + * because after clearing in this register trigerred IRQ from
  75135. + * H/W core in kernel interrupt can be occured again before OTG
  75136. + * handlers clear all IRQ sources of Core registers because of
  75137. + * timing latencies and Low Level IRQ Type.
  75138. + */
  75139. +#ifdef CONFIG_MACH_IPMATE
  75140. +#define S3C2410X_CLEAR_EINTPEND() \
  75141. +do { \
  75142. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  75143. +} while (0)
  75144. +#else
  75145. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  75146. +#endif
  75147. +
  75148. +#endif
  75149. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  75150. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c 1970-01-01 01:00:00.000000000 +0100
  75151. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c 2014-09-14 19:04:13.000000000 +0200
  75152. @@ -0,0 +1,1294 @@
  75153. +/*
  75154. + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
  75155. + *
  75156. + * Copyright (c) 2013 Raspberry Pi Foundation
  75157. + *
  75158. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  75159. + * All rights reserved.
  75160. + *
  75161. + * Redistribution and use in source and binary forms, with or without
  75162. + * modification, are permitted provided that the following conditions are met:
  75163. + * * Redistributions of source code must retain the above copyright
  75164. + * notice, this list of conditions and the following disclaimer.
  75165. + * * Redistributions in binary form must reproduce the above copyright
  75166. + * notice, this list of conditions and the following disclaimer in the
  75167. + * documentation and/or other materials provided with the distribution.
  75168. + * * Neither the name of Raspberry Pi nor the
  75169. + * names of its contributors may be used to endorse or promote products
  75170. + * derived from this software without specific prior written permission.
  75171. + *
  75172. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  75173. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  75174. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  75175. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  75176. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  75177. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  75178. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  75179. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  75180. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  75181. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  75182. + *
  75183. + * This FIQ implements functionality that performs split transactions on
  75184. + * the dwc_otg hardware without any outside intervention. A split transaction
  75185. + * is "queued" by nominating a specific host channel to perform the entirety
  75186. + * of a split transaction. This FIQ will then perform the microframe-precise
  75187. + * scheduling required in each phase of the transaction until completion.
  75188. + *
  75189. + * The FIQ functionality is glued into the Synopsys driver via the entry point
  75190. + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
  75191. + * for a FSM-enabled channel.
  75192. + *
  75193. + * NB: Large parts of this implementation have architecture-specific code.
  75194. + * For porting this functionality to other ARM machines, the minimum is required:
  75195. + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
  75196. + * to the FIQ
  75197. + * - A method of forcing a software generated interrupt from FIQ mode that then
  75198. + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
  75199. + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
  75200. + * processor core - there is no locking between the FIQ and IRQ (aside from
  75201. + * local_fiq_disable)
  75202. + *
  75203. + */
  75204. +
  75205. +#include "dwc_otg_fiq_fsm.h"
  75206. +
  75207. +
  75208. +char buffer[1000*16];
  75209. +int wptr;
  75210. +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
  75211. +{
  75212. + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
  75213. + va_list args;
  75214. + char text[17];
  75215. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
  75216. +
  75217. + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
  75218. + {
  75219. + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
  75220. + va_start(args, fmt);
  75221. + vsnprintf(text+8, 9, fmt, args);
  75222. + va_end(args);
  75223. +
  75224. + memcpy(buffer + wptr, text, 16);
  75225. + wptr = (wptr + 16) % sizeof(buffer);
  75226. + }
  75227. +}
  75228. +
  75229. +/**
  75230. + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  75231. + * @channel: channel to re-enable
  75232. + */
  75233. +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
  75234. +{
  75235. + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
  75236. +
  75237. + hcchar.b.chen = 0;
  75238. + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
  75239. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  75240. + /* Hardware bug workaround: update the ssplit index */
  75241. + if (st->channel[n].hcsplt_copy.b.spltena)
  75242. + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  75243. +
  75244. + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  75245. + }
  75246. +
  75247. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  75248. + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  75249. + hcchar.b.chen = 1;
  75250. +
  75251. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  75252. + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
  75253. +}
  75254. +
  75255. +/**
  75256. + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
  75257. + * @st: Pointer to the channel's state
  75258. + * @n : channel number
  75259. + *
  75260. + * Change host channel registers to perform a complete-split transaction. Being mindful of the
  75261. + * endpoint direction, set control regs up correctly.
  75262. + */
  75263. +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
  75264. +{
  75265. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
  75266. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  75267. +
  75268. + hcsplt.b.compsplt = 1;
  75269. + if (st->channel[n].hcchar_copy.b.epdir == 1) {
  75270. + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
  75271. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  75272. + } else {
  75273. + // If OUT, the CSPLIT result contains handshake only.
  75274. + hctsiz.b.xfersize = 0;
  75275. + }
  75276. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  75277. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  75278. + mb();
  75279. +}
  75280. +
  75281. +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
  75282. +{
  75283. + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
  75284. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  75285. +
  75286. + if (st->channel[n].hcchar_copy.b.epdir == 0) {
  75287. + return st->channel[n].hctsiz_copy.b.xfersize;
  75288. + } else {
  75289. + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
  75290. + }
  75291. +
  75292. +}
  75293. +
  75294. +
  75295. +/**
  75296. + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
  75297. + *
  75298. + * Of use only for IN periodic transfers.
  75299. + */
  75300. +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
  75301. +{
  75302. + hcdma_data_t hcdma;
  75303. + int i = st->channel[n].dma_info.index;
  75304. + int len;
  75305. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  75306. +
  75307. + len = fiq_get_xfer_len(st, n);
  75308. + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
  75309. + st->channel[n].dma_info.slot_len[i] = len;
  75310. + i++;
  75311. + if (i > 6)
  75312. + BUG();
  75313. +
  75314. + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
  75315. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  75316. + st->channel[n].dma_info.index = i;
  75317. + return 0;
  75318. +}
  75319. +
  75320. +/**
  75321. + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
  75322. + */
  75323. +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
  75324. +{
  75325. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  75326. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  75327. + hctsiz.b.pktcnt = 1;
  75328. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  75329. +}
  75330. +
  75331. +/**
  75332. + * fiq_iso_out_advance() - update DMA address and split position bits
  75333. + * for isochronous OUT transactions.
  75334. + *
  75335. + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
  75336. + * Split-BEGIN states are not handled - this is done when the transaction was queued.
  75337. + *
  75338. + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
  75339. + */
  75340. +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
  75341. +{
  75342. + hcsplt_data_t hcsplt;
  75343. + hctsiz_data_t hctsiz;
  75344. + hcdma_data_t hcdma;
  75345. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  75346. + int last = 0;
  75347. + int i = st->channel[n].dma_info.index;
  75348. +
  75349. + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
  75350. + i++;
  75351. + if (i == 4)
  75352. + last = 1;
  75353. + if (st->channel[n].dma_info.slot_len[i+1] == 255)
  75354. + last = 1;
  75355. +
  75356. + /* New DMA address - address of bounce buffer referred to in index */
  75357. + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
  75358. + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
  75359. + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
  75360. + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
  75361. + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
  75362. + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
  75363. + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
  75364. + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
  75365. + /* Set up new packet length */
  75366. + hctsiz.b.pktcnt = 1;
  75367. + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
  75368. + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
  75369. +
  75370. + st->channel[n].dma_info.index++;
  75371. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  75372. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  75373. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  75374. + return last;
  75375. +}
  75376. +
  75377. +/**
  75378. + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
  75379. + *
  75380. + * Despite the limitations of the DWC core, we can force a microframe pipeline of
  75381. + * isochronous OUT start-split transactions while waiting for a corresponding other-type
  75382. + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
  75383. + * is very unlikely that filling the start-split FIFO will cause data loss.
  75384. + * This allows much better interleaving of transactions in an order-independent way-
  75385. + * there is no requirement to prioritise isochronous, just a state-space search has
  75386. + * to be performed on each periodic start-split complete interrupt.
  75387. + */
  75388. +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
  75389. +{
  75390. + int hub_addr = st->channel[n].hub_addr;
  75391. + int port_addr = st->channel[n].port_addr;
  75392. + int i, poked = 0;
  75393. + for (i = 0; i < num_channels; i++) {
  75394. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  75395. + continue;
  75396. + if (st->channel[i].hub_addr == hub_addr &&
  75397. + st->channel[i].port_addr == port_addr) {
  75398. + switch (st->channel[i].fsm) {
  75399. + case FIQ_PER_ISO_OUT_PENDING:
  75400. + if (st->channel[i].nrpackets == 1) {
  75401. + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
  75402. + } else {
  75403. + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  75404. + }
  75405. + fiq_fsm_restart_channel(st, i, 0);
  75406. + poked = 1;
  75407. + break;
  75408. +
  75409. + default:
  75410. + break;
  75411. + }
  75412. + }
  75413. + if (poked)
  75414. + break;
  75415. + }
  75416. + return poked;
  75417. +}
  75418. +
  75419. +/**
  75420. + * fiq_fsm_tt_in_use() - search for host channels using this TT
  75421. + * @n: Channel to use as reference
  75422. + *
  75423. + */
  75424. +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
  75425. +{
  75426. + int hub_addr = st->channel[n].hub_addr;
  75427. + int port_addr = st->channel[n].port_addr;
  75428. + int i, in_use = 0;
  75429. + for (i = 0; i < num_channels; i++) {
  75430. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  75431. + continue;
  75432. + switch (st->channel[i].fsm) {
  75433. + /* TT is reserved for channels that are in the middle of a periodic
  75434. + * split transaction.
  75435. + */
  75436. + case FIQ_PER_SSPLIT_STARTED:
  75437. + case FIQ_PER_CSPLIT_WAIT:
  75438. + case FIQ_PER_CSPLIT_NYET1:
  75439. + //case FIQ_PER_CSPLIT_POLL:
  75440. + case FIQ_PER_ISO_OUT_ACTIVE:
  75441. + case FIQ_PER_ISO_OUT_LAST:
  75442. + if (st->channel[i].hub_addr == hub_addr &&
  75443. + st->channel[i].port_addr == port_addr) {
  75444. + in_use = 1;
  75445. + }
  75446. + break;
  75447. + default:
  75448. + break;
  75449. + }
  75450. + if (in_use)
  75451. + break;
  75452. + }
  75453. + return in_use;
  75454. +}
  75455. +
  75456. +/**
  75457. + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
  75458. + * to be issued for this IN transaction.
  75459. + *
  75460. + * We cannot tell the inbound PID of a data packet due to hardware limitations.
  75461. + * we need to make an educated guess as to whether we need to queue another CSPLIT
  75462. + * or not. A no-brainer is when we have received enough data to fill the endpoint
  75463. + * size, but for endpoints that give variable-length data then we have to resort
  75464. + * to heuristics.
  75465. + *
  75466. + * We also return whether this is the last CSPLIT to be queued, again based on
  75467. + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
  75468. + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
  75469. + */
  75470. +
  75471. +/*
  75472. + * We need some way of guaranteeing if a returned periodic packet of size X
  75473. + * has a DATA0 PID.
  75474. + * The heuristic value of 144 bytes assumes that the received data has maximal
  75475. + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
  75476. + * permissible limit. If the transfer length results in a final packet size
  75477. + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
  75478. + * Also used to ensure that an endpoint will nominally only return a single
  75479. + * complete-split worth of data.
  75480. + */
  75481. +#define DATA0_PID_HEURISTIC 144
  75482. +
  75483. +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
  75484. +{
  75485. +
  75486. + int i;
  75487. + int total_len = 0;
  75488. + int more_needed = 1;
  75489. + struct fiq_channel_state *st = &state->channel[n];
  75490. +
  75491. + for (i = 0; i < st->dma_info.index; i++) {
  75492. + total_len += st->dma_info.slot_len[i];
  75493. + }
  75494. +
  75495. + *probably_last = 0;
  75496. +
  75497. + if (st->hcchar_copy.b.eptype == 0x3) {
  75498. + /*
  75499. + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
  75500. + * then this is definitely the last CSPLIT.
  75501. + */
  75502. + *probably_last = 1;
  75503. + } else {
  75504. + /* Isoc IN. This is a bit risky if we are the first transaction:
  75505. + * we may have been held off slightly. */
  75506. + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
  75507. + more_needed = 0;
  75508. + }
  75509. + /* If in the next uframe we will receive enough data to fill the endpoint,
  75510. + * then only issue 1 more csplit.
  75511. + */
  75512. + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
  75513. + *probably_last = 1;
  75514. + }
  75515. +
  75516. + if (total_len >= st->hctsiz_copy.b.xfersize ||
  75517. + i == 6 || total_len == 0)
  75518. + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
  75519. + * a single endpoint. Accepting more would completely break our scheduling mechanism though
  75520. + * - in these extreme cases we will pass through a truncated packet.
  75521. + */
  75522. + more_needed = 0;
  75523. +
  75524. + return more_needed;
  75525. +}
  75526. +
  75527. +/**
  75528. + * fiq_fsm_too_late() - Test transaction for lateness
  75529. + *
  75530. + * If a SSPLIT for a large IN transaction is issued too late in a frame,
  75531. + * the hub will disable the port to the device and respond with ERR handshakes.
  75532. + * The hub status endpoint will not reflect this change.
  75533. + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
  75534. + */
  75535. +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
  75536. +{
  75537. + int uframe;
  75538. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  75539. + uframe = hfnum.b.frnum & 0x7;
  75540. + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
  75541. + return 1;
  75542. + } else {
  75543. + return 0;
  75544. + }
  75545. +}
  75546. +
  75547. +
  75548. +/**
  75549. + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
  75550. + *
  75551. + * Search pending transactions in the start-split pending state and queue them.
  75552. + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
  75553. + * Note: we specifically don't do isochronous OUT transactions first because better
  75554. + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
  75555. + */
  75556. +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
  75557. +{
  75558. + int n;
  75559. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  75560. + if ((hfnum.b.frnum & 0x7) == 5)
  75561. + return;
  75562. + for (n = 0; n < num_channels; n++) {
  75563. + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
  75564. + /* Check to see if any other transactions are using this TT */
  75565. + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
  75566. + if (!fiq_fsm_too_late(st, n)) {
  75567. + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  75568. + fiq_print(FIQDBG_INT, st, "NEXTPER ");
  75569. + fiq_fsm_restart_channel(st, n, 0);
  75570. + } else {
  75571. + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  75572. + }
  75573. + break;
  75574. + }
  75575. + }
  75576. + }
  75577. + for (n = 0; n < num_channels; n++) {
  75578. + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
  75579. + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
  75580. + fiq_print(FIQDBG_INT, st, "NEXTISO ");
  75581. + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  75582. + fiq_fsm_restart_channel(st, n, 0);
  75583. + break;
  75584. + }
  75585. + }
  75586. + }
  75587. +}
  75588. +
  75589. +/**
  75590. + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
  75591. + * @state: Pointer to fiq_state
  75592. + * @n: Channel transaction is active on
  75593. + * @hcint: Copy of host channel interrupt register
  75594. + *
  75595. + * Returns 0 if there are no more transactions for this HC to do, 1
  75596. + * otherwise.
  75597. + */
  75598. +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
  75599. +{
  75600. + struct fiq_channel_state *st = &state->channel[n];
  75601. + int xfer_len = 0, nrpackets = 0;
  75602. + hcdma_data_t hcdma;
  75603. + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
  75604. +
  75605. + xfer_len = fiq_get_xfer_len(state, n);
  75606. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
  75607. +
  75608. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
  75609. +
  75610. + st->hs_isoc_info.index++;
  75611. + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
  75612. + return 0;
  75613. + }
  75614. +
  75615. + /* grab the next DMA address offset from the array */
  75616. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
  75617. + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  75618. +
  75619. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  75620. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  75621. + * this is always set to the maximum size of the endpoint. */
  75622. + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
  75623. + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
  75624. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  75625. + if (nrpackets == 0)
  75626. + nrpackets = 1;
  75627. + st->hcchar_copy.b.multicnt = nrpackets;
  75628. + st->hctsiz_copy.b.pktcnt = nrpackets;
  75629. +
  75630. + /* Initial PID also needs to be set */
  75631. + if (st->hcchar_copy.b.epdir == 0) {
  75632. + st->hctsiz_copy.b.xfersize = xfer_len;
  75633. + switch (st->hcchar_copy.b.multicnt) {
  75634. + case 1:
  75635. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  75636. + break;
  75637. + case 2:
  75638. + case 3:
  75639. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  75640. + break;
  75641. + }
  75642. +
  75643. + } else {
  75644. + switch (st->hcchar_copy.b.multicnt) {
  75645. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  75646. + case 1:
  75647. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  75648. + break;
  75649. + case 2:
  75650. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  75651. + break;
  75652. + case 3:
  75653. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  75654. + break;
  75655. + }
  75656. + }
  75657. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
  75658. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
  75659. + /* Channel is enabled on hcint handler exit */
  75660. + fiq_print(FIQDBG_INT, state, "HSISOOUT");
  75661. + return 1;
  75662. +}
  75663. +
  75664. +
  75665. +/**
  75666. + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
  75667. + * @state: Pointer to the state struct passed from banked FIQ mode registers.
  75668. + * @num_channels: set according to the DWC hardware configuration
  75669. + *
  75670. + * The SOF handler in FSM mode has two functions
  75671. + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
  75672. + * nothing to do
  75673. + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
  75674. + * of holdoff.
  75675. + *
  75676. + * The second part is architecture-specific to mach-bcm2835 -
  75677. + * a sane interrupt controller would have a mask register for ARM interrupt sources
  75678. + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
  75679. + * number (USB) can be enabled. This means that certain parts of the USB specification
  75680. + * that require "wait a little while, then issue another packet" cannot be fulfilled with
  75681. + * the timing granularity required to achieve optimal throughout. The workaround is to use
  75682. + * the SOF "timer" (125uS) to perform this task.
  75683. + */
  75684. +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
  75685. +{
  75686. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
  75687. + int n;
  75688. + int kick_irq = 0;
  75689. +
  75690. + if ((hfnum.b.frnum & 0x7) == 1) {
  75691. + /* We cannot issue csplits for transactions in the last frame past (n+1).1
  75692. + * Check to see if there are any transactions that are stale.
  75693. + * Boot them out.
  75694. + */
  75695. + for (n = 0; n < num_channels; n++) {
  75696. + switch (state->channel[n].fsm) {
  75697. + case FIQ_PER_CSPLIT_WAIT:
  75698. + case FIQ_PER_CSPLIT_NYET1:
  75699. + case FIQ_PER_CSPLIT_POLL:
  75700. + case FIQ_PER_CSPLIT_LAST:
  75701. + /* Check if we are no longer in the same full-speed frame. */
  75702. + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
  75703. + (hfnum.b.frnum & ~0x7))
  75704. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  75705. + break;
  75706. + default:
  75707. + break;
  75708. + }
  75709. + }
  75710. + }
  75711. +
  75712. + for (n = 0; n < num_channels; n++) {
  75713. + switch (state->channel[n].fsm) {
  75714. +
  75715. + case FIQ_NP_SSPLIT_RETRY:
  75716. + case FIQ_NP_IN_CSPLIT_RETRY:
  75717. + case FIQ_NP_OUT_CSPLIT_RETRY:
  75718. + fiq_fsm_restart_channel(state, n, 0);
  75719. + break;
  75720. +
  75721. + case FIQ_HS_ISOC_SLEEPING:
  75722. + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
  75723. + fiq_fsm_restart_channel(state, n, 0);
  75724. + break;
  75725. +
  75726. + case FIQ_PER_SSPLIT_QUEUED:
  75727. + if ((hfnum.b.frnum & 0x7) == 5)
  75728. + break;
  75729. + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
  75730. + if (!fiq_fsm_too_late(state, n)) {
  75731. + fiq_print(FIQDBG_INT, st, "SOF GO %01d", n);
  75732. + fiq_fsm_restart_channel(state, n, 0);
  75733. + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  75734. + } else {
  75735. + /* Transaction cannot be started without risking a device babble error */
  75736. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  75737. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  75738. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  75739. + kick_irq |= 1;
  75740. + }
  75741. + }
  75742. + break;
  75743. +
  75744. + case FIQ_PER_ISO_OUT_PENDING:
  75745. + /* Ordinarily, this should be poked after the SSPLIT
  75746. + * complete interrupt for a competing transfer on the same
  75747. + * TT. Doesn't happen for aborted transactions though.
  75748. + */
  75749. + if ((hfnum.b.frnum & 0x7) >= 5)
  75750. + break;
  75751. + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
  75752. + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
  75753. + * that caused this.
  75754. + */
  75755. + fiq_fsm_restart_channel(state, n, 0);
  75756. + fiq_print(FIQDBG_INT, state, "SOF ISOC");
  75757. + if (state->channel[n].nrpackets == 1) {
  75758. + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  75759. + } else {
  75760. + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  75761. + }
  75762. + }
  75763. + break;
  75764. +
  75765. + case FIQ_PER_CSPLIT_WAIT:
  75766. + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
  75767. + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
  75768. + * will utterly bugger this up though.
  75769. + */
  75770. + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
  75771. + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
  75772. + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
  75773. + fiq_fsm_restart_channel(state, n, 0);
  75774. + fiq_fsm_start_next_periodic(state, num_channels);
  75775. +
  75776. + }
  75777. + break;
  75778. +
  75779. + case FIQ_PER_SPLIT_TIMEOUT:
  75780. + case FIQ_DEQUEUE_ISSUED:
  75781. + /* Ugly: we have to force a HCD interrupt.
  75782. + * Poke the mask for the channel in question.
  75783. + * We will take a fake SOF because of this, but
  75784. + * that's OK.
  75785. + */
  75786. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  75787. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  75788. + kick_irq |= 1;
  75789. + break;
  75790. +
  75791. + default:
  75792. + break;
  75793. + }
  75794. + }
  75795. +
  75796. + if (state->kick_np_queues ||
  75797. + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
  75798. + kick_irq |= 1;
  75799. +
  75800. + return !kick_irq;
  75801. +}
  75802. +
  75803. +
  75804. +/**
  75805. + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
  75806. + * @state: Pointer to the FIQ state struct
  75807. + * @num_channels: Number of channels as per hardware config
  75808. + * @n: channel for which HAINT(i) was raised
  75809. + *
  75810. + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
  75811. + */
  75812. +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
  75813. +{
  75814. + hcint_data_t hcint;
  75815. + hcintmsk_data_t hcintmsk;
  75816. + hcint_data_t hcint_probe;
  75817. + hcchar_data_t hcchar;
  75818. + int handled = 0;
  75819. + int restart = 0;
  75820. + int last_csplit = 0;
  75821. + int start_next_periodic = 0;
  75822. + struct fiq_channel_state *st = &state->channel[n];
  75823. + hfnum_data_t hfnum;
  75824. +
  75825. + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
  75826. + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
  75827. + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
  75828. +
  75829. + if (st->fsm != FIQ_PASSTHROUGH) {
  75830. + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
  75831. + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
  75832. + }
  75833. +
  75834. + switch (st->fsm) {
  75835. +
  75836. + case FIQ_PASSTHROUGH:
  75837. + case FIQ_DEQUEUE_ISSUED:
  75838. + /* doesn't belong to us, kick it upstairs */
  75839. + break;
  75840. +
  75841. + case FIQ_PASSTHROUGH_ERRORSTATE:
  75842. + /* We are here to emulate the error recovery mechanism of the dwc HCD.
  75843. + * Several interrupts are unmasked if a previous transaction failed - it's
  75844. + * death for the FIQ to attempt to handle them as the channel isn't halted.
  75845. + * Emulate what the HCD does in this situation: mask and continue.
  75846. + * The FSM has no other state setup so this has to be handled out-of-band.
  75847. + */
  75848. + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
  75849. + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
  75850. + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
  75851. + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
  75852. + * interrupt, after the device has disappeared.
  75853. + */
  75854. + if (!hcint.b.xacterr)
  75855. + st->nr_errors = 0;
  75856. + hcintmsk.b.nak = 0;
  75857. + hcintmsk.b.ack = 0;
  75858. + hcintmsk.b.datatglerr = 0;
  75859. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
  75860. + return 1;
  75861. + }
  75862. + if (hcint_probe.b.chhltd) {
  75863. + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
  75864. + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
  75865. + return 0;
  75866. + }
  75867. + break;
  75868. +
  75869. + /* Non-periodic state groups */
  75870. + case FIQ_NP_SSPLIT_STARTED:
  75871. + case FIQ_NP_SSPLIT_RETRY:
  75872. + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
  75873. + if (hcint.b.ack) {
  75874. + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
  75875. + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
  75876. + */
  75877. + if(st->hcchar_copy.b.epdir == 1)
  75878. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  75879. + else
  75880. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  75881. + st->nr_errors = 0;
  75882. + handled = 1;
  75883. + fiq_fsm_setup_csplit(state, n);
  75884. + } else if (hcint.b.nak) {
  75885. + // No buffer space in TT. Retry on a uframe boundary.
  75886. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  75887. + handled = 1;
  75888. + } else if (hcint.b.xacterr) {
  75889. + // The only other one we care about is xacterr. This implies HS bus error - retry.
  75890. + st->nr_errors++;
  75891. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  75892. + if (st->nr_errors >= 3) {
  75893. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75894. + } else {
  75895. + handled = 1;
  75896. + restart = 1;
  75897. + }
  75898. + } else {
  75899. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  75900. + handled = 0;
  75901. + restart = 0;
  75902. + }
  75903. + break;
  75904. +
  75905. + case FIQ_NP_IN_CSPLIT_RETRY:
  75906. + /* Received a CSPLIT done interrupt.
  75907. + * Expected Data/NAK/STALL/NYET for IN.
  75908. + */
  75909. + if (hcint.b.xfercomp) {
  75910. + /* For IN, data is present. */
  75911. + st->fsm = FIQ_NP_SPLIT_DONE;
  75912. + } else if (hcint.b.nak) {
  75913. + /* no endpoint data. Punt it upstairs */
  75914. + st->fsm = FIQ_NP_SPLIT_DONE;
  75915. + } else if (hcint.b.nyet) {
  75916. + /* CSPLIT NYET - retry on a uframe boundary. */
  75917. + handled = 1;
  75918. + st->nr_errors = 0;
  75919. + } else if (hcint.b.datatglerr) {
  75920. + /* data toggle errors do not set the xfercomp bit. */
  75921. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  75922. + } else if (hcint.b.xacterr) {
  75923. + /* HS error. Retry immediate */
  75924. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  75925. + st->nr_errors++;
  75926. + if (st->nr_errors >= 3) {
  75927. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75928. + } else {
  75929. + handled = 1;
  75930. + restart = 1;
  75931. + }
  75932. + } else if (hcint.b.stall || hcint.b.bblerr) {
  75933. + /* A STALL implies either a LS bus error or a genuine STALL. */
  75934. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  75935. + } else {
  75936. + /* Hardware bug. It's possible in some cases to
  75937. + * get a channel halt with nothing else set when
  75938. + * the response was a NYET. Treat as local 3-strikes retry.
  75939. + */
  75940. + hcint_data_t hcint_test = hcint;
  75941. + hcint_test.b.chhltd = 0;
  75942. + if (!hcint_test.d32) {
  75943. + st->nr_errors++;
  75944. + if (st->nr_errors >= 3) {
  75945. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75946. + } else {
  75947. + handled = 1;
  75948. + }
  75949. + } else {
  75950. + /* Bail out if something unexpected happened */
  75951. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75952. + }
  75953. + }
  75954. + break;
  75955. +
  75956. + case FIQ_NP_OUT_CSPLIT_RETRY:
  75957. + /* Received a CSPLIT done interrupt.
  75958. + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
  75959. + if (hcint.b.xfercomp) {
  75960. + st->fsm = FIQ_NP_SPLIT_DONE;
  75961. + } else if (hcint.b.nak) {
  75962. + // The HCD will implement the holdoff on frame boundaries.
  75963. + st->fsm = FIQ_NP_SPLIT_DONE;
  75964. + } else if (hcint.b.nyet) {
  75965. + // Hub still processing.
  75966. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  75967. + handled = 1;
  75968. + st->nr_errors = 0;
  75969. + //restart = 1;
  75970. + } else if (hcint.b.xacterr) {
  75971. + /* HS error. retry immediate */
  75972. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  75973. + st->nr_errors++;
  75974. + if (st->nr_errors >= 3) {
  75975. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75976. + } else {
  75977. + handled = 1;
  75978. + restart = 1;
  75979. + }
  75980. + } else if (hcint.b.stall) {
  75981. + /* LS bus error or genuine stall */
  75982. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  75983. + } else {
  75984. + /*
  75985. + * Hardware bug. It's possible in some cases to get a
  75986. + * channel halt with nothing else set when the response was a NYET.
  75987. + * Treat as local 3-strikes retry.
  75988. + */
  75989. + hcint_data_t hcint_test = hcint;
  75990. + hcint_test.b.chhltd = 0;
  75991. + if (!hcint_test.d32) {
  75992. + st->nr_errors++;
  75993. + if (st->nr_errors >= 3) {
  75994. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  75995. + } else {
  75996. + handled = 1;
  75997. + }
  75998. + } else {
  75999. + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
  76000. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  76001. + }
  76002. + }
  76003. + break;
  76004. +
  76005. + /* Periodic split states (except isoc out) */
  76006. + case FIQ_PER_SSPLIT_STARTED:
  76007. + /* Expect an ACK or failure for SSPLIT */
  76008. + if (hcint.b.ack) {
  76009. + /*
  76010. + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
  76011. + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
  76012. + * point for microframe n-3, the packet will not appear on the bus until microframe n.
  76013. + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
  76014. + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
  76015. + * coincident with SOF for n+1.
  76016. + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
  76017. + * These appear to be caused by timing/clock crossing bugs within the core itself.
  76018. + * State machine workaround.
  76019. + */
  76020. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  76021. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  76022. + fiq_fsm_setup_csplit(state, n);
  76023. + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
  76024. + * time. If not, then we're in the next SOF.
  76025. + */
  76026. + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
  76027. + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
  76028. + st->expected_uframe = hfnum.b.frnum;
  76029. + st->fsm = FIQ_PER_CSPLIT_WAIT;
  76030. + } else {
  76031. + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
  76032. + /* For isochronous IN endpoints,
  76033. + * we need to hold off if we are expecting a lot of data */
  76034. + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
  76035. + start_next_periodic = 1;
  76036. + }
  76037. + /* Danger will robinson: we are in a broken state. If our first interrupt after
  76038. + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
  76039. + * lag. Unmask the NYET interrupt.
  76040. + */
  76041. + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  76042. + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
  76043. + restart = 1;
  76044. + }
  76045. + handled = 1;
  76046. + } else if (hcint.b.xacterr) {
  76047. + /* 3-strikes retry is enabled, we have hit our max nr_errors */
  76048. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  76049. + start_next_periodic = 1;
  76050. + } else {
  76051. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  76052. + start_next_periodic = 1;
  76053. + }
  76054. + /* We can now queue the next isochronous OUT transaction, if one is pending. */
  76055. + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
  76056. + fiq_print(FIQDBG_INT, state, "NEXTISO ");
  76057. + }
  76058. + break;
  76059. +
  76060. + case FIQ_PER_CSPLIT_NYET1:
  76061. + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
  76062. + * we are too late and the TT has dropped its CSPLIT fifo.
  76063. + */
  76064. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  76065. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  76066. + start_next_periodic = 1;
  76067. + if (hcint.b.nak) {
  76068. + st->fsm = FIQ_PER_SPLIT_DONE;
  76069. + } else if (hcint.b.xfercomp) {
  76070. + fiq_increment_dma_buf(state, num_channels, n);
  76071. + st->fsm = FIQ_PER_CSPLIT_POLL;
  76072. + st->nr_errors = 0;
  76073. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  76074. + handled = 1;
  76075. + restart = 1;
  76076. + if (!last_csplit)
  76077. + start_next_periodic = 0;
  76078. + } else {
  76079. + st->fsm = FIQ_PER_SPLIT_DONE;
  76080. + }
  76081. + } else if (hcint.b.nyet) {
  76082. + /* Doh. Data lost. */
  76083. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  76084. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  76085. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  76086. + } else {
  76087. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  76088. + }
  76089. + break;
  76090. +
  76091. + case FIQ_PER_CSPLIT_BROKEN_NYET1:
  76092. + /*
  76093. + * we got here because our host channel is in the delayed-interrupt
  76094. + * state and we cannot take a NYET interrupt any later than when it
  76095. + * occurred. Disable then re-enable the channel if this happens to force
  76096. + * CSPLITs to occur at the right time.
  76097. + */
  76098. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  76099. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  76100. + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
  76101. + if (hcint.b.nak) {
  76102. + st->fsm = FIQ_PER_SPLIT_DONE;
  76103. + start_next_periodic = 1;
  76104. + } else if (hcint.b.xfercomp) {
  76105. + fiq_increment_dma_buf(state, num_channels, n);
  76106. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  76107. + st->fsm = FIQ_PER_CSPLIT_POLL;
  76108. + handled = 1;
  76109. + restart = 1;
  76110. + start_next_periodic = 1;
  76111. + /* Reload HCTSIZ for the next transfer */
  76112. + fiq_fsm_reload_hctsiz(state, n);
  76113. + if (!last_csplit)
  76114. + start_next_periodic = 0;
  76115. + } else {
  76116. + st->fsm = FIQ_PER_SPLIT_DONE;
  76117. + }
  76118. + } else if (hcint.b.nyet) {
  76119. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  76120. + start_next_periodic = 1;
  76121. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  76122. + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
  76123. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  76124. + } else {
  76125. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  76126. + }
  76127. + break;
  76128. +
  76129. + case FIQ_PER_CSPLIT_POLL:
  76130. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  76131. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  76132. + start_next_periodic = 1;
  76133. + if (hcint.b.nak) {
  76134. + st->fsm = FIQ_PER_SPLIT_DONE;
  76135. + } else if (hcint.b.xfercomp) {
  76136. + fiq_increment_dma_buf(state, num_channels, n);
  76137. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  76138. + handled = 1;
  76139. + restart = 1;
  76140. + /* Reload HCTSIZ for the next transfer */
  76141. + fiq_fsm_reload_hctsiz(state, n);
  76142. + if (!last_csplit)
  76143. + start_next_periodic = 0;
  76144. + } else {
  76145. + st->fsm = FIQ_PER_SPLIT_DONE;
  76146. + }
  76147. + } else if (hcint.b.nyet) {
  76148. + /* Are we a NYET after the first data packet? */
  76149. + if (st->nrpackets == 0) {
  76150. + st->fsm = FIQ_PER_CSPLIT_NYET1;
  76151. + handled = 1;
  76152. + restart = 1;
  76153. + } else {
  76154. + /* We got a NYET when polling CSPLITs. Can happen
  76155. + * if our heuristic fails, or if someone disables us
  76156. + * for any significant length of time.
  76157. + */
  76158. + if (st->nr_errors >= 3) {
  76159. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  76160. + } else {
  76161. + st->fsm = FIQ_PER_SPLIT_DONE;
  76162. + }
  76163. + }
  76164. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  76165. + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
  76166. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  76167. + } else {
  76168. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  76169. + }
  76170. + break;
  76171. +
  76172. + case FIQ_HS_ISOC_TURBO:
  76173. + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
  76174. + /* more transactions to come */
  76175. + handled = 1;
  76176. + restart = 1;
  76177. + fiq_print(FIQDBG_INT, state, "HSISO M ");
  76178. + } else {
  76179. + st->fsm = FIQ_HS_ISOC_DONE;
  76180. + fiq_print(FIQDBG_INT, state, "HSISO F ");
  76181. + }
  76182. + break;
  76183. +
  76184. + case FIQ_HS_ISOC_ABORTED:
  76185. + /* This abort is called by the driver rewriting the state mid-transaction
  76186. + * which allows the dequeue mechanism to work more effectively.
  76187. + */
  76188. + break;
  76189. +
  76190. + case FIQ_PER_ISO_OUT_ACTIVE:
  76191. + if (hcint.b.ack) {
  76192. + if(fiq_iso_out_advance(state, num_channels, n)) {
  76193. + /* last OUT transfer */
  76194. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  76195. + /*
  76196. + * Assuming the periodic FIFO in the dwc core
  76197. + * actually does its job properly, we can queue
  76198. + * the next ssplit now and in theory, the wire
  76199. + * transactions will be in-order.
  76200. + */
  76201. + // No it doesn't. It appears to process requests in host channel order.
  76202. + //start_next_periodic = 1;
  76203. + }
  76204. + handled = 1;
  76205. + restart = 1;
  76206. + } else {
  76207. + /*
  76208. + * Isochronous transactions carry on regardless. Log the error
  76209. + * and continue.
  76210. + */
  76211. + //explode += 1;
  76212. + st->nr_errors++;
  76213. + if(fiq_iso_out_advance(state, num_channels, n)) {
  76214. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  76215. + //start_next_periodic = 1;
  76216. + }
  76217. + handled = 1;
  76218. + restart = 1;
  76219. + }
  76220. + break;
  76221. +
  76222. + case FIQ_PER_ISO_OUT_LAST:
  76223. + if (hcint.b.ack) {
  76224. + /* All done here */
  76225. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  76226. + } else {
  76227. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  76228. + st->nr_errors++;
  76229. + }
  76230. + start_next_periodic = 1;
  76231. + break;
  76232. +
  76233. + case FIQ_PER_SPLIT_TIMEOUT:
  76234. + /* SOF kicked us because we overran. */
  76235. + start_next_periodic = 1;
  76236. + break;
  76237. +
  76238. + default:
  76239. + break;
  76240. + }
  76241. +
  76242. + if (handled) {
  76243. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
  76244. + } else {
  76245. + /* Copy the regs into the state so the IRQ knows what to do */
  76246. + st->hcint_copy.d32 = hcint.d32;
  76247. + }
  76248. +
  76249. + if (restart) {
  76250. + /* Restart always implies handled. */
  76251. + if (restart == 2) {
  76252. + /* For complete-split INs, the show must go on.
  76253. + * Force a channel restart */
  76254. + fiq_fsm_restart_channel(state, n, 1);
  76255. + } else {
  76256. + fiq_fsm_restart_channel(state, n, 0);
  76257. + }
  76258. + }
  76259. + if (start_next_periodic) {
  76260. + fiq_fsm_start_next_periodic(state, num_channels);
  76261. + }
  76262. + if (st->fsm != FIQ_PASSTHROUGH)
  76263. + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
  76264. +
  76265. + return handled;
  76266. +}
  76267. +
  76268. +
  76269. +/**
  76270. + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
  76271. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  76272. + * @num_channels: set according to the DWC hardware configuration
  76273. + * @dma: pointer to DMA bounce buffers for split transaction slots
  76274. + *
  76275. + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
  76276. + * inside an EHCI or similar host controller regarding split transactions. The DWC core
  76277. + * interrupts each and every time a split transaction packet is received or sent successfully.
  76278. + * This results in either an interrupt storm when everything is working "properly", or
  76279. + * the interrupt latency of the system in general breaks time-sensitive periodic split
  76280. + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
  76281. + * solves these problems.
  76282. + *
  76283. + * Return: void
  76284. + */
  76285. +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
  76286. +{
  76287. + gintsts_data_t gintsts, gintsts_handled;
  76288. + gintmsk_data_t gintmsk;
  76289. + //hfnum_data_t hfnum;
  76290. + haint_data_t haint, haint_handled;
  76291. + haintmsk_data_t haintmsk;
  76292. + int kick_irq = 0;
  76293. +
  76294. + gintsts_handled.d32 = 0;
  76295. + haint_handled.d32 = 0;
  76296. +
  76297. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  76298. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  76299. + gintsts.d32 &= gintmsk.d32;
  76300. +
  76301. + if (gintsts.b.sofintr) {
  76302. + /* For FSM mode, SOF is required to keep the state machine advance for
  76303. + * certain stages of the periodic pipeline. It's death to mask this
  76304. + * interrupt in that case.
  76305. + */
  76306. +
  76307. + if (!fiq_fsm_do_sof(state, num_channels)) {
  76308. + /* Kick IRQ once. Queue advancement means that all pending transactions
  76309. + * will get serviced when the IRQ finally executes.
  76310. + */
  76311. + if (state->gintmsk_saved.b.sofintr == 1)
  76312. + kick_irq |= 1;
  76313. + state->gintmsk_saved.b.sofintr = 0;
  76314. + }
  76315. + gintsts_handled.b.sofintr = 1;
  76316. + }
  76317. +
  76318. + if (gintsts.b.hcintr) {
  76319. + int i;
  76320. + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
  76321. + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
  76322. + haint.d32 &= haintmsk.d32;
  76323. + haint_handled.d32 = 0;
  76324. + for (i=0; i<num_channels; i++) {
  76325. + if (haint.b2.chint & (1 << i)) {
  76326. + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
  76327. + /* HCINT was not handled in FIQ
  76328. + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
  76329. + * Mask HAINT(i) but keep top-level hcint unmasked.
  76330. + */
  76331. + state->haintmsk_saved.b2.chint &= ~(1 << i);
  76332. + } else {
  76333. + /* do_hcintr cleaned up after itself, but clear haint */
  76334. + haint_handled.b2.chint |= (1 << i);
  76335. + }
  76336. + }
  76337. + }
  76338. +
  76339. + if (haint_handled.b2.chint) {
  76340. + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
  76341. + }
  76342. +
  76343. + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
  76344. + /*
  76345. + * This is necessary to avoid multiple retriggers of the MPHI in the case
  76346. + * where interrupts are held off and HCINTs start to pile up.
  76347. + * Only wake up the IRQ if a new interrupt came in, was not handled and was
  76348. + * masked.
  76349. + */
  76350. + haintmsk.d32 &= state->haintmsk_saved.d32;
  76351. + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
  76352. + kick_irq |= 1;
  76353. + }
  76354. + /* Top-Level interrupt - always handled because it's level-sensitive */
  76355. + gintsts_handled.b.hcintr = 1;
  76356. + }
  76357. +
  76358. +
  76359. + /* Clear the bits in the saved register that were not handled but were triggered. */
  76360. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  76361. +
  76362. + /* FIQ didn't handle something - mask has changed - write new mask */
  76363. + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
  76364. + gintmsk.d32 &= state->gintmsk_saved.d32;
  76365. + gintmsk.b.sofintr = 1;
  76366. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  76367. +// fiq_print(FIQDBG_INT, state, "KICKGINT");
  76368. +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
  76369. +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
  76370. + kick_irq |= 1;
  76371. + }
  76372. +
  76373. + if (gintsts_handled.d32) {
  76374. + /* Only applies to edge-sensitive bits in GINTSTS */
  76375. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  76376. + }
  76377. +
  76378. + /* We got an interrupt, didn't handle it. */
  76379. + if (kick_irq) {
  76380. + state->mphi_int_count++;
  76381. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  76382. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  76383. +
  76384. + }
  76385. + state->fiq_done++;
  76386. + mb();
  76387. +}
  76388. +
  76389. +
  76390. +/**
  76391. + * dwc_otg_fiq_nop() - FIQ "lite"
  76392. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  76393. + *
  76394. + * The "nop" handler does not intervene on any interrupts other than SOF.
  76395. + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
  76396. + * with non-periodic/periodic queues) needs to be kicked.
  76397. + *
  76398. + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
  76399. + *
  76400. + * Return: void
  76401. + */
  76402. +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
  76403. +{
  76404. + gintsts_data_t gintsts, gintsts_handled;
  76405. + gintmsk_data_t gintmsk;
  76406. + hfnum_data_t hfnum;
  76407. +
  76408. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  76409. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  76410. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  76411. + gintsts.d32 &= gintmsk.d32;
  76412. + gintsts_handled.d32 = 0;
  76413. +
  76414. + if (gintsts.b.sofintr) {
  76415. + if (!state->kick_np_queues &&
  76416. + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
  76417. + /* SOF handled, no work to do, just ACK interrupt */
  76418. + gintsts_handled.b.sofintr = 1;
  76419. + } else {
  76420. + /* Kick IRQ */
  76421. + state->gintmsk_saved.b.sofintr = 0;
  76422. + }
  76423. + }
  76424. +
  76425. + /* Reset handled interrupts */
  76426. + if(gintsts_handled.d32) {
  76427. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  76428. + }
  76429. +
  76430. + /* Clear the bits in the saved register that were not handled but were triggered. */
  76431. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  76432. +
  76433. + /* We got an interrupt, didn't handle it and want to mask it */
  76434. + if (~(state->gintmsk_saved.d32)) {
  76435. + state->mphi_int_count++;
  76436. + gintmsk.d32 &= state->gintmsk_saved.d32;
  76437. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  76438. + /* Force a clear before another dummy send */
  76439. + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
  76440. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  76441. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  76442. +
  76443. + }
  76444. + state->fiq_done++;
  76445. + mb();
  76446. +}
  76447. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  76448. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h 1970-01-01 01:00:00.000000000 +0100
  76449. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h 2014-09-14 19:04:13.000000000 +0200
  76450. @@ -0,0 +1,353 @@
  76451. +/*
  76452. + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
  76453. + *
  76454. + * Copyright (c) 2013 Raspberry Pi Foundation
  76455. + *
  76456. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  76457. + * All rights reserved.
  76458. + *
  76459. + * Redistribution and use in source and binary forms, with or without
  76460. + * modification, are permitted provided that the following conditions are met:
  76461. + * * Redistributions of source code must retain the above copyright
  76462. + * notice, this list of conditions and the following disclaimer.
  76463. + * * Redistributions in binary form must reproduce the above copyright
  76464. + * notice, this list of conditions and the following disclaimer in the
  76465. + * documentation and/or other materials provided with the distribution.
  76466. + * * Neither the name of Raspberry Pi nor the
  76467. + * names of its contributors may be used to endorse or promote products
  76468. + * derived from this software without specific prior written permission.
  76469. + *
  76470. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  76471. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  76472. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  76473. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  76474. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  76475. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  76476. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  76477. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  76478. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  76479. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  76480. + *
  76481. + * This FIQ implements functionality that performs split transactions on
  76482. + * the dwc_otg hardware without any outside intervention. A split transaction
  76483. + * is "queued" by nominating a specific host channel to perform the entirety
  76484. + * of a split transaction. This FIQ will then perform the microframe-precise
  76485. + * scheduling required in each phase of the transaction until completion.
  76486. + *
  76487. + * The FIQ functionality has been surgically implanted into the Synopsys
  76488. + * vendor-provided driver.
  76489. + *
  76490. + */
  76491. +
  76492. +#ifndef DWC_OTG_FIQ_FSM_H_
  76493. +#define DWC_OTG_FIQ_FSM_H_
  76494. +
  76495. +#include "dwc_otg_regs.h"
  76496. +#include "dwc_otg_cil.h"
  76497. +#include "dwc_otg_hcd.h"
  76498. +#include <linux/kernel.h>
  76499. +#include <linux/irqflags.h>
  76500. +#include <linux/string.h>
  76501. +#include <asm/barrier.h>
  76502. +
  76503. +#if 0
  76504. +#define FLAME_ON(x) \
  76505. +do { \
  76506. + int gpioreg; \
  76507. + \
  76508. + gpioreg = readl(__io_address(0x20200000+0x8)); \
  76509. + gpioreg &= ~(7 << (x-20)*3); \
  76510. + gpioreg |= 0x1 << (x-20)*3; \
  76511. + writel(gpioreg, __io_address(0x20200000+0x8)); \
  76512. + \
  76513. + writel(1<<x, __io_address(0x20200000+(0x1C))); \
  76514. +} while (0)
  76515. +
  76516. +#define FLAME_OFF(x) \
  76517. +do { \
  76518. + writel(1<<x, __io_address(0x20200000+(0x28))); \
  76519. +} while (0)
  76520. +#else
  76521. +#define FLAME_ON(x) do { } while (0)
  76522. +#define FLAME_OFF(X) do { } while (0)
  76523. +#endif
  76524. +
  76525. +/* This is a quick-and-dirty arch-specific register read/write. We know that
  76526. + * writes to a peripheral on BCM2835 will always arrive in-order, also that
  76527. + * reads and writes are executed in-order therefore the need for memory barriers
  76528. + * is obviated if we're only talking to USB.
  76529. + */
  76530. +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
  76531. +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
  76532. +
  76533. +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
  76534. +#define GINTSTS 0x014
  76535. +#define GINTMSK 0x018
  76536. +/* Debug register. Poll the top of the received packets FIFO. */
  76537. +#define GRXSTSR 0x01C
  76538. +#define HFNUM 0x408
  76539. +#define HAINT 0x414
  76540. +#define HAINTMSK 0x418
  76541. +#define HPRT0 0x440
  76542. +
  76543. +/* HC_regs start from an offset of 0x500 */
  76544. +#define HC_START 0x500
  76545. +#define HC_OFFSET 0x020
  76546. +
  76547. +#define HC_DMA 0x514
  76548. +
  76549. +#define HCCHAR 0x00
  76550. +#define HCSPLT 0x04
  76551. +#define HCINT 0x08
  76552. +#define HCINTMSK 0x0C
  76553. +#define HCTSIZ 0x10
  76554. +
  76555. +#define ISOC_XACTPOS_ALL 0b11
  76556. +#define ISOC_XACTPOS_BEGIN 0b10
  76557. +#define ISOC_XACTPOS_MID 0b00
  76558. +#define ISOC_XACTPOS_END 0b01
  76559. +
  76560. +#define DWC_PID_DATA2 0b01
  76561. +#define DWC_PID_MDATA 0b11
  76562. +#define DWC_PID_DATA1 0b10
  76563. +#define DWC_PID_DATA0 0b00
  76564. +
  76565. +typedef struct {
  76566. + volatile void* base;
  76567. + volatile void* ctrl;
  76568. + volatile void* outdda;
  76569. + volatile void* outddb;
  76570. + volatile void* intstat;
  76571. +} mphi_regs_t;
  76572. +
  76573. +
  76574. +enum fiq_debug_level {
  76575. + FIQDBG_SCHED = (1 << 0),
  76576. + FIQDBG_INT = (1 << 1),
  76577. + FIQDBG_ERR = (1 << 2),
  76578. + FIQDBG_PORTHUB = (1 << 3),
  76579. +};
  76580. +
  76581. +struct fiq_state;
  76582. +
  76583. +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  76584. +#if 0
  76585. +#define fiq_print _fiq_print
  76586. +#else
  76587. +#define fiq_print(x, y, ...)
  76588. +#endif
  76589. +
  76590. +extern bool fiq_enable, fiq_fsm_enable;
  76591. +extern ushort nak_holdoff;
  76592. +
  76593. +/**
  76594. + * enum fiq_fsm_state - The FIQ FSM states.
  76595. + *
  76596. + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
  76597. + * USB2.0 specification for host responses to various transaction states.
  76598. + * There are modifications to this host state machine because of a variety of
  76599. + * quirks and limitations in the dwc_otg hardware.
  76600. + *
  76601. + * The fsm state is also used to communicate back to the driver on completion of
  76602. + * a split transaction. The end states are used in conjunction with the interrupts
  76603. + * raised by the final transaction.
  76604. + */
  76605. +enum fiq_fsm_state {
  76606. + /* FIQ isn't enabled for this host channel */
  76607. + FIQ_PASSTHROUGH = 0,
  76608. + /* For the first interrupt received for this channel,
  76609. + * the FIQ has to ack any interrupts indicating success. */
  76610. + FIQ_PASSTHROUGH_ERRORSTATE = 31,
  76611. + /* Nonperiodic state groups */
  76612. + FIQ_NP_SSPLIT_STARTED = 1,
  76613. + FIQ_NP_SSPLIT_RETRY = 2,
  76614. + FIQ_NP_OUT_CSPLIT_RETRY = 3,
  76615. + FIQ_NP_IN_CSPLIT_RETRY = 4,
  76616. + FIQ_NP_SPLIT_DONE = 5,
  76617. + FIQ_NP_SPLIT_LS_ABORTED = 6,
  76618. + /* This differentiates a HS transaction error from a LS one
  76619. + * (handling the hub state is different) */
  76620. + FIQ_NP_SPLIT_HS_ABORTED = 7,
  76621. +
  76622. + /* Periodic state groups */
  76623. + /* Periodic transactions are either started directly by the IRQ handler
  76624. + * or deferred if the TT is already in use.
  76625. + */
  76626. + FIQ_PER_SSPLIT_QUEUED = 8,
  76627. + FIQ_PER_SSPLIT_STARTED = 9,
  76628. + FIQ_PER_SSPLIT_LAST = 10,
  76629. +
  76630. +
  76631. + FIQ_PER_ISO_OUT_PENDING = 11,
  76632. + FIQ_PER_ISO_OUT_ACTIVE = 12,
  76633. + FIQ_PER_ISO_OUT_LAST = 13,
  76634. + FIQ_PER_ISO_OUT_DONE = 27,
  76635. +
  76636. + FIQ_PER_CSPLIT_WAIT = 14,
  76637. + FIQ_PER_CSPLIT_NYET1 = 15,
  76638. + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
  76639. + FIQ_PER_CSPLIT_NYET_FAFF = 29,
  76640. + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
  76641. + FIQ_PER_CSPLIT_POLL = 16,
  76642. + /* The last CSPLIT for a transaction has been issued, differentiates
  76643. + * for the state machine to queue the next packet.
  76644. + */
  76645. + FIQ_PER_CSPLIT_LAST = 17,
  76646. +
  76647. + FIQ_PER_SPLIT_DONE = 18,
  76648. + FIQ_PER_SPLIT_LS_ABORTED = 19,
  76649. + FIQ_PER_SPLIT_HS_ABORTED = 20,
  76650. + FIQ_PER_SPLIT_NYET_ABORTED = 21,
  76651. + /* Frame rollover has occurred without the transaction finishing. */
  76652. + FIQ_PER_SPLIT_TIMEOUT = 22,
  76653. +
  76654. + /* FIQ-accelerated HS Isochronous state groups */
  76655. + FIQ_HS_ISOC_TURBO = 23,
  76656. + /* For interval > 1, SOF wakes up the isochronous FSM */
  76657. + FIQ_HS_ISOC_SLEEPING = 24,
  76658. + FIQ_HS_ISOC_DONE = 25,
  76659. + FIQ_HS_ISOC_ABORTED = 26,
  76660. + FIQ_DEQUEUE_ISSUED = 30,
  76661. + FIQ_TEST = 32,
  76662. +};
  76663. +
  76664. +struct fiq_stack {
  76665. + int magic1;
  76666. + uint8_t stack[2048];
  76667. + int magic2;
  76668. +};
  76669. +
  76670. +
  76671. +/**
  76672. + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
  76673. + * @index: Number of slots reported used for IN transactions / number of slots
  76674. + * transmitted for an OUT transaction
  76675. + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
  76676. + *
  76677. + * Split transaction transfers can have variable length depending on other bus
  76678. + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
  76679. + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
  76680. + * can happen per-frame.
  76681. + */
  76682. +struct fiq_dma_info {
  76683. + u8 index;
  76684. + u8 slot_len[6];
  76685. +};
  76686. +
  76687. +struct __attribute__((packed)) fiq_split_dma_slot {
  76688. + u8 buf[188];
  76689. +};
  76690. +
  76691. +struct fiq_dma_channel {
  76692. + struct __attribute__((packed)) fiq_split_dma_slot index[6];
  76693. +};
  76694. +
  76695. +struct fiq_dma_blob {
  76696. + struct __attribute__((packed)) fiq_dma_channel channel[0];
  76697. +};
  76698. +
  76699. +/**
  76700. + * struct fiq_hs_isoc_info - USB2.0 isochronous data
  76701. + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
  76702. + * @nrframes: Total length of iso_frame_desc array
  76703. + * @index: Current index (FIQ-maintained)
  76704. + *
  76705. + */
  76706. +struct fiq_hs_isoc_info {
  76707. + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
  76708. + unsigned int nrframes;
  76709. + unsigned int index;
  76710. +};
  76711. +
  76712. +/**
  76713. + * struct fiq_channel_state - FIQ state machine storage
  76714. + * @fsm: Current state of the channel as understood by the FIQ
  76715. + * @nr_errors: Number of transaction errors on this split-transaction
  76716. + * @hub_addr: SSPLIT/CSPLIT destination hub
  76717. + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
  76718. + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
  76719. + * split-IN, number of CSPLIT data packets that were received.
  76720. + * @hcchar_copy:
  76721. + * @hcsplt_copy:
  76722. + * @hcintmsk_copy:
  76723. + * @hctsiz_copy: Copies of the host channel registers.
  76724. + * For use as scratch, or for returning state.
  76725. + *
  76726. + * The fiq_channel_state is state storage between interrupts for a host channel. The
  76727. + * FSM state is stored here. Members of this structure must only be set up by the
  76728. + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
  76729. + * has updated the state to either a COMPLETE state group or ABORT state group.
  76730. + */
  76731. +
  76732. +struct fiq_channel_state {
  76733. + enum fiq_fsm_state fsm;
  76734. + unsigned int nr_errors;
  76735. + unsigned int hub_addr;
  76736. + unsigned int port_addr;
  76737. + /* Hardware bug workaround: sometimes channel halt interrupts are
  76738. + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
  76739. + unsigned int expected_uframe;
  76740. + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
  76741. + unsigned int nrpackets;
  76742. + struct fiq_dma_info dma_info;
  76743. + struct fiq_hs_isoc_info hs_isoc_info;
  76744. + /* Copies of HC registers - in/out communication from/to IRQ handler
  76745. + * and for ease of channel setup. A bit of mungeing is performed - for
  76746. + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
  76747. + */
  76748. + hcchar_data_t hcchar_copy;
  76749. + hcsplt_data_t hcsplt_copy;
  76750. + hcint_data_t hcint_copy;
  76751. + hcintmsk_data_t hcintmsk_copy;
  76752. + hctsiz_data_t hctsiz_copy;
  76753. + hcdma_data_t hcdma_copy;
  76754. +};
  76755. +
  76756. +/**
  76757. + * struct fiq_state - top-level FIQ state machine storage
  76758. + * @mphi_regs: virtual address of the MPHI peripheral register file
  76759. + * @dwc_regs_base: virtual address of the base of the DWC core register file
  76760. + * @dma_base: physical address for the base of the DMA bounce buffers
  76761. + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
  76762. + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
  76763. + * Used for determining which interrupts fired to set off the IRQ handler.
  76764. + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
  76765. + * @np_count: Non-periodic transactions in the active queue
  76766. + * @np_sent: Count of non-periodic transactions that have completed
  76767. + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
  76768. + * this is the next frame on which a SOF interrupt is required. Used to hold off
  76769. + * passing SOF through to the driver until necessary.
  76770. + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
  76771. + * channels configured into the core logic.
  76772. + *
  76773. + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
  76774. + * It contains top-level state information.
  76775. + */
  76776. +struct fiq_state {
  76777. + mphi_regs_t mphi_regs;
  76778. + void *dwc_regs_base;
  76779. + dma_addr_t dma_base;
  76780. + struct fiq_dma_blob *fiq_dmab;
  76781. + void *dummy_send;
  76782. + gintmsk_data_t gintmsk_saved;
  76783. + haintmsk_data_t haintmsk_saved;
  76784. + int mphi_int_count;
  76785. + unsigned int fiq_done;
  76786. + unsigned int kick_np_queues;
  76787. + unsigned int next_sched_frame;
  76788. +#ifdef FIQ_DEBUG
  76789. + char * buffer;
  76790. + unsigned int bufsiz;
  76791. +#endif
  76792. + struct fiq_channel_state channel[0];
  76793. +};
  76794. +
  76795. +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  76796. +
  76797. +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  76798. +
  76799. +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
  76800. +
  76801. +extern void dwc_otg_fiq_nop(struct fiq_state *state);
  76802. +
  76803. +#endif /* DWC_OTG_FIQ_FSM_H_ */
  76804. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  76805. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S 1970-01-01 01:00:00.000000000 +0100
  76806. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S 2014-09-14 19:04:13.000000000 +0200
  76807. @@ -0,0 +1,81 @@
  76808. +/*
  76809. + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
  76810. + *
  76811. + * Copyright (c) 2013 Raspberry Pi Foundation
  76812. + *
  76813. + * Author: Jonathan Bell <jonathan@raspberrypi.org>
  76814. + * All rights reserved.
  76815. + *
  76816. + * Redistribution and use in source and binary forms, with or without
  76817. + * modification, are permitted provided that the following conditions are met:
  76818. + * * Redistributions of source code must retain the above copyright
  76819. + * notice, this list of conditions and the following disclaimer.
  76820. + * * Redistributions in binary form must reproduce the above copyright
  76821. + * notice, this list of conditions and the following disclaimer in the
  76822. + * documentation and/or other materials provided with the distribution.
  76823. + * * Neither the name of Raspberry Pi nor the
  76824. + * names of its contributors may be used to endorse or promote products
  76825. + * derived from this software without specific prior written permission.
  76826. + *
  76827. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  76828. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  76829. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  76830. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  76831. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  76832. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  76833. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  76834. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  76835. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  76836. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  76837. + */
  76838. +
  76839. +
  76840. +#include <asm/assembler.h>
  76841. +#include <linux/linkage.h>
  76842. +
  76843. +
  76844. +.text
  76845. +
  76846. +.global _dwc_otg_fiq_stub_end;
  76847. +
  76848. +/**
  76849. + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
  76850. + * a C-style function call with arguments from the FIQ banked registers.
  76851. + * r0 = &hcd->fiq_state
  76852. + * r1 = &hcd->num_channels
  76853. + * r2 = &hcd->dma_buffers
  76854. + * Tramples: r0, r1, r2, r4, fp, ip
  76855. + */
  76856. +
  76857. +ENTRY(_dwc_otg_fiq_stub)
  76858. + /* Stash unbanked regs - SP will have been set up for us */
  76859. + mov ip, sp;
  76860. + stmdb sp!, {r0-r12, lr};
  76861. +#ifdef FIQ_DEBUG
  76862. + // Cycle profiling - read cycle counter at start
  76863. + mrc p15, 0, r5, c15, c12, 1;
  76864. +#endif
  76865. + /* r11 = fp, don't trample it */
  76866. + mov r4, fp;
  76867. + /* set EABI frame size */
  76868. + sub fp, ip, #512;
  76869. +
  76870. + /* for fiq NOP mode - just need state */
  76871. + mov r0, r8;
  76872. + /* r9 = num_channels */
  76873. + mov r1, r9;
  76874. + /* r10 = struct *dma_bufs */
  76875. +// mov r2, r10;
  76876. +
  76877. + /* r4 = &fiq_c_function */
  76878. + blx r4;
  76879. +#ifdef FIQ_DEBUG
  76880. + mrc p15, 0, r4, c15, c12, 1;
  76881. + subs r5, r5, r4;
  76882. + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
  76883. +#endif
  76884. + ldmia sp!, {r0-r12, lr};
  76885. + subs pc, lr, #4;
  76886. +_dwc_otg_fiq_stub_end:
  76887. +END(_dwc_otg_fiq_stub)
  76888. +
  76889. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  76890. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 1970-01-01 01:00:00.000000000 +0100
  76891. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.c 2014-09-14 19:04:13.000000000 +0200
  76892. @@ -0,0 +1,4217 @@
  76893. +
  76894. +/* ==========================================================================
  76895. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  76896. + * $Revision: #104 $
  76897. + * $Date: 2011/10/24 $
  76898. + * $Change: 1871159 $
  76899. + *
  76900. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  76901. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  76902. + * otherwise expressly agreed to in writing between Synopsys and you.
  76903. + *
  76904. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  76905. + * any End User Software License Agreement or Agreement for Licensed Product
  76906. + * with Synopsys or any supplement thereto. You are permitted to use and
  76907. + * redistribute this Software in source and binary forms, with or without
  76908. + * modification, provided that redistributions of source code must retain this
  76909. + * notice. You may not view, use, disclose, copy or distribute this file or
  76910. + * any information contained herein except pursuant to this license grant from
  76911. + * Synopsys. If you do not agree with this notice, including the disclaimer
  76912. + * below, then you are not authorized to use the Software.
  76913. + *
  76914. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  76915. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  76916. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76917. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  76918. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  76919. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  76920. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  76921. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  76922. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  76923. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  76924. + * DAMAGE.
  76925. + * ========================================================================== */
  76926. +#ifndef DWC_DEVICE_ONLY
  76927. +
  76928. +/** @file
  76929. + * This file implements HCD Core. All code in this file is portable and doesn't
  76930. + * use any OS specific functions.
  76931. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  76932. + * header file.
  76933. + */
  76934. +
  76935. +#include <linux/usb.h>
  76936. +#include <linux/usb/hcd.h>
  76937. +
  76938. +#include "dwc_otg_hcd.h"
  76939. +#include "dwc_otg_regs.h"
  76940. +#include "dwc_otg_fiq_fsm.h"
  76941. +
  76942. +extern bool microframe_schedule;
  76943. +extern uint16_t fiq_fsm_mask, nak_holdoff;
  76944. +
  76945. +//#define DEBUG_HOST_CHANNELS
  76946. +#ifdef DEBUG_HOST_CHANNELS
  76947. +static int last_sel_trans_num_per_scheduled = 0;
  76948. +static int last_sel_trans_num_nonper_scheduled = 0;
  76949. +static int last_sel_trans_num_avail_hc_at_start = 0;
  76950. +static int last_sel_trans_num_avail_hc_at_end = 0;
  76951. +#endif /* DEBUG_HOST_CHANNELS */
  76952. +
  76953. +
  76954. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  76955. +{
  76956. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  76957. +}
  76958. +
  76959. +/**
  76960. + * Connection timeout function. An OTG host is required to display a
  76961. + * message if the device does not connect within 10 seconds.
  76962. + */
  76963. +void dwc_otg_hcd_connect_timeout(void *ptr)
  76964. +{
  76965. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  76966. + DWC_PRINTF("Connect Timeout\n");
  76967. + __DWC_ERROR("Device Not Connected/Responding\n");
  76968. +}
  76969. +
  76970. +#if defined(DEBUG)
  76971. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  76972. +{
  76973. + if (qh->channel != NULL) {
  76974. + dwc_hc_t *hc = qh->channel;
  76975. + dwc_list_link_t *item;
  76976. + dwc_otg_qh_t *qh_item;
  76977. + int num_channels = hcd->core_if->core_params->host_channels;
  76978. + int i;
  76979. +
  76980. + dwc_otg_hc_regs_t *hc_regs;
  76981. + hcchar_data_t hcchar;
  76982. + hcsplt_data_t hcsplt;
  76983. + hctsiz_data_t hctsiz;
  76984. + uint32_t hcdma;
  76985. +
  76986. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  76987. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  76988. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  76989. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  76990. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  76991. +
  76992. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  76993. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  76994. + hcsplt.d32);
  76995. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  76996. + hcdma);
  76997. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  76998. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  76999. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  77000. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  77001. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  77002. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  77003. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  77004. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  77005. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  77006. + DWC_PRINTF(" qh: %p\n", hc->qh);
  77007. + DWC_PRINTF(" NP inactive sched:\n");
  77008. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  77009. + qh_item =
  77010. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  77011. + DWC_PRINTF(" %p\n", qh_item);
  77012. + }
  77013. + DWC_PRINTF(" NP active sched:\n");
  77014. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  77015. + qh_item =
  77016. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  77017. + DWC_PRINTF(" %p\n", qh_item);
  77018. + }
  77019. + DWC_PRINTF(" Channels: \n");
  77020. + for (i = 0; i < num_channels; i++) {
  77021. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  77022. + DWC_PRINTF(" %2d: %p\n", i, hc);
  77023. + }
  77024. + }
  77025. +}
  77026. +#else
  77027. +#define dump_channel_info(hcd, qh)
  77028. +#endif /* DEBUG */
  77029. +
  77030. +/**
  77031. + * Work queue function for starting the HCD when A-Cable is connected.
  77032. + * The hcd_start() must be called in a process context.
  77033. + */
  77034. +static void hcd_start_func(void *_vp)
  77035. +{
  77036. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  77037. +
  77038. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  77039. + if (hcd) {
  77040. + hcd->fops->start(hcd);
  77041. + }
  77042. +}
  77043. +
  77044. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  77045. +{
  77046. +#ifdef DEBUG
  77047. + int i;
  77048. + int num_channels = hcd->core_if->core_params->host_channels;
  77049. + for (i = 0; i < num_channels; i++) {
  77050. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  77051. + }
  77052. +#endif
  77053. +}
  77054. +
  77055. +static void del_timers(dwc_otg_hcd_t * hcd)
  77056. +{
  77057. + del_xfer_timers(hcd);
  77058. + DWC_TIMER_CANCEL(hcd->conn_timer);
  77059. +}
  77060. +
  77061. +/**
  77062. + * Processes all the URBs in a single list of QHs. Completes them with
  77063. + * -ESHUTDOWN and frees the QTD.
  77064. + */
  77065. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  77066. +{
  77067. + dwc_list_link_t *qh_item, *qh_tmp;
  77068. + dwc_otg_qh_t *qh;
  77069. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  77070. +
  77071. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  77072. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  77073. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  77074. + &qh->qtd_list, qtd_list_entry) {
  77075. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  77076. + if (qtd->urb != NULL) {
  77077. + hcd->fops->complete(hcd, qtd->urb->priv,
  77078. + qtd->urb, -DWC_E_SHUTDOWN);
  77079. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  77080. + }
  77081. +
  77082. + }
  77083. + if(qh->channel) {
  77084. + /* Using hcchar.chen == 1 is not a reliable test.
  77085. + * It is possible that the channel has already halted
  77086. + * but not yet been through the IRQ handler.
  77087. + */
  77088. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  77089. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  77090. + if(microframe_schedule)
  77091. + hcd->available_host_channels++;
  77092. + qh->channel = NULL;
  77093. + }
  77094. + dwc_otg_hcd_qh_remove(hcd, qh);
  77095. + }
  77096. +}
  77097. +
  77098. +/**
  77099. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  77100. + * and periodic schedules. The QTD associated with each URB is removed from
  77101. + * the schedule and freed. This function may be called when a disconnect is
  77102. + * detected or when the HCD is being stopped.
  77103. + */
  77104. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  77105. +{
  77106. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  77107. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  77108. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  77109. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  77110. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  77111. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  77112. +}
  77113. +
  77114. +/**
  77115. + * Start the connection timer. An OTG host is required to display a
  77116. + * message if the device does not connect within 10 seconds. The
  77117. + * timer is deleted if a port connect interrupt occurs before the
  77118. + * timer expires.
  77119. + */
  77120. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  77121. +{
  77122. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  77123. +}
  77124. +
  77125. +/**
  77126. + * HCD Callback function for disconnect of the HCD.
  77127. + *
  77128. + * @param p void pointer to the <code>struct usb_hcd</code>
  77129. + */
  77130. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  77131. +{
  77132. + dwc_otg_hcd_t *dwc_otg_hcd;
  77133. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  77134. + dwc_otg_hcd = p;
  77135. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  77136. + return 1;
  77137. +}
  77138. +
  77139. +/**
  77140. + * HCD Callback function for starting the HCD when A-Cable is
  77141. + * connected.
  77142. + *
  77143. + * @param p void pointer to the <code>struct usb_hcd</code>
  77144. + */
  77145. +static int32_t dwc_otg_hcd_start_cb(void *p)
  77146. +{
  77147. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  77148. + dwc_otg_core_if_t *core_if;
  77149. + hprt0_data_t hprt0;
  77150. +
  77151. + core_if = dwc_otg_hcd->core_if;
  77152. +
  77153. + if (core_if->op_state == B_HOST) {
  77154. + /*
  77155. + * Reset the port. During a HNP mode switch the reset
  77156. + * needs to occur within 1ms and have a duration of at
  77157. + * least 50ms.
  77158. + */
  77159. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  77160. + hprt0.b.prtrst = 1;
  77161. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  77162. + }
  77163. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  77164. + hcd_start_func, dwc_otg_hcd, 50,
  77165. + "start hcd");
  77166. +
  77167. + return 1;
  77168. +}
  77169. +
  77170. +/**
  77171. + * HCD Callback function for disconnect of the HCD.
  77172. + *
  77173. + * @param p void pointer to the <code>struct usb_hcd</code>
  77174. + */
  77175. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  77176. +{
  77177. + gintsts_data_t intr;
  77178. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  77179. +
  77180. + /*
  77181. + * Set status flags for the hub driver.
  77182. + */
  77183. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  77184. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  77185. + if(fiq_enable)
  77186. + local_fiq_disable();
  77187. + /*
  77188. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  77189. + * interrupt mask and status bits and disabling subsequent host
  77190. + * channel interrupts.
  77191. + */
  77192. + intr.d32 = 0;
  77193. + intr.b.nptxfempty = 1;
  77194. + intr.b.ptxfempty = 1;
  77195. + intr.b.hcintr = 1;
  77196. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  77197. + intr.d32, 0);
  77198. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  77199. + intr.d32, 0);
  77200. +
  77201. + del_timers(dwc_otg_hcd);
  77202. +
  77203. + /*
  77204. + * Turn off the vbus power only if the core has transitioned to device
  77205. + * mode. If still in host mode, need to keep power on to detect a
  77206. + * reconnection.
  77207. + */
  77208. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  77209. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  77210. + hprt0_data_t hprt0 = {.d32 = 0 };
  77211. + DWC_PRINTF("Disconnect: PortPower off\n");
  77212. + hprt0.b.prtpwr = 0;
  77213. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  77214. + hprt0.d32);
  77215. + }
  77216. +
  77217. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  77218. + }
  77219. +
  77220. + /* Respond with an error status to all URBs in the schedule. */
  77221. + kill_all_urbs(dwc_otg_hcd);
  77222. +
  77223. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  77224. + /* Clean up any host channels that were in use. */
  77225. + int num_channels;
  77226. + int i;
  77227. + dwc_hc_t *channel;
  77228. + dwc_otg_hc_regs_t *hc_regs;
  77229. + hcchar_data_t hcchar;
  77230. +
  77231. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  77232. +
  77233. + if (!dwc_otg_hcd->core_if->dma_enable) {
  77234. + /* Flush out any channel requests in slave mode. */
  77235. + for (i = 0; i < num_channels; i++) {
  77236. + channel = dwc_otg_hcd->hc_ptr_array[i];
  77237. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  77238. + (channel, hc_list_entry)) {
  77239. + hc_regs =
  77240. + dwc_otg_hcd->core_if->
  77241. + host_if->hc_regs[i];
  77242. + hcchar.d32 =
  77243. + DWC_READ_REG32(&hc_regs->hcchar);
  77244. + if (hcchar.b.chen) {
  77245. + hcchar.b.chen = 0;
  77246. + hcchar.b.chdis = 1;
  77247. + hcchar.b.epdir = 0;
  77248. + DWC_WRITE_REG32
  77249. + (&hc_regs->hcchar,
  77250. + hcchar.d32);
  77251. + }
  77252. + }
  77253. + }
  77254. + }
  77255. +
  77256. + for (i = 0; i < num_channels; i++) {
  77257. + channel = dwc_otg_hcd->hc_ptr_array[i];
  77258. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  77259. + hc_regs =
  77260. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  77261. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  77262. + if (hcchar.b.chen) {
  77263. + /* Halt the channel. */
  77264. + hcchar.b.chdis = 1;
  77265. + DWC_WRITE_REG32(&hc_regs->hcchar,
  77266. + hcchar.d32);
  77267. + }
  77268. +
  77269. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  77270. + channel);
  77271. + DWC_CIRCLEQ_INSERT_TAIL
  77272. + (&dwc_otg_hcd->free_hc_list, channel,
  77273. + hc_list_entry);
  77274. + /*
  77275. + * Added for Descriptor DMA to prevent channel double cleanup
  77276. + * in release_channel_ddma(). Which called from ep_disable
  77277. + * when device disconnect.
  77278. + */
  77279. + channel->qh = NULL;
  77280. + }
  77281. + }
  77282. + if(fiq_fsm_enable) {
  77283. + for(i=0; i < 128; i++) {
  77284. + dwc_otg_hcd->hub_port[i] = 0;
  77285. + }
  77286. + }
  77287. +
  77288. + }
  77289. +
  77290. + if(fiq_enable)
  77291. + local_fiq_enable();
  77292. +
  77293. + if (dwc_otg_hcd->fops->disconnect) {
  77294. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  77295. + }
  77296. +
  77297. + return 1;
  77298. +}
  77299. +
  77300. +/**
  77301. + * HCD Callback function for stopping the HCD.
  77302. + *
  77303. + * @param p void pointer to the <code>struct usb_hcd</code>
  77304. + */
  77305. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  77306. +{
  77307. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  77308. +
  77309. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  77310. + dwc_otg_hcd_stop(dwc_otg_hcd);
  77311. + return 1;
  77312. +}
  77313. +
  77314. +#ifdef CONFIG_USB_DWC_OTG_LPM
  77315. +/**
  77316. + * HCD Callback function for sleep of HCD.
  77317. + *
  77318. + * @param p void pointer to the <code>struct usb_hcd</code>
  77319. + */
  77320. +static int dwc_otg_hcd_sleep_cb(void *p)
  77321. +{
  77322. + dwc_otg_hcd_t *hcd = p;
  77323. +
  77324. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  77325. +
  77326. + return 0;
  77327. +}
  77328. +#endif
  77329. +
  77330. +
  77331. +/**
  77332. + * HCD Callback function for Remote Wakeup.
  77333. + *
  77334. + * @param p void pointer to the <code>struct usb_hcd</code>
  77335. + */
  77336. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  77337. +{
  77338. + dwc_otg_hcd_t *hcd = p;
  77339. +
  77340. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  77341. + hcd->flags.b.port_suspend_change = 1;
  77342. + }
  77343. +#ifdef CONFIG_USB_DWC_OTG_LPM
  77344. + else {
  77345. + hcd->flags.b.port_l1_change = 1;
  77346. + }
  77347. +#endif
  77348. + return 0;
  77349. +}
  77350. +
  77351. +/**
  77352. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  77353. + * stopped.
  77354. + */
  77355. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  77356. +{
  77357. + hprt0_data_t hprt0 = {.d32 = 0 };
  77358. +
  77359. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  77360. +
  77361. + /*
  77362. + * The root hub should be disconnected before this function is called.
  77363. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  77364. + * and the QH lists (via ..._hcd_endpoint_disable).
  77365. + */
  77366. +
  77367. + /* Turn off all host-specific interrupts. */
  77368. + dwc_otg_disable_host_interrupts(hcd->core_if);
  77369. +
  77370. + /* Turn off the vbus power */
  77371. + DWC_PRINTF("PortPower off\n");
  77372. + hprt0.b.prtpwr = 0;
  77373. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  77374. + dwc_mdelay(1);
  77375. +}
  77376. +
  77377. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  77378. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  77379. + int atomic_alloc)
  77380. +{
  77381. + int retval = 0;
  77382. + uint8_t needs_scheduling = 0;
  77383. + dwc_otg_transaction_type_e tr_type;
  77384. + dwc_otg_qtd_t *qtd;
  77385. + gintmsk_data_t intr_mask = {.d32 = 0 };
  77386. + hprt0_data_t hprt0 = { .d32 = 0 };
  77387. +
  77388. +#ifdef DEBUG /* integrity checks (Broadcom) */
  77389. + if (NULL == hcd->core_if) {
  77390. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  77391. + /* No longer connected. */
  77392. + return -DWC_E_INVALID;
  77393. + }
  77394. +#endif
  77395. + if (!hcd->flags.b.port_connect_status) {
  77396. + /* No longer connected. */
  77397. + DWC_ERROR("Not connected\n");
  77398. + return -DWC_E_NO_DEVICE;
  77399. + }
  77400. +
  77401. + /* Some core configurations cannot support LS traffic on a FS root port */
  77402. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  77403. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  77404. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  77405. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  77406. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  77407. + return -DWC_E_NO_DEVICE;
  77408. + }
  77409. + }
  77410. +
  77411. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  77412. + if (qtd == NULL) {
  77413. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  77414. + return -DWC_E_NO_MEMORY;
  77415. + }
  77416. +#ifdef DEBUG /* integrity checks (Broadcom) */
  77417. + if (qtd->urb == NULL) {
  77418. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  77419. + return -DWC_E_NO_MEMORY;
  77420. + }
  77421. + if (qtd->urb->priv == NULL) {
  77422. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  77423. + return -DWC_E_NO_MEMORY;
  77424. + }
  77425. +#endif
  77426. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  77427. + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
  77428. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  77429. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  77430. + needs_scheduling = 0;
  77431. +
  77432. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  77433. + // creates a new queue in ep_handle if it doesn't exist already
  77434. + if (retval < 0) {
  77435. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  77436. + "Error status %d\n", retval);
  77437. + dwc_otg_hcd_qtd_free(qtd);
  77438. + return retval;
  77439. + }
  77440. +
  77441. + if(needs_scheduling) {
  77442. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  77443. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  77444. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  77445. + }
  77446. + }
  77447. + return retval;
  77448. +}
  77449. +
  77450. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  77451. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  77452. +{
  77453. + dwc_otg_qh_t *qh;
  77454. + dwc_otg_qtd_t *urb_qtd;
  77455. + BUG_ON(!hcd);
  77456. + BUG_ON(!dwc_otg_urb);
  77457. +
  77458. +#ifdef DEBUG /* integrity checks (Broadcom) */
  77459. +
  77460. + if (hcd == NULL) {
  77461. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  77462. + return -DWC_E_INVALID;
  77463. + }
  77464. + if (dwc_otg_urb == NULL) {
  77465. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  77466. + return -DWC_E_INVALID;
  77467. + }
  77468. + if (dwc_otg_urb->qtd == NULL) {
  77469. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  77470. + return -DWC_E_INVALID;
  77471. + }
  77472. + urb_qtd = dwc_otg_urb->qtd;
  77473. + BUG_ON(!urb_qtd);
  77474. + if (urb_qtd->qh == NULL) {
  77475. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  77476. + return -DWC_E_INVALID;
  77477. + }
  77478. +#else
  77479. + urb_qtd = dwc_otg_urb->qtd;
  77480. + BUG_ON(!urb_qtd);
  77481. +#endif
  77482. + qh = urb_qtd->qh;
  77483. + BUG_ON(!qh);
  77484. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  77485. + if (urb_qtd->in_process) {
  77486. + dump_channel_info(hcd, qh);
  77487. + }
  77488. + }
  77489. +#ifdef DEBUG /* integrity checks (Broadcom) */
  77490. + if (hcd->core_if == NULL) {
  77491. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  77492. + return -DWC_E_INVALID;
  77493. + }
  77494. +#endif
  77495. + if (urb_qtd->in_process && qh->channel) {
  77496. + /* The QTD is in process (it has been assigned to a channel). */
  77497. + if (hcd->flags.b.port_connect_status) {
  77498. + int n = qh->channel->hc_num;
  77499. + /*
  77500. + * If still connected (i.e. in host mode), halt the
  77501. + * channel so it can be used for other transfers. If
  77502. + * no longer connected, the host registers can't be
  77503. + * written to halt the channel since the core is in
  77504. + * device mode.
  77505. + */
  77506. + /* In FIQ FSM mode, we need to shut down carefully.
  77507. + * The FIQ may attempt to restart a disabled channel */
  77508. + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
  77509. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  77510. + qh->channel->halt_pending = 1;
  77511. + hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
  77512. + } else {
  77513. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  77514. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  77515. + }
  77516. + }
  77517. + }
  77518. +
  77519. + /*
  77520. + * Free the QTD and clean up the associated QH. Leave the QH in the
  77521. + * schedule if it has any remaining QTDs.
  77522. + */
  77523. +
  77524. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  77525. + "delete %sQueue handler\n",
  77526. + hcd->core_if->dma_desc_enable?"DMA ":"");
  77527. + if (!hcd->core_if->dma_desc_enable) {
  77528. + uint8_t b = urb_qtd->in_process;
  77529. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  77530. + if (b) {
  77531. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  77532. + qh->channel = NULL;
  77533. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  77534. + dwc_otg_hcd_qh_remove(hcd, qh);
  77535. + }
  77536. + } else {
  77537. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  77538. + }
  77539. + return 0;
  77540. +}
  77541. +
  77542. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  77543. + int retry)
  77544. +{
  77545. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  77546. + int retval = 0;
  77547. + dwc_irqflags_t flags;
  77548. +
  77549. + if (retry < 0) {
  77550. + retval = -DWC_E_INVALID;
  77551. + goto done;
  77552. + }
  77553. +
  77554. + if (!qh) {
  77555. + retval = -DWC_E_INVALID;
  77556. + goto done;
  77557. + }
  77558. +
  77559. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  77560. +
  77561. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  77562. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  77563. + retry--;
  77564. + dwc_msleep(5);
  77565. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  77566. + }
  77567. +
  77568. + dwc_otg_hcd_qh_remove(hcd, qh);
  77569. +
  77570. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  77571. + /*
  77572. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  77573. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  77574. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  77575. + * and dwc_otg_hcd_frame_list_alloc().
  77576. + */
  77577. + dwc_otg_hcd_qh_free(hcd, qh);
  77578. +
  77579. +done:
  77580. + return retval;
  77581. +}
  77582. +
  77583. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  77584. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  77585. +{
  77586. + int retval = 0;
  77587. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  77588. + if (!qh)
  77589. + return -DWC_E_INVALID;
  77590. +
  77591. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  77592. + return retval;
  77593. +}
  77594. +#endif
  77595. +
  77596. +/**
  77597. + * HCD Callback structure for handling mode switching.
  77598. + */
  77599. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  77600. + .start = dwc_otg_hcd_start_cb,
  77601. + .stop = dwc_otg_hcd_stop_cb,
  77602. + .disconnect = dwc_otg_hcd_disconnect_cb,
  77603. + .session_start = dwc_otg_hcd_session_start_cb,
  77604. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  77605. +#ifdef CONFIG_USB_DWC_OTG_LPM
  77606. + .sleep = dwc_otg_hcd_sleep_cb,
  77607. +#endif
  77608. + .p = 0,
  77609. +};
  77610. +
  77611. +/**
  77612. + * Reset tasklet function
  77613. + */
  77614. +static void reset_tasklet_func(void *data)
  77615. +{
  77616. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  77617. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  77618. + hprt0_data_t hprt0;
  77619. +
  77620. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  77621. +
  77622. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  77623. + hprt0.b.prtrst = 1;
  77624. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  77625. + dwc_mdelay(60);
  77626. +
  77627. + hprt0.b.prtrst = 0;
  77628. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  77629. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  77630. +}
  77631. +
  77632. +static void completion_tasklet_func(void *ptr)
  77633. +{
  77634. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  77635. + struct urb *urb;
  77636. + urb_tq_entry_t *item;
  77637. + dwc_irqflags_t flags;
  77638. +
  77639. + /* This could just be spin_lock_irq */
  77640. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  77641. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  77642. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  77643. + urb = item->urb;
  77644. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  77645. + urb_tq_entries);
  77646. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  77647. + DWC_FREE(item);
  77648. +
  77649. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  77650. +
  77651. +
  77652. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  77653. + }
  77654. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  77655. + return;
  77656. +}
  77657. +
  77658. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  77659. +{
  77660. + dwc_list_link_t *item;
  77661. + dwc_otg_qh_t *qh;
  77662. + dwc_irqflags_t flags;
  77663. +
  77664. + if (!qh_list->next) {
  77665. + /* The list hasn't been initialized yet. */
  77666. + return;
  77667. + }
  77668. + /*
  77669. + * Hold spinlock here. Not needed in that case if bellow
  77670. + * function is being called from ISR
  77671. + */
  77672. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  77673. + /* Ensure there are no QTDs or URBs left. */
  77674. + kill_urbs_in_qh_list(hcd, qh_list);
  77675. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  77676. +
  77677. + DWC_LIST_FOREACH(item, qh_list) {
  77678. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  77679. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  77680. + }
  77681. +}
  77682. +
  77683. +/**
  77684. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  77685. + * Device during SRP time by host power up.
  77686. + */
  77687. +void dwc_otg_hcd_power_up(void *ptr)
  77688. +{
  77689. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  77690. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  77691. +
  77692. + DWC_PRINTF("%s called\n", __FUNCTION__);
  77693. +
  77694. + if (!core_if->hibernation_suspend) {
  77695. + DWC_PRINTF("Already exited from Hibernation\n");
  77696. + return;
  77697. + }
  77698. +
  77699. + /* Switch on the voltage to the core */
  77700. + gpwrdn.b.pwrdnswtch = 1;
  77701. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  77702. + dwc_udelay(10);
  77703. +
  77704. + /* Reset the core */
  77705. + gpwrdn.d32 = 0;
  77706. + gpwrdn.b.pwrdnrstn = 1;
  77707. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  77708. + dwc_udelay(10);
  77709. +
  77710. + /* Disable power clamps */
  77711. + gpwrdn.d32 = 0;
  77712. + gpwrdn.b.pwrdnclmp = 1;
  77713. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  77714. +
  77715. + /* Remove reset the core signal */
  77716. + gpwrdn.d32 = 0;
  77717. + gpwrdn.b.pwrdnrstn = 1;
  77718. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  77719. + dwc_udelay(10);
  77720. +
  77721. + /* Disable PMU interrupt */
  77722. + gpwrdn.d32 = 0;
  77723. + gpwrdn.b.pmuintsel = 1;
  77724. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  77725. +
  77726. + core_if->hibernation_suspend = 0;
  77727. +
  77728. + /* Disable PMU */
  77729. + gpwrdn.d32 = 0;
  77730. + gpwrdn.b.pmuactv = 1;
  77731. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  77732. + dwc_udelay(10);
  77733. +
  77734. + /* Enable VBUS */
  77735. + gpwrdn.d32 = 0;
  77736. + gpwrdn.b.dis_vbus = 1;
  77737. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  77738. +
  77739. + core_if->op_state = A_HOST;
  77740. + dwc_otg_core_init(core_if);
  77741. + dwc_otg_enable_global_interrupts(core_if);
  77742. + cil_hcd_start(core_if);
  77743. +}
  77744. +
  77745. +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
  77746. +{
  77747. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  77748. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  77749. + int i;
  77750. +
  77751. + st->fsm = FIQ_PASSTHROUGH;
  77752. + st->hcchar_copy.d32 = 0;
  77753. + st->hcsplt_copy.d32 = 0;
  77754. + st->hcint_copy.d32 = 0;
  77755. + st->hcintmsk_copy.d32 = 0;
  77756. + st->hctsiz_copy.d32 = 0;
  77757. + st->hcdma_copy.d32 = 0;
  77758. + st->nr_errors = 0;
  77759. + st->hub_addr = 0;
  77760. + st->port_addr = 0;
  77761. + st->expected_uframe = 0;
  77762. + st->nrpackets = 0;
  77763. + st->dma_info.index = 0;
  77764. + for (i = 0; i < 6; i++)
  77765. + st->dma_info.slot_len[i] = 255;
  77766. + st->hs_isoc_info.index = 0;
  77767. + st->hs_isoc_info.iso_desc = NULL;
  77768. + st->hs_isoc_info.nrframes = 0;
  77769. +
  77770. + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
  77771. +}
  77772. +
  77773. +/**
  77774. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  77775. + * in the struct usb_hcd field.
  77776. + */
  77777. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  77778. +{
  77779. + int i;
  77780. +
  77781. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  77782. +
  77783. + del_timers(dwc_otg_hcd);
  77784. +
  77785. + /* Free memory for QH/QTD lists */
  77786. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  77787. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  77788. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  77789. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  77790. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  77791. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  77792. +
  77793. + /* Free memory for the host channels. */
  77794. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  77795. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  77796. +
  77797. +#ifdef DEBUG
  77798. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  77799. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  77800. + }
  77801. +#endif
  77802. + if (hc != NULL) {
  77803. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  77804. + i, hc);
  77805. + DWC_FREE(hc);
  77806. + }
  77807. + }
  77808. +
  77809. + if (dwc_otg_hcd->core_if->dma_enable) {
  77810. + if (dwc_otg_hcd->status_buf_dma) {
  77811. + DWC_DMA_FREE(DWC_OTG_HCD_STATUS_BUF_SIZE,
  77812. + dwc_otg_hcd->status_buf,
  77813. + dwc_otg_hcd->status_buf_dma);
  77814. + }
  77815. + } else if (dwc_otg_hcd->status_buf != NULL) {
  77816. + DWC_FREE(dwc_otg_hcd->status_buf);
  77817. + }
  77818. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  77819. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  77820. + /* Set core_if's lock pointer to NULL */
  77821. + dwc_otg_hcd->core_if->lock = NULL;
  77822. +
  77823. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  77824. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  77825. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  77826. + DWC_FREE(dwc_otg_hcd->fiq_state);
  77827. +
  77828. +#ifdef DWC_DEV_SRPCAP
  77829. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  77830. + dwc_otg_hcd->core_if->pwron_timer) {
  77831. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  77832. + }
  77833. +#endif
  77834. + DWC_FREE(dwc_otg_hcd);
  77835. +}
  77836. +
  77837. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  77838. +
  77839. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  77840. +{
  77841. + int retval = 0;
  77842. + int num_channels;
  77843. + int i;
  77844. + dwc_hc_t *channel;
  77845. +
  77846. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  77847. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
  77848. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->channel_lock);
  77849. +#else
  77850. + hcd->lock = DWC_SPINLOCK_ALLOC();
  77851. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  77852. +#endif
  77853. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  77854. + hcd, core_if);
  77855. + if (!hcd->lock) {
  77856. + DWC_ERROR("Could not allocate lock for pcd");
  77857. + DWC_FREE(hcd);
  77858. + retval = -DWC_E_NO_MEMORY;
  77859. + goto out;
  77860. + }
  77861. + hcd->core_if = core_if;
  77862. +
  77863. + /* Register the HCD CIL Callbacks */
  77864. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  77865. + &hcd_cil_callbacks, hcd);
  77866. +
  77867. + /* Initialize the non-periodic schedule. */
  77868. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  77869. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  77870. +
  77871. + /* Initialize the periodic schedule. */
  77872. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  77873. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  77874. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  77875. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  77876. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  77877. + /*
  77878. + * Create a host channel descriptor for each host channel implemented
  77879. + * in the controller. Initialize the channel descriptor array.
  77880. + */
  77881. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  77882. + num_channels = hcd->core_if->core_params->host_channels;
  77883. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  77884. + for (i = 0; i < num_channels; i++) {
  77885. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  77886. + if (channel == NULL) {
  77887. + retval = -DWC_E_NO_MEMORY;
  77888. + DWC_ERROR("%s: host channel allocation failed\n",
  77889. + __func__);
  77890. + dwc_otg_hcd_free(hcd);
  77891. + goto out;
  77892. + }
  77893. + channel->hc_num = i;
  77894. + hcd->hc_ptr_array[i] = channel;
  77895. +#ifdef DEBUG
  77896. + hcd->core_if->hc_xfer_timer[i] =
  77897. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  77898. + &hcd->core_if->hc_xfer_info[i]);
  77899. +#endif
  77900. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  77901. + channel);
  77902. + }
  77903. +
  77904. + if (fiq_enable) {
  77905. + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
  77906. + if (!hcd->fiq_state) {
  77907. + retval = -DWC_E_NO_MEMORY;
  77908. + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
  77909. + dwc_otg_hcd_free(hcd);
  77910. + goto out;
  77911. + }
  77912. + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
  77913. +
  77914. + for (i = 0; i < num_channels; i++) {
  77915. + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
  77916. + }
  77917. + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
  77918. +
  77919. + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
  77920. + if (!hcd->fiq_stack) {
  77921. + retval = -DWC_E_NO_MEMORY;
  77922. + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
  77923. + dwc_otg_hcd_free(hcd);
  77924. + goto out;
  77925. + }
  77926. + hcd->fiq_stack->magic1 = 0xDEADBEEF;
  77927. + hcd->fiq_stack->magic2 = 0xD00DFEED;
  77928. + hcd->fiq_state->gintmsk_saved.d32 = ~0;
  77929. + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  77930. +
  77931. + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
  77932. + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
  77933. + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
  77934. + * moderately readable array casts.
  77935. + */
  77936. + hcd->fiq_dmab = DWC_DMA_ALLOC((sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
  77937. + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
  77938. + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
  77939. + sizeof(struct fiq_dma_channel) * num_channels);
  77940. +
  77941. + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
  77942. +
  77943. + /* pointer for debug in fiq_print */
  77944. + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
  77945. + if (fiq_fsm_enable) {
  77946. + int i;
  77947. + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
  77948. + dwc_otg_cleanup_fiq_channel(hcd, i);
  77949. + }
  77950. + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
  77951. + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
  77952. + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
  77953. + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
  77954. + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
  77955. + }
  77956. + }
  77957. +
  77958. + /* Initialize the Connection timeout timer. */
  77959. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  77960. + dwc_otg_hcd_connect_timeout, 0);
  77961. +
  77962. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  77963. + if (microframe_schedule)
  77964. + init_hcd_usecs(hcd);
  77965. +
  77966. + /* Initialize reset tasklet. */
  77967. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  77968. +
  77969. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  77970. + completion_tasklet_func, hcd);
  77971. +#ifdef DWC_DEV_SRPCAP
  77972. + if (hcd->core_if->power_down == 2) {
  77973. + /* Initialize Power on timer for Host power up in case hibernation */
  77974. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  77975. + dwc_otg_hcd_power_up, core_if);
  77976. + }
  77977. +#endif
  77978. +
  77979. + /*
  77980. + * Allocate space for storing data on status transactions. Normally no
  77981. + * data is sent, but this space acts as a bit bucket. This must be
  77982. + * done after usb_add_hcd since that function allocates the DMA buffer
  77983. + * pool.
  77984. + */
  77985. + if (hcd->core_if->dma_enable) {
  77986. + hcd->status_buf =
  77987. + DWC_DMA_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE,
  77988. + &hcd->status_buf_dma);
  77989. + } else {
  77990. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  77991. + }
  77992. + if (!hcd->status_buf) {
  77993. + retval = -DWC_E_NO_MEMORY;
  77994. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  77995. + dwc_otg_hcd_free(hcd);
  77996. + goto out;
  77997. + }
  77998. +
  77999. + hcd->otg_port = 1;
  78000. + hcd->frame_list = NULL;
  78001. + hcd->frame_list_dma = 0;
  78002. + hcd->periodic_qh_count = 0;
  78003. +
  78004. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  78005. +#ifdef FIQ_DEBUG
  78006. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  78007. +#endif
  78008. +
  78009. +out:
  78010. + return retval;
  78011. +}
  78012. +
  78013. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  78014. +{
  78015. + /* Turn off all host-specific interrupts. */
  78016. + dwc_otg_disable_host_interrupts(hcd->core_if);
  78017. +
  78018. + dwc_otg_hcd_free(hcd);
  78019. +}
  78020. +
  78021. +/**
  78022. + * Initializes dynamic portions of the DWC_otg HCD state.
  78023. + */
  78024. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  78025. +{
  78026. + int num_channels;
  78027. + int i;
  78028. + dwc_hc_t *channel;
  78029. + dwc_hc_t *channel_tmp;
  78030. +
  78031. + hcd->flags.d32 = 0;
  78032. +
  78033. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  78034. + if (!microframe_schedule) {
  78035. + hcd->non_periodic_channels = 0;
  78036. + hcd->periodic_channels = 0;
  78037. + } else {
  78038. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  78039. + }
  78040. + /*
  78041. + * Put all channels in the free channel list and clean up channel
  78042. + * states.
  78043. + */
  78044. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  78045. + &hcd->free_hc_list, hc_list_entry) {
  78046. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  78047. + }
  78048. +
  78049. + num_channels = hcd->core_if->core_params->host_channels;
  78050. + for (i = 0; i < num_channels; i++) {
  78051. + channel = hcd->hc_ptr_array[i];
  78052. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  78053. + hc_list_entry);
  78054. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  78055. + }
  78056. +
  78057. + /* Initialize the DWC core for host mode operation. */
  78058. + dwc_otg_core_host_init(hcd->core_if);
  78059. +
  78060. + /* Set core_if's lock pointer to the hcd->lock */
  78061. + hcd->core_if->lock = hcd->lock;
  78062. +}
  78063. +
  78064. +/**
  78065. + * Assigns transactions from a QTD to a free host channel and initializes the
  78066. + * host channel to perform the transactions. The host channel is removed from
  78067. + * the free list.
  78068. + *
  78069. + * @param hcd The HCD state structure.
  78070. + * @param qh Transactions from the first QTD for this QH are selected and
  78071. + * assigned to a free host channel.
  78072. + */
  78073. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  78074. +{
  78075. + dwc_hc_t *hc;
  78076. + dwc_otg_qtd_t *qtd;
  78077. + dwc_otg_hcd_urb_t *urb;
  78078. + void* ptr = NULL;
  78079. +
  78080. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  78081. +
  78082. + urb = qtd->urb;
  78083. +
  78084. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  78085. +
  78086. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  78087. + urb->actual_length = urb->length;
  78088. +
  78089. +
  78090. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  78091. +
  78092. + /* Remove the host channel from the free list. */
  78093. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  78094. +
  78095. + qh->channel = hc;
  78096. +
  78097. + qtd->in_process = 1;
  78098. +
  78099. + /*
  78100. + * Use usb_pipedevice to determine device address. This address is
  78101. + * 0 before the SET_ADDRESS command and the correct address afterward.
  78102. + */
  78103. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  78104. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  78105. + hc->speed = qh->dev_speed;
  78106. + hc->max_packet = dwc_max_packet(qh->maxp);
  78107. +
  78108. + hc->xfer_started = 0;
  78109. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  78110. + hc->error_state = (qtd->error_count > 0);
  78111. + hc->halt_on_queue = 0;
  78112. + hc->halt_pending = 0;
  78113. + hc->requests = 0;
  78114. +
  78115. + /*
  78116. + * The following values may be modified in the transfer type section
  78117. + * below. The xfer_len value may be reduced when the transfer is
  78118. + * started to accommodate the max widths of the XferSize and PktCnt
  78119. + * fields in the HCTSIZn register.
  78120. + */
  78121. +
  78122. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  78123. + if (hc->ep_is_in) {
  78124. + hc->do_ping = 0;
  78125. + } else {
  78126. + hc->do_ping = qh->ping_state;
  78127. + }
  78128. +
  78129. + hc->data_pid_start = qh->data_toggle;
  78130. + hc->multi_count = 1;
  78131. +
  78132. + if (hcd->core_if->dma_enable) {
  78133. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  78134. +
  78135. + /* For non-dword aligned case */
  78136. + if (((unsigned long)hc->xfer_buff & 0x3)
  78137. + && !hcd->core_if->dma_desc_enable) {
  78138. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  78139. + }
  78140. + } else {
  78141. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  78142. + }
  78143. + hc->xfer_len = urb->length - urb->actual_length;
  78144. + hc->xfer_count = 0;
  78145. +
  78146. + /*
  78147. + * Set the split attributes
  78148. + */
  78149. + hc->do_split = 0;
  78150. + if (qh->do_split) {
  78151. + uint32_t hub_addr, port_addr;
  78152. + hc->do_split = 1;
  78153. + hc->xact_pos = qtd->isoc_split_pos;
  78154. + /* We don't need to do complete splits anymore */
  78155. +// if(fiq_fsm_enable)
  78156. + if (0)
  78157. + hc->complete_split = qtd->complete_split = 0;
  78158. + else
  78159. + hc->complete_split = qtd->complete_split;
  78160. +
  78161. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  78162. + hc->hub_addr = (uint8_t) hub_addr;
  78163. + hc->port_addr = (uint8_t) port_addr;
  78164. + }
  78165. +
  78166. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  78167. + case UE_CONTROL:
  78168. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  78169. + switch (qtd->control_phase) {
  78170. + case DWC_OTG_CONTROL_SETUP:
  78171. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  78172. + hc->do_ping = 0;
  78173. + hc->ep_is_in = 0;
  78174. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  78175. + if (hcd->core_if->dma_enable) {
  78176. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  78177. + } else {
  78178. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  78179. + }
  78180. + hc->xfer_len = 8;
  78181. + ptr = NULL;
  78182. + break;
  78183. + case DWC_OTG_CONTROL_DATA:
  78184. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  78185. + hc->data_pid_start = qtd->data_toggle;
  78186. + break;
  78187. + case DWC_OTG_CONTROL_STATUS:
  78188. + /*
  78189. + * Direction is opposite of data direction or IN if no
  78190. + * data.
  78191. + */
  78192. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  78193. + if (urb->length == 0) {
  78194. + hc->ep_is_in = 1;
  78195. + } else {
  78196. + hc->ep_is_in =
  78197. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  78198. + }
  78199. + if (hc->ep_is_in) {
  78200. + hc->do_ping = 0;
  78201. + }
  78202. +
  78203. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  78204. +
  78205. + hc->xfer_len = 0;
  78206. + if (hcd->core_if->dma_enable) {
  78207. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  78208. + } else {
  78209. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  78210. + }
  78211. + ptr = NULL;
  78212. + break;
  78213. + }
  78214. + break;
  78215. + case UE_BULK:
  78216. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  78217. + break;
  78218. + case UE_INTERRUPT:
  78219. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  78220. + break;
  78221. + case UE_ISOCHRONOUS:
  78222. + {
  78223. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  78224. +
  78225. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  78226. +
  78227. + if (hcd->core_if->dma_desc_enable)
  78228. + break;
  78229. +
  78230. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  78231. +
  78232. + frame_desc->status = 0;
  78233. +
  78234. + if (hcd->core_if->dma_enable) {
  78235. + hc->xfer_buff = (uint8_t *) urb->dma;
  78236. + } else {
  78237. + hc->xfer_buff = (uint8_t *) urb->buf;
  78238. + }
  78239. + hc->xfer_buff +=
  78240. + frame_desc->offset + qtd->isoc_split_offset;
  78241. + hc->xfer_len =
  78242. + frame_desc->length - qtd->isoc_split_offset;
  78243. +
  78244. + /* For non-dword aligned buffers */
  78245. + if (((unsigned long)hc->xfer_buff & 0x3)
  78246. + && hcd->core_if->dma_enable) {
  78247. + ptr =
  78248. + (uint8_t *) urb->buf + frame_desc->offset +
  78249. + qtd->isoc_split_offset;
  78250. + } else
  78251. + ptr = NULL;
  78252. +
  78253. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  78254. + if (hc->xfer_len <= 188) {
  78255. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  78256. + } else {
  78257. + hc->xact_pos =
  78258. + DWC_HCSPLIT_XACTPOS_BEGIN;
  78259. + }
  78260. + }
  78261. + }
  78262. + break;
  78263. + }
  78264. + /* non DWORD-aligned buffer case */
  78265. + if (ptr) {
  78266. + uint32_t buf_size;
  78267. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  78268. + buf_size = hcd->core_if->core_params->max_transfer_size;
  78269. + } else {
  78270. + buf_size = 4096;
  78271. + }
  78272. + if (!qh->dw_align_buf) {
  78273. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(buf_size,
  78274. + &qh->dw_align_buf_dma);
  78275. + if (!qh->dw_align_buf) {
  78276. + DWC_ERROR
  78277. + ("%s: Failed to allocate memory to handle "
  78278. + "non-dword aligned buffer case\n",
  78279. + __func__);
  78280. + return;
  78281. + }
  78282. + }
  78283. + if (!hc->ep_is_in) {
  78284. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  78285. + }
  78286. + hc->align_buff = qh->dw_align_buf_dma;
  78287. + } else {
  78288. + hc->align_buff = 0;
  78289. + }
  78290. +
  78291. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  78292. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  78293. + /*
  78294. + * This value may be modified when the transfer is started to
  78295. + * reflect the actual transfer length.
  78296. + */
  78297. + hc->multi_count = dwc_hb_mult(qh->maxp);
  78298. + }
  78299. +
  78300. + if (hcd->core_if->dma_desc_enable)
  78301. + hc->desc_list_addr = qh->desc_list_dma;
  78302. +
  78303. + dwc_otg_hc_init(hcd->core_if, hc);
  78304. + hc->qh = qh;
  78305. +}
  78306. +
  78307. +
  78308. +/**
  78309. + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
  78310. + * @qh: pointer to the endpoint's queue head
  78311. + *
  78312. + * Transaction start/end control flow is grafted onto the existing dwc_otg
  78313. + * mechanisms, to avoid spaghettifying the functions more than they already are.
  78314. + * This function's eligibility check is altered by debug parameter.
  78315. + *
  78316. + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
  78317. + */
  78318. +
  78319. +int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh)
  78320. +{
  78321. + if (qh->do_split) {
  78322. + switch (qh->ep_type) {
  78323. + case UE_CONTROL:
  78324. + case UE_BULK:
  78325. + if (fiq_fsm_mask & (1 << 0))
  78326. + return 1;
  78327. + break;
  78328. + case UE_INTERRUPT:
  78329. + case UE_ISOCHRONOUS:
  78330. + if (fiq_fsm_mask & (1 << 1))
  78331. + return 1;
  78332. + break;
  78333. + default:
  78334. + break;
  78335. + }
  78336. + } else if (qh->ep_type == UE_ISOCHRONOUS) {
  78337. + if (fiq_fsm_mask & (1 << 2)) {
  78338. + /* HS ISOCH support. We test for compatibility:
  78339. + * - DWORD aligned buffers
  78340. + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
  78341. + * If yes, then the fsm enqueue function will handle the state machine setup.
  78342. + */
  78343. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  78344. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  78345. + struct dwc_otg_hcd_iso_packet_desc (*iso_descs)[0] = &urb->iso_descs;
  78346. + int nr_iso_frames = urb->packet_count;
  78347. + int i;
  78348. + uint32_t ptr;
  78349. +
  78350. + if (nr_iso_frames < 2)
  78351. + return 0;
  78352. + for (i = 0; i < nr_iso_frames; i++) {
  78353. + ptr = urb->dma + iso_descs[i]->offset;
  78354. + if (ptr & 0x3) {
  78355. + printk_ratelimited("%s: Non-Dword aligned isochronous frame offset."
  78356. + " Cannot queue FIQ-accelerated transfer to device %d endpoint %d\n",
  78357. + __FUNCTION__, qh->channel->dev_addr, qh->channel->ep_num);
  78358. + return 0;
  78359. + }
  78360. + }
  78361. + return 1;
  78362. + }
  78363. + }
  78364. + return 0;
  78365. +}
  78366. +
  78367. +/**
  78368. + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
  78369. + * @hcd: Pointer to the dwc_otg_hcd struct
  78370. + * @qh: Pointer to the endpoint's queue head
  78371. + *
  78372. + * Periodic split transactions are transmitted modulo 188 bytes.
  78373. + * This necessitates slicing data up into buckets for isochronous out
  78374. + * and fixing up the DMA address for all IN transfers.
  78375. + *
  78376. + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
  78377. + * HC buffer has been used.
  78378. + */
  78379. +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
  78380. + {
  78381. + int frame_length, i = 0;
  78382. + uint8_t *ptr = NULL;
  78383. + dwc_hc_t *hc = qh->channel;
  78384. + struct fiq_dma_blob *blob;
  78385. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  78386. +
  78387. + for (i = 0; i < 6; i++) {
  78388. + st->dma_info.slot_len[i] = 255;
  78389. + }
  78390. + st->dma_info.index = 0;
  78391. + i = 0;
  78392. + if (hc->ep_is_in) {
  78393. + /*
  78394. + * Set dma_regs to bounce buffer. FIQ will update the
  78395. + * state depending on transaction progress.
  78396. + */
  78397. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  78398. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  78399. + /* Calculate the max number of CSPLITS such that the FIQ can time out
  78400. + * a transaction if it fails.
  78401. + */
  78402. + frame_length = st->hcchar_copy.b.mps;
  78403. + do {
  78404. + i++;
  78405. + frame_length -= 188;
  78406. + } while (frame_length >= 0);
  78407. + st->nrpackets = i;
  78408. + return 1;
  78409. + } else {
  78410. + if (qh->ep_type == UE_ISOCHRONOUS) {
  78411. +
  78412. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  78413. +
  78414. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  78415. + frame_length = frame_desc->length;
  78416. +
  78417. + /* Virtual address for bounce buffers */
  78418. + blob = hcd->fiq_dmab;
  78419. +
  78420. + ptr = qtd->urb->buf + frame_desc->offset;
  78421. + if (frame_length == 0) {
  78422. + /*
  78423. + * for isochronous transactions, we must still transmit a packet
  78424. + * even if the length is zero.
  78425. + */
  78426. + st->dma_info.slot_len[0] = 0;
  78427. + st->nrpackets = 1;
  78428. + } else {
  78429. + do {
  78430. + if (frame_length <= 188) {
  78431. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
  78432. + st->dma_info.slot_len[i] = frame_length;
  78433. + ptr += frame_length;
  78434. + } else {
  78435. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
  78436. + st->dma_info.slot_len[i] = 188;
  78437. + ptr += 188;
  78438. + }
  78439. + i++;
  78440. + frame_length -= 188;
  78441. + } while (frame_length > 0);
  78442. + st->nrpackets = i;
  78443. + }
  78444. + ptr = qtd->urb->buf + frame_desc->offset;
  78445. + /* Point the HC at the DMA address of the bounce buffers */
  78446. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  78447. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  78448. +
  78449. + /* fixup xfersize to the actual packet size */
  78450. + st->hctsiz_copy.b.pid = 0;
  78451. + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
  78452. + return 1;
  78453. + } else {
  78454. + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
  78455. + return 0;
  78456. + }
  78457. + }
  78458. +}
  78459. +
  78460. +/*
  78461. + * Pushing a periodic request into the queue near the EOF1 point
  78462. + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
  78463. + * Usually, the request goes out on the bus causing a transfer but
  78464. + * the core does not transfer the data to memory.
  78465. + * This guard interval (in number of 60MHz clocks) is required which
  78466. + * must cater for CPU latency between reading the value and enabling
  78467. + * the channel.
  78468. + */
  78469. +#define PERIODIC_FRREM_BACKOFF 1000
  78470. +
  78471. +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  78472. +{
  78473. + dwc_hc_t *hc = qh->channel;
  78474. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  78475. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  78476. + int frame;
  78477. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  78478. + int xfer_len, nrpackets;
  78479. + hcdma_data_t hcdma;
  78480. + hfnum_data_t hfnum;
  78481. +
  78482. + if (st->fsm != FIQ_PASSTHROUGH)
  78483. + return 0;
  78484. +
  78485. + st->nr_errors = 0;
  78486. +
  78487. + st->hcchar_copy.d32 = 0;
  78488. + st->hcchar_copy.b.mps = hc->max_packet;
  78489. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  78490. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  78491. + st->hcchar_copy.b.epnum = hc->ep_num;
  78492. + st->hcchar_copy.b.eptype = hc->ep_type;
  78493. +
  78494. + st->hcintmsk_copy.b.chhltd = 1;
  78495. +
  78496. + frame = dwc_otg_hcd_get_frame_number(hcd);
  78497. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  78498. +
  78499. + st->hcchar_copy.b.lspddev = 0;
  78500. + /* Enable the channel later as a final register write. */
  78501. +
  78502. + st->hcsplt_copy.d32 = 0;
  78503. +
  78504. + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
  78505. + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
  78506. + /* grab the next DMA address offset from the array */
  78507. + st->hcdma_copy.d32 = qtd->urb->dma;
  78508. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
  78509. +
  78510. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  78511. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  78512. + * this is always set to the maximum size of the endpoint. */
  78513. + xfer_len = st->hs_isoc_info.iso_desc[0].length;
  78514. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  78515. + if (nrpackets == 0)
  78516. + nrpackets = 1;
  78517. + st->hcchar_copy.b.multicnt = nrpackets;
  78518. + st->hctsiz_copy.b.pktcnt = nrpackets;
  78519. +
  78520. + /* Initial PID also needs to be set */
  78521. + if (st->hcchar_copy.b.epdir == 0) {
  78522. + st->hctsiz_copy.b.xfersize = xfer_len;
  78523. + switch (st->hcchar_copy.b.multicnt) {
  78524. + case 1:
  78525. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  78526. + break;
  78527. + case 2:
  78528. + case 3:
  78529. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  78530. + break;
  78531. + }
  78532. +
  78533. + } else {
  78534. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  78535. + switch (st->hcchar_copy.b.multicnt) {
  78536. + case 1:
  78537. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  78538. + break;
  78539. + case 2:
  78540. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  78541. + break;
  78542. + case 3:
  78543. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  78544. + break;
  78545. + }
  78546. + }
  78547. +
  78548. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
  78549. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
  78550. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  78551. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  78552. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  78553. + local_fiq_disable();
  78554. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  78555. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  78556. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  78557. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  78558. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  78559. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  78560. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  78561. + * split transaction is queued very close to EOF.
  78562. + */
  78563. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  78564. + } else {
  78565. + st->fsm = FIQ_HS_ISOC_TURBO;
  78566. + st->hcchar_copy.b.chen = 1;
  78567. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  78568. + }
  78569. + mb();
  78570. + st->hcchar_copy.b.chen = 0;
  78571. + local_fiq_enable();
  78572. + return 0;
  78573. +}
  78574. +
  78575. +
  78576. +/**
  78577. + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
  78578. + * @hcd: Pointer to the dwc_otg_hcd struct
  78579. + * @qh: Pointer to the endpoint's queue head
  78580. + *
  78581. + * This overrides the dwc_otg driver's normal method of queueing a transaction.
  78582. + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
  78583. + * for the nominated host channel.
  78584. + *
  78585. + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
  78586. + * start is possible. If not, then the FIQ is left to start the transfer.
  78587. + */
  78588. +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  78589. +{
  78590. + int start_immediate = 1, i;
  78591. + hfnum_data_t hfnum;
  78592. + dwc_hc_t *hc = qh->channel;
  78593. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  78594. + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
  78595. + int hub_addr, port_addr, frame, uframe;
  78596. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  78597. +
  78598. + if (st->fsm != FIQ_PASSTHROUGH)
  78599. + return 0;
  78600. + st->nr_errors = 0;
  78601. +
  78602. + st->hcchar_copy.d32 = 0;
  78603. + st->hcchar_copy.b.mps = hc->max_packet;
  78604. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  78605. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  78606. + st->hcchar_copy.b.epnum = hc->ep_num;
  78607. + st->hcchar_copy.b.eptype = hc->ep_type;
  78608. + if (hc->ep_type & 0x1) {
  78609. + if (hc->ep_is_in)
  78610. + st->hcchar_copy.b.multicnt = 3;
  78611. + else
  78612. + /* Docs say set this to 1, but driver sets to 0! */
  78613. + st->hcchar_copy.b.multicnt = 0;
  78614. + } else {
  78615. + st->hcchar_copy.b.multicnt = 1;
  78616. + st->hcchar_copy.b.oddfrm = 0;
  78617. + }
  78618. + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
  78619. + /* Enable the channel later as a final register write. */
  78620. +
  78621. + st->hcsplt_copy.d32 = 0;
  78622. + if(qh->do_split) {
  78623. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  78624. + st->hcsplt_copy.b.compsplt = 0;
  78625. + st->hcsplt_copy.b.spltena = 1;
  78626. + // XACTPOS is for isoc-out only but needs initialising anyway.
  78627. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
  78628. + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
  78629. + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
  78630. + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
  78631. + * will update as necessary.
  78632. + */
  78633. + if (hc->xfer_len > 188) {
  78634. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
  78635. + }
  78636. + }
  78637. + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
  78638. + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
  78639. + st->hub_addr = hub_addr;
  78640. + st->port_addr = port_addr;
  78641. + }
  78642. +
  78643. + st->hctsiz_copy.d32 = 0;
  78644. + st->hctsiz_copy.b.dopng = 0;
  78645. + st->hctsiz_copy.b.pid = hc->data_pid_start;
  78646. +
  78647. + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  78648. + hc->xfer_len = hc->max_packet;
  78649. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  78650. + hc->xfer_len = 188;
  78651. + }
  78652. + st->hctsiz_copy.b.xfersize = hc->xfer_len;
  78653. +
  78654. + st->hctsiz_copy.b.pktcnt = 1;
  78655. +
  78656. + if (hc->ep_type & 0x1) {
  78657. + /*
  78658. + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
  78659. + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
  78660. + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
  78661. + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
  78662. + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
  78663. + * must not touch internal driver state.
  78664. + */
  78665. + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
  78666. + if (hc->align_buff) {
  78667. + st->hcdma_copy.d32 = hc->align_buff;
  78668. + } else {
  78669. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  78670. + }
  78671. + }
  78672. + } else {
  78673. + if (hc->align_buff) {
  78674. + st->hcdma_copy.d32 = hc->align_buff;
  78675. + } else {
  78676. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  78677. + }
  78678. + }
  78679. + /* The FIQ depends upon no other interrupts being enabled except channel halt.
  78680. + * Fixup channel interrupt mask. */
  78681. + st->hcintmsk_copy.d32 = 0;
  78682. + st->hcintmsk_copy.b.chhltd = 1;
  78683. + st->hcintmsk_copy.b.ahberr = 1;
  78684. +
  78685. + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
  78686. + * as Control puts the transfer into the non-periodic request queue and the
  78687. + * non-periodic handler in the hub. Makes things lots easier.
  78688. + */
  78689. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
  78690. + st->hcchar_copy.b.multicnt = 0;
  78691. + st->hcchar_copy.b.oddfrm = 0;
  78692. + st->hcchar_copy.b.eptype = UE_CONTROL;
  78693. + if (hc->align_buff) {
  78694. + st->hcdma_copy.d32 = hc->align_buff;
  78695. + } else {
  78696. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  78697. + }
  78698. + }
  78699. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  78700. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  78701. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  78702. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  78703. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  78704. +
  78705. + local_fiq_disable();
  78706. + mb();
  78707. +
  78708. + if (hc->ep_type & 0x1) {
  78709. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  78710. + frame = (hfnum.b.frnum & ~0x7) >> 3;
  78711. + uframe = hfnum.b.frnum & 0x7;
  78712. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  78713. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  78714. + * split transaction is queued very close to EOF.
  78715. + */
  78716. + start_immediate = 0;
  78717. + } else if (uframe == 5) {
  78718. + start_immediate = 0;
  78719. + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
  78720. + start_immediate = 0;
  78721. + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
  78722. + start_immediate = 0;
  78723. + } else {
  78724. + /* Search through all host channels to determine if a transaction
  78725. + * is currently in progress */
  78726. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  78727. + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
  78728. + continue;
  78729. + switch (hcd->fiq_state->channel[i].fsm) {
  78730. + /* TT is reserved for channels that are in the middle of a periodic
  78731. + * split transaction.
  78732. + */
  78733. + case FIQ_PER_SSPLIT_STARTED:
  78734. + case FIQ_PER_CSPLIT_WAIT:
  78735. + case FIQ_PER_CSPLIT_NYET1:
  78736. + case FIQ_PER_CSPLIT_POLL:
  78737. + case FIQ_PER_ISO_OUT_ACTIVE:
  78738. + case FIQ_PER_ISO_OUT_LAST:
  78739. + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
  78740. + hcd->fiq_state->channel[i].port_addr == port_addr) {
  78741. + start_immediate = 0;
  78742. + }
  78743. + break;
  78744. + default:
  78745. + break;
  78746. + }
  78747. + if (!start_immediate)
  78748. + break;
  78749. + }
  78750. + }
  78751. + }
  78752. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
  78753. + start_immediate = 1;
  78754. +
  78755. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
  78756. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
  78757. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
  78758. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  78759. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  78760. + switch (hc->ep_type) {
  78761. + case UE_CONTROL:
  78762. + case UE_BULK:
  78763. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  78764. + break;
  78765. + case UE_ISOCHRONOUS:
  78766. + if (hc->ep_is_in) {
  78767. + if (start_immediate) {
  78768. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  78769. + } else {
  78770. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  78771. + }
  78772. + } else {
  78773. + if (start_immediate) {
  78774. + /* Single-isoc OUT packets don't require FIQ involvement */
  78775. + if (st->nrpackets == 1) {
  78776. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  78777. + } else {
  78778. + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
  78779. + }
  78780. + } else {
  78781. + st->fsm = FIQ_PER_ISO_OUT_PENDING;
  78782. + }
  78783. + }
  78784. + break;
  78785. + case UE_INTERRUPT:
  78786. + if (fiq_fsm_mask & 0x8) {
  78787. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  78788. + } else if (start_immediate) {
  78789. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  78790. + } else {
  78791. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  78792. + }
  78793. + default:
  78794. + break;
  78795. + }
  78796. + if (start_immediate) {
  78797. + /* Set the oddfrm bit as close as possible to actual queueing */
  78798. + frame = dwc_otg_hcd_get_frame_number(hcd);
  78799. + st->expected_uframe = (frame + 1) & 0x3FFF;
  78800. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  78801. + st->hcchar_copy.b.chen = 1;
  78802. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  78803. + }
  78804. + mb();
  78805. + local_fiq_enable();
  78806. + return 0;
  78807. +}
  78808. +
  78809. +
  78810. +/**
  78811. + * This function selects transactions from the HCD transfer schedule and
  78812. + * assigns them to available host channels. It is called from HCD interrupt
  78813. + * handler functions.
  78814. + *
  78815. + * @param hcd The HCD state structure.
  78816. + *
  78817. + * @return The types of new transactions that were assigned to host channels.
  78818. + */
  78819. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  78820. +{
  78821. + dwc_list_link_t *qh_ptr;
  78822. + dwc_otg_qh_t *qh;
  78823. + int num_channels;
  78824. + dwc_irqflags_t flags;
  78825. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  78826. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  78827. +
  78828. +#ifdef DEBUG_HOST_CHANNELS
  78829. + last_sel_trans_num_per_scheduled = 0;
  78830. + last_sel_trans_num_nonper_scheduled = 0;
  78831. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  78832. +#endif /* DEBUG_HOST_CHANNELS */
  78833. +
  78834. + /* Process entries in the periodic ready list. */
  78835. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  78836. +
  78837. + while (qh_ptr != &hcd->periodic_sched_ready &&
  78838. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  78839. +
  78840. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  78841. +
  78842. + if (microframe_schedule) {
  78843. + // Make sure we leave one channel for non periodic transactions.
  78844. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  78845. + if (hcd->available_host_channels <= 1) {
  78846. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  78847. + break;
  78848. + }
  78849. + hcd->available_host_channels--;
  78850. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  78851. +#ifdef DEBUG_HOST_CHANNELS
  78852. + last_sel_trans_num_per_scheduled++;
  78853. +#endif /* DEBUG_HOST_CHANNELS */
  78854. + }
  78855. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  78856. + assign_and_init_hc(hcd, qh);
  78857. +
  78858. + /*
  78859. + * Move the QH from the periodic ready schedule to the
  78860. + * periodic assigned schedule.
  78861. + */
  78862. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  78863. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  78864. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  78865. + &qh->qh_list_entry);
  78866. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  78867. + }
  78868. +
  78869. + /*
  78870. + * Process entries in the inactive portion of the non-periodic
  78871. + * schedule. Some free host channels may not be used if they are
  78872. + * reserved for periodic transfers.
  78873. + */
  78874. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  78875. + num_channels = hcd->core_if->core_params->host_channels;
  78876. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  78877. + (microframe_schedule || hcd->non_periodic_channels <
  78878. + num_channels - hcd->periodic_channels) &&
  78879. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  78880. +
  78881. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  78882. + /*
  78883. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  78884. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  78885. + * cheeky devices that just hold off using NAKs
  78886. + */
  78887. + if (nak_holdoff && qh->do_split) {
  78888. + if (qh->nak_frame != 0xffff) {
  78889. + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
  78890. + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
  78891. + if (dwc_frame_num_le(frame, next_frame)) {
  78892. + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
  78893. + hcd->fiq_state->next_sched_frame = next_frame;
  78894. + }
  78895. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  78896. + continue;
  78897. + } else {
  78898. + qh->nak_frame = 0xFFFF;
  78899. + }
  78900. + }
  78901. + }
  78902. +
  78903. + if (microframe_schedule) {
  78904. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  78905. + if (hcd->available_host_channels < 1) {
  78906. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  78907. + break;
  78908. + }
  78909. + hcd->available_host_channels--;
  78910. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  78911. +#ifdef DEBUG_HOST_CHANNELS
  78912. + last_sel_trans_num_nonper_scheduled++;
  78913. +#endif /* DEBUG_HOST_CHANNELS */
  78914. + }
  78915. +
  78916. + assign_and_init_hc(hcd, qh);
  78917. +
  78918. + /*
  78919. + * Move the QH from the non-periodic inactive schedule to the
  78920. + * non-periodic active schedule.
  78921. + */
  78922. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  78923. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  78924. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  78925. + &qh->qh_list_entry);
  78926. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  78927. +
  78928. +
  78929. + if (!microframe_schedule)
  78930. + hcd->non_periodic_channels++;
  78931. + }
  78932. + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
  78933. + * stop the FIQ from kicking us. We could potentially still have elements here if we
  78934. + * ran out of host channels.
  78935. + */
  78936. + if (fiq_enable) {
  78937. + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
  78938. + hcd->fiq_state->kick_np_queues = 0;
  78939. + } else {
  78940. + /* For each entry remaining in the NP inactive queue,
  78941. + * if this a NAK'd retransmit then don't set the kick flag.
  78942. + */
  78943. + if(nak_holdoff) {
  78944. + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
  78945. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  78946. + if (qh->nak_frame == 0xFFFF) {
  78947. + hcd->fiq_state->kick_np_queues = 1;
  78948. + }
  78949. + }
  78950. + }
  78951. + }
  78952. + }
  78953. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  78954. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  78955. +
  78956. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  78957. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  78958. +
  78959. +
  78960. +#ifdef DEBUG_HOST_CHANNELS
  78961. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  78962. +#endif /* DEBUG_HOST_CHANNELS */
  78963. + return ret_val;
  78964. +}
  78965. +
  78966. +/**
  78967. + * Attempts to queue a single transaction request for a host channel
  78968. + * associated with either a periodic or non-periodic transfer. This function
  78969. + * assumes that there is space available in the appropriate request queue. For
  78970. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  78971. + * is available in the appropriate Tx FIFO.
  78972. + *
  78973. + * @param hcd The HCD state structure.
  78974. + * @param hc Host channel descriptor associated with either a periodic or
  78975. + * non-periodic transfer.
  78976. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  78977. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  78978. + * transfers.
  78979. + *
  78980. + * @return 1 if a request is queued and more requests may be needed to
  78981. + * complete the transfer, 0 if no more requests are required for this
  78982. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  78983. + */
  78984. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  78985. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  78986. +{
  78987. + int retval;
  78988. +
  78989. + if (hcd->core_if->dma_enable) {
  78990. + if (hcd->core_if->dma_desc_enable) {
  78991. + if (!hc->xfer_started
  78992. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  78993. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  78994. + hc->qh->ping_state = 0;
  78995. + }
  78996. + } else if (!hc->xfer_started) {
  78997. + if (fiq_fsm_enable && hc->error_state) {
  78998. + hcd->fiq_state->channel[hc->hc_num].nr_errors =
  78999. + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
  79000. + hcd->fiq_state->channel[hc->hc_num].fsm =
  79001. + FIQ_PASSTHROUGH_ERRORSTATE;
  79002. + }
  79003. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  79004. + hc->qh->ping_state = 0;
  79005. + }
  79006. + retval = 0;
  79007. + } else if (hc->halt_pending) {
  79008. + /* Don't queue a request if the channel has been halted. */
  79009. + retval = 0;
  79010. + } else if (hc->halt_on_queue) {
  79011. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  79012. + retval = 0;
  79013. + } else if (hc->do_ping) {
  79014. + if (!hc->xfer_started) {
  79015. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  79016. + }
  79017. + retval = 0;
  79018. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  79019. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  79020. + if (!hc->xfer_started) {
  79021. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  79022. + retval = 1;
  79023. + } else {
  79024. + retval =
  79025. + dwc_otg_hc_continue_transfer(hcd->core_if,
  79026. + hc);
  79027. + }
  79028. + } else {
  79029. + retval = -1;
  79030. + }
  79031. + } else {
  79032. + if (!hc->xfer_started) {
  79033. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  79034. + retval = 1;
  79035. + } else {
  79036. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  79037. + }
  79038. + }
  79039. +
  79040. + return retval;
  79041. +}
  79042. +
  79043. +/**
  79044. + * Processes periodic channels for the next frame and queues transactions for
  79045. + * these channels to the DWC_otg controller. After queueing transactions, the
  79046. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  79047. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  79048. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  79049. + */
  79050. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  79051. +{
  79052. + hptxsts_data_t tx_status;
  79053. + dwc_list_link_t *qh_ptr;
  79054. + dwc_otg_qh_t *qh;
  79055. + int status = 0;
  79056. + int no_queue_space = 0;
  79057. + int no_fifo_space = 0;
  79058. +
  79059. + dwc_otg_host_global_regs_t *host_regs;
  79060. + host_regs = hcd->core_if->host_if->host_global_regs;
  79061. +
  79062. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  79063. +#ifdef DEBUG
  79064. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  79065. + DWC_DEBUGPL(DBG_HCDV,
  79066. + " P Tx Req Queue Space Avail (before queue): %d\n",
  79067. + tx_status.b.ptxqspcavail);
  79068. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  79069. + tx_status.b.ptxfspcavail);
  79070. +#endif
  79071. +
  79072. + qh_ptr = hcd->periodic_sched_assigned.next;
  79073. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  79074. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  79075. + if (tx_status.b.ptxqspcavail == 0) {
  79076. + no_queue_space = 1;
  79077. + break;
  79078. + }
  79079. +
  79080. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  79081. +
  79082. + // Do not send a split start transaction any later than frame .6
  79083. + // Note, we have to schedule a periodic in .5 to make it go in .6
  79084. + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  79085. + {
  79086. + qh_ptr = qh_ptr->next;
  79087. + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  79088. + continue;
  79089. + }
  79090. +
  79091. + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  79092. + if (qh->do_split)
  79093. + fiq_fsm_queue_split_transaction(hcd, qh);
  79094. + else
  79095. + fiq_fsm_queue_isoc_transaction(hcd, qh);
  79096. + } else {
  79097. +
  79098. + /*
  79099. + * Set a flag if we're queueing high-bandwidth in slave mode.
  79100. + * The flag prevents any halts to get into the request queue in
  79101. + * the middle of multiple high-bandwidth packets getting queued.
  79102. + */
  79103. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  79104. + hcd->core_if->queuing_high_bandwidth = 1;
  79105. + }
  79106. + status = queue_transaction(hcd, qh->channel,
  79107. + tx_status.b.ptxfspcavail);
  79108. + if (status < 0) {
  79109. + no_fifo_space = 1;
  79110. + break;
  79111. + }
  79112. + }
  79113. +
  79114. + /*
  79115. + * In Slave mode, stay on the current transfer until there is
  79116. + * nothing more to do or the high-bandwidth request count is
  79117. + * reached. In DMA mode, only need to queue one request. The
  79118. + * controller automatically handles multiple packets for
  79119. + * high-bandwidth transfers.
  79120. + */
  79121. + if (hcd->core_if->dma_enable || status == 0 ||
  79122. + qh->channel->requests == qh->channel->multi_count) {
  79123. + qh_ptr = qh_ptr->next;
  79124. + /*
  79125. + * Move the QH from the periodic assigned schedule to
  79126. + * the periodic queued schedule.
  79127. + */
  79128. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  79129. + &qh->qh_list_entry);
  79130. +
  79131. + /* done queuing high bandwidth */
  79132. + hcd->core_if->queuing_high_bandwidth = 0;
  79133. + }
  79134. + }
  79135. +
  79136. + if (!hcd->core_if->dma_enable) {
  79137. + dwc_otg_core_global_regs_t *global_regs;
  79138. + gintmsk_data_t intr_mask = {.d32 = 0 };
  79139. +
  79140. + global_regs = hcd->core_if->core_global_regs;
  79141. + intr_mask.b.ptxfempty = 1;
  79142. +#ifdef DEBUG
  79143. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  79144. + DWC_DEBUGPL(DBG_HCDV,
  79145. + " P Tx Req Queue Space Avail (after queue): %d\n",
  79146. + tx_status.b.ptxqspcavail);
  79147. + DWC_DEBUGPL(DBG_HCDV,
  79148. + " P Tx FIFO Space Avail (after queue): %d\n",
  79149. + tx_status.b.ptxfspcavail);
  79150. +#endif
  79151. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  79152. + no_queue_space || no_fifo_space) {
  79153. + /*
  79154. + * May need to queue more transactions as the request
  79155. + * queue or Tx FIFO empties. Enable the periodic Tx
  79156. + * FIFO empty interrupt. (Always use the half-empty
  79157. + * level to ensure that new requests are loaded as
  79158. + * soon as possible.)
  79159. + */
  79160. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  79161. + intr_mask.d32);
  79162. + } else {
  79163. + /*
  79164. + * Disable the Tx FIFO empty interrupt since there are
  79165. + * no more transactions that need to be queued right
  79166. + * now. This function is called from interrupt
  79167. + * handlers to queue more transactions as transfer
  79168. + * states change.
  79169. + */
  79170. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  79171. + 0);
  79172. + }
  79173. + }
  79174. +}
  79175. +
  79176. +/**
  79177. + * Processes active non-periodic channels and queues transactions for these
  79178. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  79179. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  79180. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  79181. + * FIFO Empty interrupt is disabled.
  79182. + */
  79183. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  79184. +{
  79185. + gnptxsts_data_t tx_status;
  79186. + dwc_list_link_t *orig_qh_ptr;
  79187. + dwc_otg_qh_t *qh;
  79188. + int status;
  79189. + int no_queue_space = 0;
  79190. + int no_fifo_space = 0;
  79191. + int more_to_do = 0;
  79192. +
  79193. + dwc_otg_core_global_regs_t *global_regs =
  79194. + hcd->core_if->core_global_regs;
  79195. +
  79196. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  79197. +#ifdef DEBUG
  79198. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  79199. + DWC_DEBUGPL(DBG_HCDV,
  79200. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  79201. + tx_status.b.nptxqspcavail);
  79202. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  79203. + tx_status.b.nptxfspcavail);
  79204. +#endif
  79205. + /*
  79206. + * Keep track of the starting point. Skip over the start-of-list
  79207. + * entry.
  79208. + */
  79209. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  79210. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  79211. + }
  79212. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  79213. +
  79214. + /*
  79215. + * Process once through the active list or until no more space is
  79216. + * available in the request queue or the Tx FIFO.
  79217. + */
  79218. + do {
  79219. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  79220. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  79221. + no_queue_space = 1;
  79222. + break;
  79223. + }
  79224. +
  79225. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  79226. + qh_list_entry);
  79227. +
  79228. + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  79229. + fiq_fsm_queue_split_transaction(hcd, qh);
  79230. + } else {
  79231. + status = queue_transaction(hcd, qh->channel,
  79232. + tx_status.b.nptxfspcavail);
  79233. +
  79234. + if (status > 0) {
  79235. + more_to_do = 1;
  79236. + } else if (status < 0) {
  79237. + no_fifo_space = 1;
  79238. + break;
  79239. + }
  79240. + }
  79241. + /* Advance to next QH, skipping start-of-list entry. */
  79242. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  79243. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  79244. + hcd->non_periodic_qh_ptr =
  79245. + hcd->non_periodic_qh_ptr->next;
  79246. + }
  79247. +
  79248. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  79249. +
  79250. + if (!hcd->core_if->dma_enable) {
  79251. + gintmsk_data_t intr_mask = {.d32 = 0 };
  79252. + intr_mask.b.nptxfempty = 1;
  79253. +
  79254. +#ifdef DEBUG
  79255. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  79256. + DWC_DEBUGPL(DBG_HCDV,
  79257. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  79258. + tx_status.b.nptxqspcavail);
  79259. + DWC_DEBUGPL(DBG_HCDV,
  79260. + " NP Tx FIFO Space Avail (after queue): %d\n",
  79261. + tx_status.b.nptxfspcavail);
  79262. +#endif
  79263. + if (more_to_do || no_queue_space || no_fifo_space) {
  79264. + /*
  79265. + * May need to queue more transactions as the request
  79266. + * queue or Tx FIFO empties. Enable the non-periodic
  79267. + * Tx FIFO empty interrupt. (Always use the half-empty
  79268. + * level to ensure that new requests are loaded as
  79269. + * soon as possible.)
  79270. + */
  79271. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  79272. + intr_mask.d32);
  79273. + } else {
  79274. + /*
  79275. + * Disable the Tx FIFO empty interrupt since there are
  79276. + * no more transactions that need to be queued right
  79277. + * now. This function is called from interrupt
  79278. + * handlers to queue more transactions as transfer
  79279. + * states change.
  79280. + */
  79281. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  79282. + 0);
  79283. + }
  79284. + }
  79285. +}
  79286. +
  79287. +/**
  79288. + * This function processes the currently active host channels and queues
  79289. + * transactions for these channels to the DWC_otg controller. It is called
  79290. + * from HCD interrupt handler functions.
  79291. + *
  79292. + * @param hcd The HCD state structure.
  79293. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  79294. + * periodic, or both).
  79295. + */
  79296. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  79297. + dwc_otg_transaction_type_e tr_type)
  79298. +{
  79299. +#ifdef DEBUG_SOF
  79300. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  79301. +#endif
  79302. + /* Process host channels associated with periodic transfers. */
  79303. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  79304. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  79305. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  79306. +
  79307. + process_periodic_channels(hcd);
  79308. + }
  79309. +
  79310. + /* Process host channels associated with non-periodic transfers. */
  79311. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  79312. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  79313. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  79314. + process_non_periodic_channels(hcd);
  79315. + } else {
  79316. + /*
  79317. + * Ensure NP Tx FIFO empty interrupt is disabled when
  79318. + * there are no non-periodic transfers to process.
  79319. + */
  79320. + gintmsk_data_t gintmsk = {.d32 = 0 };
  79321. + gintmsk.b.nptxfempty = 1;
  79322. + DWC_MODIFY_REG32(&hcd->core_if->
  79323. + core_global_regs->gintmsk, gintmsk.d32,
  79324. + 0);
  79325. + }
  79326. + }
  79327. +}
  79328. +
  79329. +#ifdef DWC_HS_ELECT_TST
  79330. +/*
  79331. + * Quick and dirty hack to implement the HS Electrical Test
  79332. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  79333. + *
  79334. + * This code was copied from our userspace app "hset". It sends a
  79335. + * Get Device Descriptor control sequence in two parts, first the
  79336. + * Setup packet by itself, followed some time later by the In and
  79337. + * Ack packets. Rather than trying to figure out how to add this
  79338. + * functionality to the normal driver code, we just hijack the
  79339. + * hardware, using these two function to drive the hardware
  79340. + * directly.
  79341. + */
  79342. +
  79343. +static dwc_otg_core_global_regs_t *global_regs;
  79344. +static dwc_otg_host_global_regs_t *hc_global_regs;
  79345. +static dwc_otg_hc_regs_t *hc_regs;
  79346. +static uint32_t *data_fifo;
  79347. +
  79348. +static void do_setup(void)
  79349. +{
  79350. + gintsts_data_t gintsts;
  79351. + hctsiz_data_t hctsiz;
  79352. + hcchar_data_t hcchar;
  79353. + haint_data_t haint;
  79354. + hcint_data_t hcint;
  79355. +
  79356. + /* Enable HAINTs */
  79357. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  79358. +
  79359. + /* Enable HCINTs */
  79360. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  79361. +
  79362. + /* Read GINTSTS */
  79363. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79364. +
  79365. + /* Read HAINT */
  79366. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79367. +
  79368. + /* Read HCINT */
  79369. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79370. +
  79371. + /* Read HCCHAR */
  79372. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79373. +
  79374. + /* Clear HCINT */
  79375. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79376. +
  79377. + /* Clear HAINT */
  79378. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79379. +
  79380. + /* Clear GINTSTS */
  79381. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79382. +
  79383. + /* Read GINTSTS */
  79384. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79385. +
  79386. + /*
  79387. + * Send Setup packet (Get Device Descriptor)
  79388. + */
  79389. +
  79390. + /* Make sure channel is disabled */
  79391. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79392. + if (hcchar.b.chen) {
  79393. + hcchar.b.chdis = 1;
  79394. +// hcchar.b.chen = 1;
  79395. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  79396. + //sleep(1);
  79397. + dwc_mdelay(1000);
  79398. +
  79399. + /* Read GINTSTS */
  79400. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79401. +
  79402. + /* Read HAINT */
  79403. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79404. +
  79405. + /* Read HCINT */
  79406. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79407. +
  79408. + /* Read HCCHAR */
  79409. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79410. +
  79411. + /* Clear HCINT */
  79412. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79413. +
  79414. + /* Clear HAINT */
  79415. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79416. +
  79417. + /* Clear GINTSTS */
  79418. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79419. +
  79420. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79421. + }
  79422. +
  79423. + /* Set HCTSIZ */
  79424. + hctsiz.d32 = 0;
  79425. + hctsiz.b.xfersize = 8;
  79426. + hctsiz.b.pktcnt = 1;
  79427. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  79428. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  79429. +
  79430. + /* Set HCCHAR */
  79431. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79432. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  79433. + hcchar.b.epdir = 0;
  79434. + hcchar.b.epnum = 0;
  79435. + hcchar.b.mps = 8;
  79436. + hcchar.b.chen = 1;
  79437. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  79438. +
  79439. + /* Fill FIFO with Setup data for Get Device Descriptor */
  79440. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  79441. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  79442. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  79443. +
  79444. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79445. +
  79446. + /* Wait for host channel interrupt */
  79447. + do {
  79448. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79449. + } while (gintsts.b.hcintr == 0);
  79450. +
  79451. + /* Disable HCINTs */
  79452. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  79453. +
  79454. + /* Disable HAINTs */
  79455. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  79456. +
  79457. + /* Read HAINT */
  79458. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79459. +
  79460. + /* Read HCINT */
  79461. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79462. +
  79463. + /* Read HCCHAR */
  79464. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79465. +
  79466. + /* Clear HCINT */
  79467. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79468. +
  79469. + /* Clear HAINT */
  79470. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79471. +
  79472. + /* Clear GINTSTS */
  79473. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79474. +
  79475. + /* Read GINTSTS */
  79476. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79477. +}
  79478. +
  79479. +static void do_in_ack(void)
  79480. +{
  79481. + gintsts_data_t gintsts;
  79482. + hctsiz_data_t hctsiz;
  79483. + hcchar_data_t hcchar;
  79484. + haint_data_t haint;
  79485. + hcint_data_t hcint;
  79486. + host_grxsts_data_t grxsts;
  79487. +
  79488. + /* Enable HAINTs */
  79489. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  79490. +
  79491. + /* Enable HCINTs */
  79492. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  79493. +
  79494. + /* Read GINTSTS */
  79495. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79496. +
  79497. + /* Read HAINT */
  79498. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79499. +
  79500. + /* Read HCINT */
  79501. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79502. +
  79503. + /* Read HCCHAR */
  79504. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79505. +
  79506. + /* Clear HCINT */
  79507. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79508. +
  79509. + /* Clear HAINT */
  79510. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79511. +
  79512. + /* Clear GINTSTS */
  79513. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79514. +
  79515. + /* Read GINTSTS */
  79516. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79517. +
  79518. + /*
  79519. + * Receive Control In packet
  79520. + */
  79521. +
  79522. + /* Make sure channel is disabled */
  79523. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79524. + if (hcchar.b.chen) {
  79525. + hcchar.b.chdis = 1;
  79526. + hcchar.b.chen = 1;
  79527. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  79528. + //sleep(1);
  79529. + dwc_mdelay(1000);
  79530. +
  79531. + /* Read GINTSTS */
  79532. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79533. +
  79534. + /* Read HAINT */
  79535. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79536. +
  79537. + /* Read HCINT */
  79538. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79539. +
  79540. + /* Read HCCHAR */
  79541. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79542. +
  79543. + /* Clear HCINT */
  79544. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79545. +
  79546. + /* Clear HAINT */
  79547. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79548. +
  79549. + /* Clear GINTSTS */
  79550. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79551. +
  79552. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79553. + }
  79554. +
  79555. + /* Set HCTSIZ */
  79556. + hctsiz.d32 = 0;
  79557. + hctsiz.b.xfersize = 8;
  79558. + hctsiz.b.pktcnt = 1;
  79559. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  79560. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  79561. +
  79562. + /* Set HCCHAR */
  79563. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79564. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  79565. + hcchar.b.epdir = 1;
  79566. + hcchar.b.epnum = 0;
  79567. + hcchar.b.mps = 8;
  79568. + hcchar.b.chen = 1;
  79569. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  79570. +
  79571. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79572. +
  79573. + /* Wait for receive status queue interrupt */
  79574. + do {
  79575. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79576. + } while (gintsts.b.rxstsqlvl == 0);
  79577. +
  79578. + /* Read RXSTS */
  79579. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  79580. +
  79581. + /* Clear RXSTSQLVL in GINTSTS */
  79582. + gintsts.d32 = 0;
  79583. + gintsts.b.rxstsqlvl = 1;
  79584. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79585. +
  79586. + switch (grxsts.b.pktsts) {
  79587. + case DWC_GRXSTS_PKTSTS_IN:
  79588. + /* Read the data into the host buffer */
  79589. + if (grxsts.b.bcnt > 0) {
  79590. + int i;
  79591. + int word_count = (grxsts.b.bcnt + 3) / 4;
  79592. +
  79593. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  79594. +
  79595. + for (i = 0; i < word_count; i++) {
  79596. + (void)DWC_READ_REG32(data_fifo++);
  79597. + }
  79598. + }
  79599. + break;
  79600. +
  79601. + default:
  79602. + break;
  79603. + }
  79604. +
  79605. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79606. +
  79607. + /* Wait for receive status queue interrupt */
  79608. + do {
  79609. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79610. + } while (gintsts.b.rxstsqlvl == 0);
  79611. +
  79612. + /* Read RXSTS */
  79613. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  79614. +
  79615. + /* Clear RXSTSQLVL in GINTSTS */
  79616. + gintsts.d32 = 0;
  79617. + gintsts.b.rxstsqlvl = 1;
  79618. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79619. +
  79620. + switch (grxsts.b.pktsts) {
  79621. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  79622. + break;
  79623. +
  79624. + default:
  79625. + break;
  79626. + }
  79627. +
  79628. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79629. +
  79630. + /* Wait for host channel interrupt */
  79631. + do {
  79632. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79633. + } while (gintsts.b.hcintr == 0);
  79634. +
  79635. + /* Read HAINT */
  79636. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79637. +
  79638. + /* Read HCINT */
  79639. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79640. +
  79641. + /* Read HCCHAR */
  79642. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79643. +
  79644. + /* Clear HCINT */
  79645. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79646. +
  79647. + /* Clear HAINT */
  79648. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79649. +
  79650. + /* Clear GINTSTS */
  79651. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79652. +
  79653. + /* Read GINTSTS */
  79654. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79655. +
  79656. +// usleep(100000);
  79657. +// mdelay(100);
  79658. + dwc_mdelay(1);
  79659. +
  79660. + /*
  79661. + * Send handshake packet
  79662. + */
  79663. +
  79664. + /* Read HAINT */
  79665. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79666. +
  79667. + /* Read HCINT */
  79668. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79669. +
  79670. + /* Read HCCHAR */
  79671. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79672. +
  79673. + /* Clear HCINT */
  79674. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79675. +
  79676. + /* Clear HAINT */
  79677. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79678. +
  79679. + /* Clear GINTSTS */
  79680. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79681. +
  79682. + /* Read GINTSTS */
  79683. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79684. +
  79685. + /* Make sure channel is disabled */
  79686. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79687. + if (hcchar.b.chen) {
  79688. + hcchar.b.chdis = 1;
  79689. + hcchar.b.chen = 1;
  79690. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  79691. + //sleep(1);
  79692. + dwc_mdelay(1000);
  79693. +
  79694. + /* Read GINTSTS */
  79695. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79696. +
  79697. + /* Read HAINT */
  79698. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79699. +
  79700. + /* Read HCINT */
  79701. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79702. +
  79703. + /* Read HCCHAR */
  79704. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79705. +
  79706. + /* Clear HCINT */
  79707. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79708. +
  79709. + /* Clear HAINT */
  79710. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79711. +
  79712. + /* Clear GINTSTS */
  79713. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79714. +
  79715. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79716. + }
  79717. +
  79718. + /* Set HCTSIZ */
  79719. + hctsiz.d32 = 0;
  79720. + hctsiz.b.xfersize = 0;
  79721. + hctsiz.b.pktcnt = 1;
  79722. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  79723. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  79724. +
  79725. + /* Set HCCHAR */
  79726. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79727. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  79728. + hcchar.b.epdir = 0;
  79729. + hcchar.b.epnum = 0;
  79730. + hcchar.b.mps = 8;
  79731. + hcchar.b.chen = 1;
  79732. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  79733. +
  79734. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79735. +
  79736. + /* Wait for host channel interrupt */
  79737. + do {
  79738. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79739. + } while (gintsts.b.hcintr == 0);
  79740. +
  79741. + /* Disable HCINTs */
  79742. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  79743. +
  79744. + /* Disable HAINTs */
  79745. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  79746. +
  79747. + /* Read HAINT */
  79748. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  79749. +
  79750. + /* Read HCINT */
  79751. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  79752. +
  79753. + /* Read HCCHAR */
  79754. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  79755. +
  79756. + /* Clear HCINT */
  79757. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  79758. +
  79759. + /* Clear HAINT */
  79760. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  79761. +
  79762. + /* Clear GINTSTS */
  79763. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  79764. +
  79765. + /* Read GINTSTS */
  79766. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  79767. +}
  79768. +#endif
  79769. +
  79770. +/** Handles hub class-specific requests. */
  79771. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  79772. + uint16_t typeReq,
  79773. + uint16_t wValue,
  79774. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  79775. +{
  79776. + int retval = 0;
  79777. +
  79778. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  79779. + usb_hub_descriptor_t *hub_desc;
  79780. + hprt0_data_t hprt0 = {.d32 = 0 };
  79781. +
  79782. + uint32_t port_status;
  79783. +
  79784. + switch (typeReq) {
  79785. + case UCR_CLEAR_HUB_FEATURE:
  79786. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79787. + "ClearHubFeature 0x%x\n", wValue);
  79788. + switch (wValue) {
  79789. + case UHF_C_HUB_LOCAL_POWER:
  79790. + case UHF_C_HUB_OVER_CURRENT:
  79791. + /* Nothing required here */
  79792. + break;
  79793. + default:
  79794. + retval = -DWC_E_INVALID;
  79795. + DWC_ERROR("DWC OTG HCD - "
  79796. + "ClearHubFeature request %xh unknown\n",
  79797. + wValue);
  79798. + }
  79799. + break;
  79800. + case UCR_CLEAR_PORT_FEATURE:
  79801. +#ifdef CONFIG_USB_DWC_OTG_LPM
  79802. + if (wValue != UHF_PORT_L1)
  79803. +#endif
  79804. + if (!wIndex || wIndex > 1)
  79805. + goto error;
  79806. +
  79807. + switch (wValue) {
  79808. + case UHF_PORT_ENABLE:
  79809. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  79810. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  79811. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79812. + hprt0.b.prtena = 1;
  79813. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79814. + break;
  79815. + case UHF_PORT_SUSPEND:
  79816. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79817. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  79818. +
  79819. + if (core_if->power_down == 2) {
  79820. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  79821. + } else {
  79822. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  79823. + dwc_mdelay(5);
  79824. +
  79825. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79826. + hprt0.b.prtres = 1;
  79827. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79828. + hprt0.b.prtsusp = 0;
  79829. + /* Clear Resume bit */
  79830. + dwc_mdelay(100);
  79831. + hprt0.b.prtres = 0;
  79832. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79833. + }
  79834. + break;
  79835. +#ifdef CONFIG_USB_DWC_OTG_LPM
  79836. + case UHF_PORT_L1:
  79837. + {
  79838. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  79839. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  79840. +
  79841. + lpmcfg.d32 =
  79842. + DWC_READ_REG32(&core_if->
  79843. + core_global_regs->glpmcfg);
  79844. + lpmcfg.b.en_utmi_sleep = 0;
  79845. + lpmcfg.b.hird_thres &= (~(1 << 4));
  79846. + lpmcfg.b.prt_sleep_sts = 1;
  79847. + DWC_WRITE_REG32(&core_if->
  79848. + core_global_regs->glpmcfg,
  79849. + lpmcfg.d32);
  79850. +
  79851. + /* Clear Enbl_L1Gating bit. */
  79852. + pcgcctl.b.enbl_sleep_gating = 1;
  79853. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  79854. + 0);
  79855. +
  79856. + dwc_mdelay(5);
  79857. +
  79858. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79859. + hprt0.b.prtres = 1;
  79860. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  79861. + hprt0.d32);
  79862. + /* This bit will be cleared in wakeup interrupt handle */
  79863. + break;
  79864. + }
  79865. +#endif
  79866. + case UHF_PORT_POWER:
  79867. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79868. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  79869. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  79870. + hprt0.b.prtpwr = 0;
  79871. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  79872. + break;
  79873. + case UHF_PORT_INDICATOR:
  79874. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79875. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  79876. + /* Port inidicator not supported */
  79877. + break;
  79878. + case UHF_C_PORT_CONNECTION:
  79879. + /* Clears drivers internal connect status change
  79880. + * flag */
  79881. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79882. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  79883. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  79884. + break;
  79885. + case UHF_C_PORT_RESET:
  79886. + /* Clears the driver's internal Port Reset Change
  79887. + * flag */
  79888. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79889. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  79890. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  79891. + break;
  79892. + case UHF_C_PORT_ENABLE:
  79893. + /* Clears the driver's internal Port
  79894. + * Enable/Disable Change flag */
  79895. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79896. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  79897. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  79898. + break;
  79899. + case UHF_C_PORT_SUSPEND:
  79900. + /* Clears the driver's internal Port Suspend
  79901. + * Change flag, which is set when resume signaling on
  79902. + * the host port is complete */
  79903. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79904. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  79905. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  79906. + break;
  79907. +#ifdef CONFIG_USB_DWC_OTG_LPM
  79908. + case UHF_C_PORT_L1:
  79909. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  79910. + break;
  79911. +#endif
  79912. + case UHF_C_PORT_OVER_CURRENT:
  79913. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79914. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  79915. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  79916. + break;
  79917. + default:
  79918. + retval = -DWC_E_INVALID;
  79919. + DWC_ERROR("DWC OTG HCD - "
  79920. + "ClearPortFeature request %xh "
  79921. + "unknown or unsupported\n", wValue);
  79922. + }
  79923. + break;
  79924. + case UCR_GET_HUB_DESCRIPTOR:
  79925. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79926. + "GetHubDescriptor\n");
  79927. + hub_desc = (usb_hub_descriptor_t *) buf;
  79928. + hub_desc->bDescLength = 9;
  79929. + hub_desc->bDescriptorType = 0x29;
  79930. + hub_desc->bNbrPorts = 1;
  79931. + USETW(hub_desc->wHubCharacteristics, 0x08);
  79932. + hub_desc->bPwrOn2PwrGood = 1;
  79933. + hub_desc->bHubContrCurrent = 0;
  79934. + hub_desc->DeviceRemovable[0] = 0;
  79935. + hub_desc->DeviceRemovable[1] = 0xff;
  79936. + break;
  79937. + case UCR_GET_HUB_STATUS:
  79938. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79939. + "GetHubStatus\n");
  79940. + DWC_MEMSET(buf, 0, 4);
  79941. + break;
  79942. + case UCR_GET_PORT_STATUS:
  79943. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  79944. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  79945. + wIndex, dwc_otg_hcd->flags.d32);
  79946. + if (!wIndex || wIndex > 1)
  79947. + goto error;
  79948. +
  79949. + port_status = 0;
  79950. +
  79951. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  79952. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  79953. +
  79954. + if (dwc_otg_hcd->flags.b.port_enable_change)
  79955. + port_status |= (1 << UHF_C_PORT_ENABLE);
  79956. +
  79957. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  79958. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  79959. +
  79960. + if (dwc_otg_hcd->flags.b.port_l1_change)
  79961. + port_status |= (1 << UHF_C_PORT_L1);
  79962. +
  79963. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  79964. + port_status |= (1 << UHF_C_PORT_RESET);
  79965. + }
  79966. +
  79967. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  79968. + DWC_WARN("Overcurrent change detected\n");
  79969. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  79970. + }
  79971. +
  79972. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  79973. + /*
  79974. + * The port is disconnected, which means the core is
  79975. + * either in device mode or it soon will be. Just
  79976. + * return 0's for the remainder of the port status
  79977. + * since the port register can't be read if the core
  79978. + * is in device mode.
  79979. + */
  79980. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  79981. + break;
  79982. + }
  79983. +
  79984. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  79985. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  79986. +
  79987. + if (hprt0.b.prtconnsts)
  79988. + port_status |= (1 << UHF_PORT_CONNECTION);
  79989. +
  79990. + if (hprt0.b.prtena)
  79991. + port_status |= (1 << UHF_PORT_ENABLE);
  79992. +
  79993. + if (hprt0.b.prtsusp)
  79994. + port_status |= (1 << UHF_PORT_SUSPEND);
  79995. +
  79996. + if (hprt0.b.prtovrcurract)
  79997. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  79998. +
  79999. + if (hprt0.b.prtrst)
  80000. + port_status |= (1 << UHF_PORT_RESET);
  80001. +
  80002. + if (hprt0.b.prtpwr)
  80003. + port_status |= (1 << UHF_PORT_POWER);
  80004. +
  80005. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  80006. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  80007. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  80008. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  80009. +
  80010. + if (hprt0.b.prttstctl)
  80011. + port_status |= (1 << UHF_PORT_TEST);
  80012. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  80013. + port_status |= (1 << UHF_PORT_L1);
  80014. + }
  80015. + /*
  80016. + For Synopsys HW emulation of Power down wkup_control asserts the
  80017. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  80018. + We intentionally tell the software that port is in L2Suspend state.
  80019. + Only for STE.
  80020. + */
  80021. + if ((core_if->power_down == 2)
  80022. + && (core_if->hibernation_suspend == 1)) {
  80023. + port_status |= (1 << UHF_PORT_SUSPEND);
  80024. + }
  80025. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  80026. +
  80027. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  80028. +
  80029. + break;
  80030. + case UCR_SET_HUB_FEATURE:
  80031. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  80032. + "SetHubFeature\n");
  80033. + /* No HUB features supported */
  80034. + break;
  80035. + case UCR_SET_PORT_FEATURE:
  80036. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  80037. + goto error;
  80038. +
  80039. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  80040. + /*
  80041. + * The port is disconnected, which means the core is
  80042. + * either in device mode or it soon will be. Just
  80043. + * return without doing anything since the port
  80044. + * register can't be written if the core is in device
  80045. + * mode.
  80046. + */
  80047. + break;
  80048. + }
  80049. +
  80050. + switch (wValue) {
  80051. + case UHF_PORT_SUSPEND:
  80052. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  80053. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  80054. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  80055. + goto error;
  80056. + }
  80057. + if (core_if->power_down == 2) {
  80058. + int timeout = 300;
  80059. + dwc_irqflags_t flags;
  80060. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  80061. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  80062. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  80063. +#ifdef DWC_DEV_SRPCAP
  80064. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  80065. +#endif
  80066. + DWC_PRINTF("Preparing for complete power-off\n");
  80067. +
  80068. + /* Save registers before hibernation */
  80069. + dwc_otg_save_global_regs(core_if);
  80070. + dwc_otg_save_host_regs(core_if);
  80071. +
  80072. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80073. + hprt0.b.prtsusp = 1;
  80074. + hprt0.b.prtena = 0;
  80075. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  80076. + /* Spin hprt0.b.prtsusp to became 1 */
  80077. + do {
  80078. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80079. + if (hprt0.b.prtsusp) {
  80080. + break;
  80081. + }
  80082. + dwc_mdelay(1);
  80083. + } while (--timeout);
  80084. + if (!timeout) {
  80085. + DWC_WARN("Suspend wasn't genereted\n");
  80086. + }
  80087. + dwc_udelay(10);
  80088. +
  80089. + /*
  80090. + * We need to disable interrupts to prevent servicing of any IRQ
  80091. + * during going to hibernation
  80092. + */
  80093. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  80094. + core_if->lx_state = DWC_OTG_L2;
  80095. +#ifdef DWC_DEV_SRPCAP
  80096. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80097. + hprt0.b.prtpwr = 0;
  80098. + hprt0.b.prtena = 0;
  80099. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  80100. + hprt0.d32);
  80101. +#endif
  80102. + gusbcfg.d32 =
  80103. + DWC_READ_REG32(&core_if->core_global_regs->
  80104. + gusbcfg);
  80105. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  80106. + /* ULPI interface */
  80107. + /* Suspend the Phy Clock */
  80108. + pcgcctl.d32 = 0;
  80109. + pcgcctl.b.stoppclk = 1;
  80110. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  80111. + pcgcctl.d32);
  80112. + dwc_udelay(10);
  80113. + gpwrdn.b.pmuactv = 1;
  80114. + DWC_MODIFY_REG32(&core_if->
  80115. + core_global_regs->
  80116. + gpwrdn, 0, gpwrdn.d32);
  80117. + } else {
  80118. + /* UTMI+ Interface */
  80119. + gpwrdn.b.pmuactv = 1;
  80120. + DWC_MODIFY_REG32(&core_if->
  80121. + core_global_regs->
  80122. + gpwrdn, 0, gpwrdn.d32);
  80123. + dwc_udelay(10);
  80124. + pcgcctl.b.stoppclk = 1;
  80125. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  80126. + dwc_udelay(10);
  80127. + }
  80128. +#ifdef DWC_DEV_SRPCAP
  80129. + gpwrdn.d32 = 0;
  80130. + gpwrdn.b.dis_vbus = 1;
  80131. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80132. + gpwrdn, 0, gpwrdn.d32);
  80133. +#endif
  80134. + gpwrdn.d32 = 0;
  80135. + gpwrdn.b.pmuintsel = 1;
  80136. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80137. + gpwrdn, 0, gpwrdn.d32);
  80138. + dwc_udelay(10);
  80139. +
  80140. + gpwrdn.d32 = 0;
  80141. +#ifdef DWC_DEV_SRPCAP
  80142. + gpwrdn.b.srp_det_msk = 1;
  80143. +#endif
  80144. + gpwrdn.b.disconn_det_msk = 1;
  80145. + gpwrdn.b.lnstchng_msk = 1;
  80146. + gpwrdn.b.sts_chngint_msk = 1;
  80147. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80148. + gpwrdn, 0, gpwrdn.d32);
  80149. + dwc_udelay(10);
  80150. +
  80151. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  80152. + gpwrdn.d32 = 0;
  80153. + gpwrdn.b.pwrdnclmp = 1;
  80154. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80155. + gpwrdn, 0, gpwrdn.d32);
  80156. + dwc_udelay(10);
  80157. +
  80158. + /* Switch off VDD */
  80159. + gpwrdn.d32 = 0;
  80160. + gpwrdn.b.pwrdnswtch = 1;
  80161. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80162. + gpwrdn, 0, gpwrdn.d32);
  80163. +
  80164. +#ifdef DWC_DEV_SRPCAP
  80165. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  80166. + {
  80167. + core_if->pwron_timer_started = 1;
  80168. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  80169. + }
  80170. +#endif
  80171. + /* Save gpwrdn register for further usage if stschng interrupt */
  80172. + core_if->gr_backup->gpwrdn_local =
  80173. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  80174. +
  80175. + /* Set flag to indicate that we are in hibernation */
  80176. + core_if->hibernation_suspend = 1;
  80177. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  80178. +
  80179. + DWC_PRINTF("Host hibernation completed\n");
  80180. + // Exit from case statement
  80181. + break;
  80182. +
  80183. + }
  80184. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  80185. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  80186. + gotgctl_data_t gotgctl = {.d32 = 0 };
  80187. + gotgctl.b.hstsethnpen = 1;
  80188. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80189. + gotgctl, 0, gotgctl.d32);
  80190. + core_if->op_state = A_SUSPEND;
  80191. + }
  80192. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80193. + hprt0.b.prtsusp = 1;
  80194. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  80195. + {
  80196. + dwc_irqflags_t flags;
  80197. + /* Update lx_state */
  80198. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  80199. + core_if->lx_state = DWC_OTG_L2;
  80200. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  80201. + }
  80202. + /* Suspend the Phy Clock */
  80203. + {
  80204. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  80205. + pcgcctl.b.stoppclk = 1;
  80206. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  80207. + pcgcctl.d32);
  80208. + dwc_udelay(10);
  80209. + }
  80210. +
  80211. + /* For HNP the bus must be suspended for at least 200ms. */
  80212. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  80213. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  80214. + pcgcctl.b.stoppclk = 1;
  80215. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  80216. + dwc_mdelay(200);
  80217. + }
  80218. +
  80219. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  80220. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  80221. + if (core_if->adp_enable) {
  80222. + gotgctl_data_t gotgctl = {.d32 = 0 };
  80223. + gpwrdn_data_t gpwrdn;
  80224. +
  80225. + while (gotgctl.b.asesvld == 1) {
  80226. + gotgctl.d32 =
  80227. + DWC_READ_REG32(&core_if->
  80228. + core_global_regs->
  80229. + gotgctl);
  80230. + dwc_mdelay(100);
  80231. + }
  80232. +
  80233. + /* Enable Power Down Logic */
  80234. + gpwrdn.d32 = 0;
  80235. + gpwrdn.b.pmuactv = 1;
  80236. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80237. + gpwrdn, 0, gpwrdn.d32);
  80238. +
  80239. + /* Unmask SRP detected interrupt from Power Down Logic */
  80240. + gpwrdn.d32 = 0;
  80241. + gpwrdn.b.srp_det_msk = 1;
  80242. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  80243. + gpwrdn, 0, gpwrdn.d32);
  80244. +
  80245. + dwc_otg_adp_probe_start(core_if);
  80246. + }
  80247. +#endif
  80248. + break;
  80249. + case UHF_PORT_POWER:
  80250. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  80251. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  80252. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80253. + hprt0.b.prtpwr = 1;
  80254. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  80255. + break;
  80256. + case UHF_PORT_RESET:
  80257. + if ((core_if->power_down == 2)
  80258. + && (core_if->hibernation_suspend == 1)) {
  80259. + /* If we are going to exit from Hibernated
  80260. + * state via USB RESET.
  80261. + */
  80262. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  80263. + } else {
  80264. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80265. +
  80266. + DWC_DEBUGPL(DBG_HCD,
  80267. + "DWC OTG HCD HUB CONTROL - "
  80268. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  80269. + {
  80270. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  80271. + pcgcctl.b.enbl_sleep_gating = 1;
  80272. + pcgcctl.b.stoppclk = 1;
  80273. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  80274. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  80275. + }
  80276. +#ifdef CONFIG_USB_DWC_OTG_LPM
  80277. + {
  80278. + glpmcfg_data_t lpmcfg;
  80279. + lpmcfg.d32 =
  80280. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  80281. + if (lpmcfg.b.prt_sleep_sts) {
  80282. + lpmcfg.b.en_utmi_sleep = 0;
  80283. + lpmcfg.b.hird_thres &= (~(1 << 4));
  80284. + DWC_WRITE_REG32
  80285. + (&core_if->core_global_regs->glpmcfg,
  80286. + lpmcfg.d32);
  80287. + dwc_mdelay(1);
  80288. + }
  80289. + }
  80290. +#endif
  80291. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80292. + /* Clear suspend bit if resetting from suspended state. */
  80293. + hprt0.b.prtsusp = 0;
  80294. + /* When B-Host the Port reset bit is set in
  80295. + * the Start HCD Callback function, so that
  80296. + * the reset is started within 1ms of the HNP
  80297. + * success interrupt. */
  80298. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  80299. + hprt0.b.prtpwr = 1;
  80300. + hprt0.b.prtrst = 1;
  80301. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  80302. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  80303. + hprt0.d32);
  80304. + }
  80305. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  80306. + dwc_mdelay(60);
  80307. + hprt0.b.prtrst = 0;
  80308. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  80309. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  80310. + }
  80311. + break;
  80312. +#ifdef DWC_HS_ELECT_TST
  80313. + case UHF_PORT_TEST:
  80314. + {
  80315. + uint32_t t;
  80316. + gintmsk_data_t gintmsk;
  80317. +
  80318. + t = (wIndex >> 8); /* MSB wIndex USB */
  80319. + DWC_DEBUGPL(DBG_HCD,
  80320. + "DWC OTG HCD HUB CONTROL - "
  80321. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  80322. + t);
  80323. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  80324. + if (t < 6) {
  80325. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  80326. + hprt0.b.prttstctl = t;
  80327. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  80328. + hprt0.d32);
  80329. + } else {
  80330. + /* Setup global vars with reg addresses (quick and
  80331. + * dirty hack, should be cleaned up)
  80332. + */
  80333. + global_regs = core_if->core_global_regs;
  80334. + hc_global_regs =
  80335. + core_if->host_if->host_global_regs;
  80336. + hc_regs =
  80337. + (dwc_otg_hc_regs_t *) ((char *)
  80338. + global_regs +
  80339. + 0x500);
  80340. + data_fifo =
  80341. + (uint32_t *) ((char *)global_regs +
  80342. + 0x1000);
  80343. +
  80344. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  80345. + /* Save current interrupt mask */
  80346. + gintmsk.d32 =
  80347. + DWC_READ_REG32
  80348. + (&global_regs->gintmsk);
  80349. +
  80350. + /* Disable all interrupts while we muck with
  80351. + * the hardware directly
  80352. + */
  80353. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  80354. +
  80355. + /* 15 second delay per the test spec */
  80356. + dwc_mdelay(15000);
  80357. +
  80358. + /* Drive suspend on the root port */
  80359. + hprt0.d32 =
  80360. + dwc_otg_read_hprt0(core_if);
  80361. + hprt0.b.prtsusp = 1;
  80362. + hprt0.b.prtres = 0;
  80363. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  80364. +
  80365. + /* 15 second delay per the test spec */
  80366. + dwc_mdelay(15000);
  80367. +
  80368. + /* Drive resume on the root port */
  80369. + hprt0.d32 =
  80370. + dwc_otg_read_hprt0(core_if);
  80371. + hprt0.b.prtsusp = 0;
  80372. + hprt0.b.prtres = 1;
  80373. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  80374. + dwc_mdelay(100);
  80375. +
  80376. + /* Clear the resume bit */
  80377. + hprt0.b.prtres = 0;
  80378. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  80379. +
  80380. + /* Restore interrupts */
  80381. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  80382. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  80383. + /* Save current interrupt mask */
  80384. + gintmsk.d32 =
  80385. + DWC_READ_REG32
  80386. + (&global_regs->gintmsk);
  80387. +
  80388. + /* Disable all interrupts while we muck with
  80389. + * the hardware directly
  80390. + */
  80391. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  80392. +
  80393. + /* 15 second delay per the test spec */
  80394. + dwc_mdelay(15000);
  80395. +
  80396. + /* Send the Setup packet */
  80397. + do_setup();
  80398. +
  80399. + /* 15 second delay so nothing else happens for awhile */
  80400. + dwc_mdelay(15000);
  80401. +
  80402. + /* Restore interrupts */
  80403. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  80404. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  80405. + /* Save current interrupt mask */
  80406. + gintmsk.d32 =
  80407. + DWC_READ_REG32
  80408. + (&global_regs->gintmsk);
  80409. +
  80410. + /* Disable all interrupts while we muck with
  80411. + * the hardware directly
  80412. + */
  80413. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  80414. +
  80415. + /* Send the Setup packet */
  80416. + do_setup();
  80417. +
  80418. + /* 15 second delay so nothing else happens for awhile */
  80419. + dwc_mdelay(15000);
  80420. +
  80421. + /* Send the In and Ack packets */
  80422. + do_in_ack();
  80423. +
  80424. + /* 15 second delay so nothing else happens for awhile */
  80425. + dwc_mdelay(15000);
  80426. +
  80427. + /* Restore interrupts */
  80428. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  80429. + }
  80430. + }
  80431. + break;
  80432. + }
  80433. +#endif /* DWC_HS_ELECT_TST */
  80434. +
  80435. + case UHF_PORT_INDICATOR:
  80436. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  80437. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  80438. + /* Not supported */
  80439. + break;
  80440. + default:
  80441. + retval = -DWC_E_INVALID;
  80442. + DWC_ERROR("DWC OTG HCD - "
  80443. + "SetPortFeature request %xh "
  80444. + "unknown or unsupported\n", wValue);
  80445. + break;
  80446. + }
  80447. + break;
  80448. +#ifdef CONFIG_USB_DWC_OTG_LPM
  80449. + case UCR_SET_AND_TEST_PORT_FEATURE:
  80450. + if (wValue != UHF_PORT_L1) {
  80451. + goto error;
  80452. + }
  80453. + {
  80454. + int portnum, hird, devaddr, remwake;
  80455. + glpmcfg_data_t lpmcfg;
  80456. + uint32_t time_usecs;
  80457. + gintsts_data_t gintsts;
  80458. + gintmsk_data_t gintmsk;
  80459. +
  80460. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  80461. + goto error;
  80462. + }
  80463. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  80464. + goto error;
  80465. + }
  80466. + /* Check if the port currently is in SLEEP state */
  80467. + lpmcfg.d32 =
  80468. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  80469. + if (lpmcfg.b.prt_sleep_sts) {
  80470. + DWC_INFO("Port is already in sleep mode\n");
  80471. + buf[0] = 0; /* Return success */
  80472. + break;
  80473. + }
  80474. +
  80475. + portnum = wIndex & 0xf;
  80476. + hird = (wIndex >> 4) & 0xf;
  80477. + devaddr = (wIndex >> 8) & 0x7f;
  80478. + remwake = (wIndex >> 15);
  80479. +
  80480. + if (portnum != 1) {
  80481. + retval = -DWC_E_INVALID;
  80482. + DWC_WARN
  80483. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  80484. + portnum);
  80485. + break;
  80486. + }
  80487. +
  80488. + DWC_PRINTF
  80489. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  80490. + portnum, hird, devaddr, remwake);
  80491. + /* Disable LPM interrupt */
  80492. + gintmsk.d32 = 0;
  80493. + gintmsk.b.lpmtranrcvd = 1;
  80494. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  80495. + gintmsk.d32, 0);
  80496. +
  80497. + if (dwc_otg_hcd_send_lpm
  80498. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  80499. + retval = -DWC_E_INVALID;
  80500. + break;
  80501. + }
  80502. +
  80503. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  80504. + /* We will consider timeout if time_usecs microseconds pass,
  80505. + * and we don't receive LPM transaction status.
  80506. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  80507. + * core will set lpmtranrcvd bit.
  80508. + */
  80509. + do {
  80510. + gintsts.d32 =
  80511. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  80512. + if (gintsts.b.lpmtranrcvd) {
  80513. + break;
  80514. + }
  80515. + dwc_udelay(1);
  80516. + } while (--time_usecs);
  80517. + /* lpm_int bit will be cleared in LPM interrupt handler */
  80518. +
  80519. + /* Now fill status
  80520. + * 0x00 - Success
  80521. + * 0x10 - NYET
  80522. + * 0x11 - Timeout
  80523. + */
  80524. + if (!gintsts.b.lpmtranrcvd) {
  80525. + buf[0] = 0x3; /* Completion code is Timeout */
  80526. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  80527. + } else {
  80528. + lpmcfg.d32 =
  80529. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  80530. + if (lpmcfg.b.lpm_resp == 0x3) {
  80531. + /* ACK responce from the device */
  80532. + buf[0] = 0x00; /* Success */
  80533. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  80534. + /* NYET responce from the device */
  80535. + buf[0] = 0x2;
  80536. + } else {
  80537. + /* Otherwise responce with Timeout */
  80538. + buf[0] = 0x3;
  80539. + }
  80540. + }
  80541. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  80542. + lpmcfg.b.lpm_resp);
  80543. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  80544. + gintmsk.d32);
  80545. +
  80546. + break;
  80547. + }
  80548. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  80549. + default:
  80550. +error:
  80551. + retval = -DWC_E_INVALID;
  80552. + DWC_WARN("DWC OTG HCD - "
  80553. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  80554. + typeReq, wIndex, wValue);
  80555. + break;
  80556. + }
  80557. +
  80558. + return retval;
  80559. +}
  80560. +
  80561. +#ifdef CONFIG_USB_DWC_OTG_LPM
  80562. +/** Returns index of host channel to perform LPM transaction. */
  80563. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  80564. +{
  80565. + dwc_otg_core_if_t *core_if = hcd->core_if;
  80566. + dwc_hc_t *hc;
  80567. + hcchar_data_t hcchar;
  80568. + gintmsk_data_t gintmsk = {.d32 = 0 };
  80569. +
  80570. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  80571. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  80572. + return -1;
  80573. + }
  80574. +
  80575. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  80576. +
  80577. + /* Mask host channel interrupts. */
  80578. + gintmsk.b.hcintr = 1;
  80579. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  80580. +
  80581. + /* Fill fields that core needs for LPM transaction */
  80582. + hcchar.b.devaddr = devaddr;
  80583. + hcchar.b.epnum = 0;
  80584. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  80585. + hcchar.b.mps = 64;
  80586. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  80587. + hcchar.b.epdir = 0; /* OUT */
  80588. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  80589. + hcchar.d32);
  80590. +
  80591. + /* Remove the host channel from the free list. */
  80592. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  80593. +
  80594. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  80595. +
  80596. + return hc->hc_num;
  80597. +}
  80598. +
  80599. +/** Release hc after performing LPM transaction */
  80600. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  80601. +{
  80602. + dwc_hc_t *hc;
  80603. + glpmcfg_data_t lpmcfg;
  80604. + uint8_t hc_num;
  80605. +
  80606. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  80607. + hc_num = lpmcfg.b.lpm_chan_index;
  80608. +
  80609. + hc = hcd->hc_ptr_array[hc_num];
  80610. +
  80611. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  80612. + /* Return host channel to free list */
  80613. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  80614. +}
  80615. +
  80616. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  80617. + uint8_t bRemoteWake)
  80618. +{
  80619. + glpmcfg_data_t lpmcfg;
  80620. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  80621. + int channel;
  80622. +
  80623. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  80624. + if (channel < 0) {
  80625. + return channel;
  80626. + }
  80627. +
  80628. + pcgcctl.b.enbl_sleep_gating = 1;
  80629. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  80630. +
  80631. + /* Read LPM config register */
  80632. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  80633. +
  80634. + /* Program LPM transaction fields */
  80635. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  80636. + lpmcfg.b.hird = hird;
  80637. + lpmcfg.b.hird_thres = 0x1c;
  80638. + lpmcfg.b.lpm_chan_index = channel;
  80639. + lpmcfg.b.en_utmi_sleep = 1;
  80640. + /* Program LPM config register */
  80641. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  80642. +
  80643. + /* Send LPM transaction */
  80644. + lpmcfg.b.send_lpm = 1;
  80645. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  80646. +
  80647. + return 0;
  80648. +}
  80649. +
  80650. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  80651. +
  80652. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  80653. +{
  80654. + int retval;
  80655. +
  80656. + if (port != 1) {
  80657. + return -DWC_E_INVALID;
  80658. + }
  80659. +
  80660. + retval = (hcd->flags.b.port_connect_status_change ||
  80661. + hcd->flags.b.port_reset_change ||
  80662. + hcd->flags.b.port_enable_change ||
  80663. + hcd->flags.b.port_suspend_change ||
  80664. + hcd->flags.b.port_over_current_change);
  80665. +#ifdef DEBUG
  80666. + if (retval) {
  80667. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  80668. + " Root port status changed\n");
  80669. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  80670. + hcd->flags.b.port_connect_status_change);
  80671. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  80672. + hcd->flags.b.port_reset_change);
  80673. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  80674. + hcd->flags.b.port_enable_change);
  80675. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  80676. + hcd->flags.b.port_suspend_change);
  80677. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  80678. + hcd->flags.b.port_over_current_change);
  80679. + }
  80680. +#endif
  80681. + return retval;
  80682. +}
  80683. +
  80684. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  80685. +{
  80686. + hfnum_data_t hfnum;
  80687. + hfnum.d32 =
  80688. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  80689. + hfnum);
  80690. +
  80691. +#ifdef DEBUG_SOF
  80692. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  80693. + hfnum.b.frnum);
  80694. +#endif
  80695. + return hfnum.b.frnum;
  80696. +}
  80697. +
  80698. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  80699. + struct dwc_otg_hcd_function_ops *fops)
  80700. +{
  80701. + int retval = 0;
  80702. +
  80703. + hcd->fops = fops;
  80704. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  80705. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  80706. + dwc_otg_hcd_reinit(hcd);
  80707. + } else {
  80708. + retval = -DWC_E_NO_DEVICE;
  80709. + }
  80710. +
  80711. + return retval;
  80712. +}
  80713. +
  80714. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  80715. +{
  80716. + return hcd->priv;
  80717. +}
  80718. +
  80719. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  80720. +{
  80721. + hcd->priv = priv_data;
  80722. +}
  80723. +
  80724. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  80725. +{
  80726. + return hcd->otg_port;
  80727. +}
  80728. +
  80729. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  80730. +{
  80731. + uint32_t is_b_host;
  80732. + if (hcd->core_if->op_state == B_HOST) {
  80733. + is_b_host = 1;
  80734. + } else {
  80735. + is_b_host = 0;
  80736. + }
  80737. +
  80738. + return is_b_host;
  80739. +}
  80740. +
  80741. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  80742. + int iso_desc_count, int atomic_alloc)
  80743. +{
  80744. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  80745. + uint32_t size;
  80746. +
  80747. + size =
  80748. + sizeof(*dwc_otg_urb) +
  80749. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  80750. + if (atomic_alloc)
  80751. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  80752. + else
  80753. + dwc_otg_urb = DWC_ALLOC(size);
  80754. +
  80755. + if (dwc_otg_urb)
  80756. + dwc_otg_urb->packet_count = iso_desc_count;
  80757. + else {
  80758. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  80759. + "%salloc of %db failed\n",
  80760. + atomic_alloc?"atomic ":"", size);
  80761. + }
  80762. + return dwc_otg_urb;
  80763. +}
  80764. +
  80765. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  80766. + uint8_t dev_addr, uint8_t ep_num,
  80767. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  80768. +{
  80769. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  80770. + ep_type, ep_dir, mps);
  80771. +#if 0
  80772. + DWC_PRINTF
  80773. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  80774. + dev_addr, ep_num, ep_dir, ep_type, mps);
  80775. +#endif
  80776. +}
  80777. +
  80778. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  80779. + void *urb_handle, void *buf, dwc_dma_t dma,
  80780. + uint32_t buflen, void *setup_packet,
  80781. + dwc_dma_t setup_dma, uint32_t flags,
  80782. + uint16_t interval)
  80783. +{
  80784. + dwc_otg_urb->priv = urb_handle;
  80785. + dwc_otg_urb->buf = buf;
  80786. + dwc_otg_urb->dma = dma;
  80787. + dwc_otg_urb->length = buflen;
  80788. + dwc_otg_urb->setup_packet = setup_packet;
  80789. + dwc_otg_urb->setup_dma = setup_dma;
  80790. + dwc_otg_urb->flags = flags;
  80791. + dwc_otg_urb->interval = interval;
  80792. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  80793. +}
  80794. +
  80795. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  80796. +{
  80797. + return dwc_otg_urb->status;
  80798. +}
  80799. +
  80800. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  80801. +{
  80802. + return dwc_otg_urb->actual_length;
  80803. +}
  80804. +
  80805. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  80806. +{
  80807. + return dwc_otg_urb->error_count;
  80808. +}
  80809. +
  80810. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  80811. + int desc_num, uint32_t offset,
  80812. + uint32_t length)
  80813. +{
  80814. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  80815. + dwc_otg_urb->iso_descs[desc_num].length = length;
  80816. +}
  80817. +
  80818. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  80819. + int desc_num)
  80820. +{
  80821. + return dwc_otg_urb->iso_descs[desc_num].status;
  80822. +}
  80823. +
  80824. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  80825. + dwc_otg_urb, int desc_num)
  80826. +{
  80827. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  80828. +}
  80829. +
  80830. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  80831. +{
  80832. + int allocated = 0;
  80833. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  80834. +
  80835. + if (qh) {
  80836. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  80837. + allocated = 1;
  80838. + }
  80839. + }
  80840. + return allocated;
  80841. +}
  80842. +
  80843. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  80844. +{
  80845. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  80846. + int freed = 0;
  80847. + DWC_ASSERT(qh, "qh is not allocated\n");
  80848. +
  80849. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  80850. + freed = 1;
  80851. + }
  80852. +
  80853. + return freed;
  80854. +}
  80855. +
  80856. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  80857. +{
  80858. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  80859. + DWC_ASSERT(qh, "qh is not allocated\n");
  80860. + return qh->usecs;
  80861. +}
  80862. +
  80863. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  80864. +{
  80865. +#ifdef DEBUG
  80866. + int num_channels;
  80867. + int i;
  80868. + gnptxsts_data_t np_tx_status;
  80869. + hptxsts_data_t p_tx_status;
  80870. +
  80871. + num_channels = hcd->core_if->core_params->host_channels;
  80872. + DWC_PRINTF("\n");
  80873. + DWC_PRINTF
  80874. + ("************************************************************\n");
  80875. + DWC_PRINTF("HCD State:\n");
  80876. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  80877. + for (i = 0; i < num_channels; i++) {
  80878. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  80879. + DWC_PRINTF(" Channel %d:\n", i);
  80880. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  80881. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  80882. + DWC_PRINTF(" speed: %d\n", hc->speed);
  80883. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  80884. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  80885. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  80886. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  80887. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  80888. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  80889. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  80890. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  80891. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  80892. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  80893. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  80894. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  80895. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  80896. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  80897. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  80898. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  80899. + DWC_PRINTF(" requests: %d\n", hc->requests);
  80900. + DWC_PRINTF(" qh: %p\n", hc->qh);
  80901. + if (hc->xfer_started) {
  80902. + hfnum_data_t hfnum;
  80903. + hcchar_data_t hcchar;
  80904. + hctsiz_data_t hctsiz;
  80905. + hcint_data_t hcint;
  80906. + hcintmsk_data_t hcintmsk;
  80907. + hfnum.d32 =
  80908. + DWC_READ_REG32(&hcd->core_if->
  80909. + host_if->host_global_regs->hfnum);
  80910. + hcchar.d32 =
  80911. + DWC_READ_REG32(&hcd->core_if->host_if->
  80912. + hc_regs[i]->hcchar);
  80913. + hctsiz.d32 =
  80914. + DWC_READ_REG32(&hcd->core_if->host_if->
  80915. + hc_regs[i]->hctsiz);
  80916. + hcint.d32 =
  80917. + DWC_READ_REG32(&hcd->core_if->host_if->
  80918. + hc_regs[i]->hcint);
  80919. + hcintmsk.d32 =
  80920. + DWC_READ_REG32(&hcd->core_if->host_if->
  80921. + hc_regs[i]->hcintmsk);
  80922. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  80923. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  80924. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  80925. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  80926. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  80927. + }
  80928. + if (hc->xfer_started && hc->qh) {
  80929. + dwc_otg_qtd_t *qtd;
  80930. + dwc_otg_hcd_urb_t *urb;
  80931. +
  80932. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  80933. + if (!qtd->in_process)
  80934. + break;
  80935. +
  80936. + urb = qtd->urb;
  80937. + DWC_PRINTF(" URB Info:\n");
  80938. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  80939. + if (urb) {
  80940. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  80941. + dwc_otg_hcd_get_dev_addr(&urb->
  80942. + pipe_info),
  80943. + dwc_otg_hcd_get_ep_num(&urb->
  80944. + pipe_info),
  80945. + dwc_otg_hcd_is_pipe_in(&urb->
  80946. + pipe_info) ?
  80947. + "IN" : "OUT");
  80948. + DWC_PRINTF(" Max packet size: %d\n",
  80949. + dwc_otg_hcd_get_mps(&urb->
  80950. + pipe_info));
  80951. + DWC_PRINTF(" transfer_buffer: %p\n",
  80952. + urb->buf);
  80953. + DWC_PRINTF(" transfer_dma: %p\n",
  80954. + (void *)urb->dma);
  80955. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  80956. + urb->length);
  80957. + DWC_PRINTF(" actual_length: %d\n",
  80958. + urb->actual_length);
  80959. + }
  80960. + }
  80961. + }
  80962. + }
  80963. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  80964. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  80965. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  80966. + np_tx_status.d32 =
  80967. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  80968. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  80969. + np_tx_status.b.nptxqspcavail);
  80970. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  80971. + np_tx_status.b.nptxfspcavail);
  80972. + p_tx_status.d32 =
  80973. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  80974. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  80975. + p_tx_status.b.ptxqspcavail);
  80976. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  80977. + dwc_otg_hcd_dump_frrem(hcd);
  80978. + dwc_otg_dump_global_registers(hcd->core_if);
  80979. + dwc_otg_dump_host_registers(hcd->core_if);
  80980. + DWC_PRINTF
  80981. + ("************************************************************\n");
  80982. + DWC_PRINTF("\n");
  80983. +#endif
  80984. +}
  80985. +
  80986. +#ifdef DEBUG
  80987. +void dwc_print_setup_data(uint8_t * setup)
  80988. +{
  80989. + int i;
  80990. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  80991. + DWC_PRINTF("Setup Data = MSB ");
  80992. + for (i = 7; i >= 0; i--)
  80993. + DWC_PRINTF("%02x ", setup[i]);
  80994. + DWC_PRINTF("\n");
  80995. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  80996. + (setup[0] & 0x80) ? "Device-to-Host" :
  80997. + "Host-to-Device");
  80998. + DWC_PRINTF(" bmRequestType Type = ");
  80999. + switch ((setup[0] & 0x60) >> 5) {
  81000. + case 0:
  81001. + DWC_PRINTF("Standard\n");
  81002. + break;
  81003. + case 1:
  81004. + DWC_PRINTF("Class\n");
  81005. + break;
  81006. + case 2:
  81007. + DWC_PRINTF("Vendor\n");
  81008. + break;
  81009. + case 3:
  81010. + DWC_PRINTF("Reserved\n");
  81011. + break;
  81012. + }
  81013. + DWC_PRINTF(" bmRequestType Recipient = ");
  81014. + switch (setup[0] & 0x1f) {
  81015. + case 0:
  81016. + DWC_PRINTF("Device\n");
  81017. + break;
  81018. + case 1:
  81019. + DWC_PRINTF("Interface\n");
  81020. + break;
  81021. + case 2:
  81022. + DWC_PRINTF("Endpoint\n");
  81023. + break;
  81024. + case 3:
  81025. + DWC_PRINTF("Other\n");
  81026. + break;
  81027. + default:
  81028. + DWC_PRINTF("Reserved\n");
  81029. + break;
  81030. + }
  81031. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  81032. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  81033. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  81034. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  81035. + }
  81036. +}
  81037. +#endif
  81038. +
  81039. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  81040. +{
  81041. +#if 0
  81042. + DWC_PRINTF("Frame remaining at SOF:\n");
  81043. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81044. + hcd->frrem_samples, hcd->frrem_accum,
  81045. + (hcd->frrem_samples > 0) ?
  81046. + hcd->frrem_accum / hcd->frrem_samples : 0);
  81047. +
  81048. + DWC_PRINTF("\n");
  81049. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  81050. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81051. + hcd->core_if->hfnum_7_samples,
  81052. + hcd->core_if->hfnum_7_frrem_accum,
  81053. + (hcd->core_if->hfnum_7_samples >
  81054. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  81055. + hcd->core_if->hfnum_7_samples : 0);
  81056. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  81057. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81058. + hcd->core_if->hfnum_0_samples,
  81059. + hcd->core_if->hfnum_0_frrem_accum,
  81060. + (hcd->core_if->hfnum_0_samples >
  81061. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  81062. + hcd->core_if->hfnum_0_samples : 0);
  81063. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  81064. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81065. + hcd->core_if->hfnum_other_samples,
  81066. + hcd->core_if->hfnum_other_frrem_accum,
  81067. + (hcd->core_if->hfnum_other_samples >
  81068. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  81069. + hcd->core_if->hfnum_other_samples : 0);
  81070. +
  81071. + DWC_PRINTF("\n");
  81072. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  81073. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81074. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  81075. + (hcd->hfnum_7_samples_a > 0) ?
  81076. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  81077. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  81078. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81079. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  81080. + (hcd->hfnum_0_samples_a > 0) ?
  81081. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  81082. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  81083. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81084. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  81085. + (hcd->hfnum_other_samples_a > 0) ?
  81086. + hcd->hfnum_other_frrem_accum_a /
  81087. + hcd->hfnum_other_samples_a : 0);
  81088. +
  81089. + DWC_PRINTF("\n");
  81090. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  81091. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81092. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  81093. + (hcd->hfnum_7_samples_b > 0) ?
  81094. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  81095. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  81096. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81097. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  81098. + (hcd->hfnum_0_samples_b > 0) ?
  81099. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  81100. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  81101. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  81102. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  81103. + (hcd->hfnum_other_samples_b > 0) ?
  81104. + hcd->hfnum_other_frrem_accum_b /
  81105. + hcd->hfnum_other_samples_b : 0);
  81106. +#endif
  81107. +}
  81108. +
  81109. +#endif /* DWC_DEVICE_ONLY */
  81110. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  81111. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 1970-01-01 01:00:00.000000000 +0100
  81112. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c 2014-09-14 19:04:13.000000000 +0200
  81113. @@ -0,0 +1,1132 @@
  81114. +/*==========================================================================
  81115. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  81116. + * $Revision: #10 $
  81117. + * $Date: 2011/10/20 $
  81118. + * $Change: 1869464 $
  81119. + *
  81120. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  81121. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  81122. + * otherwise expressly agreed to in writing between Synopsys and you.
  81123. + *
  81124. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  81125. + * any End User Software License Agreement or Agreement for Licensed Product
  81126. + * with Synopsys or any supplement thereto. You are permitted to use and
  81127. + * redistribute this Software in source and binary forms, with or without
  81128. + * modification, provided that redistributions of source code must retain this
  81129. + * notice. You may not view, use, disclose, copy or distribute this file or
  81130. + * any information contained herein except pursuant to this license grant from
  81131. + * Synopsys. If you do not agree with this notice, including the disclaimer
  81132. + * below, then you are not authorized to use the Software.
  81133. + *
  81134. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  81135. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  81136. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  81137. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  81138. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  81139. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  81140. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  81141. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  81142. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  81143. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  81144. + * DAMAGE.
  81145. + * ========================================================================== */
  81146. +#ifndef DWC_DEVICE_ONLY
  81147. +
  81148. +/** @file
  81149. + * This file contains Descriptor DMA support implementation for host mode.
  81150. + */
  81151. +
  81152. +#include "dwc_otg_hcd.h"
  81153. +#include "dwc_otg_regs.h"
  81154. +
  81155. +extern bool microframe_schedule;
  81156. +
  81157. +static inline uint8_t frame_list_idx(uint16_t frame)
  81158. +{
  81159. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  81160. +}
  81161. +
  81162. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  81163. +{
  81164. + return (idx + inc) &
  81165. + (((speed ==
  81166. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  81167. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  81168. +}
  81169. +
  81170. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  81171. +{
  81172. + return (idx - inc) &
  81173. + (((speed ==
  81174. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  81175. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  81176. +}
  81177. +
  81178. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  81179. +{
  81180. + return (((qh->ep_type == UE_ISOCHRONOUS)
  81181. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  81182. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  81183. +}
  81184. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  81185. +{
  81186. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  81187. + ? ((qh->interval + 8 - 1) / 8)
  81188. + : qh->interval);
  81189. +}
  81190. +
  81191. +static int desc_list_alloc(dwc_otg_qh_t * qh)
  81192. +{
  81193. + int retval = 0;
  81194. +
  81195. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  81196. + DWC_DMA_ALLOC(sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  81197. + &qh->desc_list_dma);
  81198. +
  81199. + if (!qh->desc_list) {
  81200. + retval = -DWC_E_NO_MEMORY;
  81201. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  81202. +
  81203. + }
  81204. +
  81205. + dwc_memset(qh->desc_list, 0x00,
  81206. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  81207. +
  81208. + qh->n_bytes =
  81209. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  81210. +
  81211. + if (!qh->n_bytes) {
  81212. + retval = -DWC_E_NO_MEMORY;
  81213. + DWC_ERROR
  81214. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  81215. + __func__);
  81216. +
  81217. + }
  81218. + return retval;
  81219. +
  81220. +}
  81221. +
  81222. +static void desc_list_free(dwc_otg_qh_t * qh)
  81223. +{
  81224. + if (qh->desc_list) {
  81225. + DWC_DMA_FREE(max_desc_num(qh), qh->desc_list,
  81226. + qh->desc_list_dma);
  81227. + qh->desc_list = NULL;
  81228. + }
  81229. +
  81230. + if (qh->n_bytes) {
  81231. + DWC_FREE(qh->n_bytes);
  81232. + qh->n_bytes = NULL;
  81233. + }
  81234. +}
  81235. +
  81236. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  81237. +{
  81238. + int retval = 0;
  81239. + if (hcd->frame_list)
  81240. + return 0;
  81241. +
  81242. + hcd->frame_list = DWC_DMA_ALLOC(4 * MAX_FRLIST_EN_NUM,
  81243. + &hcd->frame_list_dma);
  81244. + if (!hcd->frame_list) {
  81245. + retval = -DWC_E_NO_MEMORY;
  81246. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  81247. + }
  81248. +
  81249. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  81250. +
  81251. + return retval;
  81252. +}
  81253. +
  81254. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  81255. +{
  81256. + if (!hcd->frame_list)
  81257. + return;
  81258. +
  81259. + DWC_DMA_FREE(4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  81260. + hcd->frame_list = NULL;
  81261. +}
  81262. +
  81263. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  81264. +{
  81265. +
  81266. + hcfg_data_t hcfg;
  81267. +
  81268. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  81269. +
  81270. + if (hcfg.b.perschedena) {
  81271. + /* already enabled */
  81272. + return;
  81273. + }
  81274. +
  81275. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  81276. + hcd->frame_list_dma);
  81277. +
  81278. + switch (fr_list_en) {
  81279. + case 64:
  81280. + hcfg.b.frlisten = 3;
  81281. + break;
  81282. + case 32:
  81283. + hcfg.b.frlisten = 2;
  81284. + break;
  81285. + case 16:
  81286. + hcfg.b.frlisten = 1;
  81287. + break;
  81288. + case 8:
  81289. + hcfg.b.frlisten = 0;
  81290. + break;
  81291. + default:
  81292. + break;
  81293. + }
  81294. +
  81295. + hcfg.b.perschedena = 1;
  81296. +
  81297. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  81298. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  81299. +
  81300. +}
  81301. +
  81302. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  81303. +{
  81304. + hcfg_data_t hcfg;
  81305. +
  81306. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  81307. +
  81308. + if (!hcfg.b.perschedena) {
  81309. + /* already disabled */
  81310. + return;
  81311. + }
  81312. + hcfg.b.perschedena = 0;
  81313. +
  81314. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  81315. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  81316. +}
  81317. +
  81318. +/*
  81319. + * Activates/Deactivates FrameList entries for the channel
  81320. + * based on endpoint servicing period.
  81321. + */
  81322. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  81323. +{
  81324. + uint16_t i, j, inc;
  81325. + dwc_hc_t *hc = NULL;
  81326. +
  81327. + if (!qh->channel) {
  81328. + DWC_ERROR("qh->channel = %p", qh->channel);
  81329. + return;
  81330. + }
  81331. +
  81332. + if (!hcd) {
  81333. + DWC_ERROR("------hcd = %p", hcd);
  81334. + return;
  81335. + }
  81336. +
  81337. + if (!hcd->frame_list) {
  81338. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  81339. + return;
  81340. + }
  81341. +
  81342. + hc = qh->channel;
  81343. + inc = frame_incr_val(qh);
  81344. + if (qh->ep_type == UE_ISOCHRONOUS)
  81345. + i = frame_list_idx(qh->sched_frame);
  81346. + else
  81347. + i = 0;
  81348. +
  81349. + j = i;
  81350. + do {
  81351. + if (enable)
  81352. + hcd->frame_list[j] |= (1 << hc->hc_num);
  81353. + else
  81354. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  81355. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  81356. + }
  81357. + while (j != i);
  81358. + if (!enable)
  81359. + return;
  81360. + hc->schinfo = 0;
  81361. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  81362. + j = 1;
  81363. + /* TODO - check this */
  81364. + inc = (8 + qh->interval - 1) / qh->interval;
  81365. + for (i = 0; i < inc; i++) {
  81366. + hc->schinfo |= j;
  81367. + j = j << qh->interval;
  81368. + }
  81369. + } else {
  81370. + hc->schinfo = 0xff;
  81371. + }
  81372. +}
  81373. +
  81374. +#if 1
  81375. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  81376. +{
  81377. + int i = 0;
  81378. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  81379. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  81380. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  81381. + if (!(i % 8) && i)
  81382. + DWC_PRINTF("\n");
  81383. + }
  81384. + DWC_PRINTF("\n----\n");
  81385. +
  81386. +}
  81387. +#endif
  81388. +
  81389. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  81390. +{
  81391. + dwc_irqflags_t flags;
  81392. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  81393. +
  81394. + dwc_hc_t *hc = qh->channel;
  81395. + if (dwc_qh_is_non_per(qh)) {
  81396. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  81397. + if (!microframe_schedule)
  81398. + hcd->non_periodic_channels--;
  81399. + else
  81400. + hcd->available_host_channels++;
  81401. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  81402. + } else
  81403. + update_frame_list(hcd, qh, 0);
  81404. +
  81405. + /*
  81406. + * The condition is added to prevent double cleanup try in case of device
  81407. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  81408. + */
  81409. + if (hc->qh) {
  81410. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  81411. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  81412. + hc->qh = NULL;
  81413. + }
  81414. +
  81415. + qh->channel = NULL;
  81416. + qh->ntd = 0;
  81417. +
  81418. + if (qh->desc_list) {
  81419. + dwc_memset(qh->desc_list, 0x00,
  81420. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  81421. + }
  81422. +}
  81423. +
  81424. +/**
  81425. + * Initializes a QH structure's Descriptor DMA related members.
  81426. + * Allocates memory for descriptor list.
  81427. + * On first periodic QH, allocates memory for FrameList
  81428. + * and enables periodic scheduling.
  81429. + *
  81430. + * @param hcd The HCD state structure for the DWC OTG controller.
  81431. + * @param qh The QH to init.
  81432. + *
  81433. + * @return 0 if successful, negative error code otherwise.
  81434. + */
  81435. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  81436. +{
  81437. + int retval = 0;
  81438. +
  81439. + if (qh->do_split) {
  81440. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  81441. + return -1;
  81442. + }
  81443. +
  81444. + retval = desc_list_alloc(qh);
  81445. +
  81446. + if ((retval == 0)
  81447. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  81448. + if (!hcd->frame_list) {
  81449. + retval = frame_list_alloc(hcd);
  81450. + /* Enable periodic schedule on first periodic QH */
  81451. + if (retval == 0)
  81452. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  81453. + }
  81454. + }
  81455. +
  81456. + qh->ntd = 0;
  81457. +
  81458. + return retval;
  81459. +}
  81460. +
  81461. +/**
  81462. + * Frees descriptor list memory associated with the QH.
  81463. + * If QH is periodic and the last, frees FrameList memory
  81464. + * and disables periodic scheduling.
  81465. + *
  81466. + * @param hcd The HCD state structure for the DWC OTG controller.
  81467. + * @param qh The QH to init.
  81468. + */
  81469. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  81470. +{
  81471. + desc_list_free(qh);
  81472. +
  81473. + /*
  81474. + * Channel still assigned due to some reasons.
  81475. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  81476. + * ChHalted interrupt to release the channel. Afterwards
  81477. + * when it comes here from endpoint disable routine
  81478. + * channel remains assigned.
  81479. + */
  81480. + if (qh->channel)
  81481. + release_channel_ddma(hcd, qh);
  81482. +
  81483. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  81484. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  81485. +
  81486. + per_sched_disable(hcd);
  81487. + frame_list_free(hcd);
  81488. + }
  81489. +}
  81490. +
  81491. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  81492. +{
  81493. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  81494. + /*
  81495. + * Descriptor set(8 descriptors) index
  81496. + * which is 8-aligned.
  81497. + */
  81498. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  81499. + } else {
  81500. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  81501. + }
  81502. +}
  81503. +
  81504. +/*
  81505. + * Determine starting frame for Isochronous transfer.
  81506. + * Few frames skipped to prevent race condition with HC.
  81507. + */
  81508. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  81509. + uint8_t * skip_frames)
  81510. +{
  81511. + uint16_t frame = 0;
  81512. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  81513. +
  81514. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  81515. +
  81516. + /*
  81517. + * skip_frames is used to limit activated descriptors number
  81518. + * to avoid the situation when HC services the last activated
  81519. + * descriptor firstly.
  81520. + * Example for FS:
  81521. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  81522. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  81523. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  81524. + * list will be fully programmed with Active descriptors and it is possible
  81525. + * case(rare) that the latest descriptor(considering rollback) corresponding
  81526. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  81527. + * up to 11 uframes(16 in the code) may be skipped.
  81528. + */
  81529. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  81530. + /*
  81531. + * Consider uframe counter also, to start xfer asap.
  81532. + * If half of the frame elapsed skip 2 frames otherwise
  81533. + * just 1 frame.
  81534. + * Starting descriptor index must be 8-aligned, so
  81535. + * if the current frame is near to complete the next one
  81536. + * is skipped as well.
  81537. + */
  81538. +
  81539. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  81540. + *skip_frames = 2 * 8;
  81541. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  81542. + } else {
  81543. + *skip_frames = 1 * 8;
  81544. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  81545. + }
  81546. +
  81547. + frame = dwc_full_frame_num(frame);
  81548. + } else {
  81549. + /*
  81550. + * Two frames are skipped for FS - the current and the next.
  81551. + * But for descriptor programming, 1 frame(descriptor) is enough,
  81552. + * see example above.
  81553. + */
  81554. + *skip_frames = 1;
  81555. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  81556. + }
  81557. +
  81558. + return frame;
  81559. +}
  81560. +
  81561. +/*
  81562. + * Calculate initial descriptor index for isochronous transfer
  81563. + * based on scheduled frame.
  81564. + */
  81565. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  81566. +{
  81567. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  81568. + uint8_t skip_frames = 0;
  81569. + /*
  81570. + * With current ISOC processing algorithm the channel is being
  81571. + * released when no more QTDs in the list(qh->ntd == 0).
  81572. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  81573. + *
  81574. + * So qh->channel != NULL branch is not used and just not removed from the
  81575. + * source file. It is required for another possible approach which is,
  81576. + * do not disable and release the channel when ISOC session completed,
  81577. + * just move QH to inactive schedule until new QTD arrives.
  81578. + * On new QTD, the QH moved back to 'ready' schedule,
  81579. + * starting frame and therefore starting desc_index are recalculated.
  81580. + * In this case channel is released only on ep_disable.
  81581. + */
  81582. +
  81583. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  81584. + if (qh->channel) {
  81585. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  81586. + /*
  81587. + * Calculate initial descriptor index based on FrameList current bitmap
  81588. + * and servicing period.
  81589. + */
  81590. + fr_idx_tmp = frame_list_idx(frame);
  81591. + fr_idx =
  81592. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  81593. + fr_idx_tmp)
  81594. + % frame_incr_val(qh);
  81595. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  81596. + } else {
  81597. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  81598. + fr_idx = frame_list_idx(qh->sched_frame);
  81599. + }
  81600. +
  81601. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  81602. +
  81603. + return skip_frames;
  81604. +}
  81605. +
  81606. +#define ISOC_URB_GIVEBACK_ASAP
  81607. +
  81608. +#define MAX_ISOC_XFER_SIZE_FS 1023
  81609. +#define MAX_ISOC_XFER_SIZE_HS 3072
  81610. +#define DESCNUM_THRESHOLD 4
  81611. +
  81612. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  81613. + uint8_t skip_frames)
  81614. +{
  81615. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  81616. + dwc_otg_qtd_t *qtd;
  81617. + dwc_otg_host_dma_desc_t *dma_desc;
  81618. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  81619. +
  81620. + idx = qh->td_last;
  81621. + inc = qh->interval;
  81622. + n_desc = 0;
  81623. +
  81624. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  81625. + if (skip_frames && !qh->channel)
  81626. + ntd_max = ntd_max - skip_frames / qh->interval;
  81627. +
  81628. + max_xfer_size =
  81629. + (qh->dev_speed ==
  81630. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  81631. + MAX_ISOC_XFER_SIZE_FS;
  81632. +
  81633. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  81634. + while ((qh->ntd < ntd_max)
  81635. + && (qtd->isoc_frame_index_last <
  81636. + qtd->urb->packet_count)) {
  81637. +
  81638. + dma_desc = &qh->desc_list[idx];
  81639. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  81640. +
  81641. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  81642. +
  81643. + if (frame_desc->length > max_xfer_size)
  81644. + qh->n_bytes[idx] = max_xfer_size;
  81645. + else
  81646. + qh->n_bytes[idx] = frame_desc->length;
  81647. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  81648. + dma_desc->status.b_isoc.a = 1;
  81649. + dma_desc->status.b_isoc.sts = 0;
  81650. +
  81651. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  81652. +
  81653. + qh->ntd++;
  81654. +
  81655. + qtd->isoc_frame_index_last++;
  81656. +
  81657. +#ifdef ISOC_URB_GIVEBACK_ASAP
  81658. + /*
  81659. + * Set IOC for each descriptor corresponding to the
  81660. + * last frame of the URB.
  81661. + */
  81662. + if (qtd->isoc_frame_index_last ==
  81663. + qtd->urb->packet_count)
  81664. + dma_desc->status.b_isoc.ioc = 1;
  81665. +
  81666. +#endif
  81667. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  81668. + n_desc++;
  81669. +
  81670. + }
  81671. + qtd->in_process = 1;
  81672. + }
  81673. +
  81674. + qh->td_last = idx;
  81675. +
  81676. +#ifdef ISOC_URB_GIVEBACK_ASAP
  81677. + /* Set IOC for the last descriptor if descriptor list is full */
  81678. + if (qh->ntd == ntd_max) {
  81679. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  81680. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  81681. + }
  81682. +#else
  81683. + /*
  81684. + * Set IOC bit only for one descriptor.
  81685. + * Always try to be ahead of HW processing,
  81686. + * i.e. on IOC generation driver activates next descriptors but
  81687. + * core continues to process descriptors followed the one with IOC set.
  81688. + */
  81689. +
  81690. + if (n_desc > DESCNUM_THRESHOLD) {
  81691. + /*
  81692. + * Move IOC "up". Required even if there is only one QTD
  81693. + * in the list, cause QTDs migth continue to be queued,
  81694. + * but during the activation it was only one queued.
  81695. + * Actually more than one QTD might be in the list if this function called
  81696. + * from XferCompletion - QTDs was queued during HW processing of the previous
  81697. + * descriptor chunk.
  81698. + */
  81699. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  81700. + } else {
  81701. + /*
  81702. + * Set the IOC for the latest descriptor
  81703. + * if either number of descriptor is not greather than threshold
  81704. + * or no more new descriptors activated.
  81705. + */
  81706. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  81707. + }
  81708. +
  81709. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  81710. +#endif
  81711. +}
  81712. +
  81713. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  81714. +{
  81715. +
  81716. + dwc_hc_t *hc;
  81717. + dwc_otg_host_dma_desc_t *dma_desc;
  81718. + dwc_otg_qtd_t *qtd;
  81719. + int num_packets, len, n_desc = 0;
  81720. +
  81721. + hc = qh->channel;
  81722. +
  81723. + /*
  81724. + * Start with hc->xfer_buff initialized in
  81725. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  81726. + * this pointer re-assigned to the buffer of the currently processed QTD.
  81727. + * For non-SG request there is always one QTD active.
  81728. + */
  81729. +
  81730. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  81731. +
  81732. + if (n_desc) {
  81733. + /* SG request - more than 1 QTDs */
  81734. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  81735. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  81736. + }
  81737. +
  81738. + qtd->n_desc = 0;
  81739. +
  81740. + do {
  81741. + dma_desc = &qh->desc_list[n_desc];
  81742. + len = hc->xfer_len;
  81743. +
  81744. + if (len > MAX_DMA_DESC_SIZE)
  81745. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  81746. +
  81747. + if (hc->ep_is_in) {
  81748. + if (len > 0) {
  81749. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  81750. + } else {
  81751. + /* Need 1 packet for transfer length of 0. */
  81752. + num_packets = 1;
  81753. + }
  81754. + /* Always program an integral # of max packets for IN transfers. */
  81755. + len = num_packets * hc->max_packet;
  81756. + }
  81757. +
  81758. + dma_desc->status.b.n_bytes = len;
  81759. +
  81760. + qh->n_bytes[n_desc] = len;
  81761. +
  81762. + if ((qh->ep_type == UE_CONTROL)
  81763. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  81764. + dma_desc->status.b.sup = 1; /* Setup Packet */
  81765. +
  81766. + dma_desc->status.b.a = 1; /* Active descriptor */
  81767. + dma_desc->status.b.sts = 0;
  81768. +
  81769. + dma_desc->buf =
  81770. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  81771. +
  81772. + /*
  81773. + * Last descriptor(or single) of IN transfer
  81774. + * with actual size less than MaxPacket.
  81775. + */
  81776. + if (len > hc->xfer_len) {
  81777. + hc->xfer_len = 0;
  81778. + } else {
  81779. + hc->xfer_buff += len;
  81780. + hc->xfer_len -= len;
  81781. + }
  81782. +
  81783. + qtd->n_desc++;
  81784. + n_desc++;
  81785. + }
  81786. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  81787. +
  81788. +
  81789. + qtd->in_process = 1;
  81790. +
  81791. + if (qh->ep_type == UE_CONTROL)
  81792. + break;
  81793. +
  81794. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  81795. + break;
  81796. + }
  81797. +
  81798. + if (n_desc) {
  81799. + /* Request Transfer Complete interrupt for the last descriptor */
  81800. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  81801. + /* End of List indicator */
  81802. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  81803. +
  81804. + hc->ntd = n_desc;
  81805. + }
  81806. +}
  81807. +
  81808. +/**
  81809. + * For Control and Bulk endpoints initializes descriptor list
  81810. + * and starts the transfer.
  81811. + *
  81812. + * For Interrupt and Isochronous endpoints initializes descriptor list
  81813. + * then updates FrameList, marking appropriate entries as active.
  81814. + * In case of Isochronous, the starting descriptor index is calculated based
  81815. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  81816. + * Then starts the transfer via enabling the channel.
  81817. + * For Isochronous endpoint the channel is not halted on XferComplete
  81818. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  81819. + *
  81820. + * @param hcd The HCD state structure for the DWC OTG controller.
  81821. + * @param qh The QH to init.
  81822. + *
  81823. + * @return 0 if successful, negative error code otherwise.
  81824. + */
  81825. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  81826. +{
  81827. + /* Channel is already assigned */
  81828. + dwc_hc_t *hc = qh->channel;
  81829. + uint8_t skip_frames = 0;
  81830. +
  81831. + switch (hc->ep_type) {
  81832. + case DWC_OTG_EP_TYPE_CONTROL:
  81833. + case DWC_OTG_EP_TYPE_BULK:
  81834. + init_non_isoc_dma_desc(hcd, qh);
  81835. +
  81836. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  81837. + break;
  81838. + case DWC_OTG_EP_TYPE_INTR:
  81839. + init_non_isoc_dma_desc(hcd, qh);
  81840. +
  81841. + update_frame_list(hcd, qh, 1);
  81842. +
  81843. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  81844. + break;
  81845. + case DWC_OTG_EP_TYPE_ISOC:
  81846. +
  81847. + if (!qh->ntd)
  81848. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  81849. +
  81850. + init_isoc_dma_desc(hcd, qh, skip_frames);
  81851. +
  81852. + if (!hc->xfer_started) {
  81853. +
  81854. + update_frame_list(hcd, qh, 1);
  81855. +
  81856. + /*
  81857. + * Always set to max, instead of actual size.
  81858. + * Otherwise ntd will be changed with
  81859. + * channel being enabled. Not recommended.
  81860. + *
  81861. + */
  81862. + hc->ntd = max_desc_num(qh);
  81863. + /* Enable channel only once for ISOC */
  81864. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  81865. + }
  81866. +
  81867. + break;
  81868. + default:
  81869. +
  81870. + break;
  81871. + }
  81872. +}
  81873. +
  81874. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  81875. + dwc_hc_t * hc,
  81876. + dwc_otg_hc_regs_t * hc_regs,
  81877. + dwc_otg_halt_status_e halt_status)
  81878. +{
  81879. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  81880. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  81881. + dwc_otg_qh_t *qh;
  81882. + dwc_otg_host_dma_desc_t *dma_desc;
  81883. + uint16_t idx, remain;
  81884. + uint8_t urb_compl;
  81885. +
  81886. + qh = hc->qh;
  81887. + idx = qh->td_first;
  81888. +
  81889. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  81890. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  81891. + qtd->in_process = 0;
  81892. + return;
  81893. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  81894. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  81895. + /*
  81896. + * Channel is halted in these error cases.
  81897. + * Considered as serious issues.
  81898. + * Complete all URBs marking all frames as failed,
  81899. + * irrespective whether some of the descriptors(frames) succeeded or no.
  81900. + * Pass error code to completion routine as well, to
  81901. + * update urb->status, some of class drivers might use it to stop
  81902. + * queing transfer requests.
  81903. + */
  81904. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  81905. + ? (-DWC_E_IO)
  81906. + : (-DWC_E_OVERFLOW);
  81907. +
  81908. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  81909. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  81910. + frame_desc = &qtd->urb->iso_descs[idx];
  81911. + frame_desc->status = err;
  81912. + }
  81913. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  81914. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  81915. + }
  81916. + return;
  81917. + }
  81918. +
  81919. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  81920. +
  81921. + if (!qtd->in_process)
  81922. + break;
  81923. +
  81924. + urb_compl = 0;
  81925. +
  81926. + do {
  81927. +
  81928. + dma_desc = &qh->desc_list[idx];
  81929. +
  81930. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  81931. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  81932. +
  81933. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  81934. + /*
  81935. + * XactError or, unable to complete all the transactions
  81936. + * in the scheduled micro-frame/frame,
  81937. + * both indicated by DMA_DESC_STS_PKTERR.
  81938. + */
  81939. + qtd->urb->error_count++;
  81940. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  81941. + frame_desc->status = -DWC_E_PROTOCOL;
  81942. + } else {
  81943. + /* Success */
  81944. +
  81945. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  81946. + frame_desc->status = 0;
  81947. + }
  81948. +
  81949. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  81950. + /*
  81951. + * urb->status is not used for isoc transfers here.
  81952. + * The individual frame_desc status are used instead.
  81953. + */
  81954. +
  81955. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  81956. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  81957. +
  81958. + /*
  81959. + * This check is necessary because urb_dequeue can be called
  81960. + * from urb complete callback(sound driver example).
  81961. + * All pending URBs are dequeued there, so no need for
  81962. + * further processing.
  81963. + */
  81964. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  81965. + return;
  81966. + }
  81967. +
  81968. + urb_compl = 1;
  81969. +
  81970. + }
  81971. +
  81972. + qh->ntd--;
  81973. +
  81974. + /* Stop if IOC requested descriptor reached */
  81975. + if (dma_desc->status.b_isoc.ioc) {
  81976. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  81977. + goto stop_scan;
  81978. + }
  81979. +
  81980. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  81981. +
  81982. + if (urb_compl)
  81983. + break;
  81984. + }
  81985. + while (idx != qh->td_first);
  81986. + }
  81987. +stop_scan:
  81988. + qh->td_first = idx;
  81989. +}
  81990. +
  81991. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  81992. + dwc_hc_t * hc,
  81993. + dwc_otg_qtd_t * qtd,
  81994. + dwc_otg_host_dma_desc_t * dma_desc,
  81995. + dwc_otg_halt_status_e halt_status,
  81996. + uint32_t n_bytes, uint8_t * xfer_done)
  81997. +{
  81998. +
  81999. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  82000. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  82001. +
  82002. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  82003. + urb->status = -DWC_E_IO;
  82004. + return 1;
  82005. + }
  82006. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  82007. + switch (halt_status) {
  82008. + case DWC_OTG_HC_XFER_STALL:
  82009. + urb->status = -DWC_E_PIPE;
  82010. + break;
  82011. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  82012. + urb->status = -DWC_E_OVERFLOW;
  82013. + break;
  82014. + case DWC_OTG_HC_XFER_XACT_ERR:
  82015. + urb->status = -DWC_E_PROTOCOL;
  82016. + break;
  82017. + default:
  82018. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  82019. + halt_status);
  82020. + break;
  82021. + }
  82022. + return 1;
  82023. + }
  82024. +
  82025. + if (dma_desc->status.b.a == 1) {
  82026. + DWC_DEBUGPL(DBG_HCDV,
  82027. + "Active descriptor encountered on channel %d\n",
  82028. + hc->hc_num);
  82029. + return 0;
  82030. + }
  82031. +
  82032. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  82033. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  82034. + urb->actual_length += n_bytes - remain;
  82035. + if (remain || urb->actual_length == urb->length) {
  82036. + /*
  82037. + * For Control Data stage do not set urb->status=0 to prevent
  82038. + * URB callback. Set it when Status phase done. See below.
  82039. + */
  82040. + *xfer_done = 1;
  82041. + }
  82042. +
  82043. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  82044. + urb->status = 0;
  82045. + *xfer_done = 1;
  82046. + }
  82047. + /* No handling for SETUP stage */
  82048. + } else {
  82049. + /* BULK and INTR */
  82050. + urb->actual_length += n_bytes - remain;
  82051. + if (remain || urb->actual_length == urb->length) {
  82052. + urb->status = 0;
  82053. + *xfer_done = 1;
  82054. + }
  82055. + }
  82056. +
  82057. + return 0;
  82058. +}
  82059. +
  82060. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  82061. + dwc_hc_t * hc,
  82062. + dwc_otg_hc_regs_t * hc_regs,
  82063. + dwc_otg_halt_status_e halt_status)
  82064. +{
  82065. + dwc_otg_hcd_urb_t *urb = NULL;
  82066. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  82067. + dwc_otg_qh_t *qh;
  82068. + dwc_otg_host_dma_desc_t *dma_desc;
  82069. + uint32_t n_bytes, n_desc, i;
  82070. + uint8_t failed = 0, xfer_done;
  82071. +
  82072. + n_desc = 0;
  82073. +
  82074. + qh = hc->qh;
  82075. +
  82076. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  82077. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  82078. + qtd->in_process = 0;
  82079. + }
  82080. + return;
  82081. + }
  82082. +
  82083. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  82084. +
  82085. + urb = qtd->urb;
  82086. +
  82087. + n_bytes = 0;
  82088. + xfer_done = 0;
  82089. +
  82090. + for (i = 0; i < qtd->n_desc; i++) {
  82091. + dma_desc = &qh->desc_list[n_desc];
  82092. +
  82093. + n_bytes = qh->n_bytes[n_desc];
  82094. +
  82095. + failed =
  82096. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  82097. + dma_desc,
  82098. + halt_status, n_bytes,
  82099. + &xfer_done);
  82100. +
  82101. + if (failed
  82102. + || (xfer_done
  82103. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  82104. +
  82105. + hcd->fops->complete(hcd, urb->priv, urb,
  82106. + urb->status);
  82107. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  82108. +
  82109. + if (failed)
  82110. + goto stop_scan;
  82111. + } else if (qh->ep_type == UE_CONTROL) {
  82112. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  82113. + if (urb->length > 0) {
  82114. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  82115. + } else {
  82116. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  82117. + }
  82118. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  82119. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  82120. + if (xfer_done) {
  82121. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  82122. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  82123. + } else if (i + 1 == qtd->n_desc) {
  82124. + /*
  82125. + * Last descriptor for Control data stage which is
  82126. + * not completed yet.
  82127. + */
  82128. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  82129. + }
  82130. + }
  82131. + }
  82132. +
  82133. + n_desc++;
  82134. + }
  82135. +
  82136. + }
  82137. +
  82138. +stop_scan:
  82139. +
  82140. + if (qh->ep_type != UE_CONTROL) {
  82141. + /*
  82142. + * Resetting the data toggle for bulk
  82143. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  82144. + */
  82145. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  82146. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  82147. + else
  82148. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  82149. + }
  82150. +
  82151. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  82152. + hcint_data_t hcint;
  82153. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  82154. + if (hcint.b.nyet) {
  82155. + /*
  82156. + * Got a NYET on the last transaction of the transfer. It
  82157. + * means that the endpoint should be in the PING state at the
  82158. + * beginning of the next transfer.
  82159. + */
  82160. + qh->ping_state = 1;
  82161. + clear_hc_int(hc_regs, nyet);
  82162. + }
  82163. +
  82164. + }
  82165. +
  82166. +}
  82167. +
  82168. +/**
  82169. + * This function is called from interrupt handlers.
  82170. + * Scans the descriptor list, updates URB's status and
  82171. + * calls completion routine for the URB if it's done.
  82172. + * Releases the channel to be used by other transfers.
  82173. + * In case of Isochronous endpoint the channel is not halted until
  82174. + * the end of the session, i.e. QTD list is empty.
  82175. + * If periodic channel released the FrameList is updated accordingly.
  82176. + *
  82177. + * Calls transaction selection routines to activate pending transfers.
  82178. + *
  82179. + * @param hcd The HCD state structure for the DWC OTG controller.
  82180. + * @param hc Host channel, the transfer is completed on.
  82181. + * @param hc_regs Host channel registers.
  82182. + * @param halt_status Reason the channel is being halted,
  82183. + * or just XferComplete for isochronous transfer
  82184. + */
  82185. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  82186. + dwc_hc_t * hc,
  82187. + dwc_otg_hc_regs_t * hc_regs,
  82188. + dwc_otg_halt_status_e halt_status)
  82189. +{
  82190. + uint8_t continue_isoc_xfer = 0;
  82191. + dwc_otg_transaction_type_e tr_type;
  82192. + dwc_otg_qh_t *qh = hc->qh;
  82193. +
  82194. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  82195. +
  82196. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  82197. +
  82198. + /* Release the channel if halted or session completed */
  82199. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  82200. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  82201. +
  82202. + /* Halt the channel if session completed */
  82203. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  82204. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  82205. + }
  82206. +
  82207. + release_channel_ddma(hcd, qh);
  82208. + dwc_otg_hcd_qh_remove(hcd, qh);
  82209. + } else {
  82210. + /* Keep in assigned schedule to continue transfer */
  82211. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  82212. + &qh->qh_list_entry);
  82213. + continue_isoc_xfer = 1;
  82214. +
  82215. + }
  82216. + /** @todo Consider the case when period exceeds FrameList size.
  82217. + * Frame Rollover interrupt should be used.
  82218. + */
  82219. + } else {
  82220. + /* Scan descriptor list to complete the URB(s), then release the channel */
  82221. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  82222. +
  82223. + release_channel_ddma(hcd, qh);
  82224. + dwc_otg_hcd_qh_remove(hcd, qh);
  82225. +
  82226. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  82227. + /* Add back to inactive non-periodic schedule on normal completion */
  82228. + dwc_otg_hcd_qh_add(hcd, qh);
  82229. + }
  82230. +
  82231. + }
  82232. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  82233. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  82234. + if (continue_isoc_xfer) {
  82235. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  82236. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  82237. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  82238. + tr_type = DWC_OTG_TRANSACTION_ALL;
  82239. + }
  82240. + }
  82241. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  82242. + }
  82243. +}
  82244. +
  82245. +#endif /* DWC_DEVICE_ONLY */
  82246. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  82247. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 1970-01-01 01:00:00.000000000 +0100
  82248. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd.h 2014-09-14 19:04:13.000000000 +0200
  82249. @@ -0,0 +1,862 @@
  82250. +/* ==========================================================================
  82251. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  82252. + * $Revision: #58 $
  82253. + * $Date: 2011/09/15 $
  82254. + * $Change: 1846647 $
  82255. + *
  82256. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  82257. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  82258. + * otherwise expressly agreed to in writing between Synopsys and you.
  82259. + *
  82260. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  82261. + * any End User Software License Agreement or Agreement for Licensed Product
  82262. + * with Synopsys or any supplement thereto. You are permitted to use and
  82263. + * redistribute this Software in source and binary forms, with or without
  82264. + * modification, provided that redistributions of source code must retain this
  82265. + * notice. You may not view, use, disclose, copy or distribute this file or
  82266. + * any information contained herein except pursuant to this license grant from
  82267. + * Synopsys. If you do not agree with this notice, including the disclaimer
  82268. + * below, then you are not authorized to use the Software.
  82269. + *
  82270. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  82271. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82272. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  82273. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  82274. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  82275. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  82276. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  82277. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  82278. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  82279. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  82280. + * DAMAGE.
  82281. + * ========================================================================== */
  82282. +#ifndef DWC_DEVICE_ONLY
  82283. +#ifndef __DWC_HCD_H__
  82284. +#define __DWC_HCD_H__
  82285. +
  82286. +#include "dwc_otg_os_dep.h"
  82287. +#include "usb.h"
  82288. +#include "dwc_otg_hcd_if.h"
  82289. +#include "dwc_otg_core_if.h"
  82290. +#include "dwc_list.h"
  82291. +#include "dwc_otg_cil.h"
  82292. +#include "dwc_otg_fiq_fsm.h"
  82293. +
  82294. +
  82295. +/**
  82296. + * @file
  82297. + *
  82298. + * This file contains the structures, constants, and interfaces for
  82299. + * the Host Contoller Driver (HCD).
  82300. + *
  82301. + * The Host Controller Driver (HCD) is responsible for translating requests
  82302. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  82303. + * It isolates the USBD from the specifics of the controller by providing an
  82304. + * API to the USBD.
  82305. + */
  82306. +
  82307. +struct dwc_otg_hcd_pipe_info {
  82308. + uint8_t dev_addr;
  82309. + uint8_t ep_num;
  82310. + uint8_t pipe_type;
  82311. + uint8_t pipe_dir;
  82312. + uint16_t mps;
  82313. +};
  82314. +
  82315. +struct dwc_otg_hcd_iso_packet_desc {
  82316. + uint32_t offset;
  82317. + uint32_t length;
  82318. + uint32_t actual_length;
  82319. + uint32_t status;
  82320. +};
  82321. +
  82322. +struct dwc_otg_qtd;
  82323. +
  82324. +struct dwc_otg_hcd_urb {
  82325. + void *priv;
  82326. + struct dwc_otg_qtd *qtd;
  82327. + void *buf;
  82328. + dwc_dma_t dma;
  82329. + void *setup_packet;
  82330. + dwc_dma_t setup_dma;
  82331. + uint32_t length;
  82332. + uint32_t actual_length;
  82333. + uint32_t status;
  82334. + uint32_t error_count;
  82335. + uint32_t packet_count;
  82336. + uint32_t flags;
  82337. + uint16_t interval;
  82338. + struct dwc_otg_hcd_pipe_info pipe_info;
  82339. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  82340. +};
  82341. +
  82342. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  82343. +{
  82344. + return pipe->ep_num;
  82345. +}
  82346. +
  82347. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  82348. + *pipe)
  82349. +{
  82350. + return pipe->pipe_type;
  82351. +}
  82352. +
  82353. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  82354. +{
  82355. + return pipe->mps;
  82356. +}
  82357. +
  82358. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  82359. + *pipe)
  82360. +{
  82361. + return pipe->dev_addr;
  82362. +}
  82363. +
  82364. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  82365. + *pipe)
  82366. +{
  82367. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  82368. +}
  82369. +
  82370. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  82371. + *pipe)
  82372. +{
  82373. + return (pipe->pipe_type == UE_INTERRUPT);
  82374. +}
  82375. +
  82376. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  82377. + *pipe)
  82378. +{
  82379. + return (pipe->pipe_type == UE_BULK);
  82380. +}
  82381. +
  82382. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  82383. + *pipe)
  82384. +{
  82385. + return (pipe->pipe_type == UE_CONTROL);
  82386. +}
  82387. +
  82388. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  82389. +{
  82390. + return (pipe->pipe_dir == UE_DIR_IN);
  82391. +}
  82392. +
  82393. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  82394. + *pipe)
  82395. +{
  82396. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  82397. +}
  82398. +
  82399. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  82400. + uint8_t devaddr, uint8_t ep_num,
  82401. + uint8_t pipe_type, uint8_t pipe_dir,
  82402. + uint16_t mps)
  82403. +{
  82404. + pipe->dev_addr = devaddr;
  82405. + pipe->ep_num = ep_num;
  82406. + pipe->pipe_type = pipe_type;
  82407. + pipe->pipe_dir = pipe_dir;
  82408. + pipe->mps = mps;
  82409. +}
  82410. +
  82411. +/**
  82412. + * Phases for control transfers.
  82413. + */
  82414. +typedef enum dwc_otg_control_phase {
  82415. + DWC_OTG_CONTROL_SETUP,
  82416. + DWC_OTG_CONTROL_DATA,
  82417. + DWC_OTG_CONTROL_STATUS
  82418. +} dwc_otg_control_phase_e;
  82419. +
  82420. +/** Transaction types. */
  82421. +typedef enum dwc_otg_transaction_type {
  82422. + DWC_OTG_TRANSACTION_NONE = 0,
  82423. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  82424. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  82425. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  82426. +} dwc_otg_transaction_type_e;
  82427. +
  82428. +struct dwc_otg_qh;
  82429. +
  82430. +/**
  82431. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  82432. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  82433. + * (of one of these types) submitted to the HCD. The transfer associated with
  82434. + * a QTD may require one or multiple transactions.
  82435. + *
  82436. + * A QTD is linked to a Queue Head, which is entered in either the
  82437. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  82438. + * execution, some or all of its transactions may be executed. After
  82439. + * execution, the state of the QTD is updated. The QTD may be retired if all
  82440. + * its transactions are complete or if an error occurred. Otherwise, it
  82441. + * remains in the schedule so more transactions can be executed later.
  82442. + */
  82443. +typedef struct dwc_otg_qtd {
  82444. + /**
  82445. + * Determines the PID of the next data packet for the data phase of
  82446. + * control transfers. Ignored for other transfer types.<br>
  82447. + * One of the following values:
  82448. + * - DWC_OTG_HC_PID_DATA0
  82449. + * - DWC_OTG_HC_PID_DATA1
  82450. + */
  82451. + uint8_t data_toggle;
  82452. +
  82453. + /** Current phase for control transfers (Setup, Data, or Status). */
  82454. + dwc_otg_control_phase_e control_phase;
  82455. +
  82456. + /** Keep track of the current split type
  82457. + * for FS/LS endpoints on a HS Hub */
  82458. + uint8_t complete_split;
  82459. +
  82460. + /** How many bytes transferred during SSPLIT OUT */
  82461. + uint32_t ssplit_out_xfer_count;
  82462. +
  82463. + /**
  82464. + * Holds the number of bus errors that have occurred for a transaction
  82465. + * within this transfer.
  82466. + */
  82467. + uint8_t error_count;
  82468. +
  82469. + /**
  82470. + * Index of the next frame descriptor for an isochronous transfer. A
  82471. + * frame descriptor describes the buffer position and length of the
  82472. + * data to be transferred in the next scheduled (micro)frame of an
  82473. + * isochronous transfer. It also holds status for that transaction.
  82474. + * The frame index starts at 0.
  82475. + */
  82476. + uint16_t isoc_frame_index;
  82477. +
  82478. + /** Position of the ISOC split on full/low speed */
  82479. + uint8_t isoc_split_pos;
  82480. +
  82481. + /** Position of the ISOC split in the buffer for the current frame */
  82482. + uint16_t isoc_split_offset;
  82483. +
  82484. + /** URB for this transfer */
  82485. + struct dwc_otg_hcd_urb *urb;
  82486. +
  82487. + struct dwc_otg_qh *qh;
  82488. +
  82489. + /** This list of QTDs */
  82490. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  82491. +
  82492. + /** Indicates if this QTD is currently processed by HW. */
  82493. + uint8_t in_process;
  82494. +
  82495. + /** Number of DMA descriptors for this QTD */
  82496. + uint8_t n_desc;
  82497. +
  82498. + /**
  82499. + * Last activated frame(packet) index.
  82500. + * Used in Descriptor DMA mode only.
  82501. + */
  82502. + uint16_t isoc_frame_index_last;
  82503. +
  82504. +} dwc_otg_qtd_t;
  82505. +
  82506. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  82507. +
  82508. +/**
  82509. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  82510. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  82511. + * be entered in either the non-periodic or periodic schedule.
  82512. + */
  82513. +typedef struct dwc_otg_qh {
  82514. + /**
  82515. + * Endpoint type.
  82516. + * One of the following values:
  82517. + * - UE_CONTROL
  82518. + * - UE_BULK
  82519. + * - UE_INTERRUPT
  82520. + * - UE_ISOCHRONOUS
  82521. + */
  82522. + uint8_t ep_type;
  82523. + uint8_t ep_is_in;
  82524. +
  82525. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  82526. + uint16_t maxp;
  82527. +
  82528. + /**
  82529. + * Device speed.
  82530. + * One of the following values:
  82531. + * - DWC_OTG_EP_SPEED_LOW
  82532. + * - DWC_OTG_EP_SPEED_FULL
  82533. + * - DWC_OTG_EP_SPEED_HIGH
  82534. + */
  82535. + uint8_t dev_speed;
  82536. +
  82537. + /**
  82538. + * Determines the PID of the next data packet for non-control
  82539. + * transfers. Ignored for control transfers.<br>
  82540. + * One of the following values:
  82541. + * - DWC_OTG_HC_PID_DATA0
  82542. + * - DWC_OTG_HC_PID_DATA1
  82543. + */
  82544. + uint8_t data_toggle;
  82545. +
  82546. + /** Ping state if 1. */
  82547. + uint8_t ping_state;
  82548. +
  82549. + /**
  82550. + * List of QTDs for this QH.
  82551. + */
  82552. + struct dwc_otg_qtd_list qtd_list;
  82553. +
  82554. + /** Host channel currently processing transfers for this QH. */
  82555. + struct dwc_hc *channel;
  82556. +
  82557. + /** Full/low speed endpoint on high-speed hub requires split. */
  82558. + uint8_t do_split;
  82559. +
  82560. + /** @name Periodic schedule information */
  82561. + /** @{ */
  82562. +
  82563. + /** Bandwidth in microseconds per (micro)frame. */
  82564. + uint16_t usecs;
  82565. +
  82566. + /** Interval between transfers in (micro)frames. */
  82567. + uint16_t interval;
  82568. +
  82569. + /**
  82570. + * (micro)frame to initialize a periodic transfer. The transfer
  82571. + * executes in the following (micro)frame.
  82572. + */
  82573. + uint16_t sched_frame;
  82574. +
  82575. + /*
  82576. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  82577. + */
  82578. + uint16_t nak_frame;
  82579. +
  82580. + /** (micro)frame at which last start split was initialized. */
  82581. + uint16_t start_split_frame;
  82582. +
  82583. + /** @} */
  82584. +
  82585. + /**
  82586. + * Used instead of original buffer if
  82587. + * it(physical address) is not dword-aligned.
  82588. + */
  82589. + uint8_t *dw_align_buf;
  82590. + dwc_dma_t dw_align_buf_dma;
  82591. +
  82592. + /** Entry for QH in either the periodic or non-periodic schedule. */
  82593. + dwc_list_link_t qh_list_entry;
  82594. +
  82595. + /** @name Descriptor DMA support */
  82596. + /** @{ */
  82597. +
  82598. + /** Descriptor List. */
  82599. + dwc_otg_host_dma_desc_t *desc_list;
  82600. +
  82601. + /** Descriptor List physical address. */
  82602. + dwc_dma_t desc_list_dma;
  82603. +
  82604. + /**
  82605. + * Xfer Bytes array.
  82606. + * Each element corresponds to a descriptor and indicates
  82607. + * original XferSize size value for the descriptor.
  82608. + */
  82609. + uint32_t *n_bytes;
  82610. +
  82611. + /** Actual number of transfer descriptors in a list. */
  82612. + uint16_t ntd;
  82613. +
  82614. + /** First activated isochronous transfer descriptor index. */
  82615. + uint8_t td_first;
  82616. + /** Last activated isochronous transfer descriptor index. */
  82617. + uint8_t td_last;
  82618. +
  82619. + /** @} */
  82620. +
  82621. +
  82622. + uint16_t speed;
  82623. + uint16_t frame_usecs[8];
  82624. +
  82625. + uint32_t skip_count;
  82626. +} dwc_otg_qh_t;
  82627. +
  82628. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  82629. +
  82630. +typedef struct urb_tq_entry {
  82631. + struct urb *urb;
  82632. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  82633. +} urb_tq_entry_t;
  82634. +
  82635. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  82636. +
  82637. +/**
  82638. + * This structure holds the state of the HCD, including the non-periodic and
  82639. + * periodic schedules.
  82640. + */
  82641. +struct dwc_otg_hcd {
  82642. + /** The DWC otg device pointer */
  82643. + struct dwc_otg_device *otg_dev;
  82644. + /** DWC OTG Core Interface Layer */
  82645. + dwc_otg_core_if_t *core_if;
  82646. +
  82647. + /** Function HCD driver callbacks */
  82648. + struct dwc_otg_hcd_function_ops *fops;
  82649. +
  82650. + /** Internal DWC HCD Flags */
  82651. + volatile union dwc_otg_hcd_internal_flags {
  82652. + uint32_t d32;
  82653. + struct {
  82654. + unsigned port_connect_status_change:1;
  82655. + unsigned port_connect_status:1;
  82656. + unsigned port_reset_change:1;
  82657. + unsigned port_enable_change:1;
  82658. + unsigned port_suspend_change:1;
  82659. + unsigned port_over_current_change:1;
  82660. + unsigned port_l1_change:1;
  82661. + unsigned reserved:26;
  82662. + } b;
  82663. + } flags;
  82664. +
  82665. + /**
  82666. + * Inactive items in the non-periodic schedule. This is a list of
  82667. + * Queue Heads. Transfers associated with these Queue Heads are not
  82668. + * currently assigned to a host channel.
  82669. + */
  82670. + dwc_list_link_t non_periodic_sched_inactive;
  82671. +
  82672. + /**
  82673. + * Active items in the non-periodic schedule. This is a list of
  82674. + * Queue Heads. Transfers associated with these Queue Heads are
  82675. + * currently assigned to a host channel.
  82676. + */
  82677. + dwc_list_link_t non_periodic_sched_active;
  82678. +
  82679. + /**
  82680. + * Pointer to the next Queue Head to process in the active
  82681. + * non-periodic schedule.
  82682. + */
  82683. + dwc_list_link_t *non_periodic_qh_ptr;
  82684. +
  82685. + /**
  82686. + * Inactive items in the periodic schedule. This is a list of QHs for
  82687. + * periodic transfers that are _not_ scheduled for the next frame.
  82688. + * Each QH in the list has an interval counter that determines when it
  82689. + * needs to be scheduled for execution. This scheduling mechanism
  82690. + * allows only a simple calculation for periodic bandwidth used (i.e.
  82691. + * must assume that all periodic transfers may need to execute in the
  82692. + * same frame). However, it greatly simplifies scheduling and should
  82693. + * be sufficient for the vast majority of OTG hosts, which need to
  82694. + * connect to a small number of peripherals at one time.
  82695. + *
  82696. + * Items move from this list to periodic_sched_ready when the QH
  82697. + * interval counter is 0 at SOF.
  82698. + */
  82699. + dwc_list_link_t periodic_sched_inactive;
  82700. +
  82701. + /**
  82702. + * List of periodic QHs that are ready for execution in the next
  82703. + * frame, but have not yet been assigned to host channels.
  82704. + *
  82705. + * Items move from this list to periodic_sched_assigned as host
  82706. + * channels become available during the current frame.
  82707. + */
  82708. + dwc_list_link_t periodic_sched_ready;
  82709. +
  82710. + /**
  82711. + * List of periodic QHs to be executed in the next frame that are
  82712. + * assigned to host channels.
  82713. + *
  82714. + * Items move from this list to periodic_sched_queued as the
  82715. + * transactions for the QH are queued to the DWC_otg controller.
  82716. + */
  82717. + dwc_list_link_t periodic_sched_assigned;
  82718. +
  82719. + /**
  82720. + * List of periodic QHs that have been queued for execution.
  82721. + *
  82722. + * Items move from this list to either periodic_sched_inactive or
  82723. + * periodic_sched_ready when the channel associated with the transfer
  82724. + * is released. If the interval for the QH is 1, the item moves to
  82725. + * periodic_sched_ready because it must be rescheduled for the next
  82726. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  82727. + */
  82728. + dwc_list_link_t periodic_sched_queued;
  82729. +
  82730. + /**
  82731. + * Total bandwidth claimed so far for periodic transfers. This value
  82732. + * is in microseconds per (micro)frame. The assumption is that all
  82733. + * periodic transfers may occur in the same (micro)frame.
  82734. + */
  82735. + uint16_t periodic_usecs;
  82736. +
  82737. + /**
  82738. + * Total bandwidth claimed so far for all periodic transfers
  82739. + * in a frame.
  82740. + * This will include a mixture of HS and FS transfers.
  82741. + * Units are microseconds per (micro)frame.
  82742. + * We have a budget per frame and have to schedule
  82743. + * transactions accordingly.
  82744. + * Watch out for the fact that things are actually scheduled for the
  82745. + * "next frame".
  82746. + */
  82747. + uint16_t frame_usecs[8];
  82748. +
  82749. +
  82750. + /**
  82751. + * Frame number read from the core at SOF. The value ranges from 0 to
  82752. + * DWC_HFNUM_MAX_FRNUM.
  82753. + */
  82754. + uint16_t frame_number;
  82755. +
  82756. + /**
  82757. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  82758. + */
  82759. + uint16_t periodic_qh_count;
  82760. +
  82761. + /**
  82762. + * Free host channels in the controller. This is a list of
  82763. + * dwc_hc_t items.
  82764. + */
  82765. + struct hc_list free_hc_list;
  82766. + /**
  82767. + * Number of host channels assigned to periodic transfers. Currently
  82768. + * assuming that there is a dedicated host channel for each periodic
  82769. + * transaction and at least one host channel available for
  82770. + * non-periodic transactions.
  82771. + */
  82772. + int periodic_channels; /* microframe_schedule==0 */
  82773. +
  82774. + /**
  82775. + * Number of host channels assigned to non-periodic transfers.
  82776. + */
  82777. + int non_periodic_channels; /* microframe_schedule==0 */
  82778. +
  82779. + /**
  82780. + * Number of host channels assigned to non-periodic transfers.
  82781. + */
  82782. + int available_host_channels;
  82783. +
  82784. + /**
  82785. + * Array of pointers to the host channel descriptors. Allows accessing
  82786. + * a host channel descriptor given the host channel number. This is
  82787. + * useful in interrupt handlers.
  82788. + */
  82789. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  82790. +
  82791. + /**
  82792. + * Buffer to use for any data received during the status phase of a
  82793. + * control transfer. Normally no data is transferred during the status
  82794. + * phase. This buffer is used as a bit bucket.
  82795. + */
  82796. + uint8_t *status_buf;
  82797. +
  82798. + /**
  82799. + * DMA address for status_buf.
  82800. + */
  82801. + dma_addr_t status_buf_dma;
  82802. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  82803. +
  82804. + /**
  82805. + * Connection timer. An OTG host must display a message if the device
  82806. + * does not connect. Started when the VBus power is turned on via
  82807. + * sysfs attribute "buspower".
  82808. + */
  82809. + dwc_timer_t *conn_timer;
  82810. +
  82811. + /* Tasket to do a reset */
  82812. + dwc_tasklet_t *reset_tasklet;
  82813. +
  82814. + dwc_tasklet_t *completion_tasklet;
  82815. + struct urb_list completed_urb_list;
  82816. +
  82817. + /* */
  82818. + dwc_spinlock_t *lock;
  82819. + dwc_spinlock_t *channel_lock;
  82820. + /**
  82821. + * Private data that could be used by OS wrapper.
  82822. + */
  82823. + void *priv;
  82824. +
  82825. + uint8_t otg_port;
  82826. +
  82827. + /** Frame List */
  82828. + uint32_t *frame_list;
  82829. +
  82830. + /** Hub - Port assignment */
  82831. + int hub_port[128];
  82832. +#ifdef FIQ_DEBUG
  82833. + int hub_port_alloc[2048];
  82834. +#endif
  82835. +
  82836. + /** Frame List DMA address */
  82837. + dma_addr_t frame_list_dma;
  82838. +
  82839. + struct fiq_stack *fiq_stack;
  82840. + struct fiq_state *fiq_state;
  82841. +
  82842. + /** Virtual address for split transaction DMA bounce buffers */
  82843. + struct fiq_dma_blob *fiq_dmab;
  82844. +
  82845. +#ifdef DEBUG
  82846. + uint32_t frrem_samples;
  82847. + uint64_t frrem_accum;
  82848. +
  82849. + uint32_t hfnum_7_samples_a;
  82850. + uint64_t hfnum_7_frrem_accum_a;
  82851. + uint32_t hfnum_0_samples_a;
  82852. + uint64_t hfnum_0_frrem_accum_a;
  82853. + uint32_t hfnum_other_samples_a;
  82854. + uint64_t hfnum_other_frrem_accum_a;
  82855. +
  82856. + uint32_t hfnum_7_samples_b;
  82857. + uint64_t hfnum_7_frrem_accum_b;
  82858. + uint32_t hfnum_0_samples_b;
  82859. + uint64_t hfnum_0_frrem_accum_b;
  82860. + uint32_t hfnum_other_samples_b;
  82861. + uint64_t hfnum_other_frrem_accum_b;
  82862. +#endif
  82863. +};
  82864. +
  82865. +/** @name Transaction Execution Functions */
  82866. +/** @{ */
  82867. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  82868. + * hcd);
  82869. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  82870. + dwc_otg_transaction_type_e tr_type);
  82871. +
  82872. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  82873. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  82874. +
  82875. +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  82876. +extern int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh);
  82877. +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
  82878. +
  82879. +/** @} */
  82880. +
  82881. +/** @name Interrupt Handler Functions */
  82882. +/** @{ */
  82883. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  82884. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  82885. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  82886. + dwc_otg_hcd);
  82887. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  82888. + dwc_otg_hcd);
  82889. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  82890. + dwc_otg_hcd);
  82891. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  82892. + dwc_otg_hcd);
  82893. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  82894. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  82895. + dwc_otg_hcd);
  82896. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  82897. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  82898. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  82899. + uint32_t num);
  82900. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  82901. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  82902. + dwc_otg_hcd);
  82903. +/** @} */
  82904. +
  82905. +/** @name Schedule Queue Functions */
  82906. +/** @{ */
  82907. +
  82908. +/* Implemented in dwc_otg_hcd_queue.c */
  82909. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  82910. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  82911. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82912. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82913. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82914. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  82915. + int sched_csplit);
  82916. +
  82917. +/** Remove and free a QH */
  82918. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  82919. + dwc_otg_qh_t * qh)
  82920. +{
  82921. + dwc_irqflags_t flags;
  82922. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  82923. + dwc_otg_hcd_qh_remove(hcd, qh);
  82924. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  82925. + dwc_otg_hcd_qh_free(hcd, qh);
  82926. +}
  82927. +
  82928. +/** Allocates memory for a QH structure.
  82929. + * @return Returns the memory allocate or NULL on error. */
  82930. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  82931. +{
  82932. + if (atomic_alloc)
  82933. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  82934. + else
  82935. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  82936. +}
  82937. +
  82938. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  82939. + int atomic_alloc);
  82940. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  82941. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  82942. + dwc_otg_qh_t ** qh, int atomic_alloc);
  82943. +
  82944. +/** Allocates memory for a QTD structure.
  82945. + * @return Returns the memory allocate or NULL on error. */
  82946. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  82947. +{
  82948. + if (atomic_alloc)
  82949. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  82950. + else
  82951. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  82952. +}
  82953. +
  82954. +/** Frees the memory for a QTD structure. QTD should already be removed from
  82955. + * list.
  82956. + * @param qtd QTD to free.*/
  82957. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  82958. +{
  82959. + DWC_FREE(qtd);
  82960. +}
  82961. +
  82962. +/** Removes a QTD from list.
  82963. + * @param hcd HCD instance.
  82964. + * @param qtd QTD to remove from list.
  82965. + * @param qh QTD belongs to.
  82966. + */
  82967. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  82968. + dwc_otg_qtd_t * qtd,
  82969. + dwc_otg_qh_t * qh)
  82970. +{
  82971. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  82972. +}
  82973. +
  82974. +/** Remove and free a QTD
  82975. + * Need to disable IRQ and hold hcd lock while calling this function out of
  82976. + * interrupt servicing chain */
  82977. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  82978. + dwc_otg_qtd_t * qtd,
  82979. + dwc_otg_qh_t * qh)
  82980. +{
  82981. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  82982. + dwc_otg_hcd_qtd_free(qtd);
  82983. +}
  82984. +
  82985. +/** @} */
  82986. +
  82987. +/** @name Descriptor DMA Supporting Functions */
  82988. +/** @{ */
  82989. +
  82990. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82991. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  82992. + dwc_hc_t * hc,
  82993. + dwc_otg_hc_regs_t * hc_regs,
  82994. + dwc_otg_halt_status_e halt_status);
  82995. +
  82996. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82997. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  82998. +
  82999. +/** @} */
  83000. +
  83001. +/** @name Internal Functions */
  83002. +/** @{ */
  83003. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  83004. +/** @} */
  83005. +
  83006. +#ifdef CONFIG_USB_DWC_OTG_LPM
  83007. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  83008. + uint8_t devaddr);
  83009. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  83010. +#endif
  83011. +
  83012. +/** Gets the QH that contains the list_head */
  83013. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  83014. +
  83015. +/** Gets the QTD that contains the list_head */
  83016. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  83017. +
  83018. +/** Check if QH is non-periodic */
  83019. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  83020. + (_qh_ptr_->ep_type == UE_CONTROL))
  83021. +
  83022. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  83023. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  83024. +
  83025. +/** Packet size for any kind of endpoint descriptor */
  83026. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  83027. +
  83028. +/**
  83029. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  83030. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  83031. + * frame number when the max frame number is reached.
  83032. + */
  83033. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  83034. +{
  83035. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  83036. + (DWC_HFNUM_MAX_FRNUM >> 1);
  83037. +}
  83038. +
  83039. +/**
  83040. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  83041. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  83042. + * number when the max frame number is reached.
  83043. + */
  83044. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  83045. +{
  83046. + return (frame1 != frame2) &&
  83047. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  83048. + (DWC_HFNUM_MAX_FRNUM >> 1));
  83049. +}
  83050. +
  83051. +/**
  83052. + * Increments _frame by the amount specified by _inc. The addition is done
  83053. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  83054. + */
  83055. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  83056. +{
  83057. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  83058. +}
  83059. +
  83060. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  83061. +{
  83062. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  83063. +}
  83064. +
  83065. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  83066. +{
  83067. + return frame & 0x7;
  83068. +}
  83069. +
  83070. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  83071. + dwc_otg_hc_regs_t * hc_regs,
  83072. + dwc_otg_qtd_t * qtd);
  83073. +
  83074. +#ifdef DEBUG
  83075. +/**
  83076. + * Macro to sample the remaining PHY clocks left in the current frame. This
  83077. + * may be used during debugging to determine the average time it takes to
  83078. + * execute sections of code. There are two possible sample points, "a" and
  83079. + * "b", so the _letter argument must be one of these values.
  83080. + *
  83081. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  83082. + * example, "cat /sys/devices/lm0/hcd_frrem".
  83083. + */
  83084. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  83085. +{ \
  83086. + hfnum_data_t hfnum; \
  83087. + dwc_otg_qtd_t *qtd; \
  83088. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  83089. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  83090. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  83091. + switch (hfnum.b.frnum & 0x7) { \
  83092. + case 7: \
  83093. + _hcd->hfnum_7_samples_##_letter++; \
  83094. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  83095. + break; \
  83096. + case 0: \
  83097. + _hcd->hfnum_0_samples_##_letter++; \
  83098. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  83099. + break; \
  83100. + default: \
  83101. + _hcd->hfnum_other_samples_##_letter++; \
  83102. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  83103. + break; \
  83104. + } \
  83105. + } \
  83106. +}
  83107. +#else
  83108. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  83109. +#endif
  83110. +#endif
  83111. +#endif /* DWC_DEVICE_ONLY */
  83112. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  83113. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 1970-01-01 01:00:00.000000000 +0100
  83114. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h 2014-09-14 19:04:13.000000000 +0200
  83115. @@ -0,0 +1,417 @@
  83116. +/* ==========================================================================
  83117. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  83118. + * $Revision: #12 $
  83119. + * $Date: 2011/10/26 $
  83120. + * $Change: 1873028 $
  83121. + *
  83122. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  83123. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  83124. + * otherwise expressly agreed to in writing between Synopsys and you.
  83125. + *
  83126. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  83127. + * any End User Software License Agreement or Agreement for Licensed Product
  83128. + * with Synopsys or any supplement thereto. You are permitted to use and
  83129. + * redistribute this Software in source and binary forms, with or without
  83130. + * modification, provided that redistributions of source code must retain this
  83131. + * notice. You may not view, use, disclose, copy or distribute this file or
  83132. + * any information contained herein except pursuant to this license grant from
  83133. + * Synopsys. If you do not agree with this notice, including the disclaimer
  83134. + * below, then you are not authorized to use the Software.
  83135. + *
  83136. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  83137. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  83138. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  83139. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  83140. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  83141. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  83142. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  83143. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  83144. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  83145. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  83146. + * DAMAGE.
  83147. + * ========================================================================== */
  83148. +#ifndef DWC_DEVICE_ONLY
  83149. +#ifndef __DWC_HCD_IF_H__
  83150. +#define __DWC_HCD_IF_H__
  83151. +
  83152. +#include "dwc_otg_core_if.h"
  83153. +
  83154. +/** @file
  83155. + * This file defines DWC_OTG HCD Core API.
  83156. + */
  83157. +
  83158. +struct dwc_otg_hcd;
  83159. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  83160. +
  83161. +struct dwc_otg_hcd_urb;
  83162. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  83163. +
  83164. +/** @name HCD Function Driver Callbacks */
  83165. +/** @{ */
  83166. +
  83167. +/** This function is called whenever core switches to host mode. */
  83168. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  83169. +
  83170. +/** This function is called when device has been disconnected */
  83171. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  83172. +
  83173. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  83174. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  83175. + void *urb_handle,
  83176. + uint32_t * hub_addr,
  83177. + uint32_t * port_addr);
  83178. +/** Via this function HCD core gets device speed */
  83179. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  83180. + void *urb_handle);
  83181. +
  83182. +/** This function is called when urb is completed */
  83183. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  83184. + void *urb_handle,
  83185. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  83186. + int32_t status);
  83187. +
  83188. +/** Via this function HCD core gets b_hnp_enable parameter */
  83189. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  83190. +
  83191. +struct dwc_otg_hcd_function_ops {
  83192. + dwc_otg_hcd_start_cb_t start;
  83193. + dwc_otg_hcd_disconnect_cb_t disconnect;
  83194. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  83195. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  83196. + dwc_otg_hcd_complete_urb_cb_t complete;
  83197. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  83198. +};
  83199. +/** @} */
  83200. +
  83201. +/** @name HCD Core API */
  83202. +/** @{ */
  83203. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  83204. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  83205. +
  83206. +/** This function should be called to initiate HCD Core.
  83207. + *
  83208. + * @param hcd The HCD
  83209. + * @param core_if The DWC_OTG Core
  83210. + *
  83211. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  83212. + * Returns 0 on success
  83213. + */
  83214. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  83215. +
  83216. +/** Frees HCD
  83217. + *
  83218. + * @param hcd The HCD
  83219. + */
  83220. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  83221. +
  83222. +/** This function should be called on every hardware interrupt.
  83223. + *
  83224. + * @param dwc_otg_hcd The HCD
  83225. + *
  83226. + * Returns non zero if interrupt is handled
  83227. + * Return 0 if interrupt is not handled
  83228. + */
  83229. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  83230. +
  83231. +/** This function is used to handle the fast interrupt
  83232. + *
  83233. + */
  83234. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  83235. +
  83236. +/**
  83237. + * Returns private data set by
  83238. + * dwc_otg_hcd_set_priv_data function.
  83239. + *
  83240. + * @param hcd The HCD
  83241. + */
  83242. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  83243. +
  83244. +/**
  83245. + * Set private data.
  83246. + *
  83247. + * @param hcd The HCD
  83248. + * @param priv_data pointer to be stored in private data
  83249. + */
  83250. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  83251. +
  83252. +/**
  83253. + * This function initializes the HCD Core.
  83254. + *
  83255. + * @param hcd The HCD
  83256. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  83257. + *
  83258. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  83259. + * Returns 0 on success
  83260. + */
  83261. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  83262. + struct dwc_otg_hcd_function_ops *fops);
  83263. +
  83264. +/**
  83265. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  83266. + * stopped.
  83267. + *
  83268. + * @param hcd The HCD
  83269. + */
  83270. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  83271. +
  83272. +/**
  83273. + * Handles hub class-specific requests.
  83274. + *
  83275. + * @param dwc_otg_hcd The HCD
  83276. + * @param typeReq Request Type
  83277. + * @param wValue wValue from control request
  83278. + * @param wIndex wIndex from control request
  83279. + * @param buf data buffer
  83280. + * @param wLength data buffer length
  83281. + *
  83282. + * Returns -DWC_E_INVALID if invalid argument is passed
  83283. + * Returns 0 on success
  83284. + */
  83285. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  83286. + uint16_t typeReq, uint16_t wValue,
  83287. + uint16_t wIndex, uint8_t * buf,
  83288. + uint16_t wLength);
  83289. +
  83290. +/**
  83291. + * Returns otg port number.
  83292. + *
  83293. + * @param hcd The HCD
  83294. + */
  83295. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  83296. +
  83297. +/**
  83298. + * Returns OTG version - either 1.3 or 2.0.
  83299. + *
  83300. + * @param core_if The core_if structure pointer
  83301. + */
  83302. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  83303. +
  83304. +/**
  83305. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  83306. + *
  83307. + * @param hcd The HCD
  83308. + */
  83309. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  83310. +
  83311. +/**
  83312. + * Returns current frame number.
  83313. + *
  83314. + * @param hcd The HCD
  83315. + */
  83316. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  83317. +
  83318. +/**
  83319. + * Dumps hcd state.
  83320. + *
  83321. + * @param hcd The HCD
  83322. + */
  83323. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  83324. +
  83325. +/**
  83326. + * Dump the average frame remaining at SOF. This can be used to
  83327. + * determine average interrupt latency. Frame remaining is also shown for
  83328. + * start transfer and two additional sample points.
  83329. + * Currently this function is not implemented.
  83330. + *
  83331. + * @param hcd The HCD
  83332. + */
  83333. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  83334. +
  83335. +/**
  83336. + * Sends LPM transaction to the local device.
  83337. + *
  83338. + * @param hcd The HCD
  83339. + * @param devaddr Device Address
  83340. + * @param hird Host initiated resume duration
  83341. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  83342. + *
  83343. + * Returns negative value if sending LPM transaction was not succeeded.
  83344. + * Returns 0 on success.
  83345. + */
  83346. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  83347. + uint8_t hird, uint8_t bRemoteWake);
  83348. +
  83349. +/* URB interface */
  83350. +
  83351. +/**
  83352. + * Allocates memory for dwc_otg_hcd_urb structure.
  83353. + * Allocated memory should be freed by call of DWC_FREE.
  83354. + *
  83355. + * @param hcd The HCD
  83356. + * @param iso_desc_count Count of ISOC descriptors
  83357. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  83358. + */
  83359. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  83360. + int iso_desc_count,
  83361. + int atomic_alloc);
  83362. +
  83363. +/**
  83364. + * Set pipe information in URB.
  83365. + *
  83366. + * @param hcd_urb DWC_OTG URB
  83367. + * @param devaddr Device Address
  83368. + * @param ep_num Endpoint Number
  83369. + * @param ep_type Endpoint Type
  83370. + * @param ep_dir Endpoint Direction
  83371. + * @param mps Max Packet Size
  83372. + */
  83373. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  83374. + uint8_t devaddr, uint8_t ep_num,
  83375. + uint8_t ep_type, uint8_t ep_dir,
  83376. + uint16_t mps);
  83377. +
  83378. +/* Transfer flags */
  83379. +#define URB_GIVEBACK_ASAP 0x1
  83380. +#define URB_SEND_ZERO_PACKET 0x2
  83381. +
  83382. +/**
  83383. + * Sets dwc_otg_hcd_urb parameters.
  83384. + *
  83385. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  83386. + * @param urb_handle Unique handle for request, this will be passed back
  83387. + * to function driver in completion callback.
  83388. + * @param buf The buffer for the data
  83389. + * @param dma The DMA buffer for the data
  83390. + * @param buflen Transfer length
  83391. + * @param sp Buffer for setup data
  83392. + * @param sp_dma DMA address of setup data buffer
  83393. + * @param flags Transfer flags
  83394. + * @param interval Polling interval for interrupt or isochronous transfers.
  83395. + */
  83396. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  83397. + void *urb_handle, void *buf,
  83398. + dwc_dma_t dma, uint32_t buflen, void *sp,
  83399. + dwc_dma_t sp_dma, uint32_t flags,
  83400. + uint16_t interval);
  83401. +
  83402. +/** Gets status from dwc_otg_hcd_urb
  83403. + *
  83404. + * @param dwc_otg_urb DWC_OTG URB
  83405. + */
  83406. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  83407. +
  83408. +/** Gets actual length from dwc_otg_hcd_urb
  83409. + *
  83410. + * @param dwc_otg_urb DWC_OTG URB
  83411. + */
  83412. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  83413. + dwc_otg_urb);
  83414. +
  83415. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  83416. + *
  83417. + * @param dwc_otg_urb DWC_OTG URB
  83418. + */
  83419. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  83420. + dwc_otg_urb);
  83421. +
  83422. +/** Set ISOC descriptor offset and length
  83423. + *
  83424. + * @param dwc_otg_urb DWC_OTG URB
  83425. + * @param desc_num ISOC descriptor number
  83426. + * @param offset Offset from beginig of buffer.
  83427. + * @param length Transaction length
  83428. + */
  83429. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  83430. + int desc_num, uint32_t offset,
  83431. + uint32_t length);
  83432. +
  83433. +/** Get status of ISOC descriptor, specified by desc_num
  83434. + *
  83435. + * @param dwc_otg_urb DWC_OTG URB
  83436. + * @param desc_num ISOC descriptor number
  83437. + */
  83438. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  83439. + dwc_otg_urb, int desc_num);
  83440. +
  83441. +/** Get actual length of ISOC descriptor, specified by desc_num
  83442. + *
  83443. + * @param dwc_otg_urb DWC_OTG URB
  83444. + * @param desc_num ISOC descriptor number
  83445. + */
  83446. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  83447. + dwc_otg_urb,
  83448. + int desc_num);
  83449. +
  83450. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  83451. + *
  83452. + * @param dwc_otg_hcd The HCD
  83453. + * @param dwc_otg_urb DWC_OTG URB
  83454. + * @param ep_handle Out parameter for returning endpoint handle
  83455. + * @param atomic_alloc Flag to do atomic allocation if needed
  83456. + *
  83457. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  83458. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  83459. + * Returns 0 on success.
  83460. + */
  83461. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  83462. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  83463. + void **ep_handle, int atomic_alloc);
  83464. +
  83465. +/** De-queue the specified URB
  83466. + *
  83467. + * @param dwc_otg_hcd The HCD
  83468. + * @param dwc_otg_urb DWC_OTG URB
  83469. + */
  83470. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  83471. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  83472. +
  83473. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  83474. + * Any URBs for the endpoint must already be dequeued.
  83475. + *
  83476. + * @param hcd The HCD
  83477. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  83478. + * @param retry Number of retries if there are queued transfers.
  83479. + *
  83480. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  83481. + * Returns 0 on success
  83482. + */
  83483. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  83484. + int retry);
  83485. +
  83486. +/* Resets the data toggle in qh structure. This function can be called from
  83487. + * usb_clear_halt routine.
  83488. + *
  83489. + * @param hcd The HCD
  83490. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  83491. + *
  83492. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  83493. + * Returns 0 on success
  83494. + */
  83495. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  83496. +
  83497. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  83498. + *
  83499. + * @param hcd The HCD
  83500. + * @param port Port number
  83501. + */
  83502. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  83503. +
  83504. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  83505. + * Only for ISOC and INTERRUPT endpoints.
  83506. + *
  83507. + * @param hcd The HCD
  83508. + * @param ep_handle Endpoint handle
  83509. + */
  83510. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  83511. + void *ep_handle);
  83512. +
  83513. +/** Call this function to check if bandwidth was freed for specified endpoint.
  83514. + *
  83515. + * @param hcd The HCD
  83516. + * @param ep_handle Endpoint handle
  83517. + */
  83518. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  83519. +
  83520. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  83521. + * Only for ISOC and INTERRUPT endpoints.
  83522. + *
  83523. + * @param hcd The HCD
  83524. + * @param ep_handle Endpoint handle
  83525. + */
  83526. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  83527. + void *ep_handle);
  83528. +
  83529. +/** @} */
  83530. +
  83531. +#endif /* __DWC_HCD_IF_H__ */
  83532. +#endif /* DWC_DEVICE_ONLY */
  83533. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  83534. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  83535. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c 2014-09-14 19:04:13.000000000 +0200
  83536. @@ -0,0 +1,2688 @@
  83537. +/* ==========================================================================
  83538. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  83539. + * $Revision: #89 $
  83540. + * $Date: 2011/10/20 $
  83541. + * $Change: 1869487 $
  83542. + *
  83543. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  83544. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  83545. + * otherwise expressly agreed to in writing between Synopsys and you.
  83546. + *
  83547. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  83548. + * any End User Software License Agreement or Agreement for Licensed Product
  83549. + * with Synopsys or any supplement thereto. You are permitted to use and
  83550. + * redistribute this Software in source and binary forms, with or without
  83551. + * modification, provided that redistributions of source code must retain this
  83552. + * notice. You may not view, use, disclose, copy or distribute this file or
  83553. + * any information contained herein except pursuant to this license grant from
  83554. + * Synopsys. If you do not agree with this notice, including the disclaimer
  83555. + * below, then you are not authorized to use the Software.
  83556. + *
  83557. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  83558. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  83559. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  83560. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  83561. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  83562. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  83563. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  83564. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  83565. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  83566. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  83567. + * DAMAGE.
  83568. + * ========================================================================== */
  83569. +#ifndef DWC_DEVICE_ONLY
  83570. +
  83571. +#include "dwc_otg_hcd.h"
  83572. +#include "dwc_otg_regs.h"
  83573. +
  83574. +#include <linux/jiffies.h>
  83575. +#include <mach/hardware.h>
  83576. +#include <asm/fiq.h>
  83577. +
  83578. +
  83579. +extern bool microframe_schedule;
  83580. +
  83581. +/** @file
  83582. + * This file contains the implementation of the HCD Interrupt handlers.
  83583. + */
  83584. +
  83585. +int fiq_done, int_done;
  83586. +
  83587. +#ifdef FIQ_DEBUG
  83588. +char buffer[1000*16];
  83589. +int wptr;
  83590. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  83591. +{
  83592. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  83593. + va_list args;
  83594. + char text[17];
  83595. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  83596. +
  83597. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  83598. + {
  83599. + local_fiq_disable();
  83600. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  83601. + va_start(args, fmt);
  83602. + vsnprintf(text+8, 9, fmt, args);
  83603. + va_end(args);
  83604. +
  83605. + memcpy(buffer + wptr, text, 16);
  83606. + wptr = (wptr + 16) % sizeof(buffer);
  83607. + local_fiq_enable();
  83608. + }
  83609. +}
  83610. +#endif
  83611. +
  83612. +/** This function handles interrupts for the HCD. */
  83613. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83614. +{
  83615. + int retval = 0;
  83616. + static int last_time;
  83617. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  83618. + gintsts_data_t gintsts;
  83619. + gintmsk_data_t gintmsk;
  83620. + hfnum_data_t hfnum;
  83621. + haintmsk_data_t haintmsk;
  83622. +
  83623. +#ifdef DEBUG
  83624. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  83625. +
  83626. +#endif
  83627. +
  83628. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  83629. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  83630. +
  83631. + /* Exit from ISR if core is hibernated */
  83632. + if (core_if->hibernation_suspend == 1) {
  83633. + goto exit_handler_routine;
  83634. + }
  83635. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  83636. + /* Check if HOST Mode */
  83637. + if (dwc_otg_is_host_mode(core_if)) {
  83638. + if (fiq_enable) {
  83639. + local_fiq_disable();
  83640. + /* Pull in from the FIQ's disabled mask */
  83641. + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  83642. + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  83643. + }
  83644. +
  83645. + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
  83646. + gintsts.b.hcintr = 1;
  83647. + }
  83648. +
  83649. + /* Danger will robinson: fake a SOF if necessary */
  83650. + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
  83651. + gintsts.b.sofintr = 1;
  83652. + }
  83653. + gintsts.d32 &= gintmsk.d32;
  83654. +
  83655. + if (fiq_enable)
  83656. + local_fiq_enable();
  83657. +
  83658. + if (!gintsts.d32) {
  83659. + goto exit_handler_routine;
  83660. + }
  83661. +
  83662. +#ifdef DEBUG
  83663. + // We should be OK doing this because the common interrupts should already have been serviced
  83664. + /* Don't print debug message in the interrupt handler on SOF */
  83665. +#ifndef DEBUG_SOF
  83666. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  83667. +#endif
  83668. + DWC_DEBUGPL(DBG_HCDI, "\n");
  83669. +#endif
  83670. +
  83671. +#ifdef DEBUG
  83672. +#ifndef DEBUG_SOF
  83673. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  83674. +#endif
  83675. + DWC_DEBUGPL(DBG_HCDI,
  83676. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  83677. + gintsts.d32, core_if);
  83678. +#endif
  83679. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  83680. + if (gintsts.b.sofintr) {
  83681. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  83682. + }
  83683. +
  83684. + if (gintsts.b.rxstsqlvl) {
  83685. + retval |=
  83686. + dwc_otg_hcd_handle_rx_status_q_level_intr
  83687. + (dwc_otg_hcd);
  83688. + }
  83689. + if (gintsts.b.nptxfempty) {
  83690. + retval |=
  83691. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  83692. + (dwc_otg_hcd);
  83693. + }
  83694. + if (gintsts.b.i2cintr) {
  83695. + /** @todo Implement i2cintr handler. */
  83696. + }
  83697. + if (gintsts.b.portintr) {
  83698. +
  83699. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  83700. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  83701. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  83702. + }
  83703. + if (gintsts.b.hcintr) {
  83704. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  83705. + }
  83706. + if (gintsts.b.ptxfempty) {
  83707. + retval |=
  83708. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  83709. + (dwc_otg_hcd);
  83710. + }
  83711. +#ifdef DEBUG
  83712. +#ifndef DEBUG_SOF
  83713. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  83714. +#endif
  83715. + {
  83716. + DWC_DEBUGPL(DBG_HCDI,
  83717. + "DWC OTG HCD Finished Servicing Interrupts\n");
  83718. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  83719. + DWC_READ_REG32(&global_regs->gintsts));
  83720. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  83721. + DWC_READ_REG32(&global_regs->gintmsk));
  83722. + }
  83723. +#endif
  83724. +
  83725. +#ifdef DEBUG
  83726. +#ifndef DEBUG_SOF
  83727. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  83728. +#endif
  83729. + DWC_DEBUGPL(DBG_HCDI, "\n");
  83730. +#endif
  83731. +
  83732. + }
  83733. +
  83734. +exit_handler_routine:
  83735. + if (fiq_enable) {
  83736. + gintmsk_data_t gintmsk_new;
  83737. + haintmsk_data_t haintmsk_new;
  83738. + local_fiq_disable();
  83739. + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  83740. + if(fiq_fsm_enable)
  83741. + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  83742. + else
  83743. + haintmsk_new.d32 = 0x0000FFFF;
  83744. +
  83745. + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
  83746. + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
  83747. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
  83748. + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
  83749. + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
  83750. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
  83751. + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
  83752. + ;
  83753. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
  83754. + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
  83755. + }
  83756. + int_done++;
  83757. + }
  83758. + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  83759. + /* Re-enable interrupts that the FIQ masked (first time round) */
  83760. + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  83761. + local_fiq_enable();
  83762. +
  83763. + if ((jiffies / HZ) > last_time) {
  83764. + //dwc_otg_qh_t *qh;
  83765. + //dwc_list_link_t *cur;
  83766. + /* Once a second output the fiq and irq numbers, useful for debug */
  83767. + last_time = jiffies / HZ;
  83768. + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
  83769. + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
  83770. + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
  83771. + //printk(KERN_WARNING "Periodic queues:\n");
  83772. + }
  83773. + }
  83774. +
  83775. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  83776. + return retval;
  83777. +}
  83778. +
  83779. +#ifdef DWC_TRACK_MISSED_SOFS
  83780. +
  83781. +#warning Compiling code to track missed SOFs
  83782. +#define FRAME_NUM_ARRAY_SIZE 1000
  83783. +/**
  83784. + * This function is for debug only.
  83785. + */
  83786. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  83787. +{
  83788. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  83789. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  83790. + static int frame_num_idx = 0;
  83791. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  83792. + static int dumped_frame_num_array = 0;
  83793. +
  83794. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  83795. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  83796. + curr_frame_number) {
  83797. + frame_num_array[frame_num_idx] = curr_frame_number;
  83798. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  83799. + }
  83800. + } else if (!dumped_frame_num_array) {
  83801. + int i;
  83802. + DWC_PRINTF("Frame Last Frame\n");
  83803. + DWC_PRINTF("----- ----------\n");
  83804. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  83805. + DWC_PRINTF("0x%04x 0x%04x\n",
  83806. + frame_num_array[i], last_frame_num_array[i]);
  83807. + }
  83808. + dumped_frame_num_array = 1;
  83809. + }
  83810. + last_frame_num = curr_frame_number;
  83811. +}
  83812. +#endif
  83813. +
  83814. +/**
  83815. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  83816. + * transactions may be queued to the DWC_otg controller for the current
  83817. + * (micro)frame. Periodic transactions may be queued to the controller for the
  83818. + * next (micro)frame.
  83819. + */
  83820. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  83821. +{
  83822. + hfnum_data_t hfnum;
  83823. + gintsts_data_t gintsts = { .d32 = 0 };
  83824. + dwc_list_link_t *qh_entry;
  83825. + dwc_otg_qh_t *qh;
  83826. + dwc_otg_transaction_type_e tr_type;
  83827. + int did_something = 0;
  83828. + int32_t next_sched_frame = -1;
  83829. +
  83830. + hfnum.d32 =
  83831. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  83832. +
  83833. +#ifdef DEBUG_SOF
  83834. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  83835. +#endif
  83836. + hcd->frame_number = hfnum.b.frnum;
  83837. +
  83838. +#ifdef DEBUG
  83839. + hcd->frrem_accum += hfnum.b.frrem;
  83840. + hcd->frrem_samples++;
  83841. +#endif
  83842. +
  83843. +#ifdef DWC_TRACK_MISSED_SOFS
  83844. + track_missed_sofs(hcd->frame_number);
  83845. +#endif
  83846. + /* Determine whether any periodic QHs should be executed. */
  83847. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  83848. + while (qh_entry != &hcd->periodic_sched_inactive) {
  83849. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  83850. + qh_entry = qh_entry->next;
  83851. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  83852. +
  83853. + /*
  83854. + * Move QH to the ready list to be executed next
  83855. + * (micro)frame.
  83856. + */
  83857. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  83858. + &qh->qh_list_entry);
  83859. +
  83860. + did_something = 1;
  83861. + }
  83862. + else
  83863. + {
  83864. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  83865. + {
  83866. + next_sched_frame = qh->sched_frame;
  83867. + }
  83868. + }
  83869. + }
  83870. + if (fiq_enable)
  83871. + hcd->fiq_state->next_sched_frame = next_sched_frame;
  83872. +
  83873. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  83874. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  83875. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  83876. + did_something = 1;
  83877. + }
  83878. +
  83879. + /* Clear interrupt - but do not trample on the FIQ sof */
  83880. + if (!fiq_fsm_enable) {
  83881. + gintsts.b.sofintr = 1;
  83882. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  83883. + }
  83884. + return 1;
  83885. +}
  83886. +
  83887. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  83888. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  83889. + * memory if the DWC_otg controller is operating in Slave mode. */
  83890. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83891. +{
  83892. + host_grxsts_data_t grxsts;
  83893. + dwc_hc_t *hc = NULL;
  83894. +
  83895. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  83896. +
  83897. + grxsts.d32 =
  83898. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  83899. +
  83900. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  83901. + if (!hc) {
  83902. + DWC_ERROR("Unable to get corresponding channel\n");
  83903. + return 0;
  83904. + }
  83905. +
  83906. + /* Packet Status */
  83907. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  83908. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  83909. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  83910. + hc->data_pid_start);
  83911. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  83912. +
  83913. + switch (grxsts.b.pktsts) {
  83914. + case DWC_GRXSTS_PKTSTS_IN:
  83915. + /* Read the data into the host buffer. */
  83916. + if (grxsts.b.bcnt > 0) {
  83917. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  83918. + hc->xfer_buff, grxsts.b.bcnt);
  83919. +
  83920. + /* Update the HC fields for the next packet received. */
  83921. + hc->xfer_count += grxsts.b.bcnt;
  83922. + hc->xfer_buff += grxsts.b.bcnt;
  83923. + }
  83924. +
  83925. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  83926. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  83927. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  83928. + /* Handled in interrupt, just ignore data */
  83929. + break;
  83930. + default:
  83931. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  83932. + grxsts.b.pktsts);
  83933. + break;
  83934. + }
  83935. +
  83936. + return 1;
  83937. +}
  83938. +
  83939. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  83940. + * data packets may be written to the FIFO for OUT transfers. More requests
  83941. + * may be written to the non-periodic request queue for IN transfers. This
  83942. + * interrupt is enabled only in Slave mode. */
  83943. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83944. +{
  83945. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  83946. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  83947. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  83948. + return 1;
  83949. +}
  83950. +
  83951. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  83952. + * packets may be written to the FIFO for OUT transfers. More requests may be
  83953. + * written to the periodic request queue for IN transfers. This interrupt is
  83954. + * enabled only in Slave mode. */
  83955. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83956. +{
  83957. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  83958. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  83959. + DWC_OTG_TRANSACTION_PERIODIC);
  83960. + return 1;
  83961. +}
  83962. +
  83963. +/** There are multiple conditions that can cause a port interrupt. This function
  83964. + * determines which interrupt conditions have occurred and handles them
  83965. + * appropriately. */
  83966. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  83967. +{
  83968. + int retval = 0;
  83969. + hprt0_data_t hprt0;
  83970. + hprt0_data_t hprt0_modify;
  83971. +
  83972. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  83973. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  83974. +
  83975. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  83976. + * GINTSTS */
  83977. +
  83978. + hprt0_modify.b.prtena = 0;
  83979. + hprt0_modify.b.prtconndet = 0;
  83980. + hprt0_modify.b.prtenchng = 0;
  83981. + hprt0_modify.b.prtovrcurrchng = 0;
  83982. +
  83983. + /* Port Connect Detected
  83984. + * Set flag and clear if detected */
  83985. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  83986. + // Dont modify port status if we are in hibernation state
  83987. + hprt0_modify.b.prtconndet = 1;
  83988. + hprt0_modify.b.prtenchng = 1;
  83989. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  83990. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  83991. + return retval;
  83992. + }
  83993. +
  83994. + if (hprt0.b.prtconndet) {
  83995. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  83996. + if (dwc_otg_hcd->core_if->adp_enable &&
  83997. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  83998. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  83999. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  84000. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  84001. + /* TODO - check if this is required, as
  84002. + * host initialization was already performed
  84003. + * after initial ADP probing
  84004. + */
  84005. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  84006. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  84007. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  84008. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  84009. + } else {
  84010. +
  84011. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  84012. + "Port Connect Detected--\n", hprt0.d32);
  84013. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  84014. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  84015. + hprt0_modify.b.prtconndet = 1;
  84016. +
  84017. + /* B-Device has connected, Delete the connection timer. */
  84018. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  84019. + }
  84020. + /* The Hub driver asserts a reset when it sees port connect
  84021. + * status change flag */
  84022. + retval |= 1;
  84023. + }
  84024. +
  84025. + /* Port Enable Changed
  84026. + * Clear if detected - Set internal flag if disabled */
  84027. + if (hprt0.b.prtenchng) {
  84028. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  84029. + "Port Enable Changed--\n", hprt0.d32);
  84030. + hprt0_modify.b.prtenchng = 1;
  84031. + if (hprt0.b.prtena == 1) {
  84032. + hfir_data_t hfir;
  84033. + int do_reset = 0;
  84034. + dwc_otg_core_params_t *params =
  84035. + dwc_otg_hcd->core_if->core_params;
  84036. + dwc_otg_core_global_regs_t *global_regs =
  84037. + dwc_otg_hcd->core_if->core_global_regs;
  84038. + dwc_otg_host_if_t *host_if =
  84039. + dwc_otg_hcd->core_if->host_if;
  84040. +
  84041. + /* Every time when port enables calculate
  84042. + * HFIR.FrInterval
  84043. + */
  84044. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  84045. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  84046. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  84047. +
  84048. + /* Check if we need to adjust the PHY clock speed for
  84049. + * low power and adjust it */
  84050. + if (params->host_support_fs_ls_low_power) {
  84051. + gusbcfg_data_t usbcfg;
  84052. +
  84053. + usbcfg.d32 =
  84054. + DWC_READ_REG32(&global_regs->gusbcfg);
  84055. +
  84056. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  84057. + || hprt0.b.prtspd ==
  84058. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  84059. + /*
  84060. + * Low power
  84061. + */
  84062. + hcfg_data_t hcfg;
  84063. + if (usbcfg.b.phylpwrclksel == 0) {
  84064. + /* Set PHY low power clock select for FS/LS devices */
  84065. + usbcfg.b.phylpwrclksel = 1;
  84066. + DWC_WRITE_REG32
  84067. + (&global_regs->gusbcfg,
  84068. + usbcfg.d32);
  84069. + do_reset = 1;
  84070. + }
  84071. +
  84072. + hcfg.d32 =
  84073. + DWC_READ_REG32
  84074. + (&host_if->host_global_regs->hcfg);
  84075. +
  84076. + if (hprt0.b.prtspd ==
  84077. + DWC_HPRT0_PRTSPD_LOW_SPEED
  84078. + && params->host_ls_low_power_phy_clk
  84079. + ==
  84080. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  84081. + {
  84082. + /* 6 MHZ */
  84083. + DWC_DEBUGPL(DBG_CIL,
  84084. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  84085. + if (hcfg.b.fslspclksel !=
  84086. + DWC_HCFG_6_MHZ) {
  84087. + hcfg.b.fslspclksel =
  84088. + DWC_HCFG_6_MHZ;
  84089. + DWC_WRITE_REG32
  84090. + (&host_if->host_global_regs->hcfg,
  84091. + hcfg.d32);
  84092. + do_reset = 1;
  84093. + }
  84094. + } else {
  84095. + /* 48 MHZ */
  84096. + DWC_DEBUGPL(DBG_CIL,
  84097. + "FS_PHY programming HCFG to 48 MHz ()\n");
  84098. + if (hcfg.b.fslspclksel !=
  84099. + DWC_HCFG_48_MHZ) {
  84100. + hcfg.b.fslspclksel =
  84101. + DWC_HCFG_48_MHZ;
  84102. + DWC_WRITE_REG32
  84103. + (&host_if->host_global_regs->hcfg,
  84104. + hcfg.d32);
  84105. + do_reset = 1;
  84106. + }
  84107. + }
  84108. + } else {
  84109. + /*
  84110. + * Not low power
  84111. + */
  84112. + if (usbcfg.b.phylpwrclksel == 1) {
  84113. + usbcfg.b.phylpwrclksel = 0;
  84114. + DWC_WRITE_REG32
  84115. + (&global_regs->gusbcfg,
  84116. + usbcfg.d32);
  84117. + do_reset = 1;
  84118. + }
  84119. + }
  84120. +
  84121. + if (do_reset) {
  84122. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  84123. + }
  84124. + }
  84125. +
  84126. + if (!do_reset) {
  84127. + /* Port has been enabled set the reset change flag */
  84128. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  84129. + }
  84130. + } else {
  84131. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  84132. + }
  84133. + retval |= 1;
  84134. + }
  84135. +
  84136. + /** Overcurrent Change Interrupt */
  84137. + if (hprt0.b.prtovrcurrchng) {
  84138. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  84139. + "Port Overcurrent Changed--\n", hprt0.d32);
  84140. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  84141. + hprt0_modify.b.prtovrcurrchng = 1;
  84142. + retval |= 1;
  84143. + }
  84144. +
  84145. + /* Clear Port Interrupts */
  84146. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  84147. +
  84148. + return retval;
  84149. +}
  84150. +
  84151. +/** This interrupt indicates that one or more host channels has a pending
  84152. + * interrupt. There are multiple conditions that can cause each host channel
  84153. + * interrupt. This function determines which conditions have occurred for each
  84154. + * host channel interrupt and handles them appropriately. */
  84155. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  84156. +{
  84157. + int i;
  84158. + int retval = 0;
  84159. + haint_data_t haint = { .d32 = 0 } ;
  84160. +
  84161. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  84162. + * GINTSTS */
  84163. +
  84164. + if (!fiq_fsm_enable)
  84165. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  84166. +
  84167. + // Overwrite with saved interrupts from fiq handler
  84168. + if(fiq_fsm_enable)
  84169. + {
  84170. + /* check the mask? */
  84171. + local_fiq_disable();
  84172. + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  84173. + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  84174. + local_fiq_enable();
  84175. + }
  84176. +
  84177. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  84178. + if (haint.b2.chint & (1 << i)) {
  84179. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  84180. + }
  84181. + }
  84182. +
  84183. + return retval;
  84184. +}
  84185. +
  84186. +/**
  84187. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  84188. + * holds the reason for the halt.
  84189. + *
  84190. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  84191. + * *short_read is set to 1 upon return if less than the requested
  84192. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  84193. + * return. short_read may also be NULL on entry, in which case it remains
  84194. + * unchanged.
  84195. + */
  84196. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  84197. + dwc_otg_hc_regs_t * hc_regs,
  84198. + dwc_otg_qtd_t * qtd,
  84199. + dwc_otg_halt_status_e halt_status,
  84200. + int *short_read)
  84201. +{
  84202. + hctsiz_data_t hctsiz;
  84203. + uint32_t length;
  84204. +
  84205. + if (short_read != NULL) {
  84206. + *short_read = 0;
  84207. + }
  84208. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84209. +
  84210. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  84211. + if (hc->ep_is_in) {
  84212. + length = hc->xfer_len - hctsiz.b.xfersize;
  84213. + if (short_read != NULL) {
  84214. + *short_read = (hctsiz.b.xfersize != 0);
  84215. + }
  84216. + } else if (hc->qh->do_split) {
  84217. + //length = split_out_xfersize[hc->hc_num];
  84218. + length = qtd->ssplit_out_xfer_count;
  84219. + } else {
  84220. + length = hc->xfer_len;
  84221. + }
  84222. + } else {
  84223. + /*
  84224. + * Must use the hctsiz.pktcnt field to determine how much data
  84225. + * has been transferred. This field reflects the number of
  84226. + * packets that have been transferred via the USB. This is
  84227. + * always an integral number of packets if the transfer was
  84228. + * halted before its normal completion. (Can't use the
  84229. + * hctsiz.xfersize field because that reflects the number of
  84230. + * bytes transferred via the AHB, not the USB).
  84231. + */
  84232. + length =
  84233. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  84234. + }
  84235. +
  84236. + return length;
  84237. +}
  84238. +
  84239. +/**
  84240. + * Updates the state of the URB after a Transfer Complete interrupt on the
  84241. + * host channel. Updates the actual_length field of the URB based on the
  84242. + * number of bytes transferred via the host channel. Sets the URB status
  84243. + * if the data transfer is finished.
  84244. + *
  84245. + * @return 1 if the data transfer specified by the URB is completely finished,
  84246. + * 0 otherwise.
  84247. + */
  84248. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  84249. + dwc_otg_hc_regs_t * hc_regs,
  84250. + dwc_otg_hcd_urb_t * urb,
  84251. + dwc_otg_qtd_t * qtd)
  84252. +{
  84253. + int xfer_done = 0;
  84254. + int short_read = 0;
  84255. +
  84256. + int xfer_length;
  84257. +
  84258. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  84259. + DWC_OTG_HC_XFER_COMPLETE,
  84260. + &short_read);
  84261. +
  84262. + /* non DWORD-aligned buffer case handling. */
  84263. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  84264. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  84265. + xfer_length);
  84266. + }
  84267. +
  84268. + urb->actual_length += xfer_length;
  84269. +
  84270. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  84271. + (urb->flags & URB_SEND_ZERO_PACKET)
  84272. + && (urb->actual_length == urb->length)
  84273. + && !(urb->length % hc->max_packet)) {
  84274. + xfer_done = 0;
  84275. + } else if (short_read || urb->actual_length >= urb->length) {
  84276. + xfer_done = 1;
  84277. + urb->status = 0;
  84278. + }
  84279. +
  84280. +#ifdef DEBUG
  84281. + {
  84282. + hctsiz_data_t hctsiz;
  84283. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84284. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  84285. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  84286. + hc->hc_num);
  84287. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  84288. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  84289. + hctsiz.b.xfersize);
  84290. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  84291. + urb->length);
  84292. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  84293. + urb->actual_length);
  84294. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  84295. + short_read, xfer_done);
  84296. + }
  84297. +#endif
  84298. +
  84299. + return xfer_done;
  84300. +}
  84301. +
  84302. +/*
  84303. + * Save the starting data toggle for the next transfer. The data toggle is
  84304. + * saved in the QH for non-control transfers and it's saved in the QTD for
  84305. + * control transfers.
  84306. + */
  84307. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  84308. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  84309. +{
  84310. + hctsiz_data_t hctsiz;
  84311. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84312. +
  84313. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  84314. + dwc_otg_qh_t *qh = hc->qh;
  84315. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  84316. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  84317. + } else {
  84318. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  84319. + }
  84320. + } else {
  84321. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  84322. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  84323. + } else {
  84324. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  84325. + }
  84326. + }
  84327. +}
  84328. +
  84329. +/**
  84330. + * Updates the state of an Isochronous URB when the transfer is stopped for
  84331. + * any reason. The fields of the current entry in the frame descriptor array
  84332. + * are set based on the transfer state and the input _halt_status. Completes
  84333. + * the Isochronous URB if all the URB frames have been completed.
  84334. + *
  84335. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  84336. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  84337. + */
  84338. +static dwc_otg_halt_status_e
  84339. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  84340. + dwc_hc_t * hc,
  84341. + dwc_otg_hc_regs_t * hc_regs,
  84342. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  84343. +{
  84344. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  84345. + dwc_otg_halt_status_e ret_val = halt_status;
  84346. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  84347. +
  84348. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  84349. + switch (halt_status) {
  84350. + case DWC_OTG_HC_XFER_COMPLETE:
  84351. + frame_desc->status = 0;
  84352. + frame_desc->actual_length =
  84353. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  84354. +
  84355. + /* non DWORD-aligned buffer case handling. */
  84356. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  84357. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  84358. + hc->qh->dw_align_buf, frame_desc->actual_length);
  84359. + }
  84360. +
  84361. + break;
  84362. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  84363. + urb->error_count++;
  84364. + if (hc->ep_is_in) {
  84365. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  84366. + } else {
  84367. + frame_desc->status = -DWC_E_COMMUNICATION;
  84368. + }
  84369. + frame_desc->actual_length = 0;
  84370. + break;
  84371. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  84372. + urb->error_count++;
  84373. + frame_desc->status = -DWC_E_OVERFLOW;
  84374. + /* Don't need to update actual_length in this case. */
  84375. + break;
  84376. + case DWC_OTG_HC_XFER_XACT_ERR:
  84377. + urb->error_count++;
  84378. + frame_desc->status = -DWC_E_PROTOCOL;
  84379. + frame_desc->actual_length =
  84380. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  84381. +
  84382. + /* non DWORD-aligned buffer case handling. */
  84383. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  84384. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  84385. + hc->qh->dw_align_buf, frame_desc->actual_length);
  84386. + }
  84387. + /* Skip whole frame */
  84388. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  84389. + hc->ep_is_in && hcd->core_if->dma_enable) {
  84390. + qtd->complete_split = 0;
  84391. + qtd->isoc_split_offset = 0;
  84392. + }
  84393. +
  84394. + break;
  84395. + default:
  84396. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  84397. + break;
  84398. + }
  84399. + if (++qtd->isoc_frame_index == urb->packet_count) {
  84400. + /*
  84401. + * urb->status is not used for isoc transfers.
  84402. + * The individual frame_desc statuses are used instead.
  84403. + */
  84404. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  84405. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  84406. + } else {
  84407. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  84408. + }
  84409. + return ret_val;
  84410. +}
  84411. +
  84412. +/**
  84413. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  84414. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  84415. + * still linked to the QH, the QH is added to the end of the inactive
  84416. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  84417. + * schedule if no more QTDs are linked to the QH.
  84418. + */
  84419. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  84420. +{
  84421. + int continue_split = 0;
  84422. + dwc_otg_qtd_t *qtd;
  84423. +
  84424. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  84425. +
  84426. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  84427. +
  84428. + if (qtd->complete_split) {
  84429. + continue_split = 1;
  84430. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  84431. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  84432. + continue_split = 1;
  84433. + }
  84434. +
  84435. + if (free_qtd) {
  84436. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  84437. + continue_split = 0;
  84438. + }
  84439. +
  84440. + qh->channel = NULL;
  84441. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  84442. +}
  84443. +
  84444. +/**
  84445. + * Releases a host channel for use by other transfers. Attempts to select and
  84446. + * queue more transactions since at least one host channel is available.
  84447. + *
  84448. + * @param hcd The HCD state structure.
  84449. + * @param hc The host channel to release.
  84450. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  84451. + * if the transfer is complete or an error has occurred.
  84452. + * @param halt_status Reason the channel is being released. This status
  84453. + * determines the actions taken by this function.
  84454. + */
  84455. +static void release_channel(dwc_otg_hcd_t * hcd,
  84456. + dwc_hc_t * hc,
  84457. + dwc_otg_qtd_t * qtd,
  84458. + dwc_otg_halt_status_e halt_status)
  84459. +{
  84460. + dwc_otg_transaction_type_e tr_type;
  84461. + int free_qtd;
  84462. + dwc_irqflags_t flags;
  84463. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  84464. +
  84465. + int hog_port = 0;
  84466. +
  84467. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  84468. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  84469. +
  84470. + if(fiq_fsm_enable && hc->do_split) {
  84471. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  84472. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  84473. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  84474. + hog_port = 0;
  84475. + }
  84476. + }
  84477. + }
  84478. +
  84479. + switch (halt_status) {
  84480. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  84481. + free_qtd = 1;
  84482. + break;
  84483. + case DWC_OTG_HC_XFER_AHB_ERR:
  84484. + case DWC_OTG_HC_XFER_STALL:
  84485. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  84486. + free_qtd = 1;
  84487. + break;
  84488. + case DWC_OTG_HC_XFER_XACT_ERR:
  84489. + if (qtd->error_count >= 3) {
  84490. + DWC_DEBUGPL(DBG_HCDV,
  84491. + " Complete URB with transaction error\n");
  84492. + free_qtd = 1;
  84493. + qtd->urb->status = -DWC_E_PROTOCOL;
  84494. + hcd->fops->complete(hcd, qtd->urb->priv,
  84495. + qtd->urb, -DWC_E_PROTOCOL);
  84496. + } else {
  84497. + free_qtd = 0;
  84498. + }
  84499. + break;
  84500. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  84501. + /*
  84502. + * The QTD has already been removed and the QH has been
  84503. + * deactivated. Don't want to do anything except release the
  84504. + * host channel and try to queue more transfers.
  84505. + */
  84506. + goto cleanup;
  84507. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  84508. + free_qtd = 0;
  84509. + break;
  84510. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  84511. + DWC_DEBUGPL(DBG_HCDV,
  84512. + " Complete URB with I/O error\n");
  84513. + free_qtd = 1;
  84514. + qtd->urb->status = -DWC_E_IO;
  84515. + hcd->fops->complete(hcd, qtd->urb->priv,
  84516. + qtd->urb, -DWC_E_IO);
  84517. + break;
  84518. + default:
  84519. + free_qtd = 0;
  84520. + break;
  84521. + }
  84522. +
  84523. + deactivate_qh(hcd, hc->qh, free_qtd);
  84524. +
  84525. +cleanup:
  84526. + /*
  84527. + * Release the host channel for use by other transfers. The cleanup
  84528. + * function clears the channel interrupt enables and conditions, so
  84529. + * there's no need to clear the Channel Halted interrupt separately.
  84530. + */
  84531. + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
  84532. + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
  84533. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  84534. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  84535. +
  84536. + if (!microframe_schedule) {
  84537. + switch (hc->ep_type) {
  84538. + case DWC_OTG_EP_TYPE_CONTROL:
  84539. + case DWC_OTG_EP_TYPE_BULK:
  84540. + hcd->non_periodic_channels--;
  84541. + break;
  84542. +
  84543. + default:
  84544. + /*
  84545. + * Don't release reservations for periodic channels here.
  84546. + * That's done when a periodic transfer is descheduled (i.e.
  84547. + * when the QH is removed from the periodic schedule).
  84548. + */
  84549. + break;
  84550. + }
  84551. + } else {
  84552. +
  84553. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  84554. + hcd->available_host_channels++;
  84555. + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
  84556. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  84557. + }
  84558. +
  84559. + /* Try to queue more transfers now that there's a free channel. */
  84560. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  84561. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  84562. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  84563. + }
  84564. +}
  84565. +
  84566. +/**
  84567. + * Halts a host channel. If the channel cannot be halted immediately because
  84568. + * the request queue is full, this function ensures that the FIFO empty
  84569. + * interrupt for the appropriate queue is enabled so that the halt request can
  84570. + * be queued when there is space in the request queue.
  84571. + *
  84572. + * This function may also be called in DMA mode. In that case, the channel is
  84573. + * simply released since the core always halts the channel automatically in
  84574. + * DMA mode.
  84575. + */
  84576. +static void halt_channel(dwc_otg_hcd_t * hcd,
  84577. + dwc_hc_t * hc,
  84578. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  84579. +{
  84580. + if (hcd->core_if->dma_enable) {
  84581. + release_channel(hcd, hc, qtd, halt_status);
  84582. + return;
  84583. + }
  84584. +
  84585. + /* Slave mode processing... */
  84586. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  84587. +
  84588. + if (hc->halt_on_queue) {
  84589. + gintmsk_data_t gintmsk = {.d32 = 0 };
  84590. + dwc_otg_core_global_regs_t *global_regs;
  84591. + global_regs = hcd->core_if->core_global_regs;
  84592. +
  84593. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  84594. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  84595. + /*
  84596. + * Make sure the Non-periodic Tx FIFO empty interrupt
  84597. + * is enabled so that the non-periodic schedule will
  84598. + * be processed.
  84599. + */
  84600. + gintmsk.b.nptxfempty = 1;
  84601. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  84602. + } else {
  84603. + /*
  84604. + * Move the QH from the periodic queued schedule to
  84605. + * the periodic assigned schedule. This allows the
  84606. + * halt to be queued when the periodic schedule is
  84607. + * processed.
  84608. + */
  84609. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  84610. + &hc->qh->qh_list_entry);
  84611. +
  84612. + /*
  84613. + * Make sure the Periodic Tx FIFO Empty interrupt is
  84614. + * enabled so that the periodic schedule will be
  84615. + * processed.
  84616. + */
  84617. + gintmsk.b.ptxfempty = 1;
  84618. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  84619. + }
  84620. + }
  84621. +}
  84622. +
  84623. +/**
  84624. + * Performs common cleanup for non-periodic transfers after a Transfer
  84625. + * Complete interrupt. This function should be called after any endpoint type
  84626. + * specific handling is finished to release the host channel.
  84627. + */
  84628. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  84629. + dwc_hc_t * hc,
  84630. + dwc_otg_hc_regs_t * hc_regs,
  84631. + dwc_otg_qtd_t * qtd,
  84632. + dwc_otg_halt_status_e halt_status)
  84633. +{
  84634. + hcint_data_t hcint;
  84635. +
  84636. + qtd->error_count = 0;
  84637. +
  84638. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  84639. + if (hcint.b.nyet) {
  84640. + /*
  84641. + * Got a NYET on the last transaction of the transfer. This
  84642. + * means that the endpoint should be in the PING state at the
  84643. + * beginning of the next transfer.
  84644. + */
  84645. + hc->qh->ping_state = 1;
  84646. + clear_hc_int(hc_regs, nyet);
  84647. + }
  84648. +
  84649. + /*
  84650. + * Always halt and release the host channel to make it available for
  84651. + * more transfers. There may still be more phases for a control
  84652. + * transfer or more data packets for a bulk transfer at this point,
  84653. + * but the host channel is still halted. A channel will be reassigned
  84654. + * to the transfer when the non-periodic schedule is processed after
  84655. + * the channel is released. This allows transactions to be queued
  84656. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  84657. + * Tx FIFO Empty interrupt if necessary.
  84658. + */
  84659. + if (hc->ep_is_in) {
  84660. + /*
  84661. + * IN transfers in Slave mode require an explicit disable to
  84662. + * halt the channel. (In DMA mode, this call simply releases
  84663. + * the channel.)
  84664. + */
  84665. + halt_channel(hcd, hc, qtd, halt_status);
  84666. + } else {
  84667. + /*
  84668. + * The channel is automatically disabled by the core for OUT
  84669. + * transfers in Slave mode.
  84670. + */
  84671. + release_channel(hcd, hc, qtd, halt_status);
  84672. + }
  84673. +}
  84674. +
  84675. +/**
  84676. + * Performs common cleanup for periodic transfers after a Transfer Complete
  84677. + * interrupt. This function should be called after any endpoint type specific
  84678. + * handling is finished to release the host channel.
  84679. + */
  84680. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  84681. + dwc_hc_t * hc,
  84682. + dwc_otg_hc_regs_t * hc_regs,
  84683. + dwc_otg_qtd_t * qtd,
  84684. + dwc_otg_halt_status_e halt_status)
  84685. +{
  84686. + hctsiz_data_t hctsiz;
  84687. + qtd->error_count = 0;
  84688. +
  84689. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84690. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  84691. + /* Core halts channel in these cases. */
  84692. + release_channel(hcd, hc, qtd, halt_status);
  84693. + } else {
  84694. + /* Flush any outstanding requests from the Tx queue. */
  84695. + halt_channel(hcd, hc, qtd, halt_status);
  84696. + }
  84697. +}
  84698. +
  84699. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  84700. + dwc_hc_t * hc,
  84701. + dwc_otg_hc_regs_t * hc_regs,
  84702. + dwc_otg_qtd_t * qtd)
  84703. +{
  84704. + uint32_t len;
  84705. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  84706. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  84707. +
  84708. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  84709. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  84710. +
  84711. + if (!len) {
  84712. + qtd->complete_split = 0;
  84713. + qtd->isoc_split_offset = 0;
  84714. + return 0;
  84715. + }
  84716. + frame_desc->actual_length += len;
  84717. +
  84718. + if (hc->align_buff && len)
  84719. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  84720. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  84721. + qtd->isoc_split_offset += len;
  84722. +
  84723. + if (frame_desc->length == frame_desc->actual_length) {
  84724. + frame_desc->status = 0;
  84725. + qtd->isoc_frame_index++;
  84726. + qtd->complete_split = 0;
  84727. + qtd->isoc_split_offset = 0;
  84728. + }
  84729. +
  84730. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  84731. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  84732. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  84733. + } else {
  84734. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  84735. + }
  84736. +
  84737. + return 1; /* Indicates that channel released */
  84738. +}
  84739. +
  84740. +/**
  84741. + * Handles a host channel Transfer Complete interrupt. This handler may be
  84742. + * called in either DMA mode or Slave mode.
  84743. + */
  84744. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  84745. + dwc_hc_t * hc,
  84746. + dwc_otg_hc_regs_t * hc_regs,
  84747. + dwc_otg_qtd_t * qtd)
  84748. +{
  84749. + int urb_xfer_done;
  84750. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  84751. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  84752. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  84753. +
  84754. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84755. + "Transfer Complete--\n", hc->hc_num);
  84756. +
  84757. + if (hcd->core_if->dma_desc_enable) {
  84758. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  84759. + if (pipe_type == UE_ISOCHRONOUS) {
  84760. + /* Do not disable the interrupt, just clear it */
  84761. + clear_hc_int(hc_regs, xfercomp);
  84762. + return 1;
  84763. + }
  84764. + goto handle_xfercomp_done;
  84765. + }
  84766. +
  84767. + /*
  84768. + * Handle xfer complete on CSPLIT.
  84769. + */
  84770. +
  84771. + if (hc->qh->do_split) {
  84772. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  84773. + && hcd->core_if->dma_enable) {
  84774. + if (qtd->complete_split
  84775. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  84776. + qtd))
  84777. + goto handle_xfercomp_done;
  84778. + } else {
  84779. + qtd->complete_split = 0;
  84780. + }
  84781. + }
  84782. +
  84783. + /* Update the QTD and URB states. */
  84784. + switch (pipe_type) {
  84785. + case UE_CONTROL:
  84786. + switch (qtd->control_phase) {
  84787. + case DWC_OTG_CONTROL_SETUP:
  84788. + if (urb->length > 0) {
  84789. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  84790. + } else {
  84791. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  84792. + }
  84793. + DWC_DEBUGPL(DBG_HCDV,
  84794. + " Control setup transaction done\n");
  84795. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  84796. + break;
  84797. + case DWC_OTG_CONTROL_DATA:{
  84798. + urb_xfer_done =
  84799. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  84800. + qtd);
  84801. + if (urb_xfer_done) {
  84802. + qtd->control_phase =
  84803. + DWC_OTG_CONTROL_STATUS;
  84804. + DWC_DEBUGPL(DBG_HCDV,
  84805. + " Control data transfer done\n");
  84806. + } else {
  84807. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84808. + }
  84809. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  84810. + break;
  84811. + }
  84812. + case DWC_OTG_CONTROL_STATUS:
  84813. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  84814. + if (urb->status == -DWC_E_IN_PROGRESS) {
  84815. + urb->status = 0;
  84816. + }
  84817. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  84818. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  84819. + break;
  84820. + }
  84821. +
  84822. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  84823. + break;
  84824. + case UE_BULK:
  84825. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  84826. + urb_xfer_done =
  84827. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  84828. + if (urb_xfer_done) {
  84829. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  84830. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  84831. + } else {
  84832. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  84833. + }
  84834. +
  84835. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84836. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  84837. + break;
  84838. + case UE_INTERRUPT:
  84839. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  84840. + urb_xfer_done =
  84841. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  84842. +
  84843. + /*
  84844. + * Interrupt URB is done on the first transfer complete
  84845. + * interrupt.
  84846. + */
  84847. + if (urb_xfer_done) {
  84848. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  84849. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  84850. + } else {
  84851. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  84852. + }
  84853. +
  84854. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  84855. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  84856. + break;
  84857. + case UE_ISOCHRONOUS:
  84858. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  84859. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  84860. + halt_status =
  84861. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  84862. + DWC_OTG_HC_XFER_COMPLETE);
  84863. + }
  84864. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  84865. + break;
  84866. + }
  84867. +
  84868. +handle_xfercomp_done:
  84869. + disable_hc_int(hc_regs, xfercompl);
  84870. +
  84871. + return 1;
  84872. +}
  84873. +
  84874. +/**
  84875. + * Handles a host channel STALL interrupt. This handler may be called in
  84876. + * either DMA mode or Slave mode.
  84877. + */
  84878. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  84879. + dwc_hc_t * hc,
  84880. + dwc_otg_hc_regs_t * hc_regs,
  84881. + dwc_otg_qtd_t * qtd)
  84882. +{
  84883. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  84884. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  84885. +
  84886. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  84887. + "STALL Received--\n", hc->hc_num);
  84888. +
  84889. + if (hcd->core_if->dma_desc_enable) {
  84890. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  84891. + goto handle_stall_done;
  84892. + }
  84893. +
  84894. + if (pipe_type == UE_CONTROL) {
  84895. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  84896. + }
  84897. +
  84898. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  84899. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  84900. + /*
  84901. + * USB protocol requires resetting the data toggle for bulk
  84902. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  84903. + * setup command is issued to the endpoint. Anticipate the
  84904. + * CLEAR_FEATURE command since a STALL has occurred and reset
  84905. + * the data toggle now.
  84906. + */
  84907. + hc->qh->data_toggle = 0;
  84908. + }
  84909. +
  84910. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  84911. +
  84912. +handle_stall_done:
  84913. + disable_hc_int(hc_regs, stall);
  84914. +
  84915. + return 1;
  84916. +}
  84917. +
  84918. +/*
  84919. + * Updates the state of the URB when a transfer has been stopped due to an
  84920. + * abnormal condition before the transfer completes. Modifies the
  84921. + * actual_length field of the URB to reflect the number of bytes that have
  84922. + * actually been transferred via the host channel.
  84923. + */
  84924. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  84925. + dwc_otg_hc_regs_t * hc_regs,
  84926. + dwc_otg_hcd_urb_t * urb,
  84927. + dwc_otg_qtd_t * qtd,
  84928. + dwc_otg_halt_status_e halt_status)
  84929. +{
  84930. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  84931. + halt_status, NULL);
  84932. + /* non DWORD-aligned buffer case handling. */
  84933. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  84934. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  84935. + bytes_transferred);
  84936. + }
  84937. +
  84938. + urb->actual_length += bytes_transferred;
  84939. +
  84940. +#ifdef DEBUG
  84941. + {
  84942. + hctsiz_data_t hctsiz;
  84943. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  84944. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  84945. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  84946. + hc->hc_num);
  84947. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  84948. + hc->start_pkt_count);
  84949. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  84950. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  84951. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  84952. + bytes_transferred);
  84953. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  84954. + urb->actual_length);
  84955. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  84956. + urb->length);
  84957. + }
  84958. +#endif
  84959. +}
  84960. +
  84961. +/**
  84962. + * Handles a host channel NAK interrupt. This handler may be called in either
  84963. + * DMA mode or Slave mode.
  84964. + */
  84965. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  84966. + dwc_hc_t * hc,
  84967. + dwc_otg_hc_regs_t * hc_regs,
  84968. + dwc_otg_qtd_t * qtd)
  84969. +{
  84970. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  84971. + "NAK Received--\n", hc->hc_num);
  84972. +
  84973. + /*
  84974. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  84975. + * the beginning of the next frame
  84976. + */
  84977. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  84978. + case UE_BULK:
  84979. + case UE_CONTROL:
  84980. + if (nak_holdoff && qtd->qh->do_split)
  84981. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  84982. + }
  84983. +
  84984. + /*
  84985. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  84986. + * interrupt. Re-start the SSPLIT transfer.
  84987. + */
  84988. + if (hc->do_split) {
  84989. + if (hc->complete_split) {
  84990. + qtd->error_count = 0;
  84991. + }
  84992. + qtd->complete_split = 0;
  84993. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  84994. + goto handle_nak_done;
  84995. + }
  84996. +
  84997. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  84998. + case UE_CONTROL:
  84999. + case UE_BULK:
  85000. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  85001. + /*
  85002. + * NAK interrupts are enabled on bulk/control IN
  85003. + * transfers in DMA mode for the sole purpose of
  85004. + * resetting the error count after a transaction error
  85005. + * occurs. The core will continue transferring data.
  85006. + * Disable other interrupts unmasked for the same
  85007. + * reason.
  85008. + */
  85009. + disable_hc_int(hc_regs, datatglerr);
  85010. + disable_hc_int(hc_regs, ack);
  85011. + qtd->error_count = 0;
  85012. + goto handle_nak_done;
  85013. + }
  85014. +
  85015. + /*
  85016. + * NAK interrupts normally occur during OUT transfers in DMA
  85017. + * or Slave mode. For IN transfers, more requests will be
  85018. + * queued as request queue space is available.
  85019. + */
  85020. + qtd->error_count = 0;
  85021. +
  85022. + if (!hc->qh->ping_state) {
  85023. + update_urb_state_xfer_intr(hc, hc_regs,
  85024. + qtd->urb, qtd,
  85025. + DWC_OTG_HC_XFER_NAK);
  85026. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85027. +
  85028. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  85029. + hc->qh->ping_state = 1;
  85030. + }
  85031. +
  85032. + /*
  85033. + * Halt the channel so the transfer can be re-started from
  85034. + * the appropriate point or the PING protocol will
  85035. + * start/continue.
  85036. + */
  85037. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  85038. + break;
  85039. + case UE_INTERRUPT:
  85040. + qtd->error_count = 0;
  85041. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  85042. + break;
  85043. + case UE_ISOCHRONOUS:
  85044. + /* Should never get called for isochronous transfers. */
  85045. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  85046. + break;
  85047. + }
  85048. +
  85049. +handle_nak_done:
  85050. + disable_hc_int(hc_regs, nak);
  85051. +
  85052. + return 1;
  85053. +}
  85054. +
  85055. +/**
  85056. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  85057. + * performing the PING protocol in Slave mode, when errors occur during
  85058. + * either Slave mode or DMA mode, and during Start Split transactions.
  85059. + */
  85060. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  85061. + dwc_hc_t * hc,
  85062. + dwc_otg_hc_regs_t * hc_regs,
  85063. + dwc_otg_qtd_t * qtd)
  85064. +{
  85065. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85066. + "ACK Received--\n", hc->hc_num);
  85067. +
  85068. + if (hc->do_split) {
  85069. + /*
  85070. + * Handle ACK on SSPLIT.
  85071. + * ACK should not occur in CSPLIT.
  85072. + */
  85073. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  85074. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  85075. + }
  85076. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  85077. + /* Don't need complete for isochronous out transfers. */
  85078. + qtd->complete_split = 1;
  85079. + }
  85080. +
  85081. + /* ISOC OUT */
  85082. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  85083. + switch (hc->xact_pos) {
  85084. + case DWC_HCSPLIT_XACTPOS_ALL:
  85085. + break;
  85086. + case DWC_HCSPLIT_XACTPOS_END:
  85087. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  85088. + qtd->isoc_split_offset = 0;
  85089. + break;
  85090. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  85091. + case DWC_HCSPLIT_XACTPOS_MID:
  85092. + /*
  85093. + * For BEGIN or MID, calculate the length for
  85094. + * the next microframe to determine the correct
  85095. + * SSPLIT token, either MID or END.
  85096. + */
  85097. + {
  85098. + struct dwc_otg_hcd_iso_packet_desc
  85099. + *frame_desc;
  85100. +
  85101. + frame_desc =
  85102. + &qtd->urb->
  85103. + iso_descs[qtd->isoc_frame_index];
  85104. + qtd->isoc_split_offset += 188;
  85105. +
  85106. + if ((frame_desc->length -
  85107. + qtd->isoc_split_offset) <= 188) {
  85108. + qtd->isoc_split_pos =
  85109. + DWC_HCSPLIT_XACTPOS_END;
  85110. + } else {
  85111. + qtd->isoc_split_pos =
  85112. + DWC_HCSPLIT_XACTPOS_MID;
  85113. + }
  85114. +
  85115. + }
  85116. + break;
  85117. + }
  85118. + } else {
  85119. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  85120. + }
  85121. + } else {
  85122. + /*
  85123. + * An unmasked ACK on a non-split DMA transaction is
  85124. + * for the sole purpose of resetting error counts. Disable other
  85125. + * interrupts unmasked for the same reason.
  85126. + */
  85127. + if(hcd->core_if->dma_enable) {
  85128. + disable_hc_int(hc_regs, datatglerr);
  85129. + disable_hc_int(hc_regs, nak);
  85130. + }
  85131. + qtd->error_count = 0;
  85132. +
  85133. + if (hc->qh->ping_state) {
  85134. + hc->qh->ping_state = 0;
  85135. + /*
  85136. + * Halt the channel so the transfer can be re-started
  85137. + * from the appropriate point. This only happens in
  85138. + * Slave mode. In DMA mode, the ping_state is cleared
  85139. + * when the transfer is started because the core
  85140. + * automatically executes the PING, then the transfer.
  85141. + */
  85142. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  85143. + }
  85144. + }
  85145. +
  85146. + /*
  85147. + * If the ACK occurred when _not_ in the PING state, let the channel
  85148. + * continue transferring data after clearing the error count.
  85149. + */
  85150. +
  85151. + disable_hc_int(hc_regs, ack);
  85152. +
  85153. + return 1;
  85154. +}
  85155. +
  85156. +/**
  85157. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  85158. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  85159. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  85160. + * handled in the xfercomp interrupt handler, not here. This handler may be
  85161. + * called in either DMA mode or Slave mode.
  85162. + */
  85163. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  85164. + dwc_hc_t * hc,
  85165. + dwc_otg_hc_regs_t * hc_regs,
  85166. + dwc_otg_qtd_t * qtd)
  85167. +{
  85168. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85169. + "NYET Received--\n", hc->hc_num);
  85170. +
  85171. + /*
  85172. + * NYET on CSPLIT
  85173. + * re-do the CSPLIT immediately on non-periodic
  85174. + */
  85175. + if (hc->do_split && hc->complete_split) {
  85176. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  85177. + && hcd->core_if->dma_enable) {
  85178. + qtd->complete_split = 0;
  85179. + qtd->isoc_split_offset = 0;
  85180. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  85181. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  85182. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85183. + }
  85184. + else
  85185. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  85186. + goto handle_nyet_done;
  85187. + }
  85188. +
  85189. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  85190. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  85191. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  85192. +
  85193. + // With the FIQ running we only ever see the failed NYET
  85194. + if (dwc_full_frame_num(frnum) !=
  85195. + dwc_full_frame_num(hc->qh->sched_frame) ||
  85196. + fiq_fsm_enable) {
  85197. + /*
  85198. + * No longer in the same full speed frame.
  85199. + * Treat this as a transaction error.
  85200. + */
  85201. +#if 0
  85202. + /** @todo Fix system performance so this can
  85203. + * be treated as an error. Right now complete
  85204. + * splits cannot be scheduled precisely enough
  85205. + * due to other system activity, so this error
  85206. + * occurs regularly in Slave mode.
  85207. + */
  85208. + qtd->error_count++;
  85209. +#endif
  85210. + qtd->complete_split = 0;
  85211. + halt_channel(hcd, hc, qtd,
  85212. + DWC_OTG_HC_XFER_XACT_ERR);
  85213. + /** @todo add support for isoc release */
  85214. + goto handle_nyet_done;
  85215. + }
  85216. + }
  85217. +
  85218. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  85219. + goto handle_nyet_done;
  85220. + }
  85221. +
  85222. + hc->qh->ping_state = 1;
  85223. + qtd->error_count = 0;
  85224. +
  85225. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  85226. + DWC_OTG_HC_XFER_NYET);
  85227. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85228. +
  85229. + /*
  85230. + * Halt the channel and re-start the transfer so the PING
  85231. + * protocol will start.
  85232. + */
  85233. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  85234. +
  85235. +handle_nyet_done:
  85236. + disable_hc_int(hc_regs, nyet);
  85237. + return 1;
  85238. +}
  85239. +
  85240. +/**
  85241. + * Handles a host channel babble interrupt. This handler may be called in
  85242. + * either DMA mode or Slave mode.
  85243. + */
  85244. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  85245. + dwc_hc_t * hc,
  85246. + dwc_otg_hc_regs_t * hc_regs,
  85247. + dwc_otg_qtd_t * qtd)
  85248. +{
  85249. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85250. + "Babble Error--\n", hc->hc_num);
  85251. +
  85252. + if (hcd->core_if->dma_desc_enable) {
  85253. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  85254. + DWC_OTG_HC_XFER_BABBLE_ERR);
  85255. + goto handle_babble_done;
  85256. + }
  85257. +
  85258. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  85259. + hcd->fops->complete(hcd, qtd->urb->priv,
  85260. + qtd->urb, -DWC_E_OVERFLOW);
  85261. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  85262. + } else {
  85263. + dwc_otg_halt_status_e halt_status;
  85264. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  85265. + DWC_OTG_HC_XFER_BABBLE_ERR);
  85266. + halt_channel(hcd, hc, qtd, halt_status);
  85267. + }
  85268. +
  85269. +handle_babble_done:
  85270. + disable_hc_int(hc_regs, bblerr);
  85271. + return 1;
  85272. +}
  85273. +
  85274. +/**
  85275. + * Handles a host channel AHB error interrupt. This handler is only called in
  85276. + * DMA mode.
  85277. + */
  85278. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  85279. + dwc_hc_t * hc,
  85280. + dwc_otg_hc_regs_t * hc_regs,
  85281. + dwc_otg_qtd_t * qtd)
  85282. +{
  85283. + hcchar_data_t hcchar;
  85284. + hcsplt_data_t hcsplt;
  85285. + hctsiz_data_t hctsiz;
  85286. + uint32_t hcdma;
  85287. + char *pipetype, *speed;
  85288. +
  85289. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  85290. +
  85291. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85292. + "AHB Error--\n", hc->hc_num);
  85293. +
  85294. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  85295. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  85296. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  85297. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  85298. +
  85299. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  85300. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  85301. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  85302. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  85303. + DWC_ERROR(" Device address: %d\n",
  85304. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  85305. + DWC_ERROR(" Endpoint: %d, %s\n",
  85306. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  85307. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  85308. +
  85309. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  85310. + case UE_CONTROL:
  85311. + pipetype = "CONTROL";
  85312. + break;
  85313. + case UE_BULK:
  85314. + pipetype = "BULK";
  85315. + break;
  85316. + case UE_INTERRUPT:
  85317. + pipetype = "INTERRUPT";
  85318. + break;
  85319. + case UE_ISOCHRONOUS:
  85320. + pipetype = "ISOCHRONOUS";
  85321. + break;
  85322. + default:
  85323. + pipetype = "UNKNOWN";
  85324. + break;
  85325. + }
  85326. +
  85327. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  85328. +
  85329. + switch (hc->speed) {
  85330. + case DWC_OTG_EP_SPEED_HIGH:
  85331. + speed = "HIGH";
  85332. + break;
  85333. + case DWC_OTG_EP_SPEED_FULL:
  85334. + speed = "FULL";
  85335. + break;
  85336. + case DWC_OTG_EP_SPEED_LOW:
  85337. + speed = "LOW";
  85338. + break;
  85339. + default:
  85340. + speed = "UNKNOWN";
  85341. + break;
  85342. + };
  85343. +
  85344. + DWC_ERROR(" Speed: %s\n", speed);
  85345. +
  85346. + DWC_ERROR(" Max packet size: %d\n",
  85347. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  85348. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  85349. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  85350. + urb->buf, (void *)urb->dma);
  85351. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  85352. + urb->setup_packet, (void *)urb->setup_dma);
  85353. + DWC_ERROR(" Interval: %d\n", urb->interval);
  85354. +
  85355. + /* Core haltes the channel for Descriptor DMA mode */
  85356. + if (hcd->core_if->dma_desc_enable) {
  85357. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  85358. + DWC_OTG_HC_XFER_AHB_ERR);
  85359. + goto handle_ahberr_done;
  85360. + }
  85361. +
  85362. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  85363. +
  85364. + /*
  85365. + * Force a channel halt. Don't call halt_channel because that won't
  85366. + * write to the HCCHARn register in DMA mode to force the halt.
  85367. + */
  85368. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  85369. +handle_ahberr_done:
  85370. + disable_hc_int(hc_regs, ahberr);
  85371. + return 1;
  85372. +}
  85373. +
  85374. +/**
  85375. + * Handles a host channel transaction error interrupt. This handler may be
  85376. + * called in either DMA mode or Slave mode.
  85377. + */
  85378. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  85379. + dwc_hc_t * hc,
  85380. + dwc_otg_hc_regs_t * hc_regs,
  85381. + dwc_otg_qtd_t * qtd)
  85382. +{
  85383. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85384. + "Transaction Error--\n", hc->hc_num);
  85385. +
  85386. + if (hcd->core_if->dma_desc_enable) {
  85387. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  85388. + DWC_OTG_HC_XFER_XACT_ERR);
  85389. + goto handle_xacterr_done;
  85390. + }
  85391. +
  85392. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  85393. + case UE_CONTROL:
  85394. + case UE_BULK:
  85395. + qtd->error_count++;
  85396. + if (!hc->qh->ping_state) {
  85397. +
  85398. + update_urb_state_xfer_intr(hc, hc_regs,
  85399. + qtd->urb, qtd,
  85400. + DWC_OTG_HC_XFER_XACT_ERR);
  85401. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85402. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  85403. + hc->qh->ping_state = 1;
  85404. + }
  85405. + }
  85406. +
  85407. + /*
  85408. + * Halt the channel so the transfer can be re-started from
  85409. + * the appropriate point or the PING protocol will start.
  85410. + */
  85411. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85412. + break;
  85413. + case UE_INTERRUPT:
  85414. + qtd->error_count++;
  85415. + if (hc->do_split && hc->complete_split) {
  85416. + qtd->complete_split = 0;
  85417. + }
  85418. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85419. + break;
  85420. + case UE_ISOCHRONOUS:
  85421. + {
  85422. + dwc_otg_halt_status_e halt_status;
  85423. + halt_status =
  85424. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  85425. + DWC_OTG_HC_XFER_XACT_ERR);
  85426. +
  85427. + halt_channel(hcd, hc, qtd, halt_status);
  85428. + }
  85429. + break;
  85430. + }
  85431. +handle_xacterr_done:
  85432. + disable_hc_int(hc_regs, xacterr);
  85433. +
  85434. + return 1;
  85435. +}
  85436. +
  85437. +/**
  85438. + * Handles a host channel frame overrun interrupt. This handler may be called
  85439. + * in either DMA mode or Slave mode.
  85440. + */
  85441. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  85442. + dwc_hc_t * hc,
  85443. + dwc_otg_hc_regs_t * hc_regs,
  85444. + dwc_otg_qtd_t * qtd)
  85445. +{
  85446. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85447. + "Frame Overrun--\n", hc->hc_num);
  85448. +
  85449. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  85450. + case UE_CONTROL:
  85451. + case UE_BULK:
  85452. + break;
  85453. + case UE_INTERRUPT:
  85454. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  85455. + break;
  85456. + case UE_ISOCHRONOUS:
  85457. + {
  85458. + dwc_otg_halt_status_e halt_status;
  85459. + halt_status =
  85460. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  85461. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  85462. +
  85463. + halt_channel(hcd, hc, qtd, halt_status);
  85464. + }
  85465. + break;
  85466. + }
  85467. +
  85468. + disable_hc_int(hc_regs, frmovrun);
  85469. +
  85470. + return 1;
  85471. +}
  85472. +
  85473. +/**
  85474. + * Handles a host channel data toggle error interrupt. This handler may be
  85475. + * called in either DMA mode or Slave mode.
  85476. + */
  85477. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  85478. + dwc_hc_t * hc,
  85479. + dwc_otg_hc_regs_t * hc_regs,
  85480. + dwc_otg_qtd_t * qtd)
  85481. +{
  85482. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85483. + "Data Toggle Error on %s transfer--\n",
  85484. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  85485. +
  85486. + /* Data toggles on split transactions cause the hc to halt.
  85487. + * restart transfer */
  85488. + if(hc->qh->do_split)
  85489. + {
  85490. + qtd->error_count++;
  85491. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85492. + update_urb_state_xfer_intr(hc, hc_regs,
  85493. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85494. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85495. + } else if (hc->ep_is_in) {
  85496. + /* An unmasked data toggle error on a non-split DMA transaction is
  85497. + * for the sole purpose of resetting error counts. Disable other
  85498. + * interrupts unmasked for the same reason.
  85499. + */
  85500. + if(hcd->core_if->dma_enable) {
  85501. + disable_hc_int(hc_regs, ack);
  85502. + disable_hc_int(hc_regs, nak);
  85503. + }
  85504. + qtd->error_count = 0;
  85505. + }
  85506. +
  85507. + disable_hc_int(hc_regs, datatglerr);
  85508. +
  85509. + return 1;
  85510. +}
  85511. +
  85512. +#ifdef DEBUG
  85513. +/**
  85514. + * This function is for debug only. It checks that a valid halt status is set
  85515. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  85516. + * taken and a warning is issued.
  85517. + * @return 1 if halt status is ok, 0 otherwise.
  85518. + */
  85519. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  85520. + dwc_hc_t * hc,
  85521. + dwc_otg_hc_regs_t * hc_regs,
  85522. + dwc_otg_qtd_t * qtd)
  85523. +{
  85524. + hcchar_data_t hcchar;
  85525. + hctsiz_data_t hctsiz;
  85526. + hcint_data_t hcint;
  85527. + hcintmsk_data_t hcintmsk;
  85528. + hcsplt_data_t hcsplt;
  85529. +
  85530. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  85531. + /*
  85532. + * This code is here only as a check. This condition should
  85533. + * never happen. Ignore the halt if it does occur.
  85534. + */
  85535. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  85536. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  85537. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  85538. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  85539. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  85540. + DWC_WARN
  85541. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  85542. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  85543. + "hcint 0x%08x, hcintmsk 0x%08x, "
  85544. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  85545. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  85546. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  85547. +
  85548. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  85549. + __func__, hc->hc_num);
  85550. + DWC_WARN("\n");
  85551. + clear_hc_int(hc_regs, chhltd);
  85552. + return 0;
  85553. + }
  85554. +
  85555. + /*
  85556. + * This code is here only as a check. hcchar.chdis should
  85557. + * never be set when the halt interrupt occurs. Halt the
  85558. + * channel again if it does occur.
  85559. + */
  85560. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  85561. + if (hcchar.b.chdis) {
  85562. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  85563. + "hcchar 0x%08x, trying to halt again\n",
  85564. + __func__, hcchar.d32);
  85565. + clear_hc_int(hc_regs, chhltd);
  85566. + hc->halt_pending = 0;
  85567. + halt_channel(hcd, hc, qtd, hc->halt_status);
  85568. + return 0;
  85569. + }
  85570. +
  85571. + return 1;
  85572. +}
  85573. +#endif
  85574. +
  85575. +/**
  85576. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  85577. + * determines the reason the channel halted and proceeds accordingly.
  85578. + */
  85579. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  85580. + dwc_hc_t * hc,
  85581. + dwc_otg_hc_regs_t * hc_regs,
  85582. + dwc_otg_qtd_t * qtd)
  85583. +{
  85584. + int out_nak_enh = 0;
  85585. + hcint_data_t hcint;
  85586. + hcintmsk_data_t hcintmsk;
  85587. + /* For core with OUT NAK enhancement, the flow for high-
  85588. + * speed CONTROL/BULK OUT is handled a little differently.
  85589. + */
  85590. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  85591. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  85592. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  85593. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  85594. + out_nak_enh = 1;
  85595. + }
  85596. + }
  85597. +
  85598. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  85599. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  85600. + && !hcd->core_if->dma_desc_enable)) {
  85601. + /*
  85602. + * Just release the channel. A dequeue can happen on a
  85603. + * transfer timeout. In the case of an AHB Error, the channel
  85604. + * was forced to halt because there's no way to gracefully
  85605. + * recover.
  85606. + */
  85607. + if (hcd->core_if->dma_desc_enable)
  85608. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  85609. + hc->halt_status);
  85610. + else
  85611. + release_channel(hcd, hc, qtd, hc->halt_status);
  85612. + return;
  85613. + }
  85614. +
  85615. + /* Read the HCINTn register to determine the cause for the halt. */
  85616. +
  85617. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  85618. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  85619. +
  85620. + if (hcint.b.xfercomp) {
  85621. + /** @todo This is here because of a possible hardware bug. Spec
  85622. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  85623. + * interrupt w/ACK bit set should occur, but I only see the
  85624. + * XFERCOMP bit, even with it masked out. This is a workaround
  85625. + * for that behavior. Should fix this when hardware is fixed.
  85626. + */
  85627. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  85628. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  85629. + }
  85630. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  85631. + } else if (hcint.b.stall) {
  85632. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  85633. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  85634. + if (out_nak_enh) {
  85635. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  85636. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  85637. + qtd->error_count = 0;
  85638. + } else {
  85639. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  85640. + }
  85641. + }
  85642. +
  85643. + /*
  85644. + * Must handle xacterr before nak or ack. Could get a xacterr
  85645. + * at the same time as either of these on a BULK/CONTROL OUT
  85646. + * that started with a PING. The xacterr takes precedence.
  85647. + */
  85648. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  85649. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  85650. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  85651. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  85652. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  85653. + } else if (hcint.b.bblerr) {
  85654. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  85655. + } else if (hcint.b.frmovrun) {
  85656. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  85657. + } else if (hcint.b.datatglerr) {
  85658. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  85659. + } else if (!out_nak_enh) {
  85660. + if (hcint.b.nyet) {
  85661. + /*
  85662. + * Must handle nyet before nak or ack. Could get a nyet at the
  85663. + * same time as either of those on a BULK/CONTROL OUT that
  85664. + * started with a PING. The nyet takes precedence.
  85665. + */
  85666. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  85667. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  85668. + /*
  85669. + * If nak is not masked, it's because a non-split IN transfer
  85670. + * is in an error state. In that case, the nak is handled by
  85671. + * the nak interrupt handler, not here. Handle nak here for
  85672. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  85673. + * rewinding the buffer pointer.
  85674. + */
  85675. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  85676. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  85677. + /*
  85678. + * If ack is not masked, it's because a non-split IN transfer
  85679. + * is in an error state. In that case, the ack is handled by
  85680. + * the ack interrupt handler, not here. Handle ack here for
  85681. + * split transfers. Start splits halt on ACK.
  85682. + */
  85683. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  85684. + } else {
  85685. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  85686. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  85687. + /*
  85688. + * A periodic transfer halted with no other channel
  85689. + * interrupts set. Assume it was halted by the core
  85690. + * because it could not be completed in its scheduled
  85691. + * (micro)frame.
  85692. + */
  85693. +#ifdef DEBUG
  85694. + DWC_PRINTF
  85695. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  85696. + __func__, hc->hc_num);
  85697. +#endif
  85698. + halt_channel(hcd, hc, qtd,
  85699. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  85700. + } else {
  85701. + DWC_ERROR
  85702. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  85703. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  85704. + __func__, hc->hc_num, hcint.d32,
  85705. + DWC_READ_REG32(&hcd->
  85706. + core_if->core_global_regs->
  85707. + gintsts));
  85708. + /* Failthrough: use 3-strikes rule */
  85709. + qtd->error_count++;
  85710. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85711. + update_urb_state_xfer_intr(hc, hc_regs,
  85712. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85713. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85714. + }
  85715. +
  85716. + }
  85717. + } else {
  85718. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  85719. + hcint.d32);
  85720. + /* Failthrough: use 3-strikes rule */
  85721. + qtd->error_count++;
  85722. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85723. + update_urb_state_xfer_intr(hc, hc_regs,
  85724. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85725. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  85726. + }
  85727. +}
  85728. +
  85729. +/**
  85730. + * Handles a host channel Channel Halted interrupt.
  85731. + *
  85732. + * In slave mode, this handler is called only when the driver specifically
  85733. + * requests a halt. This occurs during handling other host channel interrupts
  85734. + * (e.g. nak, xacterr, stall, nyet, etc.).
  85735. + *
  85736. + * In DMA mode, this is the interrupt that occurs when the core has finished
  85737. + * processing a transfer on a channel. Other host channel interrupts (except
  85738. + * ahberr) are disabled in DMA mode.
  85739. + */
  85740. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  85741. + dwc_hc_t * hc,
  85742. + dwc_otg_hc_regs_t * hc_regs,
  85743. + dwc_otg_qtd_t * qtd)
  85744. +{
  85745. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  85746. + "Channel Halted--\n", hc->hc_num);
  85747. +
  85748. + if (hcd->core_if->dma_enable) {
  85749. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
  85750. + } else {
  85751. +#ifdef DEBUG
  85752. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  85753. + return 1;
  85754. + }
  85755. +#endif
  85756. + release_channel(hcd, hc, qtd, hc->halt_status);
  85757. + }
  85758. +
  85759. + return 1;
  85760. +}
  85761. +
  85762. +
  85763. +/**
  85764. + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
  85765. + * FIQ transfer completion
  85766. + * @hcd: Pointer to dwc_otg_hcd struct
  85767. + * @num: Host channel number
  85768. + *
  85769. + * 1. Un-mangle the status as recorded in each iso_frame_desc status
  85770. + * 2. Copy it from the dwc_otg_urb into the real URB
  85771. + */
  85772. +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  85773. +{
  85774. + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
  85775. + int nr_frames = dwc_urb->packet_count;
  85776. + int i;
  85777. + hcint_data_t frame_hcint;
  85778. +
  85779. + for (i = 0; i < nr_frames; i++) {
  85780. + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
  85781. + if (frame_hcint.b.xfercomp) {
  85782. + dwc_urb->iso_descs[i].status = 0;
  85783. + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
  85784. + } else if (frame_hcint.b.frmovrun) {
  85785. + if (qh->ep_is_in)
  85786. + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
  85787. + else
  85788. + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
  85789. + dwc_urb->error_count++;
  85790. + dwc_urb->iso_descs[i].actual_length = 0;
  85791. + } else if (frame_hcint.b.xacterr) {
  85792. + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
  85793. + dwc_urb->error_count++;
  85794. + dwc_urb->iso_descs[i].actual_length = 0;
  85795. + } else if (frame_hcint.b.bblerr) {
  85796. + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
  85797. + dwc_urb->error_count++;
  85798. + dwc_urb->iso_descs[i].actual_length = 0;
  85799. + } else {
  85800. + /* Something went wrong */
  85801. + dwc_urb->iso_descs[i].status = -1;
  85802. + dwc_urb->iso_descs[i].actual_length = 0;
  85803. + dwc_urb->error_count++;
  85804. + }
  85805. + }
  85806. + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
  85807. + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
  85808. + hcd->fops->complete(hcd, dwc_urb->priv, dwc_urb, 0);
  85809. + release_channel(hcd, qh->channel, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85810. +}
  85811. +
  85812. +/**
  85813. + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
  85814. + * @hcd: Pointer to dwc_otg_hcd struct
  85815. + * @num: Host channel number
  85816. + *
  85817. + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
  85818. + * Returns total length of data or -1 if the buffers were not used.
  85819. + *
  85820. + */
  85821. +int dwc_otg_fiq_unsetup_per_dma(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  85822. +{
  85823. + dwc_hc_t *hc = qh->channel;
  85824. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  85825. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  85826. + uint8_t *ptr = NULL;
  85827. + int index = 0, len = 0;
  85828. + int i = 0;
  85829. + if (hc->ep_is_in) {
  85830. + /* Copy data out of the DMA bounce buffers to the URB's buffer.
  85831. + * The align_buf is ignored as this is ignored on FSM enqueue. */
  85832. + ptr = qtd->urb->buf;
  85833. + if (qh->ep_type == UE_ISOCHRONOUS) {
  85834. + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
  85835. + index = qtd->isoc_frame_index;
  85836. + ptr += qtd->urb->iso_descs[index].offset;
  85837. + } else {
  85838. + /* Need to increment by actual_length for interrupt IN */
  85839. + ptr += qtd->urb->actual_length;
  85840. + }
  85841. +
  85842. + for (i = 0; i < st->dma_info.index; i++) {
  85843. + len += st->dma_info.slot_len[i];
  85844. + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
  85845. + ptr += st->dma_info.slot_len[i];
  85846. + }
  85847. + return len;
  85848. + } else {
  85849. + /* OUT endpoints - nothing to do. */
  85850. + return -1;
  85851. + }
  85852. +
  85853. +}
  85854. +/**
  85855. + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
  85856. + * from a channel handled in the FIQ
  85857. + * @hcd: Pointer to dwc_otg_hcd struct
  85858. + * @num: Host channel number
  85859. + *
  85860. + * If a host channel interrupt was received by the IRQ and this was a channel
  85861. + * used by the FIQ, the execution flow for transfer completion is substantially
  85862. + * different from the normal (messy) path. This function and its friends handles
  85863. + * channel cleanup and transaction completion from a FIQ transaction.
  85864. + */
  85865. +int32_t dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
  85866. +{
  85867. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  85868. + dwc_hc_t *hc = hcd->hc_ptr_array[num];
  85869. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  85870. + dwc_otg_qh_t *qh = hc->qh;
  85871. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
  85872. + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
  85873. + int hostchannels = 0;
  85874. + int ret = 0;
  85875. + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
  85876. +
  85877. + hostchannels = hcd->available_host_channels;
  85878. + switch (st->fsm) {
  85879. + case FIQ_TEST:
  85880. + break;
  85881. +
  85882. + case FIQ_DEQUEUE_ISSUED:
  85883. + /* hc_halt was called. QTD no longer exists. */
  85884. + /* TODO: for a nonperiodic split transaction, need to issue a
  85885. + * CLEAR_TT_BUFFER hub command if we were in the start-split phase.
  85886. + */
  85887. + release_channel(hcd, hc, NULL, hc->halt_status);
  85888. + ret = 1;
  85889. + break;
  85890. +
  85891. + case FIQ_NP_SPLIT_DONE:
  85892. + /* Nonperiodic transaction complete. */
  85893. + if (!hc->ep_is_in) {
  85894. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  85895. + }
  85896. + if (hcint.b.xfercomp) {
  85897. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  85898. + } else if (hcint.b.nak) {
  85899. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  85900. + }
  85901. + ret = 1;
  85902. + break;
  85903. +
  85904. + case FIQ_NP_SPLIT_HS_ABORTED:
  85905. + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
  85906. + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
  85907. + * because there's no guarantee which order a non-periodic split happened in.
  85908. + * We could end up clearing a perfectly good transaction out of the buffer.
  85909. + */
  85910. + if (hcint.b.xacterr) {
  85911. + qtd->error_count += st->nr_errors;
  85912. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  85913. + } else if (hcint.b.ahberr) {
  85914. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  85915. + } else {
  85916. + local_fiq_disable();
  85917. + BUG();
  85918. + }
  85919. + break;
  85920. +
  85921. + case FIQ_NP_SPLIT_LS_ABORTED:
  85922. + /* A few cases can cause this - either an unknown state on a SSPLIT or
  85923. + * STALL/data toggle error response on a CSPLIT */
  85924. + if (hcint.b.stall) {
  85925. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  85926. + } else if (hcint.b.datatglerr) {
  85927. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  85928. + } else if (hcint.b.bblerr) {
  85929. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  85930. + } else if (hcint.b.ahberr) {
  85931. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  85932. + } else {
  85933. + local_fiq_disable();
  85934. + BUG();
  85935. + }
  85936. + break;
  85937. +
  85938. + case FIQ_PER_SPLIT_DONE:
  85939. + /* Isoc IN or Interrupt IN/OUT */
  85940. +
  85941. + /* Flow control here is different from the normal execution by the driver.
  85942. + * We need to completely ignore most of the driver's method of handling
  85943. + * split transactions and do it ourselves.
  85944. + */
  85945. + if (hc->ep_type == UE_INTERRUPT) {
  85946. + if (hcint.b.nak) {
  85947. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  85948. + } else if (hc->ep_is_in) {
  85949. + int len;
  85950. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  85951. + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
  85952. + qtd->urb->actual_length += len;
  85953. + if (qtd->urb->actual_length >= qtd->urb->length) {
  85954. + qtd->urb->status = 0;
  85955. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  85956. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85957. + } else {
  85958. + /* Interrupt transfer not complete yet - is it a short read? */
  85959. + if (len < hc->max_packet) {
  85960. + /* Interrupt transaction complete */
  85961. + qtd->urb->status = 0;
  85962. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  85963. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85964. + } else {
  85965. + /* Further transactions required */
  85966. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85967. + }
  85968. + }
  85969. + } else {
  85970. + /* Interrupt OUT complete. */
  85971. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  85972. + qtd->urb->actual_length += hc->xfer_len;
  85973. + if (qtd->urb->actual_length >= qtd->urb->length) {
  85974. + qtd->urb->status = 0;
  85975. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  85976. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85977. + } else {
  85978. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  85979. + }
  85980. + }
  85981. + } else {
  85982. + /* ISOC IN complete. */
  85983. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  85984. + int len = 0;
  85985. + /* Record errors, update qtd. */
  85986. + if (st->nr_errors) {
  85987. + frame_desc->actual_length = 0;
  85988. + frame_desc->status = -DWC_E_PROTOCOL;
  85989. + } else {
  85990. + frame_desc->status = 0;
  85991. + /* Unswizzle dma */
  85992. + len = dwc_otg_fiq_unsetup_per_dma(hcd, qh, qtd, num);
  85993. + frame_desc->actual_length = len;
  85994. + }
  85995. + qtd->isoc_frame_index++;
  85996. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  85997. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  85998. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  85999. + } else {
  86000. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  86001. + }
  86002. + }
  86003. + break;
  86004. +
  86005. + case FIQ_PER_ISO_OUT_DONE: {
  86006. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  86007. + /* Record errors, update qtd. */
  86008. + if (st->nr_errors) {
  86009. + frame_desc->actual_length = 0;
  86010. + frame_desc->status = -DWC_E_PROTOCOL;
  86011. + } else {
  86012. + frame_desc->status = 0;
  86013. + frame_desc->actual_length = frame_desc->length;
  86014. + }
  86015. + qtd->isoc_frame_index++;
  86016. + qtd->isoc_split_offset = 0;
  86017. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  86018. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  86019. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  86020. + } else {
  86021. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  86022. + }
  86023. + }
  86024. + break;
  86025. +
  86026. + case FIQ_PER_SPLIT_NYET_ABORTED:
  86027. + /* Doh. lost the data. */
  86028. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  86029. + "- FIQ reported NYET. Data may have been lost.\n",
  86030. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  86031. + if (hc->ep_type == UE_ISOCHRONOUS) {
  86032. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  86033. + /* Record errors, update qtd. */
  86034. + frame_desc->actual_length = 0;
  86035. + frame_desc->status = -DWC_E_PROTOCOL;
  86036. + qtd->isoc_frame_index++;
  86037. + qtd->isoc_split_offset = 0;
  86038. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  86039. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  86040. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  86041. + } else {
  86042. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  86043. + }
  86044. + } else {
  86045. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  86046. + }
  86047. + break;
  86048. +
  86049. + case FIQ_HS_ISOC_DONE:
  86050. + /* The FIQ has performed a whole pile of isochronous transactions.
  86051. + * The status is recorded as the interrupt state should the transaction
  86052. + * fail.
  86053. + */
  86054. + dwc_otg_fiq_unmangle_isoc(hcd, qh, qtd, num);
  86055. + break;
  86056. +
  86057. + case FIQ_PER_SPLIT_LS_ABORTED:
  86058. + if (hcint.b.xacterr) {
  86059. + /* Hub has responded with an ERR packet. Device
  86060. + * has been unplugged or the port has been disabled.
  86061. + * TODO: need to issue a reset to the hub port. */
  86062. + qtd->error_count += 3;
  86063. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  86064. + } else if (hcint.b.stall) {
  86065. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  86066. + } else if (hcint.b.bblerr) {
  86067. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  86068. + } else {
  86069. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
  86070. + "- FIQ reported FSM=%d. Data may have been lost.\n",
  86071. + st->fsm, hc->dev_addr, hc->ep_num);
  86072. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  86073. + }
  86074. + break;
  86075. +
  86076. + case FIQ_PER_SPLIT_HS_ABORTED:
  86077. + /* Either the SSPLIT phase suffered transaction errors or something
  86078. + * unexpected happened.
  86079. + */
  86080. + qtd->error_count += 3;
  86081. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  86082. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  86083. + break;
  86084. +
  86085. + case FIQ_PER_SPLIT_TIMEOUT:
  86086. + /* Couldn't complete in the nominated frame */
  86087. + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  86088. + "- FIQ timed out. Data may have been lost.\n",
  86089. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  86090. + if (hc->ep_type == UE_ISOCHRONOUS) {
  86091. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  86092. + /* Record errors, update qtd. */
  86093. + frame_desc->actual_length = 0;
  86094. + if (hc->ep_is_in) {
  86095. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  86096. + } else {
  86097. + frame_desc->status = -DWC_E_COMMUNICATION;
  86098. + }
  86099. + qtd->isoc_frame_index++;
  86100. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  86101. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  86102. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  86103. + } else {
  86104. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  86105. + }
  86106. + } else {
  86107. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  86108. + }
  86109. + break;
  86110. +
  86111. + default:
  86112. + local_fiq_disable();
  86113. + DWC_WARN("unexpected state received on hc=%d fsm=%d", hc->hc_num, st->fsm);
  86114. + BUG();
  86115. + }
  86116. + //if (hostchannels != hcd->available_host_channels) {
  86117. + /* should have incremented by now! */
  86118. + // BUG();
  86119. +// }
  86120. + return ret;
  86121. +}
  86122. +
  86123. +/** Handles interrupt for a specific Host Channel */
  86124. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  86125. +{
  86126. + int retval = 0;
  86127. + hcint_data_t hcint;
  86128. + hcintmsk_data_t hcintmsk;
  86129. + dwc_hc_t *hc;
  86130. + dwc_otg_hc_regs_t *hc_regs;
  86131. + dwc_otg_qtd_t *qtd;
  86132. +
  86133. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  86134. +
  86135. + hc = dwc_otg_hcd->hc_ptr_array[num];
  86136. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  86137. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  86138. + /* We are responding to a channel disable. Driver
  86139. + * state is cleared - our qtd has gone away.
  86140. + */
  86141. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  86142. + return 1;
  86143. + }
  86144. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  86145. +
  86146. + /*
  86147. + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
  86148. + * Execution path is fundamentally different for the channels after a FIQ has completed
  86149. + * a split transaction.
  86150. + */
  86151. + if (fiq_fsm_enable) {
  86152. + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
  86153. + case FIQ_PASSTHROUGH:
  86154. + break;
  86155. + case FIQ_PASSTHROUGH_ERRORSTATE:
  86156. + /* Hook into the error count */
  86157. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
  86158. + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
  86159. + qtd->error_count = 0;
  86160. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
  86161. + }
  86162. + break;
  86163. + default:
  86164. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  86165. + return 1;
  86166. + }
  86167. + }
  86168. +
  86169. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  86170. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  86171. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  86172. + if (!dwc_otg_hcd->core_if->dma_enable) {
  86173. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  86174. + hcint.b.chhltd = 0;
  86175. + }
  86176. + }
  86177. +
  86178. + if (hcint.b.xfercomp) {
  86179. + retval |=
  86180. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86181. + /*
  86182. + * If NYET occurred at same time as Xfer Complete, the NYET is
  86183. + * handled by the Xfer Complete interrupt handler. Don't want
  86184. + * to call the NYET interrupt handler in this case.
  86185. + */
  86186. + hcint.b.nyet = 0;
  86187. + }
  86188. + if (hcint.b.chhltd) {
  86189. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86190. + }
  86191. + if (hcint.b.ahberr) {
  86192. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86193. + }
  86194. + if (hcint.b.stall) {
  86195. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86196. + }
  86197. + if (hcint.b.nak) {
  86198. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86199. + }
  86200. + if (hcint.b.ack) {
  86201. + if(!hcint.b.chhltd)
  86202. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86203. + }
  86204. + if (hcint.b.nyet) {
  86205. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86206. + }
  86207. + if (hcint.b.xacterr) {
  86208. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86209. + }
  86210. + if (hcint.b.bblerr) {
  86211. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86212. + }
  86213. + if (hcint.b.frmovrun) {
  86214. + retval |=
  86215. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86216. + }
  86217. + if (hcint.b.datatglerr) {
  86218. + retval |=
  86219. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  86220. + }
  86221. +
  86222. + return retval;
  86223. +}
  86224. +#endif /* DWC_DEVICE_ONLY */
  86225. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  86226. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  86227. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c 2014-09-14 19:04:13.000000000 +0200
  86228. @@ -0,0 +1,985 @@
  86229. +
  86230. +/* ==========================================================================
  86231. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  86232. + * $Revision: #20 $
  86233. + * $Date: 2011/10/26 $
  86234. + * $Change: 1872981 $
  86235. + *
  86236. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  86237. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  86238. + * otherwise expressly agreed to in writing between Synopsys and you.
  86239. + *
  86240. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  86241. + * any End User Software License Agreement or Agreement for Licensed Product
  86242. + * with Synopsys or any supplement thereto. You are permitted to use and
  86243. + * redistribute this Software in source and binary forms, with or without
  86244. + * modification, provided that redistributions of source code must retain this
  86245. + * notice. You may not view, use, disclose, copy or distribute this file or
  86246. + * any information contained herein except pursuant to this license grant from
  86247. + * Synopsys. If you do not agree with this notice, including the disclaimer
  86248. + * below, then you are not authorized to use the Software.
  86249. + *
  86250. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  86251. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  86252. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  86253. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  86254. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  86255. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86256. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  86257. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  86258. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  86259. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  86260. + * DAMAGE.
  86261. + * ========================================================================== */
  86262. +#ifndef DWC_DEVICE_ONLY
  86263. +
  86264. +/**
  86265. + * @file
  86266. + *
  86267. + * This file contains the implementation of the HCD. In Linux, the HCD
  86268. + * implements the hc_driver API.
  86269. + */
  86270. +#include <linux/kernel.h>
  86271. +#include <linux/module.h>
  86272. +#include <linux/moduleparam.h>
  86273. +#include <linux/init.h>
  86274. +#include <linux/device.h>
  86275. +#include <linux/errno.h>
  86276. +#include <linux/list.h>
  86277. +#include <linux/interrupt.h>
  86278. +#include <linux/string.h>
  86279. +#include <linux/dma-mapping.h>
  86280. +#include <linux/version.h>
  86281. +#include <asm/io.h>
  86282. +#include <asm/fiq.h>
  86283. +#include <linux/usb.h>
  86284. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  86285. +#include <../drivers/usb/core/hcd.h>
  86286. +#else
  86287. +#include <linux/usb/hcd.h>
  86288. +#endif
  86289. +#include <asm/bug.h>
  86290. +
  86291. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  86292. +#define USB_URB_EP_LINKING 1
  86293. +#else
  86294. +#define USB_URB_EP_LINKING 0
  86295. +#endif
  86296. +
  86297. +#include "dwc_otg_hcd_if.h"
  86298. +#include "dwc_otg_dbg.h"
  86299. +#include "dwc_otg_driver.h"
  86300. +#include "dwc_otg_hcd.h"
  86301. +
  86302. +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
  86303. +
  86304. +/**
  86305. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  86306. + * qualified with its direction (possible 32 endpoints per device).
  86307. + */
  86308. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  86309. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  86310. +
  86311. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  86312. +
  86313. +extern bool fiq_enable;
  86314. +
  86315. +/** @name Linux HC Driver API Functions */
  86316. +/** @{ */
  86317. +/* manage i/o requests, device state */
  86318. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  86319. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  86320. + struct usb_host_endpoint *ep,
  86321. +#endif
  86322. + struct urb *urb, gfp_t mem_flags);
  86323. +
  86324. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  86325. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  86326. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  86327. +#endif
  86328. +#else /* kernels at or post 2.6.30 */
  86329. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  86330. + struct urb *urb, int status);
  86331. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  86332. +
  86333. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  86334. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  86335. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  86336. +#endif
  86337. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  86338. +extern int hcd_start(struct usb_hcd *hcd);
  86339. +extern void hcd_stop(struct usb_hcd *hcd);
  86340. +static int get_frame_number(struct usb_hcd *hcd);
  86341. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  86342. +extern int hub_control(struct usb_hcd *hcd,
  86343. + u16 typeReq,
  86344. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  86345. +
  86346. +struct wrapper_priv_data {
  86347. + dwc_otg_hcd_t *dwc_otg_hcd;
  86348. +};
  86349. +
  86350. +/** @} */
  86351. +
  86352. +static struct hc_driver dwc_otg_hc_driver = {
  86353. +
  86354. + .description = dwc_otg_hcd_name,
  86355. + .product_desc = "DWC OTG Controller",
  86356. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  86357. +
  86358. + .irq = dwc_otg_hcd_irq,
  86359. +
  86360. + .flags = HCD_MEMORY | HCD_USB2,
  86361. +
  86362. + //.reset =
  86363. + .start = hcd_start,
  86364. + //.suspend =
  86365. + //.resume =
  86366. + .stop = hcd_stop,
  86367. +
  86368. + .urb_enqueue = dwc_otg_urb_enqueue,
  86369. + .urb_dequeue = dwc_otg_urb_dequeue,
  86370. + .endpoint_disable = endpoint_disable,
  86371. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  86372. + .endpoint_reset = endpoint_reset,
  86373. +#endif
  86374. + .get_frame_number = get_frame_number,
  86375. +
  86376. + .hub_status_data = hub_status_data,
  86377. + .hub_control = hub_control,
  86378. + //.bus_suspend =
  86379. + //.bus_resume =
  86380. +};
  86381. +
  86382. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  86383. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  86384. +{
  86385. + struct wrapper_priv_data *p;
  86386. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  86387. + return p->dwc_otg_hcd;
  86388. +}
  86389. +
  86390. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  86391. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  86392. +{
  86393. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  86394. +}
  86395. +
  86396. +/** Gets the usb_host_endpoint associated with an URB. */
  86397. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  86398. +{
  86399. + struct usb_device *dev = urb->dev;
  86400. + int ep_num = usb_pipeendpoint(urb->pipe);
  86401. +
  86402. + if (usb_pipein(urb->pipe))
  86403. + return dev->ep_in[ep_num];
  86404. + else
  86405. + return dev->ep_out[ep_num];
  86406. +}
  86407. +
  86408. +static int _disconnect(dwc_otg_hcd_t * hcd)
  86409. +{
  86410. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  86411. +
  86412. + usb_hcd->self.is_b_host = 0;
  86413. + return 0;
  86414. +}
  86415. +
  86416. +static int _start(dwc_otg_hcd_t * hcd)
  86417. +{
  86418. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  86419. +
  86420. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  86421. + hcd_start(usb_hcd);
  86422. +
  86423. + return 0;
  86424. +}
  86425. +
  86426. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  86427. + uint32_t * port_addr)
  86428. +{
  86429. + struct urb *urb = (struct urb *)urb_handle;
  86430. + struct usb_bus *bus;
  86431. +#if 1 //GRAYG - temporary
  86432. + if (NULL == urb_handle)
  86433. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  86434. + if (NULL == urb->dev)
  86435. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  86436. + if (NULL == port_addr)
  86437. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  86438. +#endif
  86439. + if (urb->dev->tt) {
  86440. + if (NULL == urb->dev->tt->hub) {
  86441. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  86442. + __func__); //GRAYG
  86443. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  86444. + *hub_addr = 0; //GRAYG
  86445. + // we probably shouldn't have a transaction translator if
  86446. + // there's no associated hub?
  86447. + } else {
  86448. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  86449. + if (urb->dev->tt->hub == bus->root_hub)
  86450. + *hub_addr = 0;
  86451. + else
  86452. + *hub_addr = urb->dev->tt->hub->devnum;
  86453. + }
  86454. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  86455. + } else {
  86456. + *hub_addr = 0;
  86457. + *port_addr = urb->dev->ttport;
  86458. + }
  86459. + return 0;
  86460. +}
  86461. +
  86462. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  86463. +{
  86464. + struct urb *urb = (struct urb *)urb_handle;
  86465. + return urb->dev->speed;
  86466. +}
  86467. +
  86468. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  86469. +{
  86470. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  86471. + return usb_hcd->self.b_hnp_enable;
  86472. +}
  86473. +
  86474. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  86475. + struct urb *urb)
  86476. +{
  86477. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  86478. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  86479. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  86480. + } else {
  86481. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  86482. + }
  86483. +}
  86484. +
  86485. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  86486. + struct urb *urb)
  86487. +{
  86488. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  86489. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  86490. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  86491. + } else {
  86492. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  86493. + }
  86494. +}
  86495. +
  86496. +/**
  86497. + * Sets the final status of an URB and returns it to the device driver. Any
  86498. + * required cleanup of the URB is performed. The HCD lock should be held on
  86499. + * entry.
  86500. + */
  86501. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  86502. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  86503. +{
  86504. + struct urb *urb = (struct urb *)urb_handle;
  86505. + urb_tq_entry_t *new_entry;
  86506. + int rc = 0;
  86507. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  86508. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  86509. + __func__, urb, usb_pipedevice(urb->pipe),
  86510. + usb_pipeendpoint(urb->pipe),
  86511. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  86512. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  86513. + int i;
  86514. + for (i = 0; i < urb->number_of_packets; i++) {
  86515. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  86516. + i, urb->iso_frame_desc[i].status);
  86517. + }
  86518. + }
  86519. + }
  86520. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  86521. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  86522. + /* Convert status value. */
  86523. + switch (status) {
  86524. + case -DWC_E_PROTOCOL:
  86525. + status = -EPROTO;
  86526. + break;
  86527. + case -DWC_E_IN_PROGRESS:
  86528. + status = -EINPROGRESS;
  86529. + break;
  86530. + case -DWC_E_PIPE:
  86531. + status = -EPIPE;
  86532. + break;
  86533. + case -DWC_E_IO:
  86534. + status = -EIO;
  86535. + break;
  86536. + case -DWC_E_TIMEOUT:
  86537. + status = -ETIMEDOUT;
  86538. + break;
  86539. + case -DWC_E_OVERFLOW:
  86540. + status = -EOVERFLOW;
  86541. + break;
  86542. + case -DWC_E_SHUTDOWN:
  86543. + status = -ESHUTDOWN;
  86544. + break;
  86545. + default:
  86546. + if (status) {
  86547. + DWC_PRINTF("Uknown urb status %d\n", status);
  86548. +
  86549. + }
  86550. + }
  86551. +
  86552. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  86553. + int i;
  86554. +
  86555. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  86556. + for (i = 0; i < urb->number_of_packets; ++i) {
  86557. + urb->iso_frame_desc[i].actual_length =
  86558. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  86559. + (dwc_otg_urb, i);
  86560. + urb->iso_frame_desc[i].status =
  86561. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  86562. + }
  86563. + }
  86564. +
  86565. + urb->status = status;
  86566. + urb->hcpriv = NULL;
  86567. + if (!status) {
  86568. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  86569. + (urb->actual_length < urb->transfer_buffer_length)) {
  86570. + urb->status = -EREMOTEIO;
  86571. + }
  86572. + }
  86573. +
  86574. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  86575. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  86576. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  86577. + if (ep) {
  86578. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  86579. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  86580. + ep->hcpriv),
  86581. + urb);
  86582. + }
  86583. + }
  86584. + DWC_FREE(dwc_otg_urb);
  86585. + if (!new_entry) {
  86586. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  86587. + urb->status = -EPROTO;
  86588. + /* don't schedule the tasklet -
  86589. + * directly return the packet here with error. */
  86590. +#if USB_URB_EP_LINKING
  86591. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  86592. +#endif
  86593. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  86594. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  86595. +#else
  86596. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  86597. +#endif
  86598. + } else {
  86599. + new_entry->urb = urb;
  86600. +#if USB_URB_EP_LINKING
  86601. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  86602. + if(0 == rc) {
  86603. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  86604. + }
  86605. +#endif
  86606. + if(0 == rc) {
  86607. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  86608. + urb_tq_entries);
  86609. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  86610. + }
  86611. + }
  86612. + return 0;
  86613. +}
  86614. +
  86615. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  86616. + .start = _start,
  86617. + .disconnect = _disconnect,
  86618. + .hub_info = _hub_info,
  86619. + .speed = _speed,
  86620. + .complete = _complete,
  86621. + .get_b_hnp_enable = _get_b_hnp_enable,
  86622. +};
  86623. +
  86624. +static struct fiq_handler fh = {
  86625. + .name = "usb_fiq",
  86626. +};
  86627. +
  86628. +
  86629. +
  86630. +/**
  86631. + * Initializes the HCD. This function allocates memory for and initializes the
  86632. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  86633. + * USB bus with the core and calls the hc_driver->start() function. It returns
  86634. + * a negative error on failure.
  86635. + */
  86636. +int hcd_init(dwc_bus_dev_t *_dev)
  86637. +{
  86638. + struct usb_hcd *hcd = NULL;
  86639. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  86640. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  86641. + int retval = 0;
  86642. + u64 dmamask;
  86643. + struct pt_regs regs;
  86644. +
  86645. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  86646. +
  86647. + /* Set device flags indicating whether the HCD supports DMA. */
  86648. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  86649. + dmamask = DMA_BIT_MASK(32);
  86650. + else
  86651. + dmamask = 0;
  86652. +
  86653. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  86654. + dma_set_mask(&_dev->dev, dmamask);
  86655. + dma_set_coherent_mask(&_dev->dev, dmamask);
  86656. +#elif defined(PCI_INTERFACE)
  86657. + pci_set_dma_mask(_dev, dmamask);
  86658. + pci_set_consistent_dma_mask(_dev, dmamask);
  86659. +#endif
  86660. +
  86661. + /*
  86662. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  86663. + * Initialize the base HCD.
  86664. + */
  86665. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  86666. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  86667. +#else
  86668. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  86669. + hcd->has_tt = 1;
  86670. +// hcd->uses_new_polling = 1;
  86671. +// hcd->poll_rh = 0;
  86672. +#endif
  86673. + if (!hcd) {
  86674. + retval = -ENOMEM;
  86675. + goto error1;
  86676. + }
  86677. +
  86678. + hcd->regs = otg_dev->os_dep.base;
  86679. +
  86680. +
  86681. + /* Initialize the DWC OTG HCD. */
  86682. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  86683. + if (!dwc_otg_hcd) {
  86684. + goto error2;
  86685. + }
  86686. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  86687. + dwc_otg_hcd;
  86688. + otg_dev->hcd = dwc_otg_hcd;
  86689. +
  86690. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  86691. + goto error2;
  86692. + }
  86693. +
  86694. + if (fiq_enable)
  86695. + {
  86696. + if (claim_fiq(&fh)) {
  86697. + DWC_ERROR("Can't claim FIQ");
  86698. + goto error2;
  86699. + }
  86700. +
  86701. + DWC_WARN("FIQ at 0x%08x", (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
  86702. + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
  86703. +
  86704. + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
  86705. + memset(&regs,0,sizeof(regs));
  86706. +
  86707. + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
  86708. + if (fiq_fsm_enable) {
  86709. + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
  86710. + //regs.ARM_r10 = dwc_otg_hcd->dma;
  86711. + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
  86712. + } else {
  86713. + regs.ARM_fp = (long) dwc_otg_fiq_nop;
  86714. + }
  86715. +
  86716. + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
  86717. +
  86718. +// __show_regs(&regs);
  86719. + set_fiq_regs(&regs);
  86720. +
  86721. + //Set the mphi periph to the required registers
  86722. + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
  86723. + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  86724. + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  86725. + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  86726. + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  86727. + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
  86728. + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
  86729. + //Enable mphi peripheral
  86730. + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
  86731. +#ifdef DEBUG
  86732. + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
  86733. + DWC_WARN("MPHI periph has been enabled");
  86734. + else
  86735. + DWC_WARN("MPHI periph has NOT been enabled");
  86736. +#endif
  86737. + // Enable FIQ interrupt from USB peripheral
  86738. + enable_fiq(INTERRUPT_VC_USB);
  86739. + local_fiq_enable();
  86740. + }
  86741. +
  86742. +
  86743. + otg_dev->hcd->otg_dev = otg_dev;
  86744. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  86745. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  86746. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  86747. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  86748. +#endif
  86749. + /* Don't support SG list at this point */
  86750. + hcd->self.sg_tablesize = 0;
  86751. +#endif
  86752. + /*
  86753. + * Finish generic HCD initialization and start the HCD. This function
  86754. + * allocates the DMA buffer pool, registers the USB bus, requests the
  86755. + * IRQ line, and calls hcd_start method.
  86756. + */
  86757. +#ifdef PLATFORM_INTERFACE
  86758. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED | IRQF_DISABLED);
  86759. +#else
  86760. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED | IRQF_DISABLED);
  86761. +#endif
  86762. + if (retval < 0) {
  86763. + goto error2;
  86764. + }
  86765. +
  86766. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  86767. + return 0;
  86768. +
  86769. +error2:
  86770. + usb_put_hcd(hcd);
  86771. +error1:
  86772. + return retval;
  86773. +}
  86774. +
  86775. +/**
  86776. + * Removes the HCD.
  86777. + * Frees memory and resources associated with the HCD and deregisters the bus.
  86778. + */
  86779. +void hcd_remove(dwc_bus_dev_t *_dev)
  86780. +{
  86781. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  86782. + dwc_otg_hcd_t *dwc_otg_hcd;
  86783. + struct usb_hcd *hcd;
  86784. +
  86785. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  86786. +
  86787. + if (!otg_dev) {
  86788. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  86789. + return;
  86790. + }
  86791. +
  86792. + dwc_otg_hcd = otg_dev->hcd;
  86793. +
  86794. + if (!dwc_otg_hcd) {
  86795. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  86796. + return;
  86797. + }
  86798. +
  86799. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  86800. +
  86801. + if (!hcd) {
  86802. + DWC_DEBUGPL(DBG_ANY,
  86803. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  86804. + __func__);
  86805. + return;
  86806. + }
  86807. + usb_remove_hcd(hcd);
  86808. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  86809. + dwc_otg_hcd_remove(dwc_otg_hcd);
  86810. + usb_put_hcd(hcd);
  86811. +}
  86812. +
  86813. +/* =========================================================================
  86814. + * Linux HC Driver Functions
  86815. + * ========================================================================= */
  86816. +
  86817. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  86818. + * mode operation. Activates the root port. Returns 0 on success and a negative
  86819. + * error code on failure. */
  86820. +int hcd_start(struct usb_hcd *hcd)
  86821. +{
  86822. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86823. + struct usb_bus *bus;
  86824. +
  86825. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  86826. + bus = hcd_to_bus(hcd);
  86827. +
  86828. + hcd->state = HC_STATE_RUNNING;
  86829. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  86830. + return 0;
  86831. + }
  86832. +
  86833. + /* Initialize and connect root hub if one is not already attached */
  86834. + if (bus->root_hub) {
  86835. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  86836. + /* Inform the HUB driver to resume. */
  86837. + usb_hcd_resume_root_hub(hcd);
  86838. + }
  86839. +
  86840. + return 0;
  86841. +}
  86842. +
  86843. +/**
  86844. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  86845. + * stopped.
  86846. + */
  86847. +void hcd_stop(struct usb_hcd *hcd)
  86848. +{
  86849. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86850. +
  86851. + dwc_otg_hcd_stop(dwc_otg_hcd);
  86852. +}
  86853. +
  86854. +/** Returns the current frame number. */
  86855. +static int get_frame_number(struct usb_hcd *hcd)
  86856. +{
  86857. + hprt0_data_t hprt0;
  86858. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86859. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  86860. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  86861. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
  86862. + else
  86863. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  86864. +}
  86865. +
  86866. +#ifdef DEBUG
  86867. +static void dump_urb_info(struct urb *urb, char *fn_name)
  86868. +{
  86869. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  86870. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  86871. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  86872. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  86873. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  86874. + char *pipetype;
  86875. + switch (usb_pipetype(urb->pipe)) {
  86876. +case PIPE_CONTROL:
  86877. +pipetype = "CONTROL"; break; case PIPE_BULK:
  86878. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  86879. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  86880. +pipetype = "ISOCHRONOUS"; break; default:
  86881. + pipetype = "UNKNOWN"; break;};
  86882. + pipetype;}
  86883. + )) ;
  86884. + DWC_PRINTF(" Speed: %s\n", ( {
  86885. + char *speed; switch (urb->dev->speed) {
  86886. +case USB_SPEED_HIGH:
  86887. +speed = "HIGH"; break; case USB_SPEED_FULL:
  86888. +speed = "FULL"; break; case USB_SPEED_LOW:
  86889. +speed = "LOW"; break; default:
  86890. + speed = "UNKNOWN"; break;};
  86891. + speed;}
  86892. + )) ;
  86893. + DWC_PRINTF(" Max packet size: %d\n",
  86894. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  86895. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  86896. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  86897. + urb->transfer_buffer, (void *)urb->transfer_dma);
  86898. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  86899. + urb->setup_packet, (void *)urb->setup_dma);
  86900. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  86901. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  86902. + int i;
  86903. + for (i = 0; i < urb->number_of_packets; i++) {
  86904. + DWC_PRINTF(" ISO Desc %d:\n", i);
  86905. + DWC_PRINTF(" offset: %d, length %d\n",
  86906. + urb->iso_frame_desc[i].offset,
  86907. + urb->iso_frame_desc[i].length);
  86908. + }
  86909. + }
  86910. +}
  86911. +#endif
  86912. +
  86913. +/** Starts processing a USB transfer request specified by a USB Request Block
  86914. + * (URB). mem_flags indicates the type of memory allocation to use while
  86915. + * processing this URB. */
  86916. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  86917. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  86918. + struct usb_host_endpoint *ep,
  86919. +#endif
  86920. + struct urb *urb, gfp_t mem_flags)
  86921. +{
  86922. + int retval = 0;
  86923. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  86924. + struct usb_host_endpoint *ep = urb->ep;
  86925. +#endif
  86926. + dwc_irqflags_t irqflags;
  86927. + void **ref_ep_hcpriv = &ep->hcpriv;
  86928. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  86929. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  86930. + int i;
  86931. + int alloc_bandwidth = 0;
  86932. + uint8_t ep_type = 0;
  86933. + uint32_t flags = 0;
  86934. + void *buf;
  86935. +
  86936. +#ifdef DEBUG
  86937. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  86938. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  86939. + }
  86940. +#endif
  86941. +
  86942. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  86943. + return -EINVAL;
  86944. +
  86945. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
  86946. + || (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  86947. + if (!dwc_otg_hcd_is_bandwidth_allocated
  86948. + (dwc_otg_hcd, ref_ep_hcpriv)) {
  86949. + alloc_bandwidth = 1;
  86950. + }
  86951. + }
  86952. +
  86953. + switch (usb_pipetype(urb->pipe)) {
  86954. + case PIPE_CONTROL:
  86955. + ep_type = USB_ENDPOINT_XFER_CONTROL;
  86956. + break;
  86957. + case PIPE_ISOCHRONOUS:
  86958. + ep_type = USB_ENDPOINT_XFER_ISOC;
  86959. + break;
  86960. + case PIPE_BULK:
  86961. + ep_type = USB_ENDPOINT_XFER_BULK;
  86962. + break;
  86963. + case PIPE_INTERRUPT:
  86964. + ep_type = USB_ENDPOINT_XFER_INT;
  86965. + break;
  86966. + default:
  86967. + DWC_WARN("Wrong EP type - %d\n", usb_pipetype(urb->pipe));
  86968. + }
  86969. +
  86970. + /* # of packets is often 0 - do we really need to call this then? */
  86971. + dwc_otg_urb = dwc_otg_hcd_urb_alloc(dwc_otg_hcd,
  86972. + urb->number_of_packets,
  86973. + mem_flags == GFP_ATOMIC ? 1 : 0);
  86974. +
  86975. + if(dwc_otg_urb == NULL)
  86976. + return -ENOMEM;
  86977. +
  86978. + if (!dwc_otg_urb && urb->number_of_packets)
  86979. + return -ENOMEM;
  86980. +
  86981. + dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_urb, usb_pipedevice(urb->pipe),
  86982. + usb_pipeendpoint(urb->pipe), ep_type,
  86983. + usb_pipein(urb->pipe),
  86984. + usb_maxpacket(urb->dev, urb->pipe,
  86985. + !(usb_pipein(urb->pipe))));
  86986. +
  86987. + buf = urb->transfer_buffer;
  86988. + if (hcd->self.uses_dma) {
  86989. + /*
  86990. + * Calculate virtual address from physical address,
  86991. + * because some class driver may not fill transfer_buffer.
  86992. + * In Buffer DMA mode virual address is used,
  86993. + * when handling non DWORD aligned buffers.
  86994. + */
  86995. + //buf = phys_to_virt(urb->transfer_dma);
  86996. + // DMA addresses are bus addresses not physical addresses!
  86997. + buf = dma_to_virt(&urb->dev->dev, urb->transfer_dma);
  86998. + }
  86999. +
  87000. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  87001. + flags |= URB_GIVEBACK_ASAP;
  87002. + if (urb->transfer_flags & URB_ZERO_PACKET)
  87003. + flags |= URB_SEND_ZERO_PACKET;
  87004. +
  87005. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  87006. + urb->transfer_dma,
  87007. + urb->transfer_buffer_length,
  87008. + urb->setup_packet,
  87009. + urb->setup_dma, flags, urb->interval);
  87010. +
  87011. + for (i = 0; i < urb->number_of_packets; ++i) {
  87012. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  87013. + urb->
  87014. + iso_frame_desc[i].offset,
  87015. + urb->
  87016. + iso_frame_desc[i].length);
  87017. + }
  87018. +
  87019. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  87020. + urb->hcpriv = dwc_otg_urb;
  87021. +#if USB_URB_EP_LINKING
  87022. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  87023. + if (0 == retval)
  87024. +#endif
  87025. + {
  87026. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  87027. + /*(dwc_otg_qh_t **)*/
  87028. + ref_ep_hcpriv, 1);
  87029. + if (0 == retval) {
  87030. + if (alloc_bandwidth) {
  87031. + allocate_bus_bandwidth(hcd,
  87032. + dwc_otg_hcd_get_ep_bandwidth(
  87033. + dwc_otg_hcd, *ref_ep_hcpriv),
  87034. + urb);
  87035. + }
  87036. + } else {
  87037. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  87038. +#if USB_URB_EP_LINKING
  87039. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  87040. +#endif
  87041. + DWC_FREE(dwc_otg_urb);
  87042. + urb->hcpriv = NULL;
  87043. + if (retval == -DWC_E_NO_DEVICE)
  87044. + retval = -ENODEV;
  87045. + }
  87046. + }
  87047. +#if USB_URB_EP_LINKING
  87048. + else
  87049. + {
  87050. + DWC_FREE(dwc_otg_urb);
  87051. + urb->hcpriv = NULL;
  87052. + }
  87053. +#endif
  87054. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  87055. + return retval;
  87056. +}
  87057. +
  87058. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  87059. + * success. */
  87060. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87061. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  87062. +#else
  87063. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  87064. +#endif
  87065. +{
  87066. + dwc_irqflags_t flags;
  87067. + dwc_otg_hcd_t *dwc_otg_hcd;
  87068. + int rc;
  87069. +
  87070. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  87071. +
  87072. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  87073. +
  87074. +#ifdef DEBUG
  87075. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  87076. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  87077. + }
  87078. +#endif
  87079. +
  87080. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  87081. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  87082. + if (0 == rc) {
  87083. + if(urb->hcpriv != NULL) {
  87084. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  87085. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  87086. +
  87087. + DWC_FREE(urb->hcpriv);
  87088. + urb->hcpriv = NULL;
  87089. + }
  87090. + }
  87091. +
  87092. + if (0 == rc) {
  87093. + /* Higher layer software sets URB status. */
  87094. +#if USB_URB_EP_LINKING
  87095. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  87096. +#endif
  87097. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  87098. +
  87099. +
  87100. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  87101. + usb_hcd_giveback_urb(hcd, urb);
  87102. +#else
  87103. + usb_hcd_giveback_urb(hcd, urb, status);
  87104. +#endif
  87105. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  87106. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  87107. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  87108. + }
  87109. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  87110. + } else {
  87111. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  87112. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  87113. + rc);
  87114. + }
  87115. +
  87116. + return rc;
  87117. +}
  87118. +
  87119. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  87120. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  87121. + * must already be dequeued. */
  87122. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  87123. +{
  87124. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  87125. +
  87126. + DWC_DEBUGPL(DBG_HCD,
  87127. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  87128. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  87129. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  87130. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  87131. + ep->hcpriv = NULL;
  87132. +}
  87133. +
  87134. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  87135. +/* Resets endpoint specific parameter values, in current version used to reset
  87136. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  87137. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  87138. +{
  87139. + dwc_irqflags_t flags;
  87140. + struct usb_device *udev = NULL;
  87141. + int epnum = usb_endpoint_num(&ep->desc);
  87142. + int is_out = usb_endpoint_dir_out(&ep->desc);
  87143. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  87144. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  87145. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  87146. +
  87147. + if (dev)
  87148. + udev = to_usb_device(dev);
  87149. + else
  87150. + return;
  87151. +
  87152. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  87153. +
  87154. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  87155. + usb_settoggle(udev, epnum, is_out, 0);
  87156. + if (is_control)
  87157. + usb_settoggle(udev, epnum, !is_out, 0);
  87158. +
  87159. + if (ep->hcpriv) {
  87160. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  87161. + }
  87162. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  87163. +}
  87164. +#endif
  87165. +
  87166. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  87167. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  87168. + * interrupt.
  87169. + *
  87170. + * This function is called by the USB core when an interrupt occurs */
  87171. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  87172. +{
  87173. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  87174. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  87175. + if (retval != 0) {
  87176. + S3C2410X_CLEAR_EINTPEND();
  87177. + }
  87178. + return IRQ_RETVAL(retval);
  87179. +}
  87180. +
  87181. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  87182. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  87183. + * is the status change indicator for the single root port. Returns 1 if either
  87184. + * change indicator is 1, otherwise returns 0. */
  87185. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  87186. +{
  87187. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  87188. +
  87189. + buf[0] = 0;
  87190. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  87191. +
  87192. + return (buf[0] != 0);
  87193. +}
  87194. +
  87195. +/** Handles hub class-specific requests. */
  87196. +int hub_control(struct usb_hcd *hcd,
  87197. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  87198. +{
  87199. + int retval;
  87200. +
  87201. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  87202. + typeReq, wValue, wIndex, buf, wLength);
  87203. +
  87204. + switch (retval) {
  87205. + case -DWC_E_INVALID:
  87206. + retval = -EINVAL;
  87207. + break;
  87208. + }
  87209. +
  87210. + return retval;
  87211. +}
  87212. +
  87213. +#endif /* DWC_DEVICE_ONLY */
  87214. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  87215. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 1970-01-01 01:00:00.000000000 +0100
  87216. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c 2014-09-14 19:04:13.000000000 +0200
  87217. @@ -0,0 +1,943 @@
  87218. +/* ==========================================================================
  87219. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  87220. + * $Revision: #44 $
  87221. + * $Date: 2011/10/26 $
  87222. + * $Change: 1873028 $
  87223. + *
  87224. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  87225. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  87226. + * otherwise expressly agreed to in writing between Synopsys and you.
  87227. + *
  87228. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  87229. + * any End User Software License Agreement or Agreement for Licensed Product
  87230. + * with Synopsys or any supplement thereto. You are permitted to use and
  87231. + * redistribute this Software in source and binary forms, with or without
  87232. + * modification, provided that redistributions of source code must retain this
  87233. + * notice. You may not view, use, disclose, copy or distribute this file or
  87234. + * any information contained herein except pursuant to this license grant from
  87235. + * Synopsys. If you do not agree with this notice, including the disclaimer
  87236. + * below, then you are not authorized to use the Software.
  87237. + *
  87238. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  87239. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  87240. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  87241. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  87242. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  87243. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  87244. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87245. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  87246. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  87247. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  87248. + * DAMAGE.
  87249. + * ========================================================================== */
  87250. +#ifndef DWC_DEVICE_ONLY
  87251. +
  87252. +/**
  87253. + * @file
  87254. + *
  87255. + * This file contains the functions to manage Queue Heads and Queue
  87256. + * Transfer Descriptors.
  87257. + */
  87258. +
  87259. +#include "dwc_otg_hcd.h"
  87260. +#include "dwc_otg_regs.h"
  87261. +
  87262. +extern bool microframe_schedule;
  87263. +
  87264. +/**
  87265. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  87266. + * removed from a list. QTD list should already be empty if called from URB
  87267. + * Dequeue.
  87268. + *
  87269. + * @param hcd HCD instance.
  87270. + * @param qh The QH to free.
  87271. + */
  87272. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87273. +{
  87274. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  87275. + dwc_irqflags_t flags;
  87276. +
  87277. + /* Free each QTD in the QTD list */
  87278. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  87279. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  87280. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  87281. + dwc_otg_hcd_qtd_free(qtd);
  87282. + }
  87283. +
  87284. + if (hcd->core_if->dma_desc_enable) {
  87285. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  87286. + } else if (qh->dw_align_buf) {
  87287. + uint32_t buf_size;
  87288. + if (qh->ep_type == UE_ISOCHRONOUS) {
  87289. + buf_size = 4096;
  87290. + } else {
  87291. + buf_size = hcd->core_if->core_params->max_transfer_size;
  87292. + }
  87293. + DWC_DMA_FREE(buf_size, qh->dw_align_buf, qh->dw_align_buf_dma);
  87294. + }
  87295. +
  87296. + DWC_FREE(qh);
  87297. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  87298. + return;
  87299. +}
  87300. +
  87301. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  87302. +#define HS_HOST_DELAY 5 /* nanoseconds */
  87303. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  87304. +#define HUB_LS_SETUP 333 /* nanoseconds */
  87305. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  87306. + /* convert & round nanoseconds to microseconds */
  87307. +
  87308. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  87309. +{
  87310. + unsigned long retval;
  87311. +
  87312. + switch (speed) {
  87313. + case USB_SPEED_HIGH:
  87314. + if (is_isoc) {
  87315. + retval =
  87316. + ((38 * 8 * 2083) +
  87317. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  87318. + HS_HOST_DELAY;
  87319. + } else {
  87320. + retval =
  87321. + ((55 * 8 * 2083) +
  87322. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  87323. + HS_HOST_DELAY;
  87324. + }
  87325. + break;
  87326. + case USB_SPEED_FULL:
  87327. + if (is_isoc) {
  87328. + retval =
  87329. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  87330. + if (is_in) {
  87331. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  87332. + } else {
  87333. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  87334. + }
  87335. + } else {
  87336. + retval =
  87337. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  87338. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  87339. + }
  87340. + break;
  87341. + case USB_SPEED_LOW:
  87342. + if (is_in) {
  87343. + retval =
  87344. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  87345. + 1000;
  87346. + retval =
  87347. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  87348. + retval;
  87349. + } else {
  87350. + retval =
  87351. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  87352. + 1000;
  87353. + retval =
  87354. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  87355. + retval;
  87356. + }
  87357. + break;
  87358. + default:
  87359. + DWC_WARN("Unknown device speed\n");
  87360. + retval = -1;
  87361. + }
  87362. +
  87363. + return NS_TO_US(retval);
  87364. +}
  87365. +
  87366. +/**
  87367. + * Initializes a QH structure.
  87368. + *
  87369. + * @param hcd The HCD state structure for the DWC OTG controller.
  87370. + * @param qh The QH to init.
  87371. + * @param urb Holds the information about the device/endpoint that we need
  87372. + * to initialize the QH.
  87373. + */
  87374. +#define SCHEDULE_SLOP 10
  87375. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  87376. +{
  87377. + char *speed, *type;
  87378. + int dev_speed;
  87379. + uint32_t hub_addr, hub_port;
  87380. +
  87381. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  87382. +
  87383. + /* Initialize QH */
  87384. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  87385. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  87386. +
  87387. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  87388. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  87389. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  87390. + DWC_LIST_INIT(&qh->qh_list_entry);
  87391. + qh->channel = NULL;
  87392. +
  87393. + /* FS/LS Enpoint on HS Hub
  87394. + * NOT virtual root hub */
  87395. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  87396. +
  87397. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  87398. + qh->do_split = 0;
  87399. + if (microframe_schedule)
  87400. + qh->speed = dev_speed;
  87401. +
  87402. + qh->nak_frame = 0xffff;
  87403. +
  87404. + if (((dev_speed == USB_SPEED_LOW) ||
  87405. + (dev_speed == USB_SPEED_FULL)) &&
  87406. + (hub_addr != 0 && hub_addr != 1)) {
  87407. + DWC_DEBUGPL(DBG_HCD,
  87408. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  87409. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  87410. + hub_port);
  87411. + qh->do_split = 1;
  87412. + qh->skip_count = 0;
  87413. + }
  87414. +
  87415. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  87416. + /* Compute scheduling parameters once and save them. */
  87417. + hprt0_data_t hprt;
  87418. +
  87419. + /** @todo Account for split transfers in the bus time. */
  87420. + int bytecount =
  87421. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  87422. +
  87423. + qh->usecs =
  87424. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  87425. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  87426. + bytecount);
  87427. + /* Start in a slightly future (micro)frame. */
  87428. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  87429. + SCHEDULE_SLOP);
  87430. + qh->interval = urb->interval;
  87431. +
  87432. +#if 0
  87433. + /* Increase interrupt polling rate for debugging. */
  87434. + if (qh->ep_type == UE_INTERRUPT) {
  87435. + qh->interval = 8;
  87436. + }
  87437. +#endif
  87438. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  87439. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  87440. + ((dev_speed == USB_SPEED_LOW) ||
  87441. + (dev_speed == USB_SPEED_FULL))) {
  87442. + qh->interval *= 8;
  87443. + qh->sched_frame |= 0x7;
  87444. + qh->start_split_frame = qh->sched_frame;
  87445. + }
  87446. +
  87447. + }
  87448. +
  87449. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  87450. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  87451. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  87452. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  87453. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  87454. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  87455. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  87456. + switch (dev_speed) {
  87457. + case USB_SPEED_LOW:
  87458. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  87459. + speed = "low";
  87460. + break;
  87461. + case USB_SPEED_FULL:
  87462. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  87463. + speed = "full";
  87464. + break;
  87465. + case USB_SPEED_HIGH:
  87466. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  87467. + speed = "high";
  87468. + break;
  87469. + default:
  87470. + speed = "?";
  87471. + break;
  87472. + }
  87473. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  87474. +
  87475. + switch (qh->ep_type) {
  87476. + case UE_ISOCHRONOUS:
  87477. + type = "isochronous";
  87478. + break;
  87479. + case UE_INTERRUPT:
  87480. + type = "interrupt";
  87481. + break;
  87482. + case UE_CONTROL:
  87483. + type = "control";
  87484. + break;
  87485. + case UE_BULK:
  87486. + type = "bulk";
  87487. + break;
  87488. + default:
  87489. + type = "?";
  87490. + break;
  87491. + }
  87492. +
  87493. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  87494. +
  87495. +#ifdef DEBUG
  87496. + if (qh->ep_type == UE_INTERRUPT) {
  87497. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  87498. + qh->usecs);
  87499. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  87500. + qh->interval);
  87501. + }
  87502. +#endif
  87503. +
  87504. +}
  87505. +
  87506. +/**
  87507. + * This function allocates and initializes a QH.
  87508. + *
  87509. + * @param hcd The HCD state structure for the DWC OTG controller.
  87510. + * @param urb Holds the information about the device/endpoint that we need
  87511. + * to initialize the QH.
  87512. + * @param atomic_alloc Flag to do atomic allocation if needed
  87513. + *
  87514. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  87515. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  87516. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  87517. +{
  87518. + dwc_otg_qh_t *qh;
  87519. +
  87520. + /* Allocate memory */
  87521. + /** @todo add memflags argument */
  87522. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  87523. + if (qh == NULL) {
  87524. + DWC_ERROR("qh allocation failed");
  87525. + return NULL;
  87526. + }
  87527. +
  87528. + qh_init(hcd, qh, urb);
  87529. +
  87530. + if (hcd->core_if->dma_desc_enable
  87531. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  87532. + dwc_otg_hcd_qh_free(hcd, qh);
  87533. + return NULL;
  87534. + }
  87535. +
  87536. + return qh;
  87537. +}
  87538. +
  87539. +/* microframe_schedule=0 start */
  87540. +
  87541. +/**
  87542. + * Checks that a channel is available for a periodic transfer.
  87543. + *
  87544. + * @return 0 if successful, negative error code otherise.
  87545. + */
  87546. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  87547. +{
  87548. + /*
  87549. + * Currently assuming that there is a dedicated host channnel for each
  87550. + * periodic transaction plus at least one host channel for
  87551. + * non-periodic transactions.
  87552. + */
  87553. + int status;
  87554. + int num_channels;
  87555. +
  87556. + num_channels = hcd->core_if->core_params->host_channels;
  87557. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  87558. + && (hcd->periodic_channels < num_channels - 1)) {
  87559. + status = 0;
  87560. + } else {
  87561. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  87562. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  87563. + status = -DWC_E_NO_SPACE;
  87564. + }
  87565. +
  87566. + return status;
  87567. +}
  87568. +
  87569. +/**
  87570. + * Checks that there is sufficient bandwidth for the specified QH in the
  87571. + * periodic schedule. For simplicity, this calculation assumes that all the
  87572. + * transfers in the periodic schedule may occur in the same (micro)frame.
  87573. + *
  87574. + * @param hcd The HCD state structure for the DWC OTG controller.
  87575. + * @param qh QH containing periodic bandwidth required.
  87576. + *
  87577. + * @return 0 if successful, negative error code otherwise.
  87578. + */
  87579. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87580. +{
  87581. + int status;
  87582. + int16_t max_claimed_usecs;
  87583. +
  87584. + status = 0;
  87585. +
  87586. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  87587. + /*
  87588. + * High speed mode.
  87589. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  87590. + */
  87591. +
  87592. + max_claimed_usecs = 100 - qh->usecs;
  87593. + } else {
  87594. + /*
  87595. + * Full speed mode.
  87596. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  87597. + */
  87598. + max_claimed_usecs = 900 - qh->usecs;
  87599. + }
  87600. +
  87601. + if (hcd->periodic_usecs > max_claimed_usecs) {
  87602. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  87603. + status = -DWC_E_NO_SPACE;
  87604. + }
  87605. +
  87606. + return status;
  87607. +}
  87608. +
  87609. +/* microframe_schedule=0 end */
  87610. +
  87611. +/**
  87612. + * Microframe scheduler
  87613. + * track the total use in hcd->frame_usecs
  87614. + * keep each qh use in qh->frame_usecs
  87615. + * when surrendering the qh then donate the time back
  87616. + */
  87617. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  87618. +
  87619. +/*
  87620. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  87621. + */
  87622. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  87623. +{
  87624. + int i;
  87625. + for (i=0; i<8; i++) {
  87626. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  87627. + }
  87628. + return 0;
  87629. +}
  87630. +
  87631. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  87632. +{
  87633. + int i;
  87634. + unsigned short utime;
  87635. + int t_left;
  87636. + int ret;
  87637. + int done;
  87638. +
  87639. + ret = -1;
  87640. + utime = _qh->usecs;
  87641. + t_left = utime;
  87642. + i = 0;
  87643. + done = 0;
  87644. + while (done == 0) {
  87645. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  87646. + if (utime <= _hcd->frame_usecs[i]) {
  87647. + _hcd->frame_usecs[i] -= utime;
  87648. + _qh->frame_usecs[i] += utime;
  87649. + t_left -= utime;
  87650. + ret = i;
  87651. + done = 1;
  87652. + return ret;
  87653. + } else {
  87654. + i++;
  87655. + if (i == 8) {
  87656. + done = 1;
  87657. + ret = -1;
  87658. + }
  87659. + }
  87660. + }
  87661. + return ret;
  87662. + }
  87663. +
  87664. +/*
  87665. + * use this for FS apps that can span multiple uframes
  87666. + */
  87667. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  87668. +{
  87669. + int i;
  87670. + int j;
  87671. + unsigned short utime;
  87672. + int t_left;
  87673. + int ret;
  87674. + int done;
  87675. + unsigned short xtime;
  87676. +
  87677. + ret = -1;
  87678. + utime = _qh->usecs;
  87679. + t_left = utime;
  87680. + i = 0;
  87681. + done = 0;
  87682. +loop:
  87683. + while (done == 0) {
  87684. + if(_hcd->frame_usecs[i] <= 0) {
  87685. + i++;
  87686. + if (i == 8) {
  87687. + done = 1;
  87688. + ret = -1;
  87689. + }
  87690. + goto loop;
  87691. + }
  87692. +
  87693. + /*
  87694. + * we need n consecutive slots
  87695. + * so use j as a start slot j plus j+1 must be enough time (for now)
  87696. + */
  87697. + xtime= _hcd->frame_usecs[i];
  87698. + for (j = i+1 ; j < 8 ; j++ ) {
  87699. + /*
  87700. + * if we add this frame remaining time to xtime we may
  87701. + * be OK, if not we need to test j for a complete frame
  87702. + */
  87703. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  87704. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  87705. + j = 8;
  87706. + ret = -1;
  87707. + continue;
  87708. + }
  87709. + }
  87710. + if (xtime >= utime) {
  87711. + ret = i;
  87712. + j = 8; /* stop loop with a good value ret */
  87713. + continue;
  87714. + }
  87715. + /* add the frame time to x time */
  87716. + xtime += _hcd->frame_usecs[j];
  87717. + /* we must have a fully available next frame or break */
  87718. + if ((xtime < utime)
  87719. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  87720. + ret = -1;
  87721. + j = 8; /* stop loop with a bad value ret */
  87722. + continue;
  87723. + }
  87724. + }
  87725. + if (ret >= 0) {
  87726. + t_left = utime;
  87727. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  87728. + t_left -= _hcd->frame_usecs[j];
  87729. + if ( t_left <= 0 ) {
  87730. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  87731. + _hcd->frame_usecs[j]= -t_left;
  87732. + ret = i;
  87733. + done = 1;
  87734. + } else {
  87735. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  87736. + _hcd->frame_usecs[j] = 0;
  87737. + }
  87738. + }
  87739. + } else {
  87740. + i++;
  87741. + if (i == 8) {
  87742. + done = 1;
  87743. + ret = -1;
  87744. + }
  87745. + }
  87746. + }
  87747. + return ret;
  87748. +}
  87749. +
  87750. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  87751. +{
  87752. + int ret;
  87753. + ret = -1;
  87754. +
  87755. + if (_qh->speed == USB_SPEED_HIGH) {
  87756. + /* if this is a hs transaction we need a full frame */
  87757. + ret = find_single_uframe(_hcd, _qh);
  87758. + } else {
  87759. + /* if this is a fs transaction we may need a sequence of frames */
  87760. + ret = find_multi_uframe(_hcd, _qh);
  87761. + }
  87762. + return ret;
  87763. +}
  87764. +
  87765. +/**
  87766. + * Checks that the max transfer size allowed in a host channel is large enough
  87767. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  87768. + * transfer.
  87769. + *
  87770. + * @param hcd The HCD state structure for the DWC OTG controller.
  87771. + * @param qh QH for a periodic endpoint.
  87772. + *
  87773. + * @return 0 if successful, negative error code otherwise.
  87774. + */
  87775. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87776. +{
  87777. + int status;
  87778. + uint32_t max_xfer_size;
  87779. + uint32_t max_channel_xfer_size;
  87780. +
  87781. + status = 0;
  87782. +
  87783. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  87784. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  87785. +
  87786. + if (max_xfer_size > max_channel_xfer_size) {
  87787. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  87788. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  87789. + status = -DWC_E_NO_SPACE;
  87790. + }
  87791. +
  87792. + return status;
  87793. +}
  87794. +
  87795. +
  87796. +
  87797. +/**
  87798. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  87799. + *
  87800. + * @param hcd The HCD state structure for the DWC OTG controller.
  87801. + * @param qh QH for the periodic transfer. The QH should already contain the
  87802. + * scheduling information.
  87803. + *
  87804. + * @return 0 if successful, negative error code otherwise.
  87805. + */
  87806. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87807. +{
  87808. + int status = 0;
  87809. +
  87810. + if (microframe_schedule) {
  87811. + int frame;
  87812. + status = find_uframe(hcd, qh);
  87813. + frame = -1;
  87814. + if (status == 0) {
  87815. + frame = 7;
  87816. + } else {
  87817. + if (status > 0 )
  87818. + frame = status-1;
  87819. + }
  87820. +
  87821. + /* Set the new frame up */
  87822. + if (frame > -1) {
  87823. + qh->sched_frame &= ~0x7;
  87824. + qh->sched_frame |= (frame & 7);
  87825. + }
  87826. +
  87827. + if (status != -1)
  87828. + status = 0;
  87829. + } else {
  87830. + status = periodic_channel_available(hcd);
  87831. + if (status) {
  87832. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  87833. + return status;
  87834. + }
  87835. +
  87836. + status = check_periodic_bandwidth(hcd, qh);
  87837. + }
  87838. + if (status) {
  87839. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  87840. + "periodic transfer.\n", __func__);
  87841. + return status;
  87842. + }
  87843. + status = check_max_xfer_size(hcd, qh);
  87844. + if (status) {
  87845. + DWC_INFO("%s: Channel max transfer size too small "
  87846. + "for periodic transfer.\n", __func__);
  87847. + return status;
  87848. + }
  87849. +
  87850. + if (hcd->core_if->dma_desc_enable) {
  87851. + /* Don't rely on SOF and start in ready schedule */
  87852. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  87853. + }
  87854. + else {
  87855. + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
  87856. + {
  87857. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  87858. +
  87859. + }
  87860. + /* Always start in the inactive schedule. */
  87861. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  87862. + }
  87863. +
  87864. + if (!microframe_schedule) {
  87865. + /* Reserve the periodic channel. */
  87866. + hcd->periodic_channels++;
  87867. + }
  87868. +
  87869. + /* Update claimed usecs per (micro)frame. */
  87870. + hcd->periodic_usecs += qh->usecs;
  87871. +
  87872. + return status;
  87873. +}
  87874. +
  87875. +
  87876. +/**
  87877. + * This function adds a QH to either the non periodic or periodic schedule if
  87878. + * it is not already in the schedule. If the QH is already in the schedule, no
  87879. + * action is taken.
  87880. + *
  87881. + * @return 0 if successful, negative error code otherwise.
  87882. + */
  87883. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87884. +{
  87885. + int status = 0;
  87886. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87887. +
  87888. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  87889. + /* QH already in a schedule. */
  87890. + return status;
  87891. + }
  87892. +
  87893. + /* Add the new QH to the appropriate schedule */
  87894. + if (dwc_qh_is_non_per(qh)) {
  87895. + /* Always start in the inactive schedule. */
  87896. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  87897. + &qh->qh_list_entry);
  87898. + //hcd->fiq_state->kick_np_queues = 1;
  87899. + } else {
  87900. + status = schedule_periodic(hcd, qh);
  87901. + if ( !hcd->periodic_qh_count ) {
  87902. + intr_mask.b.sofintr = 1;
  87903. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  87904. + intr_mask.d32, intr_mask.d32);
  87905. + }
  87906. + hcd->periodic_qh_count++;
  87907. + }
  87908. +
  87909. + return status;
  87910. +}
  87911. +
  87912. +/**
  87913. + * Removes an interrupt or isochronous transfer from the periodic schedule.
  87914. + *
  87915. + * @param hcd The HCD state structure for the DWC OTG controller.
  87916. + * @param qh QH for the periodic transfer.
  87917. + */
  87918. +static void deschedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87919. +{
  87920. + int i;
  87921. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  87922. +
  87923. + /* Update claimed usecs per (micro)frame. */
  87924. + hcd->periodic_usecs -= qh->usecs;
  87925. +
  87926. + if (!microframe_schedule) {
  87927. + /* Release the periodic channel reservation. */
  87928. + hcd->periodic_channels--;
  87929. + } else {
  87930. + for (i = 0; i < 8; i++) {
  87931. + hcd->frame_usecs[i] += qh->frame_usecs[i];
  87932. + qh->frame_usecs[i] = 0;
  87933. + }
  87934. + }
  87935. +}
  87936. +
  87937. +/**
  87938. + * Removes a QH from either the non-periodic or periodic schedule. Memory is
  87939. + * not freed.
  87940. + *
  87941. + * @param hcd The HCD state structure.
  87942. + * @param qh QH to remove from schedule. */
  87943. +void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  87944. +{
  87945. + gintmsk_data_t intr_mask = {.d32 = 0 };
  87946. +
  87947. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  87948. + /* QH is not in a schedule. */
  87949. + return;
  87950. + }
  87951. +
  87952. + if (dwc_qh_is_non_per(qh)) {
  87953. + if (hcd->non_periodic_qh_ptr == &qh->qh_list_entry) {
  87954. + hcd->non_periodic_qh_ptr =
  87955. + hcd->non_periodic_qh_ptr->next;
  87956. + }
  87957. + DWC_LIST_REMOVE_INIT(&qh->qh_list_entry);
  87958. + //if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive))
  87959. + // hcd->fiq_state->kick_np_queues = 1;
  87960. + } else {
  87961. + deschedule_periodic(hcd, qh);
  87962. + hcd->periodic_qh_count--;
  87963. + if( !hcd->periodic_qh_count && !fiq_fsm_enable ) {
  87964. + intr_mask.b.sofintr = 1;
  87965. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk,
  87966. + intr_mask.d32, 0);
  87967. + }
  87968. + }
  87969. +}
  87970. +
  87971. +/**
  87972. + * Deactivates a QH. For non-periodic QHs, removes the QH from the active
  87973. + * non-periodic schedule. The QH is added to the inactive non-periodic
  87974. + * schedule if any QTDs are still attached to the QH.
  87975. + *
  87976. + * For periodic QHs, the QH is removed from the periodic queued schedule. If
  87977. + * there are any QTDs still attached to the QH, the QH is added to either the
  87978. + * periodic inactive schedule or the periodic ready schedule and its next
  87979. + * scheduled frame is calculated. The QH is placed in the ready schedule if
  87980. + * the scheduled frame has been reached already. Otherwise it's placed in the
  87981. + * inactive schedule. If there are no QTDs attached to the QH, the QH is
  87982. + * completely removed from the periodic schedule.
  87983. + */
  87984. +void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  87985. + int sched_next_periodic_split)
  87986. +{
  87987. + if (dwc_qh_is_non_per(qh)) {
  87988. + dwc_otg_hcd_qh_remove(hcd, qh);
  87989. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  87990. + /* Add back to inactive non-periodic schedule. */
  87991. + dwc_otg_hcd_qh_add(hcd, qh);
  87992. + //hcd->fiq_state->kick_np_queues = 1;
  87993. + }
  87994. + } else {
  87995. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  87996. +
  87997. + if (qh->do_split) {
  87998. + /* Schedule the next continuing periodic split transfer */
  87999. + if (sched_next_periodic_split) {
  88000. +
  88001. + qh->sched_frame = frame_number;
  88002. +
  88003. + if (dwc_frame_num_le(frame_number,
  88004. + dwc_frame_num_inc
  88005. + (qh->start_split_frame,
  88006. + 1))) {
  88007. + /*
  88008. + * Allow one frame to elapse after start
  88009. + * split microframe before scheduling
  88010. + * complete split, but DONT if we are
  88011. + * doing the next start split in the
  88012. + * same frame for an ISOC out.
  88013. + */
  88014. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  88015. + (qh->ep_is_in != 0)) {
  88016. + qh->sched_frame =
  88017. + dwc_frame_num_inc(qh->sched_frame, 1);
  88018. + }
  88019. + }
  88020. + } else {
  88021. + qh->sched_frame =
  88022. + dwc_frame_num_inc(qh->start_split_frame,
  88023. + qh->interval);
  88024. + if (dwc_frame_num_le
  88025. + (qh->sched_frame, frame_number)) {
  88026. + qh->sched_frame = frame_number;
  88027. + }
  88028. + qh->sched_frame |= 0x7;
  88029. + qh->start_split_frame = qh->sched_frame;
  88030. + }
  88031. + } else {
  88032. + qh->sched_frame =
  88033. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  88034. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  88035. + qh->sched_frame = frame_number;
  88036. + }
  88037. + }
  88038. +
  88039. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  88040. + dwc_otg_hcd_qh_remove(hcd, qh);
  88041. + } else {
  88042. + /*
  88043. + * Remove from periodic_sched_queued and move to
  88044. + * appropriate queue.
  88045. + */
  88046. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  88047. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  88048. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  88049. + &qh->qh_list_entry);
  88050. + } else {
  88051. + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
  88052. + {
  88053. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  88054. + }
  88055. +
  88056. + DWC_LIST_MOVE_HEAD
  88057. + (&hcd->periodic_sched_inactive,
  88058. + &qh->qh_list_entry);
  88059. + }
  88060. + }
  88061. + }
  88062. +}
  88063. +
  88064. +/**
  88065. + * This function allocates and initializes a QTD.
  88066. + *
  88067. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  88068. + * pointing to each other so each pair should have a unique correlation.
  88069. + * @param atomic_alloc Flag to do atomic alloc if needed
  88070. + *
  88071. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  88072. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  88073. +{
  88074. + dwc_otg_qtd_t *qtd;
  88075. +
  88076. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  88077. + if (qtd == NULL) {
  88078. + return NULL;
  88079. + }
  88080. +
  88081. + dwc_otg_hcd_qtd_init(qtd, urb);
  88082. + return qtd;
  88083. +}
  88084. +
  88085. +/**
  88086. + * Initializes a QTD structure.
  88087. + *
  88088. + * @param qtd The QTD to initialize.
  88089. + * @param urb The URB to use for initialization. */
  88090. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  88091. +{
  88092. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  88093. + qtd->urb = urb;
  88094. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  88095. + /*
  88096. + * The only time the QTD data toggle is used is on the data
  88097. + * phase of control transfers. This phase always starts with
  88098. + * DATA1.
  88099. + */
  88100. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  88101. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  88102. + }
  88103. +
  88104. + /* start split */
  88105. + qtd->complete_split = 0;
  88106. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  88107. + qtd->isoc_split_offset = 0;
  88108. + qtd->in_process = 0;
  88109. +
  88110. + /* Store the qtd ptr in the urb to reference what QTD. */
  88111. + urb->qtd = qtd;
  88112. + return;
  88113. +}
  88114. +
  88115. +/**
  88116. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  88117. + * QH to place the QTD into. If it does not find a QH, then it will create a
  88118. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  88119. + * is placed into the proper schedule based on its EP type.
  88120. + * HCD lock must be held and interrupts must be disabled on entry
  88121. + *
  88122. + * @param[in] qtd The QTD to add
  88123. + * @param[in] hcd The DWC HCD structure
  88124. + * @param[out] qh out parameter to return queue head
  88125. + * @param atomic_alloc Flag to do atomic alloc if needed
  88126. + *
  88127. + * @return 0 if successful, negative error code otherwise.
  88128. + */
  88129. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  88130. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  88131. +{
  88132. + int retval = 0;
  88133. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  88134. +
  88135. + /*
  88136. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  88137. + * doesn't exist.
  88138. + */
  88139. + if (*qh == NULL) {
  88140. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  88141. + if (*qh == NULL) {
  88142. + retval = -DWC_E_NO_MEMORY;
  88143. + goto done;
  88144. + } else {
  88145. + if (fiq_enable)
  88146. + hcd->fiq_state->kick_np_queues = 1;
  88147. + }
  88148. + }
  88149. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  88150. + if (retval == 0) {
  88151. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  88152. + qtd_list_entry);
  88153. + qtd->qh = *qh;
  88154. + }
  88155. +done:
  88156. +
  88157. + return retval;
  88158. +}
  88159. +
  88160. +#endif /* DWC_DEVICE_ONLY */
  88161. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  88162. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 1970-01-01 01:00:00.000000000 +0100
  88163. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h 2014-09-14 19:04:13.000000000 +0200
  88164. @@ -0,0 +1,188 @@
  88165. +#ifndef _DWC_OS_DEP_H_
  88166. +#define _DWC_OS_DEP_H_
  88167. +
  88168. +/**
  88169. + * @file
  88170. + *
  88171. + * This file contains OS dependent structures.
  88172. + *
  88173. + */
  88174. +
  88175. +#include <linux/kernel.h>
  88176. +#include <linux/module.h>
  88177. +#include <linux/moduleparam.h>
  88178. +#include <linux/init.h>
  88179. +#include <linux/device.h>
  88180. +#include <linux/errno.h>
  88181. +#include <linux/types.h>
  88182. +#include <linux/slab.h>
  88183. +#include <linux/list.h>
  88184. +#include <linux/interrupt.h>
  88185. +#include <linux/ctype.h>
  88186. +#include <linux/string.h>
  88187. +#include <linux/dma-mapping.h>
  88188. +#include <linux/jiffies.h>
  88189. +#include <linux/delay.h>
  88190. +#include <linux/timer.h>
  88191. +#include <linux/workqueue.h>
  88192. +#include <linux/stat.h>
  88193. +#include <linux/pci.h>
  88194. +
  88195. +#include <linux/version.h>
  88196. +
  88197. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  88198. +# include <linux/irq.h>
  88199. +#endif
  88200. +
  88201. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  88202. +# include <linux/usb/ch9.h>
  88203. +#else
  88204. +# include <linux/usb_ch9.h>
  88205. +#endif
  88206. +
  88207. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  88208. +# include <linux/usb/gadget.h>
  88209. +#else
  88210. +# include <linux/usb_gadget.h>
  88211. +#endif
  88212. +
  88213. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  88214. +# include <asm/irq.h>
  88215. +#endif
  88216. +
  88217. +#ifdef PCI_INTERFACE
  88218. +# include <asm/io.h>
  88219. +#endif
  88220. +
  88221. +#ifdef LM_INTERFACE
  88222. +# include <asm/unaligned.h>
  88223. +# include <asm/sizes.h>
  88224. +# include <asm/param.h>
  88225. +# include <asm/io.h>
  88226. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  88227. +# include <asm/arch/hardware.h>
  88228. +# include <asm/arch/lm.h>
  88229. +# include <asm/arch/irqs.h>
  88230. +# include <asm/arch/regs-irq.h>
  88231. +# else
  88232. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  88233. + here we assume that the machine architecture provides definitions
  88234. + in its own header
  88235. +*/
  88236. +# include <mach/lm.h>
  88237. +# include <mach/hardware.h>
  88238. +# endif
  88239. +#endif
  88240. +
  88241. +#ifdef PLATFORM_INTERFACE
  88242. +#include <linux/platform_device.h>
  88243. +#include <asm/mach/map.h>
  88244. +#endif
  88245. +
  88246. +/** The OS page size */
  88247. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  88248. +
  88249. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  88250. +typedef int gfp_t;
  88251. +#endif
  88252. +
  88253. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  88254. +# define IRQF_SHARED SA_SHIRQ
  88255. +#endif
  88256. +
  88257. +typedef struct os_dependent {
  88258. + /** Base address returned from ioremap() */
  88259. + void *base;
  88260. +
  88261. + /** Register offset for Diagnostic API */
  88262. + uint32_t reg_offset;
  88263. +
  88264. + /** Base address for MPHI peripheral */
  88265. + void *mphi_base;
  88266. +
  88267. +#ifdef LM_INTERFACE
  88268. + struct lm_device *lmdev;
  88269. +#elif defined(PCI_INTERFACE)
  88270. + struct pci_dev *pcidev;
  88271. +
  88272. + /** Start address of a PCI region */
  88273. + resource_size_t rsrc_start;
  88274. +
  88275. + /** Length address of a PCI region */
  88276. + resource_size_t rsrc_len;
  88277. +#elif defined(PLATFORM_INTERFACE)
  88278. + struct platform_device *platformdev;
  88279. +#endif
  88280. +
  88281. +} os_dependent_t;
  88282. +
  88283. +#ifdef __cplusplus
  88284. +}
  88285. +#endif
  88286. +
  88287. +
  88288. +
  88289. +/* Type for the our device on the chosen bus */
  88290. +#if defined(LM_INTERFACE)
  88291. +typedef struct lm_device dwc_bus_dev_t;
  88292. +#elif defined(PCI_INTERFACE)
  88293. +typedef struct pci_dev dwc_bus_dev_t;
  88294. +#elif defined(PLATFORM_INTERFACE)
  88295. +typedef struct platform_device dwc_bus_dev_t;
  88296. +#endif
  88297. +
  88298. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  88299. +#if defined(LM_INTERFACE)
  88300. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  88301. +#elif defined(PCI_INTERFACE)
  88302. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  88303. +#elif defined(PLATFORM_INTERFACE)
  88304. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  88305. +#endif
  88306. +
  88307. +/**
  88308. + * Helper macro returning the otg_device structure of a given struct device
  88309. + *
  88310. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  88311. + */
  88312. +#ifdef LM_INTERFACE
  88313. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  88314. + struct lm_device *lm_dev = \
  88315. + container_of(_dev, struct lm_device, dev); \
  88316. + _var = lm_get_drvdata(lm_dev); \
  88317. + } while (0)
  88318. +
  88319. +#elif defined(PCI_INTERFACE)
  88320. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  88321. + _var = dev_get_drvdata(_dev); \
  88322. + } while (0)
  88323. +
  88324. +#elif defined(PLATFORM_INTERFACE)
  88325. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  88326. + struct platform_device *platform_dev = \
  88327. + container_of(_dev, struct platform_device, dev); \
  88328. + _var = platform_get_drvdata(platform_dev); \
  88329. + } while (0)
  88330. +#endif
  88331. +
  88332. +
  88333. +/**
  88334. + * Helper macro returning the struct dev of the given struct os_dependent
  88335. + *
  88336. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  88337. + */
  88338. +#ifdef LM_INTERFACE
  88339. +#define DWC_OTG_OS_GETDEV(_osdep) \
  88340. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  88341. +#elif defined(PCI_INTERFACE)
  88342. +#define DWC_OTG_OS_GETDEV(_osdep) \
  88343. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  88344. +#elif defined(PLATFORM_INTERFACE)
  88345. +#define DWC_OTG_OS_GETDEV(_osdep) \
  88346. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  88347. +#endif
  88348. +
  88349. +
  88350. +
  88351. +
  88352. +#endif /* _DWC_OS_DEP_H_ */
  88353. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  88354. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 1970-01-01 01:00:00.000000000 +0100
  88355. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.c 2014-09-14 19:04:13.000000000 +0200
  88356. @@ -0,0 +1,2712 @@
  88357. +/* ==========================================================================
  88358. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  88359. + * $Revision: #101 $
  88360. + * $Date: 2012/08/10 $
  88361. + * $Change: 2047372 $
  88362. + *
  88363. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  88364. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  88365. + * otherwise expressly agreed to in writing between Synopsys and you.
  88366. + *
  88367. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  88368. + * any End User Software License Agreement or Agreement for Licensed Product
  88369. + * with Synopsys or any supplement thereto. You are permitted to use and
  88370. + * redistribute this Software in source and binary forms, with or without
  88371. + * modification, provided that redistributions of source code must retain this
  88372. + * notice. You may not view, use, disclose, copy or distribute this file or
  88373. + * any information contained herein except pursuant to this license grant from
  88374. + * Synopsys. If you do not agree with this notice, including the disclaimer
  88375. + * below, then you are not authorized to use the Software.
  88376. + *
  88377. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  88378. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  88379. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  88380. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  88381. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  88382. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  88383. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  88384. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  88385. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  88386. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  88387. + * DAMAGE.
  88388. + * ========================================================================== */
  88389. +#ifndef DWC_HOST_ONLY
  88390. +
  88391. +/** @file
  88392. + * This file implements PCD Core. All code in this file is portable and doesn't
  88393. + * use any OS specific functions.
  88394. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  88395. + * header file, which can be used to implement OS specific PCD interface.
  88396. + *
  88397. + * An important function of the PCD is managing interrupts generated
  88398. + * by the DWC_otg controller. The implementation of the DWC_otg device
  88399. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  88400. + *
  88401. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  88402. + * @todo Does it work when the request size is greater than DEPTSIZ
  88403. + * transfer size
  88404. + *
  88405. + */
  88406. +
  88407. +#include "dwc_otg_pcd.h"
  88408. +
  88409. +#ifdef DWC_UTE_CFI
  88410. +#include "dwc_otg_cfi.h"
  88411. +
  88412. +extern int init_cfi(cfiobject_t * cfiobj);
  88413. +#endif
  88414. +
  88415. +/**
  88416. + * Choose endpoint from ep arrays using usb_ep structure.
  88417. + */
  88418. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  88419. +{
  88420. + int i;
  88421. + if (pcd->ep0.priv == handle) {
  88422. + return &pcd->ep0;
  88423. + }
  88424. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  88425. + if (pcd->in_ep[i].priv == handle)
  88426. + return &pcd->in_ep[i];
  88427. + if (pcd->out_ep[i].priv == handle)
  88428. + return &pcd->out_ep[i];
  88429. + }
  88430. +
  88431. + return NULL;
  88432. +}
  88433. +
  88434. +/**
  88435. + * This function completes a request. It call's the request call back.
  88436. + */
  88437. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  88438. + int32_t status)
  88439. +{
  88440. + unsigned stopped = ep->stopped;
  88441. +
  88442. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  88443. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  88444. +
  88445. + /* don't modify queue heads during completion callback */
  88446. + ep->stopped = 1;
  88447. + /* spin_unlock/spin_lock now done in fops->complete() */
  88448. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  88449. + req->actual);
  88450. +
  88451. + if (ep->pcd->request_pending > 0) {
  88452. + --ep->pcd->request_pending;
  88453. + }
  88454. +
  88455. + ep->stopped = stopped;
  88456. + DWC_FREE(req);
  88457. +}
  88458. +
  88459. +/**
  88460. + * This function terminates all the requsts in the EP request queue.
  88461. + */
  88462. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  88463. +{
  88464. + dwc_otg_pcd_request_t *req;
  88465. +
  88466. + ep->stopped = 1;
  88467. +
  88468. + /* called with irqs blocked?? */
  88469. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  88470. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  88471. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  88472. + }
  88473. +}
  88474. +
  88475. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  88476. + const struct dwc_otg_pcd_function_ops *fops)
  88477. +{
  88478. + pcd->fops = fops;
  88479. +}
  88480. +
  88481. +/**
  88482. + * PCD Callback function for initializing the PCD when switching to
  88483. + * device mode.
  88484. + *
  88485. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  88486. + */
  88487. +static int32_t dwc_otg_pcd_start_cb(void *p)
  88488. +{
  88489. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  88490. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  88491. +
  88492. + /*
  88493. + * Initialized the Core for Device mode.
  88494. + */
  88495. + if (dwc_otg_is_device_mode(core_if)) {
  88496. + dwc_otg_core_dev_init(core_if);
  88497. + /* Set core_if's lock pointer to the pcd->lock */
  88498. + core_if->lock = pcd->lock;
  88499. + }
  88500. + return 1;
  88501. +}
  88502. +
  88503. +/** CFI-specific buffer allocation function for EP */
  88504. +#ifdef DWC_UTE_CFI
  88505. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  88506. + size_t buflen, int flags)
  88507. +{
  88508. + dwc_otg_pcd_ep_t *ep;
  88509. + ep = get_ep_from_handle(pcd, pep);
  88510. + if (!ep) {
  88511. + DWC_WARN("bad ep\n");
  88512. + return -DWC_E_INVALID;
  88513. + }
  88514. +
  88515. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  88516. + flags);
  88517. +}
  88518. +#else
  88519. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  88520. + size_t buflen, int flags);
  88521. +#endif
  88522. +
  88523. +/**
  88524. + * PCD Callback function for notifying the PCD when resuming from
  88525. + * suspend.
  88526. + *
  88527. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  88528. + */
  88529. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  88530. +{
  88531. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  88532. +
  88533. + if (pcd->fops->resume) {
  88534. + pcd->fops->resume(pcd);
  88535. + }
  88536. +
  88537. + /* Stop the SRP timeout timer. */
  88538. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  88539. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  88540. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  88541. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  88542. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  88543. + }
  88544. + }
  88545. + return 1;
  88546. +}
  88547. +
  88548. +/**
  88549. + * PCD Callback function for notifying the PCD device is suspended.
  88550. + *
  88551. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  88552. + */
  88553. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  88554. +{
  88555. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  88556. +
  88557. + if (pcd->fops->suspend) {
  88558. + DWC_SPINUNLOCK(pcd->lock);
  88559. + pcd->fops->suspend(pcd);
  88560. + DWC_SPINLOCK(pcd->lock);
  88561. + }
  88562. +
  88563. + return 1;
  88564. +}
  88565. +
  88566. +/**
  88567. + * PCD Callback function for stopping the PCD when switching to Host
  88568. + * mode.
  88569. + *
  88570. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  88571. + */
  88572. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  88573. +{
  88574. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  88575. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  88576. +
  88577. + dwc_otg_pcd_stop(pcd);
  88578. + return 1;
  88579. +}
  88580. +
  88581. +/**
  88582. + * PCD Callback structure for handling mode switching.
  88583. + */
  88584. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  88585. + .start = dwc_otg_pcd_start_cb,
  88586. + .stop = dwc_otg_pcd_stop_cb,
  88587. + .suspend = dwc_otg_pcd_suspend_cb,
  88588. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  88589. + .p = 0, /* Set at registration */
  88590. +};
  88591. +
  88592. +/**
  88593. + * This function allocates a DMA Descriptor chain for the Endpoint
  88594. + * buffer to be used for a transfer to/from the specified endpoint.
  88595. + */
  88596. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(dwc_dma_t * dma_desc_addr,
  88597. + uint32_t count)
  88598. +{
  88599. + return DWC_DMA_ALLOC_ATOMIC(count * sizeof(dwc_otg_dev_dma_desc_t),
  88600. + dma_desc_addr);
  88601. +}
  88602. +
  88603. +/**
  88604. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  88605. + */
  88606. +void dwc_otg_ep_free_desc_chain(dwc_otg_dev_dma_desc_t * desc_addr,
  88607. + uint32_t dma_desc_addr, uint32_t count)
  88608. +{
  88609. + DWC_DMA_FREE(count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  88610. + dma_desc_addr);
  88611. +}
  88612. +
  88613. +#ifdef DWC_EN_ISOC
  88614. +
  88615. +/**
  88616. + * This function initializes a descriptor chain for Isochronous transfer
  88617. + *
  88618. + * @param core_if Programming view of DWC_otg controller.
  88619. + * @param dwc_ep The EP to start the transfer on.
  88620. + *
  88621. + */
  88622. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  88623. + dwc_ep_t * dwc_ep)
  88624. +{
  88625. +
  88626. + dsts_data_t dsts = {.d32 = 0 };
  88627. + depctl_data_t depctl = {.d32 = 0 };
  88628. + volatile uint32_t *addr;
  88629. + int i, j;
  88630. + uint32_t len;
  88631. +
  88632. + if (dwc_ep->is_in)
  88633. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  88634. + else
  88635. + dwc_ep->desc_cnt =
  88636. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  88637. + dwc_ep->bInterval;
  88638. +
  88639. + /** Allocate descriptors for double buffering */
  88640. + dwc_ep->iso_desc_addr =
  88641. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  88642. + dwc_ep->desc_cnt * 2);
  88643. + if (dwc_ep->desc_addr) {
  88644. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  88645. + return;
  88646. + }
  88647. +
  88648. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  88649. +
  88650. + /** ISO OUT EP */
  88651. + if (dwc_ep->is_in == 0) {
  88652. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  88653. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  88654. + dma_addr_t dma_ad;
  88655. + uint32_t data_per_desc;
  88656. + dwc_otg_dev_out_ep_regs_t *out_regs =
  88657. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  88658. + int offset;
  88659. +
  88660. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  88661. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  88662. +
  88663. + /** Buffer 0 descriptors setup */
  88664. + dma_ad = dwc_ep->dma_addr0;
  88665. +
  88666. + sts.b_iso_out.bs = BS_HOST_READY;
  88667. + sts.b_iso_out.rxsts = 0;
  88668. + sts.b_iso_out.l = 0;
  88669. + sts.b_iso_out.sp = 0;
  88670. + sts.b_iso_out.ioc = 0;
  88671. + sts.b_iso_out.pid = 0;
  88672. + sts.b_iso_out.framenum = 0;
  88673. +
  88674. + offset = 0;
  88675. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  88676. + i += dwc_ep->pkt_per_frm) {
  88677. +
  88678. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  88679. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  88680. + if (len > dwc_ep->data_per_frame)
  88681. + data_per_desc =
  88682. + dwc_ep->data_per_frame -
  88683. + j * dwc_ep->maxpacket;
  88684. + else
  88685. + data_per_desc = dwc_ep->maxpacket;
  88686. + len = data_per_desc % 4;
  88687. + if (len)
  88688. + data_per_desc += 4 - len;
  88689. +
  88690. + sts.b_iso_out.rxbytes = data_per_desc;
  88691. + dma_desc->buf = dma_ad;
  88692. + dma_desc->status.d32 = sts.d32;
  88693. +
  88694. + offset += data_per_desc;
  88695. + dma_desc++;
  88696. + dma_ad += data_per_desc;
  88697. + }
  88698. + }
  88699. +
  88700. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  88701. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  88702. + if (len > dwc_ep->data_per_frame)
  88703. + data_per_desc =
  88704. + dwc_ep->data_per_frame -
  88705. + j * dwc_ep->maxpacket;
  88706. + else
  88707. + data_per_desc = dwc_ep->maxpacket;
  88708. + len = data_per_desc % 4;
  88709. + if (len)
  88710. + data_per_desc += 4 - len;
  88711. + sts.b_iso_out.rxbytes = data_per_desc;
  88712. + dma_desc->buf = dma_ad;
  88713. + dma_desc->status.d32 = sts.d32;
  88714. +
  88715. + offset += data_per_desc;
  88716. + dma_desc++;
  88717. + dma_ad += data_per_desc;
  88718. + }
  88719. +
  88720. + sts.b_iso_out.ioc = 1;
  88721. + len = (j + 1) * dwc_ep->maxpacket;
  88722. + if (len > dwc_ep->data_per_frame)
  88723. + data_per_desc =
  88724. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  88725. + else
  88726. + data_per_desc = dwc_ep->maxpacket;
  88727. + len = data_per_desc % 4;
  88728. + if (len)
  88729. + data_per_desc += 4 - len;
  88730. + sts.b_iso_out.rxbytes = data_per_desc;
  88731. +
  88732. + dma_desc->buf = dma_ad;
  88733. + dma_desc->status.d32 = sts.d32;
  88734. + dma_desc++;
  88735. +
  88736. + /** Buffer 1 descriptors setup */
  88737. + sts.b_iso_out.ioc = 0;
  88738. + dma_ad = dwc_ep->dma_addr1;
  88739. +
  88740. + offset = 0;
  88741. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  88742. + i += dwc_ep->pkt_per_frm) {
  88743. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  88744. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  88745. + if (len > dwc_ep->data_per_frame)
  88746. + data_per_desc =
  88747. + dwc_ep->data_per_frame -
  88748. + j * dwc_ep->maxpacket;
  88749. + else
  88750. + data_per_desc = dwc_ep->maxpacket;
  88751. + len = data_per_desc % 4;
  88752. + if (len)
  88753. + data_per_desc += 4 - len;
  88754. +
  88755. + data_per_desc =
  88756. + sts.b_iso_out.rxbytes = data_per_desc;
  88757. + dma_desc->buf = dma_ad;
  88758. + dma_desc->status.d32 = sts.d32;
  88759. +
  88760. + offset += data_per_desc;
  88761. + dma_desc++;
  88762. + dma_ad += data_per_desc;
  88763. + }
  88764. + }
  88765. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  88766. + data_per_desc =
  88767. + ((j + 1) * dwc_ep->maxpacket >
  88768. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  88769. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  88770. + data_per_desc +=
  88771. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  88772. + sts.b_iso_out.rxbytes = data_per_desc;
  88773. + dma_desc->buf = dma_ad;
  88774. + dma_desc->status.d32 = sts.d32;
  88775. +
  88776. + offset += data_per_desc;
  88777. + dma_desc++;
  88778. + dma_ad += data_per_desc;
  88779. + }
  88780. +
  88781. + sts.b_iso_out.ioc = 1;
  88782. + sts.b_iso_out.l = 1;
  88783. + data_per_desc =
  88784. + ((j + 1) * dwc_ep->maxpacket >
  88785. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  88786. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  88787. + data_per_desc +=
  88788. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  88789. + sts.b_iso_out.rxbytes = data_per_desc;
  88790. +
  88791. + dma_desc->buf = dma_ad;
  88792. + dma_desc->status.d32 = sts.d32;
  88793. +
  88794. + dwc_ep->next_frame = 0;
  88795. +
  88796. + /** Write dma_ad into DOEPDMA register */
  88797. + DWC_WRITE_REG32(&(out_regs->doepdma),
  88798. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  88799. +
  88800. + }
  88801. + /** ISO IN EP */
  88802. + else {
  88803. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  88804. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  88805. + dma_addr_t dma_ad;
  88806. + dwc_otg_dev_in_ep_regs_t *in_regs =
  88807. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  88808. + unsigned int frmnumber;
  88809. + fifosize_data_t txfifosize, rxfifosize;
  88810. +
  88811. + txfifosize.d32 =
  88812. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  88813. + dtxfsts);
  88814. + rxfifosize.d32 =
  88815. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  88816. +
  88817. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  88818. +
  88819. + dma_ad = dwc_ep->dma_addr0;
  88820. +
  88821. + dsts.d32 =
  88822. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  88823. +
  88824. + sts.b_iso_in.bs = BS_HOST_READY;
  88825. + sts.b_iso_in.txsts = 0;
  88826. + sts.b_iso_in.sp =
  88827. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  88828. + sts.b_iso_in.ioc = 0;
  88829. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  88830. +
  88831. + frmnumber = dwc_ep->next_frame;
  88832. +
  88833. + sts.b_iso_in.framenum = frmnumber;
  88834. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  88835. + sts.b_iso_in.l = 0;
  88836. +
  88837. + /** Buffer 0 descriptors setup */
  88838. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  88839. + dma_desc->buf = dma_ad;
  88840. + dma_desc->status.d32 = sts.d32;
  88841. + dma_desc++;
  88842. +
  88843. + dma_ad += dwc_ep->data_per_frame;
  88844. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  88845. + }
  88846. +
  88847. + sts.b_iso_in.ioc = 1;
  88848. + dma_desc->buf = dma_ad;
  88849. + dma_desc->status.d32 = sts.d32;
  88850. + ++dma_desc;
  88851. +
  88852. + /** Buffer 1 descriptors setup */
  88853. + sts.b_iso_in.ioc = 0;
  88854. + dma_ad = dwc_ep->dma_addr1;
  88855. +
  88856. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  88857. + i += dwc_ep->pkt_per_frm) {
  88858. + dma_desc->buf = dma_ad;
  88859. + dma_desc->status.d32 = sts.d32;
  88860. + dma_desc++;
  88861. +
  88862. + dma_ad += dwc_ep->data_per_frame;
  88863. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  88864. +
  88865. + sts.b_iso_in.ioc = 0;
  88866. + }
  88867. + sts.b_iso_in.ioc = 1;
  88868. + sts.b_iso_in.l = 1;
  88869. +
  88870. + dma_desc->buf = dma_ad;
  88871. + dma_desc->status.d32 = sts.d32;
  88872. +
  88873. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  88874. +
  88875. + /** Write dma_ad into diepdma register */
  88876. + DWC_WRITE_REG32(&(in_regs->diepdma),
  88877. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  88878. + }
  88879. + /** Enable endpoint, clear nak */
  88880. + depctl.d32 = 0;
  88881. + depctl.b.epena = 1;
  88882. + depctl.b.usbactep = 1;
  88883. + depctl.b.cnak = 1;
  88884. +
  88885. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  88886. + depctl.d32 = DWC_READ_REG32(addr);
  88887. +}
  88888. +
  88889. +/**
  88890. + * This function initializes a descriptor chain for Isochronous transfer
  88891. + *
  88892. + * @param core_if Programming view of DWC_otg controller.
  88893. + * @param ep The EP to start the transfer on.
  88894. + *
  88895. + */
  88896. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  88897. + dwc_ep_t * ep)
  88898. +{
  88899. + depctl_data_t depctl = {.d32 = 0 };
  88900. + volatile uint32_t *addr;
  88901. +
  88902. + if (ep->is_in) {
  88903. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  88904. + } else {
  88905. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  88906. + }
  88907. +
  88908. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  88909. + return;
  88910. + } else {
  88911. + deptsiz_data_t deptsiz = {.d32 = 0 };
  88912. +
  88913. + ep->xfer_len =
  88914. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  88915. + ep->pkt_cnt =
  88916. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  88917. + ep->xfer_count = 0;
  88918. + ep->xfer_buff =
  88919. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  88920. + ep->dma_addr =
  88921. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  88922. +
  88923. + if (ep->is_in) {
  88924. + /* Program the transfer size and packet count
  88925. + * as follows: xfersize = N * maxpacket +
  88926. + * short_packet pktcnt = N + (short_packet
  88927. + * exist ? 1 : 0)
  88928. + */
  88929. + deptsiz.b.mc = ep->pkt_per_frm;
  88930. + deptsiz.b.xfersize = ep->xfer_len;
  88931. + deptsiz.b.pktcnt =
  88932. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  88933. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  88934. + dieptsiz, deptsiz.d32);
  88935. +
  88936. + /* Write the DMA register */
  88937. + DWC_WRITE_REG32(&
  88938. + (core_if->dev_if->in_ep_regs[ep->num]->
  88939. + diepdma), (uint32_t) ep->dma_addr);
  88940. +
  88941. + } else {
  88942. + deptsiz.b.pktcnt =
  88943. + (ep->xfer_len + (ep->maxpacket - 1)) /
  88944. + ep->maxpacket;
  88945. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  88946. +
  88947. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  88948. + doeptsiz, deptsiz.d32);
  88949. +
  88950. + /* Write the DMA register */
  88951. + DWC_WRITE_REG32(&
  88952. + (core_if->dev_if->out_ep_regs[ep->num]->
  88953. + doepdma), (uint32_t) ep->dma_addr);
  88954. +
  88955. + }
  88956. + /** Enable endpoint, clear nak */
  88957. + depctl.d32 = 0;
  88958. + depctl.b.epena = 1;
  88959. + depctl.b.cnak = 1;
  88960. +
  88961. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  88962. + }
  88963. +}
  88964. +
  88965. +/**
  88966. + * This function does the setup for a data transfer for an EP and
  88967. + * starts the transfer. For an IN transfer, the packets will be
  88968. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  88969. + * the packets are unloaded from the Rx FIFO in the ISR.
  88970. + *
  88971. + * @param core_if Programming view of DWC_otg controller.
  88972. + * @param ep The EP to start the transfer on.
  88973. + */
  88974. +
  88975. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  88976. + dwc_ep_t * ep)
  88977. +{
  88978. + if (core_if->dma_enable) {
  88979. + if (core_if->dma_desc_enable) {
  88980. + if (ep->is_in) {
  88981. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  88982. + } else {
  88983. + ep->desc_cnt = ep->pkt_cnt;
  88984. + }
  88985. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  88986. + } else {
  88987. + if (core_if->pti_enh_enable) {
  88988. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  88989. + } else {
  88990. + ep->cur_pkt_addr =
  88991. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  88992. + xfer_buff0;
  88993. + ep->cur_pkt_dma_addr =
  88994. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  88995. + dma_addr0;
  88996. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  88997. + }
  88998. + }
  88999. + } else {
  89000. + ep->cur_pkt_addr =
  89001. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  89002. + ep->cur_pkt_dma_addr =
  89003. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  89004. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  89005. + }
  89006. +}
  89007. +
  89008. +/**
  89009. + * This function stops transfer for an EP and
  89010. + * resets the ep's variables.
  89011. + *
  89012. + * @param core_if Programming view of DWC_otg controller.
  89013. + * @param ep The EP to start the transfer on.
  89014. + */
  89015. +
  89016. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  89017. +{
  89018. + depctl_data_t depctl = {.d32 = 0 };
  89019. + volatile uint32_t *addr;
  89020. +
  89021. + if (ep->is_in == 1) {
  89022. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  89023. + } else {
  89024. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  89025. + }
  89026. +
  89027. + /* disable the ep */
  89028. + depctl.d32 = DWC_READ_REG32(addr);
  89029. +
  89030. + depctl.b.epdis = 1;
  89031. + depctl.b.snak = 1;
  89032. +
  89033. + DWC_WRITE_REG32(addr, depctl.d32);
  89034. +
  89035. + if (core_if->dma_desc_enable &&
  89036. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  89037. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  89038. + ep->iso_dma_desc_addr,
  89039. + ep->desc_cnt * 2);
  89040. + }
  89041. +
  89042. + /* reset varibales */
  89043. + ep->dma_addr0 = 0;
  89044. + ep->dma_addr1 = 0;
  89045. + ep->xfer_buff0 = 0;
  89046. + ep->xfer_buff1 = 0;
  89047. + ep->data_per_frame = 0;
  89048. + ep->data_pattern_frame = 0;
  89049. + ep->sync_frame = 0;
  89050. + ep->buf_proc_intrvl = 0;
  89051. + ep->bInterval = 0;
  89052. + ep->proc_buf_num = 0;
  89053. + ep->pkt_per_frm = 0;
  89054. + ep->pkt_per_frm = 0;
  89055. + ep->desc_cnt = 0;
  89056. + ep->iso_desc_addr = 0;
  89057. + ep->iso_dma_desc_addr = 0;
  89058. +}
  89059. +
  89060. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  89061. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  89062. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  89063. + int data_per_frame, int start_frame,
  89064. + int buf_proc_intrvl, void *req_handle,
  89065. + int atomic_alloc)
  89066. +{
  89067. + dwc_otg_pcd_ep_t *ep;
  89068. + dwc_irqflags_t flags = 0;
  89069. + dwc_ep_t *dwc_ep;
  89070. + int32_t frm_data;
  89071. + dsts_data_t dsts;
  89072. + dwc_otg_core_if_t *core_if;
  89073. +
  89074. + ep = get_ep_from_handle(pcd, ep_handle);
  89075. +
  89076. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  89077. + DWC_WARN("bad ep\n");
  89078. + return -DWC_E_INVALID;
  89079. + }
  89080. +
  89081. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89082. + core_if = GET_CORE_IF(pcd);
  89083. + dwc_ep = &ep->dwc_ep;
  89084. +
  89085. + if (ep->iso_req_handle) {
  89086. + DWC_WARN("ISO request in progress\n");
  89087. + }
  89088. +
  89089. + dwc_ep->dma_addr0 = dma0;
  89090. + dwc_ep->dma_addr1 = dma1;
  89091. +
  89092. + dwc_ep->xfer_buff0 = buf0;
  89093. + dwc_ep->xfer_buff1 = buf1;
  89094. +
  89095. + dwc_ep->data_per_frame = data_per_frame;
  89096. +
  89097. + /** @todo - pattern data support is to be implemented in the future */
  89098. + dwc_ep->data_pattern_frame = dp_frame;
  89099. + dwc_ep->sync_frame = sync_frame;
  89100. +
  89101. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  89102. +
  89103. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  89104. +
  89105. + dwc_ep->proc_buf_num = 0;
  89106. +
  89107. + dwc_ep->pkt_per_frm = 0;
  89108. + frm_data = ep->dwc_ep.data_per_frame;
  89109. + while (frm_data > 0) {
  89110. + dwc_ep->pkt_per_frm++;
  89111. + frm_data -= ep->dwc_ep.maxpacket;
  89112. + }
  89113. +
  89114. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  89115. +
  89116. + if (start_frame == -1) {
  89117. + dwc_ep->next_frame = dsts.b.soffn + 1;
  89118. + if (dwc_ep->bInterval != 1) {
  89119. + dwc_ep->next_frame =
  89120. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  89121. + dwc_ep->next_frame %
  89122. + dwc_ep->bInterval);
  89123. + }
  89124. + } else {
  89125. + dwc_ep->next_frame = start_frame;
  89126. + }
  89127. +
  89128. + if (!core_if->pti_enh_enable) {
  89129. + dwc_ep->pkt_cnt =
  89130. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  89131. + dwc_ep->bInterval;
  89132. + } else {
  89133. + dwc_ep->pkt_cnt =
  89134. + (dwc_ep->data_per_frame *
  89135. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  89136. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  89137. + }
  89138. +
  89139. + if (core_if->dma_desc_enable) {
  89140. + dwc_ep->desc_cnt =
  89141. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  89142. + dwc_ep->bInterval;
  89143. + }
  89144. +
  89145. + if (atomic_alloc) {
  89146. + dwc_ep->pkt_info =
  89147. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  89148. + } else {
  89149. + dwc_ep->pkt_info =
  89150. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  89151. + }
  89152. + if (!dwc_ep->pkt_info) {
  89153. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89154. + return -DWC_E_NO_MEMORY;
  89155. + }
  89156. + if (core_if->pti_enh_enable) {
  89157. + dwc_memset(dwc_ep->pkt_info, 0,
  89158. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  89159. + }
  89160. +
  89161. + dwc_ep->cur_pkt = 0;
  89162. + ep->iso_req_handle = req_handle;
  89163. +
  89164. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89165. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  89166. + return 0;
  89167. +}
  89168. +
  89169. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  89170. + void *req_handle)
  89171. +{
  89172. + dwc_irqflags_t flags = 0;
  89173. + dwc_otg_pcd_ep_t *ep;
  89174. + dwc_ep_t *dwc_ep;
  89175. +
  89176. + ep = get_ep_from_handle(pcd, ep_handle);
  89177. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  89178. + DWC_WARN("bad ep\n");
  89179. + return -DWC_E_INVALID;
  89180. + }
  89181. + dwc_ep = &ep->dwc_ep;
  89182. +
  89183. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  89184. +
  89185. + DWC_FREE(dwc_ep->pkt_info);
  89186. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89187. + if (ep->iso_req_handle != req_handle) {
  89188. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89189. + return -DWC_E_INVALID;
  89190. + }
  89191. +
  89192. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89193. +
  89194. + ep->iso_req_handle = 0;
  89195. + return 0;
  89196. +}
  89197. +
  89198. +/**
  89199. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  89200. + * for Isochronous EPs
  89201. + *
  89202. + * - Every time a sync period completes this function is called to
  89203. + * perform data exchange between PCD and gadget
  89204. + */
  89205. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  89206. + void *req_handle)
  89207. +{
  89208. + int i;
  89209. + dwc_ep_t *dwc_ep;
  89210. +
  89211. + dwc_ep = &ep->dwc_ep;
  89212. +
  89213. + DWC_SPINUNLOCK(ep->pcd->lock);
  89214. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  89215. + dwc_ep->proc_buf_num ^ 0x1);
  89216. + DWC_SPINLOCK(ep->pcd->lock);
  89217. +
  89218. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  89219. + dwc_ep->pkt_info[i].status = 0;
  89220. + dwc_ep->pkt_info[i].offset = 0;
  89221. + dwc_ep->pkt_info[i].length = 0;
  89222. + }
  89223. +}
  89224. +
  89225. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  89226. + void *iso_req_handle)
  89227. +{
  89228. + dwc_otg_pcd_ep_t *ep;
  89229. + dwc_ep_t *dwc_ep;
  89230. +
  89231. + ep = get_ep_from_handle(pcd, ep_handle);
  89232. + if (!ep->desc || ep->dwc_ep.num == 0) {
  89233. + DWC_WARN("bad ep\n");
  89234. + return -DWC_E_INVALID;
  89235. + }
  89236. + dwc_ep = &ep->dwc_ep;
  89237. +
  89238. + return dwc_ep->pkt_cnt;
  89239. +}
  89240. +
  89241. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  89242. + void *iso_req_handle, int packet,
  89243. + int *status, int *actual, int *offset)
  89244. +{
  89245. + dwc_otg_pcd_ep_t *ep;
  89246. + dwc_ep_t *dwc_ep;
  89247. +
  89248. + ep = get_ep_from_handle(pcd, ep_handle);
  89249. + if (!ep)
  89250. + DWC_WARN("bad ep\n");
  89251. +
  89252. + dwc_ep = &ep->dwc_ep;
  89253. +
  89254. + *status = dwc_ep->pkt_info[packet].status;
  89255. + *actual = dwc_ep->pkt_info[packet].length;
  89256. + *offset = dwc_ep->pkt_info[packet].offset;
  89257. +}
  89258. +
  89259. +#endif /* DWC_EN_ISOC */
  89260. +
  89261. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  89262. + uint32_t is_in, uint32_t ep_num)
  89263. +{
  89264. + /* Init EP structure */
  89265. + pcd_ep->desc = 0;
  89266. + pcd_ep->pcd = pcd;
  89267. + pcd_ep->stopped = 1;
  89268. + pcd_ep->queue_sof = 0;
  89269. +
  89270. + /* Init DWC ep structure */
  89271. + pcd_ep->dwc_ep.is_in = is_in;
  89272. + pcd_ep->dwc_ep.num = ep_num;
  89273. + pcd_ep->dwc_ep.active = 0;
  89274. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  89275. + /* Control until ep is actvated */
  89276. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  89277. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  89278. + pcd_ep->dwc_ep.dma_addr = 0;
  89279. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  89280. + pcd_ep->dwc_ep.xfer_buff = 0;
  89281. + pcd_ep->dwc_ep.xfer_len = 0;
  89282. + pcd_ep->dwc_ep.xfer_count = 0;
  89283. + pcd_ep->dwc_ep.sent_zlp = 0;
  89284. + pcd_ep->dwc_ep.total_len = 0;
  89285. + pcd_ep->dwc_ep.desc_addr = 0;
  89286. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  89287. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  89288. +}
  89289. +
  89290. +/**
  89291. + * Initialize ep's
  89292. + */
  89293. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  89294. +{
  89295. + int i;
  89296. + uint32_t hwcfg1;
  89297. + dwc_otg_pcd_ep_t *ep;
  89298. + int in_ep_cntr, out_ep_cntr;
  89299. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  89300. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  89301. +
  89302. + /**
  89303. + * Initialize the EP0 structure.
  89304. + */
  89305. + ep = &pcd->ep0;
  89306. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  89307. +
  89308. + in_ep_cntr = 0;
  89309. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  89310. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  89311. + if ((hwcfg1 & 0x1) == 0) {
  89312. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  89313. + in_ep_cntr++;
  89314. + /**
  89315. + * @todo NGS: Add direction to EP, based on contents
  89316. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  89317. + * sprintf(";r
  89318. + */
  89319. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  89320. +
  89321. + DWC_CIRCLEQ_INIT(&ep->queue);
  89322. + }
  89323. + hwcfg1 >>= 2;
  89324. + }
  89325. +
  89326. + out_ep_cntr = 0;
  89327. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  89328. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  89329. + if ((hwcfg1 & 0x1) == 0) {
  89330. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  89331. + out_ep_cntr++;
  89332. + /**
  89333. + * @todo NGS: Add direction to EP, based on contents
  89334. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  89335. + * sprintf(";r
  89336. + */
  89337. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  89338. + DWC_CIRCLEQ_INIT(&ep->queue);
  89339. + }
  89340. + hwcfg1 >>= 2;
  89341. + }
  89342. +
  89343. + pcd->ep0state = EP0_DISCONNECT;
  89344. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  89345. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  89346. +}
  89347. +
  89348. +/**
  89349. + * This function is called when the SRP timer expires. The SRP should
  89350. + * complete within 6 seconds.
  89351. + */
  89352. +static void srp_timeout(void *ptr)
  89353. +{
  89354. + gotgctl_data_t gotgctl;
  89355. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  89356. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  89357. +
  89358. + gotgctl.d32 = DWC_READ_REG32(addr);
  89359. +
  89360. + core_if->srp_timer_started = 0;
  89361. +
  89362. + if (core_if->adp_enable) {
  89363. + if (gotgctl.b.bsesvld == 0) {
  89364. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  89365. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  89366. + /* Power off the core */
  89367. + if (core_if->power_down == 2) {
  89368. + gpwrdn.b.pwrdnswtch = 1;
  89369. + DWC_MODIFY_REG32(&core_if->
  89370. + core_global_regs->gpwrdn,
  89371. + gpwrdn.d32, 0);
  89372. + }
  89373. +
  89374. + gpwrdn.d32 = 0;
  89375. + gpwrdn.b.pmuintsel = 1;
  89376. + gpwrdn.b.pmuactv = 1;
  89377. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  89378. + gpwrdn.d32);
  89379. + dwc_otg_adp_probe_start(core_if);
  89380. + } else {
  89381. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  89382. + core_if->op_state = B_PERIPHERAL;
  89383. + dwc_otg_core_init(core_if);
  89384. + dwc_otg_enable_global_interrupts(core_if);
  89385. + cil_pcd_start(core_if);
  89386. + }
  89387. + }
  89388. +
  89389. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  89390. + (core_if->core_params->i2c_enable)) {
  89391. + DWC_PRINTF("SRP Timeout\n");
  89392. +
  89393. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  89394. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  89395. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  89396. + }
  89397. +
  89398. + /* Clear Session Request */
  89399. + gotgctl.d32 = 0;
  89400. + gotgctl.b.sesreq = 1;
  89401. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  89402. + gotgctl.d32, 0);
  89403. +
  89404. + core_if->srp_success = 0;
  89405. + } else {
  89406. + __DWC_ERROR("Device not connected/responding\n");
  89407. + gotgctl.b.sesreq = 0;
  89408. + DWC_WRITE_REG32(addr, gotgctl.d32);
  89409. + }
  89410. + } else if (gotgctl.b.sesreq) {
  89411. + DWC_PRINTF("SRP Timeout\n");
  89412. +
  89413. + __DWC_ERROR("Device not connected/responding\n");
  89414. + gotgctl.b.sesreq = 0;
  89415. + DWC_WRITE_REG32(addr, gotgctl.d32);
  89416. + } else {
  89417. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  89418. + }
  89419. +}
  89420. +
  89421. +/**
  89422. + * Tasklet
  89423. + *
  89424. + */
  89425. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  89426. +
  89427. +static void start_xfer_tasklet_func(void *data)
  89428. +{
  89429. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  89430. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  89431. +
  89432. + int i;
  89433. + depctl_data_t diepctl;
  89434. +
  89435. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  89436. +
  89437. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  89438. +
  89439. + if (pcd->ep0.queue_sof) {
  89440. + pcd->ep0.queue_sof = 0;
  89441. + start_next_request(&pcd->ep0);
  89442. + // break;
  89443. + }
  89444. +
  89445. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  89446. + depctl_data_t diepctl;
  89447. + diepctl.d32 =
  89448. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  89449. +
  89450. + if (pcd->in_ep[i].queue_sof) {
  89451. + pcd->in_ep[i].queue_sof = 0;
  89452. + start_next_request(&pcd->in_ep[i]);
  89453. + // break;
  89454. + }
  89455. + }
  89456. +
  89457. + return;
  89458. +}
  89459. +
  89460. +/**
  89461. + * This function initialized the PCD portion of the driver.
  89462. + *
  89463. + */
  89464. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if)
  89465. +{
  89466. + dwc_otg_pcd_t *pcd = NULL;
  89467. + dwc_otg_dev_if_t *dev_if;
  89468. + int i;
  89469. +
  89470. + /*
  89471. + * Allocate PCD structure
  89472. + */
  89473. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  89474. +
  89475. + if (pcd == NULL) {
  89476. + return NULL;
  89477. + }
  89478. +
  89479. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  89480. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
  89481. +#else
  89482. + pcd->lock = DWC_SPINLOCK_ALLOC();
  89483. +#endif
  89484. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  89485. + pcd, core_if);//GRAYG
  89486. + if (!pcd->lock) {
  89487. + DWC_ERROR("Could not allocate lock for pcd");
  89488. + DWC_FREE(pcd);
  89489. + return NULL;
  89490. + }
  89491. + /* Set core_if's lock pointer to hcd->lock */
  89492. + core_if->lock = pcd->lock;
  89493. + pcd->core_if = core_if;
  89494. +
  89495. + dev_if = core_if->dev_if;
  89496. + dev_if->isoc_ep = NULL;
  89497. +
  89498. + if (core_if->hwcfg4.b.ded_fifo_en) {
  89499. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  89500. + } else {
  89501. + DWC_PRINTF("Shared Tx FIFO mode\n");
  89502. + }
  89503. +
  89504. + /*
  89505. + * Initialized the Core for Device mode here if there is nod ADP support.
  89506. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  89507. + */
  89508. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  89509. + dwc_otg_core_dev_init(core_if);
  89510. + }
  89511. +
  89512. + /*
  89513. + * Register the PCD Callbacks.
  89514. + */
  89515. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  89516. +
  89517. + /*
  89518. + * Initialize the DMA buffer for SETUP packets
  89519. + */
  89520. + if (GET_CORE_IF(pcd)->dma_enable) {
  89521. + pcd->setup_pkt =
  89522. + DWC_DMA_ALLOC(sizeof(*pcd->setup_pkt) * 5,
  89523. + &pcd->setup_pkt_dma_handle);
  89524. + if (pcd->setup_pkt == NULL) {
  89525. + DWC_FREE(pcd);
  89526. + return NULL;
  89527. + }
  89528. +
  89529. + pcd->status_buf =
  89530. + DWC_DMA_ALLOC(sizeof(uint16_t),
  89531. + &pcd->status_buf_dma_handle);
  89532. + if (pcd->status_buf == NULL) {
  89533. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  89534. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  89535. + DWC_FREE(pcd);
  89536. + return NULL;
  89537. + }
  89538. +
  89539. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  89540. + dev_if->setup_desc_addr[0] =
  89541. + dwc_otg_ep_alloc_desc_chain
  89542. + (&dev_if->dma_setup_desc_addr[0], 1);
  89543. + dev_if->setup_desc_addr[1] =
  89544. + dwc_otg_ep_alloc_desc_chain
  89545. + (&dev_if->dma_setup_desc_addr[1], 1);
  89546. + dev_if->in_desc_addr =
  89547. + dwc_otg_ep_alloc_desc_chain
  89548. + (&dev_if->dma_in_desc_addr, 1);
  89549. + dev_if->out_desc_addr =
  89550. + dwc_otg_ep_alloc_desc_chain
  89551. + (&dev_if->dma_out_desc_addr, 1);
  89552. + pcd->data_terminated = 0;
  89553. +
  89554. + if (dev_if->setup_desc_addr[0] == 0
  89555. + || dev_if->setup_desc_addr[1] == 0
  89556. + || dev_if->in_desc_addr == 0
  89557. + || dev_if->out_desc_addr == 0) {
  89558. +
  89559. + if (dev_if->out_desc_addr)
  89560. + dwc_otg_ep_free_desc_chain
  89561. + (dev_if->out_desc_addr,
  89562. + dev_if->dma_out_desc_addr, 1);
  89563. + if (dev_if->in_desc_addr)
  89564. + dwc_otg_ep_free_desc_chain
  89565. + (dev_if->in_desc_addr,
  89566. + dev_if->dma_in_desc_addr, 1);
  89567. + if (dev_if->setup_desc_addr[1])
  89568. + dwc_otg_ep_free_desc_chain
  89569. + (dev_if->setup_desc_addr[1],
  89570. + dev_if->dma_setup_desc_addr[1], 1);
  89571. + if (dev_if->setup_desc_addr[0])
  89572. + dwc_otg_ep_free_desc_chain
  89573. + (dev_if->setup_desc_addr[0],
  89574. + dev_if->dma_setup_desc_addr[0], 1);
  89575. +
  89576. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5,
  89577. + pcd->setup_pkt,
  89578. + pcd->setup_pkt_dma_handle);
  89579. + DWC_DMA_FREE(sizeof(*pcd->status_buf),
  89580. + pcd->status_buf,
  89581. + pcd->status_buf_dma_handle);
  89582. +
  89583. + DWC_FREE(pcd);
  89584. +
  89585. + return NULL;
  89586. + }
  89587. + }
  89588. + } else {
  89589. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  89590. + if (pcd->setup_pkt == NULL) {
  89591. + DWC_FREE(pcd);
  89592. + return NULL;
  89593. + }
  89594. +
  89595. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  89596. + if (pcd->status_buf == NULL) {
  89597. + DWC_FREE(pcd->setup_pkt);
  89598. + DWC_FREE(pcd);
  89599. + return NULL;
  89600. + }
  89601. + }
  89602. +
  89603. + dwc_otg_pcd_reinit(pcd);
  89604. +
  89605. + /* Allocate the cfi object for the PCD */
  89606. +#ifdef DWC_UTE_CFI
  89607. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  89608. + if (NULL == pcd->cfi)
  89609. + goto fail;
  89610. + if (init_cfi(pcd->cfi)) {
  89611. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  89612. + goto fail;
  89613. + }
  89614. +#endif
  89615. +
  89616. + /* Initialize tasklets */
  89617. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  89618. + start_xfer_tasklet_func, pcd);
  89619. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  89620. + do_test_mode, pcd);
  89621. +
  89622. + /* Initialize SRP timer */
  89623. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  89624. +
  89625. + if (core_if->core_params->dev_out_nak) {
  89626. + /**
  89627. + * Initialize xfer timeout timer. Implemented for
  89628. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  89629. + */
  89630. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  89631. + pcd->core_if->ep_xfer_timer[i] =
  89632. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  89633. + &pcd->core_if->ep_xfer_info[i]);
  89634. + }
  89635. + }
  89636. +
  89637. + return pcd;
  89638. +#ifdef DWC_UTE_CFI
  89639. +fail:
  89640. +#endif
  89641. + if (pcd->setup_pkt)
  89642. + DWC_FREE(pcd->setup_pkt);
  89643. + if (pcd->status_buf)
  89644. + DWC_FREE(pcd->status_buf);
  89645. +#ifdef DWC_UTE_CFI
  89646. + if (pcd->cfi)
  89647. + DWC_FREE(pcd->cfi);
  89648. +#endif
  89649. + if (pcd)
  89650. + DWC_FREE(pcd);
  89651. + return NULL;
  89652. +
  89653. +}
  89654. +
  89655. +/**
  89656. + * Remove PCD specific data
  89657. + */
  89658. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  89659. +{
  89660. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  89661. + int i;
  89662. + if (pcd->core_if->core_params->dev_out_nak) {
  89663. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  89664. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  89665. + pcd->core_if->ep_xfer_info[i].state = 0;
  89666. + }
  89667. + }
  89668. +
  89669. + if (GET_CORE_IF(pcd)->dma_enable) {
  89670. + DWC_DMA_FREE(sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  89671. + pcd->setup_pkt_dma_handle);
  89672. + DWC_DMA_FREE(sizeof(uint16_t), pcd->status_buf,
  89673. + pcd->status_buf_dma_handle);
  89674. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  89675. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[0],
  89676. + dev_if->dma_setup_desc_addr
  89677. + [0], 1);
  89678. + dwc_otg_ep_free_desc_chain(dev_if->setup_desc_addr[1],
  89679. + dev_if->dma_setup_desc_addr
  89680. + [1], 1);
  89681. + dwc_otg_ep_free_desc_chain(dev_if->in_desc_addr,
  89682. + dev_if->dma_in_desc_addr, 1);
  89683. + dwc_otg_ep_free_desc_chain(dev_if->out_desc_addr,
  89684. + dev_if->dma_out_desc_addr,
  89685. + 1);
  89686. + }
  89687. + } else {
  89688. + DWC_FREE(pcd->setup_pkt);
  89689. + DWC_FREE(pcd->status_buf);
  89690. + }
  89691. + DWC_SPINLOCK_FREE(pcd->lock);
  89692. + /* Set core_if's lock pointer to NULL */
  89693. + pcd->core_if->lock = NULL;
  89694. +
  89695. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  89696. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  89697. + if (pcd->core_if->core_params->dev_out_nak) {
  89698. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  89699. + if (pcd->core_if->ep_xfer_timer[i]) {
  89700. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  89701. + }
  89702. + }
  89703. + }
  89704. +
  89705. +/* Release the CFI object's dynamic memory */
  89706. +#ifdef DWC_UTE_CFI
  89707. + if (pcd->cfi->ops.release) {
  89708. + pcd->cfi->ops.release(pcd->cfi);
  89709. + }
  89710. +#endif
  89711. +
  89712. + DWC_FREE(pcd);
  89713. +}
  89714. +
  89715. +/**
  89716. + * Returns whether registered pcd is dual speed or not
  89717. + */
  89718. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  89719. +{
  89720. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  89721. +
  89722. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  89723. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  89724. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  89725. + (core_if->core_params->ulpi_fs_ls))) {
  89726. + return 0;
  89727. + }
  89728. +
  89729. + return 1;
  89730. +}
  89731. +
  89732. +/**
  89733. + * Returns whether registered pcd is OTG capable or not
  89734. + */
  89735. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  89736. +{
  89737. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  89738. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  89739. +
  89740. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  89741. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  89742. + return 0;
  89743. + }
  89744. +
  89745. + return 1;
  89746. +}
  89747. +
  89748. +/**
  89749. + * This function assigns periodic Tx FIFO to an periodic EP
  89750. + * in shared Tx FIFO mode
  89751. + */
  89752. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  89753. +{
  89754. + uint32_t TxMsk = 1;
  89755. + int i;
  89756. +
  89757. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  89758. + if ((TxMsk & core_if->tx_msk) == 0) {
  89759. + core_if->tx_msk |= TxMsk;
  89760. + return i + 1;
  89761. + }
  89762. + TxMsk <<= 1;
  89763. + }
  89764. + return 0;
  89765. +}
  89766. +
  89767. +/**
  89768. + * This function assigns periodic Tx FIFO to an periodic EP
  89769. + * in shared Tx FIFO mode
  89770. + */
  89771. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  89772. +{
  89773. + uint32_t PerTxMsk = 1;
  89774. + int i;
  89775. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  89776. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  89777. + core_if->p_tx_msk |= PerTxMsk;
  89778. + return i + 1;
  89779. + }
  89780. + PerTxMsk <<= 1;
  89781. + }
  89782. + return 0;
  89783. +}
  89784. +
  89785. +/**
  89786. + * This function releases periodic Tx FIFO
  89787. + * in shared Tx FIFO mode
  89788. + */
  89789. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  89790. + uint32_t fifo_num)
  89791. +{
  89792. + core_if->p_tx_msk =
  89793. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  89794. +}
  89795. +
  89796. +/**
  89797. + * This function releases periodic Tx FIFO
  89798. + * in shared Tx FIFO mode
  89799. + */
  89800. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  89801. +{
  89802. + core_if->tx_msk =
  89803. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  89804. +}
  89805. +
  89806. +/**
  89807. + * This function is being called from gadget
  89808. + * to enable PCD endpoint.
  89809. + */
  89810. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  89811. + const uint8_t * ep_desc, void *usb_ep)
  89812. +{
  89813. + int num, dir;
  89814. + dwc_otg_pcd_ep_t *ep = NULL;
  89815. + const usb_endpoint_descriptor_t *desc;
  89816. + dwc_irqflags_t flags;
  89817. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  89818. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  89819. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  89820. + int retval = 0;
  89821. + int i, epcount;
  89822. +
  89823. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  89824. +
  89825. + if (!desc) {
  89826. + pcd->ep0.priv = usb_ep;
  89827. + ep = &pcd->ep0;
  89828. + retval = -DWC_E_INVALID;
  89829. + goto out;
  89830. + }
  89831. +
  89832. + num = UE_GET_ADDR(desc->bEndpointAddress);
  89833. + dir = UE_GET_DIR(desc->bEndpointAddress);
  89834. +
  89835. + if (!desc->wMaxPacketSize) {
  89836. + DWC_WARN("bad maxpacketsize\n");
  89837. + retval = -DWC_E_INVALID;
  89838. + goto out;
  89839. + }
  89840. +
  89841. + if (dir == UE_DIR_IN) {
  89842. + epcount = pcd->core_if->dev_if->num_in_eps;
  89843. + for (i = 0; i < epcount; i++) {
  89844. + if (num == pcd->in_ep[i].dwc_ep.num) {
  89845. + ep = &pcd->in_ep[i];
  89846. + break;
  89847. + }
  89848. + }
  89849. + } else {
  89850. + epcount = pcd->core_if->dev_if->num_out_eps;
  89851. + for (i = 0; i < epcount; i++) {
  89852. + if (num == pcd->out_ep[i].dwc_ep.num) {
  89853. + ep = &pcd->out_ep[i];
  89854. + break;
  89855. + }
  89856. + }
  89857. + }
  89858. +
  89859. + if (!ep) {
  89860. + DWC_WARN("bad address\n");
  89861. + retval = -DWC_E_INVALID;
  89862. + goto out;
  89863. + }
  89864. +
  89865. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89866. +
  89867. + ep->desc = desc;
  89868. + ep->priv = usb_ep;
  89869. +
  89870. + /*
  89871. + * Activate the EP
  89872. + */
  89873. + ep->stopped = 0;
  89874. +
  89875. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  89876. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  89877. +
  89878. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  89879. +
  89880. + if (ep->dwc_ep.is_in) {
  89881. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  89882. + ep->dwc_ep.tx_fifo_num = 0;
  89883. +
  89884. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  89885. + /*
  89886. + * if ISOC EP then assign a Periodic Tx FIFO.
  89887. + */
  89888. + ep->dwc_ep.tx_fifo_num =
  89889. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  89890. + }
  89891. + } else {
  89892. + /*
  89893. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  89894. + */
  89895. + ep->dwc_ep.tx_fifo_num =
  89896. + assign_tx_fifo(GET_CORE_IF(pcd));
  89897. + }
  89898. +
  89899. + /* Calculating EP info controller base address */
  89900. + if (ep->dwc_ep.tx_fifo_num
  89901. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  89902. + gdfifocfg.d32 =
  89903. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  89904. + core_global_regs->gdfifocfg);
  89905. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  89906. + dptxfsiz.d32 =
  89907. + (DWC_READ_REG32
  89908. + (&GET_CORE_IF(pcd)->core_global_regs->
  89909. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  89910. + gdfifocfg.b.epinfobase =
  89911. + gdfifocfgbase.d32 + dptxfsiz.d32;
  89912. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  89913. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  89914. + core_global_regs->gdfifocfg,
  89915. + gdfifocfg.d32);
  89916. + }
  89917. + }
  89918. + }
  89919. + /* Set initial data PID. */
  89920. + if (ep->dwc_ep.type == UE_BULK) {
  89921. + ep->dwc_ep.data_pid_start = 0;
  89922. + }
  89923. +
  89924. + /* Alloc DMA Descriptors */
  89925. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  89926. +#ifndef DWC_UTE_PER_IO
  89927. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  89928. +#endif
  89929. + ep->dwc_ep.desc_addr =
  89930. + dwc_otg_ep_alloc_desc_chain(&ep->
  89931. + dwc_ep.dma_desc_addr,
  89932. + MAX_DMA_DESC_CNT);
  89933. + if (!ep->dwc_ep.desc_addr) {
  89934. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  89935. + __func__);
  89936. + retval = -DWC_E_SHUTDOWN;
  89937. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89938. + goto out;
  89939. + }
  89940. +#ifndef DWC_UTE_PER_IO
  89941. + }
  89942. +#endif
  89943. + }
  89944. +
  89945. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  89946. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  89947. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  89948. +#ifdef DWC_UTE_PER_IO
  89949. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  89950. +#endif
  89951. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  89952. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  89953. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  89954. + }
  89955. +
  89956. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  89957. +
  89958. +#ifdef DWC_UTE_CFI
  89959. + if (pcd->cfi->ops.ep_enable) {
  89960. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  89961. + }
  89962. +#endif
  89963. +
  89964. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  89965. +
  89966. +out:
  89967. + return retval;
  89968. +}
  89969. +
  89970. +/**
  89971. + * This function is being called from gadget
  89972. + * to disable PCD endpoint.
  89973. + */
  89974. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  89975. +{
  89976. + dwc_otg_pcd_ep_t *ep;
  89977. + dwc_irqflags_t flags;
  89978. + dwc_otg_dev_dma_desc_t *desc_addr;
  89979. + dwc_dma_t dma_desc_addr;
  89980. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  89981. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  89982. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  89983. +
  89984. + ep = get_ep_from_handle(pcd, ep_handle);
  89985. +
  89986. + if (!ep || !ep->desc) {
  89987. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  89988. + return -DWC_E_INVALID;
  89989. + }
  89990. +
  89991. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  89992. +
  89993. + dwc_otg_request_nuke(ep);
  89994. +
  89995. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  89996. + if (pcd->core_if->core_params->dev_out_nak) {
  89997. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  89998. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  89999. + }
  90000. + ep->desc = NULL;
  90001. + ep->stopped = 1;
  90002. +
  90003. + gdfifocfg.d32 =
  90004. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  90005. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  90006. +
  90007. + if (ep->dwc_ep.is_in) {
  90008. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  90009. + /* Flush the Tx FIFO */
  90010. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  90011. + ep->dwc_ep.tx_fifo_num);
  90012. + }
  90013. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  90014. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  90015. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  90016. + /* Decreasing EPinfo Base Addr */
  90017. + dptxfsiz.d32 =
  90018. + (DWC_READ_REG32
  90019. + (&GET_CORE_IF(pcd)->
  90020. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  90021. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  90022. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  90023. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  90024. + gdfifocfg.d32);
  90025. + }
  90026. + }
  90027. + }
  90028. +
  90029. + /* Free DMA Descriptors */
  90030. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  90031. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  90032. + desc_addr = ep->dwc_ep.desc_addr;
  90033. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  90034. +
  90035. + /* Cannot call dma_free_coherent() with IRQs disabled */
  90036. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90037. + dwc_otg_ep_free_desc_chain(desc_addr, dma_desc_addr,
  90038. + MAX_DMA_DESC_CNT);
  90039. +
  90040. + goto out_unlocked;
  90041. + }
  90042. + }
  90043. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90044. +
  90045. +out_unlocked:
  90046. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  90047. + ep->dwc_ep.is_in ? "IN" : "OUT");
  90048. + return 0;
  90049. +
  90050. +}
  90051. +
  90052. +/******************************************************************************/
  90053. +#ifdef DWC_UTE_PER_IO
  90054. +
  90055. +/**
  90056. + * Free the request and its extended parts
  90057. + *
  90058. + */
  90059. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  90060. +{
  90061. + DWC_FREE(req->ext_req.per_io_frame_descs);
  90062. + DWC_FREE(req);
  90063. +}
  90064. +
  90065. +/**
  90066. + * Start the next request in the endpoint's queue.
  90067. + *
  90068. + */
  90069. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  90070. + dwc_otg_pcd_ep_t * ep)
  90071. +{
  90072. + int i;
  90073. + dwc_otg_pcd_request_t *req = NULL;
  90074. + dwc_ep_t *dwcep = NULL;
  90075. + struct dwc_iso_xreq_port *ereq = NULL;
  90076. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  90077. + uint16_t nat;
  90078. + depctl_data_t diepctl;
  90079. +
  90080. + dwcep = &ep->dwc_ep;
  90081. +
  90082. + if (dwcep->xiso_active_xfers > 0) {
  90083. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  90084. + DWC_WARN("There are currently active transfers for EP%d \
  90085. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  90086. + dwcep->xiso_queued_xfers);
  90087. +#endif
  90088. + return 0;
  90089. + }
  90090. +
  90091. + nat = UGETW(ep->desc->wMaxPacketSize);
  90092. + nat = (nat >> 11) & 0x03;
  90093. +
  90094. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  90095. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  90096. + ereq = &req->ext_req;
  90097. + ep->stopped = 0;
  90098. +
  90099. + /* Get the frame number */
  90100. + dwcep->xiso_frame_num =
  90101. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  90102. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  90103. +
  90104. + ddesc_iso = ereq->per_io_frame_descs;
  90105. +
  90106. + if (dwcep->is_in) {
  90107. + /* Setup DMA Descriptor chain for IN Isoc request */
  90108. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  90109. + //if ((i % (nat + 1)) == 0)
  90110. + if ( i > 0 )
  90111. + dwcep->xiso_frame_num =
  90112. + (dwcep->xiso_bInterval +
  90113. + dwcep->xiso_frame_num) & 0x3FFF;
  90114. + dwcep->desc_addr[i].buf =
  90115. + req->dma + ddesc_iso[i].offset;
  90116. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  90117. + ddesc_iso[i].length;
  90118. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  90119. + dwcep->xiso_frame_num;
  90120. + dwcep->desc_addr[i].status.b_iso_in.bs =
  90121. + BS_HOST_READY;
  90122. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  90123. + dwcep->desc_addr[i].status.b_iso_in.sp =
  90124. + (ddesc_iso[i].length %
  90125. + dwcep->maxpacket) ? 1 : 0;
  90126. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  90127. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  90128. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  90129. +
  90130. + /* Process the last descriptor */
  90131. + if (i == ereq->pio_pkt_count - 1) {
  90132. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  90133. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  90134. + }
  90135. + }
  90136. +
  90137. + /* Setup and start the transfer for this endpoint */
  90138. + dwcep->xiso_active_xfers++;
  90139. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  90140. + in_ep_regs[dwcep->num]->diepdma,
  90141. + dwcep->dma_desc_addr);
  90142. + diepctl.d32 = 0;
  90143. + diepctl.b.epena = 1;
  90144. + diepctl.b.cnak = 1;
  90145. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  90146. + in_ep_regs[dwcep->num]->diepctl, 0,
  90147. + diepctl.d32);
  90148. + } else {
  90149. + /* Setup DMA Descriptor chain for OUT Isoc request */
  90150. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  90151. + //if ((i % (nat + 1)) == 0)
  90152. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  90153. + dwcep->xiso_frame_num) & 0x3FFF;
  90154. + dwcep->desc_addr[i].buf =
  90155. + req->dma + ddesc_iso[i].offset;
  90156. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  90157. + ddesc_iso[i].length;
  90158. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  90159. + dwcep->xiso_frame_num;
  90160. + dwcep->desc_addr[i].status.b_iso_out.bs =
  90161. + BS_HOST_READY;
  90162. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  90163. + dwcep->desc_addr[i].status.b_iso_out.sp =
  90164. + (ddesc_iso[i].length %
  90165. + dwcep->maxpacket) ? 1 : 0;
  90166. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  90167. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  90168. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  90169. +
  90170. + /* Process the last descriptor */
  90171. + if (i == ereq->pio_pkt_count - 1) {
  90172. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  90173. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  90174. + }
  90175. + }
  90176. +
  90177. + /* Setup and start the transfer for this endpoint */
  90178. + dwcep->xiso_active_xfers++;
  90179. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  90180. + dev_if->out_ep_regs[dwcep->num]->
  90181. + doepdma, dwcep->dma_desc_addr);
  90182. + diepctl.d32 = 0;
  90183. + diepctl.b.epena = 1;
  90184. + diepctl.b.cnak = 1;
  90185. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  90186. + dev_if->out_ep_regs[dwcep->num]->
  90187. + doepctl, 0, diepctl.d32);
  90188. + }
  90189. +
  90190. + } else {
  90191. + ep->stopped = 1;
  90192. + }
  90193. +
  90194. + return 0;
  90195. +}
  90196. +
  90197. +/**
  90198. + * - Remove the request from the queue
  90199. + */
  90200. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  90201. +{
  90202. + dwc_otg_pcd_request_t *req = NULL;
  90203. + struct dwc_iso_xreq_port *ereq = NULL;
  90204. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  90205. + dwc_ep_t *dwcep = NULL;
  90206. + int i;
  90207. +
  90208. + //DWC_DEBUG();
  90209. + dwcep = &ep->dwc_ep;
  90210. +
  90211. + /* Get the first pending request from the queue */
  90212. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  90213. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  90214. + if (!req) {
  90215. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  90216. + return;
  90217. + }
  90218. + dwcep->xiso_active_xfers--;
  90219. + dwcep->xiso_queued_xfers--;
  90220. + /* Remove this request from the queue */
  90221. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  90222. + } else {
  90223. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  90224. + return;
  90225. + }
  90226. +
  90227. + ep->stopped = 1;
  90228. + ereq = &req->ext_req;
  90229. + ddesc_iso = ereq->per_io_frame_descs;
  90230. +
  90231. + if (dwcep->xiso_active_xfers < 0) {
  90232. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  90233. + dwcep->xiso_active_xfers);
  90234. + }
  90235. +
  90236. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  90237. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  90238. + if (dwcep->is_in) { /* IN endpoints */
  90239. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  90240. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  90241. + ddesc_iso[i].status =
  90242. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  90243. + } else { /* OUT endpoints */
  90244. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  90245. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  90246. + ddesc_iso[i].status =
  90247. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  90248. + }
  90249. + }
  90250. +
  90251. + DWC_SPINUNLOCK(ep->pcd->lock);
  90252. +
  90253. + /* Call the completion function in the non-portable logic */
  90254. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  90255. + &req->ext_req);
  90256. +
  90257. + DWC_SPINLOCK(ep->pcd->lock);
  90258. +
  90259. + /* Free the request - specific freeing needed for extended request object */
  90260. + dwc_pcd_xiso_ereq_free(ep, req);
  90261. +
  90262. + /* Start the next request */
  90263. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  90264. +
  90265. + return;
  90266. +}
  90267. +
  90268. +/**
  90269. + * Create and initialize the Isoc pkt descriptors of the extended request.
  90270. + *
  90271. + */
  90272. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  90273. + void *ereq_nonport,
  90274. + int atomic_alloc)
  90275. +{
  90276. + struct dwc_iso_xreq_port *ereq = NULL;
  90277. + struct dwc_iso_xreq_port *req_mapped = NULL;
  90278. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  90279. + uint32_t pkt_count;
  90280. + int i;
  90281. +
  90282. + ereq = &req->ext_req;
  90283. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  90284. + pkt_count = req_mapped->pio_pkt_count;
  90285. +
  90286. + /* Create the isoc descs */
  90287. + if (atomic_alloc) {
  90288. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  90289. + } else {
  90290. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  90291. + }
  90292. +
  90293. + if (!ipds) {
  90294. + DWC_ERROR("Failed to allocate isoc descriptors");
  90295. + return -DWC_E_NO_MEMORY;
  90296. + }
  90297. +
  90298. + /* Initialize the extended request fields */
  90299. + ereq->per_io_frame_descs = ipds;
  90300. + ereq->error_count = 0;
  90301. + ereq->pio_alloc_pkt_count = pkt_count;
  90302. + ereq->pio_pkt_count = pkt_count;
  90303. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  90304. +
  90305. + /* Init the Isoc descriptors */
  90306. + for (i = 0; i < pkt_count; i++) {
  90307. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  90308. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  90309. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  90310. + ipds[i].actual_length =
  90311. + req_mapped->per_io_frame_descs[i].actual_length;
  90312. + }
  90313. +
  90314. + return 0;
  90315. +}
  90316. +
  90317. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  90318. +{
  90319. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  90320. + int i;
  90321. +
  90322. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  90323. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  90324. + DWC_DEBUG("error_count=%d", ereq->error_count);
  90325. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  90326. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  90327. + DWC_DEBUG("res=%d", ereq->res);
  90328. +
  90329. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  90330. + xfd = &ereq->per_io_frame_descs[0];
  90331. + DWC_DEBUG("FD #%d", i);
  90332. +
  90333. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  90334. + DWC_DEBUG("xfd->length=%d", xfd->length);
  90335. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  90336. + DWC_DEBUG("xfd->status=%d", xfd->status);
  90337. + }
  90338. +}
  90339. +
  90340. +/**
  90341. + *
  90342. + */
  90343. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  90344. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  90345. + int zero, void *req_handle, int atomic_alloc,
  90346. + void *ereq_nonport)
  90347. +{
  90348. + dwc_otg_pcd_request_t *req = NULL;
  90349. + dwc_otg_pcd_ep_t *ep;
  90350. + dwc_irqflags_t flags;
  90351. + int res;
  90352. +
  90353. + ep = get_ep_from_handle(pcd, ep_handle);
  90354. + if (!ep) {
  90355. + DWC_WARN("bad ep\n");
  90356. + return -DWC_E_INVALID;
  90357. + }
  90358. +
  90359. + /* We support this extension only for DDMA mode */
  90360. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  90361. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  90362. + return -DWC_E_INVALID;
  90363. +
  90364. + /* Create a dwc_otg_pcd_request_t object */
  90365. + if (atomic_alloc) {
  90366. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  90367. + } else {
  90368. + req = DWC_ALLOC(sizeof(*req));
  90369. + }
  90370. +
  90371. + if (!req) {
  90372. + return -DWC_E_NO_MEMORY;
  90373. + }
  90374. +
  90375. + /* Create the Isoc descs for this request which shall be the exact match
  90376. + * of the structure sent to us from the non-portable logic */
  90377. + res =
  90378. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  90379. + if (res) {
  90380. + DWC_WARN("Failed to init the Isoc descriptors");
  90381. + DWC_FREE(req);
  90382. + return res;
  90383. + }
  90384. +
  90385. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90386. +
  90387. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  90388. + req->buf = buf;
  90389. + req->dma = dma_buf;
  90390. + req->length = buflen;
  90391. + req->sent_zlp = zero;
  90392. + req->priv = req_handle;
  90393. +
  90394. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90395. + ep->dwc_ep.dma_addr = dma_buf;
  90396. + ep->dwc_ep.start_xfer_buff = buf;
  90397. + ep->dwc_ep.xfer_buff = buf;
  90398. + ep->dwc_ep.xfer_len = 0;
  90399. + ep->dwc_ep.xfer_count = 0;
  90400. + ep->dwc_ep.sent_zlp = 0;
  90401. + ep->dwc_ep.total_len = buflen;
  90402. +
  90403. + /* Add this request to the tail */
  90404. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  90405. + ep->dwc_ep.xiso_queued_xfers++;
  90406. +
  90407. +//DWC_DEBUG("CP_0");
  90408. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  90409. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  90410. +//prn_ext_request(&req->ext_req);
  90411. +
  90412. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90413. +
  90414. + /* If the req->status == ASAP then check if there is any active transfer
  90415. + * for this endpoint. If no active transfers, then get the first entry
  90416. + * from the queue and start that transfer
  90417. + */
  90418. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  90419. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  90420. + if (res) {
  90421. + DWC_WARN("Failed to start the next Isoc transfer");
  90422. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90423. + DWC_FREE(req);
  90424. + return res;
  90425. + }
  90426. + }
  90427. +
  90428. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90429. + return 0;
  90430. +}
  90431. +
  90432. +#endif
  90433. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  90434. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  90435. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  90436. + int zero, void *req_handle, int atomic_alloc)
  90437. +{
  90438. + dwc_irqflags_t flags;
  90439. + dwc_otg_pcd_request_t *req;
  90440. + dwc_otg_pcd_ep_t *ep;
  90441. + uint32_t max_transfer;
  90442. +
  90443. + ep = get_ep_from_handle(pcd, ep_handle);
  90444. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  90445. + DWC_WARN("bad ep\n");
  90446. + return -DWC_E_INVALID;
  90447. + }
  90448. +
  90449. + if (atomic_alloc) {
  90450. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  90451. + } else {
  90452. + req = DWC_ALLOC(sizeof(*req));
  90453. + }
  90454. +
  90455. + if (!req) {
  90456. + return -DWC_E_NO_MEMORY;
  90457. + }
  90458. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  90459. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  90460. + if (ep->dwc_ep.num != 0) {
  90461. + DWC_ERROR("queue req %p, len %d buf %p\n",
  90462. + req_handle, buflen, buf);
  90463. + }
  90464. + }
  90465. +
  90466. + req->buf = buf;
  90467. + req->dma = dma_buf;
  90468. + req->length = buflen;
  90469. + req->sent_zlp = zero;
  90470. + req->priv = req_handle;
  90471. + req->dw_align_buf = NULL;
  90472. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  90473. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  90474. + req->dw_align_buf = DWC_DMA_ALLOC(buflen,
  90475. + &req->dw_align_buf_dma);
  90476. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90477. +
  90478. + /*
  90479. + * After adding request to the queue for IN ISOC wait for In Token Received
  90480. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  90481. + * Received when EP is disabled interrupt to obtain starting microframe
  90482. + * (odd/even) start transfer
  90483. + */
  90484. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  90485. + if (req != 0) {
  90486. + depctl_data_t depctl = {.d32 =
  90487. + DWC_READ_REG32(&pcd->core_if->dev_if->
  90488. + in_ep_regs[ep->dwc_ep.num]->
  90489. + diepctl) };
  90490. + ++pcd->request_pending;
  90491. +
  90492. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  90493. + if (ep->dwc_ep.is_in) {
  90494. + depctl.b.cnak = 1;
  90495. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  90496. + in_ep_regs[ep->dwc_ep.num]->
  90497. + diepctl, depctl.d32);
  90498. + }
  90499. +
  90500. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90501. + }
  90502. + return 0;
  90503. + }
  90504. +
  90505. + /*
  90506. + * For EP0 IN without premature status, zlp is required?
  90507. + */
  90508. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  90509. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  90510. + //_req->zero = 1;
  90511. + }
  90512. +
  90513. + /* Start the transfer */
  90514. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  90515. + /* EP0 Transfer? */
  90516. + if (ep->dwc_ep.num == 0) {
  90517. + switch (pcd->ep0state) {
  90518. + case EP0_IN_DATA_PHASE:
  90519. + DWC_DEBUGPL(DBG_PCD,
  90520. + "%s ep0: EP0_IN_DATA_PHASE\n",
  90521. + __func__);
  90522. + break;
  90523. +
  90524. + case EP0_OUT_DATA_PHASE:
  90525. + DWC_DEBUGPL(DBG_PCD,
  90526. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  90527. + __func__);
  90528. + if (pcd->request_config) {
  90529. + /* Complete STATUS PHASE */
  90530. + ep->dwc_ep.is_in = 1;
  90531. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  90532. + }
  90533. + break;
  90534. +
  90535. + case EP0_IN_STATUS_PHASE:
  90536. + DWC_DEBUGPL(DBG_PCD,
  90537. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  90538. + __func__);
  90539. + break;
  90540. +
  90541. + default:
  90542. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  90543. + pcd->ep0state);
  90544. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90545. + return -DWC_E_SHUTDOWN;
  90546. + }
  90547. +
  90548. + ep->dwc_ep.dma_addr = dma_buf;
  90549. + ep->dwc_ep.start_xfer_buff = buf;
  90550. + ep->dwc_ep.xfer_buff = buf;
  90551. + ep->dwc_ep.xfer_len = buflen;
  90552. + ep->dwc_ep.xfer_count = 0;
  90553. + ep->dwc_ep.sent_zlp = 0;
  90554. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  90555. +
  90556. + if (zero) {
  90557. + if ((ep->dwc_ep.xfer_len %
  90558. + ep->dwc_ep.maxpacket == 0)
  90559. + && (ep->dwc_ep.xfer_len != 0)) {
  90560. + ep->dwc_ep.sent_zlp = 1;
  90561. + }
  90562. +
  90563. + }
  90564. +
  90565. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  90566. + &ep->dwc_ep);
  90567. + } // non-ep0 endpoints
  90568. + else {
  90569. +#ifdef DWC_UTE_CFI
  90570. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  90571. + /* store the request length */
  90572. + ep->dwc_ep.cfi_req_len = buflen;
  90573. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  90574. + ep, req);
  90575. + } else {
  90576. +#endif
  90577. + max_transfer =
  90578. + GET_CORE_IF(ep->pcd)->core_params->
  90579. + max_transfer_size;
  90580. +
  90581. + /* Setup and start the Transfer */
  90582. + if (req->dw_align_buf){
  90583. + if (ep->dwc_ep.is_in)
  90584. + dwc_memcpy(req->dw_align_buf,
  90585. + buf, buflen);
  90586. + ep->dwc_ep.dma_addr =
  90587. + req->dw_align_buf_dma;
  90588. + ep->dwc_ep.start_xfer_buff =
  90589. + req->dw_align_buf;
  90590. + ep->dwc_ep.xfer_buff =
  90591. + req->dw_align_buf;
  90592. + } else {
  90593. + ep->dwc_ep.dma_addr = dma_buf;
  90594. + ep->dwc_ep.start_xfer_buff = buf;
  90595. + ep->dwc_ep.xfer_buff = buf;
  90596. + }
  90597. + ep->dwc_ep.xfer_len = 0;
  90598. + ep->dwc_ep.xfer_count = 0;
  90599. + ep->dwc_ep.sent_zlp = 0;
  90600. + ep->dwc_ep.total_len = buflen;
  90601. +
  90602. + ep->dwc_ep.maxxfer = max_transfer;
  90603. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  90604. + uint32_t out_max_xfer =
  90605. + DDMA_MAX_TRANSFER_SIZE -
  90606. + (DDMA_MAX_TRANSFER_SIZE % 4);
  90607. + if (ep->dwc_ep.is_in) {
  90608. + if (ep->dwc_ep.maxxfer >
  90609. + DDMA_MAX_TRANSFER_SIZE) {
  90610. + ep->dwc_ep.maxxfer =
  90611. + DDMA_MAX_TRANSFER_SIZE;
  90612. + }
  90613. + } else {
  90614. + if (ep->dwc_ep.maxxfer >
  90615. + out_max_xfer) {
  90616. + ep->dwc_ep.maxxfer =
  90617. + out_max_xfer;
  90618. + }
  90619. + }
  90620. + }
  90621. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  90622. + ep->dwc_ep.maxxfer -=
  90623. + (ep->dwc_ep.maxxfer %
  90624. + ep->dwc_ep.maxpacket);
  90625. + }
  90626. +
  90627. + if (zero) {
  90628. + if ((ep->dwc_ep.total_len %
  90629. + ep->dwc_ep.maxpacket == 0)
  90630. + && (ep->dwc_ep.total_len != 0)) {
  90631. + ep->dwc_ep.sent_zlp = 1;
  90632. + }
  90633. + }
  90634. +#ifdef DWC_UTE_CFI
  90635. + }
  90636. +#endif
  90637. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  90638. + &ep->dwc_ep);
  90639. + }
  90640. + }
  90641. +
  90642. + if (req != 0) {
  90643. + ++pcd->request_pending;
  90644. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  90645. + if (ep->dwc_ep.is_in && ep->stopped
  90646. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  90647. + /** @todo NGS Create a function for this. */
  90648. + diepmsk_data_t diepmsk = {.d32 = 0 };
  90649. + diepmsk.b.intktxfemp = 1;
  90650. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  90651. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  90652. + dev_if->dev_global_regs->diepeachintmsk
  90653. + [ep->dwc_ep.num], 0,
  90654. + diepmsk.d32);
  90655. + } else {
  90656. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  90657. + dev_if->dev_global_regs->
  90658. + diepmsk, 0, diepmsk.d32);
  90659. + }
  90660. +
  90661. + }
  90662. + }
  90663. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90664. +
  90665. + return 0;
  90666. +}
  90667. +
  90668. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  90669. + void *req_handle)
  90670. +{
  90671. + dwc_irqflags_t flags;
  90672. + dwc_otg_pcd_request_t *req;
  90673. + dwc_otg_pcd_ep_t *ep;
  90674. +
  90675. + ep = get_ep_from_handle(pcd, ep_handle);
  90676. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  90677. + DWC_WARN("bad argument\n");
  90678. + return -DWC_E_INVALID;
  90679. + }
  90680. +
  90681. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90682. +
  90683. + /* make sure it's actually queued on this endpoint */
  90684. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  90685. + if (req->priv == (void *)req_handle) {
  90686. + break;
  90687. + }
  90688. + }
  90689. +
  90690. + if (req->priv != (void *)req_handle) {
  90691. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90692. + return -DWC_E_INVALID;
  90693. + }
  90694. +
  90695. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  90696. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  90697. + } else {
  90698. + req = NULL;
  90699. + }
  90700. +
  90701. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90702. +
  90703. + return req ? 0 : -DWC_E_SHUTDOWN;
  90704. +
  90705. +}
  90706. +
  90707. +/**
  90708. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  90709. + *
  90710. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  90711. + * requests. If the gadget driver clears the halt status, it will
  90712. + * automatically unwedge the endpoint.
  90713. + *
  90714. + * Returns zero on success, else negative DWC error code.
  90715. + */
  90716. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  90717. +{
  90718. + dwc_otg_pcd_ep_t *ep;
  90719. + dwc_irqflags_t flags;
  90720. + int retval = 0;
  90721. +
  90722. + ep = get_ep_from_handle(pcd, ep_handle);
  90723. +
  90724. + if ((!ep->desc && ep != &pcd->ep0) ||
  90725. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  90726. + DWC_WARN("%s, bad ep\n", __func__);
  90727. + return -DWC_E_INVALID;
  90728. + }
  90729. +
  90730. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90731. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  90732. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  90733. + ep->dwc_ep.is_in ? "IN" : "OUT");
  90734. + retval = -DWC_E_AGAIN;
  90735. + } else {
  90736. + /* This code needs to be reviewed */
  90737. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  90738. + dtxfsts_data_t txstatus;
  90739. + fifosize_data_t txfifosize;
  90740. +
  90741. + txfifosize.d32 =
  90742. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  90743. + core_global_regs->dtxfsiz[ep->dwc_ep.
  90744. + tx_fifo_num]);
  90745. + txstatus.d32 =
  90746. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  90747. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  90748. + dtxfsts);
  90749. +
  90750. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  90751. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  90752. + retval = -DWC_E_AGAIN;
  90753. + } else {
  90754. + if (ep->dwc_ep.num == 0) {
  90755. + pcd->ep0state = EP0_STALL;
  90756. + }
  90757. +
  90758. + ep->stopped = 1;
  90759. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  90760. + &ep->dwc_ep);
  90761. + }
  90762. + } else {
  90763. + if (ep->dwc_ep.num == 0) {
  90764. + pcd->ep0state = EP0_STALL;
  90765. + }
  90766. +
  90767. + ep->stopped = 1;
  90768. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  90769. + }
  90770. + }
  90771. +
  90772. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90773. +
  90774. + return retval;
  90775. +}
  90776. +
  90777. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  90778. +{
  90779. + dwc_otg_pcd_ep_t *ep;
  90780. + dwc_irqflags_t flags;
  90781. + int retval = 0;
  90782. +
  90783. + ep = get_ep_from_handle(pcd, ep_handle);
  90784. +
  90785. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  90786. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  90787. + DWC_WARN("%s, bad ep\n", __func__);
  90788. + return -DWC_E_INVALID;
  90789. + }
  90790. +
  90791. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90792. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  90793. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  90794. + ep->dwc_ep.is_in ? "IN" : "OUT");
  90795. + retval = -DWC_E_AGAIN;
  90796. + } else if (value == 0) {
  90797. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  90798. + } else if (value == 1) {
  90799. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  90800. + dtxfsts_data_t txstatus;
  90801. + fifosize_data_t txfifosize;
  90802. +
  90803. + txfifosize.d32 =
  90804. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  90805. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  90806. + txstatus.d32 =
  90807. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  90808. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  90809. +
  90810. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  90811. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  90812. + retval = -DWC_E_AGAIN;
  90813. + } else {
  90814. + if (ep->dwc_ep.num == 0) {
  90815. + pcd->ep0state = EP0_STALL;
  90816. + }
  90817. +
  90818. + ep->stopped = 1;
  90819. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  90820. + &ep->dwc_ep);
  90821. + }
  90822. + } else {
  90823. + if (ep->dwc_ep.num == 0) {
  90824. + pcd->ep0state = EP0_STALL;
  90825. + }
  90826. +
  90827. + ep->stopped = 1;
  90828. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  90829. + }
  90830. + } else if (value == 2) {
  90831. + ep->dwc_ep.stall_clear_flag = 0;
  90832. + } else if (value == 3) {
  90833. + ep->dwc_ep.stall_clear_flag = 1;
  90834. + }
  90835. +
  90836. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90837. +
  90838. + return retval;
  90839. +}
  90840. +
  90841. +/**
  90842. + * This function initiates remote wakeup of the host from suspend state.
  90843. + */
  90844. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  90845. +{
  90846. + dctl_data_t dctl = { 0 };
  90847. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  90848. + dsts_data_t dsts;
  90849. +
  90850. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  90851. + if (!dsts.b.suspsts) {
  90852. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  90853. + }
  90854. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  90855. + if (pcd->remote_wakeup_enable) {
  90856. + if (set) {
  90857. +
  90858. + if (core_if->adp_enable) {
  90859. + gpwrdn_data_t gpwrdn;
  90860. +
  90861. + dwc_otg_adp_probe_stop(core_if);
  90862. +
  90863. + /* Mask SRP detected interrupt from Power Down Logic */
  90864. + gpwrdn.d32 = 0;
  90865. + gpwrdn.b.srp_det_msk = 1;
  90866. + DWC_MODIFY_REG32(&core_if->
  90867. + core_global_regs->gpwrdn,
  90868. + gpwrdn.d32, 0);
  90869. +
  90870. + /* Disable Power Down Logic */
  90871. + gpwrdn.d32 = 0;
  90872. + gpwrdn.b.pmuactv = 1;
  90873. + DWC_MODIFY_REG32(&core_if->
  90874. + core_global_regs->gpwrdn,
  90875. + gpwrdn.d32, 0);
  90876. +
  90877. + /*
  90878. + * Initialize the Core for Device mode.
  90879. + */
  90880. + core_if->op_state = B_PERIPHERAL;
  90881. + dwc_otg_core_init(core_if);
  90882. + dwc_otg_enable_global_interrupts(core_if);
  90883. + cil_pcd_start(core_if);
  90884. +
  90885. + dwc_otg_initiate_srp(core_if);
  90886. + }
  90887. +
  90888. + dctl.b.rmtwkupsig = 1;
  90889. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  90890. + dctl, 0, dctl.d32);
  90891. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  90892. +
  90893. + dwc_mdelay(2);
  90894. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  90895. + dctl, dctl.d32, 0);
  90896. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  90897. + }
  90898. + } else {
  90899. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  90900. + }
  90901. +}
  90902. +
  90903. +#ifdef CONFIG_USB_DWC_OTG_LPM
  90904. +/**
  90905. + * This function initiates remote wakeup of the host from L1 sleep state.
  90906. + */
  90907. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  90908. +{
  90909. + glpmcfg_data_t lpmcfg;
  90910. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  90911. +
  90912. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  90913. +
  90914. + /* Check if we are in L1 state */
  90915. + if (!lpmcfg.b.prt_sleep_sts) {
  90916. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  90917. + return;
  90918. + }
  90919. +
  90920. + /* Check if host allows remote wakeup */
  90921. + if (!lpmcfg.b.rem_wkup_en) {
  90922. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  90923. + return;
  90924. + }
  90925. +
  90926. + /* Check if Resume OK */
  90927. + if (!lpmcfg.b.sleep_state_resumeok) {
  90928. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  90929. + return;
  90930. + }
  90931. +
  90932. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  90933. + lpmcfg.b.en_utmi_sleep = 0;
  90934. + lpmcfg.b.hird_thres &= (~(1 << 4));
  90935. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  90936. +
  90937. + if (set) {
  90938. + dctl_data_t dctl = {.d32 = 0 };
  90939. + dctl.b.rmtwkupsig = 1;
  90940. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  90941. + * Hardware will automatically clear this bit.
  90942. + */
  90943. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  90944. + 0, dctl.d32);
  90945. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  90946. + }
  90947. +
  90948. +}
  90949. +#endif
  90950. +
  90951. +/**
  90952. + * Performs remote wakeup.
  90953. + */
  90954. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  90955. +{
  90956. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  90957. + dwc_irqflags_t flags;
  90958. + if (dwc_otg_is_device_mode(core_if)) {
  90959. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  90960. +#ifdef CONFIG_USB_DWC_OTG_LPM
  90961. + if (core_if->lx_state == DWC_OTG_L1) {
  90962. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  90963. + } else {
  90964. +#endif
  90965. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  90966. +#ifdef CONFIG_USB_DWC_OTG_LPM
  90967. + }
  90968. +#endif
  90969. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  90970. + }
  90971. + return;
  90972. +}
  90973. +
  90974. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  90975. +{
  90976. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  90977. + dctl_data_t dctl = { 0 };
  90978. +
  90979. + if (dwc_otg_is_device_mode(core_if)) {
  90980. + dctl.b.sftdiscon = 1;
  90981. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  90982. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  90983. + dwc_udelay(no_of_usecs);
  90984. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  90985. +
  90986. + } else{
  90987. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  90988. + }
  90989. + return;
  90990. +
  90991. +}
  90992. +
  90993. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  90994. +{
  90995. + dsts_data_t dsts;
  90996. + gotgctl_data_t gotgctl;
  90997. +
  90998. + /*
  90999. + * This function starts the Protocol if no session is in progress. If
  91000. + * a session is already in progress, but the device is suspended,
  91001. + * remote wakeup signaling is started.
  91002. + */
  91003. +
  91004. + /* Check if valid session */
  91005. + gotgctl.d32 =
  91006. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  91007. + if (gotgctl.b.bsesvld) {
  91008. + /* Check if suspend state */
  91009. + dsts.d32 =
  91010. + DWC_READ_REG32(&
  91011. + (GET_CORE_IF(pcd)->dev_if->
  91012. + dev_global_regs->dsts));
  91013. + if (dsts.b.suspsts) {
  91014. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  91015. + }
  91016. + } else {
  91017. + dwc_otg_pcd_initiate_srp(pcd);
  91018. + }
  91019. +
  91020. + return 0;
  91021. +
  91022. +}
  91023. +
  91024. +/**
  91025. + * Start the SRP timer to detect when the SRP does not complete within
  91026. + * 6 seconds.
  91027. + *
  91028. + * @param pcd the pcd structure.
  91029. + */
  91030. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  91031. +{
  91032. + dwc_irqflags_t flags;
  91033. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  91034. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  91035. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  91036. +}
  91037. +
  91038. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  91039. +{
  91040. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  91041. +}
  91042. +
  91043. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  91044. +{
  91045. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  91046. +}
  91047. +
  91048. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  91049. +{
  91050. + return pcd->b_hnp_enable;
  91051. +}
  91052. +
  91053. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  91054. +{
  91055. + return pcd->a_hnp_support;
  91056. +}
  91057. +
  91058. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  91059. +{
  91060. + return pcd->a_alt_hnp_support;
  91061. +}
  91062. +
  91063. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  91064. +{
  91065. + return pcd->remote_wakeup_enable;
  91066. +}
  91067. +
  91068. +#endif /* DWC_HOST_ONLY */
  91069. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  91070. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 1970-01-01 01:00:00.000000000 +0100
  91071. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd.h 2014-04-13 17:33:11.000000000 +0200
  91072. @@ -0,0 +1,266 @@
  91073. +/* ==========================================================================
  91074. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  91075. + * $Revision: #48 $
  91076. + * $Date: 2012/08/10 $
  91077. + * $Change: 2047372 $
  91078. + *
  91079. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  91080. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  91081. + * otherwise expressly agreed to in writing between Synopsys and you.
  91082. + *
  91083. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  91084. + * any End User Software License Agreement or Agreement for Licensed Product
  91085. + * with Synopsys or any supplement thereto. You are permitted to use and
  91086. + * redistribute this Software in source and binary forms, with or without
  91087. + * modification, provided that redistributions of source code must retain this
  91088. + * notice. You may not view, use, disclose, copy or distribute this file or
  91089. + * any information contained herein except pursuant to this license grant from
  91090. + * Synopsys. If you do not agree with this notice, including the disclaimer
  91091. + * below, then you are not authorized to use the Software.
  91092. + *
  91093. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  91094. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  91095. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  91096. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  91097. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  91098. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  91099. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  91100. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  91101. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  91102. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  91103. + * DAMAGE.
  91104. + * ========================================================================== */
  91105. +#ifndef DWC_HOST_ONLY
  91106. +#if !defined(__DWC_PCD_H__)
  91107. +#define __DWC_PCD_H__
  91108. +
  91109. +#include "dwc_otg_os_dep.h"
  91110. +#include "usb.h"
  91111. +#include "dwc_otg_cil.h"
  91112. +#include "dwc_otg_pcd_if.h"
  91113. +struct cfiobject;
  91114. +
  91115. +/**
  91116. + * @file
  91117. + *
  91118. + * This file contains the structures, constants, and interfaces for
  91119. + * the Perpherial Contoller Driver (PCD).
  91120. + *
  91121. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  91122. + * Gadget API, so that the existing Gadget drivers can be used. For
  91123. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  91124. + * (FBS) driver will be used. The FBS driver supports the
  91125. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  91126. + * transports.
  91127. + *
  91128. + */
  91129. +
  91130. +/** Invalid DMA Address */
  91131. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  91132. +
  91133. +/** Max Transfer size for any EP */
  91134. +#define DDMA_MAX_TRANSFER_SIZE 65535
  91135. +
  91136. +/**
  91137. + * Get the pointer to the core_if from the pcd pointer.
  91138. + */
  91139. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  91140. +
  91141. +/**
  91142. + * States of EP0.
  91143. + */
  91144. +typedef enum ep0_state {
  91145. + EP0_DISCONNECT, /* no host */
  91146. + EP0_IDLE,
  91147. + EP0_IN_DATA_PHASE,
  91148. + EP0_OUT_DATA_PHASE,
  91149. + EP0_IN_STATUS_PHASE,
  91150. + EP0_OUT_STATUS_PHASE,
  91151. + EP0_STALL,
  91152. +} ep0state_e;
  91153. +
  91154. +/** Fordward declaration.*/
  91155. +struct dwc_otg_pcd;
  91156. +
  91157. +/** DWC_otg iso request structure.
  91158. + *
  91159. + */
  91160. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  91161. +
  91162. +#ifdef DWC_UTE_PER_IO
  91163. +
  91164. +/**
  91165. + * This shall be the exact analogy of the same type structure defined in the
  91166. + * usb_gadget.h. Each descriptor contains
  91167. + */
  91168. +struct dwc_iso_pkt_desc_port {
  91169. + uint32_t offset;
  91170. + uint32_t length; /* expected length */
  91171. + uint32_t actual_length;
  91172. + uint32_t status;
  91173. +};
  91174. +
  91175. +struct dwc_iso_xreq_port {
  91176. + /** transfer/submission flag */
  91177. + uint32_t tr_sub_flags;
  91178. + /** Start the request ASAP */
  91179. +#define DWC_EREQ_TF_ASAP 0x00000002
  91180. + /** Just enqueue the request w/o initiating a transfer */
  91181. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  91182. +
  91183. + /**
  91184. + * count of ISO packets attached to this request - shall
  91185. + * not exceed the pio_alloc_pkt_count
  91186. + */
  91187. + uint32_t pio_pkt_count;
  91188. + /** count of ISO packets allocated for this request */
  91189. + uint32_t pio_alloc_pkt_count;
  91190. + /** number of ISO packet errors */
  91191. + uint32_t error_count;
  91192. + /** reserved for future extension */
  91193. + uint32_t res;
  91194. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  91195. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  91196. +};
  91197. +#endif
  91198. +/** DWC_otg request structure.
  91199. + * This structure is a list of requests.
  91200. + */
  91201. +typedef struct dwc_otg_pcd_request {
  91202. + void *priv;
  91203. + void *buf;
  91204. + dwc_dma_t dma;
  91205. + uint32_t length;
  91206. + uint32_t actual;
  91207. + unsigned sent_zlp:1;
  91208. + /**
  91209. + * Used instead of original buffer if
  91210. + * it(physical address) is not dword-aligned.
  91211. + **/
  91212. + uint8_t *dw_align_buf;
  91213. + dwc_dma_t dw_align_buf_dma;
  91214. +
  91215. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  91216. +#ifdef DWC_UTE_PER_IO
  91217. + struct dwc_iso_xreq_port ext_req;
  91218. + //void *priv_ereq_nport; /* */
  91219. +#endif
  91220. +} dwc_otg_pcd_request_t;
  91221. +
  91222. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  91223. +
  91224. +/** PCD EP structure.
  91225. + * This structure describes an EP, there is an array of EPs in the PCD
  91226. + * structure.
  91227. + */
  91228. +typedef struct dwc_otg_pcd_ep {
  91229. + /** USB EP Descriptor */
  91230. + const usb_endpoint_descriptor_t *desc;
  91231. +
  91232. + /** queue of dwc_otg_pcd_requests. */
  91233. + struct req_list queue;
  91234. + unsigned stopped:1;
  91235. + unsigned disabling:1;
  91236. + unsigned dma:1;
  91237. + unsigned queue_sof:1;
  91238. +
  91239. +#ifdef DWC_EN_ISOC
  91240. + /** ISOC req handle passed */
  91241. + void *iso_req_handle;
  91242. +#endif //_EN_ISOC_
  91243. +
  91244. + /** DWC_otg ep data. */
  91245. + dwc_ep_t dwc_ep;
  91246. +
  91247. + /** Pointer to PCD */
  91248. + struct dwc_otg_pcd *pcd;
  91249. +
  91250. + void *priv;
  91251. +} dwc_otg_pcd_ep_t;
  91252. +
  91253. +/** DWC_otg PCD Structure.
  91254. + * This structure encapsulates the data for the dwc_otg PCD.
  91255. + */
  91256. +struct dwc_otg_pcd {
  91257. + const struct dwc_otg_pcd_function_ops *fops;
  91258. + /** The DWC otg device pointer */
  91259. + struct dwc_otg_device *otg_dev;
  91260. + /** Core Interface */
  91261. + dwc_otg_core_if_t *core_if;
  91262. + /** State of EP0 */
  91263. + ep0state_e ep0state;
  91264. + /** EP0 Request is pending */
  91265. + unsigned ep0_pending:1;
  91266. + /** Indicates when SET CONFIGURATION Request is in process */
  91267. + unsigned request_config:1;
  91268. + /** The state of the Remote Wakeup Enable. */
  91269. + unsigned remote_wakeup_enable:1;
  91270. + /** The state of the B-Device HNP Enable. */
  91271. + unsigned b_hnp_enable:1;
  91272. + /** The state of A-Device HNP Support. */
  91273. + unsigned a_hnp_support:1;
  91274. + /** The state of the A-Device Alt HNP support. */
  91275. + unsigned a_alt_hnp_support:1;
  91276. + /** Count of pending Requests */
  91277. + unsigned request_pending;
  91278. +
  91279. + /** SETUP packet for EP0
  91280. + * This structure is allocated as a DMA buffer on PCD initialization
  91281. + * with enough space for up to 3 setup packets.
  91282. + */
  91283. + union {
  91284. + usb_device_request_t req;
  91285. + uint32_t d32[2];
  91286. + } *setup_pkt;
  91287. +
  91288. + dwc_dma_t setup_pkt_dma_handle;
  91289. +
  91290. + /* Additional buffer and flag for CTRL_WR premature case */
  91291. + uint8_t *backup_buf;
  91292. + unsigned data_terminated;
  91293. +
  91294. + /** 2-byte dma buffer used to return status from GET_STATUS */
  91295. + uint16_t *status_buf;
  91296. + dwc_dma_t status_buf_dma_handle;
  91297. +
  91298. + /** EP0 */
  91299. + dwc_otg_pcd_ep_t ep0;
  91300. +
  91301. + /** Array of IN EPs. */
  91302. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  91303. + /** Array of OUT EPs. */
  91304. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  91305. + /** number of valid EPs in the above array. */
  91306. +// unsigned num_eps : 4;
  91307. + dwc_spinlock_t *lock;
  91308. +
  91309. + /** Tasklet to defer starting of TEST mode transmissions until
  91310. + * Status Phase has been completed.
  91311. + */
  91312. + dwc_tasklet_t *test_mode_tasklet;
  91313. +
  91314. + /** Tasklet to delay starting of xfer in DMA mode */
  91315. + dwc_tasklet_t *start_xfer_tasklet;
  91316. +
  91317. + /** The test mode to enter when the tasklet is executed. */
  91318. + unsigned test_mode;
  91319. + /** The cfi_api structure that implements most of the CFI API
  91320. + * and OTG specific core configuration functionality
  91321. + */
  91322. +#ifdef DWC_UTE_CFI
  91323. + struct cfiobject *cfi;
  91324. +#endif
  91325. +
  91326. +};
  91327. +
  91328. +//FIXME this functions should be static, and this prototypes should be removed
  91329. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  91330. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  91331. + dwc_otg_pcd_request_t * req, int32_t status);
  91332. +
  91333. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  91334. + void *req_handle);
  91335. +
  91336. +extern void do_test_mode(void *data);
  91337. +#endif
  91338. +#endif /* DWC_HOST_ONLY */
  91339. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  91340. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 1970-01-01 01:00:00.000000000 +0100
  91341. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h 2014-04-13 17:33:11.000000000 +0200
  91342. @@ -0,0 +1,360 @@
  91343. +/* ==========================================================================
  91344. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  91345. + * $Revision: #11 $
  91346. + * $Date: 2011/10/26 $
  91347. + * $Change: 1873028 $
  91348. + *
  91349. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  91350. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  91351. + * otherwise expressly agreed to in writing between Synopsys and you.
  91352. + *
  91353. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  91354. + * any End User Software License Agreement or Agreement for Licensed Product
  91355. + * with Synopsys or any supplement thereto. You are permitted to use and
  91356. + * redistribute this Software in source and binary forms, with or without
  91357. + * modification, provided that redistributions of source code must retain this
  91358. + * notice. You may not view, use, disclose, copy or distribute this file or
  91359. + * any information contained herein except pursuant to this license grant from
  91360. + * Synopsys. If you do not agree with this notice, including the disclaimer
  91361. + * below, then you are not authorized to use the Software.
  91362. + *
  91363. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  91364. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  91365. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  91366. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  91367. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  91368. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  91369. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  91370. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  91371. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  91372. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  91373. + * DAMAGE.
  91374. + * ========================================================================== */
  91375. +#ifndef DWC_HOST_ONLY
  91376. +
  91377. +#if !defined(__DWC_PCD_IF_H__)
  91378. +#define __DWC_PCD_IF_H__
  91379. +
  91380. +//#include "dwc_os.h"
  91381. +#include "dwc_otg_core_if.h"
  91382. +
  91383. +/** @file
  91384. + * This file defines DWC_OTG PCD Core API.
  91385. + */
  91386. +
  91387. +struct dwc_otg_pcd;
  91388. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  91389. +
  91390. +/** Maxpacket size for EP0 */
  91391. +#define MAX_EP0_SIZE 64
  91392. +/** Maxpacket size for any EP */
  91393. +#define MAX_PACKET_SIZE 1024
  91394. +
  91395. +/** @name Function Driver Callbacks */
  91396. +/** @{ */
  91397. +
  91398. +/** This function will be called whenever a previously queued request has
  91399. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  91400. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  91401. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  91402. + * parameters. */
  91403. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  91404. + void *req_handle, int32_t status,
  91405. + uint32_t actual);
  91406. +/**
  91407. + * This function will be called whenever a previousle queued ISOC request has
  91408. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  91409. + * function.
  91410. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  91411. + * functions.
  91412. + */
  91413. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  91414. + void *req_handle, int proc_buf_num);
  91415. +/** This function should handle any SETUP request that cannot be handled by the
  91416. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  91417. + * class-specific requests, etc. The function must non-blocking.
  91418. + *
  91419. + * Returns 0 on success.
  91420. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  91421. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  91422. + * Returns -DWC_E_SHUTDOWN on any other error. */
  91423. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  91424. +/** This is called whenever the device has been disconnected. The function
  91425. + * driver should take appropriate action to clean up all pending requests in the
  91426. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  91427. + * state. */
  91428. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  91429. +/** This function is called when device has been connected. */
  91430. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  91431. +/** This function is called when device has been suspended */
  91432. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  91433. +/** This function is called when device has received LPM tokens, i.e.
  91434. + * device has been sent to sleep state. */
  91435. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  91436. +/** This function is called when device has been resumed
  91437. + * from suspend(L2) or L1 sleep state. */
  91438. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  91439. +/** This function is called whenever hnp params has been changed.
  91440. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  91441. + * to get hnp parameters. */
  91442. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  91443. +/** This function is called whenever USB RESET is detected. */
  91444. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  91445. +
  91446. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  91447. +
  91448. +/**
  91449. + *
  91450. + * @param ep_handle Void pointer to the usb_ep structure
  91451. + * @param ereq_port Pointer to the extended request structure created in the
  91452. + * portable part.
  91453. + */
  91454. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  91455. + void *req_handle, int32_t status,
  91456. + void *ereq_port);
  91457. +/** Function Driver Ops Data Structure */
  91458. +struct dwc_otg_pcd_function_ops {
  91459. + dwc_connect_cb_t connect;
  91460. + dwc_disconnect_cb_t disconnect;
  91461. + dwc_setup_cb_t setup;
  91462. + dwc_completion_cb_t complete;
  91463. + dwc_isoc_completion_cb_t isoc_complete;
  91464. + dwc_suspend_cb_t suspend;
  91465. + dwc_sleep_cb_t sleep;
  91466. + dwc_resume_cb_t resume;
  91467. + dwc_reset_cb_t reset;
  91468. + dwc_hnp_params_changed_cb_t hnp_changed;
  91469. + cfi_setup_cb_t cfi_setup;
  91470. +#ifdef DWC_UTE_PER_IO
  91471. + xiso_completion_cb_t xisoc_complete;
  91472. +#endif
  91473. +};
  91474. +/** @} */
  91475. +
  91476. +/** @name Function Driver Functions */
  91477. +/** @{ */
  91478. +
  91479. +/** Call this function to get pointer on dwc_otg_pcd_t,
  91480. + * this pointer will be used for all PCD API functions.
  91481. + *
  91482. + * @param core_if The DWC_OTG Core
  91483. + */
  91484. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_core_if_t * core_if);
  91485. +
  91486. +/** Frees PCD allocated by dwc_otg_pcd_init
  91487. + *
  91488. + * @param pcd The PCD
  91489. + */
  91490. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  91491. +
  91492. +/** Call this to bind the function driver to the PCD Core.
  91493. + *
  91494. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  91495. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  91496. + */
  91497. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  91498. + const struct dwc_otg_pcd_function_ops *fops);
  91499. +
  91500. +/** Enables an endpoint for use. This function enables an endpoint in
  91501. + * the PCD. The endpoint is described by the ep_desc which has the
  91502. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  91503. + * to the endpoint from other API functions and in callbacks. Normally this
  91504. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  91505. + * core for that interface.
  91506. + *
  91507. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  91508. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  91509. + * Returns 0 on success.
  91510. + *
  91511. + * @param pcd The PCD
  91512. + * @param ep_desc Endpoint descriptor
  91513. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  91514. + */
  91515. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  91516. + const uint8_t * ep_desc, void *usb_ep);
  91517. +
  91518. +/** Disable the endpoint referenced by ep_handle.
  91519. + *
  91520. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  91521. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  91522. + * Returns 0 on success. */
  91523. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  91524. +
  91525. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  91526. + * After the transfer is completes, the complete callback will be called with
  91527. + * the request status.
  91528. + *
  91529. + * @param pcd The PCD
  91530. + * @param ep_handle The handle of the endpoint
  91531. + * @param buf The buffer for the data
  91532. + * @param dma_buf The DMA buffer for the data
  91533. + * @param buflen The length of the data transfer
  91534. + * @param zero Specifies whether to send zero length last packet.
  91535. + * @param req_handle Set this handle to any value to use to reference this
  91536. + * request in the ep_dequeue function or from the complete callback
  91537. + * @param atomic_alloc If driver need to perform atomic allocations
  91538. + * for internal data structures.
  91539. + *
  91540. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  91541. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  91542. + * Returns 0 on success. */
  91543. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  91544. + uint8_t * buf, dwc_dma_t dma_buf,
  91545. + uint32_t buflen, int zero, void *req_handle,
  91546. + int atomic_alloc);
  91547. +#ifdef DWC_UTE_PER_IO
  91548. +/**
  91549. + *
  91550. + * @param ereq_nonport Pointer to the extended request part of the
  91551. + * usb_request structure defined in usb_gadget.h file.
  91552. + */
  91553. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  91554. + uint8_t * buf, dwc_dma_t dma_buf,
  91555. + uint32_t buflen, int zero,
  91556. + void *req_handle, int atomic_alloc,
  91557. + void *ereq_nonport);
  91558. +
  91559. +#endif
  91560. +
  91561. +/** De-queue the specified data transfer that has not yet completed.
  91562. + *
  91563. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  91564. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  91565. + * Returns 0 on success. */
  91566. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  91567. + void *req_handle);
  91568. +
  91569. +/** Halt (STALL) an endpoint or clear it.
  91570. + *
  91571. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  91572. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  91573. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  91574. + * Returns 0 on success. */
  91575. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  91576. +
  91577. +/** This function */
  91578. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  91579. +
  91580. +/** This function should be called on every hardware interrupt */
  91581. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  91582. +
  91583. +/** This function returns current frame number */
  91584. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  91585. +
  91586. +/**
  91587. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  91588. + * For isochronous transfers duble buffering is used.
  91589. + * After processing each of buffers comlete callback will be called with
  91590. + * status for each transaction.
  91591. + *
  91592. + * @param pcd The PCD
  91593. + * @param ep_handle The handle of the endpoint
  91594. + * @param buf0 The virtual address of first data buffer
  91595. + * @param buf1 The virtual address of second data buffer
  91596. + * @param dma0 The DMA address of first data buffer
  91597. + * @param dma1 The DMA address of second data buffer
  91598. + * @param sync_frame Data pattern frame number
  91599. + * @param dp_frame Data size for pattern frame
  91600. + * @param data_per_frame Data size for regular frame
  91601. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  91602. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  91603. + * @param req_handle Handle of ISOC request
  91604. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  91605. + * internal data structures.
  91606. + *
  91607. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  91608. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  91609. + * Returns -DW_E_SHUTDOWN for any other error.
  91610. + * Returns 0 on success
  91611. + */
  91612. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  91613. + uint8_t * buf0, uint8_t * buf1,
  91614. + dwc_dma_t dma0, dwc_dma_t dma1,
  91615. + int sync_frame, int dp_frame,
  91616. + int data_per_frame, int start_frame,
  91617. + int buf_proc_intrvl, void *req_handle,
  91618. + int atomic_alloc);
  91619. +
  91620. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  91621. + *
  91622. + * @param pcd The PCD
  91623. + * @param ep_handle The handle of the endpoint
  91624. + * @param req_handle Handle of ISOC request
  91625. + *
  91626. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  91627. + * Returns 0 on success
  91628. + */
  91629. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  91630. + void *req_handle);
  91631. +
  91632. +/** Get ISOC packet status.
  91633. + *
  91634. + * @param pcd The PCD
  91635. + * @param ep_handle The handle of the endpoint
  91636. + * @param iso_req_handle Isochronoush request handle
  91637. + * @param packet Number of packet
  91638. + * @param status Out parameter for returning status
  91639. + * @param actual Out parameter for returning actual length
  91640. + * @param offset Out parameter for returning offset
  91641. + *
  91642. + */
  91643. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  91644. + void *ep_handle,
  91645. + void *iso_req_handle, int packet,
  91646. + int *status, int *actual,
  91647. + int *offset);
  91648. +
  91649. +/** Get ISOC packet count.
  91650. + *
  91651. + * @param pcd The PCD
  91652. + * @param ep_handle The handle of the endpoint
  91653. + * @param iso_req_handle
  91654. + */
  91655. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  91656. + void *ep_handle,
  91657. + void *iso_req_handle);
  91658. +
  91659. +/** This function starts the SRP Protocol if no session is in progress. If
  91660. + * a session is already in progress, but the device is suspended,
  91661. + * remote wakeup signaling is started.
  91662. + */
  91663. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  91664. +
  91665. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  91666. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  91667. +
  91668. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  91669. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  91670. +
  91671. +/** Initiate SRP */
  91672. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  91673. +
  91674. +/** Starts remote wakeup signaling. */
  91675. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  91676. +
  91677. +/** Starts micorsecond soft disconnect. */
  91678. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  91679. +/** This function returns whether device is dualspeed.*/
  91680. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  91681. +
  91682. +/** This function returns whether device is otg. */
  91683. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  91684. +
  91685. +/** These functions allow to get hnp parameters */
  91686. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  91687. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  91688. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  91689. +
  91690. +/** CFI specific Interface functions */
  91691. +/** Allocate a cfi buffer */
  91692. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  91693. + dwc_dma_t * addr, size_t buflen,
  91694. + int flags);
  91695. +
  91696. +/******************************************************************************/
  91697. +
  91698. +/** @} */
  91699. +
  91700. +#endif /* __DWC_PCD_IF_H__ */
  91701. +
  91702. +#endif /* DWC_HOST_ONLY */
  91703. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  91704. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 1970-01-01 01:00:00.000000000 +0100
  91705. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c 2014-09-14 19:04:13.000000000 +0200
  91706. @@ -0,0 +1,5147 @@
  91707. +/* ==========================================================================
  91708. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  91709. + * $Revision: #116 $
  91710. + * $Date: 2012/08/10 $
  91711. + * $Change: 2047372 $
  91712. + *
  91713. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  91714. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  91715. + * otherwise expressly agreed to in writing between Synopsys and you.
  91716. + *
  91717. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  91718. + * any End User Software License Agreement or Agreement for Licensed Product
  91719. + * with Synopsys or any supplement thereto. You are permitted to use and
  91720. + * redistribute this Software in source and binary forms, with or without
  91721. + * modification, provided that redistributions of source code must retain this
  91722. + * notice. You may not view, use, disclose, copy or distribute this file or
  91723. + * any information contained herein except pursuant to this license grant from
  91724. + * Synopsys. If you do not agree with this notice, including the disclaimer
  91725. + * below, then you are not authorized to use the Software.
  91726. + *
  91727. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  91728. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  91729. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  91730. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  91731. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  91732. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  91733. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  91734. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  91735. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  91736. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  91737. + * DAMAGE.
  91738. + * ========================================================================== */
  91739. +#ifndef DWC_HOST_ONLY
  91740. +
  91741. +#include "dwc_otg_pcd.h"
  91742. +
  91743. +#ifdef DWC_UTE_CFI
  91744. +#include "dwc_otg_cfi.h"
  91745. +#endif
  91746. +
  91747. +#ifdef DWC_UTE_PER_IO
  91748. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  91749. +#endif
  91750. +//#define PRINT_CFI_DMA_DESCS
  91751. +
  91752. +#define DEBUG_EP0
  91753. +
  91754. +/**
  91755. + * This function updates OTG.
  91756. + */
  91757. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  91758. +{
  91759. +
  91760. + if (reset) {
  91761. + pcd->b_hnp_enable = 0;
  91762. + pcd->a_hnp_support = 0;
  91763. + pcd->a_alt_hnp_support = 0;
  91764. + }
  91765. +
  91766. + if (pcd->fops->hnp_changed) {
  91767. + pcd->fops->hnp_changed(pcd);
  91768. + }
  91769. +}
  91770. +
  91771. +/** @file
  91772. + * This file contains the implementation of the PCD Interrupt handlers.
  91773. + *
  91774. + * The PCD handles the device interrupts. Many conditions can cause a
  91775. + * device interrupt. When an interrupt occurs, the device interrupt
  91776. + * service routine determines the cause of the interrupt and
  91777. + * dispatches handling to the appropriate function. These interrupt
  91778. + * handling functions are described below.
  91779. + * All interrupt registers are processed from LSB to MSB.
  91780. + */
  91781. +
  91782. +/**
  91783. + * This function prints the ep0 state for debug purposes.
  91784. + */
  91785. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  91786. +{
  91787. +#ifdef DEBUG
  91788. + char str[40];
  91789. +
  91790. + switch (pcd->ep0state) {
  91791. + case EP0_DISCONNECT:
  91792. + dwc_strcpy(str, "EP0_DISCONNECT");
  91793. + break;
  91794. + case EP0_IDLE:
  91795. + dwc_strcpy(str, "EP0_IDLE");
  91796. + break;
  91797. + case EP0_IN_DATA_PHASE:
  91798. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  91799. + break;
  91800. + case EP0_OUT_DATA_PHASE:
  91801. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  91802. + break;
  91803. + case EP0_IN_STATUS_PHASE:
  91804. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  91805. + break;
  91806. + case EP0_OUT_STATUS_PHASE:
  91807. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  91808. + break;
  91809. + case EP0_STALL:
  91810. + dwc_strcpy(str, "EP0_STALL");
  91811. + break;
  91812. + default:
  91813. + dwc_strcpy(str, "EP0_INVALID");
  91814. + }
  91815. +
  91816. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  91817. +#endif
  91818. +}
  91819. +
  91820. +/**
  91821. + * This function calculate the size of the payload in the memory
  91822. + * for out endpoints and prints size for debug purposes(used in
  91823. + * 2.93a DevOutNak feature).
  91824. + */
  91825. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  91826. +{
  91827. +#ifdef DEBUG
  91828. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  91829. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  91830. + int pack_num;
  91831. + unsigned payload;
  91832. +
  91833. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  91834. + deptsiz_updt.d32 =
  91835. + DWC_READ_REG32(&pcd->core_if->dev_if->
  91836. + out_ep_regs[ep->num]->doeptsiz);
  91837. + /* Payload will be */
  91838. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  91839. + /* Packet count is decremented every time a packet
  91840. + * is written to the RxFIFO not in to the external memory
  91841. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  91842. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  91843. + DWC_DEBUGPL(DBG_PCDV,
  91844. + "Payload for EP%d-%s\n",
  91845. + ep->num, (ep->is_in ? "IN" : "OUT"));
  91846. + DWC_DEBUGPL(DBG_PCDV,
  91847. + "Number of transfered bytes = 0x%08x\n", payload);
  91848. + DWC_DEBUGPL(DBG_PCDV,
  91849. + "Number of transfered packets = %d\n", pack_num);
  91850. +#endif
  91851. +}
  91852. +
  91853. +
  91854. +#ifdef DWC_UTE_CFI
  91855. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  91856. + const uint8_t * epname, int descnum)
  91857. +{
  91858. + CFI_INFO
  91859. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  91860. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  91861. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  91862. + ddesc->status.b.bs);
  91863. +}
  91864. +#endif
  91865. +
  91866. +/**
  91867. + * This function returns pointer to in ep struct with number ep_num
  91868. + */
  91869. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  91870. +{
  91871. + int i;
  91872. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  91873. + if (ep_num == 0) {
  91874. + return &pcd->ep0;
  91875. + } else {
  91876. + for (i = 0; i < num_in_eps; ++i) {
  91877. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  91878. + return &pcd->in_ep[i];
  91879. + }
  91880. + return 0;
  91881. + }
  91882. +}
  91883. +
  91884. +/**
  91885. + * This function returns pointer to out ep struct with number ep_num
  91886. + */
  91887. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  91888. +{
  91889. + int i;
  91890. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  91891. + if (ep_num == 0) {
  91892. + return &pcd->ep0;
  91893. + } else {
  91894. + for (i = 0; i < num_out_eps; ++i) {
  91895. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  91896. + return &pcd->out_ep[i];
  91897. + }
  91898. + return 0;
  91899. + }
  91900. +}
  91901. +
  91902. +/**
  91903. + * This functions gets a pointer to an EP from the wIndex address
  91904. + * value of the control request.
  91905. + */
  91906. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  91907. +{
  91908. + dwc_otg_pcd_ep_t *ep;
  91909. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  91910. +
  91911. + if (ep_num == 0) {
  91912. + ep = &pcd->ep0;
  91913. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  91914. + ep = &pcd->in_ep[ep_num - 1];
  91915. + } else {
  91916. + ep = &pcd->out_ep[ep_num - 1];
  91917. + }
  91918. +
  91919. + return ep;
  91920. +}
  91921. +
  91922. +/**
  91923. + * This function checks the EP request queue, if the queue is not
  91924. + * empty the next request is started.
  91925. + */
  91926. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  91927. +{
  91928. + dwc_otg_pcd_request_t *req = 0;
  91929. + uint32_t max_transfer =
  91930. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  91931. +
  91932. +#ifdef DWC_UTE_CFI
  91933. + struct dwc_otg_pcd *pcd;
  91934. + pcd = ep->pcd;
  91935. +#endif
  91936. +
  91937. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  91938. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  91939. +
  91940. +#ifdef DWC_UTE_CFI
  91941. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  91942. + ep->dwc_ep.cfi_req_len = req->length;
  91943. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  91944. + } else {
  91945. +#endif
  91946. + /* Setup and start the Transfer */
  91947. + if (req->dw_align_buf) {
  91948. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  91949. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  91950. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  91951. + } else {
  91952. + ep->dwc_ep.dma_addr = req->dma;
  91953. + ep->dwc_ep.start_xfer_buff = req->buf;
  91954. + ep->dwc_ep.xfer_buff = req->buf;
  91955. + }
  91956. + ep->dwc_ep.sent_zlp = 0;
  91957. + ep->dwc_ep.total_len = req->length;
  91958. + ep->dwc_ep.xfer_len = 0;
  91959. + ep->dwc_ep.xfer_count = 0;
  91960. +
  91961. + ep->dwc_ep.maxxfer = max_transfer;
  91962. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  91963. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  91964. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  91965. + if (ep->dwc_ep.is_in) {
  91966. + if (ep->dwc_ep.maxxfer >
  91967. + DDMA_MAX_TRANSFER_SIZE) {
  91968. + ep->dwc_ep.maxxfer =
  91969. + DDMA_MAX_TRANSFER_SIZE;
  91970. + }
  91971. + } else {
  91972. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  91973. + ep->dwc_ep.maxxfer =
  91974. + out_max_xfer;
  91975. + }
  91976. + }
  91977. + }
  91978. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  91979. + ep->dwc_ep.maxxfer -=
  91980. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  91981. + }
  91982. + if (req->sent_zlp) {
  91983. + if ((ep->dwc_ep.total_len %
  91984. + ep->dwc_ep.maxpacket == 0)
  91985. + && (ep->dwc_ep.total_len != 0)) {
  91986. + ep->dwc_ep.sent_zlp = 1;
  91987. + }
  91988. +
  91989. + }
  91990. +#ifdef DWC_UTE_CFI
  91991. + }
  91992. +#endif
  91993. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  91994. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  91995. + DWC_PRINTF("There are no more ISOC requests \n");
  91996. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  91997. + }
  91998. +}
  91999. +
  92000. +/**
  92001. + * This function handles the SOF Interrupts. At this time the SOF
  92002. + * Interrupt is disabled.
  92003. + */
  92004. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  92005. +{
  92006. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92007. +
  92008. + gintsts_data_t gintsts;
  92009. +
  92010. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  92011. +
  92012. + /* Clear interrupt */
  92013. + gintsts.d32 = 0;
  92014. + gintsts.b.sofintr = 1;
  92015. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  92016. +
  92017. + return 1;
  92018. +}
  92019. +
  92020. +/**
  92021. + * This function handles the Rx Status Queue Level Interrupt, which
  92022. + * indicates that there is a least one packet in the Rx FIFO. The
  92023. + * packets are moved from the FIFO to memory, where they will be
  92024. + * processed when the Endpoint Interrupt Register indicates Transfer
  92025. + * Complete or SETUP Phase Done.
  92026. + *
  92027. + * Repeat the following until the Rx Status Queue is empty:
  92028. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  92029. + * info
  92030. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  92031. + * and exit
  92032. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  92033. + * SETUP data to the buffer
  92034. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  92035. + * to the destination buffer
  92036. + */
  92037. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  92038. +{
  92039. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92040. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  92041. + gintmsk_data_t gintmask = {.d32 = 0 };
  92042. + device_grxsts_data_t status;
  92043. + dwc_otg_pcd_ep_t *ep;
  92044. + gintsts_data_t gintsts;
  92045. +#ifdef DEBUG
  92046. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  92047. +#endif
  92048. +
  92049. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  92050. + /* Disable the Rx Status Queue Level interrupt */
  92051. + gintmask.b.rxstsqlvl = 1;
  92052. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  92053. +
  92054. + /* Get the Status from the top of the FIFO */
  92055. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  92056. +
  92057. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  92058. + "pktsts:%x Frame:%d(0x%0x)\n",
  92059. + status.b.epnum, status.b.bcnt,
  92060. + dpid_str[status.b.dpid],
  92061. + status.b.pktsts, status.b.fn, status.b.fn);
  92062. + /* Get pointer to EP structure */
  92063. + ep = get_out_ep(pcd, status.b.epnum);
  92064. +
  92065. + switch (status.b.pktsts) {
  92066. + case DWC_DSTS_GOUT_NAK:
  92067. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  92068. + break;
  92069. + case DWC_STS_DATA_UPDT:
  92070. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  92071. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  92072. + /** @todo NGS Check for buffer overflow? */
  92073. + dwc_otg_read_packet(core_if,
  92074. + ep->dwc_ep.xfer_buff,
  92075. + status.b.bcnt);
  92076. + ep->dwc_ep.xfer_count += status.b.bcnt;
  92077. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  92078. + }
  92079. + break;
  92080. + case DWC_STS_XFER_COMP:
  92081. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  92082. + break;
  92083. + case DWC_DSTS_SETUP_COMP:
  92084. +#ifdef DEBUG_EP0
  92085. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  92086. +#endif
  92087. + break;
  92088. + case DWC_DSTS_SETUP_UPDT:
  92089. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  92090. +#ifdef DEBUG_EP0
  92091. + DWC_DEBUGPL(DBG_PCD,
  92092. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  92093. + pcd->setup_pkt->req.bmRequestType,
  92094. + pcd->setup_pkt->req.bRequest,
  92095. + UGETW(pcd->setup_pkt->req.wValue),
  92096. + UGETW(pcd->setup_pkt->req.wIndex),
  92097. + UGETW(pcd->setup_pkt->req.wLength));
  92098. +#endif
  92099. + ep->dwc_ep.xfer_count += status.b.bcnt;
  92100. + break;
  92101. + default:
  92102. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  92103. + status.b.pktsts);
  92104. + break;
  92105. + }
  92106. +
  92107. + /* Enable the Rx Status Queue Level interrupt */
  92108. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  92109. + /* Clear interrupt */
  92110. + gintsts.d32 = 0;
  92111. + gintsts.b.rxstsqlvl = 1;
  92112. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  92113. +
  92114. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  92115. + return 1;
  92116. +}
  92117. +
  92118. +/**
  92119. + * This function examines the Device IN Token Learning Queue to
  92120. + * determine the EP number of the last IN token received. This
  92121. + * implementation is for the Mass Storage device where there are only
  92122. + * 2 IN EPs (Control-IN and BULK-IN).
  92123. + *
  92124. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  92125. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  92126. + *
  92127. + * @param core_if Programming view of DWC_otg controller.
  92128. + *
  92129. + */
  92130. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  92131. +{
  92132. + dwc_otg_device_global_regs_t *dev_global_regs =
  92133. + core_if->dev_if->dev_global_regs;
  92134. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  92135. + /* Number of Token Queue Registers */
  92136. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  92137. + dtknq1_data_t dtknqr1;
  92138. + uint32_t in_tkn_epnums[4];
  92139. + int ndx = 0;
  92140. + int i = 0;
  92141. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  92142. + int epnum = 0;
  92143. +
  92144. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  92145. +
  92146. + /* Read the DTKNQ Registers */
  92147. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  92148. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  92149. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  92150. + in_tkn_epnums[i]);
  92151. + if (addr == &dev_global_regs->dvbusdis) {
  92152. + addr = &dev_global_regs->dtknqr3_dthrctl;
  92153. + } else {
  92154. + ++addr;
  92155. + }
  92156. +
  92157. + }
  92158. +
  92159. + /* Copy the DTKNQR1 data to the bit field. */
  92160. + dtknqr1.d32 = in_tkn_epnums[0];
  92161. + /* Get the EP numbers */
  92162. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  92163. + ndx = dtknqr1.b.intknwptr - 1;
  92164. +
  92165. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  92166. + if (ndx == -1) {
  92167. + /** @todo Find a simpler way to calculate the max
  92168. + * queue position.*/
  92169. + int cnt = TOKEN_Q_DEPTH;
  92170. + if (TOKEN_Q_DEPTH <= 6) {
  92171. + cnt = TOKEN_Q_DEPTH - 1;
  92172. + } else if (TOKEN_Q_DEPTH <= 14) {
  92173. + cnt = TOKEN_Q_DEPTH - 7;
  92174. + } else if (TOKEN_Q_DEPTH <= 22) {
  92175. + cnt = TOKEN_Q_DEPTH - 15;
  92176. + } else {
  92177. + cnt = TOKEN_Q_DEPTH - 23;
  92178. + }
  92179. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  92180. + } else {
  92181. + if (ndx <= 5) {
  92182. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  92183. + } else if (ndx <= 13) {
  92184. + ndx -= 6;
  92185. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  92186. + } else if (ndx <= 21) {
  92187. + ndx -= 14;
  92188. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  92189. + } else if (ndx <= 29) {
  92190. + ndx -= 22;
  92191. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  92192. + }
  92193. + }
  92194. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  92195. + return epnum;
  92196. +}
  92197. +
  92198. +/**
  92199. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  92200. + * The active request is checked for the next packet to be loaded into
  92201. + * the non-periodic Tx FIFO.
  92202. + */
  92203. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  92204. +{
  92205. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92206. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  92207. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  92208. + gnptxsts_data_t txstatus = {.d32 = 0 };
  92209. + gintsts_data_t gintsts;
  92210. +
  92211. + int epnum = 0;
  92212. + dwc_otg_pcd_ep_t *ep = 0;
  92213. + uint32_t len = 0;
  92214. + int dwords;
  92215. +
  92216. + /* Get the epnum from the IN Token Learning Queue. */
  92217. + epnum = get_ep_of_last_in_token(core_if);
  92218. + ep = get_in_ep(pcd, epnum);
  92219. +
  92220. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  92221. +
  92222. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  92223. +
  92224. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  92225. + if (len > ep->dwc_ep.maxpacket) {
  92226. + len = ep->dwc_ep.maxpacket;
  92227. + }
  92228. + dwords = (len + 3) / 4;
  92229. +
  92230. + /* While there is space in the queue and space in the FIFO and
  92231. + * More data to tranfer, Write packets to the Tx FIFO */
  92232. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  92233. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  92234. +
  92235. + while (txstatus.b.nptxqspcavail > 0 &&
  92236. + txstatus.b.nptxfspcavail > dwords &&
  92237. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  92238. + /* Write the FIFO */
  92239. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  92240. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  92241. +
  92242. + if (len > ep->dwc_ep.maxpacket) {
  92243. + len = ep->dwc_ep.maxpacket;
  92244. + }
  92245. +
  92246. + dwords = (len + 3) / 4;
  92247. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  92248. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  92249. + }
  92250. +
  92251. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  92252. + DWC_READ_REG32(&global_regs->gnptxsts));
  92253. +
  92254. + /* Clear interrupt */
  92255. + gintsts.d32 = 0;
  92256. + gintsts.b.nptxfempty = 1;
  92257. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  92258. +
  92259. + return 1;
  92260. +}
  92261. +
  92262. +/**
  92263. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  92264. + * The active request is checked for the next packet to be loaded into
  92265. + * apropriate Tx FIFO.
  92266. + */
  92267. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  92268. +{
  92269. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92270. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  92271. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  92272. + dtxfsts_data_t txstatus = {.d32 = 0 };
  92273. + dwc_otg_pcd_ep_t *ep = 0;
  92274. + uint32_t len = 0;
  92275. + int dwords;
  92276. +
  92277. + ep = get_in_ep(pcd, epnum);
  92278. +
  92279. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  92280. +
  92281. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  92282. +
  92283. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  92284. +
  92285. + if (len > ep->dwc_ep.maxpacket) {
  92286. + len = ep->dwc_ep.maxpacket;
  92287. + }
  92288. +
  92289. + dwords = (len + 3) / 4;
  92290. +
  92291. + /* While there is space in the queue and space in the FIFO and
  92292. + * More data to tranfer, Write packets to the Tx FIFO */
  92293. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  92294. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  92295. +
  92296. + while (txstatus.b.txfspcavail > dwords &&
  92297. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  92298. + ep->dwc_ep.xfer_len != 0) {
  92299. + /* Write the FIFO */
  92300. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  92301. +
  92302. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  92303. + if (len > ep->dwc_ep.maxpacket) {
  92304. + len = ep->dwc_ep.maxpacket;
  92305. + }
  92306. +
  92307. + dwords = (len + 3) / 4;
  92308. + txstatus.d32 =
  92309. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  92310. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  92311. + txstatus.d32);
  92312. + }
  92313. +
  92314. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  92315. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  92316. +
  92317. + return 1;
  92318. +}
  92319. +
  92320. +/**
  92321. + * This function is called when the Device is disconnected. It stops
  92322. + * any active requests and informs the Gadget driver of the
  92323. + * disconnect.
  92324. + */
  92325. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  92326. +{
  92327. + int i, num_in_eps, num_out_eps;
  92328. + dwc_otg_pcd_ep_t *ep;
  92329. +
  92330. + gintmsk_data_t intr_mask = {.d32 = 0 };
  92331. +
  92332. + DWC_SPINLOCK(pcd->lock);
  92333. +
  92334. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  92335. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  92336. +
  92337. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  92338. + /* don't disconnect drivers more than once */
  92339. + if (pcd->ep0state == EP0_DISCONNECT) {
  92340. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  92341. + DWC_SPINUNLOCK(pcd->lock);
  92342. + return;
  92343. + }
  92344. + pcd->ep0state = EP0_DISCONNECT;
  92345. +
  92346. + /* Reset the OTG state. */
  92347. + dwc_otg_pcd_update_otg(pcd, 1);
  92348. +
  92349. + /* Disable the NP Tx Fifo Empty Interrupt. */
  92350. + intr_mask.b.nptxfempty = 1;
  92351. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  92352. + intr_mask.d32, 0);
  92353. +
  92354. + /* Flush the FIFOs */
  92355. + /**@todo NGS Flush Periodic FIFOs */
  92356. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  92357. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  92358. +
  92359. + /* prevent new request submissions, kill any outstanding requests */
  92360. + ep = &pcd->ep0;
  92361. + dwc_otg_request_nuke(ep);
  92362. + /* prevent new request submissions, kill any outstanding requests */
  92363. + for (i = 0; i < num_in_eps; i++) {
  92364. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  92365. + dwc_otg_request_nuke(ep);
  92366. + }
  92367. + /* prevent new request submissions, kill any outstanding requests */
  92368. + for (i = 0; i < num_out_eps; i++) {
  92369. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  92370. + dwc_otg_request_nuke(ep);
  92371. + }
  92372. +
  92373. + /* report disconnect; the driver is already quiesced */
  92374. + if (pcd->fops->disconnect) {
  92375. + DWC_SPINUNLOCK(pcd->lock);
  92376. + pcd->fops->disconnect(pcd);
  92377. + DWC_SPINLOCK(pcd->lock);
  92378. + }
  92379. + DWC_SPINUNLOCK(pcd->lock);
  92380. +}
  92381. +
  92382. +/**
  92383. + * This interrupt indicates that ...
  92384. + */
  92385. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  92386. +{
  92387. + gintmsk_data_t intr_mask = {.d32 = 0 };
  92388. + gintsts_data_t gintsts;
  92389. +
  92390. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  92391. + intr_mask.b.i2cintr = 1;
  92392. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  92393. + intr_mask.d32, 0);
  92394. +
  92395. + /* Clear interrupt */
  92396. + gintsts.d32 = 0;
  92397. + gintsts.b.i2cintr = 1;
  92398. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  92399. + gintsts.d32);
  92400. + return 1;
  92401. +}
  92402. +
  92403. +/**
  92404. + * This interrupt indicates that ...
  92405. + */
  92406. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  92407. +{
  92408. + gintsts_data_t gintsts;
  92409. +#if defined(VERBOSE)
  92410. + DWC_PRINTF("Early Suspend Detected\n");
  92411. +#endif
  92412. +
  92413. + /* Clear interrupt */
  92414. + gintsts.d32 = 0;
  92415. + gintsts.b.erlysuspend = 1;
  92416. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  92417. + gintsts.d32);
  92418. + return 1;
  92419. +}
  92420. +
  92421. +/**
  92422. + * This function configures EPO to receive SETUP packets.
  92423. + *
  92424. + * @todo NGS: Update the comments from the HW FS.
  92425. + *
  92426. + * -# Program the following fields in the endpoint specific registers
  92427. + * for Control OUT EP 0, in order to receive a setup packet
  92428. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  92429. + * setup packets)
  92430. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  92431. + * to back setup packets)
  92432. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  92433. + * store any setup packets received
  92434. + *
  92435. + * @param core_if Programming view of DWC_otg controller.
  92436. + * @param pcd Programming view of the PCD.
  92437. + */
  92438. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  92439. + dwc_otg_pcd_t * pcd)
  92440. +{
  92441. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  92442. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  92443. + dwc_otg_dev_dma_desc_t *dma_desc;
  92444. + depctl_data_t doepctl = {.d32 = 0 };
  92445. +
  92446. +#ifdef VERBOSE
  92447. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  92448. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  92449. +#endif
  92450. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  92451. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  92452. + if (doepctl.b.epena) {
  92453. + return;
  92454. + }
  92455. + }
  92456. +
  92457. + doeptsize0.b.supcnt = 3;
  92458. + doeptsize0.b.pktcnt = 1;
  92459. + doeptsize0.b.xfersize = 8 * 3;
  92460. +
  92461. + if (core_if->dma_enable) {
  92462. + if (!core_if->dma_desc_enable) {
  92463. + /** put here as for Hermes mode deptisz register should not be written */
  92464. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  92465. + doeptsize0.d32);
  92466. +
  92467. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  92468. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  92469. + pcd->setup_pkt_dma_handle);
  92470. + } else {
  92471. + dev_if->setup_desc_index =
  92472. + (dev_if->setup_desc_index + 1) & 1;
  92473. + dma_desc =
  92474. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  92475. +
  92476. + /** DMA Descriptor Setup */
  92477. + dma_desc->status.b.bs = BS_HOST_BUSY;
  92478. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  92479. + dma_desc->status.b.sr = 0;
  92480. + dma_desc->status.b.mtrf = 0;
  92481. + }
  92482. + dma_desc->status.b.l = 1;
  92483. + dma_desc->status.b.ioc = 1;
  92484. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  92485. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  92486. + dma_desc->status.b.sts = 0;
  92487. + dma_desc->status.b.bs = BS_HOST_READY;
  92488. +
  92489. + /** DOEPDMA0 Register write */
  92490. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  92491. + dev_if->dma_setup_desc_addr
  92492. + [dev_if->setup_desc_index]);
  92493. + }
  92494. +
  92495. + } else {
  92496. + /** put here as for Hermes mode deptisz register should not be written */
  92497. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  92498. + doeptsize0.d32);
  92499. + }
  92500. +
  92501. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  92502. + doepctl.d32 = 0;
  92503. + doepctl.b.epena = 1;
  92504. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  92505. + doepctl.b.cnak = 1;
  92506. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  92507. + } else {
  92508. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  92509. + }
  92510. +
  92511. +#ifdef VERBOSE
  92512. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  92513. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  92514. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  92515. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  92516. +#endif
  92517. +}
  92518. +
  92519. +/**
  92520. + * This interrupt occurs when a USB Reset is detected. When the USB
  92521. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  92522. + * EP0 state is set to IDLE.
  92523. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  92524. + * -# Unmask the following interrupt bits
  92525. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  92526. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  92527. + * - DOEPMSK.SETUP = 1
  92528. + * - DOEPMSK.XferCompl = 1
  92529. + * - DIEPMSK.XferCompl = 1
  92530. + * - DIEPMSK.TimeOut = 1
  92531. + * -# Program the following fields in the endpoint specific registers
  92532. + * for Control OUT EP 0, in order to receive a setup packet
  92533. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  92534. + * setup packets)
  92535. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  92536. + * to back setup packets)
  92537. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  92538. + * store any setup packets received
  92539. + * At this point, all the required initialization, except for enabling
  92540. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  92541. + */
  92542. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  92543. +{
  92544. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92545. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  92546. + depctl_data_t doepctl = {.d32 = 0 };
  92547. + depctl_data_t diepctl = {.d32 = 0 };
  92548. + daint_data_t daintmsk = {.d32 = 0 };
  92549. + doepmsk_data_t doepmsk = {.d32 = 0 };
  92550. + diepmsk_data_t diepmsk = {.d32 = 0 };
  92551. + dcfg_data_t dcfg = {.d32 = 0 };
  92552. + grstctl_t resetctl = {.d32 = 0 };
  92553. + dctl_data_t dctl = {.d32 = 0 };
  92554. + int i = 0;
  92555. + gintsts_data_t gintsts;
  92556. + pcgcctl_data_t power = {.d32 = 0 };
  92557. +
  92558. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  92559. + if (power.b.stoppclk) {
  92560. + power.d32 = 0;
  92561. + power.b.stoppclk = 1;
  92562. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  92563. +
  92564. + power.b.pwrclmp = 1;
  92565. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  92566. +
  92567. + power.b.rstpdwnmodule = 1;
  92568. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  92569. + }
  92570. +
  92571. + core_if->lx_state = DWC_OTG_L0;
  92572. +
  92573. + DWC_PRINTF("USB RESET\n");
  92574. +#ifdef DWC_EN_ISOC
  92575. + for (i = 1; i < 16; ++i) {
  92576. + dwc_otg_pcd_ep_t *ep;
  92577. + dwc_ep_t *dwc_ep;
  92578. + ep = get_in_ep(pcd, i);
  92579. + if (ep != 0) {
  92580. + dwc_ep = &ep->dwc_ep;
  92581. + dwc_ep->next_frame = 0xffffffff;
  92582. + }
  92583. + }
  92584. +#endif /* DWC_EN_ISOC */
  92585. +
  92586. + /* reset the HNP settings */
  92587. + dwc_otg_pcd_update_otg(pcd, 1);
  92588. +
  92589. + /* Clear the Remote Wakeup Signalling */
  92590. + dctl.b.rmtwkupsig = 1;
  92591. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  92592. +
  92593. + /* Set NAK for all OUT EPs */
  92594. + doepctl.b.snak = 1;
  92595. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  92596. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  92597. + }
  92598. +
  92599. + /* Flush the NP Tx FIFO */
  92600. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  92601. + /* Flush the Learning Queue */
  92602. + resetctl.b.intknqflsh = 1;
  92603. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  92604. +
  92605. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  92606. + core_if->start_predict = 0;
  92607. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  92608. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  92609. + }
  92610. + core_if->nextep_seq[0] = 0;
  92611. + core_if->first_in_nextep_seq = 0;
  92612. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  92613. + diepctl.b.nextep = 0;
  92614. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  92615. +
  92616. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  92617. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  92618. + dcfg.b.epmscnt = 2;
  92619. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  92620. +
  92621. + DWC_DEBUGPL(DBG_PCDV,
  92622. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  92623. + __func__, core_if->first_in_nextep_seq);
  92624. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  92625. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  92626. + }
  92627. + }
  92628. +
  92629. + if (core_if->multiproc_int_enable) {
  92630. + daintmsk.b.inep0 = 1;
  92631. + daintmsk.b.outep0 = 1;
  92632. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  92633. + daintmsk.d32);
  92634. +
  92635. + doepmsk.b.setup = 1;
  92636. + doepmsk.b.xfercompl = 1;
  92637. + doepmsk.b.ahberr = 1;
  92638. + doepmsk.b.epdisabled = 1;
  92639. +
  92640. + if ((core_if->dma_desc_enable) ||
  92641. + (core_if->dma_enable
  92642. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  92643. + doepmsk.b.stsphsercvd = 1;
  92644. + }
  92645. + if (core_if->dma_desc_enable)
  92646. + doepmsk.b.bna = 1;
  92647. +/*
  92648. + doepmsk.b.babble = 1;
  92649. + doepmsk.b.nyet = 1;
  92650. +
  92651. + if (core_if->dma_enable) {
  92652. + doepmsk.b.nak = 1;
  92653. + }
  92654. +*/
  92655. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  92656. + doepmsk.d32);
  92657. +
  92658. + diepmsk.b.xfercompl = 1;
  92659. + diepmsk.b.timeout = 1;
  92660. + diepmsk.b.epdisabled = 1;
  92661. + diepmsk.b.ahberr = 1;
  92662. + diepmsk.b.intknepmis = 1;
  92663. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  92664. + diepmsk.b.intknepmis = 0;
  92665. +
  92666. +/* if (core_if->dma_desc_enable) {
  92667. + diepmsk.b.bna = 1;
  92668. + }
  92669. +*/
  92670. +/*
  92671. + if (core_if->dma_enable) {
  92672. + diepmsk.b.nak = 1;
  92673. + }
  92674. +*/
  92675. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  92676. + diepmsk.d32);
  92677. + } else {
  92678. + daintmsk.b.inep0 = 1;
  92679. + daintmsk.b.outep0 = 1;
  92680. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  92681. + daintmsk.d32);
  92682. +
  92683. + doepmsk.b.setup = 1;
  92684. + doepmsk.b.xfercompl = 1;
  92685. + doepmsk.b.ahberr = 1;
  92686. + doepmsk.b.epdisabled = 1;
  92687. +
  92688. + if ((core_if->dma_desc_enable) ||
  92689. + (core_if->dma_enable
  92690. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  92691. + doepmsk.b.stsphsercvd = 1;
  92692. + }
  92693. + if (core_if->dma_desc_enable)
  92694. + doepmsk.b.bna = 1;
  92695. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  92696. +
  92697. + diepmsk.b.xfercompl = 1;
  92698. + diepmsk.b.timeout = 1;
  92699. + diepmsk.b.epdisabled = 1;
  92700. + diepmsk.b.ahberr = 1;
  92701. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  92702. + diepmsk.b.intknepmis = 0;
  92703. +/*
  92704. + if (core_if->dma_desc_enable) {
  92705. + diepmsk.b.bna = 1;
  92706. + }
  92707. +*/
  92708. +
  92709. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  92710. + }
  92711. +
  92712. + /* Reset Device Address */
  92713. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  92714. + dcfg.b.devaddr = 0;
  92715. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  92716. +
  92717. + /* setup EP0 to receive SETUP packets */
  92718. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  92719. + ep0_out_start(core_if, pcd);
  92720. +
  92721. + /* Clear interrupt */
  92722. + gintsts.d32 = 0;
  92723. + gintsts.b.usbreset = 1;
  92724. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  92725. +
  92726. + return 1;
  92727. +}
  92728. +
  92729. +/**
  92730. + * Get the device speed from the device status register and convert it
  92731. + * to USB speed constant.
  92732. + *
  92733. + * @param core_if Programming view of DWC_otg controller.
  92734. + */
  92735. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  92736. +{
  92737. + dsts_data_t dsts;
  92738. + int speed = 0;
  92739. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  92740. +
  92741. + switch (dsts.b.enumspd) {
  92742. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  92743. + speed = USB_SPEED_HIGH;
  92744. + break;
  92745. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  92746. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  92747. + speed = USB_SPEED_FULL;
  92748. + break;
  92749. +
  92750. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  92751. + speed = USB_SPEED_LOW;
  92752. + break;
  92753. + }
  92754. +
  92755. + return speed;
  92756. +}
  92757. +
  92758. +/**
  92759. + * Read the device status register and set the device speed in the
  92760. + * data structure.
  92761. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  92762. + */
  92763. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  92764. +{
  92765. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  92766. + gintsts_data_t gintsts;
  92767. + gusbcfg_data_t gusbcfg;
  92768. + dwc_otg_core_global_regs_t *global_regs =
  92769. + GET_CORE_IF(pcd)->core_global_regs;
  92770. + uint8_t utmi16b, utmi8b;
  92771. + int speed;
  92772. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  92773. +
  92774. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  92775. + utmi16b = 6; //vahrama old value was 6;
  92776. + utmi8b = 9;
  92777. + } else {
  92778. + utmi16b = 4;
  92779. + utmi8b = 8;
  92780. + }
  92781. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  92782. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  92783. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  92784. + }
  92785. +
  92786. +#ifdef DEBUG_EP0
  92787. + print_ep0_state(pcd);
  92788. +#endif
  92789. +
  92790. + if (pcd->ep0state == EP0_DISCONNECT) {
  92791. + pcd->ep0state = EP0_IDLE;
  92792. + } else if (pcd->ep0state == EP0_STALL) {
  92793. + pcd->ep0state = EP0_IDLE;
  92794. + }
  92795. +
  92796. + pcd->ep0state = EP0_IDLE;
  92797. +
  92798. + ep0->stopped = 0;
  92799. +
  92800. + speed = get_device_speed(GET_CORE_IF(pcd));
  92801. + pcd->fops->connect(pcd, speed);
  92802. +
  92803. + /* Set USB turnaround time based on device speed and PHY interface. */
  92804. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  92805. + if (speed == USB_SPEED_HIGH) {
  92806. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  92807. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  92808. + /* ULPI interface */
  92809. + gusbcfg.b.usbtrdtim = 9;
  92810. + }
  92811. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  92812. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  92813. + /* UTMI+ interface */
  92814. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  92815. + gusbcfg.b.usbtrdtim = utmi8b;
  92816. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  92817. + b.utmi_phy_data_width == 1) {
  92818. + gusbcfg.b.usbtrdtim = utmi16b;
  92819. + } else if (GET_CORE_IF(pcd)->
  92820. + core_params->phy_utmi_width == 8) {
  92821. + gusbcfg.b.usbtrdtim = utmi8b;
  92822. + } else {
  92823. + gusbcfg.b.usbtrdtim = utmi16b;
  92824. + }
  92825. + }
  92826. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  92827. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  92828. + /* UTMI+ OR ULPI interface */
  92829. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  92830. + /* ULPI interface */
  92831. + gusbcfg.b.usbtrdtim = 9;
  92832. + } else {
  92833. + /* UTMI+ interface */
  92834. + if (GET_CORE_IF(pcd)->
  92835. + core_params->phy_utmi_width == 16) {
  92836. + gusbcfg.b.usbtrdtim = utmi16b;
  92837. + } else {
  92838. + gusbcfg.b.usbtrdtim = utmi8b;
  92839. + }
  92840. + }
  92841. + }
  92842. + } else {
  92843. + /* Full or low speed */
  92844. + gusbcfg.b.usbtrdtim = 9;
  92845. + }
  92846. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  92847. +
  92848. + /* Clear interrupt */
  92849. + gintsts.d32 = 0;
  92850. + gintsts.b.enumdone = 1;
  92851. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  92852. + gintsts.d32);
  92853. + return 1;
  92854. +}
  92855. +
  92856. +/**
  92857. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  92858. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  92859. + * read all the data from the Rx FIFO.
  92860. + */
  92861. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  92862. +{
  92863. + gintmsk_data_t intr_mask = {.d32 = 0 };
  92864. + gintsts_data_t gintsts;
  92865. +
  92866. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  92867. + "ISOC Out Dropped");
  92868. +
  92869. + intr_mask.b.isooutdrop = 1;
  92870. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  92871. + intr_mask.d32, 0);
  92872. +
  92873. + /* Clear interrupt */
  92874. + gintsts.d32 = 0;
  92875. + gintsts.b.isooutdrop = 1;
  92876. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  92877. + gintsts.d32);
  92878. +
  92879. + return 1;
  92880. +}
  92881. +
  92882. +/**
  92883. + * This interrupt indicates the end of the portion of the micro-frame
  92884. + * for periodic transactions. If there is a periodic transaction for
  92885. + * the next frame, load the packets into the EP periodic Tx FIFO.
  92886. + */
  92887. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  92888. +{
  92889. + gintmsk_data_t intr_mask = {.d32 = 0 };
  92890. + gintsts_data_t gintsts;
  92891. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  92892. +
  92893. + intr_mask.b.eopframe = 1;
  92894. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  92895. + intr_mask.d32, 0);
  92896. +
  92897. + /* Clear interrupt */
  92898. + gintsts.d32 = 0;
  92899. + gintsts.b.eopframe = 1;
  92900. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  92901. + gintsts.d32);
  92902. +
  92903. + return 1;
  92904. +}
  92905. +
  92906. +/**
  92907. + * This interrupt indicates that EP of the packet on the top of the
  92908. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  92909. + *
  92910. + * The "Device IN Token Queue" Registers are read to determine the
  92911. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  92912. + * is flushed, so it can be reloaded in the order seen in the IN Token
  92913. + * Queue.
  92914. + */
  92915. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  92916. +{
  92917. + gintsts_data_t gintsts;
  92918. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92919. + dctl_data_t dctl;
  92920. + gintmsk_data_t intr_mask = {.d32 = 0 };
  92921. +
  92922. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  92923. + core_if->start_predict = 1;
  92924. +
  92925. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  92926. +
  92927. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  92928. + if (!gintsts.b.ginnakeff) {
  92929. + /* Disable EP Mismatch interrupt */
  92930. + intr_mask.d32 = 0;
  92931. + intr_mask.b.epmismatch = 1;
  92932. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  92933. + /* Enable the Global IN NAK Effective Interrupt */
  92934. + intr_mask.d32 = 0;
  92935. + intr_mask.b.ginnakeff = 1;
  92936. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  92937. + /* Set the global non-periodic IN NAK handshake */
  92938. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  92939. + dctl.b.sgnpinnak = 1;
  92940. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  92941. + } else {
  92942. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  92943. + }
  92944. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  92945. + * handler after Global IN NAK Effective interrupt will be asserted */
  92946. + }
  92947. + /* Clear interrupt */
  92948. + gintsts.d32 = 0;
  92949. + gintsts.b.epmismatch = 1;
  92950. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  92951. +
  92952. + return 1;
  92953. +}
  92954. +
  92955. +/**
  92956. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  92957. + * core has stopped fetching data for IN endpoints due to the unavailability of
  92958. + * TxFIFO space or Request Queue space. This interrupt is used by the
  92959. + * application for an endpoint mismatch algorithm.
  92960. + *
  92961. + * @param pcd The PCD
  92962. + */
  92963. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  92964. +{
  92965. + gintsts_data_t gintsts;
  92966. + gintmsk_data_t gintmsk_data;
  92967. + dctl_data_t dctl;
  92968. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  92969. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  92970. +
  92971. + /* Clear the global non-periodic IN NAK handshake */
  92972. + dctl.d32 = 0;
  92973. + dctl.b.cgnpinnak = 1;
  92974. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  92975. +
  92976. + /* Mask GINTSTS.FETSUSP interrupt */
  92977. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  92978. + gintmsk_data.b.fetsusp = 0;
  92979. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  92980. +
  92981. + /* Clear interrupt */
  92982. + gintsts.d32 = 0;
  92983. + gintsts.b.fetsusp = 1;
  92984. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  92985. +
  92986. + return 1;
  92987. +}
  92988. +/**
  92989. + * This funcion stalls EP0.
  92990. + */
  92991. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  92992. +{
  92993. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  92994. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  92995. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  92996. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  92997. +
  92998. + ep0->dwc_ep.is_in = 1;
  92999. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  93000. + pcd->ep0.stopped = 1;
  93001. + pcd->ep0state = EP0_IDLE;
  93002. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  93003. +}
  93004. +
  93005. +/**
  93006. + * This functions delegates the setup command to the gadget driver.
  93007. + */
  93008. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  93009. + usb_device_request_t * ctrl)
  93010. +{
  93011. + int ret = 0;
  93012. + DWC_SPINUNLOCK(pcd->lock);
  93013. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  93014. + DWC_SPINLOCK(pcd->lock);
  93015. + if (ret < 0) {
  93016. + ep0_do_stall(pcd, ret);
  93017. + }
  93018. +
  93019. + /** @todo This is a g_file_storage gadget driver specific
  93020. + * workaround: a DELAYED_STATUS result from the fsg_setup
  93021. + * routine will result in the gadget queueing a EP0 IN status
  93022. + * phase for a two-stage control transfer. Exactly the same as
  93023. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  93024. + * specific request. Need a generic way to know when the gadget
  93025. + * driver will queue the status phase. Can we assume when we
  93026. + * call the gadget driver setup() function that it will always
  93027. + * queue and require the following flag? Need to look into
  93028. + * this.
  93029. + */
  93030. +
  93031. + if (ret == 256 + 999) {
  93032. + pcd->request_config = 1;
  93033. + }
  93034. +}
  93035. +
  93036. +#ifdef DWC_UTE_CFI
  93037. +/**
  93038. + * This functions delegates the CFI setup commands to the gadget driver.
  93039. + * This function will return a negative value to indicate a failure.
  93040. + */
  93041. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  93042. + struct cfi_usb_ctrlrequest *ctrl_req)
  93043. +{
  93044. + int ret = 0;
  93045. +
  93046. + if (pcd->fops && pcd->fops->cfi_setup) {
  93047. + DWC_SPINUNLOCK(pcd->lock);
  93048. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  93049. + DWC_SPINLOCK(pcd->lock);
  93050. + if (ret < 0) {
  93051. + ep0_do_stall(pcd, ret);
  93052. + return ret;
  93053. + }
  93054. + }
  93055. +
  93056. + return ret;
  93057. +}
  93058. +#endif
  93059. +
  93060. +/**
  93061. + * This function starts the Zero-Length Packet for the IN status phase
  93062. + * of a 2 stage control transfer.
  93063. + */
  93064. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  93065. +{
  93066. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  93067. + if (pcd->ep0state == EP0_STALL) {
  93068. + return;
  93069. + }
  93070. +
  93071. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  93072. +
  93073. + /* Prepare for more SETUP Packets */
  93074. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  93075. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  93076. + && (pcd->core_if->dma_desc_enable)
  93077. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  93078. + DWC_DEBUGPL(DBG_PCDV,
  93079. + "Data terminated wait next packet in out_desc_addr\n");
  93080. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  93081. + pcd->data_terminated = 1;
  93082. + }
  93083. + ep0->dwc_ep.xfer_len = 0;
  93084. + ep0->dwc_ep.xfer_count = 0;
  93085. + ep0->dwc_ep.is_in = 1;
  93086. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  93087. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  93088. +
  93089. + /* Prepare for more SETUP Packets */
  93090. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  93091. +}
  93092. +
  93093. +/**
  93094. + * This function starts the Zero-Length Packet for the OUT status phase
  93095. + * of a 2 stage control transfer.
  93096. + */
  93097. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  93098. +{
  93099. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  93100. + if (pcd->ep0state == EP0_STALL) {
  93101. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  93102. + return;
  93103. + }
  93104. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  93105. +
  93106. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  93107. + ep0->dwc_ep.xfer_len = 0;
  93108. + ep0->dwc_ep.xfer_count = 0;
  93109. + ep0->dwc_ep.is_in = 0;
  93110. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  93111. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  93112. +
  93113. + /* Prepare for more SETUP Packets */
  93114. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  93115. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  93116. + }
  93117. +}
  93118. +
  93119. +/**
  93120. + * Clear the EP halt (STALL) and if pending requests start the
  93121. + * transfer.
  93122. + */
  93123. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  93124. +{
  93125. + if (ep->dwc_ep.stall_clear_flag == 0)
  93126. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  93127. +
  93128. + /* Reactive the EP */
  93129. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  93130. + if (ep->stopped) {
  93131. + ep->stopped = 0;
  93132. + /* If there is a request in the EP queue start it */
  93133. +
  93134. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  93135. + * epmismatch not yet implemented. */
  93136. +
  93137. + /*
  93138. + * Above fixme is solved by implmenting a tasklet to call the
  93139. + * start_next_request(), outside of interrupt context at some
  93140. + * time after the current time, after a clear-halt setup packet.
  93141. + * Still need to implement ep mismatch in the future if a gadget
  93142. + * ever uses more than one endpoint at once
  93143. + */
  93144. + ep->queue_sof = 1;
  93145. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  93146. + }
  93147. + /* Start Control Status Phase */
  93148. + do_setup_in_status_phase(pcd);
  93149. +}
  93150. +
  93151. +/**
  93152. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  93153. + * is sent from the host. The Device Control register is written with
  93154. + * the Test Mode bits set to the specified Test Mode. This is done as
  93155. + * a tasklet so that the "Status" phase of the control transfer
  93156. + * completes before transmitting the TEST packets.
  93157. + *
  93158. + * @todo This has not been tested since the tasklet struct was put
  93159. + * into the PCD struct!
  93160. + *
  93161. + */
  93162. +void do_test_mode(void *data)
  93163. +{
  93164. + dctl_data_t dctl;
  93165. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  93166. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93167. + int test_mode = pcd->test_mode;
  93168. +
  93169. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  93170. +
  93171. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  93172. + switch (test_mode) {
  93173. + case 1: // TEST_J
  93174. + dctl.b.tstctl = 1;
  93175. + break;
  93176. +
  93177. + case 2: // TEST_K
  93178. + dctl.b.tstctl = 2;
  93179. + break;
  93180. +
  93181. + case 3: // TEST_SE0_NAK
  93182. + dctl.b.tstctl = 3;
  93183. + break;
  93184. +
  93185. + case 4: // TEST_PACKET
  93186. + dctl.b.tstctl = 4;
  93187. + break;
  93188. +
  93189. + case 5: // TEST_FORCE_ENABLE
  93190. + dctl.b.tstctl = 5;
  93191. + break;
  93192. + }
  93193. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  93194. +}
  93195. +
  93196. +/**
  93197. + * This function process the GET_STATUS Setup Commands.
  93198. + */
  93199. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  93200. +{
  93201. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  93202. + dwc_otg_pcd_ep_t *ep;
  93203. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  93204. + uint16_t *status = pcd->status_buf;
  93205. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93206. +
  93207. +#ifdef DEBUG_EP0
  93208. + DWC_DEBUGPL(DBG_PCD,
  93209. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  93210. + ctrl.bmRequestType, ctrl.bRequest,
  93211. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  93212. + UGETW(ctrl.wLength));
  93213. +#endif
  93214. +
  93215. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  93216. + case UT_DEVICE:
  93217. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  93218. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  93219. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  93220. + DWC_PRINTF("OTG CAP - %d, %d\n",
  93221. + core_if->core_params->otg_cap,
  93222. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  93223. + if (core_if->otg_ver == 1
  93224. + && core_if->core_params->otg_cap ==
  93225. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  93226. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  93227. + *otgsts = (core_if->otg_sts & 0x1);
  93228. + pcd->ep0_pending = 1;
  93229. + ep0->dwc_ep.start_xfer_buff =
  93230. + (uint8_t *) otgsts;
  93231. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  93232. + ep0->dwc_ep.dma_addr =
  93233. + pcd->status_buf_dma_handle;
  93234. + ep0->dwc_ep.xfer_len = 1;
  93235. + ep0->dwc_ep.xfer_count = 0;
  93236. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  93237. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  93238. + &ep0->dwc_ep);
  93239. + return;
  93240. + } else {
  93241. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93242. + return;
  93243. + }
  93244. + break;
  93245. + } else {
  93246. + *status = 0x1; /* Self powered */
  93247. + *status |= pcd->remote_wakeup_enable << 1;
  93248. + break;
  93249. + }
  93250. + case UT_INTERFACE:
  93251. + *status = 0;
  93252. + break;
  93253. +
  93254. + case UT_ENDPOINT:
  93255. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  93256. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  93257. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93258. + return;
  93259. + }
  93260. + /** @todo check for EP stall */
  93261. + *status = ep->stopped;
  93262. + break;
  93263. + }
  93264. + pcd->ep0_pending = 1;
  93265. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  93266. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  93267. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  93268. + ep0->dwc_ep.xfer_len = 2;
  93269. + ep0->dwc_ep.xfer_count = 0;
  93270. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  93271. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  93272. +}
  93273. +
  93274. +/**
  93275. + * This function process the SET_FEATURE Setup Commands.
  93276. + */
  93277. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  93278. +{
  93279. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93280. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  93281. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  93282. + dwc_otg_pcd_ep_t *ep = 0;
  93283. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  93284. + gotgctl_data_t gotgctl = {.d32 = 0 };
  93285. +
  93286. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  93287. + ctrl.bmRequestType, ctrl.bRequest,
  93288. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  93289. + UGETW(ctrl.wLength));
  93290. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  93291. +
  93292. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  93293. + case UT_DEVICE:
  93294. + switch (UGETW(ctrl.wValue)) {
  93295. + case UF_DEVICE_REMOTE_WAKEUP:
  93296. + pcd->remote_wakeup_enable = 1;
  93297. + break;
  93298. +
  93299. + case UF_TEST_MODE:
  93300. + /* Setup the Test Mode tasklet to do the Test
  93301. + * Packet generation after the SETUP Status
  93302. + * phase has completed. */
  93303. +
  93304. + /** @todo This has not been tested since the
  93305. + * tasklet struct was put into the PCD
  93306. + * struct! */
  93307. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  93308. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  93309. + break;
  93310. +
  93311. + case UF_DEVICE_B_HNP_ENABLE:
  93312. + DWC_DEBUGPL(DBG_PCDV,
  93313. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  93314. +
  93315. + /* dev may initiate HNP */
  93316. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  93317. + pcd->b_hnp_enable = 1;
  93318. + dwc_otg_pcd_update_otg(pcd, 0);
  93319. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  93320. + /**@todo Is the gotgctl.devhnpen cleared
  93321. + * by a USB Reset? */
  93322. + gotgctl.b.devhnpen = 1;
  93323. + gotgctl.b.hnpreq = 1;
  93324. + DWC_WRITE_REG32(&global_regs->gotgctl,
  93325. + gotgctl.d32);
  93326. + } else {
  93327. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93328. + return;
  93329. + }
  93330. + break;
  93331. +
  93332. + case UF_DEVICE_A_HNP_SUPPORT:
  93333. + /* RH port supports HNP */
  93334. + DWC_DEBUGPL(DBG_PCDV,
  93335. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  93336. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  93337. + pcd->a_hnp_support = 1;
  93338. + dwc_otg_pcd_update_otg(pcd, 0);
  93339. + } else {
  93340. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93341. + return;
  93342. + }
  93343. + break;
  93344. +
  93345. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  93346. + /* other RH port does */
  93347. + DWC_DEBUGPL(DBG_PCDV,
  93348. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  93349. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  93350. + pcd->a_alt_hnp_support = 1;
  93351. + dwc_otg_pcd_update_otg(pcd, 0);
  93352. + } else {
  93353. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93354. + return;
  93355. + }
  93356. + break;
  93357. +
  93358. + default:
  93359. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93360. + return;
  93361. +
  93362. + }
  93363. + do_setup_in_status_phase(pcd);
  93364. + break;
  93365. +
  93366. + case UT_INTERFACE:
  93367. + do_gadget_setup(pcd, &ctrl);
  93368. + break;
  93369. +
  93370. + case UT_ENDPOINT:
  93371. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  93372. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  93373. + if (ep == 0) {
  93374. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93375. + return;
  93376. + }
  93377. + ep->stopped = 1;
  93378. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  93379. + }
  93380. + do_setup_in_status_phase(pcd);
  93381. + break;
  93382. + }
  93383. +}
  93384. +
  93385. +/**
  93386. + * This function process the CLEAR_FEATURE Setup Commands.
  93387. + */
  93388. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  93389. +{
  93390. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  93391. + dwc_otg_pcd_ep_t *ep = 0;
  93392. +
  93393. + DWC_DEBUGPL(DBG_PCD,
  93394. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  93395. + ctrl.bmRequestType, ctrl.bRequest,
  93396. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  93397. + UGETW(ctrl.wLength));
  93398. +
  93399. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  93400. + case UT_DEVICE:
  93401. + switch (UGETW(ctrl.wValue)) {
  93402. + case UF_DEVICE_REMOTE_WAKEUP:
  93403. + pcd->remote_wakeup_enable = 0;
  93404. + break;
  93405. +
  93406. + case UF_TEST_MODE:
  93407. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  93408. + break;
  93409. +
  93410. + default:
  93411. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93412. + return;
  93413. + }
  93414. + do_setup_in_status_phase(pcd);
  93415. + break;
  93416. +
  93417. + case UT_ENDPOINT:
  93418. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  93419. + if (ep == 0) {
  93420. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  93421. + return;
  93422. + }
  93423. +
  93424. + pcd_clear_halt(pcd, ep);
  93425. +
  93426. + break;
  93427. + }
  93428. +}
  93429. +
  93430. +/**
  93431. + * This function process the SET_ADDRESS Setup Commands.
  93432. + */
  93433. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  93434. +{
  93435. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  93436. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  93437. +
  93438. + if (ctrl.bmRequestType == UT_DEVICE) {
  93439. + dcfg_data_t dcfg = {.d32 = 0 };
  93440. +
  93441. +#ifdef DEBUG_EP0
  93442. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  93443. +#endif
  93444. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  93445. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  93446. + do_setup_in_status_phase(pcd);
  93447. + }
  93448. +}
  93449. +
  93450. +/**
  93451. + * This function processes SETUP commands. In Linux, the USB Command
  93452. + * processing is done in two places - the first being the PCD and the
  93453. + * second in the Gadget Driver (for example, the File-Backed Storage
  93454. + * Gadget Driver).
  93455. + *
  93456. + * <table>
  93457. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  93458. + *
  93459. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  93460. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  93461. + * </td></tr>
  93462. + *
  93463. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  93464. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  93465. + * interface requests are ignored.</td></tr>
  93466. + *
  93467. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  93468. + * requests are processed by the PCD. Interface requests are passed
  93469. + * to the Gadget Driver.</td></tr>
  93470. + *
  93471. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  93472. + * with device address received </td></tr>
  93473. + *
  93474. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  93475. + * requested descriptor</td></tr>
  93476. + *
  93477. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  93478. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  93479. + *
  93480. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  93481. + * all EPs and enable EPs for new configuration.</td></tr>
  93482. + *
  93483. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  93484. + * the current configuration</td></tr>
  93485. + *
  93486. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  93487. + * EPs and enable EPs for new configuration.</td></tr>
  93488. + *
  93489. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  93490. + * current interface.</td></tr>
  93491. + *
  93492. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  93493. + * message.</td></tr>
  93494. + * </table>
  93495. + *
  93496. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  93497. + * processed by pcd_setup. Calling the Function Driver's setup function from
  93498. + * pcd_setup processes the gadget SETUP commands.
  93499. + */
  93500. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  93501. +{
  93502. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  93503. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  93504. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  93505. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  93506. +
  93507. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  93508. +
  93509. +#ifdef DWC_UTE_CFI
  93510. + int retval = 0;
  93511. + struct cfi_usb_ctrlrequest cfi_req;
  93512. +#endif
  93513. +
  93514. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  93515. +
  93516. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  93517. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  93518. + && (doeptsize0.b.supcnt < 2)
  93519. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  93520. + DWC_ERROR
  93521. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  93522. + }
  93523. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  93524. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  93525. + ctrl =
  93526. + (pcd->setup_pkt +
  93527. + (3 - doeptsize0.b.supcnt - 1 +
  93528. + ep0->dwc_ep.stp_rollover))->req;
  93529. + }
  93530. +#ifdef DEBUG_EP0
  93531. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  93532. + ctrl.bmRequestType, ctrl.bRequest,
  93533. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  93534. + UGETW(ctrl.wLength));
  93535. +#endif
  93536. +
  93537. + /* Clean up the request queue */
  93538. + dwc_otg_request_nuke(ep0);
  93539. + ep0->stopped = 0;
  93540. +
  93541. + if (ctrl.bmRequestType & UE_DIR_IN) {
  93542. + ep0->dwc_ep.is_in = 1;
  93543. + pcd->ep0state = EP0_IN_DATA_PHASE;
  93544. + } else {
  93545. + ep0->dwc_ep.is_in = 0;
  93546. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  93547. + }
  93548. +
  93549. + if (UGETW(ctrl.wLength) == 0) {
  93550. + ep0->dwc_ep.is_in = 1;
  93551. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  93552. + }
  93553. +
  93554. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  93555. +
  93556. +#ifdef DWC_UTE_CFI
  93557. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  93558. +
  93559. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  93560. + ctrl.bRequestType, ctrl.bRequest);
  93561. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  93562. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  93563. + retval = cfi_setup(pcd, &cfi_req);
  93564. + if (retval < 0) {
  93565. + ep0_do_stall(pcd, retval);
  93566. + pcd->ep0_pending = 0;
  93567. + return;
  93568. + }
  93569. +
  93570. + /* if need gadget setup then call it and check the retval */
  93571. + if (pcd->cfi->need_gadget_att) {
  93572. + retval =
  93573. + cfi_gadget_setup(pcd,
  93574. + &pcd->
  93575. + cfi->ctrl_req);
  93576. + if (retval < 0) {
  93577. + pcd->ep0_pending = 0;
  93578. + return;
  93579. + }
  93580. + }
  93581. +
  93582. + if (pcd->cfi->need_status_in_complete) {
  93583. + do_setup_in_status_phase(pcd);
  93584. + }
  93585. + return;
  93586. + }
  93587. + }
  93588. +#endif
  93589. +
  93590. + /* handle non-standard (class/vendor) requests in the gadget driver */
  93591. + do_gadget_setup(pcd, &ctrl);
  93592. + return;
  93593. + }
  93594. +
  93595. + /** @todo NGS: Handle bad setup packet? */
  93596. +
  93597. +///////////////////////////////////////////
  93598. +//// --- Standard Request handling --- ////
  93599. +
  93600. + switch (ctrl.bRequest) {
  93601. + case UR_GET_STATUS:
  93602. + do_get_status(pcd);
  93603. + break;
  93604. +
  93605. + case UR_CLEAR_FEATURE:
  93606. + do_clear_feature(pcd);
  93607. + break;
  93608. +
  93609. + case UR_SET_FEATURE:
  93610. + do_set_feature(pcd);
  93611. + break;
  93612. +
  93613. + case UR_SET_ADDRESS:
  93614. + do_set_address(pcd);
  93615. + break;
  93616. +
  93617. + case UR_SET_INTERFACE:
  93618. + case UR_SET_CONFIG:
  93619. +// _pcd->request_config = 1; /* Configuration changed */
  93620. + do_gadget_setup(pcd, &ctrl);
  93621. + break;
  93622. +
  93623. + case UR_SYNCH_FRAME:
  93624. + do_gadget_setup(pcd, &ctrl);
  93625. + break;
  93626. +
  93627. + default:
  93628. + /* Call the Gadget Driver's setup functions */
  93629. + do_gadget_setup(pcd, &ctrl);
  93630. + break;
  93631. + }
  93632. +}
  93633. +
  93634. +/**
  93635. + * This function completes the ep0 control transfer.
  93636. + */
  93637. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  93638. +{
  93639. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  93640. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  93641. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  93642. + dev_if->in_ep_regs[ep->dwc_ep.num];
  93643. +#ifdef DEBUG_EP0
  93644. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  93645. + dev_if->out_ep_regs[ep->dwc_ep.num];
  93646. +#endif
  93647. + deptsiz0_data_t deptsiz;
  93648. + dev_dma_desc_sts_t desc_sts;
  93649. + dwc_otg_pcd_request_t *req;
  93650. + int is_last = 0;
  93651. + dwc_otg_pcd_t *pcd = ep->pcd;
  93652. +
  93653. +#ifdef DWC_UTE_CFI
  93654. + struct cfi_usb_ctrlrequest *ctrlreq;
  93655. + int retval = -DWC_E_NOT_SUPPORTED;
  93656. +#endif
  93657. +
  93658. + desc_sts.b.bytes = 0;
  93659. +
  93660. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  93661. + if (ep->dwc_ep.is_in) {
  93662. +#ifdef DEBUG_EP0
  93663. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  93664. +#endif
  93665. + do_setup_out_status_phase(pcd);
  93666. + } else {
  93667. +#ifdef DEBUG_EP0
  93668. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  93669. +#endif
  93670. +
  93671. +#ifdef DWC_UTE_CFI
  93672. + ctrlreq = &pcd->cfi->ctrl_req;
  93673. +
  93674. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  93675. + if (ctrlreq->bRequest > 0xB0
  93676. + && ctrlreq->bRequest < 0xBF) {
  93677. +
  93678. + /* Return if the PCD failed to handle the request */
  93679. + if ((retval =
  93680. + pcd->cfi->ops.
  93681. + ctrl_write_complete(pcd->cfi,
  93682. + pcd)) < 0) {
  93683. + CFI_INFO
  93684. + ("ERROR setting a new value in the PCD(%d)\n",
  93685. + retval);
  93686. + ep0_do_stall(pcd, retval);
  93687. + pcd->ep0_pending = 0;
  93688. + return 0;
  93689. + }
  93690. +
  93691. + /* If the gadget needs to be notified on the request */
  93692. + if (pcd->cfi->need_gadget_att == 1) {
  93693. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  93694. + retval =
  93695. + cfi_gadget_setup(pcd,
  93696. + &pcd->cfi->
  93697. + ctrl_req);
  93698. +
  93699. + /* Return from the function if the gadget failed to process
  93700. + * the request properly - this should never happen !!!
  93701. + */
  93702. + if (retval < 0) {
  93703. + CFI_INFO
  93704. + ("ERROR setting a new value in the gadget(%d)\n",
  93705. + retval);
  93706. + pcd->ep0_pending = 0;
  93707. + return 0;
  93708. + }
  93709. + }
  93710. +
  93711. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  93712. + retval);
  93713. + /* If we hit here then the PCD and the gadget has properly
  93714. + * handled the request - so send the ZLP IN to the host.
  93715. + */
  93716. + /* @todo: MAS - decide whether we need to start the setup
  93717. + * stage based on the need_setup value of the cfi object
  93718. + */
  93719. + do_setup_in_status_phase(pcd);
  93720. + pcd->ep0_pending = 0;
  93721. + return 1;
  93722. + }
  93723. + }
  93724. +#endif
  93725. +
  93726. + do_setup_in_status_phase(pcd);
  93727. + }
  93728. + pcd->ep0_pending = 0;
  93729. + return 1;
  93730. + }
  93731. +
  93732. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  93733. + return 0;
  93734. + }
  93735. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  93736. +
  93737. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  93738. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  93739. + is_last = 1;
  93740. + } else if (ep->dwc_ep.is_in) {
  93741. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  93742. + if (core_if->dma_desc_enable != 0)
  93743. + desc_sts = dev_if->in_desc_addr->status;
  93744. +#ifdef DEBUG_EP0
  93745. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  93746. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  93747. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  93748. +#endif
  93749. +
  93750. + if (((core_if->dma_desc_enable == 0)
  93751. + && (deptsiz.b.xfersize == 0))
  93752. + || ((core_if->dma_desc_enable != 0)
  93753. + && (desc_sts.b.bytes == 0))) {
  93754. + req->actual = ep->dwc_ep.xfer_count;
  93755. + /* Is a Zero Len Packet needed? */
  93756. + if (req->sent_zlp) {
  93757. +#ifdef DEBUG_EP0
  93758. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  93759. +#endif
  93760. + req->sent_zlp = 0;
  93761. + }
  93762. + do_setup_out_status_phase(pcd);
  93763. + }
  93764. + } else {
  93765. + /* ep0-OUT */
  93766. +#ifdef DEBUG_EP0
  93767. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  93768. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  93769. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  93770. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  93771. +#endif
  93772. + req->actual = ep->dwc_ep.xfer_count;
  93773. +
  93774. + /* Is a Zero Len Packet needed? */
  93775. + if (req->sent_zlp) {
  93776. +#ifdef DEBUG_EP0
  93777. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  93778. +#endif
  93779. + req->sent_zlp = 0;
  93780. + }
  93781. + /* For older cores do setup in status phase in Slave/BDMA modes,
  93782. + * starting from 3.00 do that only in slave, and for DMA modes
  93783. + * just re-enable ep 0 OUT here*/
  93784. + if (core_if->dma_enable == 0
  93785. + || (core_if->dma_desc_enable == 0
  93786. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  93787. + do_setup_in_status_phase(pcd);
  93788. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  93789. + DWC_DEBUGPL(DBG_PCDV,
  93790. + "Enable out ep before in status phase\n");
  93791. + ep0_out_start(core_if, pcd);
  93792. + }
  93793. + }
  93794. +
  93795. + /* Complete the request */
  93796. + if (is_last) {
  93797. + dwc_otg_request_done(ep, req, 0);
  93798. + ep->dwc_ep.start_xfer_buff = 0;
  93799. + ep->dwc_ep.xfer_buff = 0;
  93800. + ep->dwc_ep.xfer_len = 0;
  93801. + return 1;
  93802. + }
  93803. + return 0;
  93804. +}
  93805. +
  93806. +#ifdef DWC_UTE_CFI
  93807. +/**
  93808. + * This function calculates traverses all the CFI DMA descriptors and
  93809. + * and accumulates the bytes that are left to be transfered.
  93810. + *
  93811. + * @return The total bytes left to transfered, or a negative value as failure
  93812. + */
  93813. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  93814. +{
  93815. + int32_t ret = 0;
  93816. + int i;
  93817. + struct dwc_otg_dma_desc *ddesc = NULL;
  93818. + struct cfi_ep *cfiep;
  93819. +
  93820. + /* See if the pcd_ep has its respective cfi_ep mapped */
  93821. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  93822. + if (!cfiep) {
  93823. + CFI_INFO("%s: Failed to find ep\n", __func__);
  93824. + return -1;
  93825. + }
  93826. +
  93827. + ddesc = ep->dwc_ep.descs;
  93828. +
  93829. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  93830. +
  93831. +#if defined(PRINT_CFI_DMA_DESCS)
  93832. + print_desc(ddesc, ep->ep.name, i);
  93833. +#endif
  93834. + ret += ddesc->status.b.bytes;
  93835. + ddesc++;
  93836. + }
  93837. +
  93838. + if (ret)
  93839. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  93840. + ret);
  93841. +
  93842. + return ret;
  93843. +}
  93844. +#endif
  93845. +
  93846. +/**
  93847. + * This function completes the request for the EP. If there are
  93848. + * additional requests for the EP in the queue they will be started.
  93849. + */
  93850. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  93851. +{
  93852. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  93853. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  93854. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  93855. + dev_if->in_ep_regs[ep->dwc_ep.num];
  93856. + deptsiz_data_t deptsiz;
  93857. + dev_dma_desc_sts_t desc_sts;
  93858. + dwc_otg_pcd_request_t *req = 0;
  93859. + dwc_otg_dev_dma_desc_t *dma_desc;
  93860. + uint32_t byte_count = 0;
  93861. + int is_last = 0;
  93862. + int i;
  93863. +
  93864. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  93865. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  93866. +
  93867. + /* Get any pending requests */
  93868. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  93869. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  93870. + if (!req) {
  93871. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  93872. + return;
  93873. + }
  93874. + } else {
  93875. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  93876. + return;
  93877. + }
  93878. +
  93879. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  93880. +
  93881. + if (ep->dwc_ep.is_in) {
  93882. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  93883. +
  93884. + if (core_if->dma_enable) {
  93885. + if (core_if->dma_desc_enable == 0) {
  93886. + if (deptsiz.b.xfersize == 0
  93887. + && deptsiz.b.pktcnt == 0) {
  93888. + byte_count =
  93889. + ep->dwc_ep.xfer_len -
  93890. + ep->dwc_ep.xfer_count;
  93891. +
  93892. + ep->dwc_ep.xfer_buff += byte_count;
  93893. + ep->dwc_ep.dma_addr += byte_count;
  93894. + ep->dwc_ep.xfer_count += byte_count;
  93895. +
  93896. + DWC_DEBUGPL(DBG_PCDV,
  93897. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  93898. + ep->dwc_ep.num,
  93899. + (ep->dwc_ep.
  93900. + is_in ? "IN" : "OUT"),
  93901. + ep->dwc_ep.xfer_len,
  93902. + deptsiz.b.xfersize,
  93903. + deptsiz.b.pktcnt);
  93904. +
  93905. + if (ep->dwc_ep.xfer_len <
  93906. + ep->dwc_ep.total_len) {
  93907. + dwc_otg_ep_start_transfer
  93908. + (core_if, &ep->dwc_ep);
  93909. + } else if (ep->dwc_ep.sent_zlp) {
  93910. + /*
  93911. + * This fragment of code should initiate 0
  93912. + * length transfer in case if it is queued
  93913. + * a transfer with size divisible to EPs max
  93914. + * packet size and with usb_request zero field
  93915. + * is set, which means that after data is transfered,
  93916. + * it is also should be transfered
  93917. + * a 0 length packet at the end. For Slave and
  93918. + * Buffer DMA modes in this case SW has
  93919. + * to initiate 2 transfers one with transfer size,
  93920. + * and the second with 0 size. For Descriptor
  93921. + * DMA mode SW is able to initiate a transfer,
  93922. + * which will handle all the packets including
  93923. + * the last 0 length.
  93924. + */
  93925. + ep->dwc_ep.sent_zlp = 0;
  93926. + dwc_otg_ep_start_zl_transfer
  93927. + (core_if, &ep->dwc_ep);
  93928. + } else {
  93929. + is_last = 1;
  93930. + }
  93931. + } else {
  93932. + if (ep->dwc_ep.type ==
  93933. + DWC_OTG_EP_TYPE_ISOC) {
  93934. + req->actual = 0;
  93935. + dwc_otg_request_done(ep, req, 0);
  93936. +
  93937. + ep->dwc_ep.start_xfer_buff = 0;
  93938. + ep->dwc_ep.xfer_buff = 0;
  93939. + ep->dwc_ep.xfer_len = 0;
  93940. +
  93941. + /* If there is a request in the queue start it. */
  93942. + start_next_request(ep);
  93943. + } else
  93944. + DWC_WARN
  93945. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  93946. + ep->dwc_ep.num,
  93947. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  93948. + deptsiz.b.xfersize,
  93949. + deptsiz.b.pktcnt);
  93950. + }
  93951. + } else {
  93952. + dma_desc = ep->dwc_ep.desc_addr;
  93953. + byte_count = 0;
  93954. + ep->dwc_ep.sent_zlp = 0;
  93955. +
  93956. +#ifdef DWC_UTE_CFI
  93957. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  93958. + ep->dwc_ep.buff_mode);
  93959. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  93960. + int residue;
  93961. +
  93962. + residue = cfi_calc_desc_residue(ep);
  93963. + if (residue < 0)
  93964. + return;
  93965. +
  93966. + byte_count = residue;
  93967. + } else {
  93968. +#endif
  93969. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  93970. + ++i) {
  93971. + desc_sts = dma_desc->status;
  93972. + byte_count += desc_sts.b.bytes;
  93973. + dma_desc++;
  93974. + }
  93975. +#ifdef DWC_UTE_CFI
  93976. + }
  93977. +#endif
  93978. + if (byte_count == 0) {
  93979. + ep->dwc_ep.xfer_count =
  93980. + ep->dwc_ep.total_len;
  93981. + is_last = 1;
  93982. + } else {
  93983. + DWC_WARN("Incomplete transfer\n");
  93984. + }
  93985. + }
  93986. + } else {
  93987. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  93988. + DWC_DEBUGPL(DBG_PCDV,
  93989. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  93990. + ep->dwc_ep.num,
  93991. + ep->dwc_ep.is_in ? "IN" : "OUT",
  93992. + ep->dwc_ep.xfer_len,
  93993. + deptsiz.b.xfersize,
  93994. + deptsiz.b.pktcnt);
  93995. +
  93996. + /* Check if the whole transfer was completed,
  93997. + * if no, setup transfer for next portion of data
  93998. + */
  93999. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  94000. + dwc_otg_ep_start_transfer(core_if,
  94001. + &ep->dwc_ep);
  94002. + } else if (ep->dwc_ep.sent_zlp) {
  94003. + /*
  94004. + * This fragment of code should initiate 0
  94005. + * length trasfer in case if it is queued
  94006. + * a trasfer with size divisible to EPs max
  94007. + * packet size and with usb_request zero field
  94008. + * is set, which means that after data is transfered,
  94009. + * it is also should be transfered
  94010. + * a 0 length packet at the end. For Slave and
  94011. + * Buffer DMA modes in this case SW has
  94012. + * to initiate 2 transfers one with transfer size,
  94013. + * and the second with 0 size. For Desriptor
  94014. + * DMA mode SW is able to initiate a transfer,
  94015. + * which will handle all the packets including
  94016. + * the last 0 legth.
  94017. + */
  94018. + ep->dwc_ep.sent_zlp = 0;
  94019. + dwc_otg_ep_start_zl_transfer(core_if,
  94020. + &ep->dwc_ep);
  94021. + } else {
  94022. + is_last = 1;
  94023. + }
  94024. + } else {
  94025. + DWC_WARN
  94026. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  94027. + ep->dwc_ep.num,
  94028. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  94029. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  94030. + }
  94031. + }
  94032. + } else {
  94033. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  94034. + dev_if->out_ep_regs[ep->dwc_ep.num];
  94035. + desc_sts.d32 = 0;
  94036. + if (core_if->dma_enable) {
  94037. + if (core_if->dma_desc_enable) {
  94038. + dma_desc = ep->dwc_ep.desc_addr;
  94039. + byte_count = 0;
  94040. + ep->dwc_ep.sent_zlp = 0;
  94041. +
  94042. +#ifdef DWC_UTE_CFI
  94043. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  94044. + ep->dwc_ep.buff_mode);
  94045. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  94046. + int residue;
  94047. + residue = cfi_calc_desc_residue(ep);
  94048. + if (residue < 0)
  94049. + return;
  94050. + byte_count = residue;
  94051. + } else {
  94052. +#endif
  94053. +
  94054. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  94055. + ++i) {
  94056. + desc_sts = dma_desc->status;
  94057. + byte_count += desc_sts.b.bytes;
  94058. + dma_desc++;
  94059. + }
  94060. +
  94061. +#ifdef DWC_UTE_CFI
  94062. + }
  94063. +#endif
  94064. + /* Checking for interrupt Out transfers with not
  94065. + * dword aligned mps sizes
  94066. + */
  94067. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  94068. + (ep->dwc_ep.maxpacket%4)) {
  94069. + ep->dwc_ep.xfer_count =
  94070. + ep->dwc_ep.total_len - byte_count;
  94071. + if ((ep->dwc_ep.xfer_len %
  94072. + ep->dwc_ep.maxpacket)
  94073. + && (ep->dwc_ep.xfer_len /
  94074. + ep->dwc_ep.maxpacket <
  94075. + MAX_DMA_DESC_CNT))
  94076. + ep->dwc_ep.xfer_len -=
  94077. + (ep->dwc_ep.desc_cnt -
  94078. + 1) * ep->dwc_ep.maxpacket +
  94079. + ep->dwc_ep.xfer_len %
  94080. + ep->dwc_ep.maxpacket;
  94081. + else
  94082. + ep->dwc_ep.xfer_len -=
  94083. + ep->dwc_ep.desc_cnt *
  94084. + ep->dwc_ep.maxpacket;
  94085. + if (ep->dwc_ep.xfer_len > 0) {
  94086. + dwc_otg_ep_start_transfer
  94087. + (core_if, &ep->dwc_ep);
  94088. + } else {
  94089. + is_last = 1;
  94090. + }
  94091. + } else {
  94092. + ep->dwc_ep.xfer_count =
  94093. + ep->dwc_ep.total_len - byte_count +
  94094. + ((4 -
  94095. + (ep->dwc_ep.
  94096. + total_len & 0x3)) & 0x3);
  94097. + is_last = 1;
  94098. + }
  94099. + } else {
  94100. + deptsiz.d32 = 0;
  94101. + deptsiz.d32 =
  94102. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  94103. +
  94104. + byte_count = (ep->dwc_ep.xfer_len -
  94105. + ep->dwc_ep.xfer_count -
  94106. + deptsiz.b.xfersize);
  94107. + ep->dwc_ep.xfer_buff += byte_count;
  94108. + ep->dwc_ep.dma_addr += byte_count;
  94109. + ep->dwc_ep.xfer_count += byte_count;
  94110. +
  94111. + /* Check if the whole transfer was completed,
  94112. + * if no, setup transfer for next portion of data
  94113. + */
  94114. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  94115. + dwc_otg_ep_start_transfer(core_if,
  94116. + &ep->dwc_ep);
  94117. + } else if (ep->dwc_ep.sent_zlp) {
  94118. + /*
  94119. + * This fragment of code should initiate 0
  94120. + * length trasfer in case if it is queued
  94121. + * a trasfer with size divisible to EPs max
  94122. + * packet size and with usb_request zero field
  94123. + * is set, which means that after data is transfered,
  94124. + * it is also should be transfered
  94125. + * a 0 length packet at the end. For Slave and
  94126. + * Buffer DMA modes in this case SW has
  94127. + * to initiate 2 transfers one with transfer size,
  94128. + * and the second with 0 size. For Desriptor
  94129. + * DMA mode SW is able to initiate a transfer,
  94130. + * which will handle all the packets including
  94131. + * the last 0 legth.
  94132. + */
  94133. + ep->dwc_ep.sent_zlp = 0;
  94134. + dwc_otg_ep_start_zl_transfer(core_if,
  94135. + &ep->dwc_ep);
  94136. + } else {
  94137. + is_last = 1;
  94138. + }
  94139. + }
  94140. + } else {
  94141. + /* Check if the whole transfer was completed,
  94142. + * if no, setup transfer for next portion of data
  94143. + */
  94144. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  94145. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  94146. + } else if (ep->dwc_ep.sent_zlp) {
  94147. + /*
  94148. + * This fragment of code should initiate 0
  94149. + * length transfer in case if it is queued
  94150. + * a transfer with size divisible to EPs max
  94151. + * packet size and with usb_request zero field
  94152. + * is set, which means that after data is transfered,
  94153. + * it is also should be transfered
  94154. + * a 0 length packet at the end. For Slave and
  94155. + * Buffer DMA modes in this case SW has
  94156. + * to initiate 2 transfers one with transfer size,
  94157. + * and the second with 0 size. For Descriptor
  94158. + * DMA mode SW is able to initiate a transfer,
  94159. + * which will handle all the packets including
  94160. + * the last 0 length.
  94161. + */
  94162. + ep->dwc_ep.sent_zlp = 0;
  94163. + dwc_otg_ep_start_zl_transfer(core_if,
  94164. + &ep->dwc_ep);
  94165. + } else {
  94166. + is_last = 1;
  94167. + }
  94168. + }
  94169. +
  94170. + DWC_DEBUGPL(DBG_PCDV,
  94171. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  94172. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  94173. + ep->dwc_ep.is_in ? "IN" : "OUT",
  94174. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  94175. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  94176. + }
  94177. +
  94178. + /* Complete the request */
  94179. + if (is_last) {
  94180. +#ifdef DWC_UTE_CFI
  94181. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  94182. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  94183. + } else {
  94184. +#endif
  94185. + req->actual = ep->dwc_ep.xfer_count;
  94186. +#ifdef DWC_UTE_CFI
  94187. + }
  94188. +#endif
  94189. + if (req->dw_align_buf) {
  94190. + if (!ep->dwc_ep.is_in) {
  94191. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  94192. + }
  94193. + DWC_DMA_FREE(req->length, req->dw_align_buf,
  94194. + req->dw_align_buf_dma);
  94195. + }
  94196. +
  94197. + dwc_otg_request_done(ep, req, 0);
  94198. +
  94199. + ep->dwc_ep.start_xfer_buff = 0;
  94200. + ep->dwc_ep.xfer_buff = 0;
  94201. + ep->dwc_ep.xfer_len = 0;
  94202. +
  94203. + /* If there is a request in the queue start it. */
  94204. + start_next_request(ep);
  94205. + }
  94206. +}
  94207. +
  94208. +#ifdef DWC_EN_ISOC
  94209. +
  94210. +/**
  94211. + * This function BNA interrupt for Isochronous EPs
  94212. + *
  94213. + */
  94214. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  94215. +{
  94216. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  94217. + volatile uint32_t *addr;
  94218. + depctl_data_t depctl = {.d32 = 0 };
  94219. + dwc_otg_pcd_t *pcd = ep->pcd;
  94220. + dwc_otg_dev_dma_desc_t *dma_desc;
  94221. + int i;
  94222. +
  94223. + dma_desc =
  94224. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  94225. +
  94226. + if (dwc_ep->is_in) {
  94227. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  94228. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  94229. + sts.d32 = dma_desc->status.d32;
  94230. + sts.b_iso_in.bs = BS_HOST_READY;
  94231. + dma_desc->status.d32 = sts.d32;
  94232. + }
  94233. + } else {
  94234. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  94235. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  94236. + sts.d32 = dma_desc->status.d32;
  94237. + sts.b_iso_out.bs = BS_HOST_READY;
  94238. + dma_desc->status.d32 = sts.d32;
  94239. + }
  94240. + }
  94241. +
  94242. + if (dwc_ep->is_in == 0) {
  94243. + addr =
  94244. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  94245. + num]->doepctl;
  94246. + } else {
  94247. + addr =
  94248. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  94249. + }
  94250. + depctl.b.epena = 1;
  94251. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  94252. +}
  94253. +
  94254. +/**
  94255. + * This function sets latest iso packet information(non-PTI mode)
  94256. + *
  94257. + * @param core_if Programming view of DWC_otg controller.
  94258. + * @param ep The EP to start the transfer on.
  94259. + *
  94260. + */
  94261. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  94262. +{
  94263. + deptsiz_data_t deptsiz = {.d32 = 0 };
  94264. + dma_addr_t dma_addr;
  94265. + uint32_t offset;
  94266. +
  94267. + if (ep->proc_buf_num)
  94268. + dma_addr = ep->dma_addr1;
  94269. + else
  94270. + dma_addr = ep->dma_addr0;
  94271. +
  94272. + if (ep->is_in) {
  94273. + deptsiz.d32 =
  94274. + DWC_READ_REG32(&core_if->dev_if->
  94275. + in_ep_regs[ep->num]->dieptsiz);
  94276. + offset = ep->data_per_frame;
  94277. + } else {
  94278. + deptsiz.d32 =
  94279. + DWC_READ_REG32(&core_if->dev_if->
  94280. + out_ep_regs[ep->num]->doeptsiz);
  94281. + offset =
  94282. + ep->data_per_frame +
  94283. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  94284. + }
  94285. +
  94286. + if (!deptsiz.b.xfersize) {
  94287. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  94288. + ep->pkt_info[ep->cur_pkt].offset =
  94289. + ep->cur_pkt_dma_addr - dma_addr;
  94290. + ep->pkt_info[ep->cur_pkt].status = 0;
  94291. + } else {
  94292. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  94293. + ep->pkt_info[ep->cur_pkt].offset =
  94294. + ep->cur_pkt_dma_addr - dma_addr;
  94295. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  94296. + }
  94297. + ep->cur_pkt_addr += offset;
  94298. + ep->cur_pkt_dma_addr += offset;
  94299. + ep->cur_pkt++;
  94300. +}
  94301. +
  94302. +/**
  94303. + * This function sets latest iso packet information(DDMA mode)
  94304. + *
  94305. + * @param core_if Programming view of DWC_otg controller.
  94306. + * @param dwc_ep The EP to start the transfer on.
  94307. + *
  94308. + */
  94309. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  94310. + dwc_ep_t * dwc_ep)
  94311. +{
  94312. + dwc_otg_dev_dma_desc_t *dma_desc;
  94313. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  94314. + iso_pkt_info_t *iso_packet;
  94315. + uint32_t data_per_desc;
  94316. + uint32_t offset;
  94317. + int i, j;
  94318. +
  94319. + iso_packet = dwc_ep->pkt_info;
  94320. +
  94321. + /** Reinit closed DMA Descriptors*/
  94322. + /** ISO OUT EP */
  94323. + if (dwc_ep->is_in == 0) {
  94324. + dma_desc =
  94325. + dwc_ep->iso_desc_addr +
  94326. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  94327. + offset = 0;
  94328. +
  94329. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  94330. + i += dwc_ep->pkt_per_frm) {
  94331. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  94332. + data_per_desc =
  94333. + ((j + 1) * dwc_ep->maxpacket >
  94334. + dwc_ep->
  94335. + data_per_frame) ? dwc_ep->data_per_frame -
  94336. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  94337. + data_per_desc +=
  94338. + (data_per_desc % 4) ? (4 -
  94339. + data_per_desc %
  94340. + 4) : 0;
  94341. +
  94342. + sts.d32 = dma_desc->status.d32;
  94343. +
  94344. + /* Write status in iso_packet_decsriptor */
  94345. + iso_packet->status =
  94346. + sts.b_iso_out.rxsts +
  94347. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  94348. + if (iso_packet->status) {
  94349. + iso_packet->status = -DWC_E_NO_DATA;
  94350. + }
  94351. +
  94352. + /* Received data length */
  94353. + if (!sts.b_iso_out.rxbytes) {
  94354. + iso_packet->length =
  94355. + data_per_desc -
  94356. + sts.b_iso_out.rxbytes;
  94357. + } else {
  94358. + iso_packet->length =
  94359. + data_per_desc -
  94360. + sts.b_iso_out.rxbytes + (4 -
  94361. + dwc_ep->data_per_frame
  94362. + % 4);
  94363. + }
  94364. +
  94365. + iso_packet->offset = offset;
  94366. +
  94367. + offset += data_per_desc;
  94368. + dma_desc++;
  94369. + iso_packet++;
  94370. + }
  94371. + }
  94372. +
  94373. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  94374. + data_per_desc =
  94375. + ((j + 1) * dwc_ep->maxpacket >
  94376. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  94377. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  94378. + data_per_desc +=
  94379. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  94380. +
  94381. + sts.d32 = dma_desc->status.d32;
  94382. +
  94383. + /* Write status in iso_packet_decsriptor */
  94384. + iso_packet->status =
  94385. + sts.b_iso_out.rxsts +
  94386. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  94387. + if (iso_packet->status) {
  94388. + iso_packet->status = -DWC_E_NO_DATA;
  94389. + }
  94390. +
  94391. + /* Received data length */
  94392. + iso_packet->length =
  94393. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  94394. +
  94395. + iso_packet->offset = offset;
  94396. +
  94397. + offset += data_per_desc;
  94398. + iso_packet++;
  94399. + dma_desc++;
  94400. + }
  94401. +
  94402. + sts.d32 = dma_desc->status.d32;
  94403. +
  94404. + /* Write status in iso_packet_decsriptor */
  94405. + iso_packet->status =
  94406. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  94407. + if (iso_packet->status) {
  94408. + iso_packet->status = -DWC_E_NO_DATA;
  94409. + }
  94410. + /* Received data length */
  94411. + if (!sts.b_iso_out.rxbytes) {
  94412. + iso_packet->length =
  94413. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  94414. + } else {
  94415. + iso_packet->length =
  94416. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  94417. + (4 - dwc_ep->data_per_frame % 4);
  94418. + }
  94419. +
  94420. + iso_packet->offset = offset;
  94421. + } else {
  94422. +/** ISO IN EP */
  94423. +
  94424. + dma_desc =
  94425. + dwc_ep->iso_desc_addr +
  94426. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  94427. +
  94428. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  94429. + sts.d32 = dma_desc->status.d32;
  94430. +
  94431. + /* Write status in iso packet descriptor */
  94432. + iso_packet->status =
  94433. + sts.b_iso_in.txsts +
  94434. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  94435. + if (iso_packet->status != 0) {
  94436. + iso_packet->status = -DWC_E_NO_DATA;
  94437. +
  94438. + }
  94439. + /* Bytes has been transfered */
  94440. + iso_packet->length =
  94441. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  94442. +
  94443. + dma_desc++;
  94444. + iso_packet++;
  94445. + }
  94446. +
  94447. + sts.d32 = dma_desc->status.d32;
  94448. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  94449. + sts.d32 = dma_desc->status.d32;
  94450. + }
  94451. +
  94452. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  94453. + iso_packet->status =
  94454. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  94455. + if (iso_packet->status != 0) {
  94456. + iso_packet->status = -DWC_E_NO_DATA;
  94457. + }
  94458. +
  94459. + /* Bytes has been transfered */
  94460. + iso_packet->length =
  94461. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  94462. + }
  94463. +}
  94464. +
  94465. +/**
  94466. + * This function reinitialize DMA Descriptors for Isochronous transfer
  94467. + *
  94468. + * @param core_if Programming view of DWC_otg controller.
  94469. + * @param dwc_ep The EP to start the transfer on.
  94470. + *
  94471. + */
  94472. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  94473. +{
  94474. + int i, j;
  94475. + dwc_otg_dev_dma_desc_t *dma_desc;
  94476. + dma_addr_t dma_ad;
  94477. + volatile uint32_t *addr;
  94478. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  94479. + uint32_t data_per_desc;
  94480. +
  94481. + if (dwc_ep->is_in == 0) {
  94482. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  94483. + } else {
  94484. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  94485. + }
  94486. +
  94487. + if (dwc_ep->proc_buf_num == 0) {
  94488. + /** Buffer 0 descriptors setup */
  94489. + dma_ad = dwc_ep->dma_addr0;
  94490. + } else {
  94491. + /** Buffer 1 descriptors setup */
  94492. + dma_ad = dwc_ep->dma_addr1;
  94493. + }
  94494. +
  94495. + /** Reinit closed DMA Descriptors*/
  94496. + /** ISO OUT EP */
  94497. + if (dwc_ep->is_in == 0) {
  94498. + dma_desc =
  94499. + dwc_ep->iso_desc_addr +
  94500. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  94501. +
  94502. + sts.b_iso_out.bs = BS_HOST_READY;
  94503. + sts.b_iso_out.rxsts = 0;
  94504. + sts.b_iso_out.l = 0;
  94505. + sts.b_iso_out.sp = 0;
  94506. + sts.b_iso_out.ioc = 0;
  94507. + sts.b_iso_out.pid = 0;
  94508. + sts.b_iso_out.framenum = 0;
  94509. +
  94510. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  94511. + i += dwc_ep->pkt_per_frm) {
  94512. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  94513. + data_per_desc =
  94514. + ((j + 1) * dwc_ep->maxpacket >
  94515. + dwc_ep->
  94516. + data_per_frame) ? dwc_ep->data_per_frame -
  94517. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  94518. + data_per_desc +=
  94519. + (data_per_desc % 4) ? (4 -
  94520. + data_per_desc %
  94521. + 4) : 0;
  94522. + sts.b_iso_out.rxbytes = data_per_desc;
  94523. + dma_desc->buf = dma_ad;
  94524. + dma_desc->status.d32 = sts.d32;
  94525. +
  94526. + dma_ad += data_per_desc;
  94527. + dma_desc++;
  94528. + }
  94529. + }
  94530. +
  94531. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  94532. +
  94533. + data_per_desc =
  94534. + ((j + 1) * dwc_ep->maxpacket >
  94535. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  94536. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  94537. + data_per_desc +=
  94538. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  94539. + sts.b_iso_out.rxbytes = data_per_desc;
  94540. +
  94541. + dma_desc->buf = dma_ad;
  94542. + dma_desc->status.d32 = sts.d32;
  94543. +
  94544. + dma_desc++;
  94545. + dma_ad += data_per_desc;
  94546. + }
  94547. +
  94548. + sts.b_iso_out.ioc = 1;
  94549. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  94550. +
  94551. + data_per_desc =
  94552. + ((j + 1) * dwc_ep->maxpacket >
  94553. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  94554. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  94555. + data_per_desc +=
  94556. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  94557. + sts.b_iso_out.rxbytes = data_per_desc;
  94558. +
  94559. + dma_desc->buf = dma_ad;
  94560. + dma_desc->status.d32 = sts.d32;
  94561. + } else {
  94562. +/** ISO IN EP */
  94563. +
  94564. + dma_desc =
  94565. + dwc_ep->iso_desc_addr +
  94566. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  94567. +
  94568. + sts.b_iso_in.bs = BS_HOST_READY;
  94569. + sts.b_iso_in.txsts = 0;
  94570. + sts.b_iso_in.sp = 0;
  94571. + sts.b_iso_in.ioc = 0;
  94572. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  94573. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  94574. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  94575. + sts.b_iso_in.l = 0;
  94576. +
  94577. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  94578. + dma_desc->buf = dma_ad;
  94579. + dma_desc->status.d32 = sts.d32;
  94580. +
  94581. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  94582. + dma_ad += dwc_ep->data_per_frame;
  94583. + dma_desc++;
  94584. + }
  94585. +
  94586. + sts.b_iso_in.ioc = 1;
  94587. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  94588. +
  94589. + dma_desc->buf = dma_ad;
  94590. + dma_desc->status.d32 = sts.d32;
  94591. +
  94592. + dwc_ep->next_frame =
  94593. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  94594. + }
  94595. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  94596. +}
  94597. +
  94598. +/**
  94599. + * This function is to handle Iso EP transfer complete interrupt
  94600. + * in case Iso out packet was dropped
  94601. + *
  94602. + * @param core_if Programming view of DWC_otg controller.
  94603. + * @param dwc_ep The EP for wihich transfer complete was asserted
  94604. + *
  94605. + */
  94606. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  94607. + dwc_ep_t * dwc_ep)
  94608. +{
  94609. + uint32_t dma_addr;
  94610. + uint32_t drp_pkt;
  94611. + uint32_t drp_pkt_cnt;
  94612. + deptsiz_data_t deptsiz = {.d32 = 0 };
  94613. + depctl_data_t depctl = {.d32 = 0 };
  94614. + int i;
  94615. +
  94616. + deptsiz.d32 =
  94617. + DWC_READ_REG32(&core_if->dev_if->
  94618. + out_ep_regs[dwc_ep->num]->doeptsiz);
  94619. +
  94620. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  94621. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  94622. +
  94623. + /* Setting dropped packets status */
  94624. + for (i = 0; i < drp_pkt_cnt; ++i) {
  94625. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  94626. + drp_pkt++;
  94627. + deptsiz.b.pktcnt--;
  94628. + }
  94629. +
  94630. + if (deptsiz.b.pktcnt > 0) {
  94631. + deptsiz.b.xfersize =
  94632. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  94633. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  94634. + } else {
  94635. + deptsiz.b.xfersize = 0;
  94636. + deptsiz.b.pktcnt = 0;
  94637. + }
  94638. +
  94639. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  94640. + deptsiz.d32);
  94641. +
  94642. + if (deptsiz.b.pktcnt > 0) {
  94643. + if (dwc_ep->proc_buf_num) {
  94644. + dma_addr =
  94645. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  94646. + deptsiz.b.xfersize;
  94647. + } else {
  94648. + dma_addr =
  94649. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  94650. + deptsiz.b.xfersize;;
  94651. + }
  94652. +
  94653. + DWC_WRITE_REG32(&core_if->dev_if->
  94654. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  94655. +
  94656. + /** Re-enable endpoint, clear nak */
  94657. + depctl.d32 = 0;
  94658. + depctl.b.epena = 1;
  94659. + depctl.b.cnak = 1;
  94660. +
  94661. + DWC_MODIFY_REG32(&core_if->dev_if->
  94662. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  94663. + depctl.d32);
  94664. + return 0;
  94665. + } else {
  94666. + return 1;
  94667. + }
  94668. +}
  94669. +
  94670. +/**
  94671. + * This function sets iso packets information(PTI mode)
  94672. + *
  94673. + * @param core_if Programming view of DWC_otg controller.
  94674. + * @param ep The EP to start the transfer on.
  94675. + *
  94676. + */
  94677. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  94678. +{
  94679. + int i, j;
  94680. + dma_addr_t dma_ad;
  94681. + iso_pkt_info_t *packet_info = ep->pkt_info;
  94682. + uint32_t offset;
  94683. + uint32_t frame_data;
  94684. + deptsiz_data_t deptsiz;
  94685. +
  94686. + if (ep->proc_buf_num == 0) {
  94687. + /** Buffer 0 descriptors setup */
  94688. + dma_ad = ep->dma_addr0;
  94689. + } else {
  94690. + /** Buffer 1 descriptors setup */
  94691. + dma_ad = ep->dma_addr1;
  94692. + }
  94693. +
  94694. + if (ep->is_in) {
  94695. + deptsiz.d32 =
  94696. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  94697. + dieptsiz);
  94698. + } else {
  94699. + deptsiz.d32 =
  94700. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  94701. + doeptsiz);
  94702. + }
  94703. +
  94704. + if (!deptsiz.b.xfersize) {
  94705. + offset = 0;
  94706. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  94707. + frame_data = ep->data_per_frame;
  94708. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  94709. +
  94710. + /* Packet status - is not set as initially
  94711. + * it is set to 0 and if packet was sent
  94712. + successfully, status field will remain 0*/
  94713. +
  94714. + /* Bytes has been transfered */
  94715. + packet_info->length =
  94716. + (ep->maxpacket <
  94717. + frame_data) ? ep->maxpacket : frame_data;
  94718. +
  94719. + /* Received packet offset */
  94720. + packet_info->offset = offset;
  94721. + offset += packet_info->length;
  94722. + frame_data -= packet_info->length;
  94723. +
  94724. + packet_info++;
  94725. + }
  94726. + }
  94727. + return 1;
  94728. + } else {
  94729. + /* This is a workaround for in case of Transfer Complete with
  94730. + * PktDrpSts interrupts merging - in this case Transfer complete
  94731. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  94732. + * set and with DOEPTSIZ register non zero. Investigations showed,
  94733. + * that this happens when Out packet is dropped, but because of
  94734. + * interrupts merging during first interrupt handling PktDrpSts
  94735. + * bit is cleared and for next merged interrupts it is not reset.
  94736. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  94737. + */
  94738. + if (ep->is_in) {
  94739. + return 1;
  94740. + } else {
  94741. + return handle_iso_out_pkt_dropped(core_if, ep);
  94742. + }
  94743. + }
  94744. +}
  94745. +
  94746. +/**
  94747. + * This function is to handle Iso EP transfer complete interrupt
  94748. + *
  94749. + * @param pcd The PCD
  94750. + * @param ep The EP for which transfer complete was asserted
  94751. + *
  94752. + */
  94753. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  94754. +{
  94755. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  94756. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  94757. + uint8_t is_last = 0;
  94758. +
  94759. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  94760. + DWC_WARN("Next frame is not set!\n");
  94761. + return;
  94762. + }
  94763. +
  94764. + if (core_if->dma_enable) {
  94765. + if (core_if->dma_desc_enable) {
  94766. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  94767. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  94768. + is_last = 1;
  94769. + } else {
  94770. + if (core_if->pti_enh_enable) {
  94771. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  94772. + dwc_ep->proc_buf_num =
  94773. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  94774. + dwc_otg_iso_ep_start_buf_transfer
  94775. + (core_if, dwc_ep);
  94776. + is_last = 1;
  94777. + }
  94778. + } else {
  94779. + set_current_pkt_info(core_if, dwc_ep);
  94780. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  94781. + is_last = 1;
  94782. + dwc_ep->cur_pkt = 0;
  94783. + dwc_ep->proc_buf_num =
  94784. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  94785. + if (dwc_ep->proc_buf_num) {
  94786. + dwc_ep->cur_pkt_addr =
  94787. + dwc_ep->xfer_buff1;
  94788. + dwc_ep->cur_pkt_dma_addr =
  94789. + dwc_ep->dma_addr1;
  94790. + } else {
  94791. + dwc_ep->cur_pkt_addr =
  94792. + dwc_ep->xfer_buff0;
  94793. + dwc_ep->cur_pkt_dma_addr =
  94794. + dwc_ep->dma_addr0;
  94795. + }
  94796. +
  94797. + }
  94798. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  94799. + dwc_ep);
  94800. + }
  94801. + }
  94802. + } else {
  94803. + set_current_pkt_info(core_if, dwc_ep);
  94804. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  94805. + is_last = 1;
  94806. + dwc_ep->cur_pkt = 0;
  94807. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  94808. + if (dwc_ep->proc_buf_num) {
  94809. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  94810. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  94811. + } else {
  94812. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  94813. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  94814. + }
  94815. +
  94816. + }
  94817. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  94818. + }
  94819. + if (is_last)
  94820. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  94821. +}
  94822. +#endif /* DWC_EN_ISOC */
  94823. +
  94824. +/**
  94825. + * This function handle BNA interrupt for Non Isochronous EPs
  94826. + *
  94827. + */
  94828. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  94829. +{
  94830. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  94831. + volatile uint32_t *addr;
  94832. + depctl_data_t depctl = {.d32 = 0 };
  94833. + dwc_otg_pcd_t *pcd = ep->pcd;
  94834. + dwc_otg_dev_dma_desc_t *dma_desc;
  94835. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  94836. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  94837. + int i, start;
  94838. +
  94839. + if (!dwc_ep->desc_cnt)
  94840. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  94841. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  94842. +
  94843. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  94844. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  94845. + uint32_t doepdma;
  94846. + dwc_otg_dev_out_ep_regs_t *out_regs =
  94847. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  94848. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  94849. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  94850. + dma_desc = &(dwc_ep->desc_addr[start]);
  94851. + } else {
  94852. + start = 0;
  94853. + dma_desc = dwc_ep->desc_addr;
  94854. + }
  94855. +
  94856. +
  94857. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  94858. + sts.d32 = dma_desc->status.d32;
  94859. + sts.b.bs = BS_HOST_READY;
  94860. + dma_desc->status.d32 = sts.d32;
  94861. + }
  94862. +
  94863. + if (dwc_ep->is_in == 0) {
  94864. + addr =
  94865. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  94866. + doepctl;
  94867. + } else {
  94868. + addr =
  94869. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  94870. + }
  94871. + depctl.b.epena = 1;
  94872. + depctl.b.cnak = 1;
  94873. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  94874. +}
  94875. +
  94876. +/**
  94877. + * This function handles EP0 Control transfers.
  94878. + *
  94879. + * The state of the control transfers are tracked in
  94880. + * <code>ep0state</code>.
  94881. + */
  94882. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  94883. +{
  94884. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  94885. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  94886. + dev_dma_desc_sts_t desc_sts;
  94887. + deptsiz0_data_t deptsiz;
  94888. + uint32_t byte_count;
  94889. +
  94890. +#ifdef DEBUG_EP0
  94891. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  94892. + print_ep0_state(pcd);
  94893. +#endif
  94894. +
  94895. +// DWC_PRINTF("HANDLE EP0\n");
  94896. +
  94897. + switch (pcd->ep0state) {
  94898. + case EP0_DISCONNECT:
  94899. + break;
  94900. +
  94901. + case EP0_IDLE:
  94902. + pcd->request_config = 0;
  94903. +
  94904. + pcd_setup(pcd);
  94905. + break;
  94906. +
  94907. + case EP0_IN_DATA_PHASE:
  94908. +#ifdef DEBUG_EP0
  94909. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  94910. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  94911. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  94912. +#endif
  94913. +
  94914. + if (core_if->dma_enable != 0) {
  94915. + /*
  94916. + * For EP0 we can only program 1 packet at a time so we
  94917. + * need to do the make calculations after each complete.
  94918. + * Call write_packet to make the calculations, as in
  94919. + * slave mode, and use those values to determine if we
  94920. + * can complete.
  94921. + */
  94922. + if (core_if->dma_desc_enable == 0) {
  94923. + deptsiz.d32 =
  94924. + DWC_READ_REG32(&core_if->
  94925. + dev_if->in_ep_regs[0]->
  94926. + dieptsiz);
  94927. + byte_count =
  94928. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  94929. + } else {
  94930. + desc_sts =
  94931. + core_if->dev_if->in_desc_addr->status;
  94932. + byte_count =
  94933. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  94934. + }
  94935. + ep0->dwc_ep.xfer_count += byte_count;
  94936. + ep0->dwc_ep.xfer_buff += byte_count;
  94937. + ep0->dwc_ep.dma_addr += byte_count;
  94938. + }
  94939. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  94940. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94941. + &ep0->dwc_ep);
  94942. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  94943. + } else if (ep0->dwc_ep.sent_zlp) {
  94944. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94945. + &ep0->dwc_ep);
  94946. + ep0->dwc_ep.sent_zlp = 0;
  94947. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  94948. + } else {
  94949. + ep0_complete_request(ep0);
  94950. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  94951. + }
  94952. + break;
  94953. + case EP0_OUT_DATA_PHASE:
  94954. +#ifdef DEBUG_EP0
  94955. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  94956. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  94957. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  94958. +#endif
  94959. + if (core_if->dma_enable != 0) {
  94960. + if (core_if->dma_desc_enable == 0) {
  94961. + deptsiz.d32 =
  94962. + DWC_READ_REG32(&core_if->
  94963. + dev_if->out_ep_regs[0]->
  94964. + doeptsiz);
  94965. + byte_count =
  94966. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  94967. + } else {
  94968. + desc_sts =
  94969. + core_if->dev_if->out_desc_addr->status;
  94970. + byte_count =
  94971. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  94972. + }
  94973. + ep0->dwc_ep.xfer_count += byte_count;
  94974. + ep0->dwc_ep.xfer_buff += byte_count;
  94975. + ep0->dwc_ep.dma_addr += byte_count;
  94976. + }
  94977. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  94978. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94979. + &ep0->dwc_ep);
  94980. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  94981. + } else if (ep0->dwc_ep.sent_zlp) {
  94982. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  94983. + &ep0->dwc_ep);
  94984. + ep0->dwc_ep.sent_zlp = 0;
  94985. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  94986. + } else {
  94987. + ep0_complete_request(ep0);
  94988. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  94989. + }
  94990. + break;
  94991. +
  94992. + case EP0_IN_STATUS_PHASE:
  94993. + case EP0_OUT_STATUS_PHASE:
  94994. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  94995. + ep0_complete_request(ep0);
  94996. + pcd->ep0state = EP0_IDLE;
  94997. + ep0->stopped = 1;
  94998. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  94999. +
  95000. + /* Prepare for more SETUP Packets */
  95001. + if (core_if->dma_enable) {
  95002. + ep0_out_start(core_if, pcd);
  95003. + }
  95004. + break;
  95005. +
  95006. + case EP0_STALL:
  95007. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  95008. + break;
  95009. + }
  95010. +#ifdef DEBUG_EP0
  95011. + print_ep0_state(pcd);
  95012. +#endif
  95013. +}
  95014. +
  95015. +/**
  95016. + * Restart transfer
  95017. + */
  95018. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  95019. +{
  95020. + dwc_otg_core_if_t *core_if;
  95021. + dwc_otg_dev_if_t *dev_if;
  95022. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  95023. + dwc_otg_pcd_ep_t *ep;
  95024. +
  95025. + ep = get_in_ep(pcd, epnum);
  95026. +
  95027. +#ifdef DWC_EN_ISOC
  95028. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  95029. + return;
  95030. + }
  95031. +#endif /* DWC_EN_ISOC */
  95032. +
  95033. + core_if = GET_CORE_IF(pcd);
  95034. + dev_if = core_if->dev_if;
  95035. +
  95036. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  95037. +
  95038. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  95039. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  95040. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  95041. + /*
  95042. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  95043. + */
  95044. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  95045. + ep->dwc_ep.start_xfer_buff != 0) {
  95046. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  95047. + ep->dwc_ep.xfer_count = 0;
  95048. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  95049. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  95050. + } else {
  95051. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  95052. + /* convert packet size to dwords. */
  95053. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  95054. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  95055. + }
  95056. + ep->stopped = 0;
  95057. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  95058. + "xfer_len=%0x stopped=%d\n",
  95059. + ep->dwc_ep.xfer_buff,
  95060. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  95061. + ep->stopped);
  95062. + if (epnum == 0) {
  95063. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  95064. + } else {
  95065. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  95066. + }
  95067. + }
  95068. +}
  95069. +
  95070. +/*
  95071. + * This function create new nextep sequnce based on Learn Queue.
  95072. + *
  95073. + * @param core_if Programming view of DWC_otg controller
  95074. + */
  95075. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  95076. +{
  95077. + dwc_otg_device_global_regs_t *dev_global_regs =
  95078. + core_if->dev_if->dev_global_regs;
  95079. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  95080. + /* Number of Token Queue Registers */
  95081. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  95082. + dtknq1_data_t dtknqr1;
  95083. + uint32_t in_tkn_epnums[4];
  95084. + uint8_t seqnum[MAX_EPS_CHANNELS];
  95085. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  95086. + grstctl_t resetctl = {.d32 = 0 };
  95087. + uint8_t temp;
  95088. + int ndx = 0;
  95089. + int start = 0;
  95090. + int end = 0;
  95091. + int sort_done = 0;
  95092. + int i = 0;
  95093. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  95094. +
  95095. +
  95096. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  95097. +
  95098. + /* Read the DTKNQ Registers */
  95099. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  95100. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  95101. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  95102. + in_tkn_epnums[i]);
  95103. + if (addr == &dev_global_regs->dvbusdis) {
  95104. + addr = &dev_global_regs->dtknqr3_dthrctl;
  95105. + } else {
  95106. + ++addr;
  95107. + }
  95108. +
  95109. + }
  95110. +
  95111. + /* Copy the DTKNQR1 data to the bit field. */
  95112. + dtknqr1.d32 = in_tkn_epnums[0];
  95113. + if (dtknqr1.b.wrap_bit) {
  95114. + ndx = dtknqr1.b.intknwptr;
  95115. + end = ndx -1;
  95116. + if (end < 0)
  95117. + end = TOKEN_Q_DEPTH -1;
  95118. + } else {
  95119. + ndx = 0;
  95120. + end = dtknqr1.b.intknwptr -1;
  95121. + if (end < 0)
  95122. + end = 0;
  95123. + }
  95124. + start = ndx;
  95125. +
  95126. + /* Fill seqnum[] by initial values: EP number + 31 */
  95127. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  95128. + seqnum[i] = i +31;
  95129. + }
  95130. +
  95131. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  95132. + for (i=0; i < 6; i++)
  95133. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  95134. +
  95135. + if (TOKEN_Q_DEPTH > 6) {
  95136. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  95137. + for (i=6; i < 14; i++)
  95138. + intkn_seq[i] =
  95139. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  95140. + }
  95141. +
  95142. + if (TOKEN_Q_DEPTH > 14) {
  95143. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  95144. + for (i=14; i < 22; i++)
  95145. + intkn_seq[i] =
  95146. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  95147. + }
  95148. +
  95149. + if (TOKEN_Q_DEPTH > 22) {
  95150. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  95151. + for (i=22; i < 30; i++)
  95152. + intkn_seq[i] =
  95153. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  95154. + }
  95155. +
  95156. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  95157. + start, end);
  95158. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  95159. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  95160. +
  95161. + /* Update seqnum based on intkn_seq[] */
  95162. + i = 0;
  95163. + do {
  95164. + seqnum[intkn_seq[ndx]] = i;
  95165. + ndx++;
  95166. + i++;
  95167. + if (ndx == TOKEN_Q_DEPTH)
  95168. + ndx = 0;
  95169. + } while ( i < TOKEN_Q_DEPTH );
  95170. +
  95171. + /* Mark non active EP's in seqnum[] by 0xff */
  95172. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  95173. + if (core_if->nextep_seq[i] == 0xff )
  95174. + seqnum[i] = 0xff;
  95175. + }
  95176. +
  95177. + /* Sort seqnum[] */
  95178. + sort_done = 0;
  95179. + while (!sort_done) {
  95180. + sort_done = 1;
  95181. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  95182. + if (seqnum[i] > seqnum[i+1]) {
  95183. + temp = seqnum[i];
  95184. + seqnum[i] = seqnum[i+1];
  95185. + seqnum[i+1] = temp;
  95186. + sort_done = 0;
  95187. + }
  95188. + }
  95189. + }
  95190. +
  95191. + ndx = start + seqnum[0];
  95192. + if (ndx >= TOKEN_Q_DEPTH)
  95193. + ndx = ndx % TOKEN_Q_DEPTH;
  95194. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  95195. +
  95196. + /* Update seqnum[] by EP numbers */
  95197. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  95198. + ndx = start + i;
  95199. + if (seqnum[i] < 31) {
  95200. + ndx = start + seqnum[i];
  95201. + if (ndx >= TOKEN_Q_DEPTH)
  95202. + ndx = ndx % TOKEN_Q_DEPTH;
  95203. + seqnum[i] = intkn_seq[ndx];
  95204. + } else {
  95205. + if (seqnum[i] < 0xff) {
  95206. + seqnum[i] = seqnum[i] - 31;
  95207. + } else {
  95208. + break;
  95209. + }
  95210. + }
  95211. + }
  95212. +
  95213. + /* Update nextep_seq[] based on seqnum[] */
  95214. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  95215. + if (seqnum[i] != 0xff) {
  95216. + if (seqnum[i+1] != 0xff) {
  95217. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  95218. + } else {
  95219. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  95220. + break;
  95221. + }
  95222. + } else {
  95223. + break;
  95224. + }
  95225. + }
  95226. +
  95227. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  95228. + __func__, core_if->first_in_nextep_seq);
  95229. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  95230. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  95231. + }
  95232. +
  95233. + /* Flush the Learning Queue */
  95234. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  95235. + resetctl.b.intknqflsh = 1;
  95236. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  95237. +
  95238. +
  95239. +}
  95240. +
  95241. +/**
  95242. + * handle the IN EP disable interrupt.
  95243. + */
  95244. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  95245. + const uint32_t epnum)
  95246. +{
  95247. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  95248. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  95249. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  95250. + dctl_data_t dctl = {.d32 = 0 };
  95251. + dwc_otg_pcd_ep_t *ep;
  95252. + dwc_ep_t *dwc_ep;
  95253. + gintmsk_data_t gintmsk_data;
  95254. + depctl_data_t depctl;
  95255. + uint32_t diepdma;
  95256. + uint32_t remain_to_transfer = 0;
  95257. + uint8_t i;
  95258. + uint32_t xfer_size;
  95259. +
  95260. + ep = get_in_ep(pcd, epnum);
  95261. + dwc_ep = &ep->dwc_ep;
  95262. +
  95263. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  95264. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  95265. + complete_ep(ep);
  95266. + return;
  95267. + }
  95268. +
  95269. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  95270. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  95271. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  95272. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  95273. +
  95274. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  95275. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  95276. +
  95277. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  95278. + if (ep->stopped) {
  95279. + if (core_if->en_multiple_tx_fifo)
  95280. + /* Flush the Tx FIFO */
  95281. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  95282. + /* Clear the Global IN NP NAK */
  95283. + dctl.d32 = 0;
  95284. + dctl.b.cgnpinnak = 1;
  95285. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  95286. + /* Restart the transaction */
  95287. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  95288. + restart_transfer(pcd, epnum);
  95289. + }
  95290. + } else {
  95291. + /* Restart the transaction */
  95292. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  95293. + restart_transfer(pcd, epnum);
  95294. + }
  95295. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  95296. + }
  95297. + return;
  95298. + }
  95299. +
  95300. + if (core_if->start_predict > 2) { // NP IN EP
  95301. + core_if->start_predict--;
  95302. + return;
  95303. + }
  95304. +
  95305. + core_if->start_predict--;
  95306. +
  95307. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  95308. +
  95309. + predict_nextep_seq(core_if);
  95310. +
  95311. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  95312. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  95313. + depctl.d32 =
  95314. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95315. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  95316. + depctl.b.nextep = core_if->nextep_seq[i];
  95317. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  95318. + }
  95319. + }
  95320. + /* Flush Shared NP TxFIFO */
  95321. + dwc_otg_flush_tx_fifo(core_if, 0);
  95322. + /* Rewind buffers */
  95323. + if (!core_if->dma_desc_enable) {
  95324. + i = core_if->first_in_nextep_seq;
  95325. + do {
  95326. + ep = get_in_ep(pcd, i);
  95327. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  95328. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  95329. + if (xfer_size > ep->dwc_ep.maxxfer)
  95330. + xfer_size = ep->dwc_ep.maxxfer;
  95331. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95332. + if (dieptsiz.b.pktcnt != 0) {
  95333. + if (xfer_size == 0) {
  95334. + remain_to_transfer = 0;
  95335. + } else {
  95336. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  95337. + remain_to_transfer =
  95338. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  95339. + } else {
  95340. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  95341. + + (xfer_size % ep->dwc_ep.maxpacket);
  95342. + }
  95343. + }
  95344. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  95345. + dieptsiz.b.xfersize = remain_to_transfer;
  95346. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  95347. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  95348. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  95349. + }
  95350. + i = core_if->nextep_seq[i];
  95351. + } while (i != core_if->first_in_nextep_seq);
  95352. + } else { // dma_desc_enable
  95353. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  95354. + }
  95355. +
  95356. + /* Restart transfers in predicted sequences */
  95357. + i = core_if->first_in_nextep_seq;
  95358. + do {
  95359. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  95360. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95361. + if (dieptsiz.b.pktcnt != 0) {
  95362. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  95363. + depctl.b.epena = 1;
  95364. + depctl.b.cnak = 1;
  95365. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  95366. + }
  95367. + i = core_if->nextep_seq[i];
  95368. + } while (i != core_if->first_in_nextep_seq);
  95369. +
  95370. + /* Clear the global non-periodic IN NAK handshake */
  95371. + dctl.d32 = 0;
  95372. + dctl.b.cgnpinnak = 1;
  95373. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  95374. +
  95375. + /* Unmask EP Mismatch interrupt */
  95376. + gintmsk_data.d32 = 0;
  95377. + gintmsk_data.b.epmismatch = 1;
  95378. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  95379. +
  95380. + core_if->start_predict = 0;
  95381. +
  95382. + }
  95383. +}
  95384. +
  95385. +/**
  95386. + * Handler for the IN EP timeout handshake interrupt.
  95387. + */
  95388. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  95389. + const uint32_t epnum)
  95390. +{
  95391. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  95392. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  95393. +
  95394. +#ifdef DEBUG
  95395. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  95396. + uint32_t num = 0;
  95397. +#endif
  95398. + dctl_data_t dctl = {.d32 = 0 };
  95399. + dwc_otg_pcd_ep_t *ep;
  95400. +
  95401. + gintmsk_data_t intr_mask = {.d32 = 0 };
  95402. +
  95403. + ep = get_in_ep(pcd, epnum);
  95404. +
  95405. + /* Disable the NP Tx Fifo Empty Interrrupt */
  95406. + if (!core_if->dma_enable) {
  95407. + intr_mask.b.nptxfempty = 1;
  95408. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  95409. + intr_mask.d32, 0);
  95410. + }
  95411. + /** @todo NGS Check EP type.
  95412. + * Implement for Periodic EPs */
  95413. + /*
  95414. + * Non-periodic EP
  95415. + */
  95416. + /* Enable the Global IN NAK Effective Interrupt */
  95417. + intr_mask.b.ginnakeff = 1;
  95418. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  95419. +
  95420. + /* Set Global IN NAK */
  95421. + dctl.b.sgnpinnak = 1;
  95422. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  95423. +
  95424. + ep->stopped = 1;
  95425. +
  95426. +#ifdef DEBUG
  95427. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  95428. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  95429. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  95430. +#endif
  95431. +
  95432. +#ifdef DISABLE_PERIODIC_EP
  95433. + /*
  95434. + * Set the NAK bit for this EP to
  95435. + * start the disable process.
  95436. + */
  95437. + diepctl.d32 = 0;
  95438. + diepctl.b.snak = 1;
  95439. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  95440. + diepctl.d32);
  95441. + ep->disabling = 1;
  95442. + ep->stopped = 1;
  95443. +#endif
  95444. +}
  95445. +
  95446. +/**
  95447. + * Handler for the IN EP NAK interrupt.
  95448. + */
  95449. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  95450. + const uint32_t epnum)
  95451. +{
  95452. + /** @todo implement ISR */
  95453. + dwc_otg_core_if_t *core_if;
  95454. + diepmsk_data_t intr_mask = {.d32 = 0 };
  95455. +
  95456. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  95457. + core_if = GET_CORE_IF(pcd);
  95458. + intr_mask.b.nak = 1;
  95459. +
  95460. + if (core_if->multiproc_int_enable) {
  95461. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  95462. + diepeachintmsk[epnum], intr_mask.d32, 0);
  95463. + } else {
  95464. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  95465. + intr_mask.d32, 0);
  95466. + }
  95467. +
  95468. + return 1;
  95469. +}
  95470. +
  95471. +/**
  95472. + * Handler for the OUT EP Babble interrupt.
  95473. + */
  95474. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  95475. + const uint32_t epnum)
  95476. +{
  95477. + /** @todo implement ISR */
  95478. + dwc_otg_core_if_t *core_if;
  95479. + doepmsk_data_t intr_mask = {.d32 = 0 };
  95480. +
  95481. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  95482. + "OUT EP Babble");
  95483. + core_if = GET_CORE_IF(pcd);
  95484. + intr_mask.b.babble = 1;
  95485. +
  95486. + if (core_if->multiproc_int_enable) {
  95487. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  95488. + doepeachintmsk[epnum], intr_mask.d32, 0);
  95489. + } else {
  95490. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  95491. + intr_mask.d32, 0);
  95492. + }
  95493. +
  95494. + return 1;
  95495. +}
  95496. +
  95497. +/**
  95498. + * Handler for the OUT EP NAK interrupt.
  95499. + */
  95500. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  95501. + const uint32_t epnum)
  95502. +{
  95503. + /** @todo implement ISR */
  95504. + dwc_otg_core_if_t *core_if;
  95505. + doepmsk_data_t intr_mask = {.d32 = 0 };
  95506. +
  95507. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  95508. + core_if = GET_CORE_IF(pcd);
  95509. + intr_mask.b.nak = 1;
  95510. +
  95511. + if (core_if->multiproc_int_enable) {
  95512. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  95513. + doepeachintmsk[epnum], intr_mask.d32, 0);
  95514. + } else {
  95515. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  95516. + intr_mask.d32, 0);
  95517. + }
  95518. +
  95519. + return 1;
  95520. +}
  95521. +
  95522. +/**
  95523. + * Handler for the OUT EP NYET interrupt.
  95524. + */
  95525. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  95526. + const uint32_t epnum)
  95527. +{
  95528. + /** @todo implement ISR */
  95529. + dwc_otg_core_if_t *core_if;
  95530. + doepmsk_data_t intr_mask = {.d32 = 0 };
  95531. +
  95532. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  95533. + core_if = GET_CORE_IF(pcd);
  95534. + intr_mask.b.nyet = 1;
  95535. +
  95536. + if (core_if->multiproc_int_enable) {
  95537. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  95538. + doepeachintmsk[epnum], intr_mask.d32, 0);
  95539. + } else {
  95540. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  95541. + intr_mask.d32, 0);
  95542. + }
  95543. +
  95544. + return 1;
  95545. +}
  95546. +
  95547. +/**
  95548. + * This interrupt indicates that an IN EP has a pending Interrupt.
  95549. + * The sequence for handling the IN EP interrupt is shown below:
  95550. + * -# Read the Device All Endpoint Interrupt register
  95551. + * -# Repeat the following for each IN EP interrupt bit set (from
  95552. + * LSB to MSB).
  95553. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  95554. + * -# If "Transfer Complete" call the request complete function
  95555. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  95556. + * -# If "AHB Error Interrupt" log error
  95557. + * -# If "Time-out Handshake" log error
  95558. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  95559. + * FIFO.
  95560. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  95561. + * Mismatch Interrupt)
  95562. + */
  95563. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  95564. +{
  95565. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  95566. +do { \
  95567. + diepint_data_t diepint = {.d32=0}; \
  95568. + diepint.b.__intr = 1; \
  95569. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  95570. + diepint.d32); \
  95571. +} while (0)
  95572. +
  95573. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  95574. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  95575. + diepint_data_t diepint = {.d32 = 0 };
  95576. + depctl_data_t depctl = {.d32 = 0 };
  95577. + uint32_t ep_intr;
  95578. + uint32_t epnum = 0;
  95579. + dwc_otg_pcd_ep_t *ep;
  95580. + dwc_ep_t *dwc_ep;
  95581. + gintmsk_data_t intr_mask = {.d32 = 0 };
  95582. +
  95583. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  95584. +
  95585. + /* Read in the device interrupt bits */
  95586. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  95587. +
  95588. + /* Service the Device IN interrupts for each endpoint */
  95589. + while (ep_intr) {
  95590. + if (ep_intr & 0x1) {
  95591. + uint32_t empty_msk;
  95592. + /* Get EP pointer */
  95593. + ep = get_in_ep(pcd, epnum);
  95594. + dwc_ep = &ep->dwc_ep;
  95595. +
  95596. + depctl.d32 =
  95597. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  95598. + empty_msk =
  95599. + DWC_READ_REG32(&dev_if->
  95600. + dev_global_regs->dtknqr4_fifoemptymsk);
  95601. +
  95602. + DWC_DEBUGPL(DBG_PCDV,
  95603. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  95604. + epnum, empty_msk, depctl.d32);
  95605. +
  95606. + DWC_DEBUGPL(DBG_PCD,
  95607. + "EP%d-%s: type=%d, mps=%d\n",
  95608. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  95609. + dwc_ep->type, dwc_ep->maxpacket);
  95610. +
  95611. + diepint.d32 =
  95612. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  95613. +
  95614. + DWC_DEBUGPL(DBG_PCDV,
  95615. + "EP %d Interrupt Register - 0x%x\n", epnum,
  95616. + diepint.d32);
  95617. + /* Transfer complete */
  95618. + if (diepint.b.xfercompl) {
  95619. + /* Disable the NP Tx FIFO Empty
  95620. + * Interrupt */
  95621. + if (core_if->en_multiple_tx_fifo == 0) {
  95622. + intr_mask.b.nptxfempty = 1;
  95623. + DWC_MODIFY_REG32
  95624. + (&core_if->core_global_regs->gintmsk,
  95625. + intr_mask.d32, 0);
  95626. + } else {
  95627. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  95628. + uint32_t fifoemptymsk =
  95629. + 0x1 << dwc_ep->num;
  95630. + DWC_MODIFY_REG32(&core_if->
  95631. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  95632. + fifoemptymsk, 0);
  95633. + }
  95634. + /* Clear the bit in DIEPINTn for this interrupt */
  95635. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  95636. +
  95637. + /* Complete the transfer */
  95638. + if (epnum == 0) {
  95639. + handle_ep0(pcd);
  95640. + }
  95641. +#ifdef DWC_EN_ISOC
  95642. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  95643. + if (!ep->stopped)
  95644. + complete_iso_ep(pcd, ep);
  95645. + }
  95646. +#endif /* DWC_EN_ISOC */
  95647. +#ifdef DWC_UTE_PER_IO
  95648. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  95649. + if (!ep->stopped)
  95650. + complete_xiso_ep(ep);
  95651. + }
  95652. +#endif /* DWC_UTE_PER_IO */
  95653. + else {
  95654. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  95655. + dwc_ep->bInterval > 1) {
  95656. + dwc_ep->frame_num += dwc_ep->bInterval;
  95657. + if (dwc_ep->frame_num > 0x3FFF)
  95658. + {
  95659. + dwc_ep->frm_overrun = 1;
  95660. + dwc_ep->frame_num &= 0x3FFF;
  95661. + } else
  95662. + dwc_ep->frm_overrun = 0;
  95663. + }
  95664. + complete_ep(ep);
  95665. + if(diepint.b.nak)
  95666. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  95667. + }
  95668. + }
  95669. + /* Endpoint disable */
  95670. + if (diepint.b.epdisabled) {
  95671. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  95672. + epnum);
  95673. + handle_in_ep_disable_intr(pcd, epnum);
  95674. +
  95675. + /* Clear the bit in DIEPINTn for this interrupt */
  95676. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  95677. + }
  95678. + /* AHB Error */
  95679. + if (diepint.b.ahberr) {
  95680. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  95681. + /* Clear the bit in DIEPINTn for this interrupt */
  95682. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  95683. + }
  95684. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  95685. + if (diepint.b.timeout) {
  95686. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  95687. + handle_in_ep_timeout_intr(pcd, epnum);
  95688. +
  95689. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  95690. + }
  95691. + /** IN Token received with TxF Empty */
  95692. + if (diepint.b.intktxfemp) {
  95693. + DWC_DEBUGPL(DBG_ANY,
  95694. + "EP%d IN TKN TxFifo Empty\n",
  95695. + epnum);
  95696. + if (!ep->stopped && epnum != 0) {
  95697. +
  95698. + diepmsk_data_t diepmsk = {.d32 = 0 };
  95699. + diepmsk.b.intktxfemp = 1;
  95700. +
  95701. + if (core_if->multiproc_int_enable) {
  95702. + DWC_MODIFY_REG32
  95703. + (&dev_if->dev_global_regs->diepeachintmsk
  95704. + [epnum], diepmsk.d32, 0);
  95705. + } else {
  95706. + DWC_MODIFY_REG32
  95707. + (&dev_if->dev_global_regs->diepmsk,
  95708. + diepmsk.d32, 0);
  95709. + }
  95710. + } else if (core_if->dma_desc_enable
  95711. + && epnum == 0
  95712. + && pcd->ep0state ==
  95713. + EP0_OUT_STATUS_PHASE) {
  95714. + // EP0 IN set STALL
  95715. + depctl.d32 =
  95716. + DWC_READ_REG32(&dev_if->in_ep_regs
  95717. + [epnum]->diepctl);
  95718. +
  95719. + /* set the disable and stall bits */
  95720. + if (depctl.b.epena) {
  95721. + depctl.b.epdis = 1;
  95722. + }
  95723. + depctl.b.stall = 1;
  95724. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  95725. + [epnum]->diepctl,
  95726. + depctl.d32);
  95727. + }
  95728. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  95729. + }
  95730. + /** IN Token Received with EP mismatch */
  95731. + if (diepint.b.intknepmis) {
  95732. + DWC_DEBUGPL(DBG_ANY,
  95733. + "EP%d IN TKN EP Mismatch\n", epnum);
  95734. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  95735. + }
  95736. + /** IN Endpoint NAK Effective */
  95737. + if (diepint.b.inepnakeff) {
  95738. + DWC_DEBUGPL(DBG_ANY,
  95739. + "EP%d IN EP NAK Effective\n",
  95740. + epnum);
  95741. + /* Periodic EP */
  95742. + if (ep->disabling) {
  95743. + depctl.d32 = 0;
  95744. + depctl.b.snak = 1;
  95745. + depctl.b.epdis = 1;
  95746. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  95747. + [epnum]->diepctl,
  95748. + depctl.d32,
  95749. + depctl.d32);
  95750. + }
  95751. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  95752. +
  95753. + }
  95754. +
  95755. + /** IN EP Tx FIFO Empty Intr */
  95756. + if (diepint.b.emptyintr) {
  95757. + DWC_DEBUGPL(DBG_ANY,
  95758. + "EP%d Tx FIFO Empty Intr \n",
  95759. + epnum);
  95760. + write_empty_tx_fifo(pcd, epnum);
  95761. +
  95762. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  95763. +
  95764. + }
  95765. +
  95766. + /** IN EP BNA Intr */
  95767. + if (diepint.b.bna) {
  95768. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  95769. + if (core_if->dma_desc_enable) {
  95770. +#ifdef DWC_EN_ISOC
  95771. + if (dwc_ep->type ==
  95772. + DWC_OTG_EP_TYPE_ISOC) {
  95773. + /*
  95774. + * This checking is performed to prevent first "false" BNA
  95775. + * handling occuring right after reconnect
  95776. + */
  95777. + if (dwc_ep->next_frame !=
  95778. + 0xffffffff)
  95779. + dwc_otg_pcd_handle_iso_bna(ep);
  95780. + } else
  95781. +#endif /* DWC_EN_ISOC */
  95782. + {
  95783. + dwc_otg_pcd_handle_noniso_bna(ep);
  95784. + }
  95785. + }
  95786. + }
  95787. + /* NAK Interrutp */
  95788. + if (diepint.b.nak) {
  95789. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  95790. + epnum);
  95791. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  95792. + depctl_data_t depctl;
  95793. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  95794. + ep->dwc_ep.frame_num = core_if->frame_num;
  95795. + if (ep->dwc_ep.bInterval > 1) {
  95796. + depctl.d32 = 0;
  95797. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  95798. + if (ep->dwc_ep.frame_num & 0x1) {
  95799. + depctl.b.setd1pid = 1;
  95800. + depctl.b.setd0pid = 0;
  95801. + } else {
  95802. + depctl.b.setd0pid = 1;
  95803. + depctl.b.setd1pid = 0;
  95804. + }
  95805. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  95806. + }
  95807. + start_next_request(ep);
  95808. + }
  95809. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  95810. + if (dwc_ep->frame_num > 0x3FFF) {
  95811. + dwc_ep->frm_overrun = 1;
  95812. + dwc_ep->frame_num &= 0x3FFF;
  95813. + } else
  95814. + dwc_ep->frm_overrun = 0;
  95815. + }
  95816. +
  95817. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  95818. + }
  95819. + }
  95820. + epnum++;
  95821. + ep_intr >>= 1;
  95822. + }
  95823. +
  95824. + return 1;
  95825. +#undef CLEAR_IN_EP_INTR
  95826. +}
  95827. +
  95828. +/**
  95829. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  95830. + * The sequence for handling the OUT EP interrupt is shown below:
  95831. + * -# Read the Device All Endpoint Interrupt register
  95832. + * -# Repeat the following for each OUT EP interrupt bit set (from
  95833. + * LSB to MSB).
  95834. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  95835. + * -# If "Transfer Complete" call the request complete function
  95836. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  95837. + * -# If "AHB Error Interrupt" log error
  95838. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  95839. + * Command Processing)
  95840. + */
  95841. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  95842. +{
  95843. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  95844. +do { \
  95845. + doepint_data_t doepint = {.d32=0}; \
  95846. + doepint.b.__intr = 1; \
  95847. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  95848. + doepint.d32); \
  95849. +} while (0)
  95850. +
  95851. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  95852. + uint32_t ep_intr;
  95853. + doepint_data_t doepint = {.d32 = 0 };
  95854. + uint32_t epnum = 0;
  95855. + dwc_otg_pcd_ep_t *ep;
  95856. + dwc_ep_t *dwc_ep;
  95857. + dctl_data_t dctl = {.d32 = 0 };
  95858. + gintmsk_data_t gintmsk = {.d32 = 0 };
  95859. +
  95860. +
  95861. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  95862. +
  95863. + /* Read in the device interrupt bits */
  95864. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  95865. +
  95866. + while (ep_intr) {
  95867. + if (ep_intr & 0x1) {
  95868. + /* Get EP pointer */
  95869. + ep = get_out_ep(pcd, epnum);
  95870. + dwc_ep = &ep->dwc_ep;
  95871. +
  95872. +#ifdef VERBOSE
  95873. + DWC_DEBUGPL(DBG_PCDV,
  95874. + "EP%d-%s: type=%d, mps=%d\n",
  95875. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  95876. + dwc_ep->type, dwc_ep->maxpacket);
  95877. +#endif
  95878. + doepint.d32 =
  95879. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  95880. + /* Moved this interrupt upper due to core deffect of asserting
  95881. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  95882. + if (doepint.b.stsphsercvd) {
  95883. + deptsiz0_data_t deptsiz;
  95884. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  95885. + deptsiz.d32 =
  95886. + DWC_READ_REG32(&core_if->dev_if->
  95887. + out_ep_regs[0]->doeptsiz);
  95888. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  95889. + && core_if->dma_enable
  95890. + && core_if->dma_desc_enable == 0
  95891. + && doepint.b.xfercompl
  95892. + && deptsiz.b.xfersize == 24) {
  95893. + CLEAR_OUT_EP_INTR(core_if, epnum,
  95894. + xfercompl);
  95895. + doepint.b.xfercompl = 0;
  95896. + ep0_out_start(core_if, pcd);
  95897. + }
  95898. + if ((core_if->dma_desc_enable) ||
  95899. + (core_if->dma_enable
  95900. + && core_if->snpsid >=
  95901. + OTG_CORE_REV_3_00a)) {
  95902. + do_setup_in_status_phase(pcd);
  95903. + }
  95904. + }
  95905. + /* Transfer complete */
  95906. + if (doepint.b.xfercompl) {
  95907. +
  95908. + if (epnum == 0) {
  95909. + /* Clear the bit in DOEPINTn for this interrupt */
  95910. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  95911. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  95912. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  95913. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  95914. + doepint.d32);
  95915. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  95916. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  95917. +
  95918. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  95919. + && core_if->dma_enable == 0) {
  95920. + doepint_data_t doepint;
  95921. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  95922. + out_ep_regs[0]->doepint);
  95923. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  95924. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  95925. + goto exit_xfercompl;
  95926. + }
  95927. + }
  95928. + /* In case of DDMA look at SR bit to go to the Data Stage */
  95929. + if (core_if->dma_desc_enable) {
  95930. + dev_dma_desc_sts_t status = {.d32 = 0};
  95931. + if (pcd->ep0state == EP0_IDLE) {
  95932. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  95933. + dev_if->setup_desc_index]->status.d32;
  95934. + if(pcd->data_terminated) {
  95935. + pcd->data_terminated = 0;
  95936. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  95937. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  95938. + }
  95939. + if (status.b.sr) {
  95940. + if (doepint.b.setup) {
  95941. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  95942. + /* Already started data stage, clear setup */
  95943. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  95944. + doepint.b.setup = 0;
  95945. + handle_ep0(pcd);
  95946. + /* Prepare for more setup packets */
  95947. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  95948. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  95949. + ep0_out_start(core_if, pcd);
  95950. + }
  95951. +
  95952. + goto exit_xfercompl;
  95953. + } else {
  95954. + /* Prepare for more setup packets */
  95955. + DWC_DEBUGPL(DBG_PCDV,
  95956. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  95957. + ep0_out_start(core_if, pcd);
  95958. + }
  95959. + }
  95960. + } else {
  95961. + dwc_otg_pcd_request_t *req;
  95962. + dev_dma_desc_sts_t status = {.d32 = 0};
  95963. + diepint_data_t diepint0;
  95964. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  95965. + in_ep_regs[0]->diepint);
  95966. +
  95967. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  95968. + DWC_ERROR("EP0 is stalled/disconnected\n");
  95969. + }
  95970. +
  95971. + /* Clear IN xfercompl if set */
  95972. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  95973. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  95974. + DWC_WRITE_REG32(&core_if->dev_if->
  95975. + in_ep_regs[0]->diepint, diepint0.d32);
  95976. + }
  95977. +
  95978. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  95979. + dev_if->setup_desc_index]->status.d32;
  95980. +
  95981. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  95982. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  95983. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  95984. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  95985. + status.d32 = core_if->dev_if->
  95986. + out_desc_addr->status.d32;
  95987. +
  95988. + if (status.b.sr) {
  95989. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  95990. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  95991. + } else {
  95992. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  95993. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  95994. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  95995. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  95996. + /* Read arrived setup packet from req->buf */
  95997. + dwc_memcpy(&pcd->setup_pkt->req,
  95998. + req->buf + ep->dwc_ep.xfer_count, 8);
  95999. + }
  96000. + req->actual = ep->dwc_ep.xfer_count;
  96001. + dwc_otg_request_done(ep, req, -ECONNRESET);
  96002. + ep->dwc_ep.start_xfer_buff = 0;
  96003. + ep->dwc_ep.xfer_buff = 0;
  96004. + ep->dwc_ep.xfer_len = 0;
  96005. + }
  96006. + pcd->ep0state = EP0_IDLE;
  96007. + if (doepint.b.setup) {
  96008. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  96009. + /* Data stage started, clear setup */
  96010. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  96011. + doepint.b.setup = 0;
  96012. + handle_ep0(pcd);
  96013. + /* Prepare for setup packets if ep0in was enabled*/
  96014. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  96015. + ep0_out_start(core_if, pcd);
  96016. + }
  96017. +
  96018. + goto exit_xfercompl;
  96019. + } else {
  96020. + /* Prepare for more setup packets */
  96021. + DWC_DEBUGPL(DBG_PCDV,
  96022. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  96023. + ep0_out_start(core_if, pcd);
  96024. + }
  96025. + }
  96026. + }
  96027. + }
  96028. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  96029. + && core_if->dma_desc_enable == 0) {
  96030. + doepint_data_t doepint_temp = {.d32 = 0};
  96031. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  96032. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  96033. + out_ep_regs[ep->dwc_ep.num]->doepint);
  96034. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  96035. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  96036. + if (pcd->ep0state == EP0_IDLE) {
  96037. + if (doepint_temp.b.sr) {
  96038. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  96039. + }
  96040. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  96041. + out_ep_regs[0]->doepint);
  96042. + if (doeptsize0.b.supcnt == 3) {
  96043. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  96044. + ep->dwc_ep.stp_rollover = 1;
  96045. + }
  96046. + if (doepint.b.setup) {
  96047. +retry:
  96048. + /* Already started data stage, clear setup */
  96049. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  96050. + doepint.b.setup = 0;
  96051. + handle_ep0(pcd);
  96052. + ep->dwc_ep.stp_rollover = 0;
  96053. + /* Prepare for more setup packets */
  96054. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  96055. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  96056. + ep0_out_start(core_if, pcd);
  96057. + }
  96058. + goto exit_xfercompl;
  96059. + } else {
  96060. + /* Prepare for more setup packets */
  96061. + DWC_DEBUGPL(DBG_ANY,
  96062. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  96063. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  96064. + out_ep_regs[0]->doepint);
  96065. + if(doepint.b.setup)
  96066. + goto retry;
  96067. + ep0_out_start(core_if, pcd);
  96068. + }
  96069. + } else {
  96070. + dwc_otg_pcd_request_t *req;
  96071. + diepint_data_t diepint0 = {.d32 = 0};
  96072. + doepint_data_t doepint_temp = {.d32 = 0};
  96073. + depctl_data_t diepctl0;
  96074. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  96075. + in_ep_regs[0]->diepint);
  96076. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  96077. + in_ep_regs[0]->diepctl);
  96078. +
  96079. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  96080. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  96081. + if (diepint0.b.xfercompl) {
  96082. + DWC_WRITE_REG32(&core_if->dev_if->
  96083. + in_ep_regs[0]->diepint, diepint0.d32);
  96084. + }
  96085. + if (diepctl0.b.epena) {
  96086. + diepint_data_t diepint = {.d32 = 0};
  96087. + diepctl0.b.snak = 1;
  96088. + DWC_WRITE_REG32(&core_if->dev_if->
  96089. + in_ep_regs[0]->diepctl, diepctl0.d32);
  96090. + do {
  96091. + dwc_udelay(10);
  96092. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  96093. + in_ep_regs[0]->diepint);
  96094. + } while (!diepint.b.inepnakeff);
  96095. + diepint.b.inepnakeff = 1;
  96096. + DWC_WRITE_REG32(&core_if->dev_if->
  96097. + in_ep_regs[0]->diepint, diepint.d32);
  96098. + diepctl0.d32 = 0;
  96099. + diepctl0.b.epdis = 1;
  96100. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  96101. + diepctl0.d32);
  96102. + do {
  96103. + dwc_udelay(10);
  96104. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  96105. + in_ep_regs[0]->diepint);
  96106. + } while (!diepint.b.epdisabled);
  96107. + diepint.b.epdisabled = 1;
  96108. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  96109. + diepint.d32);
  96110. + }
  96111. + }
  96112. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  96113. + out_ep_regs[ep->dwc_ep.num]->doepint);
  96114. + if (doepint_temp.b.sr) {
  96115. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  96116. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  96117. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  96118. + } else {
  96119. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  96120. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  96121. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  96122. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  96123. + /* Read arrived setup packet from req->buf */
  96124. + dwc_memcpy(&pcd->setup_pkt->req,
  96125. + req->buf + ep->dwc_ep.xfer_count, 8);
  96126. + }
  96127. + req->actual = ep->dwc_ep.xfer_count;
  96128. + dwc_otg_request_done(ep, req, -ECONNRESET);
  96129. + ep->dwc_ep.start_xfer_buff = 0;
  96130. + ep->dwc_ep.xfer_buff = 0;
  96131. + ep->dwc_ep.xfer_len = 0;
  96132. + }
  96133. + pcd->ep0state = EP0_IDLE;
  96134. + if (doepint.b.setup) {
  96135. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  96136. + /* Data stage started, clear setup */
  96137. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  96138. + doepint.b.setup = 0;
  96139. + handle_ep0(pcd);
  96140. + /* Prepare for setup packets if ep0in was enabled*/
  96141. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  96142. + ep0_out_start(core_if, pcd);
  96143. + }
  96144. + goto exit_xfercompl;
  96145. + } else {
  96146. + /* Prepare for more setup packets */
  96147. + DWC_DEBUGPL(DBG_PCDV,
  96148. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  96149. + ep0_out_start(core_if, pcd);
  96150. + }
  96151. + }
  96152. + }
  96153. + }
  96154. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  96155. + handle_ep0(pcd);
  96156. +exit_xfercompl:
  96157. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  96158. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  96159. + } else {
  96160. + if (core_if->dma_desc_enable == 0
  96161. + || pcd->ep0state != EP0_IDLE)
  96162. + handle_ep0(pcd);
  96163. + }
  96164. +#ifdef DWC_EN_ISOC
  96165. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  96166. + if (doepint.b.pktdrpsts == 0) {
  96167. + /* Clear the bit in DOEPINTn for this interrupt */
  96168. + CLEAR_OUT_EP_INTR(core_if,
  96169. + epnum,
  96170. + xfercompl);
  96171. + complete_iso_ep(pcd, ep);
  96172. + } else {
  96173. +
  96174. + doepint_data_t doepint = {.d32 = 0 };
  96175. + doepint.b.xfercompl = 1;
  96176. + doepint.b.pktdrpsts = 1;
  96177. + DWC_WRITE_REG32
  96178. + (&core_if->dev_if->out_ep_regs
  96179. + [epnum]->doepint,
  96180. + doepint.d32);
  96181. + if (handle_iso_out_pkt_dropped
  96182. + (core_if, dwc_ep)) {
  96183. + complete_iso_ep(pcd,
  96184. + ep);
  96185. + }
  96186. + }
  96187. +#endif /* DWC_EN_ISOC */
  96188. +#ifdef DWC_UTE_PER_IO
  96189. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  96190. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  96191. + if (!ep->stopped)
  96192. + complete_xiso_ep(ep);
  96193. +#endif /* DWC_UTE_PER_IO */
  96194. + } else {
  96195. + /* Clear the bit in DOEPINTn for this interrupt */
  96196. + CLEAR_OUT_EP_INTR(core_if, epnum,
  96197. + xfercompl);
  96198. +
  96199. + if (core_if->core_params->dev_out_nak) {
  96200. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  96201. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  96202. +#ifdef DEBUG
  96203. + print_memory_payload(pcd, dwc_ep);
  96204. +#endif
  96205. + }
  96206. + complete_ep(ep);
  96207. + }
  96208. +
  96209. + }
  96210. +
  96211. + /* Endpoint disable */
  96212. + if (doepint.b.epdisabled) {
  96213. +
  96214. + /* Clear the bit in DOEPINTn for this interrupt */
  96215. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  96216. + if (core_if->core_params->dev_out_nak) {
  96217. +#ifdef DEBUG
  96218. + print_memory_payload(pcd, dwc_ep);
  96219. +#endif
  96220. + /* In case of timeout condition */
  96221. + if (core_if->ep_xfer_info[epnum].state == 2) {
  96222. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  96223. + dev_global_regs->dctl);
  96224. + dctl.b.cgoutnak = 1;
  96225. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  96226. + dctl.d32);
  96227. + /* Unmask goutnakeff interrupt which was masked
  96228. + * during handle nak out interrupt */
  96229. + gintmsk.b.goutnakeff = 1;
  96230. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  96231. + 0, gintmsk.d32);
  96232. +
  96233. + complete_ep(ep);
  96234. + }
  96235. + }
  96236. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  96237. + {
  96238. + dctl_data_t dctl;
  96239. + gintmsk_data_t intr_mask = {.d32 = 0};
  96240. + dwc_otg_pcd_request_t *req = 0;
  96241. +
  96242. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  96243. + dev_global_regs->dctl);
  96244. + dctl.b.cgoutnak = 1;
  96245. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  96246. + dctl.d32);
  96247. +
  96248. + intr_mask.d32 = 0;
  96249. + intr_mask.b.incomplisoout = 1;
  96250. +
  96251. + /* Get any pending requests */
  96252. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  96253. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  96254. + if (!req) {
  96255. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  96256. + } else {
  96257. + dwc_otg_request_done(ep, req, 0);
  96258. + start_next_request(ep);
  96259. + }
  96260. + } else {
  96261. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  96262. + }
  96263. + }
  96264. + }
  96265. + /* AHB Error */
  96266. + if (doepint.b.ahberr) {
  96267. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  96268. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  96269. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  96270. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  96271. + }
  96272. + /* Setup Phase Done (contorl EPs) */
  96273. + if (doepint.b.setup) {
  96274. +#ifdef DEBUG_EP0
  96275. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  96276. +#endif
  96277. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  96278. +
  96279. + handle_ep0(pcd);
  96280. + }
  96281. +
  96282. + /** OUT EP BNA Intr */
  96283. + if (doepint.b.bna) {
  96284. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  96285. + if (core_if->dma_desc_enable) {
  96286. +#ifdef DWC_EN_ISOC
  96287. + if (dwc_ep->type ==
  96288. + DWC_OTG_EP_TYPE_ISOC) {
  96289. + /*
  96290. + * This checking is performed to prevent first "false" BNA
  96291. + * handling occuring right after reconnect
  96292. + */
  96293. + if (dwc_ep->next_frame !=
  96294. + 0xffffffff)
  96295. + dwc_otg_pcd_handle_iso_bna(ep);
  96296. + } else
  96297. +#endif /* DWC_EN_ISOC */
  96298. + {
  96299. + dwc_otg_pcd_handle_noniso_bna(ep);
  96300. + }
  96301. + }
  96302. + }
  96303. + /* Babble Interrupt */
  96304. + if (doepint.b.babble) {
  96305. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  96306. + epnum);
  96307. + handle_out_ep_babble_intr(pcd, epnum);
  96308. +
  96309. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  96310. + }
  96311. + if (doepint.b.outtknepdis) {
  96312. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  96313. + disabled\n",epnum);
  96314. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  96315. + doepmsk_data_t doepmsk = {.d32 = 0};
  96316. + ep->dwc_ep.frame_num = core_if->frame_num;
  96317. + if (ep->dwc_ep.bInterval > 1) {
  96318. + depctl_data_t depctl;
  96319. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  96320. + out_ep_regs[epnum]->doepctl);
  96321. + if (ep->dwc_ep.frame_num & 0x1) {
  96322. + depctl.b.setd1pid = 1;
  96323. + depctl.b.setd0pid = 0;
  96324. + } else {
  96325. + depctl.b.setd0pid = 1;
  96326. + depctl.b.setd1pid = 0;
  96327. + }
  96328. + DWC_WRITE_REG32(&core_if->dev_if->
  96329. + out_ep_regs[epnum]->doepctl, depctl.d32);
  96330. + }
  96331. + start_next_request(ep);
  96332. + doepmsk.b.outtknepdis = 1;
  96333. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  96334. + doepmsk.d32, 0);
  96335. + }
  96336. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  96337. + }
  96338. +
  96339. + /* NAK Interrutp */
  96340. + if (doepint.b.nak) {
  96341. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  96342. + handle_out_ep_nak_intr(pcd, epnum);
  96343. +
  96344. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  96345. + }
  96346. + /* NYET Interrutp */
  96347. + if (doepint.b.nyet) {
  96348. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  96349. + handle_out_ep_nyet_intr(pcd, epnum);
  96350. +
  96351. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  96352. + }
  96353. + }
  96354. +
  96355. + epnum++;
  96356. + ep_intr >>= 1;
  96357. + }
  96358. +
  96359. + return 1;
  96360. +
  96361. +#undef CLEAR_OUT_EP_INTR
  96362. +}
  96363. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  96364. +{
  96365. + int retval = 0;
  96366. + if(!frm_overrun && curr_fr >= trgt_fr)
  96367. + retval = 1;
  96368. + else if (frm_overrun
  96369. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  96370. + retval = 1;
  96371. + return retval;
  96372. +}
  96373. +/**
  96374. + * Incomplete ISO IN Transfer Interrupt.
  96375. + * This interrupt indicates one of the following conditions occurred
  96376. + * while transmitting an ISOC transaction.
  96377. + * - Corrupted IN Token for ISOC EP.
  96378. + * - Packet not complete in FIFO.
  96379. + * The follow actions will be taken:
  96380. + * -# Determine the EP
  96381. + * -# Set incomplete flag in dwc_ep structure
  96382. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  96383. + * Flush FIFO
  96384. + */
  96385. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  96386. +{
  96387. + gintsts_data_t gintsts;
  96388. +
  96389. +#ifdef DWC_EN_ISOC
  96390. + dwc_otg_dev_if_t *dev_if;
  96391. + deptsiz_data_t deptsiz = {.d32 = 0 };
  96392. + depctl_data_t depctl = {.d32 = 0 };
  96393. + dsts_data_t dsts = {.d32 = 0 };
  96394. + dwc_ep_t *dwc_ep;
  96395. + int i;
  96396. +
  96397. + dev_if = GET_CORE_IF(pcd)->dev_if;
  96398. +
  96399. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  96400. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  96401. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  96402. + deptsiz.d32 =
  96403. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  96404. + depctl.d32 =
  96405. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96406. +
  96407. + if (depctl.b.epdis && deptsiz.d32) {
  96408. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  96409. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  96410. + dwc_ep->cur_pkt = 0;
  96411. + dwc_ep->proc_buf_num =
  96412. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  96413. +
  96414. + if (dwc_ep->proc_buf_num) {
  96415. + dwc_ep->cur_pkt_addr =
  96416. + dwc_ep->xfer_buff1;
  96417. + dwc_ep->cur_pkt_dma_addr =
  96418. + dwc_ep->dma_addr1;
  96419. + } else {
  96420. + dwc_ep->cur_pkt_addr =
  96421. + dwc_ep->xfer_buff0;
  96422. + dwc_ep->cur_pkt_dma_addr =
  96423. + dwc_ep->dma_addr0;
  96424. + }
  96425. +
  96426. + }
  96427. +
  96428. + dsts.d32 =
  96429. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  96430. + dev_global_regs->dsts);
  96431. + dwc_ep->next_frame = dsts.b.soffn;
  96432. +
  96433. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  96434. + (pcd),
  96435. + dwc_ep);
  96436. + }
  96437. + }
  96438. + }
  96439. +
  96440. +#else
  96441. + depctl_data_t depctl = {.d32 = 0 };
  96442. + dwc_ep_t *dwc_ep;
  96443. + dwc_otg_dev_if_t *dev_if;
  96444. + int i;
  96445. + dev_if = GET_CORE_IF(pcd)->dev_if;
  96446. +
  96447. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  96448. +
  96449. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  96450. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  96451. + depctl.d32 =
  96452. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96453. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  96454. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  96455. + dwc_ep->frm_overrun))
  96456. + {
  96457. + depctl.d32 =
  96458. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96459. + depctl.b.snak = 1;
  96460. + depctl.b.epdis = 1;
  96461. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  96462. + }
  96463. + }
  96464. + }
  96465. +
  96466. + /*intr_mask.b.incomplisoin = 1;
  96467. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  96468. + intr_mask.d32, 0); */
  96469. +#endif //DWC_EN_ISOC
  96470. +
  96471. + /* Clear interrupt */
  96472. + gintsts.d32 = 0;
  96473. + gintsts.b.incomplisoin = 1;
  96474. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  96475. + gintsts.d32);
  96476. +
  96477. + return 1;
  96478. +}
  96479. +
  96480. +/**
  96481. + * Incomplete ISO OUT Transfer Interrupt.
  96482. + *
  96483. + * This interrupt indicates that the core has dropped an ISO OUT
  96484. + * packet. The following conditions can be the cause:
  96485. + * - FIFO Full, the entire packet would not fit in the FIFO.
  96486. + * - CRC Error
  96487. + * - Corrupted Token
  96488. + * The follow actions will be taken:
  96489. + * -# Determine the EP
  96490. + * -# Set incomplete flag in dwc_ep structure
  96491. + * -# Read any data from the FIFO
  96492. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  96493. + * re-enable EP.
  96494. + */
  96495. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  96496. +{
  96497. +
  96498. + gintsts_data_t gintsts;
  96499. +
  96500. +#ifdef DWC_EN_ISOC
  96501. + dwc_otg_dev_if_t *dev_if;
  96502. + deptsiz_data_t deptsiz = {.d32 = 0 };
  96503. + depctl_data_t depctl = {.d32 = 0 };
  96504. + dsts_data_t dsts = {.d32 = 0 };
  96505. + dwc_ep_t *dwc_ep;
  96506. + int i;
  96507. +
  96508. + dev_if = GET_CORE_IF(pcd)->dev_if;
  96509. +
  96510. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  96511. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  96512. + if (pcd->out_ep[i].dwc_ep.active &&
  96513. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  96514. + deptsiz.d32 =
  96515. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  96516. + depctl.d32 =
  96517. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  96518. +
  96519. + if (depctl.b.epdis && deptsiz.d32) {
  96520. + set_current_pkt_info(GET_CORE_IF(pcd),
  96521. + &pcd->out_ep[i].dwc_ep);
  96522. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  96523. + dwc_ep->cur_pkt = 0;
  96524. + dwc_ep->proc_buf_num =
  96525. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  96526. +
  96527. + if (dwc_ep->proc_buf_num) {
  96528. + dwc_ep->cur_pkt_addr =
  96529. + dwc_ep->xfer_buff1;
  96530. + dwc_ep->cur_pkt_dma_addr =
  96531. + dwc_ep->dma_addr1;
  96532. + } else {
  96533. + dwc_ep->cur_pkt_addr =
  96534. + dwc_ep->xfer_buff0;
  96535. + dwc_ep->cur_pkt_dma_addr =
  96536. + dwc_ep->dma_addr0;
  96537. + }
  96538. +
  96539. + }
  96540. +
  96541. + dsts.d32 =
  96542. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  96543. + dev_global_regs->dsts);
  96544. + dwc_ep->next_frame = dsts.b.soffn;
  96545. +
  96546. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  96547. + (pcd),
  96548. + dwc_ep);
  96549. + }
  96550. + }
  96551. + }
  96552. +#else
  96553. + /** @todo implement ISR */
  96554. + gintmsk_data_t intr_mask = {.d32 = 0 };
  96555. + dwc_otg_core_if_t *core_if;
  96556. + deptsiz_data_t deptsiz = {.d32 = 0 };
  96557. + depctl_data_t depctl = {.d32 = 0 };
  96558. + dctl_data_t dctl = {.d32 = 0 };
  96559. + dwc_ep_t *dwc_ep = NULL;
  96560. + int i;
  96561. + core_if = GET_CORE_IF(pcd);
  96562. +
  96563. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  96564. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  96565. + depctl.d32 =
  96566. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  96567. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  96568. + core_if->dev_if->isoc_ep = dwc_ep;
  96569. + deptsiz.d32 =
  96570. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  96571. + break;
  96572. + }
  96573. + }
  96574. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  96575. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  96576. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  96577. +
  96578. + if (!intr_mask.b.goutnakeff) {
  96579. + /* Unmask it */
  96580. + intr_mask.b.goutnakeff = 1;
  96581. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  96582. + }
  96583. + if (!gintsts.b.goutnakeff) {
  96584. + dctl.b.sgoutnak = 1;
  96585. + }
  96586. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  96587. +
  96588. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  96589. + if (depctl.b.epena) {
  96590. + depctl.b.epdis = 1;
  96591. + depctl.b.snak = 1;
  96592. + }
  96593. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  96594. +
  96595. + intr_mask.d32 = 0;
  96596. + intr_mask.b.incomplisoout = 1;
  96597. +
  96598. +#endif /* DWC_EN_ISOC */
  96599. +
  96600. + /* Clear interrupt */
  96601. + gintsts.d32 = 0;
  96602. + gintsts.b.incomplisoout = 1;
  96603. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  96604. + gintsts.d32);
  96605. +
  96606. + return 1;
  96607. +}
  96608. +
  96609. +/**
  96610. + * This function handles the Global IN NAK Effective interrupt.
  96611. + *
  96612. + */
  96613. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  96614. +{
  96615. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  96616. + depctl_data_t diepctl = {.d32 = 0 };
  96617. + gintmsk_data_t intr_mask = {.d32 = 0 };
  96618. + gintsts_data_t gintsts;
  96619. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  96620. + int i;
  96621. +
  96622. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  96623. +
  96624. + /* Disable all active IN EPs */
  96625. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  96626. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  96627. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  96628. + if (core_if->start_predict > 0)
  96629. + core_if->start_predict++;
  96630. + diepctl.b.epdis = 1;
  96631. + diepctl.b.snak = 1;
  96632. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  96633. + }
  96634. + }
  96635. +
  96636. +
  96637. + /* Disable the Global IN NAK Effective Interrupt */
  96638. + intr_mask.b.ginnakeff = 1;
  96639. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  96640. + intr_mask.d32, 0);
  96641. +
  96642. + /* Clear interrupt */
  96643. + gintsts.d32 = 0;
  96644. + gintsts.b.ginnakeff = 1;
  96645. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  96646. + gintsts.d32);
  96647. +
  96648. + return 1;
  96649. +}
  96650. +
  96651. +/**
  96652. + * OUT NAK Effective.
  96653. + *
  96654. + */
  96655. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  96656. +{
  96657. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  96658. + gintmsk_data_t intr_mask = {.d32 = 0 };
  96659. + gintsts_data_t gintsts;
  96660. + depctl_data_t doepctl;
  96661. + int i;
  96662. +
  96663. + /* Disable the Global OUT NAK Effective Interrupt */
  96664. + intr_mask.b.goutnakeff = 1;
  96665. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  96666. + intr_mask.d32, 0);
  96667. +
  96668. + /* If DEV OUT NAK enabled*/
  96669. + if (pcd->core_if->core_params->dev_out_nak) {
  96670. + /* Run over all out endpoints to determine the ep number on
  96671. + * which the timeout has happened
  96672. + */
  96673. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  96674. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  96675. + break;
  96676. + }
  96677. + if (i > dev_if->num_out_eps) {
  96678. + dctl_data_t dctl;
  96679. + dctl.d32 =
  96680. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  96681. + dctl.b.cgoutnak = 1;
  96682. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  96683. + dctl.d32);
  96684. + goto out;
  96685. + }
  96686. +
  96687. + /* Disable the endpoint */
  96688. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  96689. + if (doepctl.b.epena) {
  96690. + doepctl.b.epdis = 1;
  96691. + doepctl.b.snak = 1;
  96692. + }
  96693. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  96694. + return 1;
  96695. + }
  96696. + /* We come here from Incomplete ISO OUT handler */
  96697. + if (dev_if->isoc_ep) {
  96698. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  96699. + uint32_t epnum = dwc_ep->num;
  96700. + doepint_data_t doepint;
  96701. + doepint.d32 =
  96702. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  96703. + dev_if->isoc_ep = NULL;
  96704. + doepctl.d32 =
  96705. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  96706. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  96707. + if (doepctl.b.epena) {
  96708. + doepctl.b.epdis = 1;
  96709. + doepctl.b.snak = 1;
  96710. + }
  96711. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  96712. + doepctl.d32);
  96713. + return 1;
  96714. + } else
  96715. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  96716. + "Global OUT NAK Effective\n");
  96717. +
  96718. +out:
  96719. + /* Clear interrupt */
  96720. + gintsts.d32 = 0;
  96721. + gintsts.b.goutnakeff = 1;
  96722. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  96723. + gintsts.d32);
  96724. +
  96725. + return 1;
  96726. +}
  96727. +
  96728. +/**
  96729. + * PCD interrupt handler.
  96730. + *
  96731. + * The PCD handles the device interrupts. Many conditions can cause a
  96732. + * device interrupt. When an interrupt occurs, the device interrupt
  96733. + * service routine determines the cause of the interrupt and
  96734. + * dispatches handling to the appropriate function. These interrupt
  96735. + * handling functions are described below.
  96736. + *
  96737. + * All interrupt registers are processed from LSB to MSB.
  96738. + *
  96739. + */
  96740. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  96741. +{
  96742. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  96743. +#ifdef VERBOSE
  96744. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  96745. +#endif
  96746. + gintsts_data_t gintr_status;
  96747. + int32_t retval = 0;
  96748. +
  96749. + /* Exit from ISR if core is hibernated */
  96750. + if (core_if->hibernation_suspend == 1) {
  96751. + return retval;
  96752. + }
  96753. +#ifdef VERBOSE
  96754. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  96755. + __func__,
  96756. + DWC_READ_REG32(&global_regs->gintsts),
  96757. + DWC_READ_REG32(&global_regs->gintmsk));
  96758. +#endif
  96759. +
  96760. + if (dwc_otg_is_device_mode(core_if)) {
  96761. + DWC_SPINLOCK(pcd->lock);
  96762. +#ifdef VERBOSE
  96763. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  96764. + __func__,
  96765. + DWC_READ_REG32(&global_regs->gintsts),
  96766. + DWC_READ_REG32(&global_regs->gintmsk));
  96767. +#endif
  96768. +
  96769. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  96770. +
  96771. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  96772. + __func__, gintr_status.d32);
  96773. +
  96774. + if (gintr_status.b.sofintr) {
  96775. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  96776. + }
  96777. + if (gintr_status.b.rxstsqlvl) {
  96778. + retval |=
  96779. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  96780. + }
  96781. + if (gintr_status.b.nptxfempty) {
  96782. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  96783. + }
  96784. + if (gintr_status.b.goutnakeff) {
  96785. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  96786. + }
  96787. + if (gintr_status.b.i2cintr) {
  96788. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  96789. + }
  96790. + if (gintr_status.b.erlysuspend) {
  96791. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  96792. + }
  96793. + if (gintr_status.b.usbreset) {
  96794. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  96795. + }
  96796. + if (gintr_status.b.enumdone) {
  96797. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  96798. + }
  96799. + if (gintr_status.b.isooutdrop) {
  96800. + retval |=
  96801. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  96802. + (pcd);
  96803. + }
  96804. + if (gintr_status.b.eopframe) {
  96805. + retval |=
  96806. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  96807. + }
  96808. + if (gintr_status.b.inepint) {
  96809. + if (!core_if->multiproc_int_enable) {
  96810. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  96811. + }
  96812. + }
  96813. + if (gintr_status.b.outepintr) {
  96814. + if (!core_if->multiproc_int_enable) {
  96815. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  96816. + }
  96817. + }
  96818. + if (gintr_status.b.epmismatch) {
  96819. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  96820. + }
  96821. + if (gintr_status.b.fetsusp) {
  96822. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  96823. + }
  96824. + if (gintr_status.b.ginnakeff) {
  96825. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  96826. + }
  96827. + if (gintr_status.b.incomplisoin) {
  96828. + retval |=
  96829. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  96830. + }
  96831. + if (gintr_status.b.incomplisoout) {
  96832. + retval |=
  96833. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  96834. + }
  96835. +
  96836. + /* In MPI mode Device Endpoints interrupts are asserted
  96837. + * without setting outepintr and inepint bits set, so these
  96838. + * Interrupt handlers are called without checking these bit-fields
  96839. + */
  96840. + if (core_if->multiproc_int_enable) {
  96841. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  96842. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  96843. + }
  96844. +#ifdef VERBOSE
  96845. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  96846. + DWC_READ_REG32(&global_regs->gintsts));
  96847. +#endif
  96848. + DWC_SPINUNLOCK(pcd->lock);
  96849. + }
  96850. + return retval;
  96851. +}
  96852. +
  96853. +#endif /* DWC_HOST_ONLY */
  96854. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  96855. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 1970-01-01 01:00:00.000000000 +0100
  96856. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c 2014-09-14 19:04:13.000000000 +0200
  96857. @@ -0,0 +1,1360 @@
  96858. + /* ==========================================================================
  96859. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  96860. + * $Revision: #21 $
  96861. + * $Date: 2012/08/10 $
  96862. + * $Change: 2047372 $
  96863. + *
  96864. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  96865. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  96866. + * otherwise expressly agreed to in writing between Synopsys and you.
  96867. + *
  96868. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  96869. + * any End User Software License Agreement or Agreement for Licensed Product
  96870. + * with Synopsys or any supplement thereto. You are permitted to use and
  96871. + * redistribute this Software in source and binary forms, with or without
  96872. + * modification, provided that redistributions of source code must retain this
  96873. + * notice. You may not view, use, disclose, copy or distribute this file or
  96874. + * any information contained herein except pursuant to this license grant from
  96875. + * Synopsys. If you do not agree with this notice, including the disclaimer
  96876. + * below, then you are not authorized to use the Software.
  96877. + *
  96878. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  96879. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  96880. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  96881. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  96882. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  96883. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  96884. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  96885. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  96886. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  96887. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  96888. + * DAMAGE.
  96889. + * ========================================================================== */
  96890. +#ifndef DWC_HOST_ONLY
  96891. +
  96892. +/** @file
  96893. + * This file implements the Peripheral Controller Driver.
  96894. + *
  96895. + * The Peripheral Controller Driver (PCD) is responsible for
  96896. + * translating requests from the Function Driver into the appropriate
  96897. + * actions on the DWC_otg controller. It isolates the Function Driver
  96898. + * from the specifics of the controller by providing an API to the
  96899. + * Function Driver.
  96900. + *
  96901. + * The Peripheral Controller Driver for Linux will implement the
  96902. + * Gadget API, so that the existing Gadget drivers can be used.
  96903. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  96904. + *
  96905. + * The Linux Gadget API is defined in the header file
  96906. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  96907. + * defined in the structure <code>usb_ep_ops</code> and the USB
  96908. + * Controller API is defined in the structure
  96909. + * <code>usb_gadget_ops</code>.
  96910. + *
  96911. + */
  96912. +
  96913. +#include "dwc_otg_os_dep.h"
  96914. +#include "dwc_otg_pcd_if.h"
  96915. +#include "dwc_otg_pcd.h"
  96916. +#include "dwc_otg_driver.h"
  96917. +#include "dwc_otg_dbg.h"
  96918. +
  96919. +extern bool fiq_enable;
  96920. +
  96921. +static struct gadget_wrapper {
  96922. + dwc_otg_pcd_t *pcd;
  96923. +
  96924. + struct usb_gadget gadget;
  96925. + struct usb_gadget_driver *driver;
  96926. +
  96927. + struct usb_ep ep0;
  96928. + struct usb_ep in_ep[16];
  96929. + struct usb_ep out_ep[16];
  96930. +
  96931. +} *gadget_wrapper;
  96932. +
  96933. +/* Display the contents of the buffer */
  96934. +extern void dump_msg(const u8 * buf, unsigned int length);
  96935. +/**
  96936. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  96937. + * if the endpoint is not found
  96938. + */
  96939. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  96940. +{
  96941. + int i;
  96942. + if (pcd->ep0.priv == handle) {
  96943. + return &pcd->ep0;
  96944. + }
  96945. +
  96946. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  96947. + if (pcd->in_ep[i].priv == handle)
  96948. + return &pcd->in_ep[i];
  96949. + if (pcd->out_ep[i].priv == handle)
  96950. + return &pcd->out_ep[i];
  96951. + }
  96952. +
  96953. + return NULL;
  96954. +}
  96955. +
  96956. +/* USB Endpoint Operations */
  96957. +/*
  96958. + * The following sections briefly describe the behavior of the Gadget
  96959. + * API endpoint operations implemented in the DWC_otg driver
  96960. + * software. Detailed descriptions of the generic behavior of each of
  96961. + * these functions can be found in the Linux header file
  96962. + * include/linux/usb_gadget.h.
  96963. + *
  96964. + * The Gadget API provides wrapper functions for each of the function
  96965. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  96966. + * function, which then calls the underlying PCD function. The
  96967. + * following sections are named according to the wrapper
  96968. + * functions. Within each section, the corresponding DWC_otg PCD
  96969. + * function name is specified.
  96970. + *
  96971. + */
  96972. +
  96973. +/**
  96974. + * This function is called by the Gadget Driver for each EP to be
  96975. + * configured for the current configuration (SET_CONFIGURATION).
  96976. + *
  96977. + * This function initializes the dwc_otg_ep_t data structure, and then
  96978. + * calls dwc_otg_ep_activate.
  96979. + */
  96980. +static int ep_enable(struct usb_ep *usb_ep,
  96981. + const struct usb_endpoint_descriptor *ep_desc)
  96982. +{
  96983. + int retval;
  96984. +
  96985. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  96986. +
  96987. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  96988. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  96989. + return -EINVAL;
  96990. + }
  96991. + if (usb_ep == &gadget_wrapper->ep0) {
  96992. + DWC_WARN("%s, bad ep(0)\n", __func__);
  96993. + return -EINVAL;
  96994. + }
  96995. +
  96996. + /* Check FIFO size? */
  96997. + if (!ep_desc->wMaxPacketSize) {
  96998. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  96999. + return -ERANGE;
  97000. + }
  97001. +
  97002. + if (!gadget_wrapper->driver ||
  97003. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  97004. + DWC_WARN("%s, bogus device state\n", __func__);
  97005. + return -ESHUTDOWN;
  97006. + }
  97007. +
  97008. + /* Delete after check - MAS */
  97009. +#if 0
  97010. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  97011. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  97012. + nat = (nat >> 11) & 0x03;
  97013. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  97014. +#endif
  97015. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  97016. + (const uint8_t *)ep_desc,
  97017. + (void *)usb_ep);
  97018. + if (retval) {
  97019. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  97020. + return -EINVAL;
  97021. + }
  97022. +
  97023. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  97024. +
  97025. + return 0;
  97026. +}
  97027. +
  97028. +/**
  97029. + * This function is called when an EP is disabled due to disconnect or
  97030. + * change in configuration. Any pending requests will terminate with a
  97031. + * status of -ESHUTDOWN.
  97032. + *
  97033. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  97034. + * and then calls dwc_otg_ep_deactivate.
  97035. + */
  97036. +static int ep_disable(struct usb_ep *usb_ep)
  97037. +{
  97038. + int retval;
  97039. +
  97040. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  97041. + if (!usb_ep) {
  97042. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  97043. + usb_ep ? usb_ep->name : NULL);
  97044. + return -EINVAL;
  97045. + }
  97046. +
  97047. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  97048. + if (retval) {
  97049. + retval = -EINVAL;
  97050. + }
  97051. +
  97052. + return retval;
  97053. +}
  97054. +
  97055. +/**
  97056. + * This function allocates a request object to use with the specified
  97057. + * endpoint.
  97058. + *
  97059. + * @param ep The endpoint to be used with with the request
  97060. + * @param gfp_flags the GFP_* flags to use.
  97061. + */
  97062. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  97063. + gfp_t gfp_flags)
  97064. +{
  97065. + struct usb_request *usb_req;
  97066. +
  97067. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  97068. + if (0 == ep) {
  97069. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  97070. + return 0;
  97071. + }
  97072. + usb_req = kmalloc(sizeof(*usb_req), gfp_flags);
  97073. + if (0 == usb_req) {
  97074. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  97075. + return 0;
  97076. + }
  97077. + memset(usb_req, 0, sizeof(*usb_req));
  97078. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  97079. +
  97080. + return usb_req;
  97081. +}
  97082. +
  97083. +/**
  97084. + * This function frees a request object.
  97085. + *
  97086. + * @param ep The endpoint associated with the request
  97087. + * @param req The request being freed
  97088. + */
  97089. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  97090. +{
  97091. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  97092. +
  97093. + if (0 == ep || 0 == req) {
  97094. + DWC_WARN("%s() %s\n", __func__,
  97095. + "Invalid ep or req argument!\n");
  97096. + return;
  97097. + }
  97098. +
  97099. + kfree(req);
  97100. +}
  97101. +
  97102. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  97103. +/**
  97104. + * This function allocates an I/O buffer to be used for a transfer
  97105. + * to/from the specified endpoint.
  97106. + *
  97107. + * @param usb_ep The endpoint to be used with with the request
  97108. + * @param bytes The desired number of bytes for the buffer
  97109. + * @param dma Pointer to the buffer's DMA address; must be valid
  97110. + * @param gfp_flags the GFP_* flags to use.
  97111. + * @return address of a new buffer or null is buffer could not be allocated.
  97112. + */
  97113. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  97114. + dma_addr_t * dma, gfp_t gfp_flags)
  97115. +{
  97116. + void *buf;
  97117. + dwc_otg_pcd_t *pcd = 0;
  97118. +
  97119. + pcd = gadget_wrapper->pcd;
  97120. +
  97121. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  97122. + dma, gfp_flags);
  97123. +
  97124. + /* Check dword alignment */
  97125. + if ((bytes & 0x3UL) != 0) {
  97126. + DWC_WARN("%s() Buffer size is not a multiple of"
  97127. + "DWORD size (%d)", __func__, bytes);
  97128. + }
  97129. +
  97130. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  97131. +
  97132. + /* Check dword alignment */
  97133. + if (((int)buf & 0x3UL) != 0) {
  97134. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  97135. + __func__, buf);
  97136. + }
  97137. +
  97138. + return buf;
  97139. +}
  97140. +
  97141. +/**
  97142. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  97143. + *
  97144. + * @param usb_ep the endpoint associated with the buffer
  97145. + * @param buf address of the buffer
  97146. + * @param dma The buffer's DMA address
  97147. + * @param bytes The number of bytes of the buffer
  97148. + */
  97149. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  97150. + dma_addr_t dma, unsigned bytes)
  97151. +{
  97152. + dwc_otg_pcd_t *pcd = 0;
  97153. +
  97154. + pcd = gadget_wrapper->pcd;
  97155. +
  97156. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  97157. +
  97158. + dma_free_coherent(NULL, bytes, buf, dma);
  97159. +}
  97160. +#endif
  97161. +
  97162. +/**
  97163. + * This function is used to submit an I/O Request to an EP.
  97164. + *
  97165. + * - When the request completes the request's completion callback
  97166. + * is called to return the request to the driver.
  97167. + * - An EP, except control EPs, may have multiple requests
  97168. + * pending.
  97169. + * - Once submitted the request cannot be examined or modified.
  97170. + * - Each request is turned into one or more packets.
  97171. + * - A BULK EP can queue any amount of data; the transfer is
  97172. + * packetized.
  97173. + * - Zero length Packets are specified with the request 'zero'
  97174. + * flag.
  97175. + */
  97176. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  97177. + gfp_t gfp_flags)
  97178. +{
  97179. + dwc_otg_pcd_t *pcd;
  97180. + struct dwc_otg_pcd_ep *ep = NULL;
  97181. + int retval = 0, is_isoc_ep = 0;
  97182. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  97183. +
  97184. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  97185. + __func__, usb_ep, usb_req, gfp_flags);
  97186. +
  97187. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  97188. + DWC_WARN("bad params\n");
  97189. + return -EINVAL;
  97190. + }
  97191. +
  97192. + if (!usb_ep) {
  97193. + DWC_WARN("bad ep\n");
  97194. + return -EINVAL;
  97195. + }
  97196. +
  97197. + pcd = gadget_wrapper->pcd;
  97198. + if (!gadget_wrapper->driver ||
  97199. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  97200. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  97201. + gadget_wrapper->gadget.speed);
  97202. + DWC_WARN("bogus device state\n");
  97203. + return -ESHUTDOWN;
  97204. + }
  97205. +
  97206. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  97207. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  97208. +
  97209. + usb_req->status = -EINPROGRESS;
  97210. + usb_req->actual = 0;
  97211. +
  97212. + ep = ep_from_handle(pcd, usb_ep);
  97213. + if (ep == NULL)
  97214. + is_isoc_ep = 0;
  97215. + else
  97216. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  97217. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  97218. + dma_addr = usb_req->dma;
  97219. +#else
  97220. + if (GET_CORE_IF(pcd)->dma_enable) {
  97221. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  97222. + struct device *dev = NULL;
  97223. +
  97224. + if (otg_dev != NULL)
  97225. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  97226. +
  97227. + if (usb_req->length != 0 &&
  97228. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  97229. + dma_addr = dma_map_single(dev, usb_req->buf,
  97230. + usb_req->length,
  97231. + ep->dwc_ep.is_in ?
  97232. + DMA_TO_DEVICE:
  97233. + DMA_FROM_DEVICE);
  97234. + }
  97235. + }
  97236. +#endif
  97237. +
  97238. +#ifdef DWC_UTE_PER_IO
  97239. + if (is_isoc_ep == 1) {
  97240. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  97241. + usb_req->length, usb_req->zero, usb_req,
  97242. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  97243. + if (retval)
  97244. + return -EINVAL;
  97245. +
  97246. + return 0;
  97247. + }
  97248. +#endif
  97249. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  97250. + usb_req->length, usb_req->zero, usb_req,
  97251. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  97252. + if (retval) {
  97253. + return -EINVAL;
  97254. + }
  97255. +
  97256. + return 0;
  97257. +}
  97258. +
  97259. +/**
  97260. + * This function cancels an I/O request from an EP.
  97261. + */
  97262. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  97263. +{
  97264. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  97265. +
  97266. + if (!usb_ep || !usb_req) {
  97267. + DWC_WARN("bad argument\n");
  97268. + return -EINVAL;
  97269. + }
  97270. + if (!gadget_wrapper->driver ||
  97271. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  97272. + DWC_WARN("bogus device state\n");
  97273. + return -ESHUTDOWN;
  97274. + }
  97275. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  97276. + return -EINVAL;
  97277. + }
  97278. +
  97279. + return 0;
  97280. +}
  97281. +
  97282. +/**
  97283. + * usb_ep_set_halt stalls an endpoint.
  97284. + *
  97285. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  97286. + * toggle.
  97287. + *
  97288. + * Both of these functions are implemented with the same underlying
  97289. + * function. The behavior depends on the value argument.
  97290. + *
  97291. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  97292. + * @param[in] value
  97293. + * - 0 means clear_halt.
  97294. + * - 1 means set_halt,
  97295. + * - 2 means clear stall lock flag.
  97296. + * - 3 means set stall lock flag.
  97297. + */
  97298. +static int ep_halt(struct usb_ep *usb_ep, int value)
  97299. +{
  97300. + int retval = 0;
  97301. +
  97302. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  97303. +
  97304. + if (!usb_ep) {
  97305. + DWC_WARN("bad ep\n");
  97306. + return -EINVAL;
  97307. + }
  97308. +
  97309. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  97310. + if (retval == -DWC_E_AGAIN) {
  97311. + return -EAGAIN;
  97312. + } else if (retval) {
  97313. + retval = -EINVAL;
  97314. + }
  97315. +
  97316. + return retval;
  97317. +}
  97318. +
  97319. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  97320. +#if 0
  97321. +/**
  97322. + * ep_wedge: sets the halt feature and ignores clear requests
  97323. + *
  97324. + * @usb_ep: the endpoint being wedged
  97325. + *
  97326. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  97327. + * requests. If the gadget driver clears the halt status, it will
  97328. + * automatically unwedge the endpoint.
  97329. + *
  97330. + * Returns zero on success, else negative errno. *
  97331. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  97332. + */
  97333. +static int ep_wedge(struct usb_ep *usb_ep)
  97334. +{
  97335. + int retval = 0;
  97336. +
  97337. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  97338. +
  97339. + if (!usb_ep) {
  97340. + DWC_WARN("bad ep\n");
  97341. + return -EINVAL;
  97342. + }
  97343. +
  97344. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  97345. + if (retval == -DWC_E_AGAIN) {
  97346. + retval = -EAGAIN;
  97347. + } else if (retval) {
  97348. + retval = -EINVAL;
  97349. + }
  97350. +
  97351. + return retval;
  97352. +}
  97353. +#endif
  97354. +
  97355. +#ifdef DWC_EN_ISOC
  97356. +/**
  97357. + * This function is used to submit an ISOC Transfer Request to an EP.
  97358. + *
  97359. + * - Every time a sync period completes the request's completion callback
  97360. + * is called to provide data to the gadget driver.
  97361. + * - Once submitted the request cannot be modified.
  97362. + * - Each request is turned into periodic data packets untill ISO
  97363. + * Transfer is stopped..
  97364. + */
  97365. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  97366. + gfp_t gfp_flags)
  97367. +{
  97368. + int retval = 0;
  97369. +
  97370. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  97371. + DWC_WARN("bad params\n");
  97372. + return -EINVAL;
  97373. + }
  97374. +
  97375. + if (!usb_ep) {
  97376. + DWC_PRINTF("bad params\n");
  97377. + return -EINVAL;
  97378. + }
  97379. +
  97380. + req->status = -EINPROGRESS;
  97381. +
  97382. + retval =
  97383. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  97384. + req->buf1, req->dma0, req->dma1,
  97385. + req->sync_frame, req->data_pattern_frame,
  97386. + req->data_per_frame,
  97387. + req->
  97388. + flags & USB_REQ_ISO_ASAP ? -1 :
  97389. + req->start_frame, req->buf_proc_intrvl,
  97390. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  97391. +
  97392. + if (retval) {
  97393. + return -EINVAL;
  97394. + }
  97395. +
  97396. + return retval;
  97397. +}
  97398. +
  97399. +/**
  97400. + * This function stops ISO EP Periodic Data Transfer.
  97401. + */
  97402. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  97403. +{
  97404. + int retval = 0;
  97405. + if (!usb_ep) {
  97406. + DWC_WARN("bad ep\n");
  97407. + }
  97408. +
  97409. + if (!gadget_wrapper->driver ||
  97410. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  97411. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  97412. + gadget_wrapper->gadget.speed);
  97413. + DWC_WARN("bogus device state\n");
  97414. + }
  97415. +
  97416. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  97417. + if (retval) {
  97418. + retval = -EINVAL;
  97419. + }
  97420. +
  97421. + return retval;
  97422. +}
  97423. +
  97424. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  97425. + int packets, gfp_t gfp_flags)
  97426. +{
  97427. + struct usb_iso_request *pReq = NULL;
  97428. + uint32_t req_size;
  97429. +
  97430. + req_size = sizeof(struct usb_iso_request);
  97431. + req_size +=
  97432. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  97433. +
  97434. + pReq = kmalloc(req_size, gfp_flags);
  97435. + if (!pReq) {
  97436. + DWC_WARN("Can't allocate Iso Request\n");
  97437. + return 0;
  97438. + }
  97439. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  97440. +
  97441. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  97442. +
  97443. + return pReq;
  97444. +}
  97445. +
  97446. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  97447. +{
  97448. + kfree(req);
  97449. +}
  97450. +
  97451. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  97452. + .ep_ops = {
  97453. + .enable = ep_enable,
  97454. + .disable = ep_disable,
  97455. +
  97456. + .alloc_request = dwc_otg_pcd_alloc_request,
  97457. + .free_request = dwc_otg_pcd_free_request,
  97458. +
  97459. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  97460. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  97461. + .free_buffer = dwc_otg_pcd_free_buffer,
  97462. +#endif
  97463. +
  97464. + .queue = ep_queue,
  97465. + .dequeue = ep_dequeue,
  97466. +
  97467. + .set_halt = ep_halt,
  97468. + .fifo_status = 0,
  97469. + .fifo_flush = 0,
  97470. + },
  97471. + .iso_ep_start = iso_ep_start,
  97472. + .iso_ep_stop = iso_ep_stop,
  97473. + .alloc_iso_request = alloc_iso_request,
  97474. + .free_iso_request = free_iso_request,
  97475. +};
  97476. +
  97477. +#else
  97478. +
  97479. + int (*enable) (struct usb_ep *ep,
  97480. + const struct usb_endpoint_descriptor *desc);
  97481. + int (*disable) (struct usb_ep *ep);
  97482. +
  97483. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  97484. + gfp_t gfp_flags);
  97485. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  97486. +
  97487. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  97488. + gfp_t gfp_flags);
  97489. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  97490. +
  97491. + int (*set_halt) (struct usb_ep *ep, int value);
  97492. + int (*set_wedge) (struct usb_ep *ep);
  97493. +
  97494. + int (*fifo_status) (struct usb_ep *ep);
  97495. + void (*fifo_flush) (struct usb_ep *ep);
  97496. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  97497. + .enable = ep_enable,
  97498. + .disable = ep_disable,
  97499. +
  97500. + .alloc_request = dwc_otg_pcd_alloc_request,
  97501. + .free_request = dwc_otg_pcd_free_request,
  97502. +
  97503. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  97504. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  97505. + .free_buffer = dwc_otg_pcd_free_buffer,
  97506. +#else
  97507. + /* .set_wedge = ep_wedge, */
  97508. + .set_wedge = NULL, /* uses set_halt instead */
  97509. +#endif
  97510. +
  97511. + .queue = ep_queue,
  97512. + .dequeue = ep_dequeue,
  97513. +
  97514. + .set_halt = ep_halt,
  97515. + .fifo_status = 0,
  97516. + .fifo_flush = 0,
  97517. +
  97518. +};
  97519. +
  97520. +#endif /* _EN_ISOC_ */
  97521. +/* Gadget Operations */
  97522. +/**
  97523. + * The following gadget operations will be implemented in the DWC_otg
  97524. + * PCD. Functions in the API that are not described below are not
  97525. + * implemented.
  97526. + *
  97527. + * The Gadget API provides wrapper functions for each of the function
  97528. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  97529. + * wrapper function, which then calls the underlying PCD function. The
  97530. + * following sections are named according to the wrapper functions
  97531. + * (except for ioctl, which doesn't have a wrapper function). Within
  97532. + * each section, the corresponding DWC_otg PCD function name is
  97533. + * specified.
  97534. + *
  97535. + */
  97536. +
  97537. +/**
  97538. + *Gets the USB Frame number of the last SOF.
  97539. + */
  97540. +static int get_frame_number(struct usb_gadget *gadget)
  97541. +{
  97542. + struct gadget_wrapper *d;
  97543. +
  97544. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  97545. +
  97546. + if (gadget == 0) {
  97547. + return -ENODEV;
  97548. + }
  97549. +
  97550. + d = container_of(gadget, struct gadget_wrapper, gadget);
  97551. + return dwc_otg_pcd_get_frame_number(d->pcd);
  97552. +}
  97553. +
  97554. +#ifdef CONFIG_USB_DWC_OTG_LPM
  97555. +static int test_lpm_enabled(struct usb_gadget *gadget)
  97556. +{
  97557. + struct gadget_wrapper *d;
  97558. +
  97559. + d = container_of(gadget, struct gadget_wrapper, gadget);
  97560. +
  97561. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  97562. +}
  97563. +#endif
  97564. +
  97565. +/**
  97566. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  97567. + * session is in progress. If a session is already in progress, but
  97568. + * the device is suspended, remote wakeup signaling is started.
  97569. + *
  97570. + */
  97571. +static int wakeup(struct usb_gadget *gadget)
  97572. +{
  97573. + struct gadget_wrapper *d;
  97574. +
  97575. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  97576. +
  97577. + if (gadget == 0) {
  97578. + return -ENODEV;
  97579. + } else {
  97580. + d = container_of(gadget, struct gadget_wrapper, gadget);
  97581. + }
  97582. + dwc_otg_pcd_wakeup(d->pcd);
  97583. + return 0;
  97584. +}
  97585. +
  97586. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  97587. + .get_frame = get_frame_number,
  97588. + .wakeup = wakeup,
  97589. +#ifdef CONFIG_USB_DWC_OTG_LPM
  97590. + .lpm_support = test_lpm_enabled,
  97591. +#endif
  97592. + // current versions must always be self-powered
  97593. +};
  97594. +
  97595. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  97596. +{
  97597. + int retval = -DWC_E_NOT_SUPPORTED;
  97598. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  97599. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  97600. + (struct usb_ctrlrequest
  97601. + *)bytes);
  97602. + }
  97603. +
  97604. + if (retval == -ENOTSUPP) {
  97605. + retval = -DWC_E_NOT_SUPPORTED;
  97606. + } else if (retval < 0) {
  97607. + retval = -DWC_E_INVALID;
  97608. + }
  97609. +
  97610. + return retval;
  97611. +}
  97612. +
  97613. +#ifdef DWC_EN_ISOC
  97614. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  97615. + void *req_handle, int proc_buf_num)
  97616. +{
  97617. + int i, packet_count;
  97618. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  97619. + struct usb_iso_request *iso_req = req_handle;
  97620. +
  97621. + if (proc_buf_num) {
  97622. + iso_packet = iso_req->iso_packet_desc1;
  97623. + } else {
  97624. + iso_packet = iso_req->iso_packet_desc0;
  97625. + }
  97626. + packet_count =
  97627. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  97628. + for (i = 0; i < packet_count; ++i) {
  97629. + int status;
  97630. + int actual;
  97631. + int offset;
  97632. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  97633. + i, &status, &actual, &offset);
  97634. + switch (status) {
  97635. + case -DWC_E_NO_DATA:
  97636. + status = -ENODATA;
  97637. + break;
  97638. + default:
  97639. + if (status) {
  97640. + DWC_PRINTF("unknown status in isoc packet\n");
  97641. + }
  97642. +
  97643. + }
  97644. + iso_packet[i].status = status;
  97645. + iso_packet[i].offset = offset;
  97646. + iso_packet[i].actual_length = actual;
  97647. + }
  97648. +
  97649. + iso_req->status = 0;
  97650. + iso_req->process_buffer(ep_handle, iso_req);
  97651. +
  97652. + return 0;
  97653. +}
  97654. +#endif /* DWC_EN_ISOC */
  97655. +
  97656. +#ifdef DWC_UTE_PER_IO
  97657. +/**
  97658. + * Copy the contents of the extended request to the Linux usb_request's
  97659. + * extended part and call the gadget's completion.
  97660. + *
  97661. + * @param pcd Pointer to the pcd structure
  97662. + * @param ep_handle Void pointer to the usb_ep structure
  97663. + * @param req_handle Void pointer to the usb_request structure
  97664. + * @param status Request status returned from the portable logic
  97665. + * @param ereq_port Void pointer to the extended request structure
  97666. + * created in the the portable part that contains the
  97667. + * results of the processed iso packets.
  97668. + */
  97669. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  97670. + void *req_handle, int32_t status, void *ereq_port)
  97671. +{
  97672. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  97673. + struct dwc_iso_xreq_port *ereqport = NULL;
  97674. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  97675. + int i;
  97676. + struct usb_request *req;
  97677. + //struct dwc_ute_iso_packet_descriptor *
  97678. + //int status = 0;
  97679. +
  97680. + req = (struct usb_request *)req_handle;
  97681. + ereqorg = &req->ext_req;
  97682. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  97683. + desc_org = ereqorg->per_io_frame_descs;
  97684. +
  97685. + if (req && req->complete) {
  97686. + /* Copy the request data from the portable logic to our request */
  97687. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  97688. + desc_org[i].actual_length =
  97689. + ereqport->per_io_frame_descs[i].actual_length;
  97690. + desc_org[i].status =
  97691. + ereqport->per_io_frame_descs[i].status;
  97692. + }
  97693. +
  97694. + switch (status) {
  97695. + case -DWC_E_SHUTDOWN:
  97696. + req->status = -ESHUTDOWN;
  97697. + break;
  97698. + case -DWC_E_RESTART:
  97699. + req->status = -ECONNRESET;
  97700. + break;
  97701. + case -DWC_E_INVALID:
  97702. + req->status = -EINVAL;
  97703. + break;
  97704. + case -DWC_E_TIMEOUT:
  97705. + req->status = -ETIMEDOUT;
  97706. + break;
  97707. + default:
  97708. + req->status = status;
  97709. + }
  97710. +
  97711. + /* And call the gadget's completion */
  97712. + req->complete(ep_handle, req);
  97713. + }
  97714. +
  97715. + return 0;
  97716. +}
  97717. +#endif /* DWC_UTE_PER_IO */
  97718. +
  97719. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  97720. + void *req_handle, int32_t status, uint32_t actual)
  97721. +{
  97722. + struct usb_request *req = (struct usb_request *)req_handle;
  97723. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  97724. + struct dwc_otg_pcd_ep *ep = NULL;
  97725. +#endif
  97726. +
  97727. + if (req && req->complete) {
  97728. + switch (status) {
  97729. + case -DWC_E_SHUTDOWN:
  97730. + req->status = -ESHUTDOWN;
  97731. + break;
  97732. + case -DWC_E_RESTART:
  97733. + req->status = -ECONNRESET;
  97734. + break;
  97735. + case -DWC_E_INVALID:
  97736. + req->status = -EINVAL;
  97737. + break;
  97738. + case -DWC_E_TIMEOUT:
  97739. + req->status = -ETIMEDOUT;
  97740. + break;
  97741. + default:
  97742. + req->status = status;
  97743. +
  97744. + }
  97745. +
  97746. + req->actual = actual;
  97747. + DWC_SPINUNLOCK(pcd->lock);
  97748. + req->complete(ep_handle, req);
  97749. + DWC_SPINLOCK(pcd->lock);
  97750. + }
  97751. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  97752. + ep = ep_from_handle(pcd, ep_handle);
  97753. + if (GET_CORE_IF(pcd)->dma_enable) {
  97754. + if (req->length != 0) {
  97755. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  97756. + struct device *dev = NULL;
  97757. +
  97758. + if (otg_dev != NULL)
  97759. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  97760. +
  97761. + dma_unmap_single(dev, req->dma, req->length,
  97762. + ep->dwc_ep.is_in ?
  97763. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  97764. + }
  97765. + }
  97766. +#endif
  97767. +
  97768. + return 0;
  97769. +}
  97770. +
  97771. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  97772. +{
  97773. + gadget_wrapper->gadget.speed = speed;
  97774. + return 0;
  97775. +}
  97776. +
  97777. +static int _disconnect(dwc_otg_pcd_t * pcd)
  97778. +{
  97779. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  97780. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  97781. + }
  97782. + return 0;
  97783. +}
  97784. +
  97785. +static int _resume(dwc_otg_pcd_t * pcd)
  97786. +{
  97787. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  97788. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  97789. + }
  97790. +
  97791. + return 0;
  97792. +}
  97793. +
  97794. +static int _suspend(dwc_otg_pcd_t * pcd)
  97795. +{
  97796. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  97797. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  97798. + }
  97799. + return 0;
  97800. +}
  97801. +
  97802. +/**
  97803. + * This function updates the otg values in the gadget structure.
  97804. + */
  97805. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  97806. +{
  97807. +
  97808. + if (!gadget_wrapper->gadget.is_otg)
  97809. + return 0;
  97810. +
  97811. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  97812. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  97813. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  97814. + return 0;
  97815. +}
  97816. +
  97817. +static int _reset(dwc_otg_pcd_t * pcd)
  97818. +{
  97819. + return 0;
  97820. +}
  97821. +
  97822. +#ifdef DWC_UTE_CFI
  97823. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  97824. +{
  97825. + int retval = -DWC_E_INVALID;
  97826. + if (gadget_wrapper->driver->cfi_feature_setup) {
  97827. + retval =
  97828. + gadget_wrapper->driver->
  97829. + cfi_feature_setup(&gadget_wrapper->gadget,
  97830. + (struct cfi_usb_ctrlrequest *)cfi_req);
  97831. + }
  97832. +
  97833. + return retval;
  97834. +}
  97835. +#endif
  97836. +
  97837. +static const struct dwc_otg_pcd_function_ops fops = {
  97838. + .complete = _complete,
  97839. +#ifdef DWC_EN_ISOC
  97840. + .isoc_complete = _isoc_complete,
  97841. +#endif
  97842. + .setup = _setup,
  97843. + .disconnect = _disconnect,
  97844. + .connect = _connect,
  97845. + .resume = _resume,
  97846. + .suspend = _suspend,
  97847. + .hnp_changed = _hnp_changed,
  97848. + .reset = _reset,
  97849. +#ifdef DWC_UTE_CFI
  97850. + .cfi_setup = _cfi_setup,
  97851. +#endif
  97852. +#ifdef DWC_UTE_PER_IO
  97853. + .xisoc_complete = _xisoc_complete,
  97854. +#endif
  97855. +};
  97856. +
  97857. +/**
  97858. + * This function is the top level PCD interrupt handler.
  97859. + */
  97860. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  97861. +{
  97862. + dwc_otg_pcd_t *pcd = dev;
  97863. + int32_t retval = IRQ_NONE;
  97864. +
  97865. + retval = dwc_otg_pcd_handle_intr(pcd);
  97866. + if (retval != 0) {
  97867. + S3C2410X_CLEAR_EINTPEND();
  97868. + }
  97869. + return IRQ_RETVAL(retval);
  97870. +}
  97871. +
  97872. +/**
  97873. + * This function initialized the usb_ep structures to there default
  97874. + * state.
  97875. + *
  97876. + * @param d Pointer on gadget_wrapper.
  97877. + */
  97878. +void gadget_add_eps(struct gadget_wrapper *d)
  97879. +{
  97880. + static const char *names[] = {
  97881. +
  97882. + "ep0",
  97883. + "ep1in",
  97884. + "ep2in",
  97885. + "ep3in",
  97886. + "ep4in",
  97887. + "ep5in",
  97888. + "ep6in",
  97889. + "ep7in",
  97890. + "ep8in",
  97891. + "ep9in",
  97892. + "ep10in",
  97893. + "ep11in",
  97894. + "ep12in",
  97895. + "ep13in",
  97896. + "ep14in",
  97897. + "ep15in",
  97898. + "ep1out",
  97899. + "ep2out",
  97900. + "ep3out",
  97901. + "ep4out",
  97902. + "ep5out",
  97903. + "ep6out",
  97904. + "ep7out",
  97905. + "ep8out",
  97906. + "ep9out",
  97907. + "ep10out",
  97908. + "ep11out",
  97909. + "ep12out",
  97910. + "ep13out",
  97911. + "ep14out",
  97912. + "ep15out"
  97913. + };
  97914. +
  97915. + int i;
  97916. + struct usb_ep *ep;
  97917. + int8_t dev_endpoints;
  97918. +
  97919. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  97920. +
  97921. + INIT_LIST_HEAD(&d->gadget.ep_list);
  97922. + d->gadget.ep0 = &d->ep0;
  97923. + d->gadget.speed = USB_SPEED_UNKNOWN;
  97924. +
  97925. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  97926. +
  97927. + /**
  97928. + * Initialize the EP0 structure.
  97929. + */
  97930. + ep = &d->ep0;
  97931. +
  97932. + /* Init the usb_ep structure. */
  97933. + ep->name = names[0];
  97934. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  97935. +
  97936. + /**
  97937. + * @todo NGS: What should the max packet size be set to
  97938. + * here? Before EP type is set?
  97939. + */
  97940. + ep->maxpacket = MAX_PACKET_SIZE;
  97941. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  97942. +
  97943. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  97944. +
  97945. + /**
  97946. + * Initialize the EP structures.
  97947. + */
  97948. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  97949. +
  97950. + for (i = 0; i < dev_endpoints; i++) {
  97951. + ep = &d->in_ep[i];
  97952. +
  97953. + /* Init the usb_ep structure. */
  97954. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  97955. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  97956. +
  97957. + /**
  97958. + * @todo NGS: What should the max packet size be set to
  97959. + * here? Before EP type is set?
  97960. + */
  97961. + ep->maxpacket = MAX_PACKET_SIZE;
  97962. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  97963. + }
  97964. +
  97965. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  97966. +
  97967. + for (i = 0; i < dev_endpoints; i++) {
  97968. + ep = &d->out_ep[i];
  97969. +
  97970. + /* Init the usb_ep structure. */
  97971. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  97972. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  97973. +
  97974. + /**
  97975. + * @todo NGS: What should the max packet size be set to
  97976. + * here? Before EP type is set?
  97977. + */
  97978. + ep->maxpacket = MAX_PACKET_SIZE;
  97979. +
  97980. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  97981. + }
  97982. +
  97983. + /* remove ep0 from the list. There is a ep0 pointer. */
  97984. + list_del_init(&d->ep0.ep_list);
  97985. +
  97986. + d->ep0.maxpacket = MAX_EP0_SIZE;
  97987. +}
  97988. +
  97989. +/**
  97990. + * This function releases the Gadget device.
  97991. + * required by device_unregister().
  97992. + *
  97993. + * @todo Should this do something? Should it free the PCD?
  97994. + */
  97995. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  97996. +{
  97997. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  97998. +}
  97999. +
  98000. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  98001. +{
  98002. + static char pcd_name[] = "dwc_otg_pcd";
  98003. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  98004. + struct gadget_wrapper *d;
  98005. + int retval;
  98006. +
  98007. + d = DWC_ALLOC(sizeof(*d));
  98008. + if (d == NULL) {
  98009. + return NULL;
  98010. + }
  98011. +
  98012. + memset(d, 0, sizeof(*d));
  98013. +
  98014. + d->gadget.name = pcd_name;
  98015. + d->pcd = otg_dev->pcd;
  98016. +
  98017. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  98018. + strcpy(d->gadget.dev.bus_id, "gadget");
  98019. +#else
  98020. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  98021. +#endif
  98022. +
  98023. + d->gadget.dev.parent = &_dev->dev;
  98024. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  98025. + d->gadget.ops = &dwc_otg_pcd_ops;
  98026. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  98027. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  98028. +
  98029. + d->driver = 0;
  98030. + /* Register the gadget device */
  98031. + retval = device_register(&d->gadget.dev);
  98032. + if (retval != 0) {
  98033. + DWC_ERROR("device_register failed\n");
  98034. + DWC_FREE(d);
  98035. + return NULL;
  98036. + }
  98037. +
  98038. + return d;
  98039. +}
  98040. +
  98041. +static void free_wrapper(struct gadget_wrapper *d)
  98042. +{
  98043. + if (d->driver) {
  98044. + /* should have been done already by driver model core */
  98045. + DWC_WARN("driver '%s' is still registered\n",
  98046. + d->driver->driver.name);
  98047. + usb_gadget_unregister_driver(d->driver);
  98048. + }
  98049. +
  98050. + device_unregister(&d->gadget.dev);
  98051. + DWC_FREE(d);
  98052. +}
  98053. +
  98054. +/**
  98055. + * This function initialized the PCD portion of the driver.
  98056. + *
  98057. + */
  98058. +int pcd_init(dwc_bus_dev_t *_dev)
  98059. +{
  98060. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  98061. + int retval = 0;
  98062. +
  98063. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  98064. +
  98065. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev->core_if);
  98066. +
  98067. + if (!otg_dev->pcd) {
  98068. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  98069. + return -ENOMEM;
  98070. + }
  98071. +
  98072. + otg_dev->pcd->otg_dev = otg_dev;
  98073. + gadget_wrapper = alloc_wrapper(_dev);
  98074. +
  98075. + /*
  98076. + * Initialize EP structures
  98077. + */
  98078. + gadget_add_eps(gadget_wrapper);
  98079. + /*
  98080. + * Setup interupt handler
  98081. + */
  98082. +#ifdef PLATFORM_INTERFACE
  98083. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  98084. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  98085. + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
  98086. + IRQF_SHARED, gadget_wrapper->gadget.name,
  98087. + otg_dev->pcd);
  98088. + if (retval != 0) {
  98089. + DWC_ERROR("request of irq%d failed\n",
  98090. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  98091. + free_wrapper(gadget_wrapper);
  98092. + return -EBUSY;
  98093. + }
  98094. +#else
  98095. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  98096. + _dev->irq);
  98097. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  98098. + IRQF_SHARED | IRQF_DISABLED,
  98099. + gadget_wrapper->gadget.name, otg_dev->pcd);
  98100. + if (retval != 0) {
  98101. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  98102. + free_wrapper(gadget_wrapper);
  98103. + return -EBUSY;
  98104. + }
  98105. +#endif
  98106. +
  98107. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  98108. +
  98109. + return retval;
  98110. +}
  98111. +
  98112. +/**
  98113. + * Cleanup the PCD.
  98114. + */
  98115. +void pcd_remove(dwc_bus_dev_t *_dev)
  98116. +{
  98117. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  98118. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  98119. +
  98120. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  98121. +
  98122. + /*
  98123. + * Free the IRQ
  98124. + */
  98125. +#ifdef PLATFORM_INTERFACE
  98126. + free_irq(platform_get_irq(_dev, 0), pcd);
  98127. +#else
  98128. + free_irq(_dev->irq, pcd);
  98129. +#endif
  98130. + dwc_otg_pcd_remove(otg_dev->pcd);
  98131. + free_wrapper(gadget_wrapper);
  98132. + otg_dev->pcd = 0;
  98133. +}
  98134. +
  98135. +/**
  98136. + * This function registers a gadget driver with the PCD.
  98137. + *
  98138. + * When a driver is successfully registered, it will receive control
  98139. + * requests including set_configuration(), which enables non-control
  98140. + * requests. then usb traffic follows until a disconnect is reported.
  98141. + * then a host may connect again, or the driver might get unbound.
  98142. + *
  98143. + * @param driver The driver being registered
  98144. + * @param bind The bind function of gadget driver
  98145. + */
  98146. +
  98147. +int usb_gadget_probe_driver(struct usb_gadget_driver *driver)
  98148. +{
  98149. + int retval;
  98150. +
  98151. + DWC_DEBUGPL(DBG_PCD, "registering gadget driver '%s'\n",
  98152. + driver->driver.name);
  98153. +
  98154. + if (!driver || driver->max_speed == USB_SPEED_UNKNOWN ||
  98155. + !driver->bind ||
  98156. + !driver->unbind || !driver->disconnect || !driver->setup) {
  98157. + DWC_DEBUGPL(DBG_PCDV, "EINVAL\n");
  98158. + return -EINVAL;
  98159. + }
  98160. + if (gadget_wrapper == 0) {
  98161. + DWC_DEBUGPL(DBG_PCDV, "ENODEV\n");
  98162. + return -ENODEV;
  98163. + }
  98164. + if (gadget_wrapper->driver != 0) {
  98165. + DWC_DEBUGPL(DBG_PCDV, "EBUSY (%p)\n", gadget_wrapper->driver);
  98166. + return -EBUSY;
  98167. + }
  98168. +
  98169. + /* hook up the driver */
  98170. + gadget_wrapper->driver = driver;
  98171. + gadget_wrapper->gadget.dev.driver = &driver->driver;
  98172. +
  98173. + DWC_DEBUGPL(DBG_PCD, "bind to driver %s\n", driver->driver.name);
  98174. + retval = driver->bind(&gadget_wrapper->gadget, gadget_wrapper->driver);
  98175. + if (retval) {
  98176. + DWC_ERROR("bind to driver %s --> error %d\n",
  98177. + driver->driver.name, retval);
  98178. + gadget_wrapper->driver = 0;
  98179. + gadget_wrapper->gadget.dev.driver = 0;
  98180. + return retval;
  98181. + }
  98182. + DWC_DEBUGPL(DBG_ANY, "registered gadget driver '%s'\n",
  98183. + driver->driver.name);
  98184. + return 0;
  98185. +}
  98186. +EXPORT_SYMBOL(usb_gadget_probe_driver);
  98187. +
  98188. +/**
  98189. + * This function unregisters a gadget driver
  98190. + *
  98191. + * @param driver The driver being unregistered
  98192. + */
  98193. +int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
  98194. +{
  98195. + //DWC_DEBUGPL(DBG_PCDV,"%s(%p)\n", __func__, _driver);
  98196. +
  98197. + if (gadget_wrapper == 0) {
  98198. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): s_pcd==0\n", __func__,
  98199. + -ENODEV);
  98200. + return -ENODEV;
  98201. + }
  98202. + if (driver == 0 || driver != gadget_wrapper->driver) {
  98203. + DWC_DEBUGPL(DBG_ANY, "%s Return(%d): driver?\n", __func__,
  98204. + -EINVAL);
  98205. + return -EINVAL;
  98206. + }
  98207. +
  98208. + driver->unbind(&gadget_wrapper->gadget);
  98209. + gadget_wrapper->driver = 0;
  98210. +
  98211. + DWC_DEBUGPL(DBG_ANY, "unregistered driver '%s'\n", driver->driver.name);
  98212. + return 0;
  98213. +}
  98214. +
  98215. +EXPORT_SYMBOL(usb_gadget_unregister_driver);
  98216. +
  98217. +#endif /* DWC_HOST_ONLY */
  98218. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_regs.h linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  98219. --- linux-3.16.2/drivers/usb/host/dwc_otg/dwc_otg_regs.h 1970-01-01 01:00:00.000000000 +0100
  98220. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/dwc_otg_regs.h 2014-09-14 19:04:13.000000000 +0200
  98221. @@ -0,0 +1,2550 @@
  98222. +/* ==========================================================================
  98223. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  98224. + * $Revision: #98 $
  98225. + * $Date: 2012/08/10 $
  98226. + * $Change: 2047372 $
  98227. + *
  98228. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  98229. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  98230. + * otherwise expressly agreed to in writing between Synopsys and you.
  98231. + *
  98232. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  98233. + * any End User Software License Agreement or Agreement for Licensed Product
  98234. + * with Synopsys or any supplement thereto. You are permitted to use and
  98235. + * redistribute this Software in source and binary forms, with or without
  98236. + * modification, provided that redistributions of source code must retain this
  98237. + * notice. You may not view, use, disclose, copy or distribute this file or
  98238. + * any information contained herein except pursuant to this license grant from
  98239. + * Synopsys. If you do not agree with this notice, including the disclaimer
  98240. + * below, then you are not authorized to use the Software.
  98241. + *
  98242. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  98243. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  98244. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  98245. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  98246. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  98247. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  98248. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  98249. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  98250. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  98251. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  98252. + * DAMAGE.
  98253. + * ========================================================================== */
  98254. +
  98255. +#ifndef __DWC_OTG_REGS_H__
  98256. +#define __DWC_OTG_REGS_H__
  98257. +
  98258. +#include "dwc_otg_core_if.h"
  98259. +
  98260. +/**
  98261. + * @file
  98262. + *
  98263. + * This file contains the data structures for accessing the DWC_otg core registers.
  98264. + *
  98265. + * The application interfaces with the HS OTG core by reading from and
  98266. + * writing to the Control and Status Register (CSR) space through the
  98267. + * AHB Slave interface. These registers are 32 bits wide, and the
  98268. + * addresses are 32-bit-block aligned.
  98269. + * CSRs are classified as follows:
  98270. + * - Core Global Registers
  98271. + * - Device Mode Registers
  98272. + * - Device Global Registers
  98273. + * - Device Endpoint Specific Registers
  98274. + * - Host Mode Registers
  98275. + * - Host Global Registers
  98276. + * - Host Port CSRs
  98277. + * - Host Channel Specific Registers
  98278. + *
  98279. + * Only the Core Global registers can be accessed in both Device and
  98280. + * Host modes. When the HS OTG core is operating in one mode, either
  98281. + * Device or Host, the application must not access registers from the
  98282. + * other mode. When the core switches from one mode to another, the
  98283. + * registers in the new mode of operation must be reprogrammed as they
  98284. + * would be after a power-on reset.
  98285. + */
  98286. +
  98287. +/****************************************************************************/
  98288. +/** DWC_otg Core registers .
  98289. + * The dwc_otg_core_global_regs structure defines the size
  98290. + * and relative field offsets for the Core Global registers.
  98291. + */
  98292. +typedef struct dwc_otg_core_global_regs {
  98293. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  98294. + volatile uint32_t gotgctl;
  98295. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  98296. + volatile uint32_t gotgint;
  98297. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  98298. + volatile uint32_t gahbcfg;
  98299. +
  98300. +#define DWC_GLBINTRMASK 0x0001
  98301. +#define DWC_DMAENABLE 0x0020
  98302. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  98303. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  98304. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  98305. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  98306. +
  98307. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  98308. + volatile uint32_t gusbcfg;
  98309. + /**Core Reset Register. <i>Offset: 010h</i> */
  98310. + volatile uint32_t grstctl;
  98311. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  98312. + volatile uint32_t gintsts;
  98313. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  98314. + volatile uint32_t gintmsk;
  98315. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  98316. + volatile uint32_t grxstsr;
  98317. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  98318. + volatile uint32_t grxstsp;
  98319. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  98320. + volatile uint32_t grxfsiz;
  98321. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  98322. + volatile uint32_t gnptxfsiz;
  98323. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  98324. + * Only). <i>Offset: 02Ch</i> */
  98325. + volatile uint32_t gnptxsts;
  98326. + /**I2C Access Register. <i>Offset: 030h</i> */
  98327. + volatile uint32_t gi2cctl;
  98328. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  98329. + volatile uint32_t gpvndctl;
  98330. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  98331. + volatile uint32_t ggpio;
  98332. + /**User ID Register. <i>Offset: 03Ch</i> */
  98333. + volatile uint32_t guid;
  98334. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  98335. + volatile uint32_t gsnpsid;
  98336. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  98337. + volatile uint32_t ghwcfg1;
  98338. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  98339. + volatile uint32_t ghwcfg2;
  98340. +#define DWC_SLAVE_ONLY_ARCH 0
  98341. +#define DWC_EXT_DMA_ARCH 1
  98342. +#define DWC_INT_DMA_ARCH 2
  98343. +
  98344. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  98345. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  98346. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  98347. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  98348. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  98349. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  98350. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  98351. +
  98352. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  98353. + volatile uint32_t ghwcfg3;
  98354. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  98355. + volatile uint32_t ghwcfg4;
  98356. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  98357. + volatile uint32_t glpmcfg;
  98358. + /** Global PowerDn Register <i>Offset: 058h</i> */
  98359. + volatile uint32_t gpwrdn;
  98360. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  98361. + volatile uint32_t gdfifocfg;
  98362. + /** ADP Control Register <i>Offset: 060h</i> */
  98363. + volatile uint32_t adpctl;
  98364. + /** Reserved <i>Offset: 064h-0FFh</i> */
  98365. + volatile uint32_t reserved39[39];
  98366. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  98367. + volatile uint32_t hptxfsiz;
  98368. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  98369. + otherwise Device Transmit FIFO#n Register.
  98370. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  98371. + volatile uint32_t dtxfsiz[15];
  98372. +} dwc_otg_core_global_regs_t;
  98373. +
  98374. +/**
  98375. + * This union represents the bit fields of the Core OTG Control
  98376. + * and Status Register (GOTGCTL). Set the bits using the bit
  98377. + * fields then write the <i>d32</i> value to the register.
  98378. + */
  98379. +typedef union gotgctl_data {
  98380. + /** raw register data */
  98381. + uint32_t d32;
  98382. + /** register bits */
  98383. + struct {
  98384. + unsigned sesreqscs:1;
  98385. + unsigned sesreq:1;
  98386. + unsigned vbvalidoven:1;
  98387. + unsigned vbvalidovval:1;
  98388. + unsigned avalidoven:1;
  98389. + unsigned avalidovval:1;
  98390. + unsigned bvalidoven:1;
  98391. + unsigned bvalidovval:1;
  98392. + unsigned hstnegscs:1;
  98393. + unsigned hnpreq:1;
  98394. + unsigned hstsethnpen:1;
  98395. + unsigned devhnpen:1;
  98396. + unsigned reserved12_15:4;
  98397. + unsigned conidsts:1;
  98398. + unsigned dbnctime:1;
  98399. + unsigned asesvld:1;
  98400. + unsigned bsesvld:1;
  98401. + unsigned otgver:1;
  98402. + unsigned reserved1:1;
  98403. + unsigned multvalidbc:5;
  98404. + unsigned chirpen:1;
  98405. + unsigned reserved28_31:4;
  98406. + } b;
  98407. +} gotgctl_data_t;
  98408. +
  98409. +/**
  98410. + * This union represents the bit fields of the Core OTG Interrupt Register
  98411. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  98412. + * value to the register.
  98413. + */
  98414. +typedef union gotgint_data {
  98415. + /** raw register data */
  98416. + uint32_t d32;
  98417. + /** register bits */
  98418. + struct {
  98419. + /** Current Mode */
  98420. + unsigned reserved0_1:2;
  98421. +
  98422. + /** Session End Detected */
  98423. + unsigned sesenddet:1;
  98424. +
  98425. + unsigned reserved3_7:5;
  98426. +
  98427. + /** Session Request Success Status Change */
  98428. + unsigned sesreqsucstschng:1;
  98429. + /** Host Negotiation Success Status Change */
  98430. + unsigned hstnegsucstschng:1;
  98431. +
  98432. + unsigned reserved10_16:7;
  98433. +
  98434. + /** Host Negotiation Detected */
  98435. + unsigned hstnegdet:1;
  98436. + /** A-Device Timeout Change */
  98437. + unsigned adevtoutchng:1;
  98438. + /** Debounce Done */
  98439. + unsigned debdone:1;
  98440. + /** Multi-Valued input changed */
  98441. + unsigned mvic:1;
  98442. +
  98443. + unsigned reserved31_21:11;
  98444. +
  98445. + } b;
  98446. +} gotgint_data_t;
  98447. +
  98448. +/**
  98449. + * This union represents the bit fields of the Core AHB Configuration
  98450. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  98451. + * write the <i>d32</i> value to the register.
  98452. + */
  98453. +typedef union gahbcfg_data {
  98454. + /** raw register data */
  98455. + uint32_t d32;
  98456. + /** register bits */
  98457. + struct {
  98458. + unsigned glblintrmsk:1;
  98459. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  98460. +
  98461. + unsigned hburstlen:4;
  98462. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  98463. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  98464. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  98465. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  98466. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  98467. +
  98468. + unsigned dmaenable:1;
  98469. +#define DWC_GAHBCFG_DMAENABLE 1
  98470. + unsigned reserved:1;
  98471. + unsigned nptxfemplvl_txfemplvl:1;
  98472. + unsigned ptxfemplvl:1;
  98473. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  98474. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  98475. + unsigned reserved9_20:12;
  98476. + unsigned remmemsupp:1;
  98477. + unsigned notialldmawrit:1;
  98478. + unsigned ahbsingle:1;
  98479. + unsigned reserved24_31:8;
  98480. + } b;
  98481. +} gahbcfg_data_t;
  98482. +
  98483. +/**
  98484. + * This union represents the bit fields of the Core USB Configuration
  98485. + * Register (GUSBCFG). Set the bits using the bit fields then write
  98486. + * the <i>d32</i> value to the register.
  98487. + */
  98488. +typedef union gusbcfg_data {
  98489. + /** raw register data */
  98490. + uint32_t d32;
  98491. + /** register bits */
  98492. + struct {
  98493. + unsigned toutcal:3;
  98494. + unsigned phyif:1;
  98495. + unsigned ulpi_utmi_sel:1;
  98496. + unsigned fsintf:1;
  98497. + unsigned physel:1;
  98498. + unsigned ddrsel:1;
  98499. + unsigned srpcap:1;
  98500. + unsigned hnpcap:1;
  98501. + unsigned usbtrdtim:4;
  98502. + unsigned reserved1:1;
  98503. + unsigned phylpwrclksel:1;
  98504. + unsigned otgutmifssel:1;
  98505. + unsigned ulpi_fsls:1;
  98506. + unsigned ulpi_auto_res:1;
  98507. + unsigned ulpi_clk_sus_m:1;
  98508. + unsigned ulpi_ext_vbus_drv:1;
  98509. + unsigned ulpi_int_vbus_indicator:1;
  98510. + unsigned term_sel_dl_pulse:1;
  98511. + unsigned indicator_complement:1;
  98512. + unsigned indicator_pass_through:1;
  98513. + unsigned ulpi_int_prot_dis:1;
  98514. + unsigned ic_usb_cap:1;
  98515. + unsigned ic_traffic_pull_remove:1;
  98516. + unsigned tx_end_delay:1;
  98517. + unsigned force_host_mode:1;
  98518. + unsigned force_dev_mode:1;
  98519. + unsigned reserved31:1;
  98520. + } b;
  98521. +} gusbcfg_data_t;
  98522. +
  98523. +/**
  98524. + * This union represents the bit fields of the Core Reset Register
  98525. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  98526. + * <i>d32</i> value to the register.
  98527. + */
  98528. +typedef union grstctl_data {
  98529. + /** raw register data */
  98530. + uint32_t d32;
  98531. + /** register bits */
  98532. + struct {
  98533. + /** Core Soft Reset (CSftRst) (Device and Host)
  98534. + *
  98535. + * The application can flush the control logic in the
  98536. + * entire core using this bit. This bit resets the
  98537. + * pipelines in the AHB Clock domain as well as the
  98538. + * PHY Clock domain.
  98539. + *
  98540. + * The state machines are reset to an IDLE state, the
  98541. + * control bits in the CSRs are cleared, all the
  98542. + * transmit FIFOs and the receive FIFO are flushed.
  98543. + *
  98544. + * The status mask bits that control the generation of
  98545. + * the interrupt, are cleared, to clear the
  98546. + * interrupt. The interrupt status bits are not
  98547. + * cleared, so the application can get the status of
  98548. + * any events that occurred in the core after it has
  98549. + * set this bit.
  98550. + *
  98551. + * Any transactions on the AHB are terminated as soon
  98552. + * as possible following the protocol. Any
  98553. + * transactions on the USB are terminated immediately.
  98554. + *
  98555. + * The configuration settings in the CSRs are
  98556. + * unchanged, so the software doesn't have to
  98557. + * reprogram these registers (Device
  98558. + * Configuration/Host Configuration/Core System
  98559. + * Configuration/Core PHY Configuration).
  98560. + *
  98561. + * The application can write to this bit, any time it
  98562. + * wants to reset the core. This is a self clearing
  98563. + * bit and the core clears this bit after all the
  98564. + * necessary logic is reset in the core, which may
  98565. + * take several clocks, depending on the current state
  98566. + * of the core.
  98567. + */
  98568. + unsigned csftrst:1;
  98569. + /** Hclk Soft Reset
  98570. + *
  98571. + * The application uses this bit to reset the control logic in
  98572. + * the AHB clock domain. Only AHB clock domain pipelines are
  98573. + * reset.
  98574. + */
  98575. + unsigned hsftrst:1;
  98576. + /** Host Frame Counter Reset (Host Only)<br>
  98577. + *
  98578. + * The application can reset the (micro)frame number
  98579. + * counter inside the core, using this bit. When the
  98580. + * (micro)frame counter is reset, the subsequent SOF
  98581. + * sent out by the core, will have a (micro)frame
  98582. + * number of 0.
  98583. + */
  98584. + unsigned hstfrm:1;
  98585. + /** In Token Sequence Learning Queue Flush
  98586. + * (INTknQFlsh) (Device Only)
  98587. + */
  98588. + unsigned intknqflsh:1;
  98589. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  98590. + *
  98591. + * The application can flush the entire Receive FIFO
  98592. + * using this bit. The application must first
  98593. + * ensure that the core is not in the middle of a
  98594. + * transaction. The application should write into
  98595. + * this bit, only after making sure that neither the
  98596. + * DMA engine is reading from the RxFIFO nor the MAC
  98597. + * is writing the data in to the FIFO. The
  98598. + * application should wait until the bit is cleared
  98599. + * before performing any other operations. This bit
  98600. + * will takes 8 clocks (slowest of PHY or AHB clock)
  98601. + * to clear.
  98602. + */
  98603. + unsigned rxfflsh:1;
  98604. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  98605. + *
  98606. + * This bit is used to selectively flush a single or
  98607. + * all transmit FIFOs. The application must first
  98608. + * ensure that the core is not in the middle of a
  98609. + * transaction. The application should write into
  98610. + * this bit, only after making sure that neither the
  98611. + * DMA engine is writing into the TxFIFO nor the MAC
  98612. + * is reading the data out of the FIFO. The
  98613. + * application should wait until the core clears this
  98614. + * bit, before performing any operations. This bit
  98615. + * will takes 8 clocks (slowest of PHY or AHB clock)
  98616. + * to clear.
  98617. + */
  98618. + unsigned txfflsh:1;
  98619. +
  98620. + /** TxFIFO Number (TxFNum) (Device and Host).
  98621. + *
  98622. + * This is the FIFO number which needs to be flushed,
  98623. + * using the TxFIFO Flush bit. This field should not
  98624. + * be changed until the TxFIFO Flush bit is cleared by
  98625. + * the core.
  98626. + * - 0x0 : Non Periodic TxFIFO Flush
  98627. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  98628. + * or Periodic TxFIFO in host mode
  98629. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  98630. + * - ...
  98631. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  98632. + * - 0x10: Flush all the Transmit NonPeriodic and
  98633. + * Transmit Periodic FIFOs in the core
  98634. + */
  98635. + unsigned txfnum:5;
  98636. + /** Reserved */
  98637. + unsigned reserved11_29:19;
  98638. + /** DMA Request Signal. Indicated DMA request is in
  98639. + * probress. Used for debug purpose. */
  98640. + unsigned dmareq:1;
  98641. + /** AHB Master Idle. Indicates the AHB Master State
  98642. + * Machine is in IDLE condition. */
  98643. + unsigned ahbidle:1;
  98644. + } b;
  98645. +} grstctl_t;
  98646. +
  98647. +/**
  98648. + * This union represents the bit fields of the Core Interrupt Mask
  98649. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  98650. + * write the <i>d32</i> value to the register.
  98651. + */
  98652. +typedef union gintmsk_data {
  98653. + /** raw register data */
  98654. + uint32_t d32;
  98655. + /** register bits */
  98656. + struct {
  98657. + unsigned reserved0:1;
  98658. + unsigned modemismatch:1;
  98659. + unsigned otgintr:1;
  98660. + unsigned sofintr:1;
  98661. + unsigned rxstsqlvl:1;
  98662. + unsigned nptxfempty:1;
  98663. + unsigned ginnakeff:1;
  98664. + unsigned goutnakeff:1;
  98665. + unsigned ulpickint:1;
  98666. + unsigned i2cintr:1;
  98667. + unsigned erlysuspend:1;
  98668. + unsigned usbsuspend:1;
  98669. + unsigned usbreset:1;
  98670. + unsigned enumdone:1;
  98671. + unsigned isooutdrop:1;
  98672. + unsigned eopframe:1;
  98673. + unsigned restoredone:1;
  98674. + unsigned epmismatch:1;
  98675. + unsigned inepintr:1;
  98676. + unsigned outepintr:1;
  98677. + unsigned incomplisoin:1;
  98678. + unsigned incomplisoout:1;
  98679. + unsigned fetsusp:1;
  98680. + unsigned resetdet:1;
  98681. + unsigned portintr:1;
  98682. + unsigned hcintr:1;
  98683. + unsigned ptxfempty:1;
  98684. + unsigned lpmtranrcvd:1;
  98685. + unsigned conidstschng:1;
  98686. + unsigned disconnect:1;
  98687. + unsigned sessreqintr:1;
  98688. + unsigned wkupintr:1;
  98689. + } b;
  98690. +} gintmsk_data_t;
  98691. +/**
  98692. + * This union represents the bit fields of the Core Interrupt Register
  98693. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  98694. + * <i>d32</i> value to the register.
  98695. + */
  98696. +typedef union gintsts_data {
  98697. + /** raw register data */
  98698. + uint32_t d32;
  98699. +#define DWC_SOF_INTR_MASK 0x0008
  98700. + /** register bits */
  98701. + struct {
  98702. +#define DWC_HOST_MODE 1
  98703. + unsigned curmode:1;
  98704. + unsigned modemismatch:1;
  98705. + unsigned otgintr:1;
  98706. + unsigned sofintr:1;
  98707. + unsigned rxstsqlvl:1;
  98708. + unsigned nptxfempty:1;
  98709. + unsigned ginnakeff:1;
  98710. + unsigned goutnakeff:1;
  98711. + unsigned ulpickint:1;
  98712. + unsigned i2cintr:1;
  98713. + unsigned erlysuspend:1;
  98714. + unsigned usbsuspend:1;
  98715. + unsigned usbreset:1;
  98716. + unsigned enumdone:1;
  98717. + unsigned isooutdrop:1;
  98718. + unsigned eopframe:1;
  98719. + unsigned restoredone:1;
  98720. + unsigned epmismatch:1;
  98721. + unsigned inepint:1;
  98722. + unsigned outepintr:1;
  98723. + unsigned incomplisoin:1;
  98724. + unsigned incomplisoout:1;
  98725. + unsigned fetsusp:1;
  98726. + unsigned resetdet:1;
  98727. + unsigned portintr:1;
  98728. + unsigned hcintr:1;
  98729. + unsigned ptxfempty:1;
  98730. + unsigned lpmtranrcvd:1;
  98731. + unsigned conidstschng:1;
  98732. + unsigned disconnect:1;
  98733. + unsigned sessreqintr:1;
  98734. + unsigned wkupintr:1;
  98735. + } b;
  98736. +} gintsts_data_t;
  98737. +
  98738. +/**
  98739. + * This union represents the bit fields in the Device Receive Status Read and
  98740. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  98741. + * element then read out the bits using the <i>b</i>it elements.
  98742. + */
  98743. +typedef union device_grxsts_data {
  98744. + /** raw register data */
  98745. + uint32_t d32;
  98746. + /** register bits */
  98747. + struct {
  98748. + unsigned epnum:4;
  98749. + unsigned bcnt:11;
  98750. + unsigned dpid:2;
  98751. +
  98752. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  98753. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  98754. +
  98755. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  98756. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  98757. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  98758. + unsigned pktsts:4;
  98759. + unsigned fn:4;
  98760. + unsigned reserved25_31:7;
  98761. + } b;
  98762. +} device_grxsts_data_t;
  98763. +
  98764. +/**
  98765. + * This union represents the bit fields in the Host Receive Status Read and
  98766. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  98767. + * element then read out the bits using the <i>b</i>it elements.
  98768. + */
  98769. +typedef union host_grxsts_data {
  98770. + /** raw register data */
  98771. + uint32_t d32;
  98772. + /** register bits */
  98773. + struct {
  98774. + unsigned chnum:4;
  98775. + unsigned bcnt:11;
  98776. + unsigned dpid:2;
  98777. +
  98778. + unsigned pktsts:4;
  98779. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  98780. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  98781. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  98782. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  98783. +
  98784. + unsigned reserved21_31:11;
  98785. + } b;
  98786. +} host_grxsts_data_t;
  98787. +
  98788. +/**
  98789. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  98790. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  98791. + * then read out the bits using the <i>b</i>it elements.
  98792. + */
  98793. +typedef union fifosize_data {
  98794. + /** raw register data */
  98795. + uint32_t d32;
  98796. + /** register bits */
  98797. + struct {
  98798. + unsigned startaddr:16;
  98799. + unsigned depth:16;
  98800. + } b;
  98801. +} fifosize_data_t;
  98802. +
  98803. +/**
  98804. + * This union represents the bit fields in the Non-Periodic Transmit
  98805. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  98806. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  98807. + * elements.
  98808. + */
  98809. +typedef union gnptxsts_data {
  98810. + /** raw register data */
  98811. + uint32_t d32;
  98812. + /** register bits */
  98813. + struct {
  98814. + unsigned nptxfspcavail:16;
  98815. + unsigned nptxqspcavail:8;
  98816. + /** Top of the Non-Periodic Transmit Request Queue
  98817. + * - bit 24 - Terminate (Last entry for the selected
  98818. + * channel/EP)
  98819. + * - bits 26:25 - Token Type
  98820. + * - 2'b00 - IN/OUT
  98821. + * - 2'b01 - Zero Length OUT
  98822. + * - 2'b10 - PING/Complete Split
  98823. + * - 2'b11 - Channel Halt
  98824. + * - bits 30:27 - Channel/EP Number
  98825. + */
  98826. + unsigned nptxqtop_terminate:1;
  98827. + unsigned nptxqtop_token:2;
  98828. + unsigned nptxqtop_chnep:4;
  98829. + unsigned reserved:1;
  98830. + } b;
  98831. +} gnptxsts_data_t;
  98832. +
  98833. +/**
  98834. + * This union represents the bit fields in the Transmit
  98835. + * FIFO Status Register (DTXFSTS). Read the register into the
  98836. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  98837. + * elements.
  98838. + */
  98839. +typedef union dtxfsts_data {
  98840. + /** raw register data */
  98841. + uint32_t d32;
  98842. + /** register bits */
  98843. + struct {
  98844. + unsigned txfspcavail:16;
  98845. + unsigned reserved:16;
  98846. + } b;
  98847. +} dtxfsts_data_t;
  98848. +
  98849. +/**
  98850. + * This union represents the bit fields in the I2C Control Register
  98851. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  98852. + * bits using the <i>b</i>it elements.
  98853. + */
  98854. +typedef union gi2cctl_data {
  98855. + /** raw register data */
  98856. + uint32_t d32;
  98857. + /** register bits */
  98858. + struct {
  98859. + unsigned rwdata:8;
  98860. + unsigned regaddr:8;
  98861. + unsigned addr:7;
  98862. + unsigned i2cen:1;
  98863. + unsigned ack:1;
  98864. + unsigned i2csuspctl:1;
  98865. + unsigned i2cdevaddr:2;
  98866. + unsigned i2cdatse0:1;
  98867. + unsigned reserved:1;
  98868. + unsigned rw:1;
  98869. + unsigned bsydne:1;
  98870. + } b;
  98871. +} gi2cctl_data_t;
  98872. +
  98873. +/**
  98874. + * This union represents the bit fields in the PHY Vendor Control Register
  98875. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  98876. + * bits using the <i>b</i>it elements.
  98877. + */
  98878. +typedef union gpvndctl_data {
  98879. + /** raw register data */
  98880. + uint32_t d32;
  98881. + /** register bits */
  98882. + struct {
  98883. + unsigned regdata:8;
  98884. + unsigned vctrl:8;
  98885. + unsigned regaddr16_21:6;
  98886. + unsigned regwr:1;
  98887. + unsigned reserved23_24:2;
  98888. + unsigned newregreq:1;
  98889. + unsigned vstsbsy:1;
  98890. + unsigned vstsdone:1;
  98891. + unsigned reserved28_30:3;
  98892. + unsigned disulpidrvr:1;
  98893. + } b;
  98894. +} gpvndctl_data_t;
  98895. +
  98896. +/**
  98897. + * This union represents the bit fields in the General Purpose
  98898. + * Input/Output Register (GGPIO).
  98899. + * Read the register into the <i>d32</i> element then read out the
  98900. + * bits using the <i>b</i>it elements.
  98901. + */
  98902. +typedef union ggpio_data {
  98903. + /** raw register data */
  98904. + uint32_t d32;
  98905. + /** register bits */
  98906. + struct {
  98907. + unsigned gpi:16;
  98908. + unsigned gpo:16;
  98909. + } b;
  98910. +} ggpio_data_t;
  98911. +
  98912. +/**
  98913. + * This union represents the bit fields in the User ID Register
  98914. + * (GUID). Read the register into the <i>d32</i> element then read out the
  98915. + * bits using the <i>b</i>it elements.
  98916. + */
  98917. +typedef union guid_data {
  98918. + /** raw register data */
  98919. + uint32_t d32;
  98920. + /** register bits */
  98921. + struct {
  98922. + unsigned rwdata:32;
  98923. + } b;
  98924. +} guid_data_t;
  98925. +
  98926. +/**
  98927. + * This union represents the bit fields in the Synopsys ID Register
  98928. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  98929. + * bits using the <i>b</i>it elements.
  98930. + */
  98931. +typedef union gsnpsid_data {
  98932. + /** raw register data */
  98933. + uint32_t d32;
  98934. + /** register bits */
  98935. + struct {
  98936. + unsigned rwdata:32;
  98937. + } b;
  98938. +} gsnpsid_data_t;
  98939. +
  98940. +/**
  98941. + * This union represents the bit fields in the User HW Config1
  98942. + * Register. Read the register into the <i>d32</i> element then read
  98943. + * out the bits using the <i>b</i>it elements.
  98944. + */
  98945. +typedef union hwcfg1_data {
  98946. + /** raw register data */
  98947. + uint32_t d32;
  98948. + /** register bits */
  98949. + struct {
  98950. + unsigned ep_dir0:2;
  98951. + unsigned ep_dir1:2;
  98952. + unsigned ep_dir2:2;
  98953. + unsigned ep_dir3:2;
  98954. + unsigned ep_dir4:2;
  98955. + unsigned ep_dir5:2;
  98956. + unsigned ep_dir6:2;
  98957. + unsigned ep_dir7:2;
  98958. + unsigned ep_dir8:2;
  98959. + unsigned ep_dir9:2;
  98960. + unsigned ep_dir10:2;
  98961. + unsigned ep_dir11:2;
  98962. + unsigned ep_dir12:2;
  98963. + unsigned ep_dir13:2;
  98964. + unsigned ep_dir14:2;
  98965. + unsigned ep_dir15:2;
  98966. + } b;
  98967. +} hwcfg1_data_t;
  98968. +
  98969. +/**
  98970. + * This union represents the bit fields in the User HW Config2
  98971. + * Register. Read the register into the <i>d32</i> element then read
  98972. + * out the bits using the <i>b</i>it elements.
  98973. + */
  98974. +typedef union hwcfg2_data {
  98975. + /** raw register data */
  98976. + uint32_t d32;
  98977. + /** register bits */
  98978. + struct {
  98979. + /* GHWCFG2 */
  98980. + unsigned op_mode:3;
  98981. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  98982. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  98983. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  98984. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  98985. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  98986. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  98987. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  98988. +
  98989. + unsigned architecture:2;
  98990. + unsigned point2point:1;
  98991. + unsigned hs_phy_type:2;
  98992. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  98993. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  98994. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  98995. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  98996. +
  98997. + unsigned fs_phy_type:2;
  98998. + unsigned num_dev_ep:4;
  98999. + unsigned num_host_chan:4;
  99000. + unsigned perio_ep_supported:1;
  99001. + unsigned dynamic_fifo:1;
  99002. + unsigned multi_proc_int:1;
  99003. + unsigned reserved21:1;
  99004. + unsigned nonperio_tx_q_depth:2;
  99005. + unsigned host_perio_tx_q_depth:2;
  99006. + unsigned dev_token_q_depth:5;
  99007. + unsigned otg_enable_ic_usb:1;
  99008. + } b;
  99009. +} hwcfg2_data_t;
  99010. +
  99011. +/**
  99012. + * This union represents the bit fields in the User HW Config3
  99013. + * Register. Read the register into the <i>d32</i> element then read
  99014. + * out the bits using the <i>b</i>it elements.
  99015. + */
  99016. +typedef union hwcfg3_data {
  99017. + /** raw register data */
  99018. + uint32_t d32;
  99019. + /** register bits */
  99020. + struct {
  99021. + /* GHWCFG3 */
  99022. + unsigned xfer_size_cntr_width:4;
  99023. + unsigned packet_size_cntr_width:3;
  99024. + unsigned otg_func:1;
  99025. + unsigned i2c:1;
  99026. + unsigned vendor_ctrl_if:1;
  99027. + unsigned optional_features:1;
  99028. + unsigned synch_reset_type:1;
  99029. + unsigned adp_supp:1;
  99030. + unsigned otg_enable_hsic:1;
  99031. + unsigned bc_support:1;
  99032. + unsigned otg_lpm_en:1;
  99033. + unsigned dfifo_depth:16;
  99034. + } b;
  99035. +} hwcfg3_data_t;
  99036. +
  99037. +/**
  99038. + * This union represents the bit fields in the User HW Config4
  99039. + * Register. Read the register into the <i>d32</i> element then read
  99040. + * out the bits using the <i>b</i>it elements.
  99041. + */
  99042. +typedef union hwcfg4_data {
  99043. + /** raw register data */
  99044. + uint32_t d32;
  99045. + /** register bits */
  99046. + struct {
  99047. + unsigned num_dev_perio_in_ep:4;
  99048. + unsigned power_optimiz:1;
  99049. + unsigned min_ahb_freq:1;
  99050. + unsigned hiber:1;
  99051. + unsigned xhiber:1;
  99052. + unsigned reserved:6;
  99053. + unsigned utmi_phy_data_width:2;
  99054. + unsigned num_dev_mode_ctrl_ep:4;
  99055. + unsigned iddig_filt_en:1;
  99056. + unsigned vbus_valid_filt_en:1;
  99057. + unsigned a_valid_filt_en:1;
  99058. + unsigned b_valid_filt_en:1;
  99059. + unsigned session_end_filt_en:1;
  99060. + unsigned ded_fifo_en:1;
  99061. + unsigned num_in_eps:4;
  99062. + unsigned desc_dma:1;
  99063. + unsigned desc_dma_dyn:1;
  99064. + } b;
  99065. +} hwcfg4_data_t;
  99066. +
  99067. +/**
  99068. + * This union represents the bit fields of the Core LPM Configuration
  99069. + * Register (GLPMCFG). Set the bits using bit fields then write
  99070. + * the <i>d32</i> value to the register.
  99071. + */
  99072. +typedef union glpmctl_data {
  99073. + /** raw register data */
  99074. + uint32_t d32;
  99075. + /** register bits */
  99076. + struct {
  99077. + /** LPM-Capable (LPMCap) (Device and Host)
  99078. + * The application uses this bit to control
  99079. + * the DWC_otg core LPM capabilities.
  99080. + */
  99081. + unsigned lpm_cap_en:1;
  99082. + /** LPM response programmed by application (AppL1Res) (Device)
  99083. + * Handshake response to LPM token pre-programmed
  99084. + * by device application software.
  99085. + */
  99086. + unsigned appl_resp:1;
  99087. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  99088. + * In Host mode this field indicates the value of HIRD
  99089. + * to be sent in an LPM transaction.
  99090. + * In Device mode this field is updated with the
  99091. + * Received LPM Token HIRD bmAttribute
  99092. + * when an ACK/NYET/STALL response is sent
  99093. + * to an LPM transaction.
  99094. + */
  99095. + unsigned hird:4;
  99096. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  99097. + * In Host mode this bit indicates the value of remote
  99098. + * wake up to be sent in wIndex field of LPM transaction.
  99099. + * In Device mode this field is updated with the
  99100. + * Received LPM Token bRemoteWake bmAttribute
  99101. + * when an ACK/NYET/STALL response is sent
  99102. + * to an LPM transaction.
  99103. + */
  99104. + unsigned rem_wkup_en:1;
  99105. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  99106. + * The application uses this bit to control
  99107. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  99108. + */
  99109. + unsigned en_utmi_sleep:1;
  99110. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  99111. + */
  99112. + unsigned hird_thres:5;
  99113. + /** LPM Response (CoreL1Res) (Device and Host)
  99114. + * In Host mode this bit contains handsake response to
  99115. + * LPM transaction.
  99116. + * In Device mode the response of the core to
  99117. + * LPM transaction received is reflected in these two bits.
  99118. + - 0x0 : ERROR (No handshake response)
  99119. + - 0x1 : STALL
  99120. + - 0x2 : NYET
  99121. + - 0x3 : ACK
  99122. + */
  99123. + unsigned lpm_resp:2;
  99124. + /** Port Sleep Status (SlpSts) (Device and Host)
  99125. + * This bit is set as long as a Sleep condition
  99126. + * is present on the USB bus.
  99127. + */
  99128. + unsigned prt_sleep_sts:1;
  99129. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  99130. + * Indicates that the application or host
  99131. + * can start resume from Sleep state.
  99132. + */
  99133. + unsigned sleep_state_resumeok:1;
  99134. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  99135. + * The channel number on which the LPM transaction
  99136. + * has to be applied while sending
  99137. + * an LPM transaction to the local device.
  99138. + */
  99139. + unsigned lpm_chan_index:4;
  99140. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  99141. + * Number host retries that would be performed
  99142. + * if the device response was not valid response.
  99143. + */
  99144. + unsigned retry_count:3;
  99145. + /** Send LPM Transaction (SndLPM) (Host)
  99146. + * When set by application software,
  99147. + * an LPM transaction containing two tokens
  99148. + * is sent.
  99149. + */
  99150. + unsigned send_lpm:1;
  99151. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  99152. + * Number of LPM Host Retries still remaining
  99153. + * to be transmitted for the current LPM sequence
  99154. + */
  99155. + unsigned retry_count_sts:3;
  99156. + unsigned reserved28_29:2;
  99157. + /** In host mode once this bit is set, the host
  99158. + * configures to drive the HSIC Idle state on the bus.
  99159. + * It then waits for the device to initiate the Connect sequence.
  99160. + * In device mode once this bit is set, the device waits for
  99161. + * the HSIC Idle line state on the bus. Upon receving the Idle
  99162. + * line state, it initiates the HSIC Connect sequence.
  99163. + */
  99164. + unsigned hsic_connect:1;
  99165. + /** This bit overrides and functionally inverts
  99166. + * the if_select_hsic input port signal.
  99167. + */
  99168. + unsigned inv_sel_hsic:1;
  99169. + } b;
  99170. +} glpmcfg_data_t;
  99171. +
  99172. +/**
  99173. + * This union represents the bit fields of the Core ADP Timer, Control and
  99174. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  99175. + * the <i>d32</i> value to the register.
  99176. + */
  99177. +typedef union adpctl_data {
  99178. + /** raw register data */
  99179. + uint32_t d32;
  99180. + /** register bits */
  99181. + struct {
  99182. + /** Probe Discharge (PRB_DSCHG)
  99183. + * These bits set the times for TADP_DSCHG.
  99184. + * These bits are defined as follows:
  99185. + * 2'b00 - 4 msec
  99186. + * 2'b01 - 8 msec
  99187. + * 2'b10 - 16 msec
  99188. + * 2'b11 - 32 msec
  99189. + */
  99190. + unsigned prb_dschg:2;
  99191. + /** Probe Delta (PRB_DELTA)
  99192. + * These bits set the resolution for RTIM value.
  99193. + * The bits are defined in units of 32 kHz clock cycles as follows:
  99194. + * 2'b00 - 1 cycles
  99195. + * 2'b01 - 2 cycles
  99196. + * 2'b10 - 3 cycles
  99197. + * 2'b11 - 4 cycles
  99198. + * For example if this value is chosen to 2'b01, it means that RTIM
  99199. + * increments for every 3(three) 32Khz clock cycles.
  99200. + */
  99201. + unsigned prb_delta:2;
  99202. + /** Probe Period (PRB_PER)
  99203. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  99204. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  99205. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  99206. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  99207. + * 2'b11 - Reserved
  99208. + */
  99209. + unsigned prb_per:2;
  99210. + /** These bits capture the latest time it took for VBUS to ramp from
  99211. + * VADP_SINK to VADP_PRB.
  99212. + * 0x000 - 1 cycles
  99213. + * 0x001 - 2 cycles
  99214. + * 0x002 - 3 cycles
  99215. + * etc
  99216. + * 0x7FF - 2048 cycles
  99217. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  99218. + */
  99219. + unsigned rtim:11;
  99220. + /** Enable Probe (EnaPrb)
  99221. + * When programmed to 1'b1, the core performs a probe operation.
  99222. + * This bit is valid only if OTG_Ver = 1'b1.
  99223. + */
  99224. + unsigned enaprb:1;
  99225. + /** Enable Sense (EnaSns)
  99226. + * When programmed to 1'b1, the core performs a Sense operation.
  99227. + * This bit is valid only if OTG_Ver = 1'b1.
  99228. + */
  99229. + unsigned enasns:1;
  99230. + /** ADP Reset (ADPRes)
  99231. + * When set, ADP controller is reset.
  99232. + * This bit is valid only if OTG_Ver = 1'b1.
  99233. + */
  99234. + unsigned adpres:1;
  99235. + /** ADP Enable (ADPEn)
  99236. + * When set, the core performs either ADP probing or sensing
  99237. + * based on EnaPrb or EnaSns.
  99238. + * This bit is valid only if OTG_Ver = 1'b1.
  99239. + */
  99240. + unsigned adpen:1;
  99241. + /** ADP Probe Interrupt (ADP_PRB_INT)
  99242. + * When this bit is set, it means that the VBUS
  99243. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  99244. + * This bit is valid only if OTG_Ver = 1'b1.
  99245. + */
  99246. + unsigned adp_prb_int:1;
  99247. + /**
  99248. + * ADP Sense Interrupt (ADP_SNS_INT)
  99249. + * When this bit is set, it means that the VBUS voltage is greater than
  99250. + * VADP_SNS value or VADP_SNS is reached.
  99251. + * This bit is valid only if OTG_Ver = 1'b1.
  99252. + */
  99253. + unsigned adp_sns_int:1;
  99254. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  99255. + * This bit is relevant only for an ADP probe.
  99256. + * When this bit is set, it means that the ramp time has
  99257. + * completed ie ADPCTL.RTIM has reached its terminal value
  99258. + * of 0x7FF. This is a debug feature that allows software
  99259. + * to read the ramp time after each cycle.
  99260. + * This bit is valid only if OTG_Ver = 1'b1.
  99261. + */
  99262. + unsigned adp_tmout_int:1;
  99263. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  99264. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  99265. + * This bit is valid only if OTG_Ver = 1'b1.
  99266. + */
  99267. + unsigned adp_prb_int_msk:1;
  99268. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  99269. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  99270. + * This bit is valid only if OTG_Ver = 1'b1.
  99271. + */
  99272. + unsigned adp_sns_int_msk:1;
  99273. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  99274. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  99275. + * This bit is valid only if OTG_Ver = 1'b1.
  99276. + */
  99277. + unsigned adp_tmout_int_msk:1;
  99278. + /** Access Request
  99279. + * 2'b00 - Read/Write Valid (updated by the core)
  99280. + * 2'b01 - Read
  99281. + * 2'b00 - Write
  99282. + * 2'b00 - Reserved
  99283. + */
  99284. + unsigned ar:2;
  99285. + /** Reserved */
  99286. + unsigned reserved29_31:3;
  99287. + } b;
  99288. +} adpctl_data_t;
  99289. +
  99290. +////////////////////////////////////////////
  99291. +// Device Registers
  99292. +/**
  99293. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  99294. + *
  99295. + * The following structures define the size and relative field offsets
  99296. + * for the Device Mode Registers.
  99297. + *
  99298. + * <i>These registers are visible only in Device mode and must not be
  99299. + * accessed in Host mode, as the results are unknown.</i>
  99300. + */
  99301. +typedef struct dwc_otg_dev_global_regs {
  99302. + /** Device Configuration Register. <i>Offset 800h</i> */
  99303. + volatile uint32_t dcfg;
  99304. + /** Device Control Register. <i>Offset: 804h</i> */
  99305. + volatile uint32_t dctl;
  99306. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  99307. + volatile uint32_t dsts;
  99308. + /** Reserved. <i>Offset: 80Ch</i> */
  99309. + uint32_t unused;
  99310. + /** Device IN Endpoint Common Interrupt Mask
  99311. + * Register. <i>Offset: 810h</i> */
  99312. + volatile uint32_t diepmsk;
  99313. + /** Device OUT Endpoint Common Interrupt Mask
  99314. + * Register. <i>Offset: 814h</i> */
  99315. + volatile uint32_t doepmsk;
  99316. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  99317. + volatile uint32_t daint;
  99318. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  99319. + * 81Ch</i> */
  99320. + volatile uint32_t daintmsk;
  99321. + /** Device IN Token Queue Read Register-1 (Read Only).
  99322. + * <i>Offset: 820h</i> */
  99323. + volatile uint32_t dtknqr1;
  99324. + /** Device IN Token Queue Read Register-2 (Read Only).
  99325. + * <i>Offset: 824h</i> */
  99326. + volatile uint32_t dtknqr2;
  99327. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  99328. + volatile uint32_t dvbusdis;
  99329. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  99330. + volatile uint32_t dvbuspulse;
  99331. + /** Device IN Token Queue Read Register-3 (Read Only). /
  99332. + * Device Thresholding control register (Read/Write)
  99333. + * <i>Offset: 830h</i> */
  99334. + volatile uint32_t dtknqr3_dthrctl;
  99335. + /** Device IN Token Queue Read Register-4 (Read Only). /
  99336. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  99337. + * <i>Offset: 834h</i> */
  99338. + volatile uint32_t dtknqr4_fifoemptymsk;
  99339. + /** Device Each Endpoint Interrupt Register (Read Only). /
  99340. + * <i>Offset: 838h</i> */
  99341. + volatile uint32_t deachint;
  99342. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  99343. + * <i>Offset: 83Ch</i> */
  99344. + volatile uint32_t deachintmsk;
  99345. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  99346. + * <i>Offset: 840h</i> */
  99347. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  99348. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  99349. + * <i>Offset: 880h</i> */
  99350. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  99351. +} dwc_otg_device_global_regs_t;
  99352. +
  99353. +/**
  99354. + * This union represents the bit fields in the Device Configuration
  99355. + * Register. Read the register into the <i>d32</i> member then
  99356. + * set/clear the bits using the <i>b</i>it elements. Write the
  99357. + * <i>d32</i> member to the dcfg register.
  99358. + */
  99359. +typedef union dcfg_data {
  99360. + /** raw register data */
  99361. + uint32_t d32;
  99362. + /** register bits */
  99363. + struct {
  99364. + /** Device Speed */
  99365. + unsigned devspd:2;
  99366. + /** Non Zero Length Status OUT Handshake */
  99367. + unsigned nzstsouthshk:1;
  99368. +#define DWC_DCFG_SEND_STALL 1
  99369. +
  99370. + unsigned ena32khzs:1;
  99371. + /** Device Addresses */
  99372. + unsigned devaddr:7;
  99373. + /** Periodic Frame Interval */
  99374. + unsigned perfrint:2;
  99375. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  99376. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  99377. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  99378. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  99379. +
  99380. + /** Enable Device OUT NAK for bulk in DDMA mode */
  99381. + unsigned endevoutnak:1;
  99382. +
  99383. + unsigned reserved14_17:4;
  99384. + /** In Endpoint Mis-match count */
  99385. + unsigned epmscnt:5;
  99386. + /** Enable Descriptor DMA in Device mode */
  99387. + unsigned descdma:1;
  99388. + unsigned perschintvl:2;
  99389. + unsigned resvalid:6;
  99390. + } b;
  99391. +} dcfg_data_t;
  99392. +
  99393. +/**
  99394. + * This union represents the bit fields in the Device Control
  99395. + * Register. Read the register into the <i>d32</i> member then
  99396. + * set/clear the bits using the <i>b</i>it elements.
  99397. + */
  99398. +typedef union dctl_data {
  99399. + /** raw register data */
  99400. + uint32_t d32;
  99401. + /** register bits */
  99402. + struct {
  99403. + /** Remote Wakeup */
  99404. + unsigned rmtwkupsig:1;
  99405. + /** Soft Disconnect */
  99406. + unsigned sftdiscon:1;
  99407. + /** Global Non-Periodic IN NAK Status */
  99408. + unsigned gnpinnaksts:1;
  99409. + /** Global OUT NAK Status */
  99410. + unsigned goutnaksts:1;
  99411. + /** Test Control */
  99412. + unsigned tstctl:3;
  99413. + /** Set Global Non-Periodic IN NAK */
  99414. + unsigned sgnpinnak:1;
  99415. + /** Clear Global Non-Periodic IN NAK */
  99416. + unsigned cgnpinnak:1;
  99417. + /** Set Global OUT NAK */
  99418. + unsigned sgoutnak:1;
  99419. + /** Clear Global OUT NAK */
  99420. + unsigned cgoutnak:1;
  99421. + /** Power-On Programming Done */
  99422. + unsigned pwronprgdone:1;
  99423. + /** Reserved */
  99424. + unsigned reserved:1;
  99425. + /** Global Multi Count */
  99426. + unsigned gmc:2;
  99427. + /** Ignore Frame Number for ISOC EPs */
  99428. + unsigned ifrmnum:1;
  99429. + /** NAK on Babble */
  99430. + unsigned nakonbble:1;
  99431. + /** Enable Continue on BNA */
  99432. + unsigned encontonbna:1;
  99433. +
  99434. + unsigned reserved18_31:14;
  99435. + } b;
  99436. +} dctl_data_t;
  99437. +
  99438. +/**
  99439. + * This union represents the bit fields in the Device Status
  99440. + * Register. Read the register into the <i>d32</i> member then
  99441. + * set/clear the bits using the <i>b</i>it elements.
  99442. + */
  99443. +typedef union dsts_data {
  99444. + /** raw register data */
  99445. + uint32_t d32;
  99446. + /** register bits */
  99447. + struct {
  99448. + /** Suspend Status */
  99449. + unsigned suspsts:1;
  99450. + /** Enumerated Speed */
  99451. + unsigned enumspd:2;
  99452. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  99453. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  99454. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  99455. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  99456. + /** Erratic Error */
  99457. + unsigned errticerr:1;
  99458. + unsigned reserved4_7:4;
  99459. + /** Frame or Microframe Number of the received SOF */
  99460. + unsigned soffn:14;
  99461. + unsigned reserved22_31:10;
  99462. + } b;
  99463. +} dsts_data_t;
  99464. +
  99465. +/**
  99466. + * This union represents the bit fields in the Device IN EP Interrupt
  99467. + * Register and the Device IN EP Common Mask Register.
  99468. + *
  99469. + * - Read the register into the <i>d32</i> member then set/clear the
  99470. + * bits using the <i>b</i>it elements.
  99471. + */
  99472. +typedef union diepint_data {
  99473. + /** raw register data */
  99474. + uint32_t d32;
  99475. + /** register bits */
  99476. + struct {
  99477. + /** Transfer complete mask */
  99478. + unsigned xfercompl:1;
  99479. + /** Endpoint disable mask */
  99480. + unsigned epdisabled:1;
  99481. + /** AHB Error mask */
  99482. + unsigned ahberr:1;
  99483. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  99484. + unsigned timeout:1;
  99485. + /** IN Token received with TxF Empty mask */
  99486. + unsigned intktxfemp:1;
  99487. + /** IN Token Received with EP mismatch mask */
  99488. + unsigned intknepmis:1;
  99489. + /** IN Endpoint NAK Effective mask */
  99490. + unsigned inepnakeff:1;
  99491. + /** Reserved */
  99492. + unsigned emptyintr:1;
  99493. +
  99494. + unsigned txfifoundrn:1;
  99495. +
  99496. + /** BNA Interrupt mask */
  99497. + unsigned bna:1;
  99498. +
  99499. + unsigned reserved10_12:3;
  99500. + /** BNA Interrupt mask */
  99501. + unsigned nak:1;
  99502. +
  99503. + unsigned reserved14_31:18;
  99504. + } b;
  99505. +} diepint_data_t;
  99506. +
  99507. +/**
  99508. + * This union represents the bit fields in the Device IN EP
  99509. + * Common/Dedicated Interrupt Mask Register.
  99510. + */
  99511. +typedef union diepint_data diepmsk_data_t;
  99512. +
  99513. +/**
  99514. + * This union represents the bit fields in the Device OUT EP Interrupt
  99515. + * Registerand Device OUT EP Common Interrupt Mask Register.
  99516. + *
  99517. + * - Read the register into the <i>d32</i> member then set/clear the
  99518. + * bits using the <i>b</i>it elements.
  99519. + */
  99520. +typedef union doepint_data {
  99521. + /** raw register data */
  99522. + uint32_t d32;
  99523. + /** register bits */
  99524. + struct {
  99525. + /** Transfer complete */
  99526. + unsigned xfercompl:1;
  99527. + /** Endpoint disable */
  99528. + unsigned epdisabled:1;
  99529. + /** AHB Error */
  99530. + unsigned ahberr:1;
  99531. + /** Setup Phase Done (contorl EPs) */
  99532. + unsigned setup:1;
  99533. + /** OUT Token Received when Endpoint Disabled */
  99534. + unsigned outtknepdis:1;
  99535. +
  99536. + unsigned stsphsercvd:1;
  99537. + /** Back-to-Back SETUP Packets Received */
  99538. + unsigned back2backsetup:1;
  99539. +
  99540. + unsigned reserved7:1;
  99541. + /** OUT packet Error */
  99542. + unsigned outpkterr:1;
  99543. + /** BNA Interrupt */
  99544. + unsigned bna:1;
  99545. +
  99546. + unsigned reserved10:1;
  99547. + /** Packet Drop Status */
  99548. + unsigned pktdrpsts:1;
  99549. + /** Babble Interrupt */
  99550. + unsigned babble:1;
  99551. + /** NAK Interrupt */
  99552. + unsigned nak:1;
  99553. + /** NYET Interrupt */
  99554. + unsigned nyet:1;
  99555. + /** Bit indicating setup packet received */
  99556. + unsigned sr:1;
  99557. +
  99558. + unsigned reserved16_31:16;
  99559. + } b;
  99560. +} doepint_data_t;
  99561. +
  99562. +/**
  99563. + * This union represents the bit fields in the Device OUT EP
  99564. + * Common/Dedicated Interrupt Mask Register.
  99565. + */
  99566. +typedef union doepint_data doepmsk_data_t;
  99567. +
  99568. +/**
  99569. + * This union represents the bit fields in the Device All EP Interrupt
  99570. + * and Mask Registers.
  99571. + * - Read the register into the <i>d32</i> member then set/clear the
  99572. + * bits using the <i>b</i>it elements.
  99573. + */
  99574. +typedef union daint_data {
  99575. + /** raw register data */
  99576. + uint32_t d32;
  99577. + /** register bits */
  99578. + struct {
  99579. + /** IN Endpoint bits */
  99580. + unsigned in:16;
  99581. + /** OUT Endpoint bits */
  99582. + unsigned out:16;
  99583. + } ep;
  99584. + struct {
  99585. + /** IN Endpoint bits */
  99586. + unsigned inep0:1;
  99587. + unsigned inep1:1;
  99588. + unsigned inep2:1;
  99589. + unsigned inep3:1;
  99590. + unsigned inep4:1;
  99591. + unsigned inep5:1;
  99592. + unsigned inep6:1;
  99593. + unsigned inep7:1;
  99594. + unsigned inep8:1;
  99595. + unsigned inep9:1;
  99596. + unsigned inep10:1;
  99597. + unsigned inep11:1;
  99598. + unsigned inep12:1;
  99599. + unsigned inep13:1;
  99600. + unsigned inep14:1;
  99601. + unsigned inep15:1;
  99602. + /** OUT Endpoint bits */
  99603. + unsigned outep0:1;
  99604. + unsigned outep1:1;
  99605. + unsigned outep2:1;
  99606. + unsigned outep3:1;
  99607. + unsigned outep4:1;
  99608. + unsigned outep5:1;
  99609. + unsigned outep6:1;
  99610. + unsigned outep7:1;
  99611. + unsigned outep8:1;
  99612. + unsigned outep9:1;
  99613. + unsigned outep10:1;
  99614. + unsigned outep11:1;
  99615. + unsigned outep12:1;
  99616. + unsigned outep13:1;
  99617. + unsigned outep14:1;
  99618. + unsigned outep15:1;
  99619. + } b;
  99620. +} daint_data_t;
  99621. +
  99622. +/**
  99623. + * This union represents the bit fields in the Device IN Token Queue
  99624. + * Read Registers.
  99625. + * - Read the register into the <i>d32</i> member.
  99626. + * - READ-ONLY Register
  99627. + */
  99628. +typedef union dtknq1_data {
  99629. + /** raw register data */
  99630. + uint32_t d32;
  99631. + /** register bits */
  99632. + struct {
  99633. + /** In Token Queue Write Pointer */
  99634. + unsigned intknwptr:5;
  99635. + /** Reserved */
  99636. + unsigned reserved05_06:2;
  99637. + /** write pointer has wrapped. */
  99638. + unsigned wrap_bit:1;
  99639. + /** EP Numbers of IN Tokens 0 ... 4 */
  99640. + unsigned epnums0_5:24;
  99641. + } b;
  99642. +} dtknq1_data_t;
  99643. +
  99644. +/**
  99645. + * This union represents Threshold control Register
  99646. + * - Read and write the register into the <i>d32</i> member.
  99647. + * - READ-WRITABLE Register
  99648. + */
  99649. +typedef union dthrctl_data {
  99650. + /** raw register data */
  99651. + uint32_t d32;
  99652. + /** register bits */
  99653. + struct {
  99654. + /** non ISO Tx Thr. Enable */
  99655. + unsigned non_iso_thr_en:1;
  99656. + /** ISO Tx Thr. Enable */
  99657. + unsigned iso_thr_en:1;
  99658. + /** Tx Thr. Length */
  99659. + unsigned tx_thr_len:9;
  99660. + /** AHB Threshold ratio */
  99661. + unsigned ahb_thr_ratio:2;
  99662. + /** Reserved */
  99663. + unsigned reserved13_15:3;
  99664. + /** Rx Thr. Enable */
  99665. + unsigned rx_thr_en:1;
  99666. + /** Rx Thr. Length */
  99667. + unsigned rx_thr_len:9;
  99668. + unsigned reserved26:1;
  99669. + /** Arbiter Parking Enable*/
  99670. + unsigned arbprken:1;
  99671. + /** Reserved */
  99672. + unsigned reserved28_31:4;
  99673. + } b;
  99674. +} dthrctl_data_t;
  99675. +
  99676. +/**
  99677. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  99678. + * 900h-AFCh</i>
  99679. + *
  99680. + * There will be one set of endpoint registers per logical endpoint
  99681. + * implemented.
  99682. + *
  99683. + * <i>These registers are visible only in Device mode and must not be
  99684. + * accessed in Host mode, as the results are unknown.</i>
  99685. + */
  99686. +typedef struct dwc_otg_dev_in_ep_regs {
  99687. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  99688. + * (ep_num * 20h) + 00h</i> */
  99689. + volatile uint32_t diepctl;
  99690. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  99691. + uint32_t reserved04;
  99692. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  99693. + * (ep_num * 20h) + 08h</i> */
  99694. + volatile uint32_t diepint;
  99695. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  99696. + uint32_t reserved0C;
  99697. + /** Device IN Endpoint Transfer Size
  99698. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  99699. + volatile uint32_t dieptsiz;
  99700. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  99701. + * (ep_num * 20h) + 14h</i> */
  99702. + volatile uint32_t diepdma;
  99703. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  99704. + * (ep_num * 20h) + 18h</i> */
  99705. + volatile uint32_t dtxfsts;
  99706. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  99707. + * (ep_num * 20h) + 1Ch</i> */
  99708. + volatile uint32_t diepdmab;
  99709. +} dwc_otg_dev_in_ep_regs_t;
  99710. +
  99711. +/**
  99712. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  99713. + * B00h-CFCh</i>
  99714. + *
  99715. + * There will be one set of endpoint registers per logical endpoint
  99716. + * implemented.
  99717. + *
  99718. + * <i>These registers are visible only in Device mode and must not be
  99719. + * accessed in Host mode, as the results are unknown.</i>
  99720. + */
  99721. +typedef struct dwc_otg_dev_out_ep_regs {
  99722. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  99723. + * (ep_num * 20h) + 00h</i> */
  99724. + volatile uint32_t doepctl;
  99725. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  99726. + uint32_t reserved04;
  99727. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  99728. + * (ep_num * 20h) + 08h</i> */
  99729. + volatile uint32_t doepint;
  99730. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  99731. + uint32_t reserved0C;
  99732. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  99733. + * B00h + (ep_num * 20h) + 10h</i> */
  99734. + volatile uint32_t doeptsiz;
  99735. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  99736. + * + (ep_num * 20h) + 14h</i> */
  99737. + volatile uint32_t doepdma;
  99738. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  99739. + uint32_t unused;
  99740. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  99741. + * + (ep_num * 20h) + 1Ch</i> */
  99742. + uint32_t doepdmab;
  99743. +} dwc_otg_dev_out_ep_regs_t;
  99744. +
  99745. +/**
  99746. + * This union represents the bit fields in the Device EP Control
  99747. + * Register. Read the register into the <i>d32</i> member then
  99748. + * set/clear the bits using the <i>b</i>it elements.
  99749. + */
  99750. +typedef union depctl_data {
  99751. + /** raw register data */
  99752. + uint32_t d32;
  99753. + /** register bits */
  99754. + struct {
  99755. + /** Maximum Packet Size
  99756. + * IN/OUT EPn
  99757. + * IN/OUT EP0 - 2 bits
  99758. + * 2'b00: 64 Bytes
  99759. + * 2'b01: 32
  99760. + * 2'b10: 16
  99761. + * 2'b11: 8 */
  99762. + unsigned mps:11;
  99763. +#define DWC_DEP0CTL_MPS_64 0
  99764. +#define DWC_DEP0CTL_MPS_32 1
  99765. +#define DWC_DEP0CTL_MPS_16 2
  99766. +#define DWC_DEP0CTL_MPS_8 3
  99767. +
  99768. + /** Next Endpoint
  99769. + * IN EPn/IN EP0
  99770. + * OUT EPn/OUT EP0 - reserved */
  99771. + unsigned nextep:4;
  99772. +
  99773. + /** USB Active Endpoint */
  99774. + unsigned usbactep:1;
  99775. +
  99776. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  99777. + * This field contains the PID of the packet going to
  99778. + * be received or transmitted on this endpoint. The
  99779. + * application should program the PID of the first
  99780. + * packet going to be received or transmitted on this
  99781. + * endpoint , after the endpoint is
  99782. + * activated. Application use the SetD1PID and
  99783. + * SetD0PID fields of this register to program either
  99784. + * D0 or D1 PID.
  99785. + *
  99786. + * The encoding for this field is
  99787. + * - 0: D0
  99788. + * - 1: D1
  99789. + */
  99790. + unsigned dpid:1;
  99791. +
  99792. + /** NAK Status */
  99793. + unsigned naksts:1;
  99794. +
  99795. + /** Endpoint Type
  99796. + * 2'b00: Control
  99797. + * 2'b01: Isochronous
  99798. + * 2'b10: Bulk
  99799. + * 2'b11: Interrupt */
  99800. + unsigned eptype:2;
  99801. +
  99802. + /** Snoop Mode
  99803. + * OUT EPn/OUT EP0
  99804. + * IN EPn/IN EP0 - reserved */
  99805. + unsigned snp:1;
  99806. +
  99807. + /** Stall Handshake */
  99808. + unsigned stall:1;
  99809. +
  99810. + /** Tx Fifo Number
  99811. + * IN EPn/IN EP0
  99812. + * OUT EPn/OUT EP0 - reserved */
  99813. + unsigned txfnum:4;
  99814. +
  99815. + /** Clear NAK */
  99816. + unsigned cnak:1;
  99817. + /** Set NAK */
  99818. + unsigned snak:1;
  99819. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  99820. + * Writing to this field sets the Endpoint DPID (DPID)
  99821. + * field in this register to DATA0. Set Even
  99822. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  99823. + * Writing to this field sets the Even/Odd
  99824. + * (micro)frame (EO_FrNum) field to even (micro)
  99825. + * frame.
  99826. + */
  99827. + unsigned setd0pid:1;
  99828. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  99829. + * Writing to this field sets the Endpoint DPID (DPID)
  99830. + * field in this register to DATA1 Set Odd
  99831. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  99832. + * Writing to this field sets the Even/Odd
  99833. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  99834. + */
  99835. + unsigned setd1pid:1;
  99836. +
  99837. + /** Endpoint Disable */
  99838. + unsigned epdis:1;
  99839. + /** Endpoint Enable */
  99840. + unsigned epena:1;
  99841. + } b;
  99842. +} depctl_data_t;
  99843. +
  99844. +/**
  99845. + * This union represents the bit fields in the Device EP Transfer
  99846. + * Size Register. Read the register into the <i>d32</i> member then
  99847. + * set/clear the bits using the <i>b</i>it elements.
  99848. + */
  99849. +typedef union deptsiz_data {
  99850. + /** raw register data */
  99851. + uint32_t d32;
  99852. + /** register bits */
  99853. + struct {
  99854. + /** Transfer size */
  99855. + unsigned xfersize:19;
  99856. +/** Max packet count for EP (pow(2,10)-1) */
  99857. +#define MAX_PKT_CNT 1023
  99858. + /** Packet Count */
  99859. + unsigned pktcnt:10;
  99860. + /** Multi Count - Periodic IN endpoints */
  99861. + unsigned mc:2;
  99862. + unsigned reserved:1;
  99863. + } b;
  99864. +} deptsiz_data_t;
  99865. +
  99866. +/**
  99867. + * This union represents the bit fields in the Device EP 0 Transfer
  99868. + * Size Register. Read the register into the <i>d32</i> member then
  99869. + * set/clear the bits using the <i>b</i>it elements.
  99870. + */
  99871. +typedef union deptsiz0_data {
  99872. + /** raw register data */
  99873. + uint32_t d32;
  99874. + /** register bits */
  99875. + struct {
  99876. + /** Transfer size */
  99877. + unsigned xfersize:7;
  99878. + /** Reserved */
  99879. + unsigned reserved7_18:12;
  99880. + /** Packet Count */
  99881. + unsigned pktcnt:2;
  99882. + /** Reserved */
  99883. + unsigned reserved21_28:8;
  99884. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  99885. + unsigned supcnt:2;
  99886. + unsigned reserved31;
  99887. + } b;
  99888. +} deptsiz0_data_t;
  99889. +
  99890. +/////////////////////////////////////////////////
  99891. +// DMA Descriptor Specific Structures
  99892. +//
  99893. +
  99894. +/** Buffer status definitions */
  99895. +
  99896. +#define BS_HOST_READY 0x0
  99897. +#define BS_DMA_BUSY 0x1
  99898. +#define BS_DMA_DONE 0x2
  99899. +#define BS_HOST_BUSY 0x3
  99900. +
  99901. +/** Receive/Transmit status definitions */
  99902. +
  99903. +#define RTS_SUCCESS 0x0
  99904. +#define RTS_BUFFLUSH 0x1
  99905. +#define RTS_RESERVED 0x2
  99906. +#define RTS_BUFERR 0x3
  99907. +
  99908. +/**
  99909. + * This union represents the bit fields in the DMA Descriptor
  99910. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  99911. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  99912. + * <i>b_iso_in</i> elements.
  99913. + */
  99914. +typedef union dev_dma_desc_sts {
  99915. + /** raw register data */
  99916. + uint32_t d32;
  99917. + /** quadlet bits */
  99918. + struct {
  99919. + /** Received number of bytes */
  99920. + unsigned bytes:16;
  99921. + /** NAK bit - only for OUT EPs */
  99922. + unsigned nak:1;
  99923. + unsigned reserved17_22:6;
  99924. + /** Multiple Transfer - only for OUT EPs */
  99925. + unsigned mtrf:1;
  99926. + /** Setup Packet received - only for OUT EPs */
  99927. + unsigned sr:1;
  99928. + /** Interrupt On Complete */
  99929. + unsigned ioc:1;
  99930. + /** Short Packet */
  99931. + unsigned sp:1;
  99932. + /** Last */
  99933. + unsigned l:1;
  99934. + /** Receive Status */
  99935. + unsigned sts:2;
  99936. + /** Buffer Status */
  99937. + unsigned bs:2;
  99938. + } b;
  99939. +
  99940. +//#ifdef DWC_EN_ISOC
  99941. + /** iso out quadlet bits */
  99942. + struct {
  99943. + /** Received number of bytes */
  99944. + unsigned rxbytes:11;
  99945. +
  99946. + unsigned reserved11:1;
  99947. + /** Frame Number */
  99948. + unsigned framenum:11;
  99949. + /** Received ISO Data PID */
  99950. + unsigned pid:2;
  99951. + /** Interrupt On Complete */
  99952. + unsigned ioc:1;
  99953. + /** Short Packet */
  99954. + unsigned sp:1;
  99955. + /** Last */
  99956. + unsigned l:1;
  99957. + /** Receive Status */
  99958. + unsigned rxsts:2;
  99959. + /** Buffer Status */
  99960. + unsigned bs:2;
  99961. + } b_iso_out;
  99962. +
  99963. + /** iso in quadlet bits */
  99964. + struct {
  99965. + /** Transmited number of bytes */
  99966. + unsigned txbytes:12;
  99967. + /** Frame Number */
  99968. + unsigned framenum:11;
  99969. + /** Transmited ISO Data PID */
  99970. + unsigned pid:2;
  99971. + /** Interrupt On Complete */
  99972. + unsigned ioc:1;
  99973. + /** Short Packet */
  99974. + unsigned sp:1;
  99975. + /** Last */
  99976. + unsigned l:1;
  99977. + /** Transmit Status */
  99978. + unsigned txsts:2;
  99979. + /** Buffer Status */
  99980. + unsigned bs:2;
  99981. + } b_iso_in;
  99982. +//#endif /* DWC_EN_ISOC */
  99983. +} dev_dma_desc_sts_t;
  99984. +
  99985. +/**
  99986. + * DMA Descriptor structure
  99987. + *
  99988. + * DMA Descriptor structure contains two quadlets:
  99989. + * Status quadlet and Data buffer pointer.
  99990. + */
  99991. +typedef struct dwc_otg_dev_dma_desc {
  99992. + /** DMA Descriptor status quadlet */
  99993. + dev_dma_desc_sts_t status;
  99994. + /** DMA Descriptor data buffer pointer */
  99995. + uint32_t buf;
  99996. +} dwc_otg_dev_dma_desc_t;
  99997. +
  99998. +/**
  99999. + * The dwc_otg_dev_if structure contains information needed to manage
  100000. + * the DWC_otg controller acting in device mode. It represents the
  100001. + * programming view of the device-specific aspects of the controller.
  100002. + */
  100003. +typedef struct dwc_otg_dev_if {
  100004. + /** Pointer to device Global registers.
  100005. + * Device Global Registers starting at offset 800h
  100006. + */
  100007. + dwc_otg_device_global_regs_t *dev_global_regs;
  100008. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  100009. +
  100010. + /**
  100011. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  100012. + */
  100013. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  100014. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  100015. +#define DWC_EP_REG_OFFSET 0x20
  100016. +
  100017. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  100018. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  100019. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  100020. +
  100021. + /* Device configuration information */
  100022. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  100023. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  100024. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  100025. +
  100026. + /** Size of periodic FIFOs (Bytes) */
  100027. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  100028. +
  100029. + /** Size of Tx FIFOs (Bytes) */
  100030. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  100031. +
  100032. + /** Thresholding enable flags and length varaiables **/
  100033. + uint16_t rx_thr_en;
  100034. + uint16_t iso_tx_thr_en;
  100035. + uint16_t non_iso_tx_thr_en;
  100036. +
  100037. + uint16_t rx_thr_length;
  100038. + uint16_t tx_thr_length;
  100039. +
  100040. + /**
  100041. + * Pointers to the DMA Descriptors for EP0 Control
  100042. + * transfers (virtual and physical)
  100043. + */
  100044. +
  100045. + /** 2 descriptors for SETUP packets */
  100046. + dwc_dma_t dma_setup_desc_addr[2];
  100047. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  100048. +
  100049. + /** Pointer to Descriptor with latest SETUP packet */
  100050. + dwc_otg_dev_dma_desc_t *psetup;
  100051. +
  100052. + /** Index of current SETUP handler descriptor */
  100053. + uint32_t setup_desc_index;
  100054. +
  100055. + /** Descriptor for Data In or Status In phases */
  100056. + dwc_dma_t dma_in_desc_addr;
  100057. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  100058. +
  100059. + /** Descriptor for Data Out or Status Out phases */
  100060. + dwc_dma_t dma_out_desc_addr;
  100061. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  100062. +
  100063. + /** Setup Packet Detected - if set clear NAK when queueing */
  100064. + uint32_t spd;
  100065. + /** Isoc ep pointer on which incomplete happens */
  100066. + void *isoc_ep;
  100067. +
  100068. +} dwc_otg_dev_if_t;
  100069. +
  100070. +/////////////////////////////////////////////////
  100071. +// Host Mode Register Structures
  100072. +//
  100073. +/**
  100074. + * The Host Global Registers structure defines the size and relative
  100075. + * field offsets for the Host Mode Global Registers. Host Global
  100076. + * Registers offsets 400h-7FFh.
  100077. +*/
  100078. +typedef struct dwc_otg_host_global_regs {
  100079. + /** Host Configuration Register. <i>Offset: 400h</i> */
  100080. + volatile uint32_t hcfg;
  100081. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  100082. + volatile uint32_t hfir;
  100083. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  100084. + volatile uint32_t hfnum;
  100085. + /** Reserved. <i>Offset: 40Ch</i> */
  100086. + uint32_t reserved40C;
  100087. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  100088. + volatile uint32_t hptxsts;
  100089. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  100090. + volatile uint32_t haint;
  100091. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  100092. + volatile uint32_t haintmsk;
  100093. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  100094. + volatile uint32_t hflbaddr;
  100095. +} dwc_otg_host_global_regs_t;
  100096. +
  100097. +/**
  100098. + * This union represents the bit fields in the Host Configuration Register.
  100099. + * Read the register into the <i>d32</i> member then set/clear the bits using
  100100. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  100101. + */
  100102. +typedef union hcfg_data {
  100103. + /** raw register data */
  100104. + uint32_t d32;
  100105. +
  100106. + /** register bits */
  100107. + struct {
  100108. + /** FS/LS Phy Clock Select */
  100109. + unsigned fslspclksel:2;
  100110. +#define DWC_HCFG_30_60_MHZ 0
  100111. +#define DWC_HCFG_48_MHZ 1
  100112. +#define DWC_HCFG_6_MHZ 2
  100113. +
  100114. + /** FS/LS Only Support */
  100115. + unsigned fslssupp:1;
  100116. + unsigned reserved3_6:4;
  100117. + /** Enable 32-KHz Suspend Mode */
  100118. + unsigned ena32khzs:1;
  100119. + /** Resume Validation Periiod */
  100120. + unsigned resvalid:8;
  100121. + unsigned reserved16_22:7;
  100122. + /** Enable Scatter/gather DMA in Host mode */
  100123. + unsigned descdma:1;
  100124. + /** Frame List Entries */
  100125. + unsigned frlisten:2;
  100126. + /** Enable Periodic Scheduling */
  100127. + unsigned perschedena:1;
  100128. + unsigned reserved27_30:4;
  100129. + unsigned modechtimen:1;
  100130. + } b;
  100131. +} hcfg_data_t;
  100132. +
  100133. +/**
  100134. + * This union represents the bit fields in the Host Frame Remaing/Number
  100135. + * Register.
  100136. + */
  100137. +typedef union hfir_data {
  100138. + /** raw register data */
  100139. + uint32_t d32;
  100140. +
  100141. + /** register bits */
  100142. + struct {
  100143. + unsigned frint:16;
  100144. + unsigned hfirrldctrl:1;
  100145. + unsigned reserved:15;
  100146. + } b;
  100147. +} hfir_data_t;
  100148. +
  100149. +/**
  100150. + * This union represents the bit fields in the Host Frame Remaing/Number
  100151. + * Register.
  100152. + */
  100153. +typedef union hfnum_data {
  100154. + /** raw register data */
  100155. + uint32_t d32;
  100156. +
  100157. + /** register bits */
  100158. + struct {
  100159. + unsigned frnum:16;
  100160. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  100161. + unsigned frrem:16;
  100162. + } b;
  100163. +} hfnum_data_t;
  100164. +
  100165. +typedef union hptxsts_data {
  100166. + /** raw register data */
  100167. + uint32_t d32;
  100168. +
  100169. + /** register bits */
  100170. + struct {
  100171. + unsigned ptxfspcavail:16;
  100172. + unsigned ptxqspcavail:8;
  100173. + /** Top of the Periodic Transmit Request Queue
  100174. + * - bit 24 - Terminate (last entry for the selected channel)
  100175. + * - bits 26:25 - Token Type
  100176. + * - 2'b00 - Zero length
  100177. + * - 2'b01 - Ping
  100178. + * - 2'b10 - Disable
  100179. + * - bits 30:27 - Channel Number
  100180. + * - bit 31 - Odd/even microframe
  100181. + */
  100182. + unsigned ptxqtop_terminate:1;
  100183. + unsigned ptxqtop_token:2;
  100184. + unsigned ptxqtop_chnum:4;
  100185. + unsigned ptxqtop_odd:1;
  100186. + } b;
  100187. +} hptxsts_data_t;
  100188. +
  100189. +/**
  100190. + * This union represents the bit fields in the Host Port Control and Status
  100191. + * Register. Read the register into the <i>d32</i> member then set/clear the
  100192. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  100193. + * hprt0 register.
  100194. + */
  100195. +typedef union hprt0_data {
  100196. + /** raw register data */
  100197. + uint32_t d32;
  100198. + /** register bits */
  100199. + struct {
  100200. + unsigned prtconnsts:1;
  100201. + unsigned prtconndet:1;
  100202. + unsigned prtena:1;
  100203. + unsigned prtenchng:1;
  100204. + unsigned prtovrcurract:1;
  100205. + unsigned prtovrcurrchng:1;
  100206. + unsigned prtres:1;
  100207. + unsigned prtsusp:1;
  100208. + unsigned prtrst:1;
  100209. + unsigned reserved9:1;
  100210. + unsigned prtlnsts:2;
  100211. + unsigned prtpwr:1;
  100212. + unsigned prttstctl:4;
  100213. + unsigned prtspd:2;
  100214. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  100215. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  100216. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  100217. + unsigned reserved19_31:13;
  100218. + } b;
  100219. +} hprt0_data_t;
  100220. +
  100221. +/**
  100222. + * This union represents the bit fields in the Host All Interrupt
  100223. + * Register.
  100224. + */
  100225. +typedef union haint_data {
  100226. + /** raw register data */
  100227. + uint32_t d32;
  100228. + /** register bits */
  100229. + struct {
  100230. + unsigned ch0:1;
  100231. + unsigned ch1:1;
  100232. + unsigned ch2:1;
  100233. + unsigned ch3:1;
  100234. + unsigned ch4:1;
  100235. + unsigned ch5:1;
  100236. + unsigned ch6:1;
  100237. + unsigned ch7:1;
  100238. + unsigned ch8:1;
  100239. + unsigned ch9:1;
  100240. + unsigned ch10:1;
  100241. + unsigned ch11:1;
  100242. + unsigned ch12:1;
  100243. + unsigned ch13:1;
  100244. + unsigned ch14:1;
  100245. + unsigned ch15:1;
  100246. + unsigned reserved:16;
  100247. + } b;
  100248. +
  100249. + struct {
  100250. + unsigned chint:16;
  100251. + unsigned reserved:16;
  100252. + } b2;
  100253. +} haint_data_t;
  100254. +
  100255. +/**
  100256. + * This union represents the bit fields in the Host All Interrupt
  100257. + * Register.
  100258. + */
  100259. +typedef union haintmsk_data {
  100260. + /** raw register data */
  100261. + uint32_t d32;
  100262. + /** register bits */
  100263. + struct {
  100264. + unsigned ch0:1;
  100265. + unsigned ch1:1;
  100266. + unsigned ch2:1;
  100267. + unsigned ch3:1;
  100268. + unsigned ch4:1;
  100269. + unsigned ch5:1;
  100270. + unsigned ch6:1;
  100271. + unsigned ch7:1;
  100272. + unsigned ch8:1;
  100273. + unsigned ch9:1;
  100274. + unsigned ch10:1;
  100275. + unsigned ch11:1;
  100276. + unsigned ch12:1;
  100277. + unsigned ch13:1;
  100278. + unsigned ch14:1;
  100279. + unsigned ch15:1;
  100280. + unsigned reserved:16;
  100281. + } b;
  100282. +
  100283. + struct {
  100284. + unsigned chint:16;
  100285. + unsigned reserved:16;
  100286. + } b2;
  100287. +} haintmsk_data_t;
  100288. +
  100289. +/**
  100290. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  100291. + */
  100292. +typedef struct dwc_otg_hc_regs {
  100293. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  100294. + volatile uint32_t hcchar;
  100295. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  100296. + volatile uint32_t hcsplt;
  100297. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  100298. + volatile uint32_t hcint;
  100299. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  100300. + volatile uint32_t hcintmsk;
  100301. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  100302. + volatile uint32_t hctsiz;
  100303. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  100304. + volatile uint32_t hcdma;
  100305. + volatile uint32_t reserved;
  100306. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  100307. + volatile uint32_t hcdmab;
  100308. +} dwc_otg_hc_regs_t;
  100309. +
  100310. +/**
  100311. + * This union represents the bit fields in the Host Channel Characteristics
  100312. + * Register. Read the register into the <i>d32</i> member then set/clear the
  100313. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  100314. + * hcchar register.
  100315. + */
  100316. +typedef union hcchar_data {
  100317. + /** raw register data */
  100318. + uint32_t d32;
  100319. +
  100320. + /** register bits */
  100321. + struct {
  100322. + /** Maximum packet size in bytes */
  100323. + unsigned mps:11;
  100324. +
  100325. + /** Endpoint number */
  100326. + unsigned epnum:4;
  100327. +
  100328. + /** 0: OUT, 1: IN */
  100329. + unsigned epdir:1;
  100330. +
  100331. + unsigned reserved:1;
  100332. +
  100333. + /** 0: Full/high speed device, 1: Low speed device */
  100334. + unsigned lspddev:1;
  100335. +
  100336. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  100337. + unsigned eptype:2;
  100338. +
  100339. + /** Packets per frame for periodic transfers. 0 is reserved. */
  100340. + unsigned multicnt:2;
  100341. +
  100342. + /** Device address */
  100343. + unsigned devaddr:7;
  100344. +
  100345. + /**
  100346. + * Frame to transmit periodic transaction.
  100347. + * 0: even, 1: odd
  100348. + */
  100349. + unsigned oddfrm:1;
  100350. +
  100351. + /** Channel disable */
  100352. + unsigned chdis:1;
  100353. +
  100354. + /** Channel enable */
  100355. + unsigned chen:1;
  100356. + } b;
  100357. +} hcchar_data_t;
  100358. +
  100359. +typedef union hcsplt_data {
  100360. + /** raw register data */
  100361. + uint32_t d32;
  100362. +
  100363. + /** register bits */
  100364. + struct {
  100365. + /** Port Address */
  100366. + unsigned prtaddr:7;
  100367. +
  100368. + /** Hub Address */
  100369. + unsigned hubaddr:7;
  100370. +
  100371. + /** Transaction Position */
  100372. + unsigned xactpos:2;
  100373. +#define DWC_HCSPLIT_XACTPOS_MID 0
  100374. +#define DWC_HCSPLIT_XACTPOS_END 1
  100375. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  100376. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  100377. +
  100378. + /** Do Complete Split */
  100379. + unsigned compsplt:1;
  100380. +
  100381. + /** Reserved */
  100382. + unsigned reserved:14;
  100383. +
  100384. + /** Split Enble */
  100385. + unsigned spltena:1;
  100386. + } b;
  100387. +} hcsplt_data_t;
  100388. +
  100389. +/**
  100390. + * This union represents the bit fields in the Host All Interrupt
  100391. + * Register.
  100392. + */
  100393. +typedef union hcint_data {
  100394. + /** raw register data */
  100395. + uint32_t d32;
  100396. + /** register bits */
  100397. + struct {
  100398. + /** Transfer Complete */
  100399. + unsigned xfercomp:1;
  100400. + /** Channel Halted */
  100401. + unsigned chhltd:1;
  100402. + /** AHB Error */
  100403. + unsigned ahberr:1;
  100404. + /** STALL Response Received */
  100405. + unsigned stall:1;
  100406. + /** NAK Response Received */
  100407. + unsigned nak:1;
  100408. + /** ACK Response Received */
  100409. + unsigned ack:1;
  100410. + /** NYET Response Received */
  100411. + unsigned nyet:1;
  100412. + /** Transaction Err */
  100413. + unsigned xacterr:1;
  100414. + /** Babble Error */
  100415. + unsigned bblerr:1;
  100416. + /** Frame Overrun */
  100417. + unsigned frmovrun:1;
  100418. + /** Data Toggle Error */
  100419. + unsigned datatglerr:1;
  100420. + /** Buffer Not Available (only for DDMA mode) */
  100421. + unsigned bna:1;
  100422. + /** Exessive transaction error (only for DDMA mode) */
  100423. + unsigned xcs_xact:1;
  100424. + /** Frame List Rollover interrupt */
  100425. + unsigned frm_list_roll:1;
  100426. + /** Reserved */
  100427. + unsigned reserved14_31:18;
  100428. + } b;
  100429. +} hcint_data_t;
  100430. +
  100431. +/**
  100432. + * This union represents the bit fields in the Host Channel Interrupt Mask
  100433. + * Register. Read the register into the <i>d32</i> member then set/clear the
  100434. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  100435. + * hcintmsk register.
  100436. + */
  100437. +typedef union hcintmsk_data {
  100438. + /** raw register data */
  100439. + uint32_t d32;
  100440. +
  100441. + /** register bits */
  100442. + struct {
  100443. + unsigned xfercompl:1;
  100444. + unsigned chhltd:1;
  100445. + unsigned ahberr:1;
  100446. + unsigned stall:1;
  100447. + unsigned nak:1;
  100448. + unsigned ack:1;
  100449. + unsigned nyet:1;
  100450. + unsigned xacterr:1;
  100451. + unsigned bblerr:1;
  100452. + unsigned frmovrun:1;
  100453. + unsigned datatglerr:1;
  100454. + unsigned bna:1;
  100455. + unsigned xcs_xact:1;
  100456. + unsigned frm_list_roll:1;
  100457. + unsigned reserved14_31:18;
  100458. + } b;
  100459. +} hcintmsk_data_t;
  100460. +
  100461. +/**
  100462. + * This union represents the bit fields in the Host Channel Transfer Size
  100463. + * Register. Read the register into the <i>d32</i> member then set/clear the
  100464. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  100465. + * hcchar register.
  100466. + */
  100467. +
  100468. +typedef union hctsiz_data {
  100469. + /** raw register data */
  100470. + uint32_t d32;
  100471. +
  100472. + /** register bits */
  100473. + struct {
  100474. + /** Total transfer size in bytes */
  100475. + unsigned xfersize:19;
  100476. +
  100477. + /** Data packets to transfer */
  100478. + unsigned pktcnt:10;
  100479. +
  100480. + /**
  100481. + * Packet ID for next data packet
  100482. + * 0: DATA0
  100483. + * 1: DATA2
  100484. + * 2: DATA1
  100485. + * 3: MDATA (non-Control), SETUP (Control)
  100486. + */
  100487. + unsigned pid:2;
  100488. +#define DWC_HCTSIZ_DATA0 0
  100489. +#define DWC_HCTSIZ_DATA1 2
  100490. +#define DWC_HCTSIZ_DATA2 1
  100491. +#define DWC_HCTSIZ_MDATA 3
  100492. +#define DWC_HCTSIZ_SETUP 3
  100493. +
  100494. + /** Do PING protocol when 1 */
  100495. + unsigned dopng:1;
  100496. + } b;
  100497. +
  100498. + /** register bits */
  100499. + struct {
  100500. + /** Scheduling information */
  100501. + unsigned schinfo:8;
  100502. +
  100503. + /** Number of transfer descriptors.
  100504. + * Max value:
  100505. + * 64 in general,
  100506. + * 256 only for HS isochronous endpoint.
  100507. + */
  100508. + unsigned ntd:8;
  100509. +
  100510. + /** Data packets to transfer */
  100511. + unsigned reserved16_28:13;
  100512. +
  100513. + /**
  100514. + * Packet ID for next data packet
  100515. + * 0: DATA0
  100516. + * 1: DATA2
  100517. + * 2: DATA1
  100518. + * 3: MDATA (non-Control)
  100519. + */
  100520. + unsigned pid:2;
  100521. +
  100522. + /** Do PING protocol when 1 */
  100523. + unsigned dopng:1;
  100524. + } b_ddma;
  100525. +} hctsiz_data_t;
  100526. +
  100527. +/**
  100528. + * This union represents the bit fields in the Host DMA Address
  100529. + * Register used in Descriptor DMA mode.
  100530. + */
  100531. +typedef union hcdma_data {
  100532. + /** raw register data */
  100533. + uint32_t d32;
  100534. + /** register bits */
  100535. + struct {
  100536. + unsigned reserved0_2:3;
  100537. + /** Current Transfer Descriptor. Not used for ISOC */
  100538. + unsigned ctd:8;
  100539. + /** Start Address of Descriptor List */
  100540. + unsigned dma_addr:21;
  100541. + } b;
  100542. +} hcdma_data_t;
  100543. +
  100544. +/**
  100545. + * This union represents the bit fields in the DMA Descriptor
  100546. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  100547. + * set/clear the bits using the <i>b</i>it elements.
  100548. + */
  100549. +typedef union host_dma_desc_sts {
  100550. + /** raw register data */
  100551. + uint32_t d32;
  100552. + /** quadlet bits */
  100553. +
  100554. + /* for non-isochronous */
  100555. + struct {
  100556. + /** Number of bytes */
  100557. + unsigned n_bytes:17;
  100558. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  100559. + unsigned qtd_offset:6;
  100560. + /**
  100561. + * Set to request the core to jump to alternate QTD if
  100562. + * Short Packet received - only for IN EPs
  100563. + */
  100564. + unsigned a_qtd:1;
  100565. + /**
  100566. + * Setup Packet bit. When set indicates that buffer contains
  100567. + * setup packet.
  100568. + */
  100569. + unsigned sup:1;
  100570. + /** Interrupt On Complete */
  100571. + unsigned ioc:1;
  100572. + /** End of List */
  100573. + unsigned eol:1;
  100574. + unsigned reserved27:1;
  100575. + /** Rx/Tx Status */
  100576. + unsigned sts:2;
  100577. +#define DMA_DESC_STS_PKTERR 1
  100578. + unsigned reserved30:1;
  100579. + /** Active Bit */
  100580. + unsigned a:1;
  100581. + } b;
  100582. + /* for isochronous */
  100583. + struct {
  100584. + /** Number of bytes */
  100585. + unsigned n_bytes:12;
  100586. + unsigned reserved12_24:13;
  100587. + /** Interrupt On Complete */
  100588. + unsigned ioc:1;
  100589. + unsigned reserved26_27:2;
  100590. + /** Rx/Tx Status */
  100591. + unsigned sts:2;
  100592. + unsigned reserved30:1;
  100593. + /** Active Bit */
  100594. + unsigned a:1;
  100595. + } b_isoc;
  100596. +} host_dma_desc_sts_t;
  100597. +
  100598. +#define MAX_DMA_DESC_SIZE 131071
  100599. +#define MAX_DMA_DESC_NUM_GENERIC 64
  100600. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  100601. +#define MAX_FRLIST_EN_NUM 64
  100602. +/**
  100603. + * Host-mode DMA Descriptor structure
  100604. + *
  100605. + * DMA Descriptor structure contains two quadlets:
  100606. + * Status quadlet and Data buffer pointer.
  100607. + */
  100608. +typedef struct dwc_otg_host_dma_desc {
  100609. + /** DMA Descriptor status quadlet */
  100610. + host_dma_desc_sts_t status;
  100611. + /** DMA Descriptor data buffer pointer */
  100612. + uint32_t buf;
  100613. +} dwc_otg_host_dma_desc_t;
  100614. +
  100615. +/** OTG Host Interface Structure.
  100616. + *
  100617. + * The OTG Host Interface Structure structure contains information
  100618. + * needed to manage the DWC_otg controller acting in host mode. It
  100619. + * represents the programming view of the host-specific aspects of the
  100620. + * controller.
  100621. + */
  100622. +typedef struct dwc_otg_host_if {
  100623. + /** Host Global Registers starting at offset 400h.*/
  100624. + dwc_otg_host_global_regs_t *host_global_regs;
  100625. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  100626. +
  100627. + /** Host Port 0 Control and Status Register */
  100628. + volatile uint32_t *hprt0;
  100629. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  100630. +
  100631. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  100632. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  100633. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  100634. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  100635. +
  100636. + /* Host configuration information */
  100637. + /** Number of Host Channels (range: 1-16) */
  100638. + uint8_t num_host_channels;
  100639. + /** Periodic EPs supported (0: no, 1: yes) */
  100640. + uint8_t perio_eps_supported;
  100641. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  100642. + uint16_t perio_tx_fifo_size;
  100643. +
  100644. +} dwc_otg_host_if_t;
  100645. +
  100646. +/**
  100647. + * This union represents the bit fields in the Power and Clock Gating Control
  100648. + * Register. Read the register into the <i>d32</i> member then set/clear the
  100649. + * bits using the <i>b</i>it elements.
  100650. + */
  100651. +typedef union pcgcctl_data {
  100652. + /** raw register data */
  100653. + uint32_t d32;
  100654. +
  100655. + /** register bits */
  100656. + struct {
  100657. + /** Stop Pclk */
  100658. + unsigned stoppclk:1;
  100659. + /** Gate Hclk */
  100660. + unsigned gatehclk:1;
  100661. + /** Power Clamp */
  100662. + unsigned pwrclmp:1;
  100663. + /** Reset Power Down Modules */
  100664. + unsigned rstpdwnmodule:1;
  100665. + /** Reserved */
  100666. + unsigned reserved:1;
  100667. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  100668. + unsigned enbl_sleep_gating:1;
  100669. + /** PHY In Sleep (PhySleep) */
  100670. + unsigned phy_in_sleep:1;
  100671. + /** Deep Sleep*/
  100672. + unsigned deep_sleep:1;
  100673. + unsigned resetaftsusp:1;
  100674. + unsigned restoremode:1;
  100675. + unsigned enbl_extnd_hiber:1;
  100676. + unsigned extnd_hiber_pwrclmp:1;
  100677. + unsigned extnd_hiber_switch:1;
  100678. + unsigned ess_reg_restored:1;
  100679. + unsigned prt_clk_sel:2;
  100680. + unsigned port_power:1;
  100681. + unsigned max_xcvrselect:2;
  100682. + unsigned max_termsel:1;
  100683. + unsigned mac_dev_addr:7;
  100684. + unsigned p2hd_dev_enum_spd:2;
  100685. + unsigned p2hd_prt_spd:2;
  100686. + unsigned if_dev_mode:1;
  100687. + } b;
  100688. +} pcgcctl_data_t;
  100689. +
  100690. +/**
  100691. + * This union represents the bit fields in the Global Data FIFO Software
  100692. + * Configuration Register. Read the register into the <i>d32</i> member then
  100693. + * set/clear the bits using the <i>b</i>it elements.
  100694. + */
  100695. +typedef union gdfifocfg_data {
  100696. + /* raw register data */
  100697. + uint32_t d32;
  100698. + /** register bits */
  100699. + struct {
  100700. + /** OTG Data FIFO depth */
  100701. + unsigned gdfifocfg:16;
  100702. + /** Start address of EP info controller */
  100703. + unsigned epinfobase:16;
  100704. + } b;
  100705. +} gdfifocfg_data_t;
  100706. +
  100707. +/**
  100708. + * This union represents the bit fields in the Global Power Down Register
  100709. + * Register. Read the register into the <i>d32</i> member then set/clear the
  100710. + * bits using the <i>b</i>it elements.
  100711. + */
  100712. +typedef union gpwrdn_data {
  100713. + /* raw register data */
  100714. + uint32_t d32;
  100715. +
  100716. + /** register bits */
  100717. + struct {
  100718. + /** PMU Interrupt Select */
  100719. + unsigned pmuintsel:1;
  100720. + /** PMU Active */
  100721. + unsigned pmuactv:1;
  100722. + /** Restore */
  100723. + unsigned restore:1;
  100724. + /** Power Down Clamp */
  100725. + unsigned pwrdnclmp:1;
  100726. + /** Power Down Reset */
  100727. + unsigned pwrdnrstn:1;
  100728. + /** Power Down Switch */
  100729. + unsigned pwrdnswtch:1;
  100730. + /** Disable VBUS */
  100731. + unsigned dis_vbus:1;
  100732. + /** Line State Change */
  100733. + unsigned lnstschng:1;
  100734. + /** Line state change mask */
  100735. + unsigned lnstchng_msk:1;
  100736. + /** Reset Detected */
  100737. + unsigned rst_det:1;
  100738. + /** Reset Detect mask */
  100739. + unsigned rst_det_msk:1;
  100740. + /** Disconnect Detected */
  100741. + unsigned disconn_det:1;
  100742. + /** Disconnect Detect mask */
  100743. + unsigned disconn_det_msk:1;
  100744. + /** Connect Detected*/
  100745. + unsigned connect_det:1;
  100746. + /** Connect Detected Mask*/
  100747. + unsigned connect_det_msk:1;
  100748. + /** SRP Detected */
  100749. + unsigned srp_det:1;
  100750. + /** SRP Detect mask */
  100751. + unsigned srp_det_msk:1;
  100752. + /** Status Change Interrupt */
  100753. + unsigned sts_chngint:1;
  100754. + /** Status Change Interrupt Mask */
  100755. + unsigned sts_chngint_msk:1;
  100756. + /** Line State */
  100757. + unsigned linestate:2;
  100758. + /** Indicates current mode(status of IDDIG signal) */
  100759. + unsigned idsts:1;
  100760. + /** B Session Valid signal status*/
  100761. + unsigned bsessvld:1;
  100762. + /** ADP Event Detected */
  100763. + unsigned adp_int:1;
  100764. + /** Multi Valued ID pin */
  100765. + unsigned mult_val_id_bc:5;
  100766. + /** Reserved 24_31 */
  100767. + unsigned reserved29_31:3;
  100768. + } b;
  100769. +} gpwrdn_data_t;
  100770. +
  100771. +#endif
  100772. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/Makefile linux-3.16-rpi/drivers/usb/host/dwc_otg/Makefile
  100773. --- linux-3.16.2/drivers/usb/host/dwc_otg/Makefile 1970-01-01 01:00:00.000000000 +0100
  100774. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/Makefile 2014-09-14 19:04:13.000000000 +0200
  100775. @@ -0,0 +1,82 @@
  100776. +#
  100777. +# Makefile for DWC_otg Highspeed USB controller driver
  100778. +#
  100779. +
  100780. +ifneq ($(KERNELRELEASE),)
  100781. +
  100782. +# Use the BUS_INTERFACE variable to compile the software for either
  100783. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  100784. +ifeq ($(BUS_INTERFACE),)
  100785. +# BUS_INTERFACE = -DPCI_INTERFACE
  100786. +# BUS_INTERFACE = -DLM_INTERFACE
  100787. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  100788. +endif
  100789. +
  100790. +#ccflags-y += -DDEBUG
  100791. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  100792. +
  100793. +# Use one of the following flags to compile the software in host-only or
  100794. +# device-only mode.
  100795. +#ccflags-y += -DDWC_HOST_ONLY
  100796. +#ccflags-y += -DDWC_DEVICE_ONLY
  100797. +
  100798. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  100799. +#ccflags-y += -DDWC_EN_ISOC
  100800. +ccflags-y += -I$(obj)/../dwc_common_port
  100801. +#ccflags-y += -I$(PORTLIB)
  100802. +ccflags-y += -DDWC_LINUX
  100803. +ccflags-y += $(CFI)
  100804. +ccflags-y += $(BUS_INTERFACE)
  100805. +#ccflags-y += -DDWC_DEV_SRPCAP
  100806. +
  100807. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  100808. +
  100809. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  100810. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  100811. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  100812. +dwc_otg-objs += dwc_otg_hcd.o dwc_otg_hcd_linux.o dwc_otg_hcd_intr.o dwc_otg_hcd_queue.o dwc_otg_hcd_ddma.o
  100813. +dwc_otg-objs += dwc_otg_adp.o
  100814. +dwc_otg-objs += dwc_otg_fiq_fsm.o
  100815. +dwc_otg-objs += dwc_otg_fiq_stub.o
  100816. +ifneq ($(CFI),)
  100817. +dwc_otg-objs += dwc_otg_cfi.o
  100818. +endif
  100819. +
  100820. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  100821. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  100822. +
  100823. +ifneq ($(kernrel3),2.6.20)
  100824. +ccflags-y += $(CPPFLAGS)
  100825. +endif
  100826. +
  100827. +else
  100828. +
  100829. +PWD := $(shell pwd)
  100830. +PORTLIB := $(PWD)/../dwc_common_port
  100831. +
  100832. +# Command paths
  100833. +CTAGS := $(CTAGS)
  100834. +DOXYGEN := $(DOXYGEN)
  100835. +
  100836. +default: portlib
  100837. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  100838. +
  100839. +install: default
  100840. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  100841. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  100842. +
  100843. +portlib:
  100844. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  100845. + cp $(PORTLIB)/Module.symvers $(PWD)/
  100846. +
  100847. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  100848. + $(DOXYGEN) doc/doxygen.cfg
  100849. +
  100850. +tags: $(wildcard *.[hc])
  100851. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  100852. +
  100853. +
  100854. +clean:
  100855. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  100856. +
  100857. +endif
  100858. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm linux-3.16-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  100859. --- linux-3.16.2/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 1970-01-01 01:00:00.000000000 +0100
  100860. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm 2014-09-14 19:04:13.000000000 +0200
  100861. @@ -0,0 +1,337 @@
  100862. +package dwc_otg_test;
  100863. +
  100864. +use strict;
  100865. +use Exporter ();
  100866. +
  100867. +use vars qw(@ISA @EXPORT
  100868. +$sysfsdir $paramdir $errors $params
  100869. +);
  100870. +
  100871. +@ISA = qw(Exporter);
  100872. +
  100873. +#
  100874. +# Globals
  100875. +#
  100876. +$sysfsdir = "/sys/devices/lm0";
  100877. +$paramdir = "/sys/module/dwc_otg";
  100878. +$errors = 0;
  100879. +
  100880. +$params = [
  100881. + {
  100882. + NAME => "otg_cap",
  100883. + DEFAULT => 0,
  100884. + ENUM => [],
  100885. + LOW => 0,
  100886. + HIGH => 2
  100887. + },
  100888. + {
  100889. + NAME => "dma_enable",
  100890. + DEFAULT => 0,
  100891. + ENUM => [],
  100892. + LOW => 0,
  100893. + HIGH => 1
  100894. + },
  100895. + {
  100896. + NAME => "dma_burst_size",
  100897. + DEFAULT => 32,
  100898. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  100899. + LOW => 1,
  100900. + HIGH => 256
  100901. + },
  100902. + {
  100903. + NAME => "host_speed",
  100904. + DEFAULT => 0,
  100905. + ENUM => [],
  100906. + LOW => 0,
  100907. + HIGH => 1
  100908. + },
  100909. + {
  100910. + NAME => "host_support_fs_ls_low_power",
  100911. + DEFAULT => 0,
  100912. + ENUM => [],
  100913. + LOW => 0,
  100914. + HIGH => 1
  100915. + },
  100916. + {
  100917. + NAME => "host_ls_low_power_phy_clk",
  100918. + DEFAULT => 0,
  100919. + ENUM => [],
  100920. + LOW => 0,
  100921. + HIGH => 1
  100922. + },
  100923. + {
  100924. + NAME => "dev_speed",
  100925. + DEFAULT => 0,
  100926. + ENUM => [],
  100927. + LOW => 0,
  100928. + HIGH => 1
  100929. + },
  100930. + {
  100931. + NAME => "enable_dynamic_fifo",
  100932. + DEFAULT => 1,
  100933. + ENUM => [],
  100934. + LOW => 0,
  100935. + HIGH => 1
  100936. + },
  100937. + {
  100938. + NAME => "data_fifo_size",
  100939. + DEFAULT => 8192,
  100940. + ENUM => [],
  100941. + LOW => 32,
  100942. + HIGH => 32768
  100943. + },
  100944. + {
  100945. + NAME => "dev_rx_fifo_size",
  100946. + DEFAULT => 1064,
  100947. + ENUM => [],
  100948. + LOW => 16,
  100949. + HIGH => 32768
  100950. + },
  100951. + {
  100952. + NAME => "dev_nperio_tx_fifo_size",
  100953. + DEFAULT => 1024,
  100954. + ENUM => [],
  100955. + LOW => 16,
  100956. + HIGH => 32768
  100957. + },
  100958. + {
  100959. + NAME => "dev_perio_tx_fifo_size_1",
  100960. + DEFAULT => 256,
  100961. + ENUM => [],
  100962. + LOW => 4,
  100963. + HIGH => 768
  100964. + },
  100965. + {
  100966. + NAME => "dev_perio_tx_fifo_size_2",
  100967. + DEFAULT => 256,
  100968. + ENUM => [],
  100969. + LOW => 4,
  100970. + HIGH => 768
  100971. + },
  100972. + {
  100973. + NAME => "dev_perio_tx_fifo_size_3",
  100974. + DEFAULT => 256,
  100975. + ENUM => [],
  100976. + LOW => 4,
  100977. + HIGH => 768
  100978. + },
  100979. + {
  100980. + NAME => "dev_perio_tx_fifo_size_4",
  100981. + DEFAULT => 256,
  100982. + ENUM => [],
  100983. + LOW => 4,
  100984. + HIGH => 768
  100985. + },
  100986. + {
  100987. + NAME => "dev_perio_tx_fifo_size_5",
  100988. + DEFAULT => 256,
  100989. + ENUM => [],
  100990. + LOW => 4,
  100991. + HIGH => 768
  100992. + },
  100993. + {
  100994. + NAME => "dev_perio_tx_fifo_size_6",
  100995. + DEFAULT => 256,
  100996. + ENUM => [],
  100997. + LOW => 4,
  100998. + HIGH => 768
  100999. + },
  101000. + {
  101001. + NAME => "dev_perio_tx_fifo_size_7",
  101002. + DEFAULT => 256,
  101003. + ENUM => [],
  101004. + LOW => 4,
  101005. + HIGH => 768
  101006. + },
  101007. + {
  101008. + NAME => "dev_perio_tx_fifo_size_8",
  101009. + DEFAULT => 256,
  101010. + ENUM => [],
  101011. + LOW => 4,
  101012. + HIGH => 768
  101013. + },
  101014. + {
  101015. + NAME => "dev_perio_tx_fifo_size_9",
  101016. + DEFAULT => 256,
  101017. + ENUM => [],
  101018. + LOW => 4,
  101019. + HIGH => 768
  101020. + },
  101021. + {
  101022. + NAME => "dev_perio_tx_fifo_size_10",
  101023. + DEFAULT => 256,
  101024. + ENUM => [],
  101025. + LOW => 4,
  101026. + HIGH => 768
  101027. + },
  101028. + {
  101029. + NAME => "dev_perio_tx_fifo_size_11",
  101030. + DEFAULT => 256,
  101031. + ENUM => [],
  101032. + LOW => 4,
  101033. + HIGH => 768
  101034. + },
  101035. + {
  101036. + NAME => "dev_perio_tx_fifo_size_12",
  101037. + DEFAULT => 256,
  101038. + ENUM => [],
  101039. + LOW => 4,
  101040. + HIGH => 768
  101041. + },
  101042. + {
  101043. + NAME => "dev_perio_tx_fifo_size_13",
  101044. + DEFAULT => 256,
  101045. + ENUM => [],
  101046. + LOW => 4,
  101047. + HIGH => 768
  101048. + },
  101049. + {
  101050. + NAME => "dev_perio_tx_fifo_size_14",
  101051. + DEFAULT => 256,
  101052. + ENUM => [],
  101053. + LOW => 4,
  101054. + HIGH => 768
  101055. + },
  101056. + {
  101057. + NAME => "dev_perio_tx_fifo_size_15",
  101058. + DEFAULT => 256,
  101059. + ENUM => [],
  101060. + LOW => 4,
  101061. + HIGH => 768
  101062. + },
  101063. + {
  101064. + NAME => "host_rx_fifo_size",
  101065. + DEFAULT => 1024,
  101066. + ENUM => [],
  101067. + LOW => 16,
  101068. + HIGH => 32768
  101069. + },
  101070. + {
  101071. + NAME => "host_nperio_tx_fifo_size",
  101072. + DEFAULT => 1024,
  101073. + ENUM => [],
  101074. + LOW => 16,
  101075. + HIGH => 32768
  101076. + },
  101077. + {
  101078. + NAME => "host_perio_tx_fifo_size",
  101079. + DEFAULT => 1024,
  101080. + ENUM => [],
  101081. + LOW => 16,
  101082. + HIGH => 32768
  101083. + },
  101084. + {
  101085. + NAME => "max_transfer_size",
  101086. + DEFAULT => 65535,
  101087. + ENUM => [],
  101088. + LOW => 2047,
  101089. + HIGH => 65535
  101090. + },
  101091. + {
  101092. + NAME => "max_packet_count",
  101093. + DEFAULT => 511,
  101094. + ENUM => [],
  101095. + LOW => 15,
  101096. + HIGH => 511
  101097. + },
  101098. + {
  101099. + NAME => "host_channels",
  101100. + DEFAULT => 12,
  101101. + ENUM => [],
  101102. + LOW => 1,
  101103. + HIGH => 16
  101104. + },
  101105. + {
  101106. + NAME => "dev_endpoints",
  101107. + DEFAULT => 6,
  101108. + ENUM => [],
  101109. + LOW => 1,
  101110. + HIGH => 15
  101111. + },
  101112. + {
  101113. + NAME => "phy_type",
  101114. + DEFAULT => 1,
  101115. + ENUM => [],
  101116. + LOW => 0,
  101117. + HIGH => 2
  101118. + },
  101119. + {
  101120. + NAME => "phy_utmi_width",
  101121. + DEFAULT => 16,
  101122. + ENUM => [8, 16],
  101123. + LOW => 8,
  101124. + HIGH => 16
  101125. + },
  101126. + {
  101127. + NAME => "phy_ulpi_ddr",
  101128. + DEFAULT => 0,
  101129. + ENUM => [],
  101130. + LOW => 0,
  101131. + HIGH => 1
  101132. + },
  101133. + ];
  101134. +
  101135. +
  101136. +#
  101137. +#
  101138. +sub check_arch {
  101139. + $_ = `uname -m`;
  101140. + chomp;
  101141. + unless (m/armv4tl/) {
  101142. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  101143. + return 0;
  101144. + }
  101145. + return 1;
  101146. +}
  101147. +
  101148. +#
  101149. +#
  101150. +sub load_module {
  101151. + my $params = shift;
  101152. + print "\nRemoving Module\n";
  101153. + system "rmmod dwc_otg";
  101154. + print "Loading Module\n";
  101155. + if ($params ne "") {
  101156. + print "Module Parameters: $params\n";
  101157. + }
  101158. + if (system("modprobe dwc_otg $params")) {
  101159. + warn "Unable to load module\n";
  101160. + return 0;
  101161. + }
  101162. + return 1;
  101163. +}
  101164. +
  101165. +#
  101166. +#
  101167. +sub test_status {
  101168. + my $arg = shift;
  101169. +
  101170. + print "\n";
  101171. +
  101172. + if (defined $arg) {
  101173. + warn "WARNING: $arg\n";
  101174. + }
  101175. +
  101176. + if ($errors > 0) {
  101177. + warn "TEST FAILED with $errors errors\n";
  101178. + return 0;
  101179. + } else {
  101180. + print "TEST PASSED\n";
  101181. + return 0 if (defined $arg);
  101182. + }
  101183. + return 1;
  101184. +}
  101185. +
  101186. +#
  101187. +#
  101188. +@EXPORT = qw(
  101189. +$sysfsdir
  101190. +$paramdir
  101191. +$params
  101192. +$errors
  101193. +check_arch
  101194. +load_module
  101195. +test_status
  101196. +);
  101197. +
  101198. +1;
  101199. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/test/Makefile linux-3.16-rpi/drivers/usb/host/dwc_otg/test/Makefile
  101200. --- linux-3.16.2/drivers/usb/host/dwc_otg/test/Makefile 1970-01-01 01:00:00.000000000 +0100
  101201. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/test/Makefile 2014-04-13 17:33:11.000000000 +0200
  101202. @@ -0,0 +1,16 @@
  101203. +
  101204. +PERL=/usr/bin/perl
  101205. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  101206. +
  101207. +.PHONY : test
  101208. +test : perl_tests
  101209. +
  101210. +perl_tests :
  101211. + @echo
  101212. + @echo Running perl tests
  101213. + @for test in $(PL_TESTS); do \
  101214. + if $(PERL) ./$$test ; then \
  101215. + echo "=======> $$test, PASSED" ; \
  101216. + else echo "=======> $$test, FAILED" ; \
  101217. + fi \
  101218. + done
  101219. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/test/test_mod_param.pl linux-3.16-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  101220. --- linux-3.16.2/drivers/usb/host/dwc_otg/test/test_mod_param.pl 1970-01-01 01:00:00.000000000 +0100
  101221. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/test/test_mod_param.pl 2014-09-14 19:04:13.000000000 +0200
  101222. @@ -0,0 +1,133 @@
  101223. +#!/usr/bin/perl -w
  101224. +#
  101225. +# Run this program on the integrator.
  101226. +#
  101227. +# - Tests module parameter default values.
  101228. +# - Tests setting of valid module parameter values via modprobe.
  101229. +# - Tests invalid module parameter values.
  101230. +# -----------------------------------------------------------------------------
  101231. +use strict;
  101232. +use dwc_otg_test;
  101233. +
  101234. +check_arch() or die;
  101235. +
  101236. +#
  101237. +#
  101238. +sub test {
  101239. + my ($param,$expected) = @_;
  101240. + my $value = get($param);
  101241. +
  101242. + if ($value == $expected) {
  101243. + print "$param = $value, okay\n";
  101244. + }
  101245. +
  101246. + else {
  101247. + warn "ERROR: value of $param != $expected, $value\n";
  101248. + $errors ++;
  101249. + }
  101250. +}
  101251. +
  101252. +#
  101253. +#
  101254. +sub get {
  101255. + my $param = shift;
  101256. + my $tmp = `cat $paramdir/$param`;
  101257. + chomp $tmp;
  101258. + return $tmp;
  101259. +}
  101260. +
  101261. +#
  101262. +#
  101263. +sub test_main {
  101264. +
  101265. + print "\nTesting Module Parameters\n";
  101266. +
  101267. + load_module("") or die;
  101268. +
  101269. + # Test initial values
  101270. + print "\nTesting Default Values\n";
  101271. + foreach (@{$params}) {
  101272. + test ($_->{NAME}, $_->{DEFAULT});
  101273. + }
  101274. +
  101275. + # Test low value
  101276. + print "\nTesting Low Value\n";
  101277. + my $cmd_params = "";
  101278. + foreach (@{$params}) {
  101279. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  101280. + }
  101281. + load_module($cmd_params) or die;
  101282. +
  101283. + foreach (@{$params}) {
  101284. + test ($_->{NAME}, $_->{LOW});
  101285. + }
  101286. +
  101287. + # Test high value
  101288. + print "\nTesting High Value\n";
  101289. + $cmd_params = "";
  101290. + foreach (@{$params}) {
  101291. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  101292. + }
  101293. + load_module($cmd_params) or die;
  101294. +
  101295. + foreach (@{$params}) {
  101296. + test ($_->{NAME}, $_->{HIGH});
  101297. + }
  101298. +
  101299. + # Test Enum
  101300. + print "\nTesting Enumerated\n";
  101301. + foreach (@{$params}) {
  101302. + if (defined $_->{ENUM}) {
  101303. + my $value;
  101304. + foreach $value (@{$_->{ENUM}}) {
  101305. + $cmd_params = "$_->{NAME}=$value";
  101306. + load_module($cmd_params) or die;
  101307. + test ($_->{NAME}, $value);
  101308. + }
  101309. + }
  101310. + }
  101311. +
  101312. + # Test Invalid Values
  101313. + print "\nTesting Invalid Values\n";
  101314. + $cmd_params = "";
  101315. + foreach (@{$params}) {
  101316. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  101317. + }
  101318. + load_module($cmd_params) or die;
  101319. +
  101320. + foreach (@{$params}) {
  101321. + test ($_->{NAME}, $_->{DEFAULT});
  101322. + }
  101323. +
  101324. + $cmd_params = "";
  101325. + foreach (@{$params}) {
  101326. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  101327. + }
  101328. + load_module($cmd_params) or die;
  101329. +
  101330. + foreach (@{$params}) {
  101331. + test ($_->{NAME}, $_->{DEFAULT});
  101332. + }
  101333. +
  101334. + print "\nTesting Enumerated\n";
  101335. + foreach (@{$params}) {
  101336. + if (defined $_->{ENUM}) {
  101337. + my $value;
  101338. + foreach $value (@{$_->{ENUM}}) {
  101339. + $value = $value + 1;
  101340. + $cmd_params = "$_->{NAME}=$value";
  101341. + load_module($cmd_params) or die;
  101342. + test ($_->{NAME}, $_->{DEFAULT});
  101343. + $value = $value - 2;
  101344. + $cmd_params = "$_->{NAME}=$value";
  101345. + load_module($cmd_params) or die;
  101346. + test ($_->{NAME}, $_->{DEFAULT});
  101347. + }
  101348. + }
  101349. + }
  101350. +
  101351. + test_status() or die;
  101352. +}
  101353. +
  101354. +test_main();
  101355. +0;
  101356. diff -Nur linux-3.16.2/drivers/usb/host/dwc_otg/test/test_sysfs.pl linux-3.16-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  101357. --- linux-3.16.2/drivers/usb/host/dwc_otg/test/test_sysfs.pl 1970-01-01 01:00:00.000000000 +0100
  101358. +++ linux-3.16-rpi/drivers/usb/host/dwc_otg/test/test_sysfs.pl 2014-09-14 19:04:13.000000000 +0200
  101359. @@ -0,0 +1,193 @@
  101360. +#!/usr/bin/perl -w
  101361. +#
  101362. +# Run this program on the integrator
  101363. +# - Tests select sysfs attributes.
  101364. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  101365. +# -----------------------------------------------------------------------------
  101366. +use strict;
  101367. +use dwc_otg_test;
  101368. +
  101369. +check_arch() or die;
  101370. +
  101371. +#
  101372. +#
  101373. +sub test {
  101374. + my ($attr,$expected) = @_;
  101375. + my $string = get($attr);
  101376. +
  101377. + if ($string eq $expected) {
  101378. + printf("$attr = $string, okay\n");
  101379. + }
  101380. + else {
  101381. + warn "ERROR: value of $attr != $expected, $string\n";
  101382. + $errors ++;
  101383. + }
  101384. +}
  101385. +
  101386. +#
  101387. +#
  101388. +sub set {
  101389. + my ($reg, $value) = @_;
  101390. + system "echo $value > $sysfsdir/$reg";
  101391. +}
  101392. +
  101393. +#
  101394. +#
  101395. +sub get {
  101396. + my $attr = shift;
  101397. + my $string = `cat $sysfsdir/$attr`;
  101398. + chomp $string;
  101399. + if ($string =~ m/\s\=\s/) {
  101400. + my $tmp;
  101401. + ($tmp, $string) = split /\s=\s/, $string;
  101402. + }
  101403. + return $string;
  101404. +}
  101405. +
  101406. +#
  101407. +#
  101408. +sub test_main {
  101409. + print("\nTesting Sysfs Attributes\n");
  101410. +
  101411. + load_module("") or die;
  101412. +
  101413. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  101414. + print("\nTesting Default Values\n");
  101415. +
  101416. + test("regoffset", "0xffffffff");
  101417. + test("regvalue", "invalid offset");
  101418. + test("guid", "0x12345678"); # this will fail if it has been changed
  101419. + test("gsnpsid", "0x4f54200a");
  101420. +
  101421. + # Test operation of regoffset/regvalue
  101422. + print("\nTesting regoffset\n");
  101423. + set('regoffset', '5a5a5a5a');
  101424. + test("regoffset", "0xffffffff");
  101425. +
  101426. + set('regoffset', '0');
  101427. + test("regoffset", "0x00000000");
  101428. +
  101429. + set('regoffset', '40000');
  101430. + test("regoffset", "0x00000000");
  101431. +
  101432. + set('regoffset', '3ffff');
  101433. + test("regoffset", "0x0003ffff");
  101434. +
  101435. + set('regoffset', '1');
  101436. + test("regoffset", "0x00000001");
  101437. +
  101438. + print("\nTesting regvalue\n");
  101439. + set('regoffset', '3c');
  101440. + test("regvalue", "0x12345678");
  101441. + set('regvalue', '5a5a5a5a');
  101442. + test("regvalue", "0x5a5a5a5a");
  101443. + set('regvalue','a5a5a5a5');
  101444. + test("regvalue", "0xa5a5a5a5");
  101445. + set('guid','12345678');
  101446. +
  101447. + # Test HNP Capable
  101448. + print("\nTesting HNP Capable bit\n");
  101449. + set('hnpcapable', '1');
  101450. + test("hnpcapable", "0x1");
  101451. + set('hnpcapable','0');
  101452. + test("hnpcapable", "0x0");
  101453. +
  101454. + set('regoffset','0c');
  101455. +
  101456. + my $old = get('gusbcfg');
  101457. + print("setting hnpcapable\n");
  101458. + set('hnpcapable', '1');
  101459. + test("hnpcapable", "0x1");
  101460. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  101461. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  101462. +
  101463. + $old = get('gusbcfg');
  101464. + print("clearing hnpcapable\n");
  101465. + set('hnpcapable', '0');
  101466. + test("hnpcapable", "0x0");
  101467. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  101468. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  101469. +
  101470. + # Test SRP Capable
  101471. + print("\nTesting SRP Capable bit\n");
  101472. + set('srpcapable', '1');
  101473. + test("srpcapable", "0x1");
  101474. + set('srpcapable','0');
  101475. + test("srpcapable", "0x0");
  101476. +
  101477. + set('regoffset','0c');
  101478. +
  101479. + $old = get('gusbcfg');
  101480. + print("setting srpcapable\n");
  101481. + set('srpcapable', '1');
  101482. + test("srpcapable", "0x1");
  101483. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  101484. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  101485. +
  101486. + $old = get('gusbcfg');
  101487. + print("clearing srpcapable\n");
  101488. + set('srpcapable', '0');
  101489. + test("srpcapable", "0x0");
  101490. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  101491. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  101492. +
  101493. + # Test GGPIO
  101494. + print("\nTesting GGPIO\n");
  101495. + set('ggpio','5a5a5a5a');
  101496. + test('ggpio','0x5a5a0000');
  101497. + set('ggpio','a5a5a5a5');
  101498. + test('ggpio','0xa5a50000');
  101499. + set('ggpio','11110000');
  101500. + test('ggpio','0x11110000');
  101501. + set('ggpio','00001111');
  101502. + test('ggpio','0x00000000');
  101503. +
  101504. + # Test DEVSPEED
  101505. + print("\nTesting DEVSPEED\n");
  101506. + set('regoffset','800');
  101507. + $old = get('regvalue');
  101508. + set('devspeed','0');
  101509. + test('devspeed','0x0');
  101510. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  101511. + set('devspeed','1');
  101512. + test('devspeed','0x1');
  101513. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  101514. + set('devspeed','2');
  101515. + test('devspeed','0x2');
  101516. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  101517. + set('devspeed','3');
  101518. + test('devspeed','0x3');
  101519. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  101520. + set('devspeed','4');
  101521. + test('devspeed','0x0');
  101522. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  101523. + set('devspeed','5');
  101524. + test('devspeed','0x1');
  101525. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  101526. +
  101527. +
  101528. + # mode Returns the current mode:0 for device mode1 for host mode Read
  101529. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  101530. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  101531. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  101532. + # bussuspend Suspend the USB bus. Read/Write
  101533. + # busconnected Get the connection status of the bus Read
  101534. +
  101535. + # gotgctl Get or set the Core Control Status Register. Read/Write
  101536. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  101537. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  101538. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  101539. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  101540. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  101541. + ## guid Get or set the value of the User ID Register Read/Write
  101542. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  101543. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  101544. + # enumspeed Gets the device enumeration Speed. Read
  101545. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  101546. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  101547. +
  101548. + test_status("TEST NYI") or die;
  101549. +}
  101550. +
  101551. +test_main();
  101552. +0;
  101553. diff -Nur linux-3.16.2/drivers/usb/host/Kconfig linux-3.16-rpi/drivers/usb/host/Kconfig
  101554. --- linux-3.16.2/drivers/usb/host/Kconfig 2014-09-06 01:37:11.000000000 +0200
  101555. +++ linux-3.16-rpi/drivers/usb/host/Kconfig 2014-09-14 19:04:13.000000000 +0200
  101556. @@ -713,6 +713,19 @@
  101557. To compile this driver a module, choose M here: the module
  101558. will be called "hwa-hc".
  101559. +config USB_DWCOTG
  101560. + tristate "Synopsis DWC host support"
  101561. + depends on USB
  101562. + help
  101563. + The Synopsis DWC controller is a dual-role
  101564. + host/peripheral/OTG ("On The Go") USB controllers.
  101565. +
  101566. + Enable this option to support this IP in host controller mode.
  101567. + If unsure, say N.
  101568. +
  101569. + To compile this driver as a module, choose M here: the
  101570. + modules built will be called dwc_otg and dwc_common_port.
  101571. +
  101572. config USB_IMX21_HCD
  101573. tristate "i.MX21 HCD support"
  101574. depends on ARM && ARCH_MXC
  101575. diff -Nur linux-3.16.2/drivers/usb/host/Makefile linux-3.16-rpi/drivers/usb/host/Makefile
  101576. --- linux-3.16.2/drivers/usb/host/Makefile 2014-09-06 01:37:11.000000000 +0200
  101577. +++ linux-3.16-rpi/drivers/usb/host/Makefile 2014-09-14 19:04:13.000000000 +0200
  101578. @@ -66,6 +66,8 @@
  101579. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  101580. obj-$(CONFIG_USB_ISP1760_HCD) += isp1760.o
  101581. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  101582. +
  101583. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  101584. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  101585. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += fsl-mph-dr-of.o
  101586. obj-$(CONFIG_USB_OCTEON2_COMMON) += octeon2-common.o
  101587. diff -Nur linux-3.16.2/drivers/usb/Makefile linux-3.16-rpi/drivers/usb/Makefile
  101588. --- linux-3.16.2/drivers/usb/Makefile 2014-09-06 01:37:11.000000000 +0200
  101589. +++ linux-3.16-rpi/drivers/usb/Makefile 2014-09-14 19:04:12.000000000 +0200
  101590. @@ -24,6 +24,7 @@
  101591. obj-$(CONFIG_USB_R8A66597_HCD) += host/
  101592. obj-$(CONFIG_USB_HWA_HCD) += host/
  101593. obj-$(CONFIG_USB_ISP1760_HCD) += host/
  101594. +obj-$(CONFIG_USB_DWCOTG) += host/
  101595. obj-$(CONFIG_USB_IMX21_HCD) += host/
  101596. obj-$(CONFIG_USB_FSL_MPH_DR_OF) += host/
  101597. obj-$(CONFIG_USB_FUSBH200_HCD) += host/
  101598. diff -Nur linux-3.16.2/drivers/video/fbdev/bcm2708_fb.c linux-3.16-rpi/drivers/video/fbdev/bcm2708_fb.c
  101599. --- linux-3.16.2/drivers/video/fbdev/bcm2708_fb.c 1970-01-01 01:00:00.000000000 +0100
  101600. +++ linux-3.16-rpi/drivers/video/fbdev/bcm2708_fb.c 2014-09-14 19:04:14.000000000 +0200
  101601. @@ -0,0 +1,788 @@
  101602. +/*
  101603. + * linux/drivers/video/bcm2708_fb.c
  101604. + *
  101605. + * Copyright (C) 2010 Broadcom
  101606. + *
  101607. + * This file is subject to the terms and conditions of the GNU General Public
  101608. + * License. See the file COPYING in the main directory of this archive
  101609. + * for more details.
  101610. + *
  101611. + * Broadcom simple framebuffer driver
  101612. + *
  101613. + * This file is derived from cirrusfb.c
  101614. + * Copyright 1999-2001 Jeff Garzik <jgarzik@pobox.com>
  101615. + *
  101616. + */
  101617. +#include <linux/module.h>
  101618. +#include <linux/kernel.h>
  101619. +#include <linux/errno.h>
  101620. +#include <linux/string.h>
  101621. +#include <linux/slab.h>
  101622. +#include <linux/mm.h>
  101623. +#include <linux/fb.h>
  101624. +#include <linux/init.h>
  101625. +#include <linux/interrupt.h>
  101626. +#include <linux/ioport.h>
  101627. +#include <linux/list.h>
  101628. +#include <linux/platform_device.h>
  101629. +#include <linux/clk.h>
  101630. +#include <linux/printk.h>
  101631. +#include <linux/console.h>
  101632. +#include <linux/debugfs.h>
  101633. +
  101634. +#include <mach/dma.h>
  101635. +#include <mach/platform.h>
  101636. +#include <mach/vcio.h>
  101637. +
  101638. +#include <asm/sizes.h>
  101639. +#include <linux/io.h>
  101640. +#include <linux/dma-mapping.h>
  101641. +
  101642. +#ifdef BCM2708_FB_DEBUG
  101643. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  101644. +#else
  101645. +#define print_debug(fmt,...)
  101646. +#endif
  101647. +
  101648. +/* This is limited to 16 characters when displayed by X startup */
  101649. +static const char *bcm2708_name = "BCM2708 FB";
  101650. +
  101651. +#define DRIVER_NAME "bcm2708_fb"
  101652. +
  101653. +static int fbwidth = 800; /* module parameter */
  101654. +static int fbheight = 480; /* module parameter */
  101655. +static int fbdepth = 16; /* module parameter */
  101656. +static int fbswap = 0; /* module parameter */
  101657. +
  101658. +static u32 dma_busy_wait_threshold = 1<<15;
  101659. +module_param(dma_busy_wait_threshold, int, 0644);
  101660. +MODULE_PARM_DESC(dma_busy_wait_threshold, "Busy-wait for DMA completion below this area");
  101661. +
  101662. +/* this data structure describes each frame buffer device we find */
  101663. +
  101664. +struct fbinfo_s {
  101665. + u32 xres, yres, xres_virtual, yres_virtual;
  101666. + u32 pitch, bpp;
  101667. + u32 xoffset, yoffset;
  101668. + u32 base;
  101669. + u32 screen_size;
  101670. + u16 cmap[256];
  101671. +};
  101672. +
  101673. +struct bcm2708_fb_stats {
  101674. + struct debugfs_regset32 regset;
  101675. + u32 dma_copies;
  101676. + u32 dma_irqs;
  101677. +};
  101678. +
  101679. +struct bcm2708_fb {
  101680. + struct fb_info fb;
  101681. + struct platform_device *dev;
  101682. + struct fbinfo_s *info;
  101683. + dma_addr_t dma;
  101684. + u32 cmap[16];
  101685. + int dma_chan;
  101686. + int dma_irq;
  101687. + void __iomem *dma_chan_base;
  101688. + void *cb_base; /* DMA control blocks */
  101689. + dma_addr_t cb_handle;
  101690. + struct dentry *debugfs_dir;
  101691. + wait_queue_head_t dma_waitq;
  101692. + struct bcm2708_fb_stats stats;
  101693. + unsigned long fb_bus_address;
  101694. +};
  101695. +
  101696. +#define to_bcm2708(info) container_of(info, struct bcm2708_fb, fb)
  101697. +
  101698. +static void bcm2708_fb_debugfs_deinit(struct bcm2708_fb *fb)
  101699. +{
  101700. + debugfs_remove_recursive(fb->debugfs_dir);
  101701. + fb->debugfs_dir = NULL;
  101702. +}
  101703. +
  101704. +static int bcm2708_fb_debugfs_init(struct bcm2708_fb *fb)
  101705. +{
  101706. + static struct debugfs_reg32 stats_registers[] = {
  101707. + {
  101708. + "dma_copies",
  101709. + offsetof(struct bcm2708_fb_stats, dma_copies)
  101710. + },
  101711. + {
  101712. + "dma_irqs",
  101713. + offsetof(struct bcm2708_fb_stats, dma_irqs)
  101714. + },
  101715. + };
  101716. +
  101717. + fb->debugfs_dir = debugfs_create_dir(DRIVER_NAME, NULL);
  101718. + if (!fb->debugfs_dir) {
  101719. + pr_warn("%s: could not create debugfs entry\n",
  101720. + __func__);
  101721. + return -EFAULT;
  101722. + }
  101723. +
  101724. + fb->stats.regset.regs = stats_registers;
  101725. + fb->stats.regset.nregs = ARRAY_SIZE(stats_registers);
  101726. + fb->stats.regset.base = &fb->stats;
  101727. +
  101728. + if (!debugfs_create_regset32(
  101729. + "stats", 0444, fb->debugfs_dir, &fb->stats.regset)) {
  101730. + pr_warn("%s: could not create statistics registers\n",
  101731. + __func__);
  101732. + goto fail;
  101733. + }
  101734. + return 0;
  101735. +
  101736. +fail:
  101737. + bcm2708_fb_debugfs_deinit(fb);
  101738. + return -EFAULT;
  101739. +}
  101740. +
  101741. +static int bcm2708_fb_set_bitfields(struct fb_var_screeninfo *var)
  101742. +{
  101743. + int ret = 0;
  101744. +
  101745. + memset(&var->transp, 0, sizeof(var->transp));
  101746. +
  101747. + var->red.msb_right = 0;
  101748. + var->green.msb_right = 0;
  101749. + var->blue.msb_right = 0;
  101750. +
  101751. + switch (var->bits_per_pixel) {
  101752. + case 1:
  101753. + case 2:
  101754. + case 4:
  101755. + case 8:
  101756. + var->red.length = var->bits_per_pixel;
  101757. + var->red.offset = 0;
  101758. + var->green.length = var->bits_per_pixel;
  101759. + var->green.offset = 0;
  101760. + var->blue.length = var->bits_per_pixel;
  101761. + var->blue.offset = 0;
  101762. + break;
  101763. + case 16:
  101764. + var->red.length = 5;
  101765. + var->blue.length = 5;
  101766. + /*
  101767. + * Green length can be 5 or 6 depending whether
  101768. + * we're operating in RGB555 or RGB565 mode.
  101769. + */
  101770. + if (var->green.length != 5 && var->green.length != 6)
  101771. + var->green.length = 6;
  101772. + break;
  101773. + case 24:
  101774. + var->red.length = 8;
  101775. + var->blue.length = 8;
  101776. + var->green.length = 8;
  101777. + break;
  101778. + case 32:
  101779. + var->red.length = 8;
  101780. + var->green.length = 8;
  101781. + var->blue.length = 8;
  101782. + var->transp.length = 8;
  101783. + break;
  101784. + default:
  101785. + ret = -EINVAL;
  101786. + break;
  101787. + }
  101788. +
  101789. + /*
  101790. + * >= 16bpp displays have separate colour component bitfields
  101791. + * encoded in the pixel data. Calculate their position from
  101792. + * the bitfield length defined above.
  101793. + */
  101794. + if (ret == 0 && var->bits_per_pixel >= 24 && fbswap) {
  101795. + var->blue.offset = 0;
  101796. + var->green.offset = var->blue.offset + var->blue.length;
  101797. + var->red.offset = var->green.offset + var->green.length;
  101798. + var->transp.offset = var->red.offset + var->red.length;
  101799. + } else if (ret == 0 && var->bits_per_pixel >= 24) {
  101800. + var->red.offset = 0;
  101801. + var->green.offset = var->red.offset + var->red.length;
  101802. + var->blue.offset = var->green.offset + var->green.length;
  101803. + var->transp.offset = var->blue.offset + var->blue.length;
  101804. + } else if (ret == 0 && var->bits_per_pixel >= 16) {
  101805. + var->blue.offset = 0;
  101806. + var->green.offset = var->blue.offset + var->blue.length;
  101807. + var->red.offset = var->green.offset + var->green.length;
  101808. + var->transp.offset = var->red.offset + var->red.length;
  101809. + }
  101810. +
  101811. + return ret;
  101812. +}
  101813. +
  101814. +static int bcm2708_fb_check_var(struct fb_var_screeninfo *var,
  101815. + struct fb_info *info)
  101816. +{
  101817. + /* info input, var output */
  101818. + int yres;
  101819. +
  101820. + /* info input, var output */
  101821. + print_debug("bcm2708_fb_check_var info(%p) %dx%d (%dx%d), %d, %d\n", info,
  101822. + info->var.xres, info->var.yres, info->var.xres_virtual,
  101823. + info->var.yres_virtual, (int)info->screen_size,
  101824. + info->var.bits_per_pixel);
  101825. + print_debug("bcm2708_fb_check_var var(%p) %dx%d (%dx%d), %d\n", var,
  101826. + var->xres, var->yres, var->xres_virtual, var->yres_virtual,
  101827. + var->bits_per_pixel);
  101828. +
  101829. + if (!var->bits_per_pixel)
  101830. + var->bits_per_pixel = 16;
  101831. +
  101832. + if (bcm2708_fb_set_bitfields(var) != 0) {
  101833. + pr_err("bcm2708_fb_check_var: invalid bits_per_pixel %d\n",
  101834. + var->bits_per_pixel);
  101835. + return -EINVAL;
  101836. + }
  101837. +
  101838. +
  101839. + if (var->xres_virtual < var->xres)
  101840. + var->xres_virtual = var->xres;
  101841. + /* use highest possible virtual resolution */
  101842. + if (var->yres_virtual == -1) {
  101843. + var->yres_virtual = 480;
  101844. +
  101845. + pr_err
  101846. + ("bcm2708_fb_check_var: virtual resolution set to maximum of %dx%d\n",
  101847. + var->xres_virtual, var->yres_virtual);
  101848. + }
  101849. + if (var->yres_virtual < var->yres)
  101850. + var->yres_virtual = var->yres;
  101851. +
  101852. + if (var->xoffset < 0)
  101853. + var->xoffset = 0;
  101854. + if (var->yoffset < 0)
  101855. + var->yoffset = 0;
  101856. +
  101857. + /* truncate xoffset and yoffset to maximum if too high */
  101858. + if (var->xoffset > var->xres_virtual - var->xres)
  101859. + var->xoffset = var->xres_virtual - var->xres - 1;
  101860. + if (var->yoffset > var->yres_virtual - var->yres)
  101861. + var->yoffset = var->yres_virtual - var->yres - 1;
  101862. +
  101863. + yres = var->yres;
  101864. + if (var->vmode & FB_VMODE_DOUBLE)
  101865. + yres *= 2;
  101866. + else if (var->vmode & FB_VMODE_INTERLACED)
  101867. + yres = (yres + 1) / 2;
  101868. +
  101869. + if (var->xres * yres > 1920 * 1200) {
  101870. + pr_err("bcm2708_fb_check_var: ERROR: Pixel size >= 1920x1200; "
  101871. + "special treatment required! (TODO)\n");
  101872. + return -EINVAL;
  101873. + }
  101874. +
  101875. + return 0;
  101876. +}
  101877. +
  101878. +static int bcm2708_fb_set_par(struct fb_info *info)
  101879. +{
  101880. + uint32_t val = 0;
  101881. + struct bcm2708_fb *fb = to_bcm2708(info);
  101882. + volatile struct fbinfo_s *fbinfo = fb->info;
  101883. + fbinfo->xres = info->var.xres;
  101884. + fbinfo->yres = info->var.yres;
  101885. + fbinfo->xres_virtual = info->var.xres_virtual;
  101886. + fbinfo->yres_virtual = info->var.yres_virtual;
  101887. + fbinfo->bpp = info->var.bits_per_pixel;
  101888. + fbinfo->xoffset = info->var.xoffset;
  101889. + fbinfo->yoffset = info->var.yoffset;
  101890. + fbinfo->base = 0; /* filled in by VC */
  101891. + fbinfo->pitch = 0; /* filled in by VC */
  101892. +
  101893. + print_debug("bcm2708_fb_set_par info(%p) %dx%d (%dx%d), %d, %d\n", info,
  101894. + info->var.xres, info->var.yres, info->var.xres_virtual,
  101895. + info->var.yres_virtual, (int)info->screen_size,
  101896. + info->var.bits_per_pixel);
  101897. +
  101898. + /* ensure last write to fbinfo is visible to GPU */
  101899. + wmb();
  101900. +
  101901. + /* inform vc about new framebuffer */
  101902. + bcm_mailbox_write(MBOX_CHAN_FB, fb->dma);
  101903. +
  101904. + /* TODO: replace fb driver with vchiq version */
  101905. + /* wait for response */
  101906. + bcm_mailbox_read(MBOX_CHAN_FB, &val);
  101907. +
  101908. + /* ensure GPU writes are visible to us */
  101909. + rmb();
  101910. +
  101911. + if (val == 0) {
  101912. + fb->fb.fix.line_length = fbinfo->pitch;
  101913. +
  101914. + if (info->var.bits_per_pixel <= 8)
  101915. + fb->fb.fix.visual = FB_VISUAL_PSEUDOCOLOR;
  101916. + else
  101917. + fb->fb.fix.visual = FB_VISUAL_TRUECOLOR;
  101918. +
  101919. + fb->fb_bus_address = fbinfo->base;
  101920. + fbinfo->base &= ~0xc0000000;
  101921. + fb->fb.fix.smem_start = fbinfo->base;
  101922. + fb->fb.fix.smem_len = fbinfo->pitch * fbinfo->yres_virtual;
  101923. + fb->fb.screen_size = fbinfo->screen_size;
  101924. + if (fb->fb.screen_base)
  101925. + iounmap(fb->fb.screen_base);
  101926. + fb->fb.screen_base =
  101927. + (void *)ioremap_wc(fbinfo->base, fb->fb.screen_size);
  101928. + if (!fb->fb.screen_base) {
  101929. + /* the console may currently be locked */
  101930. + console_trylock();
  101931. + console_unlock();
  101932. +
  101933. + BUG(); /* what can we do here */
  101934. + }
  101935. + }
  101936. + print_debug
  101937. + ("BCM2708FB: start = %p,%p width=%d, height=%d, bpp=%d, pitch=%d size=%d success=%d\n",
  101938. + (void *)fb->fb.screen_base, (void *)fb->fb_bus_address,
  101939. + fbinfo->xres, fbinfo->yres, fbinfo->bpp,
  101940. + fbinfo->pitch, (int)fb->fb.screen_size, val);
  101941. +
  101942. + return val;
  101943. +}
  101944. +
  101945. +static inline u32 convert_bitfield(int val, struct fb_bitfield *bf)
  101946. +{
  101947. + unsigned int mask = (1 << bf->length) - 1;
  101948. +
  101949. + return (val >> (16 - bf->length) & mask) << bf->offset;
  101950. +}
  101951. +
  101952. +
  101953. +static int bcm2708_fb_setcolreg(unsigned int regno, unsigned int red,
  101954. + unsigned int green, unsigned int blue,
  101955. + unsigned int transp, struct fb_info *info)
  101956. +{
  101957. + struct bcm2708_fb *fb = to_bcm2708(info);
  101958. +
  101959. + /*print_debug("BCM2708FB: setcolreg %d:(%02x,%02x,%02x,%02x) %x\n", regno, red, green, blue, transp, fb->fb.fix.visual);*/
  101960. + if (fb->fb.var.bits_per_pixel <= 8) {
  101961. + if (regno < 256) {
  101962. + /* blue [0:4], green [5:10], red [11:15] */
  101963. + fb->info->cmap[regno] = ((red >> (16-5)) & 0x1f) << 11 |
  101964. + ((green >> (16-6)) & 0x3f) << 5 |
  101965. + ((blue >> (16-5)) & 0x1f) << 0;
  101966. + }
  101967. + /* Hack: we need to tell GPU the palette has changed, but currently bcm2708_fb_set_par takes noticable time when called for every (256) colour */
  101968. + /* So just call it for what looks like the last colour in a list for now. */
  101969. + if (regno == 15 || regno == 255)
  101970. + bcm2708_fb_set_par(info);
  101971. + } else if (regno < 16) {
  101972. + fb->cmap[regno] = convert_bitfield(transp, &fb->fb.var.transp) |
  101973. + convert_bitfield(blue, &fb->fb.var.blue) |
  101974. + convert_bitfield(green, &fb->fb.var.green) |
  101975. + convert_bitfield(red, &fb->fb.var.red);
  101976. + }
  101977. + return regno > 255;
  101978. +}
  101979. +static int bcm2708_fb_blank(int blank_mode, struct fb_info *info)
  101980. +{
  101981. + s32 result = -1 ;
  101982. + u32 p[7];
  101983. + if ( (blank_mode == FB_BLANK_NORMAL) ||
  101984. + (blank_mode == FB_BLANK_UNBLANK)) {
  101985. +
  101986. + pr_info("bcm2708_fb_blank blank_mode=%d\n",blank_mode);
  101987. +
  101988. +
  101989. + p[0] = 28; // size = sizeof u32 * length of p
  101990. + p[1] = VCMSG_PROCESS_REQUEST; // process request
  101991. + p[2] = VCMSG_SET_BLANK_SCREEN; // (the tag id)
  101992. + p[3] = 4; // (size of the response buffer)
  101993. + p[4] = 4; // (size of the request data)
  101994. + p[5] = blank_mode;
  101995. + p[6] = VCMSG_PROPERTY_END; // end tag
  101996. +
  101997. + bcm_mailbox_property(&p, p[0]);
  101998. +
  101999. + pr_info("bcm2708_fb_blank returns=%d p[1]=0x%x\n",p[5],p[1]);
  102000. +
  102001. + if ( p[1] == VCMSG_REQUEST_SUCCESSFUL )
  102002. + result = 0 ;
  102003. +
  102004. + }
  102005. + return result;
  102006. +
  102007. +
  102008. +}
  102009. +static void bcm2708_fb_fillrect(struct fb_info *info,
  102010. + const struct fb_fillrect *rect)
  102011. +{
  102012. + /* (is called) print_debug("bcm2708_fb_fillrect\n"); */
  102013. + cfb_fillrect(info, rect);
  102014. +}
  102015. +
  102016. +/* A helper function for configuring dma control block */
  102017. +static void set_dma_cb(struct bcm2708_dma_cb *cb,
  102018. + int burst_size,
  102019. + dma_addr_t dst,
  102020. + int dst_stride,
  102021. + dma_addr_t src,
  102022. + int src_stride,
  102023. + int w,
  102024. + int h)
  102025. +{
  102026. + cb->info = BCM2708_DMA_BURST(burst_size) | BCM2708_DMA_S_WIDTH |
  102027. + BCM2708_DMA_S_INC | BCM2708_DMA_D_WIDTH |
  102028. + BCM2708_DMA_D_INC | BCM2708_DMA_TDMODE;
  102029. + cb->dst = dst;
  102030. + cb->src = src;
  102031. + /*
  102032. + * This is not really obvious from the DMA documentation,
  102033. + * but the top 16 bits must be programmmed to "height -1"
  102034. + * and not "height" in 2D mode.
  102035. + */
  102036. + cb->length = ((h - 1) << 16) | w;
  102037. + cb->stride = ((dst_stride - w) << 16) | (u16)(src_stride - w);
  102038. + cb->pad[0] = 0;
  102039. + cb->pad[1] = 0;
  102040. +}
  102041. +
  102042. +static void bcm2708_fb_copyarea(struct fb_info *info,
  102043. + const struct fb_copyarea *region)
  102044. +{
  102045. + struct bcm2708_fb *fb = to_bcm2708(info);
  102046. + struct bcm2708_dma_cb *cb = fb->cb_base;
  102047. + int bytes_per_pixel = (info->var.bits_per_pixel + 7) >> 3;
  102048. + /* Channel 0 supports larger bursts and is a bit faster */
  102049. + int burst_size = (fb->dma_chan == 0) ? 8 : 2;
  102050. + int pixels = region->width * region->height;
  102051. +
  102052. + /* Fallback to cfb_copyarea() if we don't like something */
  102053. + if (bytes_per_pixel > 4 ||
  102054. + info->var.xres * info->var.yres > 1920 * 1200 ||
  102055. + region->width <= 0 || region->width > info->var.xres ||
  102056. + region->height <= 0 || region->height > info->var.yres ||
  102057. + region->sx < 0 || region->sx >= info->var.xres ||
  102058. + region->sy < 0 || region->sy >= info->var.yres ||
  102059. + region->dx < 0 || region->dx >= info->var.xres ||
  102060. + region->dy < 0 || region->dy >= info->var.yres ||
  102061. + region->sx + region->width > info->var.xres ||
  102062. + region->dx + region->width > info->var.xres ||
  102063. + region->sy + region->height > info->var.yres ||
  102064. + region->dy + region->height > info->var.yres) {
  102065. + cfb_copyarea(info, region);
  102066. + return;
  102067. + }
  102068. +
  102069. + if (region->dy == region->sy && region->dx > region->sx) {
  102070. + /*
  102071. + * A difficult case of overlapped copy. Because DMA can't
  102072. + * copy individual scanlines in backwards direction, we need
  102073. + * two-pass processing. We do it by programming a chain of dma
  102074. + * control blocks in the first 16K part of the buffer and use
  102075. + * the remaining 48K as the intermediate temporary scratch
  102076. + * buffer. The buffer size is sufficient to handle up to
  102077. + * 1920x1200 resolution at 32bpp pixel depth.
  102078. + */
  102079. + int y;
  102080. + dma_addr_t control_block_pa = fb->cb_handle;
  102081. + dma_addr_t scratchbuf = fb->cb_handle + 16 * 1024;
  102082. + int scanline_size = bytes_per_pixel * region->width;
  102083. + int scanlines_per_cb = (64 * 1024 - 16 * 1024) / scanline_size;
  102084. +
  102085. + for (y = 0; y < region->height; y += scanlines_per_cb) {
  102086. + dma_addr_t src =
  102087. + fb->fb_bus_address +
  102088. + bytes_per_pixel * region->sx +
  102089. + (region->sy + y) * fb->fb.fix.line_length;
  102090. + dma_addr_t dst =
  102091. + fb->fb_bus_address +
  102092. + bytes_per_pixel * region->dx +
  102093. + (region->dy + y) * fb->fb.fix.line_length;
  102094. +
  102095. + if (region->height - y < scanlines_per_cb)
  102096. + scanlines_per_cb = region->height - y;
  102097. +
  102098. + set_dma_cb(cb, burst_size, scratchbuf, scanline_size,
  102099. + src, fb->fb.fix.line_length,
  102100. + scanline_size, scanlines_per_cb);
  102101. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  102102. + cb->next = control_block_pa;
  102103. + cb++;
  102104. +
  102105. + set_dma_cb(cb, burst_size, dst, fb->fb.fix.line_length,
  102106. + scratchbuf, scanline_size,
  102107. + scanline_size, scanlines_per_cb);
  102108. + control_block_pa += sizeof(struct bcm2708_dma_cb);
  102109. + cb->next = control_block_pa;
  102110. + cb++;
  102111. + }
  102112. + /* move the pointer back to the last dma control block */
  102113. + cb--;
  102114. + } else {
  102115. + /* A single dma control block is enough. */
  102116. + int sy, dy, stride;
  102117. + if (region->dy <= region->sy) {
  102118. + /* processing from top to bottom */
  102119. + dy = region->dy;
  102120. + sy = region->sy;
  102121. + stride = fb->fb.fix.line_length;
  102122. + } else {
  102123. + /* processing from bottom to top */
  102124. + dy = region->dy + region->height - 1;
  102125. + sy = region->sy + region->height - 1;
  102126. + stride = -fb->fb.fix.line_length;
  102127. + }
  102128. + set_dma_cb(cb, burst_size,
  102129. + fb->fb_bus_address + dy * fb->fb.fix.line_length +
  102130. + bytes_per_pixel * region->dx,
  102131. + stride,
  102132. + fb->fb_bus_address + sy * fb->fb.fix.line_length +
  102133. + bytes_per_pixel * region->sx,
  102134. + stride,
  102135. + region->width * bytes_per_pixel,
  102136. + region->height);
  102137. + }
  102138. +
  102139. + /* end of dma control blocks chain */
  102140. + cb->next = 0;
  102141. +
  102142. +
  102143. + if (pixels < dma_busy_wait_threshold) {
  102144. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  102145. + bcm_dma_wait_idle(fb->dma_chan_base);
  102146. + } else {
  102147. + void __iomem *dma_chan = fb->dma_chan_base;
  102148. + cb->info |= BCM2708_DMA_INT_EN;
  102149. + bcm_dma_start(fb->dma_chan_base, fb->cb_handle);
  102150. + while (bcm_dma_is_busy(dma_chan)) {
  102151. + wait_event_interruptible(
  102152. + fb->dma_waitq,
  102153. + !bcm_dma_is_busy(dma_chan));
  102154. + }
  102155. + fb->stats.dma_irqs++;
  102156. + }
  102157. + fb->stats.dma_copies++;
  102158. +}
  102159. +
  102160. +static void bcm2708_fb_imageblit(struct fb_info *info,
  102161. + const struct fb_image *image)
  102162. +{
  102163. + /* (is called) print_debug("bcm2708_fb_imageblit\n"); */
  102164. + cfb_imageblit(info, image);
  102165. +}
  102166. +
  102167. +static irqreturn_t bcm2708_fb_dma_irq(int irq, void *cxt)
  102168. +{
  102169. + struct bcm2708_fb *fb = cxt;
  102170. +
  102171. + /* FIXME: should read status register to check if this is
  102172. + * actually interrupting us or not, in case this interrupt
  102173. + * ever becomes shared amongst several DMA channels
  102174. + *
  102175. + * readl(dma_chan_base + BCM2708_DMA_CS) & BCM2708_DMA_IRQ;
  102176. + */
  102177. +
  102178. + /* acknowledge the interrupt */
  102179. + writel(BCM2708_DMA_INT, fb->dma_chan_base + BCM2708_DMA_CS);
  102180. +
  102181. + wake_up(&fb->dma_waitq);
  102182. + return IRQ_HANDLED;
  102183. +}
  102184. +
  102185. +static struct fb_ops bcm2708_fb_ops = {
  102186. + .owner = THIS_MODULE,
  102187. + .fb_check_var = bcm2708_fb_check_var,
  102188. + .fb_set_par = bcm2708_fb_set_par,
  102189. + .fb_setcolreg = bcm2708_fb_setcolreg,
  102190. + .fb_blank = bcm2708_fb_blank,
  102191. + .fb_fillrect = bcm2708_fb_fillrect,
  102192. + .fb_copyarea = bcm2708_fb_copyarea,
  102193. + .fb_imageblit = bcm2708_fb_imageblit,
  102194. +};
  102195. +
  102196. +static int bcm2708_fb_register(struct bcm2708_fb *fb)
  102197. +{
  102198. + int ret;
  102199. + dma_addr_t dma;
  102200. + void *mem;
  102201. +
  102202. + mem =
  102203. + dma_alloc_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), &dma,
  102204. + GFP_KERNEL);
  102205. +
  102206. + if (NULL == mem) {
  102207. + pr_err(": unable to allocate fbinfo buffer\n");
  102208. + ret = -ENOMEM;
  102209. + } else {
  102210. + fb->info = (struct fbinfo_s *)mem;
  102211. + fb->dma = dma;
  102212. + }
  102213. + fb->fb.fbops = &bcm2708_fb_ops;
  102214. + fb->fb.flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_COPYAREA;
  102215. + fb->fb.pseudo_palette = fb->cmap;
  102216. +
  102217. + strncpy(fb->fb.fix.id, bcm2708_name, sizeof(fb->fb.fix.id));
  102218. + fb->fb.fix.type = FB_TYPE_PACKED_PIXELS;
  102219. + fb->fb.fix.type_aux = 0;
  102220. + fb->fb.fix.xpanstep = 0;
  102221. + fb->fb.fix.ypanstep = 0;
  102222. + fb->fb.fix.ywrapstep = 0;
  102223. + fb->fb.fix.accel = FB_ACCEL_NONE;
  102224. +
  102225. + fb->fb.var.xres = fbwidth;
  102226. + fb->fb.var.yres = fbheight;
  102227. + fb->fb.var.xres_virtual = fbwidth;
  102228. + fb->fb.var.yres_virtual = fbheight;
  102229. + fb->fb.var.bits_per_pixel = fbdepth;
  102230. + fb->fb.var.vmode = FB_VMODE_NONINTERLACED;
  102231. + fb->fb.var.activate = FB_ACTIVATE_NOW;
  102232. + fb->fb.var.nonstd = 0;
  102233. + fb->fb.var.height = -1; /* height of picture in mm */
  102234. + fb->fb.var.width = -1; /* width of picture in mm */
  102235. + fb->fb.var.accel_flags = 0;
  102236. +
  102237. + fb->fb.monspecs.hfmin = 0;
  102238. + fb->fb.monspecs.hfmax = 100000;
  102239. + fb->fb.monspecs.vfmin = 0;
  102240. + fb->fb.monspecs.vfmax = 400;
  102241. + fb->fb.monspecs.dclkmin = 1000000;
  102242. + fb->fb.monspecs.dclkmax = 100000000;
  102243. +
  102244. + bcm2708_fb_set_bitfields(&fb->fb.var);
  102245. + init_waitqueue_head(&fb->dma_waitq);
  102246. +
  102247. + /*
  102248. + * Allocate colourmap.
  102249. + */
  102250. +
  102251. + fb_set_var(&fb->fb, &fb->fb.var);
  102252. +
  102253. + print_debug("BCM2708FB: registering framebuffer (%dx%d@%d) (%d)\n", fbwidth
  102254. + fbheight, fbdepth, fbswap);
  102255. +
  102256. + ret = register_framebuffer(&fb->fb);
  102257. + print_debug("BCM2708FB: register framebuffer (%d)\n", ret);
  102258. + if (ret == 0)
  102259. + goto out;
  102260. +
  102261. + print_debug("BCM2708FB: cannot register framebuffer (%d)\n", ret);
  102262. +out:
  102263. + return ret;
  102264. +}
  102265. +
  102266. +static int bcm2708_fb_probe(struct platform_device *dev)
  102267. +{
  102268. + struct bcm2708_fb *fb;
  102269. + int ret;
  102270. +
  102271. + fb = kzalloc(sizeof(struct bcm2708_fb), GFP_KERNEL);
  102272. + if (!fb) {
  102273. + dev_err(&dev->dev,
  102274. + "could not allocate new bcm2708_fb struct\n");
  102275. + ret = -ENOMEM;
  102276. + goto free_region;
  102277. + }
  102278. +
  102279. + bcm2708_fb_debugfs_init(fb);
  102280. +
  102281. + fb->cb_base = dma_alloc_writecombine(&dev->dev, SZ_64K,
  102282. + &fb->cb_handle, GFP_KERNEL);
  102283. + if (!fb->cb_base) {
  102284. + dev_err(&dev->dev, "cannot allocate DMA CBs\n");
  102285. + ret = -ENOMEM;
  102286. + goto free_fb;
  102287. + }
  102288. +
  102289. + pr_info("BCM2708FB: allocated DMA memory %08x\n",
  102290. + fb->cb_handle);
  102291. +
  102292. + ret = bcm_dma_chan_alloc(BCM_DMA_FEATURE_BULK,
  102293. + &fb->dma_chan_base, &fb->dma_irq);
  102294. + if (ret < 0) {
  102295. + dev_err(&dev->dev, "couldn't allocate a DMA channel\n");
  102296. + goto free_cb;
  102297. + }
  102298. + fb->dma_chan = ret;
  102299. +
  102300. + ret = request_irq(fb->dma_irq, bcm2708_fb_dma_irq,
  102301. + 0, "bcm2708_fb dma", fb);
  102302. + if (ret) {
  102303. + pr_err("%s: failed to request DMA irq\n", __func__);
  102304. + goto free_dma_chan;
  102305. + }
  102306. +
  102307. +
  102308. + pr_info("BCM2708FB: allocated DMA channel %d @ %p\n",
  102309. + fb->dma_chan, fb->dma_chan_base);
  102310. +
  102311. + fb->dev = dev;
  102312. +
  102313. + ret = bcm2708_fb_register(fb);
  102314. + if (ret == 0) {
  102315. + platform_set_drvdata(dev, fb);
  102316. + goto out;
  102317. + }
  102318. +
  102319. +free_dma_chan:
  102320. + bcm_dma_chan_free(fb->dma_chan);
  102321. +free_cb:
  102322. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  102323. +free_fb:
  102324. + kfree(fb);
  102325. +free_region:
  102326. + dev_err(&dev->dev, "probe failed, err %d\n", ret);
  102327. +out:
  102328. + return ret;
  102329. +}
  102330. +
  102331. +static int bcm2708_fb_remove(struct platform_device *dev)
  102332. +{
  102333. + struct bcm2708_fb *fb = platform_get_drvdata(dev);
  102334. +
  102335. + platform_set_drvdata(dev, NULL);
  102336. +
  102337. + if (fb->fb.screen_base)
  102338. + iounmap(fb->fb.screen_base);
  102339. + unregister_framebuffer(&fb->fb);
  102340. +
  102341. + dma_free_writecombine(&dev->dev, SZ_64K, fb->cb_base, fb->cb_handle);
  102342. + bcm_dma_chan_free(fb->dma_chan);
  102343. +
  102344. + dma_free_coherent(NULL, PAGE_ALIGN(sizeof(*fb->info)), (void *)fb->info,
  102345. + fb->dma);
  102346. + bcm2708_fb_debugfs_deinit(fb);
  102347. +
  102348. + free_irq(fb->dma_irq, fb);
  102349. +
  102350. + kfree(fb);
  102351. +
  102352. + return 0;
  102353. +}
  102354. +
  102355. +static struct platform_driver bcm2708_fb_driver = {
  102356. + .probe = bcm2708_fb_probe,
  102357. + .remove = bcm2708_fb_remove,
  102358. + .driver = {
  102359. + .name = DRIVER_NAME,
  102360. + .owner = THIS_MODULE,
  102361. + },
  102362. +};
  102363. +
  102364. +static int __init bcm2708_fb_init(void)
  102365. +{
  102366. + return platform_driver_register(&bcm2708_fb_driver);
  102367. +}
  102368. +
  102369. +module_init(bcm2708_fb_init);
  102370. +
  102371. +static void __exit bcm2708_fb_exit(void)
  102372. +{
  102373. + platform_driver_unregister(&bcm2708_fb_driver);
  102374. +}
  102375. +
  102376. +module_exit(bcm2708_fb_exit);
  102377. +
  102378. +module_param(fbwidth, int, 0644);
  102379. +module_param(fbheight, int, 0644);
  102380. +module_param(fbdepth, int, 0644);
  102381. +module_param(fbswap, int, 0644);
  102382. +
  102383. +MODULE_DESCRIPTION("BCM2708 framebuffer driver");
  102384. +MODULE_LICENSE("GPL");
  102385. +
  102386. +MODULE_PARM_DESC(fbwidth, "Width of ARM Framebuffer");
  102387. +MODULE_PARM_DESC(fbheight, "Height of ARM Framebuffer");
  102388. +MODULE_PARM_DESC(fbdepth, "Bit depth of ARM Framebuffer");
  102389. +MODULE_PARM_DESC(fbswap, "Swap order of red and blue in 24 and 32 bit modes");
  102390. diff -Nur linux-3.16.2/drivers/video/fbdev/core/cfbimgblt.c linux-3.16-rpi/drivers/video/fbdev/core/cfbimgblt.c
  102391. --- linux-3.16.2/drivers/video/fbdev/core/cfbimgblt.c 2014-09-06 01:37:11.000000000 +0200
  102392. +++ linux-3.16-rpi/drivers/video/fbdev/core/cfbimgblt.c 2014-09-14 19:04:14.000000000 +0200
  102393. @@ -28,6 +28,11 @@
  102394. *
  102395. * Also need to add code to deal with cards endians that are different than
  102396. * the native cpu endians. I also need to deal with MSB position in the word.
  102397. + * Modified by Harm Hanemaaijer (fgenfb@yahoo.com) 2013:
  102398. + * - Provide optimized versions of fast_imageblit for 16 and 32bpp that are
  102399. + * significantly faster than the previous implementation.
  102400. + * - Simplify the fast/slow_imageblit selection code, avoiding integer
  102401. + * divides.
  102402. */
  102403. #include <linux/module.h>
  102404. #include <linux/string.h>
  102405. @@ -262,6 +267,133 @@
  102406. }
  102407. }
  102408. +/*
  102409. + * Optimized fast_imageblit for bpp == 16. ppw = 2, bit_mask = 3 folded
  102410. + * into the code, main loop unrolled.
  102411. + */
  102412. +
  102413. +static inline void fast_imageblit16(const struct fb_image *image,
  102414. + struct fb_info *p, u8 __iomem * dst1,
  102415. + u32 fgcolor, u32 bgcolor)
  102416. +{
  102417. + u32 fgx = fgcolor, bgx = bgcolor;
  102418. + u32 spitch = (image->width + 7) / 8;
  102419. + u32 end_mask, eorx;
  102420. + const char *s = image->data, *src;
  102421. + u32 __iomem *dst;
  102422. + const u32 *tab = NULL;
  102423. + int i, j, k;
  102424. +
  102425. + tab = fb_be_math(p) ? cfb_tab16_be : cfb_tab16_le;
  102426. +
  102427. + fgx <<= 16;
  102428. + bgx <<= 16;
  102429. + fgx |= fgcolor;
  102430. + bgx |= bgcolor;
  102431. +
  102432. + eorx = fgx ^ bgx;
  102433. + k = image->width / 2;
  102434. +
  102435. + for (i = image->height; i--;) {
  102436. + dst = (u32 __iomem *) dst1;
  102437. + src = s;
  102438. +
  102439. + j = k;
  102440. + while (j >= 4) {
  102441. + u8 bits = *src;
  102442. + end_mask = tab[(bits >> 6) & 3];
  102443. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102444. + end_mask = tab[(bits >> 4) & 3];
  102445. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102446. + end_mask = tab[(bits >> 2) & 3];
  102447. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102448. + end_mask = tab[bits & 3];
  102449. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102450. + src++;
  102451. + j -= 4;
  102452. + }
  102453. + if (j != 0) {
  102454. + u8 bits = *src;
  102455. + end_mask = tab[(bits >> 6) & 3];
  102456. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102457. + if (j >= 2) {
  102458. + end_mask = tab[(bits >> 4) & 3];
  102459. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102460. + if (j == 3) {
  102461. + end_mask = tab[(bits >> 2) & 3];
  102462. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  102463. + }
  102464. + }
  102465. + }
  102466. + dst1 += p->fix.line_length;
  102467. + s += spitch;
  102468. + }
  102469. +}
  102470. +
  102471. +/*
  102472. + * Optimized fast_imageblit for bpp == 32. ppw = 1, bit_mask = 1 folded
  102473. + * into the code, main loop unrolled.
  102474. + */
  102475. +
  102476. +static inline void fast_imageblit32(const struct fb_image *image,
  102477. + struct fb_info *p, u8 __iomem * dst1,
  102478. + u32 fgcolor, u32 bgcolor)
  102479. +{
  102480. + u32 fgx = fgcolor, bgx = bgcolor;
  102481. + u32 spitch = (image->width + 7) / 8;
  102482. + u32 end_mask, eorx;
  102483. + const char *s = image->data, *src;
  102484. + u32 __iomem *dst;
  102485. + const u32 *tab = NULL;
  102486. + int i, j, k;
  102487. +
  102488. + tab = cfb_tab32;
  102489. +
  102490. + eorx = fgx ^ bgx;
  102491. + k = image->width;
  102492. +
  102493. + for (i = image->height; i--;) {
  102494. + dst = (u32 __iomem *) dst1;
  102495. + src = s;
  102496. +
  102497. + j = k;
  102498. + while (j >= 8) {
  102499. + u8 bits = *src;
  102500. + end_mask = tab[(bits >> 7) & 1];
  102501. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102502. + end_mask = tab[(bits >> 6) & 1];
  102503. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102504. + end_mask = tab[(bits >> 5) & 1];
  102505. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102506. + end_mask = tab[(bits >> 4) & 1];
  102507. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102508. + end_mask = tab[(bits >> 3) & 1];
  102509. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102510. + end_mask = tab[(bits >> 2) & 1];
  102511. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102512. + end_mask = tab[(bits >> 1) & 1];
  102513. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102514. + end_mask = tab[bits & 1];
  102515. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102516. + src++;
  102517. + j -= 8;
  102518. + }
  102519. + if (j != 0) {
  102520. + u32 bits = (u32) * src;
  102521. + while (j > 1) {
  102522. + end_mask = tab[(bits >> 7) & 1];
  102523. + FB_WRITEL((end_mask & eorx) ^ bgx, dst++);
  102524. + bits <<= 1;
  102525. + j--;
  102526. + }
  102527. + end_mask = tab[(bits >> 7) & 1];
  102528. + FB_WRITEL((end_mask & eorx) ^ bgx, dst);
  102529. + }
  102530. + dst1 += p->fix.line_length;
  102531. + s += spitch;
  102532. + }
  102533. +}
  102534. +
  102535. void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
  102536. {
  102537. u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
  102538. @@ -294,11 +426,21 @@
  102539. bgcolor = image->bg_color;
  102540. }
  102541. - if (32 % bpp == 0 && !start_index && !pitch_index &&
  102542. - ((width & (32/bpp-1)) == 0) &&
  102543. - bpp >= 8 && bpp <= 32)
  102544. - fast_imageblit(image, p, dst1, fgcolor, bgcolor);
  102545. - else
  102546. + if (!start_index && !pitch_index) {
  102547. + if (bpp == 32)
  102548. + fast_imageblit32(image, p, dst1, fgcolor,
  102549. + bgcolor);
  102550. + else if (bpp == 16 && (width & 1) == 0)
  102551. + fast_imageblit16(image, p, dst1, fgcolor,
  102552. + bgcolor);
  102553. + else if (bpp == 8 && (width & 3) == 0)
  102554. + fast_imageblit(image, p, dst1, fgcolor,
  102555. + bgcolor);
  102556. + else
  102557. + slow_imageblit(image, p, dst1, fgcolor,
  102558. + bgcolor,
  102559. + start_index, pitch_index);
  102560. + } else
  102561. slow_imageblit(image, p, dst1, fgcolor, bgcolor,
  102562. start_index, pitch_index);
  102563. } else
  102564. diff -Nur linux-3.16.2/drivers/video/fbdev/core/fbmem.c linux-3.16-rpi/drivers/video/fbdev/core/fbmem.c
  102565. --- linux-3.16.2/drivers/video/fbdev/core/fbmem.c 2014-09-06 01:37:11.000000000 +0200
  102566. +++ linux-3.16-rpi/drivers/video/fbdev/core/fbmem.c 2014-09-14 19:04:14.000000000 +0200
  102567. @@ -1084,6 +1084,25 @@
  102568. }
  102569. EXPORT_SYMBOL(fb_blank);
  102570. +static int fb_copyarea_user(struct fb_info *info,
  102571. + struct fb_copyarea *copy)
  102572. +{
  102573. + int ret = 0;
  102574. + if (!lock_fb_info(info))
  102575. + return -ENODEV;
  102576. + if (copy->dx + copy->width > info->var.xres ||
  102577. + copy->sx + copy->width > info->var.xres ||
  102578. + copy->dy + copy->height > info->var.yres ||
  102579. + copy->sy + copy->height > info->var.yres) {
  102580. + ret = -EINVAL;
  102581. + goto out;
  102582. + }
  102583. + info->fbops->fb_copyarea(info, copy);
  102584. +out:
  102585. + unlock_fb_info(info);
  102586. + return ret;
  102587. +}
  102588. +
  102589. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
  102590. unsigned long arg)
  102591. {
  102592. @@ -1094,6 +1113,7 @@
  102593. struct fb_cmap cmap_from;
  102594. struct fb_cmap_user cmap;
  102595. struct fb_event event;
  102596. + struct fb_copyarea copy;
  102597. void __user *argp = (void __user *)arg;
  102598. long ret = 0;
  102599. @@ -1211,6 +1231,15 @@
  102600. unlock_fb_info(info);
  102601. console_unlock();
  102602. break;
  102603. + case FBIOCOPYAREA:
  102604. + if (info->flags & FBINFO_HWACCEL_COPYAREA) {
  102605. + /* only provide this ioctl if it is accelerated */
  102606. + if (copy_from_user(&copy, argp, sizeof(copy)))
  102607. + return -EFAULT;
  102608. + ret = fb_copyarea_user(info, &copy);
  102609. + break;
  102610. + }
  102611. + /* fall through */
  102612. default:
  102613. if (!lock_fb_info(info))
  102614. return -ENODEV;
  102615. @@ -1365,6 +1394,7 @@
  102616. case FBIOPAN_DISPLAY:
  102617. case FBIOGET_CON2FBMAP:
  102618. case FBIOPUT_CON2FBMAP:
  102619. + case FBIOCOPYAREA:
  102620. arg = (unsigned long) compat_ptr(arg);
  102621. case FBIOBLANK:
  102622. ret = do_fb_ioctl(info, cmd, arg);
  102623. diff -Nur linux-3.16.2/drivers/video/fbdev/Kconfig linux-3.16-rpi/drivers/video/fbdev/Kconfig
  102624. --- linux-3.16.2/drivers/video/fbdev/Kconfig 2014-09-06 01:37:11.000000000 +0200
  102625. +++ linux-3.16-rpi/drivers/video/fbdev/Kconfig 2014-09-14 19:04:14.000000000 +0200
  102626. @@ -220,6 +220,20 @@
  102627. comment "Frame buffer hardware drivers"
  102628. depends on FB
  102629. +config FB_BCM2708
  102630. + tristate "BCM2708 framebuffer support"
  102631. + depends on FB && ARM
  102632. + select FB_CFB_FILLRECT
  102633. + select FB_CFB_COPYAREA
  102634. + select FB_CFB_IMAGEBLIT
  102635. + help
  102636. + This framebuffer device driver is for the BCM2708 framebuffer.
  102637. +
  102638. + If you want to compile this as a module (=code which can be
  102639. + inserted into and removed from the running kernel), say M
  102640. + here and read <file:Documentation/kbuild/modules.txt>. The module
  102641. + will be called bcm2708_fb.
  102642. +
  102643. config FB_GRVGA
  102644. tristate "Aeroflex Gaisler framebuffer support"
  102645. depends on FB && SPARC
  102646. diff -Nur linux-3.16.2/drivers/video/fbdev/Makefile linux-3.16-rpi/drivers/video/fbdev/Makefile
  102647. --- linux-3.16.2/drivers/video/fbdev/Makefile 2014-09-06 01:37:11.000000000 +0200
  102648. +++ linux-3.16-rpi/drivers/video/fbdev/Makefile 2014-09-14 19:04:14.000000000 +0200
  102649. @@ -12,6 +12,7 @@
  102650. obj-$(CONFIG_FB_WMT_GE_ROPS) += wmt_ge_rops.o
  102651. # Hardware specific drivers go first
  102652. +obj-$(CONFIG_FB_BCM2708) += bcm2708_fb.o
  102653. obj-$(CONFIG_FB_AMIGA) += amifb.o c2p_planar.o
  102654. obj-$(CONFIG_FB_ARC) += arcfb.o
  102655. obj-$(CONFIG_FB_CLPS711X) += clps711xfb.o
  102656. diff -Nur linux-3.16.2/drivers/video/logo/logo_linux_clut224.ppm linux-3.16-rpi/drivers/video/logo/logo_linux_clut224.ppm
  102657. --- linux-3.16.2/drivers/video/logo/logo_linux_clut224.ppm 2014-09-06 01:37:11.000000000 +0200
  102658. +++ linux-3.16-rpi/drivers/video/logo/logo_linux_clut224.ppm 2014-04-13 17:33:14.000000000 +0200
  102659. @@ -1,1604 +1,883 @@
  102660. P3
  102661. -# Standard 224-color Linux logo
  102662. -80 80
  102663. +63 80
  102664. 255
  102665. - 0 0 0 0 0 0 0 0 0 0 0 0
  102666. - 0 0 0 0 0 0 0 0 0 0 0 0
  102667. - 0 0 0 0 0 0 0 0 0 0 0 0
  102668. - 0 0 0 0 0 0 0 0 0 0 0 0
  102669. - 0 0 0 0 0 0 0 0 0 0 0 0
  102670. - 0 0 0 0 0 0 0 0 0 0 0 0
  102671. - 0 0 0 0 0 0 0 0 0 0 0 0
  102672. - 0 0 0 0 0 0 0 0 0 0 0 0
  102673. - 0 0 0 0 0 0 0 0 0 0 0 0
  102674. - 6 6 6 6 6 6 10 10 10 10 10 10
  102675. - 10 10 10 6 6 6 6 6 6 6 6 6
  102676. - 0 0 0 0 0 0 0 0 0 0 0 0
  102677. - 0 0 0 0 0 0 0 0 0 0 0 0
  102678. - 0 0 0 0 0 0 0 0 0 0 0 0
  102679. - 0 0 0 0 0 0 0 0 0 0 0 0
  102680. - 0 0 0 0 0 0 0 0 0 0 0 0
  102681. - 0 0 0 0 0 0 0 0 0 0 0 0
  102682. - 0 0 0 0 0 0 0 0 0 0 0 0
  102683. - 0 0 0 0 0 0 0 0 0 0 0 0
  102684. - 0 0 0 0 0 0 0 0 0 0 0 0
  102685. - 0 0 0 0 0 0 0 0 0 0 0 0
  102686. - 0 0 0 0 0 0 0 0 0 0 0 0
  102687. - 0 0 0 0 0 0 0 0 0 0 0 0
  102688. - 0 0 0 0 0 0 0 0 0 0 0 0
  102689. - 0 0 0 0 0 0 0 0 0 0 0 0
  102690. - 0 0 0 0 0 0 0 0 0 0 0 0
  102691. - 0 0 0 0 0 0 0 0 0 0 0 0
  102692. - 0 0 0 0 0 0 0 0 0 0 0 0
  102693. - 0 0 0 6 6 6 10 10 10 14 14 14
  102694. - 22 22 22 26 26 26 30 30 30 34 34 34
  102695. - 30 30 30 30 30 30 26 26 26 18 18 18
  102696. - 14 14 14 10 10 10 6 6 6 0 0 0
  102697. - 0 0 0 0 0 0 0 0 0 0 0 0
  102698. - 0 0 0 0 0 0 0 0 0 0 0 0
  102699. - 0 0 0 0 0 0 0 0 0 0 0 0
  102700. - 0 0 0 0 0 0 0 0 0 0 0 0
  102701. - 0 0 0 0 0 0 0 0 0 0 0 0
  102702. - 0 0 0 0 0 0 0 0 0 0 0 0
  102703. - 0 0 0 0 0 0 0 0 0 0 0 0
  102704. - 0 0 0 0 0 0 0 0 0 0 0 0
  102705. - 0 0 0 0 0 0 0 0 0 0 0 0
  102706. - 0 0 0 0 0 1 0 0 1 0 0 0
  102707. - 0 0 0 0 0 0 0 0 0 0 0 0
  102708. - 0 0 0 0 0 0 0 0 0 0 0 0
  102709. - 0 0 0 0 0 0 0 0 0 0 0 0
  102710. - 0 0 0 0 0 0 0 0 0 0 0 0
  102711. - 0 0 0 0 0 0 0 0 0 0 0 0
  102712. - 0 0 0 0 0 0 0 0 0 0 0 0
  102713. - 6 6 6 14 14 14 26 26 26 42 42 42
  102714. - 54 54 54 66 66 66 78 78 78 78 78 78
  102715. - 78 78 78 74 74 74 66 66 66 54 54 54
  102716. - 42 42 42 26 26 26 18 18 18 10 10 10
  102717. - 6 6 6 0 0 0 0 0 0 0 0 0
  102718. - 0 0 0 0 0 0 0 0 0 0 0 0
  102719. - 0 0 0 0 0 0 0 0 0 0 0 0
  102720. - 0 0 0 0 0 0 0 0 0 0 0 0
  102721. - 0 0 0 0 0 0 0 0 0 0 0 0
  102722. - 0 0 0 0 0 0 0 0 0 0 0 0
  102723. - 0 0 0 0 0 0 0 0 0 0 0 0
  102724. - 0 0 0 0 0 0 0 0 0 0 0 0
  102725. - 0 0 0 0 0 0 0 0 0 0 0 0
  102726. - 0 0 1 0 0 0 0 0 0 0 0 0
  102727. - 0 0 0 0 0 0 0 0 0 0 0 0
  102728. - 0 0 0 0 0 0 0 0 0 0 0 0
  102729. - 0 0 0 0 0 0 0 0 0 0 0 0
  102730. - 0 0 0 0 0 0 0 0 0 0 0 0
  102731. - 0 0 0 0 0 0 0 0 0 0 0 0
  102732. - 0 0 0 0 0 0 0 0 0 10 10 10
  102733. - 22 22 22 42 42 42 66 66 66 86 86 86
  102734. - 66 66 66 38 38 38 38 38 38 22 22 22
  102735. - 26 26 26 34 34 34 54 54 54 66 66 66
  102736. - 86 86 86 70 70 70 46 46 46 26 26 26
  102737. - 14 14 14 6 6 6 0 0 0 0 0 0
  102738. - 0 0 0 0 0 0 0 0 0 0 0 0
  102739. - 0 0 0 0 0 0 0 0 0 0 0 0
  102740. - 0 0 0 0 0 0 0 0 0 0 0 0
  102741. - 0 0 0 0 0 0 0 0 0 0 0 0
  102742. - 0 0 0 0 0 0 0 0 0 0 0 0
  102743. - 0 0 0 0 0 0 0 0 0 0 0 0
  102744. - 0 0 0 0 0 0 0 0 0 0 0 0
  102745. - 0 0 0 0 0 0 0 0 0 0 0 0
  102746. - 0 0 1 0 0 1 0 0 1 0 0 0
  102747. - 0 0 0 0 0 0 0 0 0 0 0 0
  102748. - 0 0 0 0 0 0 0 0 0 0 0 0
  102749. - 0 0 0 0 0 0 0 0 0 0 0 0
  102750. - 0 0 0 0 0 0 0 0 0 0 0 0
  102751. - 0 0 0 0 0 0 0 0 0 0 0 0
  102752. - 0 0 0 0 0 0 10 10 10 26 26 26
  102753. - 50 50 50 82 82 82 58 58 58 6 6 6
  102754. - 2 2 6 2 2 6 2 2 6 2 2 6
  102755. - 2 2 6 2 2 6 2 2 6 2 2 6
  102756. - 6 6 6 54 54 54 86 86 86 66 66 66
  102757. - 38 38 38 18 18 18 6 6 6 0 0 0
  102758. - 0 0 0 0 0 0 0 0 0 0 0 0
  102759. - 0 0 0 0 0 0 0 0 0 0 0 0
  102760. - 0 0 0 0 0 0 0 0 0 0 0 0
  102761. - 0 0 0 0 0 0 0 0 0 0 0 0
  102762. - 0 0 0 0 0 0 0 0 0 0 0 0
  102763. - 0 0 0 0 0 0 0 0 0 0 0 0
  102764. - 0 0 0 0 0 0 0 0 0 0 0 0
  102765. - 0 0 0 0 0 0 0 0 0 0 0 0
  102766. - 0 0 0 0 0 0 0 0 0 0 0 0
  102767. - 0 0 0 0 0 0 0 0 0 0 0 0
  102768. - 0 0 0 0 0 0 0 0 0 0 0 0
  102769. - 0 0 0 0 0 0 0 0 0 0 0 0
  102770. - 0 0 0 0 0 0 0 0 0 0 0 0
  102771. - 0 0 0 0 0 0 0 0 0 0 0 0
  102772. - 0 0 0 6 6 6 22 22 22 50 50 50
  102773. - 78 78 78 34 34 34 2 2 6 2 2 6
  102774. - 2 2 6 2 2 6 2 2 6 2 2 6
  102775. - 2 2 6 2 2 6 2 2 6 2 2 6
  102776. - 2 2 6 2 2 6 6 6 6 70 70 70
  102777. - 78 78 78 46 46 46 22 22 22 6 6 6
  102778. - 0 0 0 0 0 0 0 0 0 0 0 0
  102779. - 0 0 0 0 0 0 0 0 0 0 0 0
  102780. - 0 0 0 0 0 0 0 0 0 0 0 0
  102781. - 0 0 0 0 0 0 0 0 0 0 0 0
  102782. - 0 0 0 0 0 0 0 0 0 0 0 0
  102783. - 0 0 0 0 0 0 0 0 0 0 0 0
  102784. - 0 0 0 0 0 0 0 0 0 0 0 0
  102785. - 0 0 0 0 0 0 0 0 0 0 0 0
  102786. - 0 0 1 0 0 1 0 0 1 0 0 0
  102787. - 0 0 0 0 0 0 0 0 0 0 0 0
  102788. - 0 0 0 0 0 0 0 0 0 0 0 0
  102789. - 0 0 0 0 0 0 0 0 0 0 0 0
  102790. - 0 0 0 0 0 0 0 0 0 0 0 0
  102791. - 0 0 0 0 0 0 0 0 0 0 0 0
  102792. - 6 6 6 18 18 18 42 42 42 82 82 82
  102793. - 26 26 26 2 2 6 2 2 6 2 2 6
  102794. - 2 2 6 2 2 6 2 2 6 2 2 6
  102795. - 2 2 6 2 2 6 2 2 6 14 14 14
  102796. - 46 46 46 34 34 34 6 6 6 2 2 6
  102797. - 42 42 42 78 78 78 42 42 42 18 18 18
  102798. - 6 6 6 0 0 0 0 0 0 0 0 0
  102799. - 0 0 0 0 0 0 0 0 0 0 0 0
  102800. - 0 0 0 0 0 0 0 0 0 0 0 0
  102801. - 0 0 0 0 0 0 0 0 0 0 0 0
  102802. - 0 0 0 0 0 0 0 0 0 0 0 0
  102803. - 0 0 0 0 0 0 0 0 0 0 0 0
  102804. - 0 0 0 0 0 0 0 0 0 0 0 0
  102805. - 0 0 0 0 0 0 0 0 0 0 0 0
  102806. - 0 0 1 0 0 0 0 0 1 0 0 0
  102807. - 0 0 0 0 0 0 0 0 0 0 0 0
  102808. - 0 0 0 0 0 0 0 0 0 0 0 0
  102809. - 0 0 0 0 0 0 0 0 0 0 0 0
  102810. - 0 0 0 0 0 0 0 0 0 0 0 0
  102811. - 0 0 0 0 0 0 0 0 0 0 0 0
  102812. - 10 10 10 30 30 30 66 66 66 58 58 58
  102813. - 2 2 6 2 2 6 2 2 6 2 2 6
  102814. - 2 2 6 2 2 6 2 2 6 2 2 6
  102815. - 2 2 6 2 2 6 2 2 6 26 26 26
  102816. - 86 86 86 101 101 101 46 46 46 10 10 10
  102817. - 2 2 6 58 58 58 70 70 70 34 34 34
  102818. - 10 10 10 0 0 0 0 0 0 0 0 0
  102819. - 0 0 0 0 0 0 0 0 0 0 0 0
  102820. - 0 0 0 0 0 0 0 0 0 0 0 0
  102821. - 0 0 0 0 0 0 0 0 0 0 0 0
  102822. - 0 0 0 0 0 0 0 0 0 0 0 0
  102823. - 0 0 0 0 0 0 0 0 0 0 0 0
  102824. - 0 0 0 0 0 0 0 0 0 0 0 0
  102825. - 0 0 0 0 0 0 0 0 0 0 0 0
  102826. - 0 0 1 0 0 1 0 0 1 0 0 0
  102827. - 0 0 0 0 0 0 0 0 0 0 0 0
  102828. - 0 0 0 0 0 0 0 0 0 0 0 0
  102829. - 0 0 0 0 0 0 0 0 0 0 0 0
  102830. - 0 0 0 0 0 0 0 0 0 0 0 0
  102831. - 0 0 0 0 0 0 0 0 0 0 0 0
  102832. - 14 14 14 42 42 42 86 86 86 10 10 10
  102833. - 2 2 6 2 2 6 2 2 6 2 2 6
  102834. - 2 2 6 2 2 6 2 2 6 2 2 6
  102835. - 2 2 6 2 2 6 2 2 6 30 30 30
  102836. - 94 94 94 94 94 94 58 58 58 26 26 26
  102837. - 2 2 6 6 6 6 78 78 78 54 54 54
  102838. - 22 22 22 6 6 6 0 0 0 0 0 0
  102839. - 0 0 0 0 0 0 0 0 0 0 0 0
  102840. - 0 0 0 0 0 0 0 0 0 0 0 0
  102841. - 0 0 0 0 0 0 0 0 0 0 0 0
  102842. - 0 0 0 0 0 0 0 0 0 0 0 0
  102843. - 0 0 0 0 0 0 0 0 0 0 0 0
  102844. - 0 0 0 0 0 0 0 0 0 0 0 0
  102845. - 0 0 0 0 0 0 0 0 0 0 0 0
  102846. - 0 0 0 0 0 0 0 0 0 0 0 0
  102847. - 0 0 0 0 0 0 0 0 0 0 0 0
  102848. - 0 0 0 0 0 0 0 0 0 0 0 0
  102849. - 0 0 0 0 0 0 0 0 0 0 0 0
  102850. - 0 0 0 0 0 0 0 0 0 0 0 0
  102851. - 0 0 0 0 0 0 0 0 0 6 6 6
  102852. - 22 22 22 62 62 62 62 62 62 2 2 6
  102853. - 2 2 6 2 2 6 2 2 6 2 2 6
  102854. - 2 2 6 2 2 6 2 2 6 2 2 6
  102855. - 2 2 6 2 2 6 2 2 6 26 26 26
  102856. - 54 54 54 38 38 38 18 18 18 10 10 10
  102857. - 2 2 6 2 2 6 34 34 34 82 82 82
  102858. - 38 38 38 14 14 14 0 0 0 0 0 0
  102859. - 0 0 0 0 0 0 0 0 0 0 0 0
  102860. - 0 0 0 0 0 0 0 0 0 0 0 0
  102861. - 0 0 0 0 0 0 0 0 0 0 0 0
  102862. - 0 0 0 0 0 0 0 0 0 0 0 0
  102863. - 0 0 0 0 0 0 0 0 0 0 0 0
  102864. - 0 0 0 0 0 0 0 0 0 0 0 0
  102865. - 0 0 0 0 0 0 0 0 0 0 0 0
  102866. - 0 0 0 0 0 1 0 0 1 0 0 0
  102867. - 0 0 0 0 0 0 0 0 0 0 0 0
  102868. - 0 0 0 0 0 0 0 0 0 0 0 0
  102869. - 0 0 0 0 0 0 0 0 0 0 0 0
  102870. - 0 0 0 0 0 0 0 0 0 0 0 0
  102871. - 0 0 0 0 0 0 0 0 0 6 6 6
  102872. - 30 30 30 78 78 78 30 30 30 2 2 6
  102873. - 2 2 6 2 2 6 2 2 6 2 2 6
  102874. - 2 2 6 2 2 6 2 2 6 2 2 6
  102875. - 2 2 6 2 2 6 2 2 6 10 10 10
  102876. - 10 10 10 2 2 6 2 2 6 2 2 6
  102877. - 2 2 6 2 2 6 2 2 6 78 78 78
  102878. - 50 50 50 18 18 18 6 6 6 0 0 0
  102879. - 0 0 0 0 0 0 0 0 0 0 0 0
  102880. - 0 0 0 0 0 0 0 0 0 0 0 0
  102881. - 0 0 0 0 0 0 0 0 0 0 0 0
  102882. - 0 0 0 0 0 0 0 0 0 0 0 0
  102883. - 0 0 0 0 0 0 0 0 0 0 0 0
  102884. - 0 0 0 0 0 0 0 0 0 0 0 0
  102885. - 0 0 0 0 0 0 0 0 0 0 0 0
  102886. - 0 0 1 0 0 0 0 0 0 0 0 0
  102887. - 0 0 0 0 0 0 0 0 0 0 0 0
  102888. - 0 0 0 0 0 0 0 0 0 0 0 0
  102889. - 0 0 0 0 0 0 0 0 0 0 0 0
  102890. - 0 0 0 0 0 0 0 0 0 0 0 0
  102891. - 0 0 0 0 0 0 0 0 0 10 10 10
  102892. - 38 38 38 86 86 86 14 14 14 2 2 6
  102893. - 2 2 6 2 2 6 2 2 6 2 2 6
  102894. - 2 2 6 2 2 6 2 2 6 2 2 6
  102895. - 2 2 6 2 2 6 2 2 6 2 2 6
  102896. - 2 2 6 2 2 6 2 2 6 2 2 6
  102897. - 2 2 6 2 2 6 2 2 6 54 54 54
  102898. - 66 66 66 26 26 26 6 6 6 0 0 0
  102899. - 0 0 0 0 0 0 0 0 0 0 0 0
  102900. - 0 0 0 0 0 0 0 0 0 0 0 0
  102901. - 0 0 0 0 0 0 0 0 0 0 0 0
  102902. - 0 0 0 0 0 0 0 0 0 0 0 0
  102903. - 0 0 0 0 0 0 0 0 0 0 0 0
  102904. - 0 0 0 0 0 0 0 0 0 0 0 0
  102905. - 0 0 0 0 0 0 0 0 0 0 0 0
  102906. - 0 0 0 0 0 1 0 0 1 0 0 0
  102907. - 0 0 0 0 0 0 0 0 0 0 0 0
  102908. - 0 0 0 0 0 0 0 0 0 0 0 0
  102909. - 0 0 0 0 0 0 0 0 0 0 0 0
  102910. - 0 0 0 0 0 0 0 0 0 0 0 0
  102911. - 0 0 0 0 0 0 0 0 0 14 14 14
  102912. - 42 42 42 82 82 82 2 2 6 2 2 6
  102913. - 2 2 6 6 6 6 10 10 10 2 2 6
  102914. - 2 2 6 2 2 6 2 2 6 2 2 6
  102915. - 2 2 6 2 2 6 2 2 6 6 6 6
  102916. - 14 14 14 10 10 10 2 2 6 2 2 6
  102917. - 2 2 6 2 2 6 2 2 6 18 18 18
  102918. - 82 82 82 34 34 34 10 10 10 0 0 0
  102919. - 0 0 0 0 0 0 0 0 0 0 0 0
  102920. - 0 0 0 0 0 0 0 0 0 0 0 0
  102921. - 0 0 0 0 0 0 0 0 0 0 0 0
  102922. - 0 0 0 0 0 0 0 0 0 0 0 0
  102923. - 0 0 0 0 0 0 0 0 0 0 0 0
  102924. - 0 0 0 0 0 0 0 0 0 0 0 0
  102925. - 0 0 0 0 0 0 0 0 0 0 0 0
  102926. - 0 0 1 0 0 0 0 0 0 0 0 0
  102927. - 0 0 0 0 0 0 0 0 0 0 0 0
  102928. - 0 0 0 0 0 0 0 0 0 0 0 0
  102929. - 0 0 0 0 0 0 0 0 0 0 0 0
  102930. - 0 0 0 0 0 0 0 0 0 0 0 0
  102931. - 0 0 0 0 0 0 0 0 0 14 14 14
  102932. - 46 46 46 86 86 86 2 2 6 2 2 6
  102933. - 6 6 6 6 6 6 22 22 22 34 34 34
  102934. - 6 6 6 2 2 6 2 2 6 2 2 6
  102935. - 2 2 6 2 2 6 18 18 18 34 34 34
  102936. - 10 10 10 50 50 50 22 22 22 2 2 6
  102937. - 2 2 6 2 2 6 2 2 6 10 10 10
  102938. - 86 86 86 42 42 42 14 14 14 0 0 0
  102939. - 0 0 0 0 0 0 0 0 0 0 0 0
  102940. - 0 0 0 0 0 0 0 0 0 0 0 0
  102941. - 0 0 0 0 0 0 0 0 0 0 0 0
  102942. - 0 0 0 0 0 0 0 0 0 0 0 0
  102943. - 0 0 0 0 0 0 0 0 0 0 0 0
  102944. - 0 0 0 0 0 0 0 0 0 0 0 0
  102945. - 0 0 0 0 0 0 0 0 0 0 0 0
  102946. - 0 0 1 0 0 1 0 0 1 0 0 0
  102947. - 0 0 0 0 0 0 0 0 0 0 0 0
  102948. - 0 0 0 0 0 0 0 0 0 0 0 0
  102949. - 0 0 0 0 0 0 0 0 0 0 0 0
  102950. - 0 0 0 0 0 0 0 0 0 0 0 0
  102951. - 0 0 0 0 0 0 0 0 0 14 14 14
  102952. - 46 46 46 86 86 86 2 2 6 2 2 6
  102953. - 38 38 38 116 116 116 94 94 94 22 22 22
  102954. - 22 22 22 2 2 6 2 2 6 2 2 6
  102955. - 14 14 14 86 86 86 138 138 138 162 162 162
  102956. -154 154 154 38 38 38 26 26 26 6 6 6
  102957. - 2 2 6 2 2 6 2 2 6 2 2 6
  102958. - 86 86 86 46 46 46 14 14 14 0 0 0
  102959. - 0 0 0 0 0 0 0 0 0 0 0 0
  102960. - 0 0 0 0 0 0 0 0 0 0 0 0
  102961. - 0 0 0 0 0 0 0 0 0 0 0 0
  102962. - 0 0 0 0 0 0 0 0 0 0 0 0
  102963. - 0 0 0 0 0 0 0 0 0 0 0 0
  102964. - 0 0 0 0 0 0 0 0 0 0 0 0
  102965. - 0 0 0 0 0 0 0 0 0 0 0 0
  102966. - 0 0 0 0 0 0 0 0 0 0 0 0
  102967. - 0 0 0 0 0 0 0 0 0 0 0 0
  102968. - 0 0 0 0 0 0 0 0 0 0 0 0
  102969. - 0 0 0 0 0 0 0 0 0 0 0 0
  102970. - 0 0 0 0 0 0 0 0 0 0 0 0
  102971. - 0 0 0 0 0 0 0 0 0 14 14 14
  102972. - 46 46 46 86 86 86 2 2 6 14 14 14
  102973. -134 134 134 198 198 198 195 195 195 116 116 116
  102974. - 10 10 10 2 2 6 2 2 6 6 6 6
  102975. -101 98 89 187 187 187 210 210 210 218 218 218
  102976. -214 214 214 134 134 134 14 14 14 6 6 6
  102977. - 2 2 6 2 2 6 2 2 6 2 2 6
  102978. - 86 86 86 50 50 50 18 18 18 6 6 6
  102979. - 0 0 0 0 0 0 0 0 0 0 0 0
  102980. - 0 0 0 0 0 0 0 0 0 0 0 0
  102981. - 0 0 0 0 0 0 0 0 0 0 0 0
  102982. - 0 0 0 0 0 0 0 0 0 0 0 0
  102983. - 0 0 0 0 0 0 0 0 0 0 0 0
  102984. - 0 0 0 0 0 0 0 0 0 0 0 0
  102985. - 0 0 0 0 0 0 0 0 1 0 0 0
  102986. - 0 0 1 0 0 1 0 0 1 0 0 0
  102987. - 0 0 0 0 0 0 0 0 0 0 0 0
  102988. - 0 0 0 0 0 0 0 0 0 0 0 0
  102989. - 0 0 0 0 0 0 0 0 0 0 0 0
  102990. - 0 0 0 0 0 0 0 0 0 0 0 0
  102991. - 0 0 0 0 0 0 0 0 0 14 14 14
  102992. - 46 46 46 86 86 86 2 2 6 54 54 54
  102993. -218 218 218 195 195 195 226 226 226 246 246 246
  102994. - 58 58 58 2 2 6 2 2 6 30 30 30
  102995. -210 210 210 253 253 253 174 174 174 123 123 123
  102996. -221 221 221 234 234 234 74 74 74 2 2 6
  102997. - 2 2 6 2 2 6 2 2 6 2 2 6
  102998. - 70 70 70 58 58 58 22 22 22 6 6 6
  102999. - 0 0 0 0 0 0 0 0 0 0 0 0
  103000. - 0 0 0 0 0 0 0 0 0 0 0 0
  103001. - 0 0 0 0 0 0 0 0 0 0 0 0
  103002. - 0 0 0 0 0 0 0 0 0 0 0 0
  103003. - 0 0 0 0 0 0 0 0 0 0 0 0
  103004. - 0 0 0 0 0 0 0 0 0 0 0 0
  103005. - 0 0 0 0 0 0 0 0 0 0 0 0
  103006. - 0 0 0 0 0 0 0 0 0 0 0 0
  103007. - 0 0 0 0 0 0 0 0 0 0 0 0
  103008. - 0 0 0 0 0 0 0 0 0 0 0 0
  103009. - 0 0 0 0 0 0 0 0 0 0 0 0
  103010. - 0 0 0 0 0 0 0 0 0 0 0 0
  103011. - 0 0 0 0 0 0 0 0 0 14 14 14
  103012. - 46 46 46 82 82 82 2 2 6 106 106 106
  103013. -170 170 170 26 26 26 86 86 86 226 226 226
  103014. -123 123 123 10 10 10 14 14 14 46 46 46
  103015. -231 231 231 190 190 190 6 6 6 70 70 70
  103016. - 90 90 90 238 238 238 158 158 158 2 2 6
  103017. - 2 2 6 2 2 6 2 2 6 2 2 6
  103018. - 70 70 70 58 58 58 22 22 22 6 6 6
  103019. - 0 0 0 0 0 0 0 0 0 0 0 0
  103020. - 0 0 0 0 0 0 0 0 0 0 0 0
  103021. - 0 0 0 0 0 0 0 0 0 0 0 0
  103022. - 0 0 0 0 0 0 0 0 0 0 0 0
  103023. - 0 0 0 0 0 0 0 0 0 0 0 0
  103024. - 0 0 0 0 0 0 0 0 0 0 0 0
  103025. - 0 0 0 0 0 0 0 0 1 0 0 0
  103026. - 0 0 1 0 0 1 0 0 1 0 0 0
  103027. - 0 0 0 0 0 0 0 0 0 0 0 0
  103028. - 0 0 0 0 0 0 0 0 0 0 0 0
  103029. - 0 0 0 0 0 0 0 0 0 0 0 0
  103030. - 0 0 0 0 0 0 0 0 0 0 0 0
  103031. - 0 0 0 0 0 0 0 0 0 14 14 14
  103032. - 42 42 42 86 86 86 6 6 6 116 116 116
  103033. -106 106 106 6 6 6 70 70 70 149 149 149
  103034. -128 128 128 18 18 18 38 38 38 54 54 54
  103035. -221 221 221 106 106 106 2 2 6 14 14 14
  103036. - 46 46 46 190 190 190 198 198 198 2 2 6
  103037. - 2 2 6 2 2 6 2 2 6 2 2 6
  103038. - 74 74 74 62 62 62 22 22 22 6 6 6
  103039. - 0 0 0 0 0 0 0 0 0 0 0 0
  103040. - 0 0 0 0 0 0 0 0 0 0 0 0
  103041. - 0 0 0 0 0 0 0 0 0 0 0 0
  103042. - 0 0 0 0 0 0 0 0 0 0 0 0
  103043. - 0 0 0 0 0 0 0 0 0 0 0 0
  103044. - 0 0 0 0 0 0 0 0 0 0 0 0
  103045. - 0 0 0 0 0 0 0 0 1 0 0 0
  103046. - 0 0 1 0 0 0 0 0 1 0 0 0
  103047. - 0 0 0 0 0 0 0 0 0 0 0 0
  103048. - 0 0 0 0 0 0 0 0 0 0 0 0
  103049. - 0 0 0 0 0 0 0 0 0 0 0 0
  103050. - 0 0 0 0 0 0 0 0 0 0 0 0
  103051. - 0 0 0 0 0 0 0 0 0 14 14 14
  103052. - 42 42 42 94 94 94 14 14 14 101 101 101
  103053. -128 128 128 2 2 6 18 18 18 116 116 116
  103054. -118 98 46 121 92 8 121 92 8 98 78 10
  103055. -162 162 162 106 106 106 2 2 6 2 2 6
  103056. - 2 2 6 195 195 195 195 195 195 6 6 6
  103057. - 2 2 6 2 2 6 2 2 6 2 2 6
  103058. - 74 74 74 62 62 62 22 22 22 6 6 6
  103059. - 0 0 0 0 0 0 0 0 0 0 0 0
  103060. - 0 0 0 0 0 0 0 0 0 0 0 0
  103061. - 0 0 0 0 0 0 0 0 0 0 0 0
  103062. - 0 0 0 0 0 0 0 0 0 0 0 0
  103063. - 0 0 0 0 0 0 0 0 0 0 0 0
  103064. - 0 0 0 0 0 0 0 0 0 0 0 0
  103065. - 0 0 0 0 0 0 0 0 1 0 0 1
  103066. - 0 0 1 0 0 0 0 0 1 0 0 0
  103067. - 0 0 0 0 0 0 0 0 0 0 0 0
  103068. - 0 0 0 0 0 0 0 0 0 0 0 0
  103069. - 0 0 0 0 0 0 0 0 0 0 0 0
  103070. - 0 0 0 0 0 0 0 0 0 0 0 0
  103071. - 0 0 0 0 0 0 0 0 0 10 10 10
  103072. - 38 38 38 90 90 90 14 14 14 58 58 58
  103073. -210 210 210 26 26 26 54 38 6 154 114 10
  103074. -226 170 11 236 186 11 225 175 15 184 144 12
  103075. -215 174 15 175 146 61 37 26 9 2 2 6
  103076. - 70 70 70 246 246 246 138 138 138 2 2 6
  103077. - 2 2 6 2 2 6 2 2 6 2 2 6
  103078. - 70 70 70 66 66 66 26 26 26 6 6 6
  103079. - 0 0 0 0 0 0 0 0 0 0 0 0
  103080. - 0 0 0 0 0 0 0 0 0 0 0 0
  103081. - 0 0 0 0 0 0 0 0 0 0 0 0
  103082. - 0 0 0 0 0 0 0 0 0 0 0 0
  103083. - 0 0 0 0 0 0 0 0 0 0 0 0
  103084. - 0 0 0 0 0 0 0 0 0 0 0 0
  103085. - 0 0 0 0 0 0 0 0 0 0 0 0
  103086. - 0 0 0 0 0 0 0 0 0 0 0 0
  103087. - 0 0 0 0 0 0 0 0 0 0 0 0
  103088. - 0 0 0 0 0 0 0 0 0 0 0 0
  103089. - 0 0 0 0 0 0 0 0 0 0 0 0
  103090. - 0 0 0 0 0 0 0 0 0 0 0 0
  103091. - 0 0 0 0 0 0 0 0 0 10 10 10
  103092. - 38 38 38 86 86 86 14 14 14 10 10 10
  103093. -195 195 195 188 164 115 192 133 9 225 175 15
  103094. -239 182 13 234 190 10 232 195 16 232 200 30
  103095. -245 207 45 241 208 19 232 195 16 184 144 12
  103096. -218 194 134 211 206 186 42 42 42 2 2 6
  103097. - 2 2 6 2 2 6 2 2 6 2 2 6
  103098. - 50 50 50 74 74 74 30 30 30 6 6 6
  103099. - 0 0 0 0 0 0 0 0 0 0 0 0
  103100. - 0 0 0 0 0 0 0 0 0 0 0 0
  103101. - 0 0 0 0 0 0 0 0 0 0 0 0
  103102. - 0 0 0 0 0 0 0 0 0 0 0 0
  103103. - 0 0 0 0 0 0 0 0 0 0 0 0
  103104. - 0 0 0 0 0 0 0 0 0 0 0 0
  103105. - 0 0 0 0 0 0 0 0 0 0 0 0
  103106. - 0 0 0 0 0 0 0 0 0 0 0 0
  103107. - 0 0 0 0 0 0 0 0 0 0 0 0
  103108. - 0 0 0 0 0 0 0 0 0 0 0 0
  103109. - 0 0 0 0 0 0 0 0 0 0 0 0
  103110. - 0 0 0 0 0 0 0 0 0 0 0 0
  103111. - 0 0 0 0 0 0 0 0 0 10 10 10
  103112. - 34 34 34 86 86 86 14 14 14 2 2 6
  103113. -121 87 25 192 133 9 219 162 10 239 182 13
  103114. -236 186 11 232 195 16 241 208 19 244 214 54
  103115. -246 218 60 246 218 38 246 215 20 241 208 19
  103116. -241 208 19 226 184 13 121 87 25 2 2 6
  103117. - 2 2 6 2 2 6 2 2 6 2 2 6
  103118. - 50 50 50 82 82 82 34 34 34 10 10 10
  103119. - 0 0 0 0 0 0 0 0 0 0 0 0
  103120. - 0 0 0 0 0 0 0 0 0 0 0 0
  103121. - 0 0 0 0 0 0 0 0 0 0 0 0
  103122. - 0 0 0 0 0 0 0 0 0 0 0 0
  103123. - 0 0 0 0 0 0 0 0 0 0 0 0
  103124. - 0 0 0 0 0 0 0 0 0 0 0 0
  103125. - 0 0 0 0 0 0 0 0 0 0 0 0
  103126. - 0 0 0 0 0 0 0 0 0 0 0 0
  103127. - 0 0 0 0 0 0 0 0 0 0 0 0
  103128. - 0 0 0 0 0 0 0 0 0 0 0 0
  103129. - 0 0 0 0 0 0 0 0 0 0 0 0
  103130. - 0 0 0 0 0 0 0 0 0 0 0 0
  103131. - 0 0 0 0 0 0 0 0 0 10 10 10
  103132. - 34 34 34 82 82 82 30 30 30 61 42 6
  103133. -180 123 7 206 145 10 230 174 11 239 182 13
  103134. -234 190 10 238 202 15 241 208 19 246 218 74
  103135. -246 218 38 246 215 20 246 215 20 246 215 20
  103136. -226 184 13 215 174 15 184 144 12 6 6 6
  103137. - 2 2 6 2 2 6 2 2 6 2 2 6
  103138. - 26 26 26 94 94 94 42 42 42 14 14 14
  103139. - 0 0 0 0 0 0 0 0 0 0 0 0
  103140. - 0 0 0 0 0 0 0 0 0 0 0 0
  103141. - 0 0 0 0 0 0 0 0 0 0 0 0
  103142. - 0 0 0 0 0 0 0 0 0 0 0 0
  103143. - 0 0 0 0 0 0 0 0 0 0 0 0
  103144. - 0 0 0 0 0 0 0 0 0 0 0 0
  103145. - 0 0 0 0 0 0 0 0 0 0 0 0
  103146. - 0 0 0 0 0 0 0 0 0 0 0 0
  103147. - 0 0 0 0 0 0 0 0 0 0 0 0
  103148. - 0 0 0 0 0 0 0 0 0 0 0 0
  103149. - 0 0 0 0 0 0 0 0 0 0 0 0
  103150. - 0 0 0 0 0 0 0 0 0 0 0 0
  103151. - 0 0 0 0 0 0 0 0 0 10 10 10
  103152. - 30 30 30 78 78 78 50 50 50 104 69 6
  103153. -192 133 9 216 158 10 236 178 12 236 186 11
  103154. -232 195 16 241 208 19 244 214 54 245 215 43
  103155. -246 215 20 246 215 20 241 208 19 198 155 10
  103156. -200 144 11 216 158 10 156 118 10 2 2 6
  103157. - 2 2 6 2 2 6 2 2 6 2 2 6
  103158. - 6 6 6 90 90 90 54 54 54 18 18 18
  103159. - 6 6 6 0 0 0 0 0 0 0 0 0
  103160. - 0 0 0 0 0 0 0 0 0 0 0 0
  103161. - 0 0 0 0 0 0 0 0 0 0 0 0
  103162. - 0 0 0 0 0 0 0 0 0 0 0 0
  103163. - 0 0 0 0 0 0 0 0 0 0 0 0
  103164. - 0 0 0 0 0 0 0 0 0 0 0 0
  103165. - 0 0 0 0 0 0 0 0 0 0 0 0
  103166. - 0 0 0 0 0 0 0 0 0 0 0 0
  103167. - 0 0 0 0 0 0 0 0 0 0 0 0
  103168. - 0 0 0 0 0 0 0 0 0 0 0 0
  103169. - 0 0 0 0 0 0 0 0 0 0 0 0
  103170. - 0 0 0 0 0 0 0 0 0 0 0 0
  103171. - 0 0 0 0 0 0 0 0 0 10 10 10
  103172. - 30 30 30 78 78 78 46 46 46 22 22 22
  103173. -137 92 6 210 162 10 239 182 13 238 190 10
  103174. -238 202 15 241 208 19 246 215 20 246 215 20
  103175. -241 208 19 203 166 17 185 133 11 210 150 10
  103176. -216 158 10 210 150 10 102 78 10 2 2 6
  103177. - 6 6 6 54 54 54 14 14 14 2 2 6
  103178. - 2 2 6 62 62 62 74 74 74 30 30 30
  103179. - 10 10 10 0 0 0 0 0 0 0 0 0
  103180. - 0 0 0 0 0 0 0 0 0 0 0 0
  103181. - 0 0 0 0 0 0 0 0 0 0 0 0
  103182. - 0 0 0 0 0 0 0 0 0 0 0 0
  103183. - 0 0 0 0 0 0 0 0 0 0 0 0
  103184. - 0 0 0 0 0 0 0 0 0 0 0 0
  103185. - 0 0 0 0 0 0 0 0 0 0 0 0
  103186. - 0 0 0 0 0 0 0 0 0 0 0 0
  103187. - 0 0 0 0 0 0 0 0 0 0 0 0
  103188. - 0 0 0 0 0 0 0 0 0 0 0 0
  103189. - 0 0 0 0 0 0 0 0 0 0 0 0
  103190. - 0 0 0 0 0 0 0 0 0 0 0 0
  103191. - 0 0 0 0 0 0 0 0 0 10 10 10
  103192. - 34 34 34 78 78 78 50 50 50 6 6 6
  103193. - 94 70 30 139 102 15 190 146 13 226 184 13
  103194. -232 200 30 232 195 16 215 174 15 190 146 13
  103195. -168 122 10 192 133 9 210 150 10 213 154 11
  103196. -202 150 34 182 157 106 101 98 89 2 2 6
  103197. - 2 2 6 78 78 78 116 116 116 58 58 58
  103198. - 2 2 6 22 22 22 90 90 90 46 46 46
  103199. - 18 18 18 6 6 6 0 0 0 0 0 0
  103200. - 0 0 0 0 0 0 0 0 0 0 0 0
  103201. - 0 0 0 0 0 0 0 0 0 0 0 0
  103202. - 0 0 0 0 0 0 0 0 0 0 0 0
  103203. - 0 0 0 0 0 0 0 0 0 0 0 0
  103204. - 0 0 0 0 0 0 0 0 0 0 0 0
  103205. - 0 0 0 0 0 0 0 0 0 0 0 0
  103206. - 0 0 0 0 0 0 0 0 0 0 0 0
  103207. - 0 0 0 0 0 0 0 0 0 0 0 0
  103208. - 0 0 0 0 0 0 0 0 0 0 0 0
  103209. - 0 0 0 0 0 0 0 0 0 0 0 0
  103210. - 0 0 0 0 0 0 0 0 0 0 0 0
  103211. - 0 0 0 0 0 0 0 0 0 10 10 10
  103212. - 38 38 38 86 86 86 50 50 50 6 6 6
  103213. -128 128 128 174 154 114 156 107 11 168 122 10
  103214. -198 155 10 184 144 12 197 138 11 200 144 11
  103215. -206 145 10 206 145 10 197 138 11 188 164 115
  103216. -195 195 195 198 198 198 174 174 174 14 14 14
  103217. - 2 2 6 22 22 22 116 116 116 116 116 116
  103218. - 22 22 22 2 2 6 74 74 74 70 70 70
  103219. - 30 30 30 10 10 10 0 0 0 0 0 0
  103220. - 0 0 0 0 0 0 0 0 0 0 0 0
  103221. - 0 0 0 0 0 0 0 0 0 0 0 0
  103222. - 0 0 0 0 0 0 0 0 0 0 0 0
  103223. - 0 0 0 0 0 0 0 0 0 0 0 0
  103224. - 0 0 0 0 0 0 0 0 0 0 0 0
  103225. - 0 0 0 0 0 0 0 0 0 0 0 0
  103226. - 0 0 0 0 0 0 0 0 0 0 0 0
  103227. - 0 0 0 0 0 0 0 0 0 0 0 0
  103228. - 0 0 0 0 0 0 0 0 0 0 0 0
  103229. - 0 0 0 0 0 0 0 0 0 0 0 0
  103230. - 0 0 0 0 0 0 0 0 0 0 0 0
  103231. - 0 0 0 0 0 0 6 6 6 18 18 18
  103232. - 50 50 50 101 101 101 26 26 26 10 10 10
  103233. -138 138 138 190 190 190 174 154 114 156 107 11
  103234. -197 138 11 200 144 11 197 138 11 192 133 9
  103235. -180 123 7 190 142 34 190 178 144 187 187 187
  103236. -202 202 202 221 221 221 214 214 214 66 66 66
  103237. - 2 2 6 2 2 6 50 50 50 62 62 62
  103238. - 6 6 6 2 2 6 10 10 10 90 90 90
  103239. - 50 50 50 18 18 18 6 6 6 0 0 0
  103240. - 0 0 0 0 0 0 0 0 0 0 0 0
  103241. - 0 0 0 0 0 0 0 0 0 0 0 0
  103242. - 0 0 0 0 0 0 0 0 0 0 0 0
  103243. - 0 0 0 0 0 0 0 0 0 0 0 0
  103244. - 0 0 0 0 0 0 0 0 0 0 0 0
  103245. - 0 0 0 0 0 0 0 0 0 0 0 0
  103246. - 0 0 0 0 0 0 0 0 0 0 0 0
  103247. - 0 0 0 0 0 0 0 0 0 0 0 0
  103248. - 0 0 0 0 0 0 0 0 0 0 0 0
  103249. - 0 0 0 0 0 0 0 0 0 0 0 0
  103250. - 0 0 0 0 0 0 0 0 0 0 0 0
  103251. - 0 0 0 0 0 0 10 10 10 34 34 34
  103252. - 74 74 74 74 74 74 2 2 6 6 6 6
  103253. -144 144 144 198 198 198 190 190 190 178 166 146
  103254. -154 121 60 156 107 11 156 107 11 168 124 44
  103255. -174 154 114 187 187 187 190 190 190 210 210 210
  103256. -246 246 246 253 253 253 253 253 253 182 182 182
  103257. - 6 6 6 2 2 6 2 2 6 2 2 6
  103258. - 2 2 6 2 2 6 2 2 6 62 62 62
  103259. - 74 74 74 34 34 34 14 14 14 0 0 0
  103260. - 0 0 0 0 0 0 0 0 0 0 0 0
  103261. - 0 0 0 0 0 0 0 0 0 0 0 0
  103262. - 0 0 0 0 0 0 0 0 0 0 0 0
  103263. - 0 0 0 0 0 0 0 0 0 0 0 0
  103264. - 0 0 0 0 0 0 0 0 0 0 0 0
  103265. - 0 0 0 0 0 0 0 0 0 0 0 0
  103266. - 0 0 0 0 0 0 0 0 0 0 0 0
  103267. - 0 0 0 0 0 0 0 0 0 0 0 0
  103268. - 0 0 0 0 0 0 0 0 0 0 0 0
  103269. - 0 0 0 0 0 0 0 0 0 0 0 0
  103270. - 0 0 0 0 0 0 0 0 0 0 0 0
  103271. - 0 0 0 10 10 10 22 22 22 54 54 54
  103272. - 94 94 94 18 18 18 2 2 6 46 46 46
  103273. -234 234 234 221 221 221 190 190 190 190 190 190
  103274. -190 190 190 187 187 187 187 187 187 190 190 190
  103275. -190 190 190 195 195 195 214 214 214 242 242 242
  103276. -253 253 253 253 253 253 253 253 253 253 253 253
  103277. - 82 82 82 2 2 6 2 2 6 2 2 6
  103278. - 2 2 6 2 2 6 2 2 6 14 14 14
  103279. - 86 86 86 54 54 54 22 22 22 6 6 6
  103280. - 0 0 0 0 0 0 0 0 0 0 0 0
  103281. - 0 0 0 0 0 0 0 0 0 0 0 0
  103282. - 0 0 0 0 0 0 0 0 0 0 0 0
  103283. - 0 0 0 0 0 0 0 0 0 0 0 0
  103284. - 0 0 0 0 0 0 0 0 0 0 0 0
  103285. - 0 0 0 0 0 0 0 0 0 0 0 0
  103286. - 0 0 0 0 0 0 0 0 0 0 0 0
  103287. - 0 0 0 0 0 0 0 0 0 0 0 0
  103288. - 0 0 0 0 0 0 0 0 0 0 0 0
  103289. - 0 0 0 0 0 0 0 0 0 0 0 0
  103290. - 0 0 0 0 0 0 0 0 0 0 0 0
  103291. - 6 6 6 18 18 18 46 46 46 90 90 90
  103292. - 46 46 46 18 18 18 6 6 6 182 182 182
  103293. -253 253 253 246 246 246 206 206 206 190 190 190
  103294. -190 190 190 190 190 190 190 190 190 190 190 190
  103295. -206 206 206 231 231 231 250 250 250 253 253 253
  103296. -253 253 253 253 253 253 253 253 253 253 253 253
  103297. -202 202 202 14 14 14 2 2 6 2 2 6
  103298. - 2 2 6 2 2 6 2 2 6 2 2 6
  103299. - 42 42 42 86 86 86 42 42 42 18 18 18
  103300. - 6 6 6 0 0 0 0 0 0 0 0 0
  103301. - 0 0 0 0 0 0 0 0 0 0 0 0
  103302. - 0 0 0 0 0 0 0 0 0 0 0 0
  103303. - 0 0 0 0 0 0 0 0 0 0 0 0
  103304. - 0 0 0 0 0 0 0 0 0 0 0 0
  103305. - 0 0 0 0 0 0 0 0 0 0 0 0
  103306. - 0 0 0 0 0 0 0 0 0 0 0 0
  103307. - 0 0 0 0 0 0 0 0 0 0 0 0
  103308. - 0 0 0 0 0 0 0 0 0 0 0 0
  103309. - 0 0 0 0 0 0 0 0 0 0 0 0
  103310. - 0 0 0 0 0 0 0 0 0 6 6 6
  103311. - 14 14 14 38 38 38 74 74 74 66 66 66
  103312. - 2 2 6 6 6 6 90 90 90 250 250 250
  103313. -253 253 253 253 253 253 238 238 238 198 198 198
  103314. -190 190 190 190 190 190 195 195 195 221 221 221
  103315. -246 246 246 253 253 253 253 253 253 253 253 253
  103316. -253 253 253 253 253 253 253 253 253 253 253 253
  103317. -253 253 253 82 82 82 2 2 6 2 2 6
  103318. - 2 2 6 2 2 6 2 2 6 2 2 6
  103319. - 2 2 6 78 78 78 70 70 70 34 34 34
  103320. - 14 14 14 6 6 6 0 0 0 0 0 0
  103321. - 0 0 0 0 0 0 0 0 0 0 0 0
  103322. - 0 0 0 0 0 0 0 0 0 0 0 0
  103323. - 0 0 0 0 0 0 0 0 0 0 0 0
  103324. - 0 0 0 0 0 0 0 0 0 0 0 0
  103325. - 0 0 0 0 0 0 0 0 0 0 0 0
  103326. - 0 0 0 0 0 0 0 0 0 0 0 0
  103327. - 0 0 0 0 0 0 0 0 0 0 0 0
  103328. - 0 0 0 0 0 0 0 0 0 0 0 0
  103329. - 0 0 0 0 0 0 0 0 0 0 0 0
  103330. - 0 0 0 0 0 0 0 0 0 14 14 14
  103331. - 34 34 34 66 66 66 78 78 78 6 6 6
  103332. - 2 2 6 18 18 18 218 218 218 253 253 253
  103333. -253 253 253 253 253 253 253 253 253 246 246 246
  103334. -226 226 226 231 231 231 246 246 246 253 253 253
  103335. -253 253 253 253 253 253 253 253 253 253 253 253
  103336. -253 253 253 253 253 253 253 253 253 253 253 253
  103337. -253 253 253 178 178 178 2 2 6 2 2 6
  103338. - 2 2 6 2 2 6 2 2 6 2 2 6
  103339. - 2 2 6 18 18 18 90 90 90 62 62 62
  103340. - 30 30 30 10 10 10 0 0 0 0 0 0
  103341. - 0 0 0 0 0 0 0 0 0 0 0 0
  103342. - 0 0 0 0 0 0 0 0 0 0 0 0
  103343. - 0 0 0 0 0 0 0 0 0 0 0 0
  103344. - 0 0 0 0 0 0 0 0 0 0 0 0
  103345. - 0 0 0 0 0 0 0 0 0 0 0 0
  103346. - 0 0 0 0 0 0 0 0 0 0 0 0
  103347. - 0 0 0 0 0 0 0 0 0 0 0 0
  103348. - 0 0 0 0 0 0 0 0 0 0 0 0
  103349. - 0 0 0 0 0 0 0 0 0 0 0 0
  103350. - 0 0 0 0 0 0 10 10 10 26 26 26
  103351. - 58 58 58 90 90 90 18 18 18 2 2 6
  103352. - 2 2 6 110 110 110 253 253 253 253 253 253
  103353. -253 253 253 253 253 253 253 253 253 253 253 253
  103354. -250 250 250 253 253 253 253 253 253 253 253 253
  103355. -253 253 253 253 253 253 253 253 253 253 253 253
  103356. -253 253 253 253 253 253 253 253 253 253 253 253
  103357. -253 253 253 231 231 231 18 18 18 2 2 6
  103358. - 2 2 6 2 2 6 2 2 6 2 2 6
  103359. - 2 2 6 2 2 6 18 18 18 94 94 94
  103360. - 54 54 54 26 26 26 10 10 10 0 0 0
  103361. - 0 0 0 0 0 0 0 0 0 0 0 0
  103362. - 0 0 0 0 0 0 0 0 0 0 0 0
  103363. - 0 0 0 0 0 0 0 0 0 0 0 0
  103364. - 0 0 0 0 0 0 0 0 0 0 0 0
  103365. - 0 0 0 0 0 0 0 0 0 0 0 0
  103366. - 0 0 0 0 0 0 0 0 0 0 0 0
  103367. - 0 0 0 0 0 0 0 0 0 0 0 0
  103368. - 0 0 0 0 0 0 0 0 0 0 0 0
  103369. - 0 0 0 0 0 0 0 0 0 0 0 0
  103370. - 0 0 0 6 6 6 22 22 22 50 50 50
  103371. - 90 90 90 26 26 26 2 2 6 2 2 6
  103372. - 14 14 14 195 195 195 250 250 250 253 253 253
  103373. -253 253 253 253 253 253 253 253 253 253 253 253
  103374. -253 253 253 253 253 253 253 253 253 253 253 253
  103375. -253 253 253 253 253 253 253 253 253 253 253 253
  103376. -253 253 253 253 253 253 253 253 253 253 253 253
  103377. -250 250 250 242 242 242 54 54 54 2 2 6
  103378. - 2 2 6 2 2 6 2 2 6 2 2 6
  103379. - 2 2 6 2 2 6 2 2 6 38 38 38
  103380. - 86 86 86 50 50 50 22 22 22 6 6 6
  103381. - 0 0 0 0 0 0 0 0 0 0 0 0
  103382. - 0 0 0 0 0 0 0 0 0 0 0 0
  103383. - 0 0 0 0 0 0 0 0 0 0 0 0
  103384. - 0 0 0 0 0 0 0 0 0 0 0 0
  103385. - 0 0 0 0 0 0 0 0 0 0 0 0
  103386. - 0 0 0 0 0 0 0 0 0 0 0 0
  103387. - 0 0 0 0 0 0 0 0 0 0 0 0
  103388. - 0 0 0 0 0 0 0 0 0 0 0 0
  103389. - 0 0 0 0 0 0 0 0 0 0 0 0
  103390. - 6 6 6 14 14 14 38 38 38 82 82 82
  103391. - 34 34 34 2 2 6 2 2 6 2 2 6
  103392. - 42 42 42 195 195 195 246 246 246 253 253 253
  103393. -253 253 253 253 253 253 253 253 253 250 250 250
  103394. -242 242 242 242 242 242 250 250 250 253 253 253
  103395. -253 253 253 253 253 253 253 253 253 253 253 253
  103396. -253 253 253 250 250 250 246 246 246 238 238 238
  103397. -226 226 226 231 231 231 101 101 101 6 6 6
  103398. - 2 2 6 2 2 6 2 2 6 2 2 6
  103399. - 2 2 6 2 2 6 2 2 6 2 2 6
  103400. - 38 38 38 82 82 82 42 42 42 14 14 14
  103401. - 6 6 6 0 0 0 0 0 0 0 0 0
  103402. - 0 0 0 0 0 0 0 0 0 0 0 0
  103403. - 0 0 0 0 0 0 0 0 0 0 0 0
  103404. - 0 0 0 0 0 0 0 0 0 0 0 0
  103405. - 0 0 0 0 0 0 0 0 0 0 0 0
  103406. - 0 0 0 0 0 0 0 0 0 0 0 0
  103407. - 0 0 0 0 0 0 0 0 0 0 0 0
  103408. - 0 0 0 0 0 0 0 0 0 0 0 0
  103409. - 0 0 0 0 0 0 0 0 0 0 0 0
  103410. - 10 10 10 26 26 26 62 62 62 66 66 66
  103411. - 2 2 6 2 2 6 2 2 6 6 6 6
  103412. - 70 70 70 170 170 170 206 206 206 234 234 234
  103413. -246 246 246 250 250 250 250 250 250 238 238 238
  103414. -226 226 226 231 231 231 238 238 238 250 250 250
  103415. -250 250 250 250 250 250 246 246 246 231 231 231
  103416. -214 214 214 206 206 206 202 202 202 202 202 202
  103417. -198 198 198 202 202 202 182 182 182 18 18 18
  103418. - 2 2 6 2 2 6 2 2 6 2 2 6
  103419. - 2 2 6 2 2 6 2 2 6 2 2 6
  103420. - 2 2 6 62 62 62 66 66 66 30 30 30
  103421. - 10 10 10 0 0 0 0 0 0 0 0 0
  103422. - 0 0 0 0 0 0 0 0 0 0 0 0
  103423. - 0 0 0 0 0 0 0 0 0 0 0 0
  103424. - 0 0 0 0 0 0 0 0 0 0 0 0
  103425. - 0 0 0 0 0 0 0 0 0 0 0 0
  103426. - 0 0 0 0 0 0 0 0 0 0 0 0
  103427. - 0 0 0 0 0 0 0 0 0 0 0 0
  103428. - 0 0 0 0 0 0 0 0 0 0 0 0
  103429. - 0 0 0 0 0 0 0 0 0 0 0 0
  103430. - 14 14 14 42 42 42 82 82 82 18 18 18
  103431. - 2 2 6 2 2 6 2 2 6 10 10 10
  103432. - 94 94 94 182 182 182 218 218 218 242 242 242
  103433. -250 250 250 253 253 253 253 253 253 250 250 250
  103434. -234 234 234 253 253 253 253 253 253 253 253 253
  103435. -253 253 253 253 253 253 253 253 253 246 246 246
  103436. -238 238 238 226 226 226 210 210 210 202 202 202
  103437. -195 195 195 195 195 195 210 210 210 158 158 158
  103438. - 6 6 6 14 14 14 50 50 50 14 14 14
  103439. - 2 2 6 2 2 6 2 2 6 2 2 6
  103440. - 2 2 6 6 6 6 86 86 86 46 46 46
  103441. - 18 18 18 6 6 6 0 0 0 0 0 0
  103442. - 0 0 0 0 0 0 0 0 0 0 0 0
  103443. - 0 0 0 0 0 0 0 0 0 0 0 0
  103444. - 0 0 0 0 0 0 0 0 0 0 0 0
  103445. - 0 0 0 0 0 0 0 0 0 0 0 0
  103446. - 0 0 0 0 0 0 0 0 0 0 0 0
  103447. - 0 0 0 0 0 0 0 0 0 0 0 0
  103448. - 0 0 0 0 0 0 0 0 0 0 0 0
  103449. - 0 0 0 0 0 0 0 0 0 6 6 6
  103450. - 22 22 22 54 54 54 70 70 70 2 2 6
  103451. - 2 2 6 10 10 10 2 2 6 22 22 22
  103452. -166 166 166 231 231 231 250 250 250 253 253 253
  103453. -253 253 253 253 253 253 253 253 253 250 250 250
  103454. -242 242 242 253 253 253 253 253 253 253 253 253
  103455. -253 253 253 253 253 253 253 253 253 253 253 253
  103456. -253 253 253 253 253 253 253 253 253 246 246 246
  103457. -231 231 231 206 206 206 198 198 198 226 226 226
  103458. - 94 94 94 2 2 6 6 6 6 38 38 38
  103459. - 30 30 30 2 2 6 2 2 6 2 2 6
  103460. - 2 2 6 2 2 6 62 62 62 66 66 66
  103461. - 26 26 26 10 10 10 0 0 0 0 0 0
  103462. - 0 0 0 0 0 0 0 0 0 0 0 0
  103463. - 0 0 0 0 0 0 0 0 0 0 0 0
  103464. - 0 0 0 0 0 0 0 0 0 0 0 0
  103465. - 0 0 0 0 0 0 0 0 0 0 0 0
  103466. - 0 0 0 0 0 0 0 0 0 0 0 0
  103467. - 0 0 0 0 0 0 0 0 0 0 0 0
  103468. - 0 0 0 0 0 0 0 0 0 0 0 0
  103469. - 0 0 0 0 0 0 0 0 0 10 10 10
  103470. - 30 30 30 74 74 74 50 50 50 2 2 6
  103471. - 26 26 26 26 26 26 2 2 6 106 106 106
  103472. -238 238 238 253 253 253 253 253 253 253 253 253
  103473. -253 253 253 253 253 253 253 253 253 253 253 253
  103474. -253 253 253 253 253 253 253 253 253 253 253 253
  103475. -253 253 253 253 253 253 253 253 253 253 253 253
  103476. -253 253 253 253 253 253 253 253 253 253 253 253
  103477. -253 253 253 246 246 246 218 218 218 202 202 202
  103478. -210 210 210 14 14 14 2 2 6 2 2 6
  103479. - 30 30 30 22 22 22 2 2 6 2 2 6
  103480. - 2 2 6 2 2 6 18 18 18 86 86 86
  103481. - 42 42 42 14 14 14 0 0 0 0 0 0
  103482. - 0 0 0 0 0 0 0 0 0 0 0 0
  103483. - 0 0 0 0 0 0 0 0 0 0 0 0
  103484. - 0 0 0 0 0 0 0 0 0 0 0 0
  103485. - 0 0 0 0 0 0 0 0 0 0 0 0
  103486. - 0 0 0 0 0 0 0 0 0 0 0 0
  103487. - 0 0 0 0 0 0 0 0 0 0 0 0
  103488. - 0 0 0 0 0 0 0 0 0 0 0 0
  103489. - 0 0 0 0 0 0 0 0 0 14 14 14
  103490. - 42 42 42 90 90 90 22 22 22 2 2 6
  103491. - 42 42 42 2 2 6 18 18 18 218 218 218
  103492. -253 253 253 253 253 253 253 253 253 253 253 253
  103493. -253 253 253 253 253 253 253 253 253 253 253 253
  103494. -253 253 253 253 253 253 253 253 253 253 253 253
  103495. -253 253 253 253 253 253 253 253 253 253 253 253
  103496. -253 253 253 253 253 253 253 253 253 253 253 253
  103497. -253 253 253 253 253 253 250 250 250 221 221 221
  103498. -218 218 218 101 101 101 2 2 6 14 14 14
  103499. - 18 18 18 38 38 38 10 10 10 2 2 6
  103500. - 2 2 6 2 2 6 2 2 6 78 78 78
  103501. - 58 58 58 22 22 22 6 6 6 0 0 0
  103502. - 0 0 0 0 0 0 0 0 0 0 0 0
  103503. - 0 0 0 0 0 0 0 0 0 0 0 0
  103504. - 0 0 0 0 0 0 0 0 0 0 0 0
  103505. - 0 0 0 0 0 0 0 0 0 0 0 0
  103506. - 0 0 0 0 0 0 0 0 0 0 0 0
  103507. - 0 0 0 0 0 0 0 0 0 0 0 0
  103508. - 0 0 0 0 0 0 0 0 0 0 0 0
  103509. - 0 0 0 0 0 0 6 6 6 18 18 18
  103510. - 54 54 54 82 82 82 2 2 6 26 26 26
  103511. - 22 22 22 2 2 6 123 123 123 253 253 253
  103512. -253 253 253 253 253 253 253 253 253 253 253 253
  103513. -253 253 253 253 253 253 253 253 253 253 253 253
  103514. -253 253 253 253 253 253 253 253 253 253 253 253
  103515. -253 253 253 253 253 253 253 253 253 253 253 253
  103516. -253 253 253 253 253 253 253 253 253 253 253 253
  103517. -253 253 253 253 253 253 253 253 253 250 250 250
  103518. -238 238 238 198 198 198 6 6 6 38 38 38
  103519. - 58 58 58 26 26 26 38 38 38 2 2 6
  103520. - 2 2 6 2 2 6 2 2 6 46 46 46
  103521. - 78 78 78 30 30 30 10 10 10 0 0 0
  103522. - 0 0 0 0 0 0 0 0 0 0 0 0
  103523. - 0 0 0 0 0 0 0 0 0 0 0 0
  103524. - 0 0 0 0 0 0 0 0 0 0 0 0
  103525. - 0 0 0 0 0 0 0 0 0 0 0 0
  103526. - 0 0 0 0 0 0 0 0 0 0 0 0
  103527. - 0 0 0 0 0 0 0 0 0 0 0 0
  103528. - 0 0 0 0 0 0 0 0 0 0 0 0
  103529. - 0 0 0 0 0 0 10 10 10 30 30 30
  103530. - 74 74 74 58 58 58 2 2 6 42 42 42
  103531. - 2 2 6 22 22 22 231 231 231 253 253 253
  103532. -253 253 253 253 253 253 253 253 253 253 253 253
  103533. -253 253 253 253 253 253 253 253 253 250 250 250
  103534. -253 253 253 253 253 253 253 253 253 253 253 253
  103535. -253 253 253 253 253 253 253 253 253 253 253 253
  103536. -253 253 253 253 253 253 253 253 253 253 253 253
  103537. -253 253 253 253 253 253 253 253 253 253 253 253
  103538. -253 253 253 246 246 246 46 46 46 38 38 38
  103539. - 42 42 42 14 14 14 38 38 38 14 14 14
  103540. - 2 2 6 2 2 6 2 2 6 6 6 6
  103541. - 86 86 86 46 46 46 14 14 14 0 0 0
  103542. - 0 0 0 0 0 0 0 0 0 0 0 0
  103543. - 0 0 0 0 0 0 0 0 0 0 0 0
  103544. - 0 0 0 0 0 0 0 0 0 0 0 0
  103545. - 0 0 0 0 0 0 0 0 0 0 0 0
  103546. - 0 0 0 0 0 0 0 0 0 0 0 0
  103547. - 0 0 0 0 0 0 0 0 0 0 0 0
  103548. - 0 0 0 0 0 0 0 0 0 0 0 0
  103549. - 0 0 0 6 6 6 14 14 14 42 42 42
  103550. - 90 90 90 18 18 18 18 18 18 26 26 26
  103551. - 2 2 6 116 116 116 253 253 253 253 253 253
  103552. -253 253 253 253 253 253 253 253 253 253 253 253
  103553. -253 253 253 253 253 253 250 250 250 238 238 238
  103554. -253 253 253 253 253 253 253 253 253 253 253 253
  103555. -253 253 253 253 253 253 253 253 253 253 253 253
  103556. -253 253 253 253 253 253 253 253 253 253 253 253
  103557. -253 253 253 253 253 253 253 253 253 253 253 253
  103558. -253 253 253 253 253 253 94 94 94 6 6 6
  103559. - 2 2 6 2 2 6 10 10 10 34 34 34
  103560. - 2 2 6 2 2 6 2 2 6 2 2 6
  103561. - 74 74 74 58 58 58 22 22 22 6 6 6
  103562. - 0 0 0 0 0 0 0 0 0 0 0 0
  103563. - 0 0 0 0 0 0 0 0 0 0 0 0
  103564. - 0 0 0 0 0 0 0 0 0 0 0 0
  103565. - 0 0 0 0 0 0 0 0 0 0 0 0
  103566. - 0 0 0 0 0 0 0 0 0 0 0 0
  103567. - 0 0 0 0 0 0 0 0 0 0 0 0
  103568. - 0 0 0 0 0 0 0 0 0 0 0 0
  103569. - 0 0 0 10 10 10 26 26 26 66 66 66
  103570. - 82 82 82 2 2 6 38 38 38 6 6 6
  103571. - 14 14 14 210 210 210 253 253 253 253 253 253
  103572. -253 253 253 253 253 253 253 253 253 253 253 253
  103573. -253 253 253 253 253 253 246 246 246 242 242 242
  103574. -253 253 253 253 253 253 253 253 253 253 253 253
  103575. -253 253 253 253 253 253 253 253 253 253 253 253
  103576. -253 253 253 253 253 253 253 253 253 253 253 253
  103577. -253 253 253 253 253 253 253 253 253 253 253 253
  103578. -253 253 253 253 253 253 144 144 144 2 2 6
  103579. - 2 2 6 2 2 6 2 2 6 46 46 46
  103580. - 2 2 6 2 2 6 2 2 6 2 2 6
  103581. - 42 42 42 74 74 74 30 30 30 10 10 10
  103582. - 0 0 0 0 0 0 0 0 0 0 0 0
  103583. - 0 0 0 0 0 0 0 0 0 0 0 0
  103584. - 0 0 0 0 0 0 0 0 0 0 0 0
  103585. - 0 0 0 0 0 0 0 0 0 0 0 0
  103586. - 0 0 0 0 0 0 0 0 0 0 0 0
  103587. - 0 0 0 0 0 0 0 0 0 0 0 0
  103588. - 0 0 0 0 0 0 0 0 0 0 0 0
  103589. - 6 6 6 14 14 14 42 42 42 90 90 90
  103590. - 26 26 26 6 6 6 42 42 42 2 2 6
  103591. - 74 74 74 250 250 250 253 253 253 253 253 253
  103592. -253 253 253 253 253 253 253 253 253 253 253 253
  103593. -253 253 253 253 253 253 242 242 242 242 242 242
  103594. -253 253 253 253 253 253 253 253 253 253 253 253
  103595. -253 253 253 253 253 253 253 253 253 253 253 253
  103596. -253 253 253 253 253 253 253 253 253 253 253 253
  103597. -253 253 253 253 253 253 253 253 253 253 253 253
  103598. -253 253 253 253 253 253 182 182 182 2 2 6
  103599. - 2 2 6 2 2 6 2 2 6 46 46 46
  103600. - 2 2 6 2 2 6 2 2 6 2 2 6
  103601. - 10 10 10 86 86 86 38 38 38 10 10 10
  103602. - 0 0 0 0 0 0 0 0 0 0 0 0
  103603. - 0 0 0 0 0 0 0 0 0 0 0 0
  103604. - 0 0 0 0 0 0 0 0 0 0 0 0
  103605. - 0 0 0 0 0 0 0 0 0 0 0 0
  103606. - 0 0 0 0 0 0 0 0 0 0 0 0
  103607. - 0 0 0 0 0 0 0 0 0 0 0 0
  103608. - 0 0 0 0 0 0 0 0 0 0 0 0
  103609. - 10 10 10 26 26 26 66 66 66 82 82 82
  103610. - 2 2 6 22 22 22 18 18 18 2 2 6
  103611. -149 149 149 253 253 253 253 253 253 253 253 253
  103612. -253 253 253 253 253 253 253 253 253 253 253 253
  103613. -253 253 253 253 253 253 234 234 234 242 242 242
  103614. -253 253 253 253 253 253 253 253 253 253 253 253
  103615. -253 253 253 253 253 253 253 253 253 253 253 253
  103616. -253 253 253 253 253 253 253 253 253 253 253 253
  103617. -253 253 253 253 253 253 253 253 253 253 253 253
  103618. -253 253 253 253 253 253 206 206 206 2 2 6
  103619. - 2 2 6 2 2 6 2 2 6 38 38 38
  103620. - 2 2 6 2 2 6 2 2 6 2 2 6
  103621. - 6 6 6 86 86 86 46 46 46 14 14 14
  103622. - 0 0 0 0 0 0 0 0 0 0 0 0
  103623. - 0 0 0 0 0 0 0 0 0 0 0 0
  103624. - 0 0 0 0 0 0 0 0 0 0 0 0
  103625. - 0 0 0 0 0 0 0 0 0 0 0 0
  103626. - 0 0 0 0 0 0 0 0 0 0 0 0
  103627. - 0 0 0 0 0 0 0 0 0 0 0 0
  103628. - 0 0 0 0 0 0 0 0 0 6 6 6
  103629. - 18 18 18 46 46 46 86 86 86 18 18 18
  103630. - 2 2 6 34 34 34 10 10 10 6 6 6
  103631. -210 210 210 253 253 253 253 253 253 253 253 253
  103632. -253 253 253 253 253 253 253 253 253 253 253 253
  103633. -253 253 253 253 253 253 234 234 234 242 242 242
  103634. -253 253 253 253 253 253 253 253 253 253 253 253
  103635. -253 253 253 253 253 253 253 253 253 253 253 253
  103636. -253 253 253 253 253 253 253 253 253 253 253 253
  103637. -253 253 253 253 253 253 253 253 253 253 253 253
  103638. -253 253 253 253 253 253 221 221 221 6 6 6
  103639. - 2 2 6 2 2 6 6 6 6 30 30 30
  103640. - 2 2 6 2 2 6 2 2 6 2 2 6
  103641. - 2 2 6 82 82 82 54 54 54 18 18 18
  103642. - 6 6 6 0 0 0 0 0 0 0 0 0
  103643. - 0 0 0 0 0 0 0 0 0 0 0 0
  103644. - 0 0 0 0 0 0 0 0 0 0 0 0
  103645. - 0 0 0 0 0 0 0 0 0 0 0 0
  103646. - 0 0 0 0 0 0 0 0 0 0 0 0
  103647. - 0 0 0 0 0 0 0 0 0 0 0 0
  103648. - 0 0 0 0 0 0 0 0 0 10 10 10
  103649. - 26 26 26 66 66 66 62 62 62 2 2 6
  103650. - 2 2 6 38 38 38 10 10 10 26 26 26
  103651. -238 238 238 253 253 253 253 253 253 253 253 253
  103652. -253 253 253 253 253 253 253 253 253 253 253 253
  103653. -253 253 253 253 253 253 231 231 231 238 238 238
  103654. -253 253 253 253 253 253 253 253 253 253 253 253
  103655. -253 253 253 253 253 253 253 253 253 253 253 253
  103656. -253 253 253 253 253 253 253 253 253 253 253 253
  103657. -253 253 253 253 253 253 253 253 253 253 253 253
  103658. -253 253 253 253 253 253 231 231 231 6 6 6
  103659. - 2 2 6 2 2 6 10 10 10 30 30 30
  103660. - 2 2 6 2 2 6 2 2 6 2 2 6
  103661. - 2 2 6 66 66 66 58 58 58 22 22 22
  103662. - 6 6 6 0 0 0 0 0 0 0 0 0
  103663. - 0 0 0 0 0 0 0 0 0 0 0 0
  103664. - 0 0 0 0 0 0 0 0 0 0 0 0
  103665. - 0 0 0 0 0 0 0 0 0 0 0 0
  103666. - 0 0 0 0 0 0 0 0 0 0 0 0
  103667. - 0 0 0 0 0 0 0 0 0 0 0 0
  103668. - 0 0 0 0 0 0 0 0 0 10 10 10
  103669. - 38 38 38 78 78 78 6 6 6 2 2 6
  103670. - 2 2 6 46 46 46 14 14 14 42 42 42
  103671. -246 246 246 253 253 253 253 253 253 253 253 253
  103672. -253 253 253 253 253 253 253 253 253 253 253 253
  103673. -253 253 253 253 253 253 231 231 231 242 242 242
  103674. -253 253 253 253 253 253 253 253 253 253 253 253
  103675. -253 253 253 253 253 253 253 253 253 253 253 253
  103676. -253 253 253 253 253 253 253 253 253 253 253 253
  103677. -253 253 253 253 253 253 253 253 253 253 253 253
  103678. -253 253 253 253 253 253 234 234 234 10 10 10
  103679. - 2 2 6 2 2 6 22 22 22 14 14 14
  103680. - 2 2 6 2 2 6 2 2 6 2 2 6
  103681. - 2 2 6 66 66 66 62 62 62 22 22 22
  103682. - 6 6 6 0 0 0 0 0 0 0 0 0
  103683. - 0 0 0 0 0 0 0 0 0 0 0 0
  103684. - 0 0 0 0 0 0 0 0 0 0 0 0
  103685. - 0 0 0 0 0 0 0 0 0 0 0 0
  103686. - 0 0 0 0 0 0 0 0 0 0 0 0
  103687. - 0 0 0 0 0 0 0 0 0 0 0 0
  103688. - 0 0 0 0 0 0 6 6 6 18 18 18
  103689. - 50 50 50 74 74 74 2 2 6 2 2 6
  103690. - 14 14 14 70 70 70 34 34 34 62 62 62
  103691. -250 250 250 253 253 253 253 253 253 253 253 253
  103692. -253 253 253 253 253 253 253 253 253 253 253 253
  103693. -253 253 253 253 253 253 231 231 231 246 246 246
  103694. -253 253 253 253 253 253 253 253 253 253 253 253
  103695. -253 253 253 253 253 253 253 253 253 253 253 253
  103696. -253 253 253 253 253 253 253 253 253 253 253 253
  103697. -253 253 253 253 253 253 253 253 253 253 253 253
  103698. -253 253 253 253 253 253 234 234 234 14 14 14
  103699. - 2 2 6 2 2 6 30 30 30 2 2 6
  103700. - 2 2 6 2 2 6 2 2 6 2 2 6
  103701. - 2 2 6 66 66 66 62 62 62 22 22 22
  103702. - 6 6 6 0 0 0 0 0 0 0 0 0
  103703. - 0 0 0 0 0 0 0 0 0 0 0 0
  103704. - 0 0 0 0 0 0 0 0 0 0 0 0
  103705. - 0 0 0 0 0 0 0 0 0 0 0 0
  103706. - 0 0 0 0 0 0 0 0 0 0 0 0
  103707. - 0 0 0 0 0 0 0 0 0 0 0 0
  103708. - 0 0 0 0 0 0 6 6 6 18 18 18
  103709. - 54 54 54 62 62 62 2 2 6 2 2 6
  103710. - 2 2 6 30 30 30 46 46 46 70 70 70
  103711. -250 250 250 253 253 253 253 253 253 253 253 253
  103712. -253 253 253 253 253 253 253 253 253 253 253 253
  103713. -253 253 253 253 253 253 231 231 231 246 246 246
  103714. -253 253 253 253 253 253 253 253 253 253 253 253
  103715. -253 253 253 253 253 253 253 253 253 253 253 253
  103716. -253 253 253 253 253 253 253 253 253 253 253 253
  103717. -253 253 253 253 253 253 253 253 253 253 253 253
  103718. -253 253 253 253 253 253 226 226 226 10 10 10
  103719. - 2 2 6 6 6 6 30 30 30 2 2 6
  103720. - 2 2 6 2 2 6 2 2 6 2 2 6
  103721. - 2 2 6 66 66 66 58 58 58 22 22 22
  103722. - 6 6 6 0 0 0 0 0 0 0 0 0
  103723. - 0 0 0 0 0 0 0 0 0 0 0 0
  103724. - 0 0 0 0 0 0 0 0 0 0 0 0
  103725. - 0 0 0 0 0 0 0 0 0 0 0 0
  103726. - 0 0 0 0 0 0 0 0 0 0 0 0
  103727. - 0 0 0 0 0 0 0 0 0 0 0 0
  103728. - 0 0 0 0 0 0 6 6 6 22 22 22
  103729. - 58 58 58 62 62 62 2 2 6 2 2 6
  103730. - 2 2 6 2 2 6 30 30 30 78 78 78
  103731. -250 250 250 253 253 253 253 253 253 253 253 253
  103732. -253 253 253 253 253 253 253 253 253 253 253 253
  103733. -253 253 253 253 253 253 231 231 231 246 246 246
  103734. -253 253 253 253 253 253 253 253 253 253 253 253
  103735. -253 253 253 253 253 253 253 253 253 253 253 253
  103736. -253 253 253 253 253 253 253 253 253 253 253 253
  103737. -253 253 253 253 253 253 253 253 253 253 253 253
  103738. -253 253 253 253 253 253 206 206 206 2 2 6
  103739. - 22 22 22 34 34 34 18 14 6 22 22 22
  103740. - 26 26 26 18 18 18 6 6 6 2 2 6
  103741. - 2 2 6 82 82 82 54 54 54 18 18 18
  103742. - 6 6 6 0 0 0 0 0 0 0 0 0
  103743. - 0 0 0 0 0 0 0 0 0 0 0 0
  103744. - 0 0 0 0 0 0 0 0 0 0 0 0
  103745. - 0 0 0 0 0 0 0 0 0 0 0 0
  103746. - 0 0 0 0 0 0 0 0 0 0 0 0
  103747. - 0 0 0 0 0 0 0 0 0 0 0 0
  103748. - 0 0 0 0 0 0 6 6 6 26 26 26
  103749. - 62 62 62 106 106 106 74 54 14 185 133 11
  103750. -210 162 10 121 92 8 6 6 6 62 62 62
  103751. -238 238 238 253 253 253 253 253 253 253 253 253
  103752. -253 253 253 253 253 253 253 253 253 253 253 253
  103753. -253 253 253 253 253 253 231 231 231 246 246 246
  103754. -253 253 253 253 253 253 253 253 253 253 253 253
  103755. -253 253 253 253 253 253 253 253 253 253 253 253
  103756. -253 253 253 253 253 253 253 253 253 253 253 253
  103757. -253 253 253 253 253 253 253 253 253 253 253 253
  103758. -253 253 253 253 253 253 158 158 158 18 18 18
  103759. - 14 14 14 2 2 6 2 2 6 2 2 6
  103760. - 6 6 6 18 18 18 66 66 66 38 38 38
  103761. - 6 6 6 94 94 94 50 50 50 18 18 18
  103762. - 6 6 6 0 0 0 0 0 0 0 0 0
  103763. - 0 0 0 0 0 0 0 0 0 0 0 0
  103764. - 0 0 0 0 0 0 0 0 0 0 0 0
  103765. - 0 0 0 0 0 0 0 0 0 0 0 0
  103766. - 0 0 0 0 0 0 0 0 0 0 0 0
  103767. - 0 0 0 0 0 0 0 0 0 6 6 6
  103768. - 10 10 10 10 10 10 18 18 18 38 38 38
  103769. - 78 78 78 142 134 106 216 158 10 242 186 14
  103770. -246 190 14 246 190 14 156 118 10 10 10 10
  103771. - 90 90 90 238 238 238 253 253 253 253 253 253
  103772. -253 253 253 253 253 253 253 253 253 253 253 253
  103773. -253 253 253 253 253 253 231 231 231 250 250 250
  103774. -253 253 253 253 253 253 253 253 253 253 253 253
  103775. -253 253 253 253 253 253 253 253 253 253 253 253
  103776. -253 253 253 253 253 253 253 253 253 253 253 253
  103777. -253 253 253 253 253 253 253 253 253 246 230 190
  103778. -238 204 91 238 204 91 181 142 44 37 26 9
  103779. - 2 2 6 2 2 6 2 2 6 2 2 6
  103780. - 2 2 6 2 2 6 38 38 38 46 46 46
  103781. - 26 26 26 106 106 106 54 54 54 18 18 18
  103782. - 6 6 6 0 0 0 0 0 0 0 0 0
  103783. - 0 0 0 0 0 0 0 0 0 0 0 0
  103784. - 0 0 0 0 0 0 0 0 0 0 0 0
  103785. - 0 0 0 0 0 0 0 0 0 0 0 0
  103786. - 0 0 0 0 0 0 0 0 0 0 0 0
  103787. - 0 0 0 6 6 6 14 14 14 22 22 22
  103788. - 30 30 30 38 38 38 50 50 50 70 70 70
  103789. -106 106 106 190 142 34 226 170 11 242 186 14
  103790. -246 190 14 246 190 14 246 190 14 154 114 10
  103791. - 6 6 6 74 74 74 226 226 226 253 253 253
  103792. -253 253 253 253 253 253 253 253 253 253 253 253
  103793. -253 253 253 253 253 253 231 231 231 250 250 250
  103794. -253 253 253 253 253 253 253 253 253 253 253 253
  103795. -253 253 253 253 253 253 253 253 253 253 253 253
  103796. -253 253 253 253 253 253 253 253 253 253 253 253
  103797. -253 253 253 253 253 253 253 253 253 228 184 62
  103798. -241 196 14 241 208 19 232 195 16 38 30 10
  103799. - 2 2 6 2 2 6 2 2 6 2 2 6
  103800. - 2 2 6 6 6 6 30 30 30 26 26 26
  103801. -203 166 17 154 142 90 66 66 66 26 26 26
  103802. - 6 6 6 0 0 0 0 0 0 0 0 0
  103803. - 0 0 0 0 0 0 0 0 0 0 0 0
  103804. - 0 0 0 0 0 0 0 0 0 0 0 0
  103805. - 0 0 0 0 0 0 0 0 0 0 0 0
  103806. - 0 0 0 0 0 0 0 0 0 0 0 0
  103807. - 6 6 6 18 18 18 38 38 38 58 58 58
  103808. - 78 78 78 86 86 86 101 101 101 123 123 123
  103809. -175 146 61 210 150 10 234 174 13 246 186 14
  103810. -246 190 14 246 190 14 246 190 14 238 190 10
  103811. -102 78 10 2 2 6 46 46 46 198 198 198
  103812. -253 253 253 253 253 253 253 253 253 253 253 253
  103813. -253 253 253 253 253 253 234 234 234 242 242 242
  103814. -253 253 253 253 253 253 253 253 253 253 253 253
  103815. -253 253 253 253 253 253 253 253 253 253 253 253
  103816. -253 253 253 253 253 253 253 253 253 253 253 253
  103817. -253 253 253 253 253 253 253 253 253 224 178 62
  103818. -242 186 14 241 196 14 210 166 10 22 18 6
  103819. - 2 2 6 2 2 6 2 2 6 2 2 6
  103820. - 2 2 6 2 2 6 6 6 6 121 92 8
  103821. -238 202 15 232 195 16 82 82 82 34 34 34
  103822. - 10 10 10 0 0 0 0 0 0 0 0 0
  103823. - 0 0 0 0 0 0 0 0 0 0 0 0
  103824. - 0 0 0 0 0 0 0 0 0 0 0 0
  103825. - 0 0 0 0 0 0 0 0 0 0 0 0
  103826. - 0 0 0 0 0 0 0 0 0 0 0 0
  103827. - 14 14 14 38 38 38 70 70 70 154 122 46
  103828. -190 142 34 200 144 11 197 138 11 197 138 11
  103829. -213 154 11 226 170 11 242 186 14 246 190 14
  103830. -246 190 14 246 190 14 246 190 14 246 190 14
  103831. -225 175 15 46 32 6 2 2 6 22 22 22
  103832. -158 158 158 250 250 250 253 253 253 253 253 253
  103833. -253 253 253 253 253 253 253 253 253 253 253 253
  103834. -253 253 253 253 253 253 253 253 253 253 253 253
  103835. -253 253 253 253 253 253 253 253 253 253 253 253
  103836. -253 253 253 253 253 253 253 253 253 253 253 253
  103837. -253 253 253 250 250 250 242 242 242 224 178 62
  103838. -239 182 13 236 186 11 213 154 11 46 32 6
  103839. - 2 2 6 2 2 6 2 2 6 2 2 6
  103840. - 2 2 6 2 2 6 61 42 6 225 175 15
  103841. -238 190 10 236 186 11 112 100 78 42 42 42
  103842. - 14 14 14 0 0 0 0 0 0 0 0 0
  103843. - 0 0 0 0 0 0 0 0 0 0 0 0
  103844. - 0 0 0 0 0 0 0 0 0 0 0 0
  103845. - 0 0 0 0 0 0 0 0 0 0 0 0
  103846. - 0 0 0 0 0 0 0 0 0 6 6 6
  103847. - 22 22 22 54 54 54 154 122 46 213 154 11
  103848. -226 170 11 230 174 11 226 170 11 226 170 11
  103849. -236 178 12 242 186 14 246 190 14 246 190 14
  103850. -246 190 14 246 190 14 246 190 14 246 190 14
  103851. -241 196 14 184 144 12 10 10 10 2 2 6
  103852. - 6 6 6 116 116 116 242 242 242 253 253 253
  103853. -253 253 253 253 253 253 253 253 253 253 253 253
  103854. -253 253 253 253 253 253 253 253 253 253 253 253
  103855. -253 253 253 253 253 253 253 253 253 253 253 253
  103856. -253 253 253 253 253 253 253 253 253 253 253 253
  103857. -253 253 253 231 231 231 198 198 198 214 170 54
  103858. -236 178 12 236 178 12 210 150 10 137 92 6
  103859. - 18 14 6 2 2 6 2 2 6 2 2 6
  103860. - 6 6 6 70 47 6 200 144 11 236 178 12
  103861. -239 182 13 239 182 13 124 112 88 58 58 58
  103862. - 22 22 22 6 6 6 0 0 0 0 0 0
  103863. - 0 0 0 0 0 0 0 0 0 0 0 0
  103864. - 0 0 0 0 0 0 0 0 0 0 0 0
  103865. - 0 0 0 0 0 0 0 0 0 0 0 0
  103866. - 0 0 0 0 0 0 0 0 0 10 10 10
  103867. - 30 30 30 70 70 70 180 133 36 226 170 11
  103868. -239 182 13 242 186 14 242 186 14 246 186 14
  103869. -246 190 14 246 190 14 246 190 14 246 190 14
  103870. -246 190 14 246 190 14 246 190 14 246 190 14
  103871. -246 190 14 232 195 16 98 70 6 2 2 6
  103872. - 2 2 6 2 2 6 66 66 66 221 221 221
  103873. -253 253 253 253 253 253 253 253 253 253 253 253
  103874. -253 253 253 253 253 253 253 253 253 253 253 253
  103875. -253 253 253 253 253 253 253 253 253 253 253 253
  103876. -253 253 253 253 253 253 253 253 253 253 253 253
  103877. -253 253 253 206 206 206 198 198 198 214 166 58
  103878. -230 174 11 230 174 11 216 158 10 192 133 9
  103879. -163 110 8 116 81 8 102 78 10 116 81 8
  103880. -167 114 7 197 138 11 226 170 11 239 182 13
  103881. -242 186 14 242 186 14 162 146 94 78 78 78
  103882. - 34 34 34 14 14 14 6 6 6 0 0 0
  103883. - 0 0 0 0 0 0 0 0 0 0 0 0
  103884. - 0 0 0 0 0 0 0 0 0 0 0 0
  103885. - 0 0 0 0 0 0 0 0 0 0 0 0
  103886. - 0 0 0 0 0 0 0 0 0 6 6 6
  103887. - 30 30 30 78 78 78 190 142 34 226 170 11
  103888. -239 182 13 246 190 14 246 190 14 246 190 14
  103889. -246 190 14 246 190 14 246 190 14 246 190 14
  103890. -246 190 14 246 190 14 246 190 14 246 190 14
  103891. -246 190 14 241 196 14 203 166 17 22 18 6
  103892. - 2 2 6 2 2 6 2 2 6 38 38 38
  103893. -218 218 218 253 253 253 253 253 253 253 253 253
  103894. -253 253 253 253 253 253 253 253 253 253 253 253
  103895. -253 253 253 253 253 253 253 253 253 253 253 253
  103896. -253 253 253 253 253 253 253 253 253 253 253 253
  103897. -250 250 250 206 206 206 198 198 198 202 162 69
  103898. -226 170 11 236 178 12 224 166 10 210 150 10
  103899. -200 144 11 197 138 11 192 133 9 197 138 11
  103900. -210 150 10 226 170 11 242 186 14 246 190 14
  103901. -246 190 14 246 186 14 225 175 15 124 112 88
  103902. - 62 62 62 30 30 30 14 14 14 6 6 6
  103903. - 0 0 0 0 0 0 0 0 0 0 0 0
  103904. - 0 0 0 0 0 0 0 0 0 0 0 0
  103905. - 0 0 0 0 0 0 0 0 0 0 0 0
  103906. - 0 0 0 0 0 0 0 0 0 10 10 10
  103907. - 30 30 30 78 78 78 174 135 50 224 166 10
  103908. -239 182 13 246 190 14 246 190 14 246 190 14
  103909. -246 190 14 246 190 14 246 190 14 246 190 14
  103910. -246 190 14 246 190 14 246 190 14 246 190 14
  103911. -246 190 14 246 190 14 241 196 14 139 102 15
  103912. - 2 2 6 2 2 6 2 2 6 2 2 6
  103913. - 78 78 78 250 250 250 253 253 253 253 253 253
  103914. -253 253 253 253 253 253 253 253 253 253 253 253
  103915. -253 253 253 253 253 253 253 253 253 253 253 253
  103916. -253 253 253 253 253 253 253 253 253 253 253 253
  103917. -250 250 250 214 214 214 198 198 198 190 150 46
  103918. -219 162 10 236 178 12 234 174 13 224 166 10
  103919. -216 158 10 213 154 11 213 154 11 216 158 10
  103920. -226 170 11 239 182 13 246 190 14 246 190 14
  103921. -246 190 14 246 190 14 242 186 14 206 162 42
  103922. -101 101 101 58 58 58 30 30 30 14 14 14
  103923. - 6 6 6 0 0 0 0 0 0 0 0 0
  103924. - 0 0 0 0 0 0 0 0 0 0 0 0
  103925. - 0 0 0 0 0 0 0 0 0 0 0 0
  103926. - 0 0 0 0 0 0 0 0 0 10 10 10
  103927. - 30 30 30 74 74 74 174 135 50 216 158 10
  103928. -236 178 12 246 190 14 246 190 14 246 190 14
  103929. -246 190 14 246 190 14 246 190 14 246 190 14
  103930. -246 190 14 246 190 14 246 190 14 246 190 14
  103931. -246 190 14 246 190 14 241 196 14 226 184 13
  103932. - 61 42 6 2 2 6 2 2 6 2 2 6
  103933. - 22 22 22 238 238 238 253 253 253 253 253 253
  103934. -253 253 253 253 253 253 253 253 253 253 253 253
  103935. -253 253 253 253 253 253 253 253 253 253 253 253
  103936. -253 253 253 253 253 253 253 253 253 253 253 253
  103937. -253 253 253 226 226 226 187 187 187 180 133 36
  103938. -216 158 10 236 178 12 239 182 13 236 178 12
  103939. -230 174 11 226 170 11 226 170 11 230 174 11
  103940. -236 178 12 242 186 14 246 190 14 246 190 14
  103941. -246 190 14 246 190 14 246 186 14 239 182 13
  103942. -206 162 42 106 106 106 66 66 66 34 34 34
  103943. - 14 14 14 6 6 6 0 0 0 0 0 0
  103944. - 0 0 0 0 0 0 0 0 0 0 0 0
  103945. - 0 0 0 0 0 0 0 0 0 0 0 0
  103946. - 0 0 0 0 0 0 0 0 0 6 6 6
  103947. - 26 26 26 70 70 70 163 133 67 213 154 11
  103948. -236 178 12 246 190 14 246 190 14 246 190 14
  103949. -246 190 14 246 190 14 246 190 14 246 190 14
  103950. -246 190 14 246 190 14 246 190 14 246 190 14
  103951. -246 190 14 246 190 14 246 190 14 241 196 14
  103952. -190 146 13 18 14 6 2 2 6 2 2 6
  103953. - 46 46 46 246 246 246 253 253 253 253 253 253
  103954. -253 253 253 253 253 253 253 253 253 253 253 253
  103955. -253 253 253 253 253 253 253 253 253 253 253 253
  103956. -253 253 253 253 253 253 253 253 253 253 253 253
  103957. -253 253 253 221 221 221 86 86 86 156 107 11
  103958. -216 158 10 236 178 12 242 186 14 246 186 14
  103959. -242 186 14 239 182 13 239 182 13 242 186 14
  103960. -242 186 14 246 186 14 246 190 14 246 190 14
  103961. -246 190 14 246 190 14 246 190 14 246 190 14
  103962. -242 186 14 225 175 15 142 122 72 66 66 66
  103963. - 30 30 30 10 10 10 0 0 0 0 0 0
  103964. - 0 0 0 0 0 0 0 0 0 0 0 0
  103965. - 0 0 0 0 0 0 0 0 0 0 0 0
  103966. - 0 0 0 0 0 0 0 0 0 6 6 6
  103967. - 26 26 26 70 70 70 163 133 67 210 150 10
  103968. -236 178 12 246 190 14 246 190 14 246 190 14
  103969. -246 190 14 246 190 14 246 190 14 246 190 14
  103970. -246 190 14 246 190 14 246 190 14 246 190 14
  103971. -246 190 14 246 190 14 246 190 14 246 190 14
  103972. -232 195 16 121 92 8 34 34 34 106 106 106
  103973. -221 221 221 253 253 253 253 253 253 253 253 253
  103974. -253 253 253 253 253 253 253 253 253 253 253 253
  103975. -253 253 253 253 253 253 253 253 253 253 253 253
  103976. -253 253 253 253 253 253 253 253 253 253 253 253
  103977. -242 242 242 82 82 82 18 14 6 163 110 8
  103978. -216 158 10 236 178 12 242 186 14 246 190 14
  103979. -246 190 14 246 190 14 246 190 14 246 190 14
  103980. -246 190 14 246 190 14 246 190 14 246 190 14
  103981. -246 190 14 246 190 14 246 190 14 246 190 14
  103982. -246 190 14 246 190 14 242 186 14 163 133 67
  103983. - 46 46 46 18 18 18 6 6 6 0 0 0
  103984. - 0 0 0 0 0 0 0 0 0 0 0 0
  103985. - 0 0 0 0 0 0 0 0 0 0 0 0
  103986. - 0 0 0 0 0 0 0 0 0 10 10 10
  103987. - 30 30 30 78 78 78 163 133 67 210 150 10
  103988. -236 178 12 246 186 14 246 190 14 246 190 14
  103989. -246 190 14 246 190 14 246 190 14 246 190 14
  103990. -246 190 14 246 190 14 246 190 14 246 190 14
  103991. -246 190 14 246 190 14 246 190 14 246 190 14
  103992. -241 196 14 215 174 15 190 178 144 253 253 253
  103993. -253 253 253 253 253 253 253 253 253 253 253 253
  103994. -253 253 253 253 253 253 253 253 253 253 253 253
  103995. -253 253 253 253 253 253 253 253 253 253 253 253
  103996. -253 253 253 253 253 253 253 253 253 218 218 218
  103997. - 58 58 58 2 2 6 22 18 6 167 114 7
  103998. -216 158 10 236 178 12 246 186 14 246 190 14
  103999. -246 190 14 246 190 14 246 190 14 246 190 14
  104000. -246 190 14 246 190 14 246 190 14 246 190 14
  104001. -246 190 14 246 190 14 246 190 14 246 190 14
  104002. -246 190 14 246 186 14 242 186 14 190 150 46
  104003. - 54 54 54 22 22 22 6 6 6 0 0 0
  104004. - 0 0 0 0 0 0 0 0 0 0 0 0
  104005. - 0 0 0 0 0 0 0 0 0 0 0 0
  104006. - 0 0 0 0 0 0 0 0 0 14 14 14
  104007. - 38 38 38 86 86 86 180 133 36 213 154 11
  104008. -236 178 12 246 186 14 246 190 14 246 190 14
  104009. -246 190 14 246 190 14 246 190 14 246 190 14
  104010. -246 190 14 246 190 14 246 190 14 246 190 14
  104011. -246 190 14 246 190 14 246 190 14 246 190 14
  104012. -246 190 14 232 195 16 190 146 13 214 214 214
  104013. -253 253 253 253 253 253 253 253 253 253 253 253
  104014. -253 253 253 253 253 253 253 253 253 253 253 253
  104015. -253 253 253 253 253 253 253 253 253 253 253 253
  104016. -253 253 253 250 250 250 170 170 170 26 26 26
  104017. - 2 2 6 2 2 6 37 26 9 163 110 8
  104018. -219 162 10 239 182 13 246 186 14 246 190 14
  104019. -246 190 14 246 190 14 246 190 14 246 190 14
  104020. -246 190 14 246 190 14 246 190 14 246 190 14
  104021. -246 190 14 246 190 14 246 190 14 246 190 14
  104022. -246 186 14 236 178 12 224 166 10 142 122 72
  104023. - 46 46 46 18 18 18 6 6 6 0 0 0
  104024. - 0 0 0 0 0 0 0 0 0 0 0 0
  104025. - 0 0 0 0 0 0 0 0 0 0 0 0
  104026. - 0 0 0 0 0 0 6 6 6 18 18 18
  104027. - 50 50 50 109 106 95 192 133 9 224 166 10
  104028. -242 186 14 246 190 14 246 190 14 246 190 14
  104029. -246 190 14 246 190 14 246 190 14 246 190 14
  104030. -246 190 14 246 190 14 246 190 14 246 190 14
  104031. -246 190 14 246 190 14 246 190 14 246 190 14
  104032. -242 186 14 226 184 13 210 162 10 142 110 46
  104033. -226 226 226 253 253 253 253 253 253 253 253 253
  104034. -253 253 253 253 253 253 253 253 253 253 253 253
  104035. -253 253 253 253 253 253 253 253 253 253 253 253
  104036. -198 198 198 66 66 66 2 2 6 2 2 6
  104037. - 2 2 6 2 2 6 50 34 6 156 107 11
  104038. -219 162 10 239 182 13 246 186 14 246 190 14
  104039. -246 190 14 246 190 14 246 190 14 246 190 14
  104040. -246 190 14 246 190 14 246 190 14 246 190 14
  104041. -246 190 14 246 190 14 246 190 14 242 186 14
  104042. -234 174 13 213 154 11 154 122 46 66 66 66
  104043. - 30 30 30 10 10 10 0 0 0 0 0 0
  104044. - 0 0 0 0 0 0 0 0 0 0 0 0
  104045. - 0 0 0 0 0 0 0 0 0 0 0 0
  104046. - 0 0 0 0 0 0 6 6 6 22 22 22
  104047. - 58 58 58 154 121 60 206 145 10 234 174 13
  104048. -242 186 14 246 186 14 246 190 14 246 190 14
  104049. -246 190 14 246 190 14 246 190 14 246 190 14
  104050. -246 190 14 246 190 14 246 190 14 246 190 14
  104051. -246 190 14 246 190 14 246 190 14 246 190 14
  104052. -246 186 14 236 178 12 210 162 10 163 110 8
  104053. - 61 42 6 138 138 138 218 218 218 250 250 250
  104054. -253 253 253 253 253 253 253 253 253 250 250 250
  104055. -242 242 242 210 210 210 144 144 144 66 66 66
  104056. - 6 6 6 2 2 6 2 2 6 2 2 6
  104057. - 2 2 6 2 2 6 61 42 6 163 110 8
  104058. -216 158 10 236 178 12 246 190 14 246 190 14
  104059. -246 190 14 246 190 14 246 190 14 246 190 14
  104060. -246 190 14 246 190 14 246 190 14 246 190 14
  104061. -246 190 14 239 182 13 230 174 11 216 158 10
  104062. -190 142 34 124 112 88 70 70 70 38 38 38
  104063. - 18 18 18 6 6 6 0 0 0 0 0 0
  104064. - 0 0 0 0 0 0 0 0 0 0 0 0
  104065. - 0 0 0 0 0 0 0 0 0 0 0 0
  104066. - 0 0 0 0 0 0 6 6 6 22 22 22
  104067. - 62 62 62 168 124 44 206 145 10 224 166 10
  104068. -236 178 12 239 182 13 242 186 14 242 186 14
  104069. -246 186 14 246 190 14 246 190 14 246 190 14
  104070. -246 190 14 246 190 14 246 190 14 246 190 14
  104071. -246 190 14 246 190 14 246 190 14 246 190 14
  104072. -246 190 14 236 178 12 216 158 10 175 118 6
  104073. - 80 54 7 2 2 6 6 6 6 30 30 30
  104074. - 54 54 54 62 62 62 50 50 50 38 38 38
  104075. - 14 14 14 2 2 6 2 2 6 2 2 6
  104076. - 2 2 6 2 2 6 2 2 6 2 2 6
  104077. - 2 2 6 6 6 6 80 54 7 167 114 7
  104078. -213 154 11 236 178 12 246 190 14 246 190 14
  104079. -246 190 14 246 190 14 246 190 14 246 190 14
  104080. -246 190 14 242 186 14 239 182 13 239 182 13
  104081. -230 174 11 210 150 10 174 135 50 124 112 88
  104082. - 82 82 82 54 54 54 34 34 34 18 18 18
  104083. - 6 6 6 0 0 0 0 0 0 0 0 0
  104084. - 0 0 0 0 0 0 0 0 0 0 0 0
  104085. - 0 0 0 0 0 0 0 0 0 0 0 0
  104086. - 0 0 0 0 0 0 6 6 6 18 18 18
  104087. - 50 50 50 158 118 36 192 133 9 200 144 11
  104088. -216 158 10 219 162 10 224 166 10 226 170 11
  104089. -230 174 11 236 178 12 239 182 13 239 182 13
  104090. -242 186 14 246 186 14 246 190 14 246 190 14
  104091. -246 190 14 246 190 14 246 190 14 246 190 14
  104092. -246 186 14 230 174 11 210 150 10 163 110 8
  104093. -104 69 6 10 10 10 2 2 6 2 2 6
  104094. - 2 2 6 2 2 6 2 2 6 2 2 6
  104095. - 2 2 6 2 2 6 2 2 6 2 2 6
  104096. - 2 2 6 2 2 6 2 2 6 2 2 6
  104097. - 2 2 6 6 6 6 91 60 6 167 114 7
  104098. -206 145 10 230 174 11 242 186 14 246 190 14
  104099. -246 190 14 246 190 14 246 186 14 242 186 14
  104100. -239 182 13 230 174 11 224 166 10 213 154 11
  104101. -180 133 36 124 112 88 86 86 86 58 58 58
  104102. - 38 38 38 22 22 22 10 10 10 6 6 6
  104103. - 0 0 0 0 0 0 0 0 0 0 0 0
  104104. - 0 0 0 0 0 0 0 0 0 0 0 0
  104105. - 0 0 0 0 0 0 0 0 0 0 0 0
  104106. - 0 0 0 0 0 0 0 0 0 14 14 14
  104107. - 34 34 34 70 70 70 138 110 50 158 118 36
  104108. -167 114 7 180 123 7 192 133 9 197 138 11
  104109. -200 144 11 206 145 10 213 154 11 219 162 10
  104110. -224 166 10 230 174 11 239 182 13 242 186 14
  104111. -246 186 14 246 186 14 246 186 14 246 186 14
  104112. -239 182 13 216 158 10 185 133 11 152 99 6
  104113. -104 69 6 18 14 6 2 2 6 2 2 6
  104114. - 2 2 6 2 2 6 2 2 6 2 2 6
  104115. - 2 2 6 2 2 6 2 2 6 2 2 6
  104116. - 2 2 6 2 2 6 2 2 6 2 2 6
  104117. - 2 2 6 6 6 6 80 54 7 152 99 6
  104118. -192 133 9 219 162 10 236 178 12 239 182 13
  104119. -246 186 14 242 186 14 239 182 13 236 178 12
  104120. -224 166 10 206 145 10 192 133 9 154 121 60
  104121. - 94 94 94 62 62 62 42 42 42 22 22 22
  104122. - 14 14 14 6 6 6 0 0 0 0 0 0
  104123. - 0 0 0 0 0 0 0 0 0 0 0 0
  104124. - 0 0 0 0 0 0 0 0 0 0 0 0
  104125. - 0 0 0 0 0 0 0 0 0 0 0 0
  104126. - 0 0 0 0 0 0 0 0 0 6 6 6
  104127. - 18 18 18 34 34 34 58 58 58 78 78 78
  104128. -101 98 89 124 112 88 142 110 46 156 107 11
  104129. -163 110 8 167 114 7 175 118 6 180 123 7
  104130. -185 133 11 197 138 11 210 150 10 219 162 10
  104131. -226 170 11 236 178 12 236 178 12 234 174 13
  104132. -219 162 10 197 138 11 163 110 8 130 83 6
  104133. - 91 60 6 10 10 10 2 2 6 2 2 6
  104134. - 18 18 18 38 38 38 38 38 38 38 38 38
  104135. - 38 38 38 38 38 38 38 38 38 38 38 38
  104136. - 38 38 38 38 38 38 26 26 26 2 2 6
  104137. - 2 2 6 6 6 6 70 47 6 137 92 6
  104138. -175 118 6 200 144 11 219 162 10 230 174 11
  104139. -234 174 13 230 174 11 219 162 10 210 150 10
  104140. -192 133 9 163 110 8 124 112 88 82 82 82
  104141. - 50 50 50 30 30 30 14 14 14 6 6 6
  104142. - 0 0 0 0 0 0 0 0 0 0 0 0
  104143. - 0 0 0 0 0 0 0 0 0 0 0 0
  104144. - 0 0 0 0 0 0 0 0 0 0 0 0
  104145. - 0 0 0 0 0 0 0 0 0 0 0 0
  104146. - 0 0 0 0 0 0 0 0 0 0 0 0
  104147. - 6 6 6 14 14 14 22 22 22 34 34 34
  104148. - 42 42 42 58 58 58 74 74 74 86 86 86
  104149. -101 98 89 122 102 70 130 98 46 121 87 25
  104150. -137 92 6 152 99 6 163 110 8 180 123 7
  104151. -185 133 11 197 138 11 206 145 10 200 144 11
  104152. -180 123 7 156 107 11 130 83 6 104 69 6
  104153. - 50 34 6 54 54 54 110 110 110 101 98 89
  104154. - 86 86 86 82 82 82 78 78 78 78 78 78
  104155. - 78 78 78 78 78 78 78 78 78 78 78 78
  104156. - 78 78 78 82 82 82 86 86 86 94 94 94
  104157. -106 106 106 101 101 101 86 66 34 124 80 6
  104158. -156 107 11 180 123 7 192 133 9 200 144 11
  104159. -206 145 10 200 144 11 192 133 9 175 118 6
  104160. -139 102 15 109 106 95 70 70 70 42 42 42
  104161. - 22 22 22 10 10 10 0 0 0 0 0 0
  104162. - 0 0 0 0 0 0 0 0 0 0 0 0
  104163. - 0 0 0 0 0 0 0 0 0 0 0 0
  104164. - 0 0 0 0 0 0 0 0 0 0 0 0
  104165. - 0 0 0 0 0 0 0 0 0 0 0 0
  104166. - 0 0 0 0 0 0 0 0 0 0 0 0
  104167. - 0 0 0 0 0 0 6 6 6 10 10 10
  104168. - 14 14 14 22 22 22 30 30 30 38 38 38
  104169. - 50 50 50 62 62 62 74 74 74 90 90 90
  104170. -101 98 89 112 100 78 121 87 25 124 80 6
  104171. -137 92 6 152 99 6 152 99 6 152 99 6
  104172. -138 86 6 124 80 6 98 70 6 86 66 30
  104173. -101 98 89 82 82 82 58 58 58 46 46 46
  104174. - 38 38 38 34 34 34 34 34 34 34 34 34
  104175. - 34 34 34 34 34 34 34 34 34 34 34 34
  104176. - 34 34 34 34 34 34 38 38 38 42 42 42
  104177. - 54 54 54 82 82 82 94 86 76 91 60 6
  104178. -134 86 6 156 107 11 167 114 7 175 118 6
  104179. -175 118 6 167 114 7 152 99 6 121 87 25
  104180. -101 98 89 62 62 62 34 34 34 18 18 18
  104181. - 6 6 6 0 0 0 0 0 0 0 0 0
  104182. - 0 0 0 0 0 0 0 0 0 0 0 0
  104183. - 0 0 0 0 0 0 0 0 0 0 0 0
  104184. - 0 0 0 0 0 0 0 0 0 0 0 0
  104185. - 0 0 0 0 0 0 0 0 0 0 0 0
  104186. - 0 0 0 0 0 0 0 0 0 0 0 0
  104187. - 0 0 0 0 0 0 0 0 0 0 0 0
  104188. - 0 0 0 6 6 6 6 6 6 10 10 10
  104189. - 18 18 18 22 22 22 30 30 30 42 42 42
  104190. - 50 50 50 66 66 66 86 86 86 101 98 89
  104191. -106 86 58 98 70 6 104 69 6 104 69 6
  104192. -104 69 6 91 60 6 82 62 34 90 90 90
  104193. - 62 62 62 38 38 38 22 22 22 14 14 14
  104194. - 10 10 10 10 10 10 10 10 10 10 10 10
  104195. - 10 10 10 10 10 10 6 6 6 10 10 10
  104196. - 10 10 10 10 10 10 10 10 10 14 14 14
  104197. - 22 22 22 42 42 42 70 70 70 89 81 66
  104198. - 80 54 7 104 69 6 124 80 6 137 92 6
  104199. -134 86 6 116 81 8 100 82 52 86 86 86
  104200. - 58 58 58 30 30 30 14 14 14 6 6 6
  104201. - 0 0 0 0 0 0 0 0 0 0 0 0
  104202. - 0 0 0 0 0 0 0 0 0 0 0 0
  104203. - 0 0 0 0 0 0 0 0 0 0 0 0
  104204. - 0 0 0 0 0 0 0 0 0 0 0 0
  104205. - 0 0 0 0 0 0 0 0 0 0 0 0
  104206. - 0 0 0 0 0 0 0 0 0 0 0 0
  104207. - 0 0 0 0 0 0 0 0 0 0 0 0
  104208. - 0 0 0 0 0 0 0 0 0 0 0 0
  104209. - 0 0 0 6 6 6 10 10 10 14 14 14
  104210. - 18 18 18 26 26 26 38 38 38 54 54 54
  104211. - 70 70 70 86 86 86 94 86 76 89 81 66
  104212. - 89 81 66 86 86 86 74 74 74 50 50 50
  104213. - 30 30 30 14 14 14 6 6 6 0 0 0
  104214. - 0 0 0 0 0 0 0 0 0 0 0 0
  104215. - 0 0 0 0 0 0 0 0 0 0 0 0
  104216. - 0 0 0 0 0 0 0 0 0 0 0 0
  104217. - 6 6 6 18 18 18 34 34 34 58 58 58
  104218. - 82 82 82 89 81 66 89 81 66 89 81 66
  104219. - 94 86 66 94 86 76 74 74 74 50 50 50
  104220. - 26 26 26 14 14 14 6 6 6 0 0 0
  104221. - 0 0 0 0 0 0 0 0 0 0 0 0
  104222. - 0 0 0 0 0 0 0 0 0 0 0 0
  104223. - 0 0 0 0 0 0 0 0 0 0 0 0
  104224. - 0 0 0 0 0 0 0 0 0 0 0 0
  104225. - 0 0 0 0 0 0 0 0 0 0 0 0
  104226. - 0 0 0 0 0 0 0 0 0 0 0 0
  104227. - 0 0 0 0 0 0 0 0 0 0 0 0
  104228. - 0 0 0 0 0 0 0 0 0 0 0 0
  104229. - 0 0 0 0 0 0 0 0 0 0 0 0
  104230. - 6 6 6 6 6 6 14 14 14 18 18 18
  104231. - 30 30 30 38 38 38 46 46 46 54 54 54
  104232. - 50 50 50 42 42 42 30 30 30 18 18 18
  104233. - 10 10 10 0 0 0 0 0 0 0 0 0
  104234. - 0 0 0 0 0 0 0 0 0 0 0 0
  104235. - 0 0 0 0 0 0 0 0 0 0 0 0
  104236. - 0 0 0 0 0 0 0 0 0 0 0 0
  104237. - 0 0 0 6 6 6 14 14 14 26 26 26
  104238. - 38 38 38 50 50 50 58 58 58 58 58 58
  104239. - 54 54 54 42 42 42 30 30 30 18 18 18
  104240. - 10 10 10 0 0 0 0 0 0 0 0 0
  104241. - 0 0 0 0 0 0 0 0 0 0 0 0
  104242. - 0 0 0 0 0 0 0 0 0 0 0 0
  104243. - 0 0 0 0 0 0 0 0 0 0 0 0
  104244. - 0 0 0 0 0 0 0 0 0 0 0 0
  104245. - 0 0 0 0 0 0 0 0 0 0 0 0
  104246. - 0 0 0 0 0 0 0 0 0 0 0 0
  104247. - 0 0 0 0 0 0 0 0 0 0 0 0
  104248. - 0 0 0 0 0 0 0 0 0 0 0 0
  104249. - 0 0 0 0 0 0 0 0 0 0 0 0
  104250. - 0 0 0 0 0 0 0 0 0 6 6 6
  104251. - 6 6 6 10 10 10 14 14 14 18 18 18
  104252. - 18 18 18 14 14 14 10 10 10 6 6 6
  104253. - 0 0 0 0 0 0 0 0 0 0 0 0
  104254. - 0 0 0 0 0 0 0 0 0 0 0 0
  104255. - 0 0 0 0 0 0 0 0 0 0 0 0
  104256. - 0 0 0 0 0 0 0 0 0 0 0 0
  104257. - 0 0 0 0 0 0 0 0 0 6 6 6
  104258. - 14 14 14 18 18 18 22 22 22 22 22 22
  104259. - 18 18 18 14 14 14 10 10 10 6 6 6
  104260. - 0 0 0 0 0 0 0 0 0 0 0 0
  104261. - 0 0 0 0 0 0 0 0 0 0 0 0
  104262. - 0 0 0 0 0 0 0 0 0 0 0 0
  104263. - 0 0 0 0 0 0 0 0 0 0 0 0
  104264. - 0 0 0 0 0 0 0 0 0 0 0 0
  104265. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104266. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104267. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104268. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104269. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104270. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104271. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104272. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104273. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104274. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104275. +0 0 0 0 0 0 0 0 0
  104276. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104277. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104278. +0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0
  104279. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104280. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104281. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104282. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104283. +0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0
  104284. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104285. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104286. +0 0 0 0 0 0 0 0 0
  104287. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104288. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  104289. +10 15 3 2 3 1 12 18 4 42 61 14 19 27 6 11 16 4
  104290. +38 55 13 10 15 3 3 4 1 10 15 3 0 0 0 0 0 0
  104291. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104292. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104293. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 1
  104294. +12 18 4 1 1 0 23 34 8 31 45 11 10 15 3 32 47 11
  104295. +34 49 12 3 4 1 3 4 1 3 4 1 0 0 0 0 0 0
  104296. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104297. +0 0 0 0 0 0 0 0 0
  104298. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104299. +0 0 0 0 0 0 10 15 3 29 42 10 26 37 9 12 18 4
  104300. +55 80 19 81 118 28 55 80 19 92 132 31 106 153 36 69 100 23
  104301. +100 144 34 80 116 27 42 61 14 81 118 28 23 34 8 27 40 9
  104302. +15 21 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104303. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104304. +0 0 0 0 0 0 1 1 0 29 42 10 15 21 5 50 72 17
  104305. +74 107 25 45 64 15 102 148 35 80 116 27 84 121 28 111 160 38
  104306. +69 100 23 65 94 22 81 118 28 29 42 10 17 25 6 29 42 10
  104307. +23 34 8 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  104308. +0 0 0 0 0 0 0 0 0
  104309. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  104310. +15 21 5 15 21 5 34 49 12 101 146 34 111 161 38 97 141 33
  104311. +97 141 33 119 172 41 117 170 40 116 167 40 118 170 40 118 171 40
  104312. +117 169 40 118 170 40 111 160 38 118 170 40 96 138 32 89 128 30
  104313. +81 118 28 11 16 4 10 15 3 1 1 0 0 0 0 0 0 0
  104314. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104315. +3 4 1 3 4 1 34 49 12 101 146 34 79 115 27 111 160 38
  104316. +114 165 39 113 163 39 118 170 40 117 169 40 118 171 40 117 169 40
  104317. +116 167 40 119 172 41 113 163 39 92 132 31 105 151 36 113 163 39
  104318. +75 109 26 19 27 6 16 23 5 11 16 4 0 1 0 0 0 0
  104319. +0 0 0 0 0 0 0 0 0
  104320. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  104321. +80 116 27 106 153 36 105 151 36 114 165 39 118 170 40 118 171 40
  104322. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104323. +117 169 40 117 169 40 117 170 40 117 169 40 118 170 40 118 170 40
  104324. +117 170 40 75 109 26 75 109 26 34 49 12 0 0 0 0 0 0
  104325. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 4 1
  104326. +64 92 22 65 94 22 100 144 34 118 171 40 118 170 40 117 169 40
  104327. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104328. +117 169 40 117 169 40 117 169 40 118 171 41 118 170 40 117 169 40
  104329. +109 158 37 105 151 36 104 150 35 47 69 16 0 0 0 0 0 0
  104330. +0 0 0 0 0 0 0 0 0
  104331. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104332. +42 61 14 115 167 39 118 170 40 117 169 40 117 169 40 117 169 40
  104333. +117 170 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  104334. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104335. +117 169 40 117 169 40 118 170 40 96 138 32 17 25 6 0 0 0
  104336. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47 69 16
  104337. +114 165 39 117 168 40 117 170 40 117 169 40 117 169 40 117 169 40
  104338. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104339. +117 169 40 117 169 40 118 170 40 117 169 40 117 169 40 117 169 40
  104340. +117 170 40 119 172 41 96 138 32 12 18 4 0 0 0 0 0 0
  104341. +0 0 0 0 0 0 0 0 0
  104342. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 15 3
  104343. +32 47 11 105 151 36 118 170 40 117 169 40 117 169 40 116 168 40
  104344. +109 157 37 111 160 38 117 169 40 118 171 40 117 169 40 117 169 40
  104345. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104346. +117 169 40 117 169 40 117 169 40 118 171 40 69 100 23 2 3 1
  104347. +0 0 0 0 0 0 0 0 0 0 0 0 19 27 6 101 146 34
  104348. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104349. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 170 40
  104350. +118 171 40 115 166 39 107 154 36 111 161 38 117 169 40 117 169 40
  104351. +117 169 40 118 171 40 75 109 26 19 27 6 2 3 1 0 0 0
  104352. +0 0 0 0 0 0 0 0 0
  104353. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16 23 5
  104354. +89 128 30 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104355. +111 160 38 92 132 31 79 115 27 96 138 32 115 166 39 119 171 41
  104356. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104357. +117 169 40 117 169 40 117 169 40 118 170 40 109 157 37 26 37 9
  104358. +0 0 0 0 0 0 0 0 0 0 0 0 64 92 22 118 171 40
  104359. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104360. +117 169 40 117 169 40 117 169 40 118 170 40 118 171 40 109 157 37
  104361. +89 128 30 81 118 28 100 144 34 115 166 39 117 169 40 117 169 40
  104362. +117 169 40 117 170 40 113 163 39 60 86 20 1 1 0 0 0 0
  104363. +0 0 0 0 0 0 0 0 0
  104364. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104365. +27 40 9 96 138 32 118 170 40 117 169 40 117 169 40 117 169 40
  104366. +117 170 40 117 169 40 101 146 34 67 96 23 55 80 19 84 121 28
  104367. +113 163 39 119 171 41 117 169 40 117 169 40 117 169 40 117 169 40
  104368. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 65 94 22
  104369. +0 0 0 0 0 0 0 0 0 15 21 5 101 146 34 118 171 40
  104370. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104371. +117 169 40 118 170 40 118 171 40 104 150 35 69 100 23 53 76 18
  104372. +81 118 28 111 160 38 118 170 40 117 169 40 117 169 40 117 169 40
  104373. +117 169 40 114 165 39 69 100 23 10 15 3 0 0 0 0 0 0
  104374. +0 0 0 0 0 0 0 0 0
  104375. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  104376. +31 45 11 77 111 26 117 169 40 117 169 40 117 169 40 117 169 40
  104377. +117 169 40 117 169 40 118 170 40 116 168 40 92 132 31 47 69 16
  104378. +38 55 13 81 118 28 113 163 39 119 171 41 117 169 40 117 169 40
  104379. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 41 92 132 31
  104380. +10 15 3 0 0 0 0 0 0 36 52 12 115 166 39 117 169 40
  104381. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  104382. +118 171 40 102 148 35 64 92 22 34 49 12 65 94 22 106 153 36
  104383. +118 171 40 117 170 40 117 169 40 117 169 40 117 169 40 117 169 40
  104384. +118 170 40 107 154 36 55 80 19 15 21 5 0 0 0 0 0 0
  104385. +0 0 0 0 0 0 0 0 0
  104386. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104387. +29 42 10 101 146 34 118 171 40 117 169 40 117 169 40 117 169 40
  104388. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 113 163 39
  104389. +75 109 26 27 40 9 36 52 12 89 128 30 116 167 40 118 171 40
  104390. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 104 150 35
  104391. +16 23 5 0 0 0 0 0 0 53 76 18 118 171 40 117 169 40
  104392. +117 169 40 117 169 40 117 169 40 117 169 40 119 171 41 109 157 37
  104393. +67 96 23 23 34 8 42 61 14 96 138 32 118 170 40 118 170 40
  104394. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104395. +117 169 40 117 169 40 74 107 25 10 15 3 0 0 0 0 0 0
  104396. +0 0 0 0 0 0 0 0 0
  104397. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104398. +0 0 0 31 45 11 101 146 34 118 170 40 117 169 40 117 169 40
  104399. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104400. +119 171 41 102 148 35 47 69 16 14 20 5 50 72 17 102 148 35
  104401. +118 171 40 117 169 40 117 169 40 117 169 40 118 170 40 102 148 35
  104402. +15 21 5 0 0 0 0 0 0 50 72 17 118 170 40 117 169 40
  104403. +117 169 40 117 169 40 118 170 40 116 167 40 84 121 28 27 40 9
  104404. +19 27 6 74 107 25 114 165 39 118 171 40 117 169 40 117 169 40
  104405. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104406. +117 169 40 75 109 26 10 15 4 0 0 0 0 0 0 0 0 0
  104407. +0 0 0 0 0 0 0 0 0
  104408. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104409. +0 0 0 38 55 13 102 148 35 118 171 40 117 169 40 117 169 40
  104410. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104411. +117 169 40 118 170 40 115 167 39 77 111 26 17 25 6 19 27 6
  104412. +77 111 26 115 166 39 118 170 40 117 169 40 119 172 41 81 118 28
  104413. +3 4 1 0 0 0 0 0 0 27 40 9 111 160 38 118 170 40
  104414. +117 169 40 118 171 40 105 151 36 50 72 17 10 15 3 38 55 13
  104415. +100 144 34 118 171 40 117 169 40 117 169 40 117 169 40 117 169 40
  104416. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104417. +117 169 40 79 115 27 15 21 5 0 0 0 0 0 0 0 0 0
  104418. +0 0 0 0 0 0 0 0 0
  104419. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104420. +0 0 0 10 15 3 64 92 22 111 160 38 117 169 40 117 169 40
  104421. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104422. +117 169 40 117 169 40 117 169 40 118 171 40 96 138 32 32 47 11
  104423. +3 4 1 50 72 17 107 154 36 120 173 41 105 151 36 31 45 11
  104424. +0 0 0 0 0 0 0 0 0 3 4 1 65 94 22 117 169 40
  104425. +118 170 40 89 128 30 26 37 9 3 4 1 60 86 20 111 161 38
  104426. +118 171 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104427. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104428. +97 141 33 36 52 12 1 1 0 0 0 0 0 0 0 0 0 0
  104429. +0 0 0 0 0 0 0 0 0
  104430. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104431. +0 0 0 0 0 0 14 20 5 75 109 26 117 168 40 117 169 40
  104432. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104433. +117 169 40 117 169 40 117 169 40 117 169 40 118 171 40 107 154 36
  104434. +45 64 15 2 3 1 31 45 11 75 109 26 32 47 11 0 1 0
  104435. +0 0 0 0 0 0 0 0 0 0 0 0 10 15 3 55 80 19
  104436. +65 94 22 11 16 4 11 16 4 75 109 26 116 168 40 118 170 40
  104437. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104438. +117 169 40 117 169 40 117 169 40 117 169 40 118 170 40 107 154 36
  104439. +47 69 16 3 4 1 0 0 0 0 0 0 0 0 0 0 0 0
  104440. +0 0 0 0 0 0 0 0 0
  104441. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104442. +0 0 0 0 0 0 12 18 4 69 100 23 111 161 38 118 171 40
  104443. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104444. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 118 170 40
  104445. +111 160 38 50 72 17 2 3 1 2 3 1 0 0 0 0 0 0
  104446. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0
  104447. +1 1 0 12 18 4 81 118 28 118 170 40 117 169 40 117 169 40
  104448. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104449. +117 169 40 117 169 40 117 169 40 117 170 40 118 171 40 101 146 34
  104450. +42 61 14 2 3 1 0 0 0 0 0 0 0 0 0 0 0 0
  104451. +0 0 0 0 0 0 0 0 0
  104452. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104453. +0 0 0 0 0 0 0 0 0 3 4 1 36 52 12 89 128 30
  104454. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104455. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104456. +118 171 41 101 146 34 14 20 5 0 0 0 0 0 0 0 0 0
  104457. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104458. +0 0 0 47 69 16 118 170 40 117 169 40 117 169 40 117 169 40
  104459. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104460. +117 169 40 117 169 40 117 170 40 111 160 38 69 100 23 19 27 6
  104461. +0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104462. +0 0 0 0 0 0 0 0 0
  104463. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104464. +0 0 0 0 0 0 0 0 0 0 0 0 11 16 4 69 100 23
  104465. +115 167 39 119 172 41 117 169 40 117 169 40 117 169 40 117 169 40
  104466. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104467. +119 172 41 75 109 26 3 4 1 0 0 0 0 0 0 0 0 0
  104468. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104469. +0 0 0 23 34 8 106 153 36 118 170 40 117 169 40 117 169 40
  104470. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104471. +117 169 40 118 170 40 119 172 41 105 151 36 42 61 14 2 3 1
  104472. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104473. +0 0 0 0 0 0 0 0 0
  104474. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104475. +0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 15 21 5
  104476. +45 64 15 80 116 27 114 165 39 118 170 40 117 169 40 117 169 40
  104477. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 119 172 41
  104478. +97 141 33 20 30 7 0 0 0 0 0 0 0 0 0 0 0 0
  104479. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104480. +0 0 0 1 1 0 53 76 18 114 165 39 118 171 40 117 169 40
  104481. +117 169 40 117 169 40 117 169 40 117 169 40 117 169 40 117 169 40
  104482. +118 171 40 104 150 35 64 92 22 31 45 11 10 15 3 0 0 0
  104483. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104484. +0 0 0 0 0 0 0 0 0
  104485. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104486. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104487. +0 0 0 36 52 12 97 141 33 109 158 37 113 163 39 116 168 40
  104488. +117 169 40 117 170 40 118 170 40 119 172 41 115 167 39 84 121 28
  104489. +23 34 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104490. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104491. +0 0 0 0 0 0 3 4 1 50 72 17 102 148 35 118 171 40
  104492. +119 171 41 118 170 40 117 169 40 117 169 40 115 166 39 111 161 38
  104493. +109 157 37 79 115 27 12 18 4 0 0 0 0 0 0 0 0 0
  104494. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104495. +0 0 0 0 0 0 0 0 0
  104496. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104497. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104498. +0 0 0 3 4 1 15 21 5 23 34 8 45 64 15 106 153 36
  104499. +116 167 40 111 160 38 101 146 34 79 115 27 42 61 14 10 15 3
  104500. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104501. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104502. +0 0 0 0 0 0 0 0 0 1 1 0 20 30 7 60 86 20
  104503. +89 128 30 106 153 36 113 163 39 117 169 40 84 121 28 29 42 10
  104504. +19 27 6 10 15 3 2 3 1 0 0 0 0 0 0 0 0 0
  104505. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104506. +0 0 0 0 0 0 0 0 0
  104507. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104508. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104509. +0 0 0 0 0 0 0 0 0 0 0 0 16 23 5 38 55 13
  104510. +36 52 12 26 37 9 12 18 4 2 3 1 0 0 0 0 0 0
  104511. +0 0 0 0 0 0 0 0 0 1 0 0 19 2 7 52 5 18
  104512. +78 7 27 88 8 31 81 7 29 56 5 19 25 2 9 3 0 1
  104513. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104514. +3 4 1 19 27 6 31 45 11 38 55 13 32 47 11 3 4 1
  104515. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104516. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104517. +0 0 0 0 0 0 0 0 0
  104518. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104519. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104520. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  104521. +9 0 3 12 1 4 9 0 3 4 0 1 0 0 0 0 0 0
  104522. +0 0 0 0 0 0 28 3 10 99 9 35 156 14 55 182 16 64
  104523. +189 17 66 190 17 67 189 17 66 184 17 65 166 15 58 118 13 41
  104524. +45 4 16 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  104525. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104526. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104527. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104528. +0 0 0 0 0 0 0 0 0
  104529. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104530. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104531. +0 0 0 0 0 0 11 1 4 52 5 18 101 9 35 134 12 47
  104532. +151 14 53 154 14 54 151 14 53 113 10 40 11 1 4 0 0 0
  104533. +3 0 1 67 6 24 159 14 56 190 17 67 190 17 67 188 17 66
  104534. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 191 17 67
  104535. +174 16 61 101 9 35 14 1 5 0 0 0 35 3 12 108 10 38
  104536. +122 11 43 122 11 43 112 10 39 87 8 30 50 5 17 13 1 5
  104537. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104538. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104539. +0 0 0 0 0 0 0 0 0
  104540. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104541. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104542. +3 0 1 56 5 19 141 13 49 182 16 64 191 17 67 191 17 67
  104543. +190 17 67 190 17 67 191 17 67 113 10 40 3 0 1 1 0 0
  104544. +79 7 28 180 16 63 190 17 67 188 17 66 188 17 66 188 17 66
  104545. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104546. +189 17 66 188 17 66 122 11 43 11 1 4 41 4 14 176 16 62
  104547. +191 17 67 191 17 67 191 17 67 190 17 67 181 16 63 146 13 51
  104548. +75 7 26 10 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  104549. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104550. +0 0 0 0 0 0 0 0 0
  104551. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104552. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 1 2
  104553. +90 8 32 178 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  104554. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 41 4 14
  104555. +173 16 61 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104556. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104557. +188 17 66 188 17 66 188 17 66 88 8 31 1 0 0 89 8 31
  104558. +185 17 65 189 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  104559. +186 17 65 124 11 43 25 2 9 0 0 0 0 0 0 0 0 0
  104560. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104561. +0 0 0 0 0 0 0 0 0
  104562. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104563. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 89 8 31
  104564. +184 17 65 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104565. +190 17 67 151 14 53 34 3 12 0 0 0 0 0 0 79 7 28
  104566. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104567. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104568. +188 17 66 188 17 66 191 17 67 146 13 51 9 1 3 7 1 2
  104569. +108 10 38 187 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  104570. +188 17 66 190 17 67 141 13 49 22 2 8 0 0 0 0 0 0
  104571. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104572. +0 0 0 0 0 0 0 0 0
  104573. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104574. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 176 16 62
  104575. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104576. +151 14 53 38 3 13 0 0 0 0 0 0 0 0 0 50 5 17
  104577. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104578. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104579. +188 17 66 188 17 66 191 17 67 141 13 49 7 1 3 0 0 0
  104580. +11 1 4 112 10 39 187 17 66 189 17 66 188 17 66 188 17 66
  104581. +188 17 66 188 17 66 190 17 67 113 10 40 5 0 2 0 0 0
  104582. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104583. +0 0 0 0 0 0 0 0 0
  104584. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104585. +0 0 0 0 0 0 0 0 0 7 1 3 132 12 46 191 17 67
  104586. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 146 13 51
  104587. +35 3 12 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  104588. +101 9 35 185 17 65 190 17 67 188 17 66 188 17 66 188 17 66
  104589. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104590. +188 17 66 190 17 67 180 16 63 67 6 24 0 0 0 0 0 0
  104591. +0 0 0 11 1 4 108 10 38 186 17 65 189 17 66 188 17 66
  104592. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  104593. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104594. +0 0 0 0 0 0 0 0 0
  104595. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104596. +0 0 0 0 0 0 0 0 0 44 4 15 177 16 62 189 17 66
  104597. +188 17 66 188 17 66 189 17 66 189 17 66 134 12 47 28 3 10
  104598. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104599. +8 1 3 79 7 28 159 14 56 188 17 66 191 17 67 190 17 67
  104600. +189 17 66 189 17 66 189 17 66 189 17 66 190 17 67 191 17 67
  104601. +188 17 66 158 14 55 72 7 25 4 0 1 0 0 0 0 0 0
  104602. +0 0 0 0 0 0 8 1 3 95 9 33 182 16 64 189 17 67
  104603. +188 17 66 188 17 66 188 17 66 191 17 67 122 11 43 3 0 1
  104604. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104605. +0 0 0 0 0 0 0 0 0
  104606. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104607. +0 0 0 0 0 0 0 0 0 88 8 31 190 17 67 188 17 66
  104608. +188 17 66 189 17 66 185 17 65 113 10 40 18 2 6 0 0 0
  104609. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104610. +0 0 0 1 0 0 24 2 8 77 7 27 124 11 43 154 14 54
  104611. +168 15 59 173 16 61 173 16 61 168 15 59 154 14 54 124 11 43
  104612. +77 7 27 22 2 8 0 0 0 0 0 0 0 0 0 0 0 0
  104613. +0 0 0 0 0 0 0 0 0 5 0 2 77 7 27 173 16 61
  104614. +190 17 67 188 17 66 188 17 66 190 17 67 164 15 57 23 2 8
  104615. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104616. +0 0 0 0 0 0 0 0 0
  104617. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104618. +0 0 0 0 0 0 1 0 0 118 13 41 191 17 67 188 17 66
  104619. +190 17 67 174 16 61 87 8 30 8 1 3 0 0 0 0 0 0
  104620. +0 0 0 0 0 0 10 1 4 29 3 10 40 4 14 36 3 13
  104621. +18 2 6 2 0 1 0 0 0 0 0 0 3 0 1 14 1 5
  104622. +26 2 9 33 3 11 32 3 11 25 2 9 13 1 5 3 0 1
  104623. +0 0 0 14 1 5 56 5 19 95 9 33 109 10 38 101 9 35
  104624. +77 7 27 35 3 12 5 0 2 0 0 0 1 0 0 56 5 19
  104625. +156 14 55 190 17 67 188 17 66 188 17 66 182 16 64 50 5 17
  104626. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104627. +0 0 0 0 0 0 0 0 0
  104628. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104629. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 189 17 66
  104630. +151 14 53 52 5 18 2 0 1 0 0 0 0 0 0 1 0 0
  104631. +28 3 10 90 8 32 146 13 51 170 15 60 178 16 62 174 16 61
  104632. +158 14 55 112 10 39 40 4 14 1 0 0 0 0 0 0 0 0
  104633. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 1
  104634. +56 5 19 146 13 51 183 17 64 191 17 67 191 17 67 191 17 67
  104635. +188 17 66 173 16 61 122 11 43 41 4 14 1 0 0 0 0 0
  104636. +30 3 10 124 11 43 185 17 65 190 17 67 187 17 66 67 6 24
  104637. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104638. +0 0 0 0 0 0 0 0 0
  104639. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104640. +0 0 0 0 0 0 6 1 2 134 12 47 168 15 59 99 9 35
  104641. +21 2 7 0 0 0 0 0 0 0 0 0 6 1 2 77 7 27
  104642. +162 15 57 190 17 67 191 17 67 189 17 66 189 17 66 189 17 66
  104643. +190 17 67 191 17 67 169 15 59 75 7 26 3 0 1 0 0 0
  104644. +0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 79 7 28
  104645. +178 16 62 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104646. +188 17 66 189 17 66 191 17 67 170 15 60 79 7 28 5 0 2
  104647. +0 0 0 10 1 3 78 7 27 159 14 56 188 17 66 75 7 26
  104648. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104649. +0 0 0 0 0 0 0 0 0
  104650. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104651. +0 0 0 0 0 0 1 0 0 35 3 12 29 3 10 2 0 1
  104652. +0 0 0 0 0 0 0 0 0 9 1 3 101 9 35 183 17 64
  104653. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104654. +188 17 66 188 17 66 190 17 67 178 16 63 67 6 23 0 0 0
  104655. +0 0 0 0 0 0 0 0 0 0 0 0 52 5 18 174 16 61
  104656. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104657. +188 17 66 188 17 66 188 17 66 190 17 67 182 16 64 89 8 31
  104658. +4 0 1 0 0 0 0 0 0 25 2 9 73 7 26 31 3 11
  104659. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104660. +0 0 0 0 0 0 0 0 0
  104661. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104662. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104663. +0 0 0 0 0 0 4 0 1 98 9 34 187 17 66 189 17 66
  104664. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104665. +188 17 66 188 17 66 188 17 66 190 17 67 158 14 55 25 2 9
  104666. +0 0 0 0 0 0 0 0 0 8 1 3 134 12 47 191 17 67
  104667. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104668. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 180 16 63
  104669. +68 6 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104670. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104671. +0 0 0 0 0 0 0 0 0
  104672. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104673. +0 0 0 6 1 2 19 2 7 3 0 1 0 0 0 0 0 0
  104674. +0 0 0 0 0 0 65 6 23 180 16 63 189 17 66 188 17 66
  104675. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104676. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 83 8 29
  104677. +0 0 0 0 0 0 0 0 0 41 4 14 177 16 62 189 17 66
  104678. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104679. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104680. +159 14 56 28 3 10 0 0 0 0 0 0 0 0 0 23 2 8
  104681. +41 4 14 5 0 2 0 0 0 0 0 0 0 0 0 0 0 0
  104682. +0 0 0 0 0 0 0 0 0
  104683. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104684. +23 2 8 113 10 40 159 14 56 65 6 23 0 0 0 0 0 0
  104685. +0 0 0 16 1 6 146 13 51 191 17 67 188 17 66 188 17 66
  104686. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104687. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 132 12 46
  104688. +5 0 2 0 0 0 0 0 0 77 7 27 189 17 66 188 17 66
  104689. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104690. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104691. +190 17 67 98 9 34 0 0 0 0 0 0 12 1 4 134 12 47
  104692. +178 16 63 108 10 38 16 1 6 0 0 0 0 0 0 0 0 0
  104693. +0 0 0 0 0 0 0 0 0
  104694. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 30 3 10
  104695. +141 13 49 190 17 67 191 17 67 134 12 47 6 1 2 0 0 0
  104696. +0 0 0 68 6 24 186 17 65 188 17 66 188 17 66 188 17 66
  104697. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104698. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 156 14 55
  104699. +14 1 5 0 0 0 0 0 0 98 9 34 191 17 67 188 17 66
  104700. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104701. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104702. +190 17 67 156 14 55 19 2 7 0 0 0 47 4 16 181 16 63
  104703. +190 17 67 189 17 66 126 14 44 17 2 6 0 0 0 0 0 0
  104704. +0 0 0 0 0 0 0 0 0
  104705. +0 0 0 0 0 0 0 0 0 0 0 0 16 1 6 134 12 47
  104706. +191 17 67 188 17 66 190 17 67 162 15 57 19 2 7 0 0 0
  104707. +3 0 1 123 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  104708. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104709. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 163 15 57
  104710. +20 2 7 0 0 0 0 0 0 101 9 35 191 17 67 188 17 66
  104711. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104712. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104713. +188 17 66 182 16 64 52 5 18 0 0 0 73 7 26 188 17 66
  104714. +188 17 66 188 17 66 189 17 66 109 10 38 5 0 2 0 0 0
  104715. +0 0 0 0 0 0 0 0 0
  104716. +0 0 0 0 0 0 0 0 0 0 0 0 95 9 33 189 17 66
  104717. +188 17 66 188 17 66 189 17 66 171 15 60 29 3 10 0 0 0
  104718. +16 1 6 156 14 55 190 17 67 188 17 66 188 17 66 188 17 66
  104719. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104720. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 158 14 55
  104721. +17 2 6 0 0 0 0 0 0 85 8 30 190 17 67 188 17 66
  104722. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104723. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104724. +188 17 66 189 17 66 81 7 29 0 0 0 85 8 30 190 17 67
  104725. +188 17 66 188 17 66 189 17 66 180 16 63 56 5 19 0 0 0
  104726. +0 0 0 0 0 0 0 0 0
  104727. +0 0 0 0 0 0 0 0 0 25 2 9 162 15 57 190 17 67
  104728. +188 17 66 188 17 66 189 17 66 173 16 61 31 3 11 0 0 0
  104729. +30 3 10 171 15 60 189 17 66 188 17 66 188 17 66 188 17 66
  104730. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104731. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 141 13 49
  104732. +7 1 2 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  104733. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104734. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104735. +188 17 66 191 17 67 98 9 34 0 0 0 88 8 31 190 17 67
  104736. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 5 0 2
  104737. +0 0 0 0 0 0 0 0 0
  104738. +0 0 0 0 0 0 0 0 0 68 6 24 187 17 66 188 17 66
  104739. +188 17 66 188 17 66 189 17 66 170 15 60 28 3 10 0 0 0
  104740. +34 3 12 174 16 61 189 17 66 188 17 66 188 17 66 188 17 66
  104741. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104742. +188 17 66 188 17 66 188 17 66 188 17 66 191 17 67 101 9 35
  104743. +0 0 0 0 0 0 0 0 0 21 2 7 159 14 56 190 17 67
  104744. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104745. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104746. +188 17 66 191 17 67 98 9 34 0 0 0 81 7 29 189 17 66
  104747. +188 17 66 188 17 66 188 17 66 189 17 66 168 15 59 28 3 10
  104748. +0 0 0 0 0 0 0 0 0
  104749. +0 0 0 0 0 0 0 0 0 109 10 38 191 17 67 188 17 66
  104750. +188 17 66 188 17 66 190 17 67 163 15 57 21 2 7 0 0 0
  104751. +26 2 9 168 15 59 189 17 66 188 17 66 188 17 66 188 17 66
  104752. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104753. +188 17 66 188 17 66 188 17 66 189 17 66 180 16 63 47 4 16
  104754. +0 0 0 0 0 0 0 0 0 0 0 0 108 10 38 190 17 67
  104755. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104756. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104757. +188 17 66 189 17 66 78 7 27 0 0 0 68 6 24 187 17 66
  104758. +188 17 66 188 17 66 188 17 66 188 17 66 183 17 64 56 5 19
  104759. +0 0 0 0 0 0 0 0 0
  104760. +0 0 0 0 0 0 3 0 1 131 12 46 191 17 67 188 17 66
  104761. +188 17 66 188 17 66 190 17 67 151 14 53 12 1 4 0 0 0
  104762. +11 1 4 146 13 51 190 17 67 188 17 66 188 17 66 188 17 66
  104763. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104764. +188 17 66 188 17 66 188 17 66 191 17 67 126 14 44 7 1 2
  104765. +0 0 0 0 0 0 0 0 0 0 0 0 32 3 11 164 15 58
  104766. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104767. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104768. +189 17 66 178 16 62 44 4 15 0 0 0 50 5 17 182 16 64
  104769. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  104770. +0 0 0 0 0 0 0 0 0
  104771. +0 0 0 0 0 0 5 0 2 134 12 47 191 17 67 188 17 66
  104772. +188 17 66 188 17 66 191 17 67 131 12 46 3 0 1 0 0 0
  104773. +0 0 0 101 9 35 190 17 67 188 17 66 188 17 66 188 17 66
  104774. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104775. +188 17 66 188 17 66 190 17 67 170 15 60 44 4 15 0 0 0
  104776. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77 7 27
  104777. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104778. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104779. +191 17 67 134 12 47 9 1 3 0 0 0 31 3 11 171 15 60
  104780. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 72 7 25
  104781. +0 0 0 0 0 0 0 0 0
  104782. +0 0 0 0 0 0 2 0 1 124 11 43 191 17 67 188 17 66
  104783. +188 17 66 188 17 66 191 17 67 101 9 35 0 0 0 0 0 0
  104784. +0 0 0 35 3 12 168 15 59 190 17 67 188 17 66 188 17 66
  104785. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104786. +188 17 66 189 17 66 182 16 64 77 7 27 0 0 0 0 0 0
  104787. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 1 2
  104788. +99 9 35 185 17 65 189 17 66 188 17 66 188 17 66 188 17 66
  104789. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  104790. +177 16 62 56 5 19 0 0 0 0 0 0 13 1 5 151 14 53
  104791. +190 17 67 188 17 66 188 17 66 188 17 66 185 17 65 56 5 19
  104792. +0 0 0 0 0 0 0 0 0
  104793. +0 0 0 0 0 0 0 0 0 99 9 35 191 17 67 188 17 66
  104794. +188 17 66 188 17 66 186 17 65 65 6 23 0 0 0 0 0 0
  104795. +0 0 0 0 0 0 79 7 28 182 16 64 190 17 67 188 17 66
  104796. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104797. +191 17 67 177 16 62 83 8 29 4 0 1 0 0 0 0 0 0
  104798. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104799. +8 1 3 89 8 31 175 16 62 191 17 67 189 17 66 188 17 66
  104800. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 181 16 63
  104801. +85 8 30 3 0 1 0 0 0 0 0 0 1 0 0 118 13 41
  104802. +191 17 67 188 17 66 188 17 66 189 17 66 173 16 61 34 3 12
  104803. +0 0 0 0 0 0 0 0 0
  104804. +0 0 0 0 0 0 0 0 0 56 5 19 183 17 64 188 17 66
  104805. +188 17 66 189 17 66 169 15 59 30 3 10 0 0 0 0 0 0
  104806. +0 0 0 0 0 0 5 0 2 83 8 29 173 16 61 191 17 67
  104807. +190 17 67 189 17 66 189 17 66 190 17 67 191 17 67 187 17 66
  104808. +151 14 53 56 5 19 3 0 1 0 0 0 16 1 6 50 5 17
  104809. +79 7 28 95 9 33 95 9 33 75 7 26 41 4 14 10 1 4
  104810. +0 0 0 2 0 1 50 5 17 132 12 46 178 16 62 190 17 67
  104811. +191 17 67 191 17 67 191 17 67 186 17 65 154 14 54 68 6 24
  104812. +4 0 1 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  104813. +187 17 66 188 17 66 188 17 66 191 17 67 141 13 49 9 1 3
  104814. +0 0 0 0 0 0 0 0 0
  104815. +0 0 0 0 0 0 0 0 0 14 1 5 151 14 53 190 17 67
  104816. +188 17 66 191 17 67 131 12 46 5 0 2 0 0 0 0 0 0
  104817. +0 0 0 0 0 0 0 0 0 2 0 1 44 4 15 113 10 40
  104818. +156 14 55 173 16 61 174 16 61 164 15 58 134 12 47 77 7 27
  104819. +18 2 6 0 0 0 16 1 6 85 8 30 151 14 53 182 16 64
  104820. +189 17 66 191 17 67 190 17 67 188 17 66 177 16 62 141 13 49
  104821. +68 6 24 8 1 3 0 0 0 8 1 3 44 4 15 88 8 31
  104822. +113 10 40 122 11 43 108 10 38 67 6 24 20 2 7 0 0 0
  104823. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 28 3 10
  104824. +166 15 58 190 17 67 188 17 66 187 17 66 79 7 28 0 0 0
  104825. +0 0 0 0 0 0 0 0 0
  104826. +0 0 0 0 0 0 0 0 0 0 0 0 73 7 26 185 17 65
  104827. +189 17 66 184 17 65 65 6 23 0 0 0 0 0 0 0 0 0
  104828. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1
  104829. +17 2 6 32 3 11 34 3 12 22 2 8 6 1 2 0 0 0
  104830. +0 0 0 38 3 13 141 13 49 188 17 66 190 17 67 188 17 66
  104831. +188 17 66 188 17 66 188 17 66 188 17 66 189 17 66 191 17 67
  104832. +184 17 65 122 11 43 21 2 7 0 0 0 0 0 0 0 0 0
  104833. +0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104834. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  104835. +108 10 38 191 17 67 191 17 67 141 13 49 16 1 6 0 0 0
  104836. +0 0 0 0 0 0 0 0 0
  104837. +0 0 0 0 0 0 0 0 0 0 0 0 8 1 3 112 10 39
  104838. +186 17 65 124 11 43 10 1 4 0 0 0 0 0 0 0 0 0
  104839. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104840. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104841. +36 3 13 156 14 55 191 17 67 188 17 66 188 17 66 188 17 66
  104842. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104843. +189 17 66 190 17 67 134 12 47 18 2 6 0 0 0 0 0 0
  104844. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104845. +0 0 0 7 1 2 41 4 14 75 7 26 66 5 23 19 2 7
  104846. +26 2 9 144 13 50 154 14 54 40 4 14 0 0 0 0 0 0
  104847. +0 0 0 0 0 0 0 0 0
  104848. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  104849. +56 5 19 19 2 7 0 0 0 7 1 2 29 3 10 35 3 12
  104850. +19 2 7 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0
  104851. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 13 1 5
  104852. +134 12 47 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104853. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104854. +188 17 66 188 17 66 189 17 67 108 10 38 3 0 1 0 0 0
  104855. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  104856. +40 4 14 124 11 43 177 16 62 188 17 66 187 17 66 144 13 50
  104857. +24 2 8 17 2 6 22 2 8 0 0 0 0 0 0 0 0 0
  104858. +0 0 0 0 0 0 0 0 0
  104859. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104860. +0 0 0 0 0 0 19 2 7 122 11 43 171 15 60 175 16 62
  104861. +159 14 56 112 10 39 40 4 14 2 0 1 0 0 0 0 0 0
  104862. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 72 7 25
  104863. +186 17 65 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104864. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104865. +188 17 66 188 17 66 189 17 66 174 16 61 41 4 14 0 0 0
  104866. +0 0 0 0 0 0 0 0 0 0 0 0 3 0 1 72 7 25
  104867. +168 15 59 191 17 67 189 17 66 188 17 66 188 17 66 190 17 67
  104868. +95 9 33 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104869. +0 0 0 0 0 0 0 0 0
  104870. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104871. +0 0 0 0 0 0 95 9 33 191 17 67 189 17 66 189 17 66
  104872. +190 17 67 191 17 67 171 15 60 90 8 32 12 1 4 0 0 0
  104873. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 132 12 46
  104874. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104875. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104876. +188 17 66 188 17 66 188 17 66 190 17 67 98 9 34 0 0 0
  104877. +0 0 0 0 0 0 0 0 0 5 0 2 88 8 31 180 16 63
  104878. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 191 17 67
  104879. +146 13 51 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  104880. +0 0 0 0 0 0 0 0 0
  104881. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104882. +0 0 0 9 1 3 144 13 50 191 17 67 188 17 66 188 17 66
  104883. +188 17 66 188 17 66 189 17 66 187 17 66 123 11 43 20 2 7
  104884. +0 0 0 0 0 0 0 0 0 0 0 0 21 2 7 163 15 57
  104885. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104886. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104887. +188 17 66 188 17 66 188 17 66 191 17 67 134 12 47 5 0 2
  104888. +0 0 0 0 0 0 3 0 1 88 8 31 182 16 64 189 17 66
  104889. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  104890. +171 15 60 31 3 11 0 0 0 0 0 0 0 0 0 0 0 0
  104891. +0 0 0 0 0 0 0 0 0
  104892. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104893. +0 0 0 20 2 7 162 15 57 190 17 67 188 17 66 188 17 66
  104894. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 132 12 46
  104895. +20 2 7 0 0 0 0 0 0 0 0 0 32 3 11 173 16 61
  104896. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104897. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104898. +188 17 66 188 17 66 188 17 66 190 17 67 151 14 53 12 1 4
  104899. +0 0 0 0 0 0 72 7 25 180 16 63 189 17 66 188 17 66
  104900. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104901. +181 16 63 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  104902. +0 0 0 0 0 0 0 0 0
  104903. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104904. +0 0 0 21 2 7 163 15 57 190 17 67 188 17 66 188 17 66
  104905. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104906. +122 11 43 9 1 3 0 0 0 0 0 0 30 3 10 171 15 60
  104907. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104908. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104909. +188 17 66 188 17 66 188 17 66 190 17 67 146 13 51 10 1 4
  104910. +0 0 0 38 3 13 166 15 58 190 17 67 188 17 66 188 17 66
  104911. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104912. +183 17 64 52 5 18 0 0 0 0 0 0 0 0 0 0 0 0
  104913. +0 0 0 0 0 0 0 0 0
  104914. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104915. +0 0 0 13 1 5 154 14 54 190 17 67 188 17 66 188 17 66
  104916. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104917. +186 17 65 79 7 28 0 0 0 0 0 0 14 1 5 156 14 54
  104918. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104919. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104920. +188 17 66 188 17 66 188 17 66 191 17 67 124 11 43 2 0 1
  104921. +5 0 2 122 11 43 191 17 67 188 17 66 188 17 66 188 17 66
  104922. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104923. +182 16 64 47 4 16 0 0 0 0 0 0 0 0 0 0 0 0
  104924. +0 0 0 0 0 0 0 0 0
  104925. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104926. +0 0 0 3 0 1 126 14 44 191 17 67 188 17 66 188 17 66
  104927. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104928. +190 17 67 158 14 55 23 2 8 0 0 0 1 0 0 113 10 40
  104929. +191 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104930. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104931. +188 17 66 188 17 66 188 17 66 188 17 66 78 7 27 0 0 0
  104932. +47 4 16 177 16 62 189 17 66 188 17 66 188 17 66 188 17 66
  104933. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 189 17 66
  104934. +173 16 61 34 3 12 0 0 0 0 0 0 0 0 0 0 0 0
  104935. +0 0 0 0 0 0 0 0 0
  104936. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104937. +0 0 0 0 0 0 85 8 30 189 17 66 188 17 66 188 17 66
  104938. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104939. +188 17 66 188 17 66 79 7 28 0 0 0 0 0 0 47 4 16
  104940. +175 16 62 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104941. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104942. +188 17 66 188 17 66 190 17 67 156 14 55 22 2 8 0 0 0
  104943. +109 10 38 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104944. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104945. +151 14 53 13 1 5 0 0 0 0 0 0 0 0 0 0 0 0
  104946. +0 0 0 0 0 0 0 0 0
  104947. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104948. +0 0 0 0 0 0 35 3 12 173 16 61 189 17 66 188 17 66
  104949. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104950. +188 17 66 191 17 67 134 12 47 7 1 2 0 0 0 3 0 1
  104951. +99 9 35 188 17 66 189 17 66 188 17 66 188 17 66 188 17 66
  104952. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104953. +188 17 66 189 17 66 181 16 63 68 6 24 0 0 0 18 2 6
  104954. +156 14 55 190 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104955. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 190 17 67
  104956. +101 9 35 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104957. +0 0 0 0 0 0 0 0 0
  104958. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104959. +0 0 0 0 0 0 3 0 1 118 13 41 191 17 67 188 17 66
  104960. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104961. +188 17 66 189 17 66 168 15 59 28 3 10 0 0 0 0 0 0
  104962. +12 1 4 113 10 40 187 17 66 189 17 67 188 17 66 188 17 66
  104963. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104964. +190 17 67 180 16 63 88 8 31 4 0 1 0 0 0 47 4 16
  104965. +180 16 63 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104966. +188 17 66 188 17 66 188 17 66 188 17 66 190 17 67 168 15 59
  104967. +36 3 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104968. +0 0 0 0 0 0 0 0 0
  104969. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104970. +0 0 0 0 0 0 0 0 0 38 3 13 164 15 58 190 17 67
  104971. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104972. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  104973. +0 0 0 11 1 4 90 8 32 169 15 59 190 17 67 190 17 67
  104974. +189 17 66 189 17 66 189 17 66 189 17 66 191 17 67 189 17 66
  104975. +158 14 55 68 6 24 4 0 1 0 0 0 0 0 0 73 7 26
  104976. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104977. +188 17 66 188 17 66 188 17 66 189 17 66 185 17 65 83 8 29
  104978. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104979. +0 0 0 0 0 0 0 0 0
  104980. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104981. +0 0 0 0 0 0 0 0 0 0 0 0 65 6 23 174 16 61
  104982. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104983. +188 17 66 188 17 66 185 17 65 56 5 19 0 0 0 0 0 0
  104984. +0 0 0 0 0 0 2 0 1 35 3 12 99 9 35 146 13 51
  104985. +170 15 60 177 16 62 177 16 62 166 15 58 141 13 49 85 8 30
  104986. +24 2 8 0 0 0 0 0 0 0 0 0 0 0 0 85 8 30
  104987. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104988. +188 17 66 188 17 66 188 17 66 189 17 66 112 10 39 8 1 3
  104989. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104990. +0 0 0 0 0 0 0 0 0
  104991. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  104992. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 68 6 24
  104993. +170 15 60 191 17 67 188 17 66 188 17 66 188 17 66 188 17 66
  104994. +188 17 66 188 17 66 182 16 64 50 5 17 0 0 0 0 0 0
  104995. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 11 1 4
  104996. +28 3 10 40 4 14 38 3 13 25 2 9 8 1 3 0 0 0
  104997. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 78 7 27
  104998. +189 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  104999. +188 17 66 189 17 66 187 17 66 113 10 40 14 1 5 0 0 0
  105000. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105001. +0 0 0 0 0 0 0 0 0
  105002. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105003. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
  105004. +47 4 16 141 13 49 186 17 65 191 17 67 190 17 67 189 17 66
  105005. +189 17 66 191 17 67 156 14 55 20 2 7 0 0 0 0 0 0
  105006. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105007. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105008. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 44 4 15
  105009. +178 16 62 190 17 67 188 17 66 188 17 66 188 17 66 190 17 67
  105010. +191 17 67 173 16 61 90 8 32 10 1 4 0 0 0 0 0 0
  105011. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105012. +0 0 0 0 0 0 0 0 0
  105013. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105014. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105015. +0 0 0 14 1 5 68 6 24 131 12 46 162 15 57 174 16 61
  105016. +171 15 60 146 13 51 56 5 19 0 0 0 0 0 0 0 0 0
  105017. +0 0 0 0 0 0 0 0 0 3 0 1 14 1 5 29 3 10
  105018. +41 4 14 47 4 16 50 5 17 45 4 16 34 3 12 18 2 6
  105019. +5 0 2 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  105020. +90 8 32 169 15 59 185 17 65 187 17 66 182 16 64 163 15 57
  105021. +113 10 40 41 4 14 2 0 1 0 0 0 0 0 0 0 0 0
  105022. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105023. +0 0 0 0 0 0 0 0 0
  105024. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105025. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105026. +0 0 0 0 0 0 0 0 0 5 0 2 21 2 7 34 3 12
  105027. +29 3 10 11 1 4 0 0 0 0 0 0 0 0 0 0 0 0
  105028. +3 0 1 32 3 11 79 7 28 124 11 43 154 14 54 171 15 60
  105029. +180 16 63 182 16 64 182 16 64 180 16 63 174 16 61 159 14 56
  105030. +132 12 46 88 8 31 34 3 12 3 0 1 0 0 0 0 0 0
  105031. +3 0 1 29 3 10 56 5 19 65 6 23 50 5 17 23 2 8
  105032. +3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105033. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105034. +0 0 0 0 0 0 0 0 0
  105035. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105036. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105037. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105038. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 25 2 9
  105039. +109 10 38 169 15 59 189 17 66 191 17 67 190 17 67 189 17 66
  105040. +189 17 66 188 17 66 188 17 66 188 17 66 189 17 66 190 17 67
  105041. +191 17 67 190 17 67 171 15 60 98 9 34 10 1 3 0 0 0
  105042. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105043. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105044. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105045. +0 0 0 0 0 0 0 0 0
  105046. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105047. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105048. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105049. +0 0 0 0 0 0 0 0 0 0 0 0 14 1 5 141 13 49
  105050. +191 17 67 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105051. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105052. +188 17 66 188 17 66 189 17 67 186 17 65 65 6 23 0 0 0
  105053. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105054. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105055. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105056. +0 0 0 0 0 0 0 0 0
  105057. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105058. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105059. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105060. +0 0 0 0 0 0 0 0 0 0 0 0 23 2 8 166 15 58
  105061. +190 17 67 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105062. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105063. +188 17 66 188 17 66 189 17 66 176 16 62 45 4 16 0 0 0
  105064. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105065. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105066. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105067. +0 0 0 0 0 0 0 0 0
  105068. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105069. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105070. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105071. +0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 83 8 29
  105072. +183 17 64 189 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105073. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105074. +188 17 66 189 17 66 185 17 65 95 9 33 3 0 1 0 0 0
  105075. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105076. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105077. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105078. +0 0 0 0 0 0 0 0 0
  105079. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105080. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105081. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105082. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 0 2
  105083. +85 8 30 176 16 62 191 17 67 188 17 66 188 17 66 188 17 66
  105084. +188 17 66 188 17 66 188 17 66 188 17 66 188 17 66 188 17 66
  105085. +191 17 67 180 16 63 95 9 33 7 1 3 0 0 0 0 0 0
  105086. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105087. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105088. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105089. +0 0 0 0 0 0 0 0 0
  105090. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105091. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105092. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105093. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105094. +2 0 1 52 5 18 141 13 49 185 17 65 191 17 67 189 17 67
  105095. +189 17 66 188 17 66 188 17 66 189 17 66 191 17 67 187 17 66
  105096. +146 13 51 56 5 19 4 0 1 0 0 0 0 0 0 0 0 0
  105097. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105098. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105099. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105100. +0 0 0 0 0 0 0 0 0
  105101. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105102. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105103. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105104. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105105. +0 0 0 0 0 0 14 1 5 68 6 24 131 12 46 166 15 58
  105106. +180 16 63 183 17 64 180 16 63 168 15 59 134 12 47 75 7 26
  105107. +17 2 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105108. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105109. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105110. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105111. +0 0 0 0 0 0 0 0 0
  105112. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105113. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105114. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105115. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105116. +0 0 0 0 0 0 0 0 0 0 0 0 5 0 2 24 2 8
  105117. +44 4 15 52 5 18 45 4 16 26 2 9 6 1 2 0 0 0
  105118. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105119. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105120. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105121. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105122. +0 0 0 0 0 0 0 0 0
  105123. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105124. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105125. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105126. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105127. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105128. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105129. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105130. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105131. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105132. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105133. +0 0 0 0 0 0 0 0 0
  105134. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105135. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105136. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105137. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105138. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105139. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105140. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105141. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105142. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105143. +0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  105144. +0 0 0 0 0 0 0 0 0
  105145. diff -Nur linux-3.16.2/drivers/w1/masters/w1-gpio.c linux-3.16-rpi/drivers/w1/masters/w1-gpio.c
  105146. --- linux-3.16.2/drivers/w1/masters/w1-gpio.c 2014-09-06 01:37:11.000000000 +0200
  105147. +++ linux-3.16-rpi/drivers/w1/masters/w1-gpio.c 2014-09-14 19:04:14.000000000 +0200
  105148. @@ -23,6 +23,15 @@
  105149. #include "../w1.h"
  105150. #include "../w1_int.h"
  105151. +static int w1_gpio_pullup = -1;
  105152. +static int w1_gpio_pullup_orig = -1;
  105153. +module_param_named(pullup, w1_gpio_pullup, int, 0);
  105154. +MODULE_PARM_DESC(pullup, "GPIO pin pullup number");
  105155. +static int w1_gpio_pin = -1;
  105156. +static int w1_gpio_pin_orig = -1;
  105157. +module_param_named(gpiopin, w1_gpio_pin, int, 0);
  105158. +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
  105159. +
  105160. static u8 w1_gpio_set_pullup(void *data, int delay)
  105161. {
  105162. struct w1_gpio_platform_data *pdata = data;
  105163. @@ -67,6 +76,16 @@
  105164. return gpio_get_value(pdata->pin) ? 1 : 0;
  105165. }
  105166. +static void w1_gpio_bitbang_pullup(void *data, u8 on)
  105167. +{
  105168. + struct w1_gpio_platform_data *pdata = data;
  105169. +
  105170. + if (on)
  105171. + gpio_direction_output(pdata->pin, 1);
  105172. + else
  105173. + gpio_direction_input(pdata->pin);
  105174. +}
  105175. +
  105176. #if defined(CONFIG_OF)
  105177. static struct of_device_id w1_gpio_dt_ids[] = {
  105178. { .compatible = "w1-gpio" },
  105179. @@ -113,13 +132,15 @@
  105180. static int w1_gpio_probe(struct platform_device *pdev)
  105181. {
  105182. struct w1_bus_master *master;
  105183. - struct w1_gpio_platform_data *pdata;
  105184. + struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
  105185. int err;
  105186. - if (of_have_populated_dt()) {
  105187. - err = w1_gpio_probe_dt(pdev);
  105188. - if (err < 0)
  105189. - return err;
  105190. + if(pdata == NULL) {
  105191. + if (of_have_populated_dt()) {
  105192. + err = w1_gpio_probe_dt(pdev);
  105193. + if (err < 0)
  105194. + return err;
  105195. + }
  105196. }
  105197. pdata = dev_get_platdata(&pdev->dev);
  105198. @@ -136,6 +157,19 @@
  105199. return -ENOMEM;
  105200. }
  105201. + w1_gpio_pin_orig = pdata->pin;
  105202. + w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
  105203. +
  105204. + if(gpio_is_valid(w1_gpio_pin)) {
  105205. + pdata->pin = w1_gpio_pin;
  105206. + pdata->ext_pullup_enable_pin = -1;
  105207. + }
  105208. + if(gpio_is_valid(w1_gpio_pullup)) {
  105209. + pdata->ext_pullup_enable_pin = w1_gpio_pullup;
  105210. + }
  105211. +
  105212. + dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
  105213. +
  105214. err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
  105215. if (err) {
  105216. dev_err(&pdev->dev, "gpio_request (pin) failed\n");
  105217. @@ -165,6 +199,14 @@
  105218. master->set_pullup = w1_gpio_set_pullup;
  105219. }
  105220. + if (gpio_is_valid(w1_gpio_pullup)) {
  105221. + if (pdata->is_open_drain)
  105222. + printk(KERN_ERR "w1-gpio 'pullup' option "
  105223. + "doesn't work with open drain GPIO\n");
  105224. + else
  105225. + master->bitbang_pullup = w1_gpio_bitbang_pullup;
  105226. + }
  105227. +
  105228. err = w1_add_master_device(master);
  105229. if (err) {
  105230. dev_err(&pdev->dev, "w1_add_master device failed\n");
  105231. @@ -195,6 +237,9 @@
  105232. w1_remove_master_device(master);
  105233. + pdata->pin = w1_gpio_pin_orig;
  105234. + pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
  105235. +
  105236. return 0;
  105237. }
  105238. diff -Nur linux-3.16.2/drivers/w1/w1.h linux-3.16-rpi/drivers/w1/w1.h
  105239. --- linux-3.16.2/drivers/w1/w1.h 2014-09-06 01:37:11.000000000 +0200
  105240. +++ linux-3.16-rpi/drivers/w1/w1.h 2014-09-14 19:04:14.000000000 +0200
  105241. @@ -171,6 +171,12 @@
  105242. u8 (*set_pullup)(void *, int);
  105243. + /**
  105244. + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
  105245. + * @return -1=Error, 0=completed
  105246. + */
  105247. + void (*bitbang_pullup) (void *, u8);
  105248. +
  105249. void (*search)(void *, struct w1_master *,
  105250. u8, w1_slave_found_callback);
  105251. };
  105252. diff -Nur linux-3.16.2/drivers/w1/w1_int.c linux-3.16-rpi/drivers/w1/w1_int.c
  105253. --- linux-3.16.2/drivers/w1/w1_int.c 2014-09-06 01:37:11.000000000 +0200
  105254. +++ linux-3.16-rpi/drivers/w1/w1_int.c 2014-09-14 19:04:14.000000000 +0200
  105255. @@ -124,6 +124,20 @@
  105256. return(-EINVAL);
  105257. }
  105258. + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
  105259. + * and takes care of timing itself */
  105260. + if (!master->write_byte && !master->touch_bit && master->set_pullup) {
  105261. + printk(KERN_ERR "w1_add_master_device: set_pullup requires "
  105262. + "write_byte or touch_bit, disabling\n");
  105263. + master->set_pullup = NULL;
  105264. + }
  105265. +
  105266. + if (master->set_pullup && master->bitbang_pullup) {
  105267. + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
  105268. + "be set when bitbang_pullup is used, disabling\n");
  105269. + master->set_pullup = NULL;
  105270. + }
  105271. +
  105272. /* Lock until the device is added (or not) to w1_masters. */
  105273. mutex_lock(&w1_mlock);
  105274. /* Search for the first available id (starting at 1). */
  105275. diff -Nur linux-3.16.2/drivers/w1/w1_io.c linux-3.16-rpi/drivers/w1/w1_io.c
  105276. --- linux-3.16.2/drivers/w1/w1_io.c 2014-09-06 01:37:11.000000000 +0200
  105277. +++ linux-3.16-rpi/drivers/w1/w1_io.c 2014-09-14 19:04:14.000000000 +0200
  105278. @@ -134,10 +134,22 @@
  105279. static void w1_post_write(struct w1_master *dev)
  105280. {
  105281. if (dev->pullup_duration) {
  105282. - if (dev->enable_pullup && dev->bus_master->set_pullup)
  105283. - dev->bus_master->set_pullup(dev->bus_master->data, 0);
  105284. - else
  105285. + if (dev->enable_pullup) {
  105286. + if (dev->bus_master->set_pullup) {
  105287. + dev->bus_master->set_pullup(dev->
  105288. + bus_master->data,
  105289. + 0);
  105290. + } else if (dev->bus_master->bitbang_pullup) {
  105291. + dev->bus_master->
  105292. + bitbang_pullup(dev->bus_master->data, 1);
  105293. msleep(dev->pullup_duration);
  105294. + dev->bus_master->
  105295. + bitbang_pullup(dev->bus_master->data, 0);
  105296. + }
  105297. + } else {
  105298. + msleep(dev->pullup_duration);
  105299. + }
  105300. +
  105301. dev->pullup_duration = 0;
  105302. }
  105303. }
  105304. diff -Nur linux-3.16.2/drivers/watchdog/bcm2708_wdog.c linux-3.16-rpi/drivers/watchdog/bcm2708_wdog.c
  105305. --- linux-3.16.2/drivers/watchdog/bcm2708_wdog.c 1970-01-01 01:00:00.000000000 +0100
  105306. +++ linux-3.16-rpi/drivers/watchdog/bcm2708_wdog.c 2014-09-14 19:04:14.000000000 +0200
  105307. @@ -0,0 +1,382 @@
  105308. +/*
  105309. + * Broadcom BCM2708 watchdog driver.
  105310. + *
  105311. + * (c) Copyright 2010 Broadcom Europe Ltd
  105312. + *
  105313. + * This program is free software; you can redistribute it and/or
  105314. + * modify it under the terms of the GNU General Public License
  105315. + * as published by the Free Software Foundation; either version
  105316. + * 2 of the License, or (at your option) any later version.
  105317. + *
  105318. + * BCM2708 watchdog driver. Loosely based on wdt driver.
  105319. + */
  105320. +
  105321. +#include <linux/interrupt.h>
  105322. +#include <linux/module.h>
  105323. +#include <linux/moduleparam.h>
  105324. +#include <linux/types.h>
  105325. +#include <linux/miscdevice.h>
  105326. +#include <linux/watchdog.h>
  105327. +#include <linux/fs.h>
  105328. +#include <linux/ioport.h>
  105329. +#include <linux/notifier.h>
  105330. +#include <linux/reboot.h>
  105331. +#include <linux/init.h>
  105332. +#include <linux/io.h>
  105333. +#include <linux/uaccess.h>
  105334. +#include <mach/platform.h>
  105335. +
  105336. +#define SECS_TO_WDOG_TICKS(x) ((x) << 16)
  105337. +#define WDOG_TICKS_TO_SECS(x) ((x) >> 16)
  105338. +
  105339. +static unsigned long wdog_is_open;
  105340. +static uint32_t wdog_ticks; /* Ticks to load into wdog timer */
  105341. +static char expect_close;
  105342. +
  105343. +/*
  105344. + * Module parameters
  105345. + */
  105346. +
  105347. +#define WD_TIMO 10 /* Default heartbeat = 60 seconds */
  105348. +static int heartbeat = WD_TIMO; /* Heartbeat in seconds */
  105349. +
  105350. +module_param(heartbeat, int, 0);
  105351. +MODULE_PARM_DESC(heartbeat,
  105352. + "Watchdog heartbeat in seconds. (0 < heartbeat < 65536, default="
  105353. + __MODULE_STRING(WD_TIMO) ")");
  105354. +
  105355. +static int nowayout = WATCHDOG_NOWAYOUT;
  105356. +module_param(nowayout, int, 0);
  105357. +MODULE_PARM_DESC(nowayout,
  105358. + "Watchdog cannot be stopped once started (default="
  105359. + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
  105360. +
  105361. +static DEFINE_SPINLOCK(wdog_lock);
  105362. +
  105363. +/**
  105364. + * Start the watchdog driver.
  105365. + */
  105366. +
  105367. +static int wdog_start(unsigned long timeout)
  105368. +{
  105369. + uint32_t cur;
  105370. + unsigned long flags;
  105371. + spin_lock_irqsave(&wdog_lock, flags);
  105372. +
  105373. + /* enable the watchdog */
  105374. + iowrite32(PM_PASSWORD | (timeout & PM_WDOG_TIME_SET),
  105375. + __io_address(PM_WDOG));
  105376. + cur = ioread32(__io_address(PM_RSTC));
  105377. + iowrite32(PM_PASSWORD | (cur & PM_RSTC_WRCFG_CLR) |
  105378. + PM_RSTC_WRCFG_FULL_RESET, __io_address(PM_RSTC));
  105379. +
  105380. + spin_unlock_irqrestore(&wdog_lock, flags);
  105381. + return 0;
  105382. +}
  105383. +
  105384. +/**
  105385. + * Stop the watchdog driver.
  105386. + */
  105387. +
  105388. +static int wdog_stop(void)
  105389. +{
  105390. + iowrite32(PM_PASSWORD | PM_RSTC_RESET, __io_address(PM_RSTC));
  105391. + printk(KERN_INFO "watchdog stopped\n");
  105392. + return 0;
  105393. +}
  105394. +
  105395. +/**
  105396. + * Reload counter one with the watchdog heartbeat. We don't bother
  105397. + * reloading the cascade counter.
  105398. + */
  105399. +
  105400. +static void wdog_ping(void)
  105401. +{
  105402. + wdog_start(wdog_ticks);
  105403. +}
  105404. +
  105405. +/**
  105406. + * @t: the new heartbeat value that needs to be set.
  105407. + *
  105408. + * Set a new heartbeat value for the watchdog device. If the heartbeat
  105409. + * value is incorrect we keep the old value and return -EINVAL. If
  105410. + * successful we return 0.
  105411. + */
  105412. +
  105413. +static int wdog_set_heartbeat(int t)
  105414. +{
  105415. + if (t < 1 || t > WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET))
  105416. + return -EINVAL;
  105417. +
  105418. + heartbeat = t;
  105419. + wdog_ticks = SECS_TO_WDOG_TICKS(t);
  105420. + return 0;
  105421. +}
  105422. +
  105423. +/**
  105424. + * @file: file handle to the watchdog
  105425. + * @buf: buffer to write (unused as data does not matter here
  105426. + * @count: count of bytes
  105427. + * @ppos: pointer to the position to write. No seeks allowed
  105428. + *
  105429. + * A write to a watchdog device is defined as a keepalive signal.
  105430. + *
  105431. + * if 'nowayout' is set then normally a close() is ignored. But
  105432. + * if you write 'V' first then the close() will stop the timer.
  105433. + */
  105434. +
  105435. +static ssize_t wdog_write(struct file *file, const char __user *buf,
  105436. + size_t count, loff_t *ppos)
  105437. +{
  105438. + if (count) {
  105439. + if (!nowayout) {
  105440. + size_t i;
  105441. +
  105442. + /* In case it was set long ago */
  105443. + expect_close = 0;
  105444. +
  105445. + for (i = 0; i != count; i++) {
  105446. + char c;
  105447. + if (get_user(c, buf + i))
  105448. + return -EFAULT;
  105449. + if (c == 'V')
  105450. + expect_close = 42;
  105451. + }
  105452. + }
  105453. + wdog_ping();
  105454. + }
  105455. + return count;
  105456. +}
  105457. +
  105458. +static int wdog_get_status(void)
  105459. +{
  105460. + unsigned long flags;
  105461. + int status = 0;
  105462. + spin_lock_irqsave(&wdog_lock, flags);
  105463. + /* FIXME: readback reset reason */
  105464. + spin_unlock_irqrestore(&wdog_lock, flags);
  105465. + return status;
  105466. +}
  105467. +
  105468. +static uint32_t wdog_get_remaining(void)
  105469. +{
  105470. + uint32_t ret = ioread32(__io_address(PM_WDOG));
  105471. + return ret & PM_WDOG_TIME_SET;
  105472. +}
  105473. +
  105474. +/**
  105475. + * @file: file handle to the device
  105476. + * @cmd: watchdog command
  105477. + * @arg: argument pointer
  105478. + *
  105479. + * The watchdog API defines a common set of functions for all watchdogs
  105480. + * according to their available features. We only actually usefully support
  105481. + * querying capabilities and current status.
  105482. + */
  105483. +
  105484. +static long wdog_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  105485. +{
  105486. + void __user *argp = (void __user *)arg;
  105487. + int __user *p = argp;
  105488. + int new_heartbeat;
  105489. + int status;
  105490. + int options;
  105491. + uint32_t remaining;
  105492. +
  105493. + struct watchdog_info ident = {
  105494. + .options = WDIOF_SETTIMEOUT|
  105495. + WDIOF_MAGICCLOSE|
  105496. + WDIOF_KEEPALIVEPING,
  105497. + .firmware_version = 1,
  105498. + .identity = "BCM2708",
  105499. + };
  105500. +
  105501. + switch (cmd) {
  105502. + case WDIOC_GETSUPPORT:
  105503. + return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
  105504. + case WDIOC_GETSTATUS:
  105505. + status = wdog_get_status();
  105506. + return put_user(status, p);
  105507. + case WDIOC_GETBOOTSTATUS:
  105508. + return put_user(0, p);
  105509. + case WDIOC_KEEPALIVE:
  105510. + wdog_ping();
  105511. + return 0;
  105512. + case WDIOC_SETTIMEOUT:
  105513. + if (get_user(new_heartbeat, p))
  105514. + return -EFAULT;
  105515. + if (wdog_set_heartbeat(new_heartbeat))
  105516. + return -EINVAL;
  105517. + wdog_ping();
  105518. + /* Fall */
  105519. + case WDIOC_GETTIMEOUT:
  105520. + return put_user(heartbeat, p);
  105521. + case WDIOC_GETTIMELEFT:
  105522. + remaining = WDOG_TICKS_TO_SECS(wdog_get_remaining());
  105523. + return put_user(remaining, p);
  105524. + case WDIOC_SETOPTIONS:
  105525. + if (get_user(options, p))
  105526. + return -EFAULT;
  105527. + if (options & WDIOS_DISABLECARD)
  105528. + wdog_stop();
  105529. + if (options & WDIOS_ENABLECARD)
  105530. + wdog_start(wdog_ticks);
  105531. + return 0;
  105532. + default:
  105533. + return -ENOTTY;
  105534. + }
  105535. +}
  105536. +
  105537. +/**
  105538. + * @inode: inode of device
  105539. + * @file: file handle to device
  105540. + *
  105541. + * The watchdog device has been opened. The watchdog device is single
  105542. + * open and on opening we load the counters.
  105543. + */
  105544. +
  105545. +static int wdog_open(struct inode *inode, struct file *file)
  105546. +{
  105547. + if (test_and_set_bit(0, &wdog_is_open))
  105548. + return -EBUSY;
  105549. + /*
  105550. + * Activate
  105551. + */
  105552. + wdog_start(wdog_ticks);
  105553. + return nonseekable_open(inode, file);
  105554. +}
  105555. +
  105556. +/**
  105557. + * @inode: inode to board
  105558. + * @file: file handle to board
  105559. + *
  105560. + * The watchdog has a configurable API. There is a religious dispute
  105561. + * between people who want their watchdog to be able to shut down and
  105562. + * those who want to be sure if the watchdog manager dies the machine
  105563. + * reboots. In the former case we disable the counters, in the latter
  105564. + * case you have to open it again very soon.
  105565. + */
  105566. +
  105567. +static int wdog_release(struct inode *inode, struct file *file)
  105568. +{
  105569. + if (expect_close == 42) {
  105570. + wdog_stop();
  105571. + } else {
  105572. + printk(KERN_CRIT
  105573. + "wdt: WDT device closed unexpectedly. WDT will not stop!\n");
  105574. + wdog_ping();
  105575. + }
  105576. + clear_bit(0, &wdog_is_open);
  105577. + expect_close = 0;
  105578. + return 0;
  105579. +}
  105580. +
  105581. +/**
  105582. + * @this: our notifier block
  105583. + * @code: the event being reported
  105584. + * @unused: unused
  105585. + *
  105586. + * Our notifier is called on system shutdowns. Turn the watchdog
  105587. + * off so that it does not fire during the next reboot.
  105588. + */
  105589. +
  105590. +static int wdog_notify_sys(struct notifier_block *this, unsigned long code,
  105591. + void *unused)
  105592. +{
  105593. + if (code == SYS_DOWN || code == SYS_HALT)
  105594. + wdog_stop();
  105595. + return NOTIFY_DONE;
  105596. +}
  105597. +
  105598. +/*
  105599. + * Kernel Interfaces
  105600. + */
  105601. +
  105602. +
  105603. +static const struct file_operations wdog_fops = {
  105604. + .owner = THIS_MODULE,
  105605. + .llseek = no_llseek,
  105606. + .write = wdog_write,
  105607. + .unlocked_ioctl = wdog_ioctl,
  105608. + .open = wdog_open,
  105609. + .release = wdog_release,
  105610. +};
  105611. +
  105612. +static struct miscdevice wdog_miscdev = {
  105613. + .minor = WATCHDOG_MINOR,
  105614. + .name = "watchdog",
  105615. + .fops = &wdog_fops,
  105616. +};
  105617. +
  105618. +/*
  105619. + * The WDT card needs to learn about soft shutdowns in order to
  105620. + * turn the timebomb registers off.
  105621. + */
  105622. +
  105623. +static struct notifier_block wdog_notifier = {
  105624. + .notifier_call = wdog_notify_sys,
  105625. +};
  105626. +
  105627. +/**
  105628. + * cleanup_module:
  105629. + *
  105630. + * Unload the watchdog. You cannot do this with any file handles open.
  105631. + * If your watchdog is set to continue ticking on close and you unload
  105632. + * it, well it keeps ticking. We won't get the interrupt but the board
  105633. + * will not touch PC memory so all is fine. You just have to load a new
  105634. + * module in 60 seconds or reboot.
  105635. + */
  105636. +
  105637. +static void __exit wdog_exit(void)
  105638. +{
  105639. + misc_deregister(&wdog_miscdev);
  105640. + unregister_reboot_notifier(&wdog_notifier);
  105641. +}
  105642. +
  105643. +static int __init wdog_init(void)
  105644. +{
  105645. + int ret;
  105646. +
  105647. + /* Check that the heartbeat value is within it's range;
  105648. + if not reset to the default */
  105649. + if (wdog_set_heartbeat(heartbeat)) {
  105650. + wdog_set_heartbeat(WD_TIMO);
  105651. + printk(KERN_INFO "bcm2708_wdog: heartbeat value must be "
  105652. + "0 < heartbeat < %d, using %d\n",
  105653. + WDOG_TICKS_TO_SECS(PM_WDOG_TIME_SET),
  105654. + WD_TIMO);
  105655. + }
  105656. +
  105657. + ret = register_reboot_notifier(&wdog_notifier);
  105658. + if (ret) {
  105659. + printk(KERN_ERR
  105660. + "wdt: cannot register reboot notifier (err=%d)\n", ret);
  105661. + goto out_reboot;
  105662. + }
  105663. +
  105664. + ret = misc_register(&wdog_miscdev);
  105665. + if (ret) {
  105666. + printk(KERN_ERR
  105667. + "wdt: cannot register miscdev on minor=%d (err=%d)\n",
  105668. + WATCHDOG_MINOR, ret);
  105669. + goto out_misc;
  105670. + }
  105671. +
  105672. + printk(KERN_INFO "bcm2708 watchdog, heartbeat=%d sec (nowayout=%d)\n",
  105673. + heartbeat, nowayout);
  105674. + return 0;
  105675. +
  105676. +out_misc:
  105677. + unregister_reboot_notifier(&wdog_notifier);
  105678. +out_reboot:
  105679. + return ret;
  105680. +}
  105681. +
  105682. +module_init(wdog_init);
  105683. +module_exit(wdog_exit);
  105684. +
  105685. +MODULE_AUTHOR("Luke Diamand");
  105686. +MODULE_DESCRIPTION("Driver for BCM2708 watchdog");
  105687. +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
  105688. +MODULE_ALIAS_MISCDEV(TEMP_MINOR);
  105689. +MODULE_LICENSE("GPL");
  105690. diff -Nur linux-3.16.2/drivers/watchdog/Kconfig linux-3.16-rpi/drivers/watchdog/Kconfig
  105691. --- linux-3.16.2/drivers/watchdog/Kconfig 2014-09-06 01:37:11.000000000 +0200
  105692. +++ linux-3.16-rpi/drivers/watchdog/Kconfig 2014-09-14 19:04:14.000000000 +0200
  105693. @@ -413,6 +413,12 @@
  105694. To compile this driver as a module, choose M here: the
  105695. module will be called retu_wdt.
  105696. +config BCM2708_WDT
  105697. + tristate "BCM2708 Watchdog"
  105698. + depends on ARCH_BCM2708
  105699. + help
  105700. + Enables BCM2708 watchdog support.
  105701. +
  105702. config MOXART_WDT
  105703. tristate "MOXART watchdog"
  105704. depends on ARCH_MOXART
  105705. diff -Nur linux-3.16.2/drivers/watchdog/Makefile linux-3.16-rpi/drivers/watchdog/Makefile
  105706. --- linux-3.16.2/drivers/watchdog/Makefile 2014-09-06 01:37:11.000000000 +0200
  105707. +++ linux-3.16-rpi/drivers/watchdog/Makefile 2014-09-14 19:04:14.000000000 +0200
  105708. @@ -54,6 +54,7 @@
  105709. obj-$(CONFIG_IMX2_WDT) += imx2_wdt.o
  105710. obj-$(CONFIG_UX500_WATCHDOG) += ux500_wdt.o
  105711. obj-$(CONFIG_RETU_WATCHDOG) += retu_wdt.o
  105712. +obj-$(CONFIG_BCM2708_WDT) += bcm2708_wdog.o
  105713. obj-$(CONFIG_BCM2835_WDT) += bcm2835_wdt.o
  105714. obj-$(CONFIG_MOXART_WDT) += moxart_wdt.o
  105715. obj-$(CONFIG_SIRFSOC_WATCHDOG) += sirfsoc_wdt.o
  105716. diff -Nur linux-3.16.2/include/linux/broadcom/vc_cma.h linux-3.16-rpi/include/linux/broadcom/vc_cma.h
  105717. --- linux-3.16.2/include/linux/broadcom/vc_cma.h 1970-01-01 01:00:00.000000000 +0100
  105718. +++ linux-3.16-rpi/include/linux/broadcom/vc_cma.h 2014-09-14 19:04:23.000000000 +0200
  105719. @@ -0,0 +1,29 @@
  105720. +/*****************************************************************************
  105721. +* Copyright 2012 Broadcom Corporation. All rights reserved.
  105722. +*
  105723. +* Unless you and Broadcom execute a separate written software license
  105724. +* agreement governing use of this software, this software is licensed to you
  105725. +* under the terms of the GNU General Public License version 2, available at
  105726. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  105727. +*
  105728. +* Notwithstanding the above, under no circumstances may you combine this
  105729. +* software in any way with any other Broadcom software provided under a
  105730. +* license other than the GPL, without Broadcom's express prior written
  105731. +* consent.
  105732. +*****************************************************************************/
  105733. +
  105734. +#if !defined( VC_CMA_H )
  105735. +#define VC_CMA_H
  105736. +
  105737. +#include <linux/ioctl.h>
  105738. +
  105739. +#define VC_CMA_IOC_MAGIC 0xc5
  105740. +
  105741. +#define VC_CMA_IOC_RESERVE _IO(VC_CMA_IOC_MAGIC, 0)
  105742. +
  105743. +#ifdef __KERNEL__
  105744. +extern void __init vc_cma_early_init(void);
  105745. +extern void __init vc_cma_reserve(void);
  105746. +#endif
  105747. +
  105748. +#endif /* VC_CMA_H */
  105749. diff -Nur linux-3.16.2/include/linux/mmc/host.h linux-3.16-rpi/include/linux/mmc/host.h
  105750. --- linux-3.16.2/include/linux/mmc/host.h 2014-09-06 01:37:11.000000000 +0200
  105751. +++ linux-3.16-rpi/include/linux/mmc/host.h 2014-09-14 19:04:23.000000000 +0200
  105752. @@ -283,6 +283,7 @@
  105753. #define MMC_CAP2_HS400 (MMC_CAP2_HS400_1_8V | \
  105754. MMC_CAP2_HS400_1_2V)
  105755. #define MMC_CAP2_SDIO_IRQ_NOTHREAD (1 << 17)
  105756. +#define MMC_CAP2_FORCE_MULTIBLOCK (1 << 31) /* Always use multiblock transfers */
  105757. mmc_pm_flag_t pm_caps; /* supported pm features */
  105758. diff -Nur linux-3.16.2/include/linux/mmc/sdhci.h linux-3.16-rpi/include/linux/mmc/sdhci.h
  105759. --- linux-3.16.2/include/linux/mmc/sdhci.h 2014-09-06 01:37:11.000000000 +0200
  105760. +++ linux-3.16-rpi/include/linux/mmc/sdhci.h 2014-09-14 19:04:23.000000000 +0200
  105761. @@ -131,6 +131,7 @@
  105762. #define SDHCI_SDIO_IRQ_ENABLED (1<<9) /* SDIO irq enabled */
  105763. #define SDHCI_SDR104_NEEDS_TUNING (1<<10) /* SDR104/HS200 needs tuning */
  105764. #define SDHCI_USING_RETUNING_TIMER (1<<11) /* Host is using a retuning timer for the card */
  105765. +#define SDHCI_USE_PLATDMA (1<<12) /* Host uses 3rd party DMA */
  105766. unsigned int version; /* SDHCI spec. version */
  105767. diff -Nur linux-3.16.2/include/linux/vmstat.h linux-3.16-rpi/include/linux/vmstat.h
  105768. --- linux-3.16.2/include/linux/vmstat.h 2014-09-06 01:37:11.000000000 +0200
  105769. +++ linux-3.16-rpi/include/linux/vmstat.h 2014-09-14 19:04:24.000000000 +0200
  105770. @@ -241,7 +241,11 @@
  105771. static inline void __dec_zone_state(struct zone *zone, enum zone_stat_item item)
  105772. {
  105773. atomic_long_dec(&zone->vm_stat[item]);
  105774. + if (item == NR_FILE_DIRTY && unlikely(atomic_long_read(&zone->vm_stat[item]) < 0))
  105775. + atomic_long_set(&zone->vm_stat[item], 0);
  105776. atomic_long_dec(&vm_stat[item]);
  105777. + if (item == NR_FILE_DIRTY && unlikely(atomic_long_read(&vm_stat[item]) < 0))
  105778. + atomic_long_set(&vm_stat[item], 0);
  105779. }
  105780. static inline void __inc_zone_page_state(struct page *page,
  105781. diff -Nur linux-3.16.2/include/uapi/linux/fb.h linux-3.16-rpi/include/uapi/linux/fb.h
  105782. --- linux-3.16.2/include/uapi/linux/fb.h 2014-09-06 01:37:11.000000000 +0200
  105783. +++ linux-3.16-rpi/include/uapi/linux/fb.h 2014-04-13 17:33:21.000000000 +0200
  105784. @@ -34,6 +34,11 @@
  105785. #define FBIOPUT_MODEINFO 0x4617
  105786. #define FBIOGET_DISPINFO 0x4618
  105787. #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
  105788. +/*
  105789. + * HACK: use 'z' in order not to clash with any other ioctl numbers which might
  105790. + * be concurrently added to the mainline kernel
  105791. + */
  105792. +#define FBIOCOPYAREA _IOW('z', 0x21, struct fb_copyarea)
  105793. #define FB_TYPE_PACKED_PIXELS 0 /* Packed Pixels */
  105794. #define FB_TYPE_PLANES 1 /* Non interleaved planes */
  105795. diff -Nur linux-3.16.2/kernel/cgroup.c linux-3.16-rpi/kernel/cgroup.c
  105796. --- linux-3.16.2/kernel/cgroup.c 2014-09-06 01:37:11.000000000 +0200
  105797. +++ linux-3.16-rpi/kernel/cgroup.c 2014-09-14 19:04:24.000000000 +0200
  105798. @@ -5205,6 +5205,29 @@
  105799. }
  105800. __setup("cgroup_disable=", cgroup_disable);
  105801. +static int __init cgroup_enable(char *str)
  105802. +{
  105803. + struct cgroup_subsys *ss;
  105804. + char *token;
  105805. + int i;
  105806. +
  105807. + while ((token = strsep(&str, ",")) != NULL) {
  105808. + if (!*token)
  105809. + continue;
  105810. +
  105811. + for_each_subsys(ss, i) {
  105812. + if (!strcmp(token, ss->name)) {
  105813. + ss->disabled = 0;
  105814. + printk(KERN_INFO "Enabling %s control group"
  105815. + " subsystem\n", ss->name);
  105816. + break;
  105817. + }
  105818. + }
  105819. + }
  105820. + return 1;
  105821. +}
  105822. +__setup("cgroup_enable=", cgroup_enable);
  105823. +
  105824. /**
  105825. * css_tryget_online_from_dir - get corresponding css from a cgroup dentry
  105826. * @dentry: directory dentry of interest
  105827. diff -Nur linux-3.16.2/mm/memcontrol.c linux-3.16-rpi/mm/memcontrol.c
  105828. --- linux-3.16.2/mm/memcontrol.c 2014-09-06 01:37:11.000000000 +0200
  105829. +++ linux-3.16-rpi/mm/memcontrol.c 2014-09-14 19:04:25.000000000 +0200
  105830. @@ -7029,6 +7029,7 @@
  105831. .bind = mem_cgroup_bind,
  105832. .base_cftypes = mem_cgroup_files,
  105833. .early_init = 0,
  105834. + .disabled = 1,
  105835. };
  105836. #ifdef CONFIG_MEMCG_SWAP
  105837. diff -Nur linux-3.16.2/sound/arm/bcm2835.c linux-3.16-rpi/sound/arm/bcm2835.c
  105838. --- linux-3.16.2/sound/arm/bcm2835.c 1970-01-01 01:00:00.000000000 +0100
  105839. +++ linux-3.16-rpi/sound/arm/bcm2835.c 2014-09-14 19:04:33.000000000 +0200
  105840. @@ -0,0 +1,420 @@
  105841. +/*****************************************************************************
  105842. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  105843. +*
  105844. +* Unless you and Broadcom execute a separate written software license
  105845. +* agreement governing use of this software, this software is licensed to you
  105846. +* under the terms of the GNU General Public License version 2, available at
  105847. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  105848. +*
  105849. +* Notwithstanding the above, under no circumstances may you combine this
  105850. +* software in any way with any other Broadcom software provided under a
  105851. +* license other than the GPL, without Broadcom's express prior written
  105852. +* consent.
  105853. +*****************************************************************************/
  105854. +
  105855. +#include <linux/platform_device.h>
  105856. +
  105857. +#include <linux/init.h>
  105858. +#include <linux/slab.h>
  105859. +#include <linux/module.h>
  105860. +
  105861. +#include "bcm2835.h"
  105862. +
  105863. +/* module parameters (see "Module Parameters") */
  105864. +/* SNDRV_CARDS: maximum number of cards supported by this module */
  105865. +static int index[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = -1 };
  105866. +static char *id[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = NULL };
  105867. +static int enable[MAX_SUBSTREAMS] = {[0 ... (MAX_SUBSTREAMS - 1)] = 1 };
  105868. +
  105869. +/* HACKY global pointers needed for successive probes to work : ssp
  105870. + * But compared against the changes we will have to do in VC audio_ipc code
  105871. + * to export 8 audio_ipc devices as a single IPC device and then monitor all
  105872. + * four devices in a thread, this gets things done quickly and should be easier
  105873. + * to debug if we run into issues
  105874. + */
  105875. +
  105876. +static struct snd_card *g_card = NULL;
  105877. +static bcm2835_chip_t *g_chip = NULL;
  105878. +
  105879. +static int snd_bcm2835_free(bcm2835_chip_t * chip)
  105880. +{
  105881. + kfree(chip);
  105882. + return 0;
  105883. +}
  105884. +
  105885. +/* component-destructor
  105886. + * (see "Management of Cards and Components")
  105887. + */
  105888. +static int snd_bcm2835_dev_free(struct snd_device *device)
  105889. +{
  105890. + return snd_bcm2835_free(device->device_data);
  105891. +}
  105892. +
  105893. +/* chip-specific constructor
  105894. + * (see "Management of Cards and Components")
  105895. + */
  105896. +static int snd_bcm2835_create(struct snd_card *card,
  105897. + struct platform_device *pdev,
  105898. + bcm2835_chip_t ** rchip)
  105899. +{
  105900. + bcm2835_chip_t *chip;
  105901. + int err;
  105902. + static struct snd_device_ops ops = {
  105903. + .dev_free = snd_bcm2835_dev_free,
  105904. + };
  105905. +
  105906. + *rchip = NULL;
  105907. +
  105908. + chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  105909. + if (chip == NULL)
  105910. + return -ENOMEM;
  105911. +
  105912. + chip->card = card;
  105913. +
  105914. + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  105915. + if (err < 0) {
  105916. + snd_bcm2835_free(chip);
  105917. + return err;
  105918. + }
  105919. +
  105920. + *rchip = chip;
  105921. + return 0;
  105922. +}
  105923. +
  105924. +static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
  105925. +{
  105926. + static int dev;
  105927. + bcm2835_chip_t *chip;
  105928. + struct snd_card *card;
  105929. + int err;
  105930. +
  105931. + if (dev >= MAX_SUBSTREAMS)
  105932. + return -ENODEV;
  105933. +
  105934. + if (!enable[dev]) {
  105935. + dev++;
  105936. + return -ENOENT;
  105937. + }
  105938. +
  105939. + if (dev > 0)
  105940. + goto add_register_map;
  105941. +
  105942. + err = snd_card_new(NULL, index[dev], id[dev], THIS_MODULE, 0, &g_card);
  105943. + if (err < 0)
  105944. + goto out;
  105945. +
  105946. + snd_card_set_dev(g_card, &pdev->dev);
  105947. + strcpy(g_card->driver, "bcm2835");
  105948. + strcpy(g_card->shortname, "bcm2835 ALSA");
  105949. + sprintf(g_card->longname, "%s", g_card->shortname);
  105950. +
  105951. + err = snd_bcm2835_create(g_card, pdev, &chip);
  105952. + if (err < 0) {
  105953. + dev_err(&pdev->dev, "Failed to create bcm2835 chip\n");
  105954. + goto out_bcm2835_create;
  105955. + }
  105956. +
  105957. + g_chip = chip;
  105958. + err = snd_bcm2835_new_pcm(chip);
  105959. + if (err < 0) {
  105960. + dev_err(&pdev->dev, "Failed to create new BCM2835 pcm device\n");
  105961. + goto out_bcm2835_new_pcm;
  105962. + }
  105963. +
  105964. + err = snd_bcm2835_new_spdif_pcm(chip);
  105965. + if (err < 0) {
  105966. + dev_err(&pdev->dev, "Failed to create new BCM2835 spdif pcm device\n");
  105967. + goto out_bcm2835_new_spdif;
  105968. + }
  105969. +
  105970. + err = snd_bcm2835_new_ctl(chip);
  105971. + if (err < 0) {
  105972. + dev_err(&pdev->dev, "Failed to create new BCM2835 ctl\n");
  105973. + goto out_bcm2835_new_ctl;
  105974. + }
  105975. +
  105976. +add_register_map:
  105977. + card = g_card;
  105978. + chip = g_chip;
  105979. +
  105980. + BUG_ON(!(card && chip));
  105981. +
  105982. + chip->avail_substreams |= (1 << dev);
  105983. + chip->pdev[dev] = pdev;
  105984. +
  105985. + if (dev == 0) {
  105986. + err = snd_card_register(card);
  105987. + if (err < 0) {
  105988. + dev_err(&pdev->dev,
  105989. + "Failed to register bcm2835 ALSA card \n");
  105990. + goto out_card_register;
  105991. + }
  105992. + platform_set_drvdata(pdev, card);
  105993. + audio_info("bcm2835 ALSA card created!\n");
  105994. + } else {
  105995. + audio_info("bcm2835 ALSA chip created!\n");
  105996. + platform_set_drvdata(pdev, (void *)dev);
  105997. + }
  105998. +
  105999. + dev++;
  106000. +
  106001. + return 0;
  106002. +
  106003. +out_card_register:
  106004. +out_bcm2835_new_ctl:
  106005. +out_bcm2835_new_spdif:
  106006. +out_bcm2835_new_pcm:
  106007. +out_bcm2835_create:
  106008. + BUG_ON(!g_card);
  106009. + if (snd_card_free(g_card))
  106010. + dev_err(&pdev->dev, "Failed to free Registered alsa card\n");
  106011. + g_card = NULL;
  106012. +out:
  106013. + dev = SNDRV_CARDS; /* stop more avail_substreams from being probed */
  106014. + dev_err(&pdev->dev, "BCM2835 ALSA Probe failed !!\n");
  106015. + return err;
  106016. +}
  106017. +
  106018. +static int snd_bcm2835_alsa_remove(struct platform_device *pdev)
  106019. +{
  106020. + uint32_t idx;
  106021. + void *drv_data;
  106022. +
  106023. + drv_data = platform_get_drvdata(pdev);
  106024. +
  106025. + if (drv_data == (void *)g_card) {
  106026. + /* This is the card device */
  106027. + snd_card_free((struct snd_card *)drv_data);
  106028. + g_card = NULL;
  106029. + g_chip = NULL;
  106030. + } else {
  106031. + idx = (uint32_t) drv_data;
  106032. + if (g_card != NULL) {
  106033. + BUG_ON(!g_chip);
  106034. + /* We pass chip device numbers in audio ipc devices
  106035. + * other than the one we registered our card with
  106036. + */
  106037. + idx = (uint32_t) drv_data;
  106038. + BUG_ON(!idx || idx > MAX_SUBSTREAMS);
  106039. + g_chip->avail_substreams &= ~(1 << idx);
  106040. + /* There should be atleast one substream registered
  106041. + * after we are done here, as it wil be removed when
  106042. + * the *remove* is called for the card device
  106043. + */
  106044. + BUG_ON(!g_chip->avail_substreams);
  106045. + }
  106046. + }
  106047. +
  106048. + platform_set_drvdata(pdev, NULL);
  106049. +
  106050. + return 0;
  106051. +}
  106052. +
  106053. +#ifdef CONFIG_PM
  106054. +static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
  106055. + pm_message_t state)
  106056. +{
  106057. + return 0;
  106058. +}
  106059. +
  106060. +static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
  106061. +{
  106062. + return 0;
  106063. +}
  106064. +
  106065. +#endif
  106066. +
  106067. +static struct platform_driver bcm2835_alsa0_driver = {
  106068. + .probe = snd_bcm2835_alsa_probe,
  106069. + .remove = snd_bcm2835_alsa_remove,
  106070. +#ifdef CONFIG_PM
  106071. + .suspend = snd_bcm2835_alsa_suspend,
  106072. + .resume = snd_bcm2835_alsa_resume,
  106073. +#endif
  106074. + .driver = {
  106075. + .name = "bcm2835_AUD0",
  106076. + .owner = THIS_MODULE,
  106077. + },
  106078. +};
  106079. +
  106080. +static struct platform_driver bcm2835_alsa1_driver = {
  106081. + .probe = snd_bcm2835_alsa_probe,
  106082. + .remove = snd_bcm2835_alsa_remove,
  106083. +#ifdef CONFIG_PM
  106084. + .suspend = snd_bcm2835_alsa_suspend,
  106085. + .resume = snd_bcm2835_alsa_resume,
  106086. +#endif
  106087. + .driver = {
  106088. + .name = "bcm2835_AUD1",
  106089. + .owner = THIS_MODULE,
  106090. + },
  106091. +};
  106092. +
  106093. +static struct platform_driver bcm2835_alsa2_driver = {
  106094. + .probe = snd_bcm2835_alsa_probe,
  106095. + .remove = snd_bcm2835_alsa_remove,
  106096. +#ifdef CONFIG_PM
  106097. + .suspend = snd_bcm2835_alsa_suspend,
  106098. + .resume = snd_bcm2835_alsa_resume,
  106099. +#endif
  106100. + .driver = {
  106101. + .name = "bcm2835_AUD2",
  106102. + .owner = THIS_MODULE,
  106103. + },
  106104. +};
  106105. +
  106106. +static struct platform_driver bcm2835_alsa3_driver = {
  106107. + .probe = snd_bcm2835_alsa_probe,
  106108. + .remove = snd_bcm2835_alsa_remove,
  106109. +#ifdef CONFIG_PM
  106110. + .suspend = snd_bcm2835_alsa_suspend,
  106111. + .resume = snd_bcm2835_alsa_resume,
  106112. +#endif
  106113. + .driver = {
  106114. + .name = "bcm2835_AUD3",
  106115. + .owner = THIS_MODULE,
  106116. + },
  106117. +};
  106118. +
  106119. +static struct platform_driver bcm2835_alsa4_driver = {
  106120. + .probe = snd_bcm2835_alsa_probe,
  106121. + .remove = snd_bcm2835_alsa_remove,
  106122. +#ifdef CONFIG_PM
  106123. + .suspend = snd_bcm2835_alsa_suspend,
  106124. + .resume = snd_bcm2835_alsa_resume,
  106125. +#endif
  106126. + .driver = {
  106127. + .name = "bcm2835_AUD4",
  106128. + .owner = THIS_MODULE,
  106129. + },
  106130. +};
  106131. +
  106132. +static struct platform_driver bcm2835_alsa5_driver = {
  106133. + .probe = snd_bcm2835_alsa_probe,
  106134. + .remove = snd_bcm2835_alsa_remove,
  106135. +#ifdef CONFIG_PM
  106136. + .suspend = snd_bcm2835_alsa_suspend,
  106137. + .resume = snd_bcm2835_alsa_resume,
  106138. +#endif
  106139. + .driver = {
  106140. + .name = "bcm2835_AUD5",
  106141. + .owner = THIS_MODULE,
  106142. + },
  106143. +};
  106144. +
  106145. +static struct platform_driver bcm2835_alsa6_driver = {
  106146. + .probe = snd_bcm2835_alsa_probe,
  106147. + .remove = snd_bcm2835_alsa_remove,
  106148. +#ifdef CONFIG_PM
  106149. + .suspend = snd_bcm2835_alsa_suspend,
  106150. + .resume = snd_bcm2835_alsa_resume,
  106151. +#endif
  106152. + .driver = {
  106153. + .name = "bcm2835_AUD6",
  106154. + .owner = THIS_MODULE,
  106155. + },
  106156. +};
  106157. +
  106158. +static struct platform_driver bcm2835_alsa7_driver = {
  106159. + .probe = snd_bcm2835_alsa_probe,
  106160. + .remove = snd_bcm2835_alsa_remove,
  106161. +#ifdef CONFIG_PM
  106162. + .suspend = snd_bcm2835_alsa_suspend,
  106163. + .resume = snd_bcm2835_alsa_resume,
  106164. +#endif
  106165. + .driver = {
  106166. + .name = "bcm2835_AUD7",
  106167. + .owner = THIS_MODULE,
  106168. + },
  106169. +};
  106170. +
  106171. +static int bcm2835_alsa_device_init(void)
  106172. +{
  106173. + int err;
  106174. + err = platform_driver_register(&bcm2835_alsa0_driver);
  106175. + if (err) {
  106176. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106177. + goto out;
  106178. + }
  106179. +
  106180. + err = platform_driver_register(&bcm2835_alsa1_driver);
  106181. + if (err) {
  106182. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106183. + goto unregister_0;
  106184. + }
  106185. +
  106186. + err = platform_driver_register(&bcm2835_alsa2_driver);
  106187. + if (err) {
  106188. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106189. + goto unregister_1;
  106190. + }
  106191. +
  106192. + err = platform_driver_register(&bcm2835_alsa3_driver);
  106193. + if (err) {
  106194. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106195. + goto unregister_2;
  106196. + }
  106197. +
  106198. + err = platform_driver_register(&bcm2835_alsa4_driver);
  106199. + if (err) {
  106200. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106201. + goto unregister_3;
  106202. + }
  106203. +
  106204. + err = platform_driver_register(&bcm2835_alsa5_driver);
  106205. + if (err) {
  106206. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106207. + goto unregister_4;
  106208. + }
  106209. +
  106210. + err = platform_driver_register(&bcm2835_alsa6_driver);
  106211. + if (err) {
  106212. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106213. + goto unregister_5;
  106214. + }
  106215. +
  106216. + err = platform_driver_register(&bcm2835_alsa7_driver);
  106217. + if (err) {
  106218. + pr_err("Error registering bcm2835_alsa0_driver %d .\n", err);
  106219. + goto unregister_6;
  106220. + }
  106221. +
  106222. + return 0;
  106223. +
  106224. +unregister_6:
  106225. + platform_driver_unregister(&bcm2835_alsa6_driver);
  106226. +unregister_5:
  106227. + platform_driver_unregister(&bcm2835_alsa5_driver);
  106228. +unregister_4:
  106229. + platform_driver_unregister(&bcm2835_alsa4_driver);
  106230. +unregister_3:
  106231. + platform_driver_unregister(&bcm2835_alsa3_driver);
  106232. +unregister_2:
  106233. + platform_driver_unregister(&bcm2835_alsa2_driver);
  106234. +unregister_1:
  106235. + platform_driver_unregister(&bcm2835_alsa1_driver);
  106236. +unregister_0:
  106237. + platform_driver_unregister(&bcm2835_alsa0_driver);
  106238. +out:
  106239. + return err;
  106240. +}
  106241. +
  106242. +static void bcm2835_alsa_device_exit(void)
  106243. +{
  106244. + platform_driver_unregister(&bcm2835_alsa0_driver);
  106245. + platform_driver_unregister(&bcm2835_alsa1_driver);
  106246. + platform_driver_unregister(&bcm2835_alsa2_driver);
  106247. + platform_driver_unregister(&bcm2835_alsa3_driver);
  106248. + platform_driver_unregister(&bcm2835_alsa4_driver);
  106249. + platform_driver_unregister(&bcm2835_alsa5_driver);
  106250. + platform_driver_unregister(&bcm2835_alsa6_driver);
  106251. + platform_driver_unregister(&bcm2835_alsa7_driver);
  106252. +}
  106253. +
  106254. +late_initcall(bcm2835_alsa_device_init);
  106255. +module_exit(bcm2835_alsa_device_exit);
  106256. +
  106257. +MODULE_AUTHOR("Dom Cobley");
  106258. +MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
  106259. +MODULE_LICENSE("GPL");
  106260. +MODULE_ALIAS("platform:bcm2835_alsa");
  106261. diff -Nur linux-3.16.2/sound/arm/bcm2835-ctl.c linux-3.16-rpi/sound/arm/bcm2835-ctl.c
  106262. --- linux-3.16.2/sound/arm/bcm2835-ctl.c 1970-01-01 01:00:00.000000000 +0100
  106263. +++ linux-3.16-rpi/sound/arm/bcm2835-ctl.c 2014-09-14 19:04:33.000000000 +0200
  106264. @@ -0,0 +1,323 @@
  106265. +/*****************************************************************************
  106266. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  106267. +*
  106268. +* Unless you and Broadcom execute a separate written software license
  106269. +* agreement governing use of this software, this software is licensed to you
  106270. +* under the terms of the GNU General Public License version 2, available at
  106271. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  106272. +*
  106273. +* Notwithstanding the above, under no circumstances may you combine this
  106274. +* software in any way with any other Broadcom software provided under a
  106275. +* license other than the GPL, without Broadcom's express prior written
  106276. +* consent.
  106277. +*****************************************************************************/
  106278. +
  106279. +#include <linux/platform_device.h>
  106280. +#include <linux/init.h>
  106281. +#include <linux/io.h>
  106282. +#include <linux/jiffies.h>
  106283. +#include <linux/slab.h>
  106284. +#include <linux/time.h>
  106285. +#include <linux/wait.h>
  106286. +#include <linux/delay.h>
  106287. +#include <linux/moduleparam.h>
  106288. +#include <linux/sched.h>
  106289. +
  106290. +#include <sound/core.h>
  106291. +#include <sound/control.h>
  106292. +#include <sound/pcm.h>
  106293. +#include <sound/pcm_params.h>
  106294. +#include <sound/rawmidi.h>
  106295. +#include <sound/initval.h>
  106296. +#include <sound/tlv.h>
  106297. +#include <sound/asoundef.h>
  106298. +
  106299. +#include "bcm2835.h"
  106300. +
  106301. +/* volume maximum and minimum in terms of 0.01dB */
  106302. +#define CTRL_VOL_MAX 400
  106303. +#define CTRL_VOL_MIN -10239 /* originally -10240 */
  106304. +
  106305. +
  106306. +static int snd_bcm2835_ctl_info(struct snd_kcontrol *kcontrol,
  106307. + struct snd_ctl_elem_info *uinfo)
  106308. +{
  106309. + audio_info(" ... IN\n");
  106310. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  106311. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  106312. + uinfo->count = 1;
  106313. + uinfo->value.integer.min = CTRL_VOL_MIN;
  106314. + uinfo->value.integer.max = CTRL_VOL_MAX; /* 2303 */
  106315. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  106316. + uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  106317. + uinfo->count = 1;
  106318. + uinfo->value.integer.min = 0;
  106319. + uinfo->value.integer.max = 1;
  106320. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  106321. + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  106322. + uinfo->count = 1;
  106323. + uinfo->value.integer.min = 0;
  106324. + uinfo->value.integer.max = AUDIO_DEST_MAX-1;
  106325. + }
  106326. + audio_info(" ... OUT\n");
  106327. + return 0;
  106328. +}
  106329. +
  106330. +/* toggles mute on or off depending on the value of nmute, and returns
  106331. + * 1 if the mute value was changed, otherwise 0
  106332. + */
  106333. +static int toggle_mute(struct bcm2835_chip *chip, int nmute)
  106334. +{
  106335. + /* if settings are ok, just return 0 */
  106336. + if(chip->mute == nmute)
  106337. + return 0;
  106338. +
  106339. + /* if the sound is muted then we need to unmute */
  106340. + if(chip->mute == CTRL_VOL_MUTE)
  106341. + {
  106342. + chip->volume = chip->old_volume; /* copy the old volume back */
  106343. + audio_info("Unmuting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  106344. + }
  106345. + else /* otherwise we mute */
  106346. + {
  106347. + chip->old_volume = chip->volume;
  106348. + chip->volume = 26214; /* set volume to minimum level AKA mute */
  106349. + audio_info("Muting, old_volume = %d, volume = %d ...\n", chip->old_volume, chip->volume);
  106350. + }
  106351. +
  106352. + chip->mute = nmute;
  106353. + return 1;
  106354. +}
  106355. +
  106356. +static int snd_bcm2835_ctl_get(struct snd_kcontrol *kcontrol,
  106357. + struct snd_ctl_elem_value *ucontrol)
  106358. +{
  106359. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  106360. +
  106361. + BUG_ON(!chip && !(chip->avail_substreams & AVAIL_SUBSTREAMS_MASK));
  106362. +
  106363. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME)
  106364. + ucontrol->value.integer.value[0] = chip2alsa(chip->volume);
  106365. + else if (kcontrol->private_value == PCM_PLAYBACK_MUTE)
  106366. + ucontrol->value.integer.value[0] = chip->mute;
  106367. + else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE)
  106368. + ucontrol->value.integer.value[0] = chip->dest;
  106369. +
  106370. + return 0;
  106371. +}
  106372. +
  106373. +static int snd_bcm2835_ctl_put(struct snd_kcontrol *kcontrol,
  106374. + struct snd_ctl_elem_value *ucontrol)
  106375. +{
  106376. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  106377. + int changed = 0;
  106378. +
  106379. + if (kcontrol->private_value == PCM_PLAYBACK_VOLUME) {
  106380. + audio_info("Volume change attempted.. volume = %d new_volume = %d\n", chip->volume, (int)ucontrol->value.integer.value[0]);
  106381. + if (chip->mute == CTRL_VOL_MUTE) {
  106382. + /* changed = toggle_mute(chip, CTRL_VOL_UNMUTE); */
  106383. + return 1; /* should return 0 to signify no change but the mixer takes this as the opposite sign (no idea why) */
  106384. + }
  106385. + if (changed
  106386. + || (ucontrol->value.integer.value[0] != chip2alsa(chip->volume))) {
  106387. +
  106388. + chip->volume = alsa2chip(ucontrol->value.integer.value[0]);
  106389. + changed = 1;
  106390. + }
  106391. +
  106392. + } else if (kcontrol->private_value == PCM_PLAYBACK_MUTE) {
  106393. + /* Now implemented */
  106394. + audio_info(" Mute attempted\n");
  106395. + changed = toggle_mute(chip, ucontrol->value.integer.value[0]);
  106396. +
  106397. + } else if (kcontrol->private_value == PCM_PLAYBACK_DEVICE) {
  106398. + if (ucontrol->value.integer.value[0] != chip->dest) {
  106399. + chip->dest = ucontrol->value.integer.value[0];
  106400. + changed = 1;
  106401. + }
  106402. + }
  106403. +
  106404. + if (changed) {
  106405. + if (bcm2835_audio_set_ctls(chip))
  106406. + printk(KERN_ERR "Failed to set ALSA controls..\n");
  106407. + }
  106408. +
  106409. + return changed;
  106410. +}
  106411. +
  106412. +static DECLARE_TLV_DB_SCALE(snd_bcm2835_db_scale, CTRL_VOL_MIN, 1, 1);
  106413. +
  106414. +static struct snd_kcontrol_new snd_bcm2835_ctl[] = {
  106415. + {
  106416. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  106417. + .name = "PCM Playback Volume",
  106418. + .index = 0,
  106419. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  106420. + .private_value = PCM_PLAYBACK_VOLUME,
  106421. + .info = snd_bcm2835_ctl_info,
  106422. + .get = snd_bcm2835_ctl_get,
  106423. + .put = snd_bcm2835_ctl_put,
  106424. + .count = 1,
  106425. + .tlv = {.p = snd_bcm2835_db_scale}
  106426. + },
  106427. + {
  106428. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  106429. + .name = "PCM Playback Switch",
  106430. + .index = 0,
  106431. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  106432. + .private_value = PCM_PLAYBACK_MUTE,
  106433. + .info = snd_bcm2835_ctl_info,
  106434. + .get = snd_bcm2835_ctl_get,
  106435. + .put = snd_bcm2835_ctl_put,
  106436. + .count = 1,
  106437. + },
  106438. + {
  106439. + .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  106440. + .name = "PCM Playback Route",
  106441. + .index = 0,
  106442. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  106443. + .private_value = PCM_PLAYBACK_DEVICE,
  106444. + .info = snd_bcm2835_ctl_info,
  106445. + .get = snd_bcm2835_ctl_get,
  106446. + .put = snd_bcm2835_ctl_put,
  106447. + .count = 1,
  106448. + },
  106449. +};
  106450. +
  106451. +static int snd_bcm2835_spdif_default_info(struct snd_kcontrol *kcontrol,
  106452. + struct snd_ctl_elem_info *uinfo)
  106453. +{
  106454. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  106455. + uinfo->count = 1;
  106456. + return 0;
  106457. +}
  106458. +
  106459. +static int snd_bcm2835_spdif_default_get(struct snd_kcontrol *kcontrol,
  106460. + struct snd_ctl_elem_value *ucontrol)
  106461. +{
  106462. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  106463. + int i;
  106464. +
  106465. + for (i = 0; i < 4; i++)
  106466. + ucontrol->value.iec958.status[i] =
  106467. + (chip->spdif_status >> (i * 8)) && 0xff;
  106468. +
  106469. + return 0;
  106470. +}
  106471. +
  106472. +static int snd_bcm2835_spdif_default_put(struct snd_kcontrol *kcontrol,
  106473. + struct snd_ctl_elem_value *ucontrol)
  106474. +{
  106475. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  106476. + unsigned int val = 0;
  106477. + int i, change;
  106478. +
  106479. + for (i = 0; i < 4; i++)
  106480. + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  106481. +
  106482. + change = val != chip->spdif_status;
  106483. + chip->spdif_status = val;
  106484. +
  106485. + return change;
  106486. +}
  106487. +
  106488. +static int snd_bcm2835_spdif_mask_info(struct snd_kcontrol *kcontrol,
  106489. + struct snd_ctl_elem_info *uinfo)
  106490. +{
  106491. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  106492. + uinfo->count = 1;
  106493. + return 0;
  106494. +}
  106495. +
  106496. +static int snd_bcm2835_spdif_mask_get(struct snd_kcontrol *kcontrol,
  106497. + struct snd_ctl_elem_value *ucontrol)
  106498. +{
  106499. + /* bcm2835 supports only consumer mode and sets all other format flags
  106500. + * automatically. So the only thing left is signalling non-audio
  106501. + * content */
  106502. + ucontrol->value.iec958.status[0] = IEC958_AES0_NONAUDIO;
  106503. + return 0;
  106504. +}
  106505. +
  106506. +static int snd_bcm2835_spdif_stream_info(struct snd_kcontrol *kcontrol,
  106507. + struct snd_ctl_elem_info *uinfo)
  106508. +{
  106509. + uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  106510. + uinfo->count = 1;
  106511. + return 0;
  106512. +}
  106513. +
  106514. +static int snd_bcm2835_spdif_stream_get(struct snd_kcontrol *kcontrol,
  106515. + struct snd_ctl_elem_value *ucontrol)
  106516. +{
  106517. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  106518. + int i;
  106519. +
  106520. + for (i = 0; i < 4; i++)
  106521. + ucontrol->value.iec958.status[i] =
  106522. + (chip->spdif_status >> (i * 8)) & 0xff;
  106523. + return 0;
  106524. +}
  106525. +
  106526. +static int snd_bcm2835_spdif_stream_put(struct snd_kcontrol *kcontrol,
  106527. + struct snd_ctl_elem_value *ucontrol)
  106528. +{
  106529. + struct bcm2835_chip *chip = snd_kcontrol_chip(kcontrol);
  106530. + unsigned int val = 0;
  106531. + int i, change;
  106532. +
  106533. + for (i = 0; i < 4; i++)
  106534. + val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  106535. + change = val != chip->spdif_status;
  106536. + chip->spdif_status = val;
  106537. +
  106538. + return change;
  106539. +}
  106540. +
  106541. +static struct snd_kcontrol_new snd_bcm2835_spdif[] = {
  106542. + {
  106543. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  106544. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  106545. + .info = snd_bcm2835_spdif_default_info,
  106546. + .get = snd_bcm2835_spdif_default_get,
  106547. + .put = snd_bcm2835_spdif_default_put
  106548. + },
  106549. + {
  106550. + .access = SNDRV_CTL_ELEM_ACCESS_READ,
  106551. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  106552. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  106553. + .info = snd_bcm2835_spdif_mask_info,
  106554. + .get = snd_bcm2835_spdif_mask_get,
  106555. + },
  106556. + {
  106557. + .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  106558. + SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  106559. + .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  106560. + .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PCM_STREAM),
  106561. + .info = snd_bcm2835_spdif_stream_info,
  106562. + .get = snd_bcm2835_spdif_stream_get,
  106563. + .put = snd_bcm2835_spdif_stream_put,
  106564. + },
  106565. +};
  106566. +
  106567. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip)
  106568. +{
  106569. + int err;
  106570. + unsigned int idx;
  106571. +
  106572. + strcpy(chip->card->mixername, "Broadcom Mixer");
  106573. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_ctl); idx++) {
  106574. + err =
  106575. + snd_ctl_add(chip->card,
  106576. + snd_ctl_new1(&snd_bcm2835_ctl[idx], chip));
  106577. + if (err < 0)
  106578. + return err;
  106579. + }
  106580. + for (idx = 0; idx < ARRAY_SIZE(snd_bcm2835_spdif); idx++) {
  106581. + err = snd_ctl_add(chip->card,
  106582. + snd_ctl_new1(&snd_bcm2835_spdif[idx], chip));
  106583. + if (err < 0)
  106584. + return err;
  106585. + }
  106586. + return 0;
  106587. +}
  106588. diff -Nur linux-3.16.2/sound/arm/bcm2835.h linux-3.16-rpi/sound/arm/bcm2835.h
  106589. --- linux-3.16.2/sound/arm/bcm2835.h 1970-01-01 01:00:00.000000000 +0100
  106590. +++ linux-3.16-rpi/sound/arm/bcm2835.h 2014-09-14 19:04:33.000000000 +0200
  106591. @@ -0,0 +1,167 @@
  106592. +/*****************************************************************************
  106593. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  106594. +*
  106595. +* Unless you and Broadcom execute a separate written software license
  106596. +* agreement governing use of this software, this software is licensed to you
  106597. +* under the terms of the GNU General Public License version 2, available at
  106598. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  106599. +*
  106600. +* Notwithstanding the above, under no circumstances may you combine this
  106601. +* software in any way with any other Broadcom software provided under a
  106602. +* license other than the GPL, without Broadcom's express prior written
  106603. +* consent.
  106604. +*****************************************************************************/
  106605. +
  106606. +#ifndef __SOUND_ARM_BCM2835_H
  106607. +#define __SOUND_ARM_BCM2835_H
  106608. +
  106609. +#include <linux/device.h>
  106610. +#include <linux/list.h>
  106611. +#include <linux/interrupt.h>
  106612. +#include <linux/wait.h>
  106613. +#include <sound/core.h>
  106614. +#include <sound/initval.h>
  106615. +#include <sound/pcm.h>
  106616. +#include <sound/pcm_params.h>
  106617. +#include <sound/pcm-indirect.h>
  106618. +#include <linux/workqueue.h>
  106619. +
  106620. +/*
  106621. +#define AUDIO_DEBUG_ENABLE
  106622. +#define AUDIO_VERBOSE_DEBUG_ENABLE
  106623. +*/
  106624. +
  106625. +/* Debug macros */
  106626. +
  106627. +#ifdef AUDIO_DEBUG_ENABLE
  106628. +#ifdef AUDIO_VERBOSE_DEBUG_ENABLE
  106629. +
  106630. +#define audio_debug(fmt, arg...) \
  106631. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  106632. +
  106633. +#define audio_info(fmt, arg...) \
  106634. + printk(KERN_INFO"%s:%d " fmt, __func__, __LINE__, ##arg)
  106635. +
  106636. +#else
  106637. +
  106638. +#define audio_debug(fmt, arg...)
  106639. +
  106640. +#define audio_info(fmt, arg...)
  106641. +
  106642. +#endif /* AUDIO_VERBOSE_DEBUG_ENABLE */
  106643. +
  106644. +#else
  106645. +
  106646. +#define audio_debug(fmt, arg...)
  106647. +
  106648. +#define audio_info(fmt, arg...)
  106649. +
  106650. +#endif /* AUDIO_DEBUG_ENABLE */
  106651. +
  106652. +#define audio_error(fmt, arg...) \
  106653. + printk(KERN_ERR"%s:%d " fmt, __func__, __LINE__, ##arg)
  106654. +
  106655. +#define audio_warning(fmt, arg...) \
  106656. + printk(KERN_WARNING"%s:%d " fmt, __func__, __LINE__, ##arg)
  106657. +
  106658. +#define audio_alert(fmt, arg...) \
  106659. + printk(KERN_ALERT"%s:%d " fmt, __func__, __LINE__, ##arg)
  106660. +
  106661. +#define MAX_SUBSTREAMS (8)
  106662. +#define AVAIL_SUBSTREAMS_MASK (0xff)
  106663. +enum {
  106664. + CTRL_VOL_MUTE,
  106665. + CTRL_VOL_UNMUTE
  106666. +};
  106667. +
  106668. +/* macros for alsa2chip and chip2alsa, instead of functions */
  106669. +
  106670. +#define alsa2chip(vol) (uint)(-((vol << 8) / 100)) /* convert alsa to chip volume (defined as macro rather than function call) */
  106671. +#define chip2alsa(vol) -((vol * 100) >> 8) /* convert chip to alsa volume */
  106672. +
  106673. +/* Some constants for values .. */
  106674. +typedef enum {
  106675. + AUDIO_DEST_AUTO = 0,
  106676. + AUDIO_DEST_HEADPHONES = 1,
  106677. + AUDIO_DEST_HDMI = 2,
  106678. + AUDIO_DEST_MAX,
  106679. +} SND_BCM2835_ROUTE_T;
  106680. +
  106681. +typedef enum {
  106682. + PCM_PLAYBACK_VOLUME,
  106683. + PCM_PLAYBACK_MUTE,
  106684. + PCM_PLAYBACK_DEVICE,
  106685. +} SND_BCM2835_CTRL_T;
  106686. +
  106687. +/* definition of the chip-specific record */
  106688. +typedef struct bcm2835_chip {
  106689. + struct snd_card *card;
  106690. + struct snd_pcm *pcm;
  106691. + struct snd_pcm *pcm_spdif;
  106692. + /* Bitmat for valid reg_base and irq numbers */
  106693. + uint32_t avail_substreams;
  106694. + struct platform_device *pdev[MAX_SUBSTREAMS];
  106695. + struct bcm2835_alsa_stream *alsa_stream[MAX_SUBSTREAMS];
  106696. +
  106697. + int volume;
  106698. + int old_volume; /* stores the volume value whist muted */
  106699. + int dest;
  106700. + int mute;
  106701. +
  106702. + unsigned int opened;
  106703. + unsigned int spdif_status;
  106704. + struct mutex audio_mutex;
  106705. +} bcm2835_chip_t;
  106706. +
  106707. +typedef struct bcm2835_alsa_stream {
  106708. + bcm2835_chip_t *chip;
  106709. + struct snd_pcm_substream *substream;
  106710. + struct snd_pcm_indirect pcm_indirect;
  106711. +
  106712. + struct semaphore buffers_update_sem;
  106713. + struct semaphore control_sem;
  106714. + spinlock_t lock;
  106715. + volatile uint32_t control;
  106716. + volatile uint32_t status;
  106717. +
  106718. + int open;
  106719. + int running;
  106720. + int draining;
  106721. +
  106722. + int channels;
  106723. + int params_rate;
  106724. + int pcm_format_width;
  106725. +
  106726. + unsigned int pos;
  106727. + unsigned int buffer_size;
  106728. + unsigned int period_size;
  106729. +
  106730. + uint32_t enable_fifo_irq;
  106731. + irq_handler_t fifo_irq_handler;
  106732. +
  106733. + atomic_t retrieved;
  106734. + struct opaque_AUDIO_INSTANCE_T *instance;
  106735. + struct workqueue_struct *my_wq;
  106736. + int idx;
  106737. +} bcm2835_alsa_stream_t;
  106738. +
  106739. +int snd_bcm2835_new_ctl(bcm2835_chip_t * chip);
  106740. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip);
  106741. +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip);
  106742. +
  106743. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream);
  106744. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream);
  106745. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  106746. + uint32_t channels, uint32_t samplerate,
  106747. + uint32_t bps);
  106748. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream);
  106749. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream);
  106750. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream);
  106751. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip);
  106752. +int bcm2835_audio_write(bcm2835_alsa_stream_t * alsa_stream, uint32_t count,
  106753. + void *src);
  106754. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream);
  106755. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream);
  106756. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream);
  106757. +
  106758. +#endif /* __SOUND_ARM_BCM2835_H */
  106759. diff -Nur linux-3.16.2/sound/arm/bcm2835-pcm.c linux-3.16-rpi/sound/arm/bcm2835-pcm.c
  106760. --- linux-3.16.2/sound/arm/bcm2835-pcm.c 1970-01-01 01:00:00.000000000 +0100
  106761. +++ linux-3.16-rpi/sound/arm/bcm2835-pcm.c 2014-09-14 19:04:33.000000000 +0200
  106762. @@ -0,0 +1,547 @@
  106763. +/*****************************************************************************
  106764. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  106765. +*
  106766. +* Unless you and Broadcom execute a separate written software license
  106767. +* agreement governing use of this software, this software is licensed to you
  106768. +* under the terms of the GNU General Public License version 2, available at
  106769. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  106770. +*
  106771. +* Notwithstanding the above, under no circumstances may you combine this
  106772. +* software in any way with any other Broadcom software provided under a
  106773. +* license other than the GPL, without Broadcom's express prior written
  106774. +* consent.
  106775. +*****************************************************************************/
  106776. +
  106777. +#include <linux/interrupt.h>
  106778. +#include <linux/slab.h>
  106779. +
  106780. +#include <sound/asoundef.h>
  106781. +
  106782. +#include "bcm2835.h"
  106783. +
  106784. +/* hardware definition */
  106785. +static struct snd_pcm_hardware snd_bcm2835_playback_hw = {
  106786. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  106787. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  106788. + .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  106789. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  106790. + .rate_min = 8000,
  106791. + .rate_max = 48000,
  106792. + .channels_min = 1,
  106793. + .channels_max = 2,
  106794. + .buffer_bytes_max = 128 * 1024,
  106795. + .period_bytes_min = 1 * 1024,
  106796. + .period_bytes_max = 128 * 1024,
  106797. + .periods_min = 1,
  106798. + .periods_max = 128,
  106799. +};
  106800. +
  106801. +static struct snd_pcm_hardware snd_bcm2835_playback_spdif_hw = {
  106802. + .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  106803. + SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID),
  106804. + .formats = SNDRV_PCM_FMTBIT_S16_LE,
  106805. + .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_44100 |
  106806. + SNDRV_PCM_RATE_48000,
  106807. + .rate_min = 44100,
  106808. + .rate_max = 48000,
  106809. + .channels_min = 2,
  106810. + .channels_max = 2,
  106811. + .buffer_bytes_max = 128 * 1024,
  106812. + .period_bytes_min = 1 * 1024,
  106813. + .period_bytes_max = 128 * 1024,
  106814. + .periods_min = 1,
  106815. + .periods_max = 128,
  106816. +};
  106817. +
  106818. +static void snd_bcm2835_playback_free(struct snd_pcm_runtime *runtime)
  106819. +{
  106820. + audio_info("Freeing up alsa stream here ..\n");
  106821. + if (runtime->private_data)
  106822. + kfree(runtime->private_data);
  106823. + runtime->private_data = NULL;
  106824. +}
  106825. +
  106826. +static irqreturn_t bcm2835_playback_fifo_irq(int irq, void *dev_id)
  106827. +{
  106828. + bcm2835_alsa_stream_t *alsa_stream = (bcm2835_alsa_stream_t *) dev_id;
  106829. + uint32_t consumed = 0;
  106830. + int new_period = 0;
  106831. +
  106832. + audio_info(" .. IN\n");
  106833. +
  106834. + audio_info("alsa_stream=%p substream=%p\n", alsa_stream,
  106835. + alsa_stream ? alsa_stream->substream : 0);
  106836. +
  106837. + if (alsa_stream->open)
  106838. + consumed = bcm2835_audio_retrieve_buffers(alsa_stream);
  106839. +
  106840. + /* We get called only if playback was triggered, So, the number of buffers we retrieve in
  106841. + * each iteration are the buffers that have been played out already
  106842. + */
  106843. +
  106844. + if (alsa_stream->period_size) {
  106845. + if ((alsa_stream->pos / alsa_stream->period_size) !=
  106846. + ((alsa_stream->pos + consumed) / alsa_stream->period_size))
  106847. + new_period = 1;
  106848. + }
  106849. + audio_debug("updating pos cur: %d + %d max:%d period_bytes:%d, hw_ptr: %d new_period:%d\n",
  106850. + alsa_stream->pos,
  106851. + consumed,
  106852. + alsa_stream->buffer_size,
  106853. + (int)(alsa_stream->period_size*alsa_stream->substream->runtime->periods),
  106854. + frames_to_bytes(alsa_stream->substream->runtime, alsa_stream->substream->runtime->status->hw_ptr),
  106855. + new_period);
  106856. + if (alsa_stream->buffer_size) {
  106857. + alsa_stream->pos += consumed &~ (1<<30);
  106858. + alsa_stream->pos %= alsa_stream->buffer_size;
  106859. + }
  106860. +
  106861. + if (alsa_stream->substream) {
  106862. + if (new_period)
  106863. + snd_pcm_period_elapsed(alsa_stream->substream);
  106864. + } else {
  106865. + audio_warning(" unexpected NULL substream\n");
  106866. + }
  106867. + audio_info(" .. OUT\n");
  106868. +
  106869. + return IRQ_HANDLED;
  106870. +}
  106871. +
  106872. +/* open callback */
  106873. +static int snd_bcm2835_playback_open_generic(
  106874. + struct snd_pcm_substream *substream, int spdif)
  106875. +{
  106876. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  106877. + struct snd_pcm_runtime *runtime = substream->runtime;
  106878. + bcm2835_alsa_stream_t *alsa_stream;
  106879. + int idx;
  106880. + int err;
  106881. +
  106882. + audio_info(" .. IN (%d)\n", substream->number);
  106883. +
  106884. + if(mutex_lock_interruptible(&chip->audio_mutex))
  106885. + {
  106886. + audio_error("Interrupted whilst waiting for lock\n");
  106887. + return -EINTR;
  106888. + }
  106889. + audio_info("Alsa open (%d)\n", substream->number);
  106890. + idx = substream->number;
  106891. +
  106892. + if (spdif && chip->opened != 0)
  106893. + return -EBUSY;
  106894. + else if (!spdif && (chip->opened & (1 << idx)))
  106895. + return -EBUSY;
  106896. +
  106897. + if (idx > MAX_SUBSTREAMS) {
  106898. + audio_error
  106899. + ("substream(%d) device doesn't exist max(%d) substreams allowed\n",
  106900. + idx, MAX_SUBSTREAMS);
  106901. + err = -ENODEV;
  106902. + goto out;
  106903. + }
  106904. +
  106905. + /* Check if we are ready */
  106906. + if (!(chip->avail_substreams & (1 << idx))) {
  106907. + /* We are not ready yet */
  106908. + audio_error("substream(%d) device is not ready yet\n", idx);
  106909. + err = -EAGAIN;
  106910. + goto out;
  106911. + }
  106912. +
  106913. + alsa_stream = kzalloc(sizeof(bcm2835_alsa_stream_t), GFP_KERNEL);
  106914. + if (alsa_stream == NULL) {
  106915. + err = -ENOMEM;
  106916. + goto out;
  106917. + }
  106918. +
  106919. + /* Initialise alsa_stream */
  106920. + alsa_stream->chip = chip;
  106921. + alsa_stream->substream = substream;
  106922. + alsa_stream->idx = idx;
  106923. +
  106924. + sema_init(&alsa_stream->buffers_update_sem, 0);
  106925. + sema_init(&alsa_stream->control_sem, 0);
  106926. + spin_lock_init(&alsa_stream->lock);
  106927. +
  106928. + /* Enabled in start trigger, called on each "fifo irq" after that */
  106929. + alsa_stream->enable_fifo_irq = 0;
  106930. + alsa_stream->fifo_irq_handler = bcm2835_playback_fifo_irq;
  106931. +
  106932. + err = bcm2835_audio_open(alsa_stream);
  106933. + if (err != 0) {
  106934. + kfree(alsa_stream);
  106935. + return err;
  106936. + }
  106937. + runtime->private_data = alsa_stream;
  106938. + runtime->private_free = snd_bcm2835_playback_free;
  106939. + if (spdif) {
  106940. + runtime->hw = snd_bcm2835_playback_spdif_hw;
  106941. + } else {
  106942. + /* clear spdif status, as we are not in spdif mode */
  106943. + chip->spdif_status = 0;
  106944. + runtime->hw = snd_bcm2835_playback_hw;
  106945. + }
  106946. + /* minimum 16 bytes alignment (for vchiq bulk transfers) */
  106947. + snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  106948. + 16);
  106949. +
  106950. + chip->alsa_stream[idx] = alsa_stream;
  106951. +
  106952. + chip->opened |= (1 << idx);
  106953. + alsa_stream->open = 1;
  106954. + alsa_stream->draining = 1;
  106955. +
  106956. +out:
  106957. + mutex_unlock(&chip->audio_mutex);
  106958. +
  106959. + audio_info(" .. OUT =%d\n", err);
  106960. +
  106961. + return err;
  106962. +}
  106963. +
  106964. +static int snd_bcm2835_playback_open(struct snd_pcm_substream *substream)
  106965. +{
  106966. + return snd_bcm2835_playback_open_generic(substream, 0);
  106967. +}
  106968. +
  106969. +static int snd_bcm2835_playback_spdif_open(struct snd_pcm_substream *substream)
  106970. +{
  106971. + return snd_bcm2835_playback_open_generic(substream, 1);
  106972. +}
  106973. +
  106974. +/* close callback */
  106975. +static int snd_bcm2835_playback_close(struct snd_pcm_substream *substream)
  106976. +{
  106977. + /* the hardware-specific codes will be here */
  106978. +
  106979. + audio_info(" .. IN\n");
  106980. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  106981. + if(mutex_lock_interruptible(&chip->audio_mutex))
  106982. + {
  106983. + audio_error("Interrupted whilst waiting for lock\n");
  106984. + return -EINTR;
  106985. + }
  106986. + struct snd_pcm_runtime *runtime = substream->runtime;
  106987. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  106988. +
  106989. + audio_info("Alsa close\n");
  106990. +
  106991. + /*
  106992. + * Call stop if it's still running. This happens when app
  106993. + * is force killed and we don't get a stop trigger.
  106994. + */
  106995. + if (alsa_stream->running) {
  106996. + int err;
  106997. + err = bcm2835_audio_stop(alsa_stream);
  106998. + alsa_stream->running = 0;
  106999. + if (err != 0)
  107000. + audio_error(" Failed to STOP alsa device\n");
  107001. + }
  107002. +
  107003. + alsa_stream->period_size = 0;
  107004. + alsa_stream->buffer_size = 0;
  107005. +
  107006. + if (alsa_stream->open) {
  107007. + alsa_stream->open = 0;
  107008. + bcm2835_audio_close(alsa_stream);
  107009. + }
  107010. + if (alsa_stream->chip)
  107011. + alsa_stream->chip->alsa_stream[alsa_stream->idx] = NULL;
  107012. + /*
  107013. + * Do not free up alsa_stream here, it will be freed up by
  107014. + * runtime->private_free callback we registered in *_open above
  107015. + */
  107016. +
  107017. + chip->opened &= ~(1 << substream->number);
  107018. +
  107019. + mutex_unlock(&chip->audio_mutex);
  107020. + audio_info(" .. OUT\n");
  107021. +
  107022. + return 0;
  107023. +}
  107024. +
  107025. +/* hw_params callback */
  107026. +static int snd_bcm2835_pcm_hw_params(struct snd_pcm_substream *substream,
  107027. + struct snd_pcm_hw_params *params)
  107028. +{
  107029. + struct snd_pcm_runtime *runtime = substream->runtime;
  107030. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  107031. + int err;
  107032. +
  107033. + audio_info(" .. IN\n");
  107034. +
  107035. + err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  107036. + if (err < 0) {
  107037. + audio_error
  107038. + (" pcm_lib_malloc failed to allocated pages for buffers\n");
  107039. + return err;
  107040. + }
  107041. +
  107042. + alsa_stream->channels = params_channels(params);
  107043. + alsa_stream->params_rate = params_rate(params);
  107044. + alsa_stream->pcm_format_width = snd_pcm_format_width(params_format (params));
  107045. + audio_info(" .. OUT\n");
  107046. +
  107047. + return err;
  107048. +}
  107049. +
  107050. +/* hw_free callback */
  107051. +static int snd_bcm2835_pcm_hw_free(struct snd_pcm_substream *substream)
  107052. +{
  107053. + audio_info(" .. IN\n");
  107054. + return snd_pcm_lib_free_pages(substream);
  107055. +}
  107056. +
  107057. +/* prepare callback */
  107058. +static int snd_bcm2835_pcm_prepare(struct snd_pcm_substream *substream)
  107059. +{
  107060. + bcm2835_chip_t *chip = snd_pcm_substream_chip(substream);
  107061. + struct snd_pcm_runtime *runtime = substream->runtime;
  107062. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  107063. + int channels;
  107064. + int err;
  107065. +
  107066. + audio_info(" .. IN\n");
  107067. +
  107068. + /* notify the vchiq that it should enter spdif passthrough mode by
  107069. + * setting channels=0 (see
  107070. + * https://github.com/raspberrypi/linux/issues/528) */
  107071. + if (chip->spdif_status & IEC958_AES0_NONAUDIO)
  107072. + channels = 0;
  107073. + else
  107074. + channels = alsa_stream->channels;
  107075. +
  107076. + err = bcm2835_audio_set_params(alsa_stream, channels,
  107077. + alsa_stream->params_rate,
  107078. + alsa_stream->pcm_format_width);
  107079. + if (err < 0) {
  107080. + audio_error(" error setting hw params\n");
  107081. + }
  107082. +
  107083. + bcm2835_audio_setup(alsa_stream);
  107084. +
  107085. + /* in preparation of the stream, set the controls (volume level) of the stream */
  107086. + bcm2835_audio_set_ctls(alsa_stream->chip);
  107087. +
  107088. +
  107089. + memset(&alsa_stream->pcm_indirect, 0, sizeof(alsa_stream->pcm_indirect));
  107090. +
  107091. + alsa_stream->pcm_indirect.hw_buffer_size =
  107092. + alsa_stream->pcm_indirect.sw_buffer_size =
  107093. + snd_pcm_lib_buffer_bytes(substream);
  107094. +
  107095. + alsa_stream->buffer_size = snd_pcm_lib_buffer_bytes(substream);
  107096. + alsa_stream->period_size = snd_pcm_lib_period_bytes(substream);
  107097. + alsa_stream->pos = 0;
  107098. +
  107099. + audio_debug("buffer_size=%d, period_size=%d pos=%d frame_bits=%d\n",
  107100. + alsa_stream->buffer_size, alsa_stream->period_size,
  107101. + alsa_stream->pos, runtime->frame_bits);
  107102. +
  107103. + audio_info(" .. OUT\n");
  107104. + return 0;
  107105. +}
  107106. +
  107107. +static void snd_bcm2835_pcm_transfer(struct snd_pcm_substream *substream,
  107108. + struct snd_pcm_indirect *rec, size_t bytes)
  107109. +{
  107110. + struct snd_pcm_runtime *runtime = substream->runtime;
  107111. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  107112. + void *src = (void *)(substream->runtime->dma_area + rec->sw_data);
  107113. + int err;
  107114. +
  107115. + err = bcm2835_audio_write(alsa_stream, bytes, src);
  107116. + if (err)
  107117. + audio_error(" Failed to transfer to alsa device (%d)\n", err);
  107118. +
  107119. +}
  107120. +
  107121. +static int snd_bcm2835_pcm_ack(struct snd_pcm_substream *substream)
  107122. +{
  107123. + struct snd_pcm_runtime *runtime = substream->runtime;
  107124. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  107125. + struct snd_pcm_indirect *pcm_indirect = &alsa_stream->pcm_indirect;
  107126. +
  107127. + pcm_indirect->hw_queue_size = runtime->hw.buffer_bytes_max;
  107128. + snd_pcm_indirect_playback_transfer(substream, pcm_indirect,
  107129. + snd_bcm2835_pcm_transfer);
  107130. + return 0;
  107131. +}
  107132. +
  107133. +/* trigger callback */
  107134. +static int snd_bcm2835_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  107135. +{
  107136. + struct snd_pcm_runtime *runtime = substream->runtime;
  107137. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  107138. + int err = 0;
  107139. +
  107140. + audio_info(" .. IN\n");
  107141. +
  107142. + switch (cmd) {
  107143. + case SNDRV_PCM_TRIGGER_START:
  107144. + audio_debug("bcm2835_AUDIO_TRIGGER_START running=%d\n",
  107145. + alsa_stream->running);
  107146. + if (!alsa_stream->running) {
  107147. + err = bcm2835_audio_start(alsa_stream);
  107148. + if (err == 0) {
  107149. + alsa_stream->pcm_indirect.hw_io =
  107150. + alsa_stream->pcm_indirect.hw_data =
  107151. + bytes_to_frames(runtime,
  107152. + alsa_stream->pos);
  107153. + substream->ops->ack(substream);
  107154. + alsa_stream->running = 1;
  107155. + alsa_stream->draining = 1;
  107156. + } else {
  107157. + audio_error(" Failed to START alsa device (%d)\n", err);
  107158. + }
  107159. + }
  107160. + break;
  107161. + case SNDRV_PCM_TRIGGER_STOP:
  107162. + audio_debug
  107163. + ("bcm2835_AUDIO_TRIGGER_STOP running=%d draining=%d\n",
  107164. + alsa_stream->running, runtime->status->state == SNDRV_PCM_STATE_DRAINING);
  107165. + if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
  107166. + audio_info("DRAINING\n");
  107167. + alsa_stream->draining = 1;
  107168. + } else {
  107169. + audio_info("DROPPING\n");
  107170. + alsa_stream->draining = 0;
  107171. + }
  107172. + if (alsa_stream->running) {
  107173. + err = bcm2835_audio_stop(alsa_stream);
  107174. + if (err != 0)
  107175. + audio_error(" Failed to STOP alsa device (%d)\n", err);
  107176. + alsa_stream->running = 0;
  107177. + }
  107178. + break;
  107179. + default:
  107180. + err = -EINVAL;
  107181. + }
  107182. +
  107183. + audio_info(" .. OUT\n");
  107184. + return err;
  107185. +}
  107186. +
  107187. +/* pointer callback */
  107188. +static snd_pcm_uframes_t
  107189. +snd_bcm2835_pcm_pointer(struct snd_pcm_substream *substream)
  107190. +{
  107191. + struct snd_pcm_runtime *runtime = substream->runtime;
  107192. + bcm2835_alsa_stream_t *alsa_stream = runtime->private_data;
  107193. +
  107194. + audio_info(" .. IN\n");
  107195. +
  107196. + audio_debug("pcm_pointer... (%d) hwptr=%d appl=%d pos=%d\n", 0,
  107197. + frames_to_bytes(runtime, runtime->status->hw_ptr),
  107198. + frames_to_bytes(runtime, runtime->control->appl_ptr),
  107199. + alsa_stream->pos);
  107200. +
  107201. + audio_info(" .. OUT\n");
  107202. + return snd_pcm_indirect_playback_pointer(substream,
  107203. + &alsa_stream->pcm_indirect,
  107204. + alsa_stream->pos);
  107205. +}
  107206. +
  107207. +static int snd_bcm2835_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  107208. + unsigned int cmd, void *arg)
  107209. +{
  107210. + int ret = snd_pcm_lib_ioctl(substream, cmd, arg);
  107211. + audio_info(" .. substream=%p, cmd=%d, arg=%p (%x) ret=%d\n", substream,
  107212. + cmd, arg, arg ? *(unsigned *)arg : 0, ret);
  107213. + return ret;
  107214. +}
  107215. +
  107216. +/* operators */
  107217. +static struct snd_pcm_ops snd_bcm2835_playback_ops = {
  107218. + .open = snd_bcm2835_playback_open,
  107219. + .close = snd_bcm2835_playback_close,
  107220. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  107221. + .hw_params = snd_bcm2835_pcm_hw_params,
  107222. + .hw_free = snd_bcm2835_pcm_hw_free,
  107223. + .prepare = snd_bcm2835_pcm_prepare,
  107224. + .trigger = snd_bcm2835_pcm_trigger,
  107225. + .pointer = snd_bcm2835_pcm_pointer,
  107226. + .ack = snd_bcm2835_pcm_ack,
  107227. +};
  107228. +
  107229. +static struct snd_pcm_ops snd_bcm2835_playback_spdif_ops = {
  107230. + .open = snd_bcm2835_playback_spdif_open,
  107231. + .close = snd_bcm2835_playback_close,
  107232. + .ioctl = snd_bcm2835_pcm_lib_ioctl,
  107233. + .hw_params = snd_bcm2835_pcm_hw_params,
  107234. + .hw_free = snd_bcm2835_pcm_hw_free,
  107235. + .prepare = snd_bcm2835_pcm_prepare,
  107236. + .trigger = snd_bcm2835_pcm_trigger,
  107237. + .pointer = snd_bcm2835_pcm_pointer,
  107238. + .ack = snd_bcm2835_pcm_ack,
  107239. +};
  107240. +
  107241. +/* create a pcm device */
  107242. +int snd_bcm2835_new_pcm(bcm2835_chip_t * chip)
  107243. +{
  107244. + struct snd_pcm *pcm;
  107245. + int err;
  107246. +
  107247. + audio_info(" .. IN\n");
  107248. + mutex_init(&chip->audio_mutex);
  107249. + if(mutex_lock_interruptible(&chip->audio_mutex))
  107250. + {
  107251. + audio_error("Interrupted whilst waiting for lock\n");
  107252. + return -EINTR;
  107253. + }
  107254. + err =
  107255. + snd_pcm_new(chip->card, "bcm2835 ALSA", 0, MAX_SUBSTREAMS, 0, &pcm);
  107256. + if (err < 0)
  107257. + return err;
  107258. + pcm->private_data = chip;
  107259. + strcpy(pcm->name, "bcm2835 ALSA");
  107260. + chip->pcm = pcm;
  107261. + chip->dest = AUDIO_DEST_AUTO;
  107262. + chip->volume = alsa2chip(0);
  107263. + chip->mute = CTRL_VOL_UNMUTE; /*disable mute on startup */
  107264. + /* set operators */
  107265. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  107266. + &snd_bcm2835_playback_ops);
  107267. +
  107268. + /* pre-allocation of buffers */
  107269. + /* NOTE: this may fail */
  107270. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  107271. + snd_dma_continuous_data
  107272. + (GFP_KERNEL), 64 * 1024,
  107273. + 64 * 1024);
  107274. +
  107275. + mutex_unlock(&chip->audio_mutex);
  107276. + audio_info(" .. OUT\n");
  107277. +
  107278. + return 0;
  107279. +}
  107280. +
  107281. +int snd_bcm2835_new_spdif_pcm(bcm2835_chip_t * chip)
  107282. +{
  107283. + struct snd_pcm *pcm;
  107284. + int err;
  107285. +
  107286. + audio_info(" .. IN\n");
  107287. + if(mutex_lock_interruptible(&chip->audio_mutex))
  107288. + {
  107289. + audio_error("Interrupted whilst waiting for lock\n");
  107290. + return -EINTR;
  107291. + }
  107292. + err = snd_pcm_new(chip->card, "bcm2835 ALSA", 1, 1, 0, &pcm);
  107293. + if (err < 0)
  107294. + return err;
  107295. +
  107296. + pcm->private_data = chip;
  107297. + strcpy(pcm->name, "bcm2835 IEC958/HDMI");
  107298. + chip->pcm_spdif = pcm;
  107299. + snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  107300. + &snd_bcm2835_playback_spdif_ops);
  107301. +
  107302. + snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  107303. + snd_dma_continuous_data (GFP_KERNEL),
  107304. + 64 * 1024, 64 * 1024);
  107305. + mutex_unlock(&chip->audio_mutex);
  107306. + audio_info(" .. OUT\n");
  107307. +
  107308. + return 0;
  107309. +}
  107310. diff -Nur linux-3.16.2/sound/arm/bcm2835-vchiq.c linux-3.16-rpi/sound/arm/bcm2835-vchiq.c
  107311. --- linux-3.16.2/sound/arm/bcm2835-vchiq.c 1970-01-01 01:00:00.000000000 +0100
  107312. +++ linux-3.16-rpi/sound/arm/bcm2835-vchiq.c 2014-09-14 19:04:33.000000000 +0200
  107313. @@ -0,0 +1,902 @@
  107314. +/*****************************************************************************
  107315. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  107316. +*
  107317. +* Unless you and Broadcom execute a separate written software license
  107318. +* agreement governing use of this software, this software is licensed to you
  107319. +* under the terms of the GNU General Public License version 2, available at
  107320. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  107321. +*
  107322. +* Notwithstanding the above, under no circumstances may you combine this
  107323. +* software in any way with any other Broadcom software provided under a
  107324. +* license other than the GPL, without Broadcom's express prior written
  107325. +* consent.
  107326. +*****************************************************************************/
  107327. +
  107328. +#include <linux/device.h>
  107329. +#include <sound/core.h>
  107330. +#include <sound/initval.h>
  107331. +#include <sound/pcm.h>
  107332. +#include <linux/io.h>
  107333. +#include <linux/interrupt.h>
  107334. +#include <linux/fs.h>
  107335. +#include <linux/file.h>
  107336. +#include <linux/mm.h>
  107337. +#include <linux/syscalls.h>
  107338. +#include <asm/uaccess.h>
  107339. +#include <linux/slab.h>
  107340. +#include <linux/delay.h>
  107341. +#include <linux/atomic.h>
  107342. +#include <linux/module.h>
  107343. +#include <linux/completion.h>
  107344. +
  107345. +#include "bcm2835.h"
  107346. +
  107347. +/* ---- Include Files -------------------------------------------------------- */
  107348. +
  107349. +#include "interface/vchi/vchi.h"
  107350. +#include "vc_vchi_audioserv_defs.h"
  107351. +
  107352. +/* ---- Private Constants and Types ------------------------------------------ */
  107353. +
  107354. +#define BCM2835_AUDIO_STOP 0
  107355. +#define BCM2835_AUDIO_START 1
  107356. +#define BCM2835_AUDIO_WRITE 2
  107357. +
  107358. +/* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
  107359. +#ifdef AUDIO_DEBUG_ENABLE
  107360. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  107361. + #define LOG_WARN( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  107362. + #define LOG_INFO( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  107363. + #define LOG_DBG( fmt, arg... ) pr_info( "%s:%d " fmt, __func__, __LINE__, ##arg)
  107364. +#else
  107365. + #define LOG_ERR( fmt, arg... ) pr_err( "%s:%d " fmt, __func__, __LINE__, ##arg)
  107366. + #define LOG_WARN( fmt, arg... )
  107367. + #define LOG_INFO( fmt, arg... )
  107368. + #define LOG_DBG( fmt, arg... )
  107369. +#endif
  107370. +
  107371. +typedef struct opaque_AUDIO_INSTANCE_T {
  107372. + uint32_t num_connections;
  107373. + VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
  107374. + struct completion msg_avail_comp;
  107375. + struct mutex vchi_mutex;
  107376. + bcm2835_alsa_stream_t *alsa_stream;
  107377. + int32_t result;
  107378. + short peer_version;
  107379. +} AUDIO_INSTANCE_T;
  107380. +
  107381. +bool force_bulk = false;
  107382. +
  107383. +/* ---- Private Variables ---------------------------------------------------- */
  107384. +
  107385. +/* ---- Private Function Prototypes ------------------------------------------ */
  107386. +
  107387. +/* ---- Private Functions ---------------------------------------------------- */
  107388. +
  107389. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream);
  107390. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream);
  107391. +static int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  107392. + uint32_t count, void *src);
  107393. +
  107394. +typedef struct {
  107395. + struct work_struct my_work;
  107396. + bcm2835_alsa_stream_t *alsa_stream;
  107397. + int cmd;
  107398. + void *src;
  107399. + uint32_t count;
  107400. +} my_work_t;
  107401. +
  107402. +static void my_wq_function(struct work_struct *work)
  107403. +{
  107404. + my_work_t *w = (my_work_t *) work;
  107405. + int ret = -9;
  107406. + LOG_DBG(" .. IN %p:%d\n", w->alsa_stream, w->cmd);
  107407. + switch (w->cmd) {
  107408. + case BCM2835_AUDIO_START:
  107409. + ret = bcm2835_audio_start_worker(w->alsa_stream);
  107410. + break;
  107411. + case BCM2835_AUDIO_STOP:
  107412. + ret = bcm2835_audio_stop_worker(w->alsa_stream);
  107413. + break;
  107414. + case BCM2835_AUDIO_WRITE:
  107415. + ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
  107416. + w->src);
  107417. + break;
  107418. + default:
  107419. + LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
  107420. + break;
  107421. + }
  107422. + kfree((void *)work);
  107423. + LOG_DBG(" .. OUT %d\n", ret);
  107424. +}
  107425. +
  107426. +int bcm2835_audio_start(bcm2835_alsa_stream_t * alsa_stream)
  107427. +{
  107428. + int ret = -1;
  107429. + LOG_DBG(" .. IN\n");
  107430. + if (alsa_stream->my_wq) {
  107431. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  107432. + /*--- Queue some work (item 1) ---*/
  107433. + if (work) {
  107434. + INIT_WORK((struct work_struct *)work, my_wq_function);
  107435. + work->alsa_stream = alsa_stream;
  107436. + work->cmd = BCM2835_AUDIO_START;
  107437. + if (queue_work
  107438. + (alsa_stream->my_wq, (struct work_struct *)work))
  107439. + ret = 0;
  107440. + } else
  107441. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  107442. + }
  107443. + LOG_DBG(" .. OUT %d\n", ret);
  107444. + return ret;
  107445. +}
  107446. +
  107447. +int bcm2835_audio_stop(bcm2835_alsa_stream_t * alsa_stream)
  107448. +{
  107449. + int ret = -1;
  107450. + LOG_DBG(" .. IN\n");
  107451. + if (alsa_stream->my_wq) {
  107452. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  107453. + /*--- Queue some work (item 1) ---*/
  107454. + if (work) {
  107455. + INIT_WORK((struct work_struct *)work, my_wq_function);
  107456. + work->alsa_stream = alsa_stream;
  107457. + work->cmd = BCM2835_AUDIO_STOP;
  107458. + if (queue_work
  107459. + (alsa_stream->my_wq, (struct work_struct *)work))
  107460. + ret = 0;
  107461. + } else
  107462. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  107463. + }
  107464. + LOG_DBG(" .. OUT %d\n", ret);
  107465. + return ret;
  107466. +}
  107467. +
  107468. +int bcm2835_audio_write(bcm2835_alsa_stream_t *alsa_stream,
  107469. + uint32_t count, void *src)
  107470. +{
  107471. + int ret = -1;
  107472. + LOG_DBG(" .. IN\n");
  107473. + if (alsa_stream->my_wq) {
  107474. + my_work_t *work = kmalloc(sizeof(my_work_t), GFP_ATOMIC);
  107475. + /*--- Queue some work (item 1) ---*/
  107476. + if (work) {
  107477. + INIT_WORK((struct work_struct *)work, my_wq_function);
  107478. + work->alsa_stream = alsa_stream;
  107479. + work->cmd = BCM2835_AUDIO_WRITE;
  107480. + work->src = src;
  107481. + work->count = count;
  107482. + if (queue_work
  107483. + (alsa_stream->my_wq, (struct work_struct *)work))
  107484. + ret = 0;
  107485. + } else
  107486. + LOG_ERR(" .. Error: NULL work kmalloc\n");
  107487. + }
  107488. + LOG_DBG(" .. OUT %d\n", ret);
  107489. + return ret;
  107490. +}
  107491. +
  107492. +void my_workqueue_init(bcm2835_alsa_stream_t * alsa_stream)
  107493. +{
  107494. + alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
  107495. + return;
  107496. +}
  107497. +
  107498. +void my_workqueue_quit(bcm2835_alsa_stream_t * alsa_stream)
  107499. +{
  107500. + if (alsa_stream->my_wq) {
  107501. + flush_workqueue(alsa_stream->my_wq);
  107502. + destroy_workqueue(alsa_stream->my_wq);
  107503. + alsa_stream->my_wq = NULL;
  107504. + }
  107505. + return;
  107506. +}
  107507. +
  107508. +static void audio_vchi_callback(void *param,
  107509. + const VCHI_CALLBACK_REASON_T reason,
  107510. + void *msg_handle)
  107511. +{
  107512. + AUDIO_INSTANCE_T *instance = (AUDIO_INSTANCE_T *) param;
  107513. + int32_t status;
  107514. + int32_t msg_len;
  107515. + VC_AUDIO_MSG_T m;
  107516. + LOG_DBG(" .. IN instance=%p, handle=%p, alsa=%p, reason=%d, handle=%p\n",
  107517. + instance, instance ? instance->vchi_handle[0] : NULL, instance ? instance->alsa_stream : NULL, reason, msg_handle);
  107518. +
  107519. + if (reason != VCHI_CALLBACK_MSG_AVAILABLE) {
  107520. + return;
  107521. + }
  107522. + if (!instance) {
  107523. + LOG_ERR(" .. instance is null\n");
  107524. + BUG();
  107525. + return;
  107526. + }
  107527. + if (!instance->vchi_handle[0]) {
  107528. + LOG_ERR(" .. instance->vchi_handle[0] is null\n");
  107529. + BUG();
  107530. + return;
  107531. + }
  107532. + status = vchi_msg_dequeue(instance->vchi_handle[0],
  107533. + &m, sizeof m, &msg_len, VCHI_FLAGS_NONE);
  107534. + if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
  107535. + LOG_DBG
  107536. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
  107537. + instance, m.u.result.success);
  107538. + instance->result = m.u.result.success;
  107539. + complete(&instance->msg_avail_comp);
  107540. + } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
  107541. + bcm2835_alsa_stream_t *alsa_stream = instance->alsa_stream;
  107542. + irq_handler_t callback = (irq_handler_t) m.u.complete.callback;
  107543. + LOG_DBG
  107544. + (" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
  107545. + instance, m.u.complete.count);
  107546. + if (alsa_stream && callback) {
  107547. + atomic_add(m.u.complete.count, &alsa_stream->retrieved);
  107548. + callback(0, alsa_stream);
  107549. + } else {
  107550. + LOG_ERR(" .. unexpected alsa_stream=%p, callback=%p\n",
  107551. + alsa_stream, callback);
  107552. + }
  107553. + } else {
  107554. + LOG_ERR(" .. unexpected m.type=%d\n", m.type);
  107555. + }
  107556. + LOG_DBG(" .. OUT\n");
  107557. +}
  107558. +
  107559. +static AUDIO_INSTANCE_T *vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
  107560. + VCHI_CONNECTION_T **
  107561. + vchi_connections,
  107562. + uint32_t num_connections)
  107563. +{
  107564. + uint32_t i;
  107565. + AUDIO_INSTANCE_T *instance;
  107566. + int status;
  107567. +
  107568. + LOG_DBG("%s: start", __func__);
  107569. +
  107570. + if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
  107571. + LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
  107572. + __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
  107573. +
  107574. + return NULL;
  107575. + }
  107576. + /* Allocate memory for this instance */
  107577. + instance = kmalloc(sizeof(*instance), GFP_KERNEL);
  107578. + if (!instance)
  107579. + return NULL;
  107580. +
  107581. + memset(instance, 0, sizeof(*instance));
  107582. + instance->num_connections = num_connections;
  107583. +
  107584. + /* Create a lock for exclusive, serialized VCHI connection access */
  107585. + mutex_init(&instance->vchi_mutex);
  107586. + /* Open the VCHI service connections */
  107587. + for (i = 0; i < num_connections; i++) {
  107588. + SERVICE_CREATION_T params = {
  107589. + VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
  107590. + VC_AUDIO_SERVER_NAME, // 4cc service code
  107591. + vchi_connections[i], // passed in fn pointers
  107592. + 0, // rx fifo size (unused)
  107593. + 0, // tx fifo size (unused)
  107594. + audio_vchi_callback, // service callback
  107595. + instance, // service callback parameter
  107596. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk recieves
  107597. + 1, //TODO: remove VCOS_FALSE, // unaligned bulk transmits
  107598. + 0 // want crc check on bulk transfers
  107599. + };
  107600. +
  107601. + LOG_DBG("%s: about to open %i\n", __func__, i);
  107602. + status = vchi_service_open(vchi_instance, &params,
  107603. + &instance->vchi_handle[i]);
  107604. + LOG_DBG("%s: opened %i: %p=%d\n", __func__, i, instance->vchi_handle[i], status);
  107605. + if (status) {
  107606. + LOG_ERR
  107607. + ("%s: failed to open VCHI service connection (status=%d)\n",
  107608. + __func__, status);
  107609. +
  107610. + goto err_close_services;
  107611. + }
  107612. + /* Finished with the service for now */
  107613. + vchi_service_release(instance->vchi_handle[i]);
  107614. + }
  107615. +
  107616. + LOG_DBG("%s: okay\n", __func__);
  107617. + return instance;
  107618. +
  107619. +err_close_services:
  107620. + for (i = 0; i < instance->num_connections; i++) {
  107621. + LOG_ERR("%s: closing %i: %p\n", __func__, i, instance->vchi_handle[i]);
  107622. + if (instance->vchi_handle[i])
  107623. + vchi_service_close(instance->vchi_handle[i]);
  107624. + }
  107625. +
  107626. + kfree(instance);
  107627. + LOG_ERR("%s: error\n", __func__);
  107628. +
  107629. + return NULL;
  107630. +}
  107631. +
  107632. +static int32_t vc_vchi_audio_deinit(AUDIO_INSTANCE_T * instance)
  107633. +{
  107634. + uint32_t i;
  107635. +
  107636. + LOG_DBG(" .. IN\n");
  107637. +
  107638. + if (instance == NULL) {
  107639. + LOG_ERR("%s: invalid handle %p\n", __func__, instance);
  107640. +
  107641. + return -1;
  107642. + }
  107643. +
  107644. + LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
  107645. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107646. + {
  107647. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107648. + return -EINTR;
  107649. + }
  107650. +
  107651. + /* Close all VCHI service connections */
  107652. + for (i = 0; i < instance->num_connections; i++) {
  107653. + int32_t success;
  107654. + LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
  107655. + vchi_service_use(instance->vchi_handle[i]);
  107656. +
  107657. + success = vchi_service_close(instance->vchi_handle[i]);
  107658. + if (success != 0) {
  107659. + LOG_ERR
  107660. + ("%s: failed to close VCHI service connection (status=%d)\n",
  107661. + __func__, success);
  107662. + }
  107663. + }
  107664. +
  107665. + mutex_unlock(&instance->vchi_mutex);
  107666. +
  107667. + kfree(instance);
  107668. +
  107669. + LOG_DBG(" .. OUT\n");
  107670. +
  107671. + return 0;
  107672. +}
  107673. +
  107674. +static int bcm2835_audio_open_connection(bcm2835_alsa_stream_t * alsa_stream)
  107675. +{
  107676. + static VCHI_INSTANCE_T vchi_instance;
  107677. + static VCHI_CONNECTION_T *vchi_connection;
  107678. + static int initted;
  107679. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107680. + int ret;
  107681. + LOG_DBG(" .. IN\n");
  107682. +
  107683. + LOG_INFO("%s: start\n", __func__);
  107684. + BUG_ON(instance);
  107685. + if (instance) {
  107686. + LOG_ERR("%s: VCHI instance already open (%p)\n",
  107687. + __func__, instance);
  107688. + instance->alsa_stream = alsa_stream;
  107689. + alsa_stream->instance = instance;
  107690. + ret = 0; // xxx todo -1;
  107691. + goto err_free_mem;
  107692. + }
  107693. +
  107694. + /* Initialize and create a VCHI connection */
  107695. + if (!initted) {
  107696. + ret = vchi_initialise(&vchi_instance);
  107697. + if (ret != 0) {
  107698. + LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
  107699. + __func__, ret);
  107700. +
  107701. + ret = -EIO;
  107702. + goto err_free_mem;
  107703. + }
  107704. + ret = vchi_connect(NULL, 0, vchi_instance);
  107705. + if (ret != 0) {
  107706. + LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
  107707. + __func__, ret);
  107708. +
  107709. + ret = -EIO;
  107710. + goto err_free_mem;
  107711. + }
  107712. + initted = 1;
  107713. + }
  107714. +
  107715. + /* Initialize an instance of the audio service */
  107716. + instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
  107717. +
  107718. + if (instance == NULL) {
  107719. + LOG_ERR("%s: failed to initialize audio service\n", __func__);
  107720. +
  107721. + ret = -EPERM;
  107722. + goto err_free_mem;
  107723. + }
  107724. +
  107725. + instance->alsa_stream = alsa_stream;
  107726. + alsa_stream->instance = instance;
  107727. +
  107728. + LOG_DBG(" success !\n");
  107729. +err_free_mem:
  107730. + LOG_DBG(" .. OUT\n");
  107731. +
  107732. + return ret;
  107733. +}
  107734. +
  107735. +int bcm2835_audio_open(bcm2835_alsa_stream_t * alsa_stream)
  107736. +{
  107737. + AUDIO_INSTANCE_T *instance;
  107738. + VC_AUDIO_MSG_T m;
  107739. + int32_t success;
  107740. + int ret;
  107741. + LOG_DBG(" .. IN\n");
  107742. +
  107743. + my_workqueue_init(alsa_stream);
  107744. +
  107745. + ret = bcm2835_audio_open_connection(alsa_stream);
  107746. + if (ret != 0) {
  107747. + ret = -1;
  107748. + goto exit;
  107749. + }
  107750. + instance = alsa_stream->instance;
  107751. + LOG_DBG(" instance (%p)\n", instance);
  107752. +
  107753. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107754. + {
  107755. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107756. + return -EINTR;
  107757. + }
  107758. + vchi_service_use(instance->vchi_handle[0]);
  107759. +
  107760. + m.type = VC_AUDIO_MSG_TYPE_OPEN;
  107761. +
  107762. + /* Send the message to the videocore */
  107763. + success = vchi_msg_queue(instance->vchi_handle[0],
  107764. + &m, sizeof m,
  107765. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107766. +
  107767. + if (success != 0) {
  107768. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107769. + __func__, success);
  107770. +
  107771. + ret = -1;
  107772. + goto unlock;
  107773. + }
  107774. +
  107775. + ret = 0;
  107776. +
  107777. +unlock:
  107778. + vchi_service_release(instance->vchi_handle[0]);
  107779. + mutex_unlock(&instance->vchi_mutex);
  107780. +exit:
  107781. + LOG_DBG(" .. OUT\n");
  107782. + return ret;
  107783. +}
  107784. +
  107785. +static int bcm2835_audio_set_ctls_chan(bcm2835_alsa_stream_t * alsa_stream,
  107786. + bcm2835_chip_t * chip)
  107787. +{
  107788. + VC_AUDIO_MSG_T m;
  107789. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107790. + int32_t success;
  107791. + int ret;
  107792. + LOG_DBG(" .. IN\n");
  107793. +
  107794. + LOG_INFO
  107795. + (" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  107796. +
  107797. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107798. + {
  107799. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107800. + return -EINTR;
  107801. + }
  107802. + vchi_service_use(instance->vchi_handle[0]);
  107803. +
  107804. + instance->result = -1;
  107805. +
  107806. + m.type = VC_AUDIO_MSG_TYPE_CONTROL;
  107807. + m.u.control.dest = chip->dest;
  107808. + m.u.control.volume = chip->volume;
  107809. +
  107810. + /* Create the message available completion */
  107811. + init_completion(&instance->msg_avail_comp);
  107812. +
  107813. + /* Send the message to the videocore */
  107814. + success = vchi_msg_queue(instance->vchi_handle[0],
  107815. + &m, sizeof m,
  107816. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107817. +
  107818. + if (success != 0) {
  107819. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107820. + __func__, success);
  107821. +
  107822. + ret = -1;
  107823. + goto unlock;
  107824. + }
  107825. +
  107826. + /* We are expecting a reply from the videocore */
  107827. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  107828. + if (ret) {
  107829. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  107830. + __func__, success);
  107831. + goto unlock;
  107832. + }
  107833. +
  107834. + if (instance->result != 0) {
  107835. + LOG_ERR("%s: result=%d\n", __func__, instance->result);
  107836. +
  107837. + ret = -1;
  107838. + goto unlock;
  107839. + }
  107840. +
  107841. + ret = 0;
  107842. +
  107843. +unlock:
  107844. + vchi_service_release(instance->vchi_handle[0]);
  107845. + mutex_unlock(&instance->vchi_mutex);
  107846. +
  107847. + LOG_DBG(" .. OUT\n");
  107848. + return ret;
  107849. +}
  107850. +
  107851. +int bcm2835_audio_set_ctls(bcm2835_chip_t * chip)
  107852. +{
  107853. + int i;
  107854. + int ret = 0;
  107855. + LOG_DBG(" .. IN\n");
  107856. + LOG_DBG(" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
  107857. +
  107858. + /* change ctls for all substreams */
  107859. + for (i = 0; i < MAX_SUBSTREAMS; i++) {
  107860. + if (chip->avail_substreams & (1 << i)) {
  107861. + if (!chip->alsa_stream[i])
  107862. + {
  107863. + LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
  107864. + ret = 0;
  107865. + }
  107866. + else if (bcm2835_audio_set_ctls_chan /* returns 0 on success */
  107867. + (chip->alsa_stream[i], chip) != 0)
  107868. + {
  107869. + LOG_ERR("Couldn't set the controls for stream %d\n", i);
  107870. + ret = -1;
  107871. + }
  107872. + else LOG_ERR(" Controls set for stream %d\n", i);
  107873. + }
  107874. + }
  107875. + LOG_DBG(" .. OUT ret=%d\n", ret);
  107876. + return ret;
  107877. +}
  107878. +
  107879. +int bcm2835_audio_set_params(bcm2835_alsa_stream_t * alsa_stream,
  107880. + uint32_t channels, uint32_t samplerate,
  107881. + uint32_t bps)
  107882. +{
  107883. + VC_AUDIO_MSG_T m;
  107884. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107885. + int32_t success;
  107886. + int ret;
  107887. + LOG_DBG(" .. IN\n");
  107888. +
  107889. + LOG_INFO
  107890. + (" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
  107891. + channels, samplerate, bps);
  107892. +
  107893. + /* resend ctls - alsa_stream may not have been open when first send */
  107894. + ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
  107895. + if (ret != 0) {
  107896. + LOG_ERR(" Alsa controls not supported\n");
  107897. + return -EINVAL;
  107898. + }
  107899. +
  107900. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107901. + {
  107902. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107903. + return -EINTR;
  107904. + }
  107905. + vchi_service_use(instance->vchi_handle[0]);
  107906. +
  107907. + instance->result = -1;
  107908. +
  107909. + m.type = VC_AUDIO_MSG_TYPE_CONFIG;
  107910. + m.u.config.channels = channels;
  107911. + m.u.config.samplerate = samplerate;
  107912. + m.u.config.bps = bps;
  107913. +
  107914. + /* Create the message available completion */
  107915. + init_completion(&instance->msg_avail_comp);
  107916. +
  107917. + /* Send the message to the videocore */
  107918. + success = vchi_msg_queue(instance->vchi_handle[0],
  107919. + &m, sizeof m,
  107920. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107921. +
  107922. + if (success != 0) {
  107923. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107924. + __func__, success);
  107925. +
  107926. + ret = -1;
  107927. + goto unlock;
  107928. + }
  107929. +
  107930. + /* We are expecting a reply from the videocore */
  107931. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  107932. + if (ret) {
  107933. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  107934. + __func__, success);
  107935. + goto unlock;
  107936. + }
  107937. +
  107938. + if (instance->result != 0) {
  107939. + LOG_ERR("%s: result=%d", __func__, instance->result);
  107940. +
  107941. + ret = -1;
  107942. + goto unlock;
  107943. + }
  107944. +
  107945. + ret = 0;
  107946. +
  107947. +unlock:
  107948. + vchi_service_release(instance->vchi_handle[0]);
  107949. + mutex_unlock(&instance->vchi_mutex);
  107950. +
  107951. + LOG_DBG(" .. OUT\n");
  107952. + return ret;
  107953. +}
  107954. +
  107955. +int bcm2835_audio_setup(bcm2835_alsa_stream_t * alsa_stream)
  107956. +{
  107957. + LOG_DBG(" .. IN\n");
  107958. +
  107959. + LOG_DBG(" .. OUT\n");
  107960. +
  107961. + return 0;
  107962. +}
  107963. +
  107964. +static int bcm2835_audio_start_worker(bcm2835_alsa_stream_t * alsa_stream)
  107965. +{
  107966. + VC_AUDIO_MSG_T m;
  107967. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  107968. + int32_t success;
  107969. + int ret;
  107970. + LOG_DBG(" .. IN\n");
  107971. +
  107972. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  107973. + {
  107974. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  107975. + return -EINTR;
  107976. + }
  107977. + vchi_service_use(instance->vchi_handle[0]);
  107978. +
  107979. + m.type = VC_AUDIO_MSG_TYPE_START;
  107980. +
  107981. + /* Send the message to the videocore */
  107982. + success = vchi_msg_queue(instance->vchi_handle[0],
  107983. + &m, sizeof m,
  107984. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  107985. +
  107986. + if (success != 0) {
  107987. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  107988. + __func__, success);
  107989. +
  107990. + ret = -1;
  107991. + goto unlock;
  107992. + }
  107993. +
  107994. + ret = 0;
  107995. +
  107996. +unlock:
  107997. + vchi_service_release(instance->vchi_handle[0]);
  107998. + mutex_unlock(&instance->vchi_mutex);
  107999. + LOG_DBG(" .. OUT\n");
  108000. + return ret;
  108001. +}
  108002. +
  108003. +static int bcm2835_audio_stop_worker(bcm2835_alsa_stream_t * alsa_stream)
  108004. +{
  108005. + VC_AUDIO_MSG_T m;
  108006. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  108007. + int32_t success;
  108008. + int ret;
  108009. + LOG_DBG(" .. IN\n");
  108010. +
  108011. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  108012. + {
  108013. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  108014. + return -EINTR;
  108015. + }
  108016. + vchi_service_use(instance->vchi_handle[0]);
  108017. +
  108018. + m.type = VC_AUDIO_MSG_TYPE_STOP;
  108019. + m.u.stop.draining = alsa_stream->draining;
  108020. +
  108021. + /* Send the message to the videocore */
  108022. + success = vchi_msg_queue(instance->vchi_handle[0],
  108023. + &m, sizeof m,
  108024. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  108025. +
  108026. + if (success != 0) {
  108027. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  108028. + __func__, success);
  108029. +
  108030. + ret = -1;
  108031. + goto unlock;
  108032. + }
  108033. +
  108034. + ret = 0;
  108035. +
  108036. +unlock:
  108037. + vchi_service_release(instance->vchi_handle[0]);
  108038. + mutex_unlock(&instance->vchi_mutex);
  108039. + LOG_DBG(" .. OUT\n");
  108040. + return ret;
  108041. +}
  108042. +
  108043. +int bcm2835_audio_close(bcm2835_alsa_stream_t * alsa_stream)
  108044. +{
  108045. + VC_AUDIO_MSG_T m;
  108046. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  108047. + int32_t success;
  108048. + int ret;
  108049. + LOG_DBG(" .. IN\n");
  108050. +
  108051. + my_workqueue_quit(alsa_stream);
  108052. +
  108053. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  108054. + {
  108055. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  108056. + return -EINTR;
  108057. + }
  108058. + vchi_service_use(instance->vchi_handle[0]);
  108059. +
  108060. + m.type = VC_AUDIO_MSG_TYPE_CLOSE;
  108061. +
  108062. + /* Create the message available completion */
  108063. + init_completion(&instance->msg_avail_comp);
  108064. +
  108065. + /* Send the message to the videocore */
  108066. + success = vchi_msg_queue(instance->vchi_handle[0],
  108067. + &m, sizeof m,
  108068. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  108069. +
  108070. + if (success != 0) {
  108071. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  108072. + __func__, success);
  108073. + ret = -1;
  108074. + goto unlock;
  108075. + }
  108076. +
  108077. + ret = wait_for_completion_interruptible(&instance->msg_avail_comp);
  108078. + if (ret) {
  108079. + LOG_ERR("%s: failed on waiting for event (status=%d)\n",
  108080. + __func__, success);
  108081. + goto unlock;
  108082. + }
  108083. + if (instance->result != 0) {
  108084. + LOG_ERR("%s: failed result (status=%d)\n",
  108085. + __func__, instance->result);
  108086. +
  108087. + ret = -1;
  108088. + goto unlock;
  108089. + }
  108090. +
  108091. + ret = 0;
  108092. +
  108093. +unlock:
  108094. + vchi_service_release(instance->vchi_handle[0]);
  108095. + mutex_unlock(&instance->vchi_mutex);
  108096. +
  108097. + /* Stop the audio service */
  108098. + if (instance) {
  108099. + vc_vchi_audio_deinit(instance);
  108100. + alsa_stream->instance = NULL;
  108101. + }
  108102. + LOG_DBG(" .. OUT\n");
  108103. + return ret;
  108104. +}
  108105. +
  108106. +int bcm2835_audio_write_worker(bcm2835_alsa_stream_t *alsa_stream,
  108107. + uint32_t count, void *src)
  108108. +{
  108109. + VC_AUDIO_MSG_T m;
  108110. + AUDIO_INSTANCE_T *instance = alsa_stream->instance;
  108111. + int32_t success;
  108112. + int ret;
  108113. +
  108114. + LOG_DBG(" .. IN\n");
  108115. +
  108116. + LOG_INFO(" Writing %d bytes from %p\n", count, src);
  108117. +
  108118. + if(mutex_lock_interruptible(&instance->vchi_mutex))
  108119. + {
  108120. + LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",instance->num_connections);
  108121. + return -EINTR;
  108122. + }
  108123. + vchi_service_use(instance->vchi_handle[0]);
  108124. +
  108125. + if ( instance->peer_version==0 && vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0 ) {
  108126. + LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
  108127. + }
  108128. + m.type = VC_AUDIO_MSG_TYPE_WRITE;
  108129. + m.u.write.count = count;
  108130. + // old version uses bulk, new version uses control
  108131. + m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0:4000;
  108132. + m.u.write.callback = alsa_stream->fifo_irq_handler;
  108133. + m.u.write.cookie = alsa_stream;
  108134. + m.u.write.silence = src == NULL;
  108135. +
  108136. + /* Send the message to the videocore */
  108137. + success = vchi_msg_queue(instance->vchi_handle[0],
  108138. + &m, sizeof m,
  108139. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  108140. +
  108141. + if (success != 0) {
  108142. + LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
  108143. + __func__, success);
  108144. +
  108145. + ret = -1;
  108146. + goto unlock;
  108147. + }
  108148. + if (!m.u.write.silence) {
  108149. + if (m.u.write.max_packet == 0) {
  108150. + /* Send the message to the videocore */
  108151. + success = vchi_bulk_queue_transmit(instance->vchi_handle[0],
  108152. + src, count,
  108153. + 0 *
  108154. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED
  108155. + +
  108156. + 1 *
  108157. + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
  108158. + NULL);
  108159. + } else {
  108160. + while (count > 0) {
  108161. + int bytes = min((int)m.u.write.max_packet, (int)count);
  108162. + success = vchi_msg_queue(instance->vchi_handle[0],
  108163. + src, bytes,
  108164. + VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
  108165. + src = (char *)src + bytes;
  108166. + count -= bytes;
  108167. + }
  108168. + }
  108169. + if (success != 0) {
  108170. + LOG_ERR
  108171. + ("%s: failed on vchi_bulk_queue_transmit (status=%d)\n",
  108172. + __func__, success);
  108173. +
  108174. + ret = -1;
  108175. + goto unlock;
  108176. + }
  108177. + }
  108178. + ret = 0;
  108179. +
  108180. +unlock:
  108181. + vchi_service_release(instance->vchi_handle[0]);
  108182. + mutex_unlock(&instance->vchi_mutex);
  108183. + LOG_DBG(" .. OUT\n");
  108184. + return ret;
  108185. +}
  108186. +
  108187. +/**
  108188. + * Returns all buffers from arm->vc
  108189. + */
  108190. +void bcm2835_audio_flush_buffers(bcm2835_alsa_stream_t * alsa_stream)
  108191. +{
  108192. + LOG_DBG(" .. IN\n");
  108193. + LOG_DBG(" .. OUT\n");
  108194. + return;
  108195. +}
  108196. +
  108197. +/**
  108198. + * Forces VC to flush(drop) its filled playback buffers and
  108199. + * return them the us. (VC->ARM)
  108200. + */
  108201. +void bcm2835_audio_flush_playback_buffers(bcm2835_alsa_stream_t * alsa_stream)
  108202. +{
  108203. + LOG_DBG(" .. IN\n");
  108204. + LOG_DBG(" .. OUT\n");
  108205. +}
  108206. +
  108207. +uint32_t bcm2835_audio_retrieve_buffers(bcm2835_alsa_stream_t * alsa_stream)
  108208. +{
  108209. + uint32_t count = atomic_read(&alsa_stream->retrieved);
  108210. + atomic_sub(count, &alsa_stream->retrieved);
  108211. + return count;
  108212. +}
  108213. +
  108214. +module_param(force_bulk, bool, 0444);
  108215. +MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");
  108216. diff -Nur linux-3.16.2/sound/arm/Kconfig linux-3.16-rpi/sound/arm/Kconfig
  108217. --- linux-3.16.2/sound/arm/Kconfig 2014-09-06 01:37:11.000000000 +0200
  108218. +++ linux-3.16-rpi/sound/arm/Kconfig 2014-04-13 17:33:26.000000000 +0200
  108219. @@ -39,5 +39,12 @@
  108220. Say Y or M if you want to support any AC97 codec attached to
  108221. the PXA2xx AC97 interface.
  108222. +config SND_BCM2835
  108223. + tristate "BCM2835 ALSA driver"
  108224. + depends on ARCH_BCM2708 && BCM2708_VCHIQ && SND
  108225. + select SND_PCM
  108226. + help
  108227. + Say Y or M if you want to support BCM2835 Alsa pcm card driver
  108228. +
  108229. endif # SND_ARM
  108230. diff -Nur linux-3.16.2/sound/arm/Makefile linux-3.16-rpi/sound/arm/Makefile
  108231. --- linux-3.16.2/sound/arm/Makefile 2014-09-06 01:37:11.000000000 +0200
  108232. +++ linux-3.16-rpi/sound/arm/Makefile 2014-09-14 19:04:33.000000000 +0200
  108233. @@ -14,3 +14,8 @@
  108234. obj-$(CONFIG_SND_PXA2XX_AC97) += snd-pxa2xx-ac97.o
  108235. snd-pxa2xx-ac97-objs := pxa2xx-ac97.o
  108236. +
  108237. +obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
  108238. +snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
  108239. +
  108240. +ccflags-y += -Idrivers/misc/vc04_services -Idrivers/misc/vc04_services/interface/vcos/linuxkernel -D__VCCOREVER__=0x04000000
  108241. diff -Nur linux-3.16.2/sound/arm/vc_vchi_audioserv_defs.h linux-3.16-rpi/sound/arm/vc_vchi_audioserv_defs.h
  108242. --- linux-3.16.2/sound/arm/vc_vchi_audioserv_defs.h 1970-01-01 01:00:00.000000000 +0100
  108243. +++ linux-3.16-rpi/sound/arm/vc_vchi_audioserv_defs.h 2014-04-13 17:33:26.000000000 +0200
  108244. @@ -0,0 +1,116 @@
  108245. +/*****************************************************************************
  108246. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  108247. +*
  108248. +* Unless you and Broadcom execute a separate written software license
  108249. +* agreement governing use of this software, this software is licensed to you
  108250. +* under the terms of the GNU General Public License version 2, available at
  108251. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  108252. +*
  108253. +* Notwithstanding the above, under no circumstances may you combine this
  108254. +* software in any way with any other Broadcom software provided under a
  108255. +* license other than the GPL, without Broadcom's express prior written
  108256. +* consent.
  108257. +*****************************************************************************/
  108258. +
  108259. +#ifndef _VC_AUDIO_DEFS_H_
  108260. +#define _VC_AUDIO_DEFS_H_
  108261. +
  108262. +#define VC_AUDIOSERV_MIN_VER 1
  108263. +#define VC_AUDIOSERV_VER 2
  108264. +
  108265. +// FourCC code used for VCHI connection
  108266. +#define VC_AUDIO_SERVER_NAME MAKE_FOURCC("AUDS")
  108267. +
  108268. +// Maximum message length
  108269. +#define VC_AUDIO_MAX_MSG_LEN (sizeof( VC_AUDIO_MSG_T ))
  108270. +
  108271. +// List of screens that are currently supported
  108272. +// All message types supported for HOST->VC direction
  108273. +typedef enum {
  108274. + VC_AUDIO_MSG_TYPE_RESULT, // Generic result
  108275. + VC_AUDIO_MSG_TYPE_COMPLETE, // Generic result
  108276. + VC_AUDIO_MSG_TYPE_CONFIG, // Configure audio
  108277. + VC_AUDIO_MSG_TYPE_CONTROL, // Configure audio
  108278. + VC_AUDIO_MSG_TYPE_OPEN, // Configure audio
  108279. + VC_AUDIO_MSG_TYPE_CLOSE, // Configure audio
  108280. + VC_AUDIO_MSG_TYPE_START, // Configure audio
  108281. + VC_AUDIO_MSG_TYPE_STOP, // Configure audio
  108282. + VC_AUDIO_MSG_TYPE_WRITE, // Configure audio
  108283. + VC_AUDIO_MSG_TYPE_MAX
  108284. +} VC_AUDIO_MSG_TYPE;
  108285. +
  108286. +// configure the audio
  108287. +typedef struct {
  108288. + uint32_t channels;
  108289. + uint32_t samplerate;
  108290. + uint32_t bps;
  108291. +
  108292. +} VC_AUDIO_CONFIG_T;
  108293. +
  108294. +typedef struct {
  108295. + uint32_t volume;
  108296. + uint32_t dest;
  108297. +
  108298. +} VC_AUDIO_CONTROL_T;
  108299. +
  108300. +// audio
  108301. +typedef struct {
  108302. + uint32_t dummy;
  108303. +
  108304. +} VC_AUDIO_OPEN_T;
  108305. +
  108306. +// audio
  108307. +typedef struct {
  108308. + uint32_t dummy;
  108309. +
  108310. +} VC_AUDIO_CLOSE_T;
  108311. +// audio
  108312. +typedef struct {
  108313. + uint32_t dummy;
  108314. +
  108315. +} VC_AUDIO_START_T;
  108316. +// audio
  108317. +typedef struct {
  108318. + uint32_t draining;
  108319. +
  108320. +} VC_AUDIO_STOP_T;
  108321. +
  108322. +// configure the write audio samples
  108323. +typedef struct {
  108324. + uint32_t count; // in bytes
  108325. + void *callback;
  108326. + void *cookie;
  108327. + uint16_t silence;
  108328. + uint16_t max_packet;
  108329. +} VC_AUDIO_WRITE_T;
  108330. +
  108331. +// Generic result for a request (VC->HOST)
  108332. +typedef struct {
  108333. + int32_t success; // Success value
  108334. +
  108335. +} VC_AUDIO_RESULT_T;
  108336. +
  108337. +// Generic result for a request (VC->HOST)
  108338. +typedef struct {
  108339. + int32_t count; // Success value
  108340. + void *callback;
  108341. + void *cookie;
  108342. +} VC_AUDIO_COMPLETE_T;
  108343. +
  108344. +// Message header for all messages in HOST->VC direction
  108345. +typedef struct {
  108346. + int32_t type; // Message type (VC_AUDIO_MSG_TYPE)
  108347. + union {
  108348. + VC_AUDIO_CONFIG_T config;
  108349. + VC_AUDIO_CONTROL_T control;
  108350. + VC_AUDIO_OPEN_T open;
  108351. + VC_AUDIO_CLOSE_T close;
  108352. + VC_AUDIO_START_T start;
  108353. + VC_AUDIO_STOP_T stop;
  108354. + VC_AUDIO_WRITE_T write;
  108355. + VC_AUDIO_RESULT_T result;
  108356. + VC_AUDIO_COMPLETE_T complete;
  108357. + } u;
  108358. +} VC_AUDIO_MSG_T;
  108359. +
  108360. +#endif // _VC_AUDIO_DEFS_H_
  108361. diff -Nur linux-3.16.2/sound/soc/bcm/bcm2708-i2s.c linux-3.16-rpi/sound/soc/bcm/bcm2708-i2s.c
  108362. --- linux-3.16.2/sound/soc/bcm/bcm2708-i2s.c 1970-01-01 01:00:00.000000000 +0100
  108363. +++ linux-3.16-rpi/sound/soc/bcm/bcm2708-i2s.c 2014-09-14 19:04:34.000000000 +0200
  108364. @@ -0,0 +1,985 @@
  108365. +/*
  108366. + * ALSA SoC I2S Audio Layer for Broadcom BCM2708 SoC
  108367. + *
  108368. + * Author: Florian Meier <florian.meier@koalo.de>
  108369. + * Copyright 2013
  108370. + *
  108371. + * Based on
  108372. + * Raspberry Pi PCM I2S ALSA Driver
  108373. + * Copyright (c) by Phil Poole 2013
  108374. + *
  108375. + * ALSA SoC I2S (McBSP) Audio Layer for TI DAVINCI processor
  108376. + * Vladimir Barinov, <vbarinov@embeddedalley.com>
  108377. + * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  108378. + *
  108379. + * OMAP ALSA SoC DAI driver using McBSP port
  108380. + * Copyright (C) 2008 Nokia Corporation
  108381. + * Contact: Jarkko Nikula <jarkko.nikula@bitmer.com>
  108382. + * Peter Ujfalusi <peter.ujfalusi@ti.com>
  108383. + *
  108384. + * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver
  108385. + * Author: Timur Tabi <timur@freescale.com>
  108386. + * Copyright 2007-2010 Freescale Semiconductor, Inc.
  108387. + *
  108388. + * This program is free software; you can redistribute it and/or
  108389. + * modify it under the terms of the GNU General Public License
  108390. + * version 2 as published by the Free Software Foundation.
  108391. + *
  108392. + * This program is distributed in the hope that it will be useful, but
  108393. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  108394. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  108395. + * General Public License for more details.
  108396. + */
  108397. +
  108398. +#include "bcm2708-i2s.h"
  108399. +
  108400. +#include <linux/init.h>
  108401. +#include <linux/module.h>
  108402. +#include <linux/device.h>
  108403. +#include <linux/slab.h>
  108404. +#include <linux/delay.h>
  108405. +#include <linux/io.h>
  108406. +#include <linux/clk.h>
  108407. +
  108408. +#include <sound/core.h>
  108409. +#include <sound/pcm.h>
  108410. +#include <sound/pcm_params.h>
  108411. +#include <sound/initval.h>
  108412. +#include <sound/soc.h>
  108413. +#include <sound/dmaengine_pcm.h>
  108414. +
  108415. +#include <asm/system_info.h>
  108416. +
  108417. +/* Clock registers */
  108418. +#define BCM2708_CLK_PCMCTL_REG 0x00
  108419. +#define BCM2708_CLK_PCMDIV_REG 0x04
  108420. +
  108421. +/* Clock register settings */
  108422. +#define BCM2708_CLK_PASSWD (0x5a000000)
  108423. +#define BCM2708_CLK_PASSWD_MASK (0xff000000)
  108424. +#define BCM2708_CLK_MASH(v) ((v) << 9)
  108425. +#define BCM2708_CLK_FLIP BIT(8)
  108426. +#define BCM2708_CLK_BUSY BIT(7)
  108427. +#define BCM2708_CLK_KILL BIT(5)
  108428. +#define BCM2708_CLK_ENAB BIT(4)
  108429. +#define BCM2708_CLK_SRC(v) (v)
  108430. +
  108431. +#define BCM2708_CLK_SHIFT (12)
  108432. +#define BCM2708_CLK_DIVI(v) ((v) << BCM2708_CLK_SHIFT)
  108433. +#define BCM2708_CLK_DIVF(v) (v)
  108434. +#define BCM2708_CLK_DIVF_MASK (0xFFF)
  108435. +
  108436. +enum {
  108437. + BCM2708_CLK_MASH_0 = 0,
  108438. + BCM2708_CLK_MASH_1,
  108439. + BCM2708_CLK_MASH_2,
  108440. + BCM2708_CLK_MASH_3,
  108441. +};
  108442. +
  108443. +enum {
  108444. + BCM2708_CLK_SRC_GND = 0,
  108445. + BCM2708_CLK_SRC_OSC,
  108446. + BCM2708_CLK_SRC_DBG0,
  108447. + BCM2708_CLK_SRC_DBG1,
  108448. + BCM2708_CLK_SRC_PLLA,
  108449. + BCM2708_CLK_SRC_PLLC,
  108450. + BCM2708_CLK_SRC_PLLD,
  108451. + BCM2708_CLK_SRC_HDMI,
  108452. +};
  108453. +
  108454. +/* Most clocks are not useable (freq = 0) */
  108455. +static const unsigned int bcm2708_clk_freq[BCM2708_CLK_SRC_HDMI+1] = {
  108456. + [BCM2708_CLK_SRC_GND] = 0,
  108457. + [BCM2708_CLK_SRC_OSC] = 19200000,
  108458. + [BCM2708_CLK_SRC_DBG0] = 0,
  108459. + [BCM2708_CLK_SRC_DBG1] = 0,
  108460. + [BCM2708_CLK_SRC_PLLA] = 0,
  108461. + [BCM2708_CLK_SRC_PLLC] = 0,
  108462. + [BCM2708_CLK_SRC_PLLD] = 500000000,
  108463. + [BCM2708_CLK_SRC_HDMI] = 0,
  108464. +};
  108465. +
  108466. +/* I2S registers */
  108467. +#define BCM2708_I2S_CS_A_REG 0x00
  108468. +#define BCM2708_I2S_FIFO_A_REG 0x04
  108469. +#define BCM2708_I2S_MODE_A_REG 0x08
  108470. +#define BCM2708_I2S_RXC_A_REG 0x0c
  108471. +#define BCM2708_I2S_TXC_A_REG 0x10
  108472. +#define BCM2708_I2S_DREQ_A_REG 0x14
  108473. +#define BCM2708_I2S_INTEN_A_REG 0x18
  108474. +#define BCM2708_I2S_INTSTC_A_REG 0x1c
  108475. +#define BCM2708_I2S_GRAY_REG 0x20
  108476. +
  108477. +/* I2S register settings */
  108478. +#define BCM2708_I2S_STBY BIT(25)
  108479. +#define BCM2708_I2S_SYNC BIT(24)
  108480. +#define BCM2708_I2S_RXSEX BIT(23)
  108481. +#define BCM2708_I2S_RXF BIT(22)
  108482. +#define BCM2708_I2S_TXE BIT(21)
  108483. +#define BCM2708_I2S_RXD BIT(20)
  108484. +#define BCM2708_I2S_TXD BIT(19)
  108485. +#define BCM2708_I2S_RXR BIT(18)
  108486. +#define BCM2708_I2S_TXW BIT(17)
  108487. +#define BCM2708_I2S_CS_RXERR BIT(16)
  108488. +#define BCM2708_I2S_CS_TXERR BIT(15)
  108489. +#define BCM2708_I2S_RXSYNC BIT(14)
  108490. +#define BCM2708_I2S_TXSYNC BIT(13)
  108491. +#define BCM2708_I2S_DMAEN BIT(9)
  108492. +#define BCM2708_I2S_RXTHR(v) ((v) << 7)
  108493. +#define BCM2708_I2S_TXTHR(v) ((v) << 5)
  108494. +#define BCM2708_I2S_RXCLR BIT(4)
  108495. +#define BCM2708_I2S_TXCLR BIT(3)
  108496. +#define BCM2708_I2S_TXON BIT(2)
  108497. +#define BCM2708_I2S_RXON BIT(1)
  108498. +#define BCM2708_I2S_EN (1)
  108499. +
  108500. +#define BCM2708_I2S_CLKDIS BIT(28)
  108501. +#define BCM2708_I2S_PDMN BIT(27)
  108502. +#define BCM2708_I2S_PDME BIT(26)
  108503. +#define BCM2708_I2S_FRXP BIT(25)
  108504. +#define BCM2708_I2S_FTXP BIT(24)
  108505. +#define BCM2708_I2S_CLKM BIT(23)
  108506. +#define BCM2708_I2S_CLKI BIT(22)
  108507. +#define BCM2708_I2S_FSM BIT(21)
  108508. +#define BCM2708_I2S_FSI BIT(20)
  108509. +#define BCM2708_I2S_FLEN(v) ((v) << 10)
  108510. +#define BCM2708_I2S_FSLEN(v) (v)
  108511. +
  108512. +#define BCM2708_I2S_CHWEX BIT(15)
  108513. +#define BCM2708_I2S_CHEN BIT(14)
  108514. +#define BCM2708_I2S_CHPOS(v) ((v) << 4)
  108515. +#define BCM2708_I2S_CHWID(v) (v)
  108516. +#define BCM2708_I2S_CH1(v) ((v) << 16)
  108517. +#define BCM2708_I2S_CH2(v) (v)
  108518. +
  108519. +#define BCM2708_I2S_TX_PANIC(v) ((v) << 24)
  108520. +#define BCM2708_I2S_RX_PANIC(v) ((v) << 16)
  108521. +#define BCM2708_I2S_TX(v) ((v) << 8)
  108522. +#define BCM2708_I2S_RX(v) (v)
  108523. +
  108524. +#define BCM2708_I2S_INT_RXERR BIT(3)
  108525. +#define BCM2708_I2S_INT_TXERR BIT(2)
  108526. +#define BCM2708_I2S_INT_RXR BIT(1)
  108527. +#define BCM2708_I2S_INT_TXW BIT(0)
  108528. +
  108529. +/* I2S DMA interface */
  108530. +#define BCM2708_I2S_FIFO_PHYSICAL_ADDR 0x7E203004
  108531. +#define BCM2708_DMA_DREQ_PCM_TX 2
  108532. +#define BCM2708_DMA_DREQ_PCM_RX 3
  108533. +
  108534. +/* I2S pin configuration */
  108535. +static int bcm2708_i2s_gpio=BCM2708_I2S_GPIO_AUTO;
  108536. +
  108537. +/* General device struct */
  108538. +struct bcm2708_i2s_dev {
  108539. + struct device *dev;
  108540. + struct snd_dmaengine_dai_dma_data dma_data[2];
  108541. + unsigned int fmt;
  108542. + unsigned int bclk_ratio;
  108543. +
  108544. + struct regmap *i2s_regmap;
  108545. + struct regmap *clk_regmap;
  108546. +};
  108547. +
  108548. +void bcm2708_i2s_set_gpio(int gpio) {
  108549. + bcm2708_i2s_gpio=gpio;
  108550. +}
  108551. +EXPORT_SYMBOL(bcm2708_i2s_set_gpio);
  108552. +
  108553. +
  108554. +static void bcm2708_i2s_start_clock(struct bcm2708_i2s_dev *dev)
  108555. +{
  108556. + /* Start the clock if in master mode */
  108557. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  108558. +
  108559. + switch (master) {
  108560. + case SND_SOC_DAIFMT_CBS_CFS:
  108561. + case SND_SOC_DAIFMT_CBS_CFM:
  108562. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  108563. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  108564. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  108565. + break;
  108566. + default:
  108567. + break;
  108568. + }
  108569. +}
  108570. +
  108571. +static void bcm2708_i2s_stop_clock(struct bcm2708_i2s_dev *dev)
  108572. +{
  108573. + uint32_t clkreg;
  108574. + int timeout = 1000;
  108575. +
  108576. + /* Stop clock */
  108577. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  108578. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  108579. + BCM2708_CLK_PASSWD);
  108580. +
  108581. + /* Wait for the BUSY flag going down */
  108582. + while (--timeout) {
  108583. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  108584. + if (!(clkreg & BCM2708_CLK_BUSY))
  108585. + break;
  108586. + }
  108587. +
  108588. + if (!timeout) {
  108589. + /* KILL the clock */
  108590. + dev_err(dev->dev, "I2S clock didn't stop. Kill the clock!\n");
  108591. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  108592. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD_MASK,
  108593. + BCM2708_CLK_KILL | BCM2708_CLK_PASSWD);
  108594. + }
  108595. +}
  108596. +
  108597. +static void bcm2708_i2s_clear_fifos(struct bcm2708_i2s_dev *dev,
  108598. + bool tx, bool rx)
  108599. +{
  108600. + int timeout = 1000;
  108601. + uint32_t syncval;
  108602. + uint32_t csreg;
  108603. + uint32_t i2s_active_state;
  108604. + uint32_t clkreg;
  108605. + uint32_t clk_active_state;
  108606. + uint32_t off;
  108607. + uint32_t clr;
  108608. +
  108609. + off = tx ? BCM2708_I2S_TXON : 0;
  108610. + off |= rx ? BCM2708_I2S_RXON : 0;
  108611. +
  108612. + clr = tx ? BCM2708_I2S_TXCLR : 0;
  108613. + clr |= rx ? BCM2708_I2S_RXCLR : 0;
  108614. +
  108615. + /* Backup the current state */
  108616. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  108617. + i2s_active_state = csreg & (BCM2708_I2S_RXON | BCM2708_I2S_TXON);
  108618. +
  108619. + regmap_read(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, &clkreg);
  108620. + clk_active_state = clkreg & BCM2708_CLK_ENAB;
  108621. +
  108622. + /* Start clock if not running */
  108623. + if (!clk_active_state) {
  108624. + regmap_update_bits(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG,
  108625. + BCM2708_CLK_PASSWD_MASK | BCM2708_CLK_ENAB,
  108626. + BCM2708_CLK_PASSWD | BCM2708_CLK_ENAB);
  108627. + }
  108628. +
  108629. + /* Stop I2S module */
  108630. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, off, 0);
  108631. +
  108632. + /*
  108633. + * Clear the FIFOs
  108634. + * Requires at least 2 PCM clock cycles to take effect
  108635. + */
  108636. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, clr, clr);
  108637. +
  108638. + /* Wait for 2 PCM clock cycles */
  108639. +
  108640. + /*
  108641. + * Toggle the SYNC flag. After 2 PCM clock cycles it can be read back
  108642. + * FIXME: This does not seem to work for slave mode!
  108643. + */
  108644. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &syncval);
  108645. + syncval &= BCM2708_I2S_SYNC;
  108646. +
  108647. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  108648. + BCM2708_I2S_SYNC, ~syncval);
  108649. +
  108650. + /* Wait for the SYNC flag changing it's state */
  108651. + while (--timeout) {
  108652. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  108653. + if ((csreg & BCM2708_I2S_SYNC) != syncval)
  108654. + break;
  108655. + }
  108656. +
  108657. + if (!timeout)
  108658. + dev_err(dev->dev, "I2S SYNC error!\n");
  108659. +
  108660. + /* Stop clock if it was not running before */
  108661. + if (!clk_active_state)
  108662. + bcm2708_i2s_stop_clock(dev);
  108663. +
  108664. + /* Restore I2S state */
  108665. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  108666. + BCM2708_I2S_RXON | BCM2708_I2S_TXON, i2s_active_state);
  108667. +}
  108668. +
  108669. +static int bcm2708_i2s_set_dai_fmt(struct snd_soc_dai *dai,
  108670. + unsigned int fmt)
  108671. +{
  108672. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108673. + dev->fmt = fmt;
  108674. + return 0;
  108675. +}
  108676. +
  108677. +static int bcm2708_i2s_set_dai_bclk_ratio(struct snd_soc_dai *dai,
  108678. + unsigned int ratio)
  108679. +{
  108680. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108681. + dev->bclk_ratio = ratio;
  108682. + return 0;
  108683. +}
  108684. +
  108685. +static void bcm2708_i2s_setup_gpio(void)
  108686. +{
  108687. + /*
  108688. + * This is the common way to handle the GPIO pins for
  108689. + * the Raspberry Pi.
  108690. + * TODO Better way would be to handle
  108691. + * this in the device tree!
  108692. + */
  108693. +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
  108694. +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
  108695. +
  108696. + unsigned int *gpio;
  108697. + int pin,pinconfig,startpin,alt;
  108698. +
  108699. + gpio = ioremap(GPIO_BASE, SZ_16K);
  108700. +
  108701. + /* SPI is on different GPIOs on different boards */
  108702. + /* for Raspberry Pi B+, this is pin GPIO18-21, for original on 28-31 */
  108703. + if (bcm2708_i2s_gpio==BCM2708_I2S_GPIO_AUTO) {
  108704. + if (system_rev >= 0x10) {
  108705. + /* Model B+ */
  108706. + pinconfig=BCM2708_I2S_GPIO_PIN18;
  108707. + } else {
  108708. + /* original */
  108709. + pinconfig=BCM2708_I2S_GPIO_PIN28;
  108710. + }
  108711. + } else {
  108712. + pinconfig=bcm2708_i2s_gpio;
  108713. + }
  108714. +
  108715. + if (pinconfig==BCM2708_I2S_GPIO_PIN18) {
  108716. + startpin=18;
  108717. + alt=BCM2708_I2S_GPIO_PIN18_ALT;
  108718. + } else if (pinconfig==BCM2708_I2S_GPIO_PIN28) {
  108719. + startpin=28;
  108720. + alt=BCM2708_I2S_GPIO_PIN28_ALT;
  108721. + } else {
  108722. + printk(KERN_INFO "Can't configure I2S GPIOs, unknown pin mode for I2S: %i\n",pinconfig);
  108723. + return;
  108724. + }
  108725. +
  108726. + /* configure I2S pins to correct ALT mode */
  108727. + for (pin = startpin; pin <= startpin+3; pin++) {
  108728. + INP_GPIO(pin); /* set mode to GPIO input first */
  108729. + SET_GPIO_ALT(pin, alt); /* set mode to ALT */
  108730. + }
  108731. +
  108732. +#undef INP_GPIO
  108733. +#undef SET_GPIO_ALT
  108734. +}
  108735. +
  108736. +static int bcm2708_i2s_hw_params(struct snd_pcm_substream *substream,
  108737. + struct snd_pcm_hw_params *params,
  108738. + struct snd_soc_dai *dai)
  108739. +{
  108740. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  108741. +
  108742. + unsigned int sampling_rate = params_rate(params);
  108743. + unsigned int data_length, data_delay, bclk_ratio;
  108744. + unsigned int ch1pos, ch2pos, mode, format;
  108745. + unsigned int mash = BCM2708_CLK_MASH_1;
  108746. + unsigned int divi, divf, target_frequency;
  108747. + int clk_src = -1;
  108748. + unsigned int master = dev->fmt & SND_SOC_DAIFMT_MASTER_MASK;
  108749. + bool bit_master = (master == SND_SOC_DAIFMT_CBS_CFS
  108750. + || master == SND_SOC_DAIFMT_CBS_CFM);
  108751. +
  108752. + bool frame_master = (master == SND_SOC_DAIFMT_CBS_CFS
  108753. + || master == SND_SOC_DAIFMT_CBM_CFS);
  108754. + uint32_t csreg;
  108755. +
  108756. + /*
  108757. + * If a stream is already enabled,
  108758. + * the registers are already set properly.
  108759. + */
  108760. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &csreg);
  108761. +
  108762. + if (csreg & (BCM2708_I2S_TXON | BCM2708_I2S_RXON))
  108763. + return 0;
  108764. +
  108765. +
  108766. + bcm2708_i2s_setup_gpio();
  108767. +
  108768. + /*
  108769. + * Adjust the data length according to the format.
  108770. + * We prefill the half frame length with an integer
  108771. + * divider of 2400 as explained at the clock settings.
  108772. + * Maybe it is overwritten there, if the Integer mode
  108773. + * does not apply.
  108774. + */
  108775. + switch (params_format(params)) {
  108776. + case SNDRV_PCM_FORMAT_S16_LE:
  108777. + data_length = 16;
  108778. + bclk_ratio = 40;
  108779. + break;
  108780. + case SNDRV_PCM_FORMAT_S24_LE:
  108781. + data_length = 24;
  108782. + bclk_ratio = 40;
  108783. + break;
  108784. + case SNDRV_PCM_FORMAT_S32_LE:
  108785. + data_length = 32;
  108786. + bclk_ratio = 80;
  108787. + break;
  108788. + default:
  108789. + return -EINVAL;
  108790. + }
  108791. +
  108792. + /* If bclk_ratio already set, use that one. */
  108793. + if (dev->bclk_ratio)
  108794. + bclk_ratio = dev->bclk_ratio;
  108795. +
  108796. + /*
  108797. + * Clock Settings
  108798. + *
  108799. + * The target frequency of the bit clock is
  108800. + * sampling rate * frame length
  108801. + *
  108802. + * Integer mode:
  108803. + * Sampling rates that are multiples of 8000 kHz
  108804. + * can be driven by the oscillator of 19.2 MHz
  108805. + * with an integer divider as long as the frame length
  108806. + * is an integer divider of 19200000/8000=2400 as set up above.
  108807. + * This is no longer possible if the sampling rate
  108808. + * is too high (e.g. 192 kHz), because the oscillator is too slow.
  108809. + *
  108810. + * MASH mode:
  108811. + * For all other sampling rates, it is not possible to
  108812. + * have an integer divider. Approximate the clock
  108813. + * with the MASH module that induces a slight frequency
  108814. + * variance. To minimize that it is best to have the fastest
  108815. + * clock here. That is PLLD with 500 MHz.
  108816. + */
  108817. + target_frequency = sampling_rate * bclk_ratio;
  108818. + clk_src = BCM2708_CLK_SRC_OSC;
  108819. + mash = BCM2708_CLK_MASH_0;
  108820. +
  108821. + if (bcm2708_clk_freq[clk_src] % target_frequency == 0
  108822. + && bit_master && frame_master) {
  108823. + divi = bcm2708_clk_freq[clk_src] / target_frequency;
  108824. + divf = 0;
  108825. + } else {
  108826. + uint64_t dividend;
  108827. +
  108828. + if (!dev->bclk_ratio) {
  108829. + /*
  108830. + * Overwrite bclk_ratio, because the
  108831. + * above trick is not needed or can
  108832. + * not be used.
  108833. + */
  108834. + bclk_ratio = 2 * data_length;
  108835. + }
  108836. +
  108837. + target_frequency = sampling_rate * bclk_ratio;
  108838. +
  108839. + clk_src = BCM2708_CLK_SRC_PLLD;
  108840. + mash = BCM2708_CLK_MASH_1;
  108841. +
  108842. + dividend = bcm2708_clk_freq[clk_src];
  108843. + dividend <<= BCM2708_CLK_SHIFT;
  108844. + do_div(dividend, target_frequency);
  108845. + divi = dividend >> BCM2708_CLK_SHIFT;
  108846. + divf = dividend & BCM2708_CLK_DIVF_MASK;
  108847. + }
  108848. +
  108849. + /* Set clock divider */
  108850. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMDIV_REG, BCM2708_CLK_PASSWD
  108851. + | BCM2708_CLK_DIVI(divi)
  108852. + | BCM2708_CLK_DIVF(divf));
  108853. +
  108854. + /* Setup clock, but don't start it yet */
  108855. + regmap_write(dev->clk_regmap, BCM2708_CLK_PCMCTL_REG, BCM2708_CLK_PASSWD
  108856. + | BCM2708_CLK_MASH(mash)
  108857. + | BCM2708_CLK_SRC(clk_src));
  108858. +
  108859. + /* Setup the frame format */
  108860. + format = BCM2708_I2S_CHEN;
  108861. +
  108862. + if (data_length >= 24)
  108863. + format |= BCM2708_I2S_CHWEX;
  108864. +
  108865. + format |= BCM2708_I2S_CHWID((data_length-8)&0xf);
  108866. +
  108867. + switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  108868. + case SND_SOC_DAIFMT_I2S:
  108869. + data_delay = 1;
  108870. + break;
  108871. + default:
  108872. + /*
  108873. + * TODO
  108874. + * Others are possible but are not implemented at the moment.
  108875. + */
  108876. + dev_err(dev->dev, "%s:bad format\n", __func__);
  108877. + return -EINVAL;
  108878. + }
  108879. +
  108880. + ch1pos = data_delay;
  108881. + ch2pos = bclk_ratio / 2 + data_delay;
  108882. +
  108883. + switch (params_channels(params)) {
  108884. + case 2:
  108885. + format = BCM2708_I2S_CH1(format) | BCM2708_I2S_CH2(format);
  108886. + format |= BCM2708_I2S_CH1(BCM2708_I2S_CHPOS(ch1pos));
  108887. + format |= BCM2708_I2S_CH2(BCM2708_I2S_CHPOS(ch2pos));
  108888. + break;
  108889. + default:
  108890. + return -EINVAL;
  108891. + }
  108892. +
  108893. + /*
  108894. + * Set format for both streams.
  108895. + * We cannot set another frame length
  108896. + * (and therefore word length) anyway,
  108897. + * so the format will be the same.
  108898. + */
  108899. + regmap_write(dev->i2s_regmap, BCM2708_I2S_RXC_A_REG, format);
  108900. + regmap_write(dev->i2s_regmap, BCM2708_I2S_TXC_A_REG, format);
  108901. +
  108902. + /* Setup the I2S mode */
  108903. + mode = 0;
  108904. +
  108905. + if (data_length <= 16) {
  108906. + /*
  108907. + * Use frame packed mode (2 channels per 32 bit word)
  108908. + * We cannot set another frame length in the second stream
  108909. + * (and therefore word length) anyway,
  108910. + * so the format will be the same.
  108911. + */
  108912. + mode |= BCM2708_I2S_FTXP | BCM2708_I2S_FRXP;
  108913. + }
  108914. +
  108915. + mode |= BCM2708_I2S_FLEN(bclk_ratio - 1);
  108916. + mode |= BCM2708_I2S_FSLEN(bclk_ratio / 2);
  108917. +
  108918. + /* Master or slave? */
  108919. + switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  108920. + case SND_SOC_DAIFMT_CBS_CFS:
  108921. + /* CPU is master */
  108922. + break;
  108923. + case SND_SOC_DAIFMT_CBM_CFS:
  108924. + /*
  108925. + * CODEC is bit clock master
  108926. + * CPU is frame master
  108927. + */
  108928. + mode |= BCM2708_I2S_CLKM;
  108929. + break;
  108930. + case SND_SOC_DAIFMT_CBS_CFM:
  108931. + /*
  108932. + * CODEC is frame master
  108933. + * CPU is bit clock master
  108934. + */
  108935. + mode |= BCM2708_I2S_FSM;
  108936. + break;
  108937. + case SND_SOC_DAIFMT_CBM_CFM:
  108938. + /* CODEC is master */
  108939. + mode |= BCM2708_I2S_CLKM;
  108940. + mode |= BCM2708_I2S_FSM;
  108941. + break;
  108942. + default:
  108943. + dev_err(dev->dev, "%s:bad master\n", __func__);
  108944. + return -EINVAL;
  108945. + }
  108946. +
  108947. + /*
  108948. + * Invert clocks?
  108949. + *
  108950. + * The BCM approach seems to be inverted to the classical I2S approach.
  108951. + */
  108952. + switch (dev->fmt & SND_SOC_DAIFMT_INV_MASK) {
  108953. + case SND_SOC_DAIFMT_NB_NF:
  108954. + /* None. Therefore, both for BCM */
  108955. + mode |= BCM2708_I2S_CLKI;
  108956. + mode |= BCM2708_I2S_FSI;
  108957. + break;
  108958. + case SND_SOC_DAIFMT_IB_IF:
  108959. + /* Both. Therefore, none for BCM */
  108960. + break;
  108961. + case SND_SOC_DAIFMT_NB_IF:
  108962. + /*
  108963. + * Invert only frame sync. Therefore,
  108964. + * invert only bit clock for BCM
  108965. + */
  108966. + mode |= BCM2708_I2S_CLKI;
  108967. + break;
  108968. + case SND_SOC_DAIFMT_IB_NF:
  108969. + /*
  108970. + * Invert only bit clock. Therefore,
  108971. + * invert only frame sync for BCM
  108972. + */
  108973. + mode |= BCM2708_I2S_FSI;
  108974. + break;
  108975. + default:
  108976. + return -EINVAL;
  108977. + }
  108978. +
  108979. + regmap_write(dev->i2s_regmap, BCM2708_I2S_MODE_A_REG, mode);
  108980. +
  108981. + /* Setup the DMA parameters */
  108982. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  108983. + BCM2708_I2S_RXTHR(1)
  108984. + | BCM2708_I2S_TXTHR(1)
  108985. + | BCM2708_I2S_DMAEN, 0xffffffff);
  108986. +
  108987. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_DREQ_A_REG,
  108988. + BCM2708_I2S_TX_PANIC(0x10)
  108989. + | BCM2708_I2S_RX_PANIC(0x30)
  108990. + | BCM2708_I2S_TX(0x30)
  108991. + | BCM2708_I2S_RX(0x20), 0xffffffff);
  108992. +
  108993. + /* Clear FIFOs */
  108994. + bcm2708_i2s_clear_fifos(dev, true, true);
  108995. +
  108996. + return 0;
  108997. +}
  108998. +
  108999. +static int bcm2708_i2s_prepare(struct snd_pcm_substream *substream,
  109000. + struct snd_soc_dai *dai)
  109001. +{
  109002. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  109003. + uint32_t cs_reg;
  109004. +
  109005. + bcm2708_i2s_start_clock(dev);
  109006. +
  109007. + /*
  109008. + * Clear both FIFOs if the one that should be started
  109009. + * is not empty at the moment. This should only happen
  109010. + * after overrun. Otherwise, hw_params would have cleared
  109011. + * the FIFO.
  109012. + */
  109013. + regmap_read(dev->i2s_regmap, BCM2708_I2S_CS_A_REG, &cs_reg);
  109014. +
  109015. + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  109016. + && !(cs_reg & BCM2708_I2S_TXE))
  109017. + bcm2708_i2s_clear_fifos(dev, true, false);
  109018. + else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE
  109019. + && (cs_reg & BCM2708_I2S_RXD))
  109020. + bcm2708_i2s_clear_fifos(dev, false, true);
  109021. +
  109022. + return 0;
  109023. +}
  109024. +
  109025. +static void bcm2708_i2s_stop(struct bcm2708_i2s_dev *dev,
  109026. + struct snd_pcm_substream *substream,
  109027. + struct snd_soc_dai *dai)
  109028. +{
  109029. + uint32_t mask;
  109030. +
  109031. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  109032. + mask = BCM2708_I2S_RXON;
  109033. + else
  109034. + mask = BCM2708_I2S_TXON;
  109035. +
  109036. + regmap_update_bits(dev->i2s_regmap,
  109037. + BCM2708_I2S_CS_A_REG, mask, 0);
  109038. +
  109039. + /* Stop also the clock when not SND_SOC_DAIFMT_CONT */
  109040. + if (!dai->active && !(dev->fmt & SND_SOC_DAIFMT_CONT))
  109041. + bcm2708_i2s_stop_clock(dev);
  109042. +}
  109043. +
  109044. +static int bcm2708_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  109045. + struct snd_soc_dai *dai)
  109046. +{
  109047. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  109048. + uint32_t mask;
  109049. +
  109050. + switch (cmd) {
  109051. + case SNDRV_PCM_TRIGGER_START:
  109052. + case SNDRV_PCM_TRIGGER_RESUME:
  109053. + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  109054. + bcm2708_i2s_start_clock(dev);
  109055. +
  109056. + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  109057. + mask = BCM2708_I2S_RXON;
  109058. + else
  109059. + mask = BCM2708_I2S_TXON;
  109060. +
  109061. + regmap_update_bits(dev->i2s_regmap,
  109062. + BCM2708_I2S_CS_A_REG, mask, mask);
  109063. + break;
  109064. +
  109065. + case SNDRV_PCM_TRIGGER_STOP:
  109066. + case SNDRV_PCM_TRIGGER_SUSPEND:
  109067. + case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  109068. + bcm2708_i2s_stop(dev, substream, dai);
  109069. + break;
  109070. + default:
  109071. + return -EINVAL;
  109072. + }
  109073. +
  109074. + return 0;
  109075. +}
  109076. +
  109077. +static int bcm2708_i2s_startup(struct snd_pcm_substream *substream,
  109078. + struct snd_soc_dai *dai)
  109079. +{
  109080. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  109081. +
  109082. + if (dai->active)
  109083. + return 0;
  109084. +
  109085. + /* Should this still be running stop it */
  109086. + bcm2708_i2s_stop_clock(dev);
  109087. +
  109088. + /* Enable PCM block */
  109089. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  109090. + BCM2708_I2S_EN, BCM2708_I2S_EN);
  109091. +
  109092. + /*
  109093. + * Disable STBY.
  109094. + * Requires at least 4 PCM clock cycles to take effect.
  109095. + */
  109096. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  109097. + BCM2708_I2S_STBY, BCM2708_I2S_STBY);
  109098. +
  109099. + return 0;
  109100. +}
  109101. +
  109102. +static void bcm2708_i2s_shutdown(struct snd_pcm_substream *substream,
  109103. + struct snd_soc_dai *dai)
  109104. +{
  109105. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  109106. +
  109107. + bcm2708_i2s_stop(dev, substream, dai);
  109108. +
  109109. + /* If both streams are stopped, disable module and clock */
  109110. + if (dai->active)
  109111. + return;
  109112. +
  109113. + /* Disable the module */
  109114. + regmap_update_bits(dev->i2s_regmap, BCM2708_I2S_CS_A_REG,
  109115. + BCM2708_I2S_EN, 0);
  109116. +
  109117. + /*
  109118. + * Stopping clock is necessary, because stop does
  109119. + * not stop the clock when SND_SOC_DAIFMT_CONT
  109120. + */
  109121. + bcm2708_i2s_stop_clock(dev);
  109122. +}
  109123. +
  109124. +static const struct snd_soc_dai_ops bcm2708_i2s_dai_ops = {
  109125. + .startup = bcm2708_i2s_startup,
  109126. + .shutdown = bcm2708_i2s_shutdown,
  109127. + .prepare = bcm2708_i2s_prepare,
  109128. + .trigger = bcm2708_i2s_trigger,
  109129. + .hw_params = bcm2708_i2s_hw_params,
  109130. + .set_fmt = bcm2708_i2s_set_dai_fmt,
  109131. + .set_bclk_ratio = bcm2708_i2s_set_dai_bclk_ratio
  109132. +};
  109133. +
  109134. +static int bcm2708_i2s_dai_probe(struct snd_soc_dai *dai)
  109135. +{
  109136. + struct bcm2708_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
  109137. +
  109138. + dai->playback_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK];
  109139. + dai->capture_dma_data = &dev->dma_data[SNDRV_PCM_STREAM_CAPTURE];
  109140. +
  109141. + return 0;
  109142. +}
  109143. +
  109144. +static struct snd_soc_dai_driver bcm2708_i2s_dai = {
  109145. + .name = "bcm2708-i2s",
  109146. + .probe = bcm2708_i2s_dai_probe,
  109147. + .playback = {
  109148. + .channels_min = 2,
  109149. + .channels_max = 2,
  109150. + .rates = SNDRV_PCM_RATE_8000_192000,
  109151. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  109152. + // | SNDRV_PCM_FMTBIT_S24_LE : disable for now, it causes white noise with xbmc
  109153. + | SNDRV_PCM_FMTBIT_S32_LE
  109154. + },
  109155. + .capture = {
  109156. + .channels_min = 2,
  109157. + .channels_max = 2,
  109158. + .rates = SNDRV_PCM_RATE_8000_192000,
  109159. + .formats = SNDRV_PCM_FMTBIT_S16_LE
  109160. + | SNDRV_PCM_FMTBIT_S24_LE
  109161. + | SNDRV_PCM_FMTBIT_S32_LE
  109162. + },
  109163. + .ops = &bcm2708_i2s_dai_ops,
  109164. + .symmetric_rates = 1
  109165. +};
  109166. +
  109167. +static bool bcm2708_i2s_volatile_reg(struct device *dev, unsigned int reg)
  109168. +{
  109169. + switch (reg) {
  109170. + case BCM2708_I2S_CS_A_REG:
  109171. + case BCM2708_I2S_FIFO_A_REG:
  109172. + case BCM2708_I2S_INTSTC_A_REG:
  109173. + case BCM2708_I2S_GRAY_REG:
  109174. + return true;
  109175. + default:
  109176. + return false;
  109177. + };
  109178. +}
  109179. +
  109180. +static bool bcm2708_i2s_precious_reg(struct device *dev, unsigned int reg)
  109181. +{
  109182. + switch (reg) {
  109183. + case BCM2708_I2S_FIFO_A_REG:
  109184. + return true;
  109185. + default:
  109186. + return false;
  109187. + };
  109188. +}
  109189. +
  109190. +static bool bcm2708_clk_volatile_reg(struct device *dev, unsigned int reg)
  109191. +{
  109192. + switch (reg) {
  109193. + case BCM2708_CLK_PCMCTL_REG:
  109194. + return true;
  109195. + default:
  109196. + return false;
  109197. + };
  109198. +}
  109199. +
  109200. +static const struct regmap_config bcm2708_regmap_config[] = {
  109201. + {
  109202. + .reg_bits = 32,
  109203. + .reg_stride = 4,
  109204. + .val_bits = 32,
  109205. + .max_register = BCM2708_I2S_GRAY_REG,
  109206. + .precious_reg = bcm2708_i2s_precious_reg,
  109207. + .volatile_reg = bcm2708_i2s_volatile_reg,
  109208. + .cache_type = REGCACHE_RBTREE,
  109209. + },
  109210. + {
  109211. + .reg_bits = 32,
  109212. + .reg_stride = 4,
  109213. + .val_bits = 32,
  109214. + .max_register = BCM2708_CLK_PCMDIV_REG,
  109215. + .volatile_reg = bcm2708_clk_volatile_reg,
  109216. + .cache_type = REGCACHE_RBTREE,
  109217. + },
  109218. +};
  109219. +
  109220. +static const struct snd_soc_component_driver bcm2708_i2s_component = {
  109221. + .name = "bcm2708-i2s-comp",
  109222. +};
  109223. +
  109224. +static const struct snd_pcm_hardware bcm2708_pcm_hardware = {
  109225. + .info = SNDRV_PCM_INFO_INTERLEAVED |
  109226. + SNDRV_PCM_INFO_JOINT_DUPLEX,
  109227. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  109228. + SNDRV_PCM_FMTBIT_S24_LE |
  109229. + SNDRV_PCM_FMTBIT_S32_LE,
  109230. + .period_bytes_min = 32,
  109231. + .period_bytes_max = 64 * PAGE_SIZE,
  109232. + .periods_min = 2,
  109233. + .periods_max = 255,
  109234. + .buffer_bytes_max = 128 * PAGE_SIZE,
  109235. +};
  109236. +
  109237. +static const struct snd_dmaengine_pcm_config bcm2708_dmaengine_pcm_config = {
  109238. + .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
  109239. + .pcm_hardware = &bcm2708_pcm_hardware,
  109240. + .prealloc_buffer_size = 256 * PAGE_SIZE,
  109241. +};
  109242. +
  109243. +
  109244. +static int bcm2708_i2s_probe(struct platform_device *pdev)
  109245. +{
  109246. + struct bcm2708_i2s_dev *dev;
  109247. + int i;
  109248. + int ret;
  109249. + struct regmap *regmap[2];
  109250. + struct resource *mem[2];
  109251. +
  109252. + /* Request both ioareas */
  109253. + for (i = 0; i <= 1; i++) {
  109254. + void __iomem *base;
  109255. +
  109256. + mem[i] = platform_get_resource(pdev, IORESOURCE_MEM, i);
  109257. + base = devm_ioremap_resource(&pdev->dev, mem[i]);
  109258. + if (IS_ERR(base))
  109259. + return PTR_ERR(base);
  109260. +
  109261. + regmap[i] = devm_regmap_init_mmio(&pdev->dev, base,
  109262. + &bcm2708_regmap_config[i]);
  109263. + if (IS_ERR(regmap[i])) {
  109264. + dev_err(&pdev->dev, "I2S probe: regmap init failed\n");
  109265. + return PTR_ERR(regmap[i]);
  109266. + }
  109267. + }
  109268. +
  109269. + dev = devm_kzalloc(&pdev->dev, sizeof(*dev),
  109270. + GFP_KERNEL);
  109271. + if (IS_ERR(dev))
  109272. + return PTR_ERR(dev);
  109273. +
  109274. + dev->i2s_regmap = regmap[0];
  109275. + dev->clk_regmap = regmap[1];
  109276. +
  109277. + /* Set the DMA address */
  109278. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr =
  109279. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  109280. +
  109281. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr =
  109282. + (dma_addr_t)BCM2708_I2S_FIFO_PHYSICAL_ADDR;
  109283. +
  109284. + /* Set the DREQ */
  109285. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].slave_id =
  109286. + BCM2708_DMA_DREQ_PCM_TX;
  109287. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].slave_id =
  109288. + BCM2708_DMA_DREQ_PCM_RX;
  109289. +
  109290. + /* Set the bus width */
  109291. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].addr_width =
  109292. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  109293. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].addr_width =
  109294. + DMA_SLAVE_BUSWIDTH_4_BYTES;
  109295. +
  109296. + /* Set burst */
  109297. + dev->dma_data[SNDRV_PCM_STREAM_PLAYBACK].maxburst = 2;
  109298. + dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].maxburst = 2;
  109299. +
  109300. + /* BCLK ratio - use default */
  109301. + dev->bclk_ratio = 0;
  109302. +
  109303. + /* Store the pdev */
  109304. + dev->dev = &pdev->dev;
  109305. + dev_set_drvdata(&pdev->dev, dev);
  109306. +
  109307. + ret = snd_soc_register_component(&pdev->dev,
  109308. + &bcm2708_i2s_component, &bcm2708_i2s_dai, 1);
  109309. +
  109310. + if (ret) {
  109311. + dev_err(&pdev->dev, "Could not register DAI: %d\n", ret);
  109312. + ret = -ENOMEM;
  109313. + return ret;
  109314. + }
  109315. +
  109316. + ret = snd_dmaengine_pcm_register(&pdev->dev,
  109317. + &bcm2708_dmaengine_pcm_config,
  109318. + SND_DMAENGINE_PCM_FLAG_COMPAT);
  109319. + if (ret) {
  109320. + dev_err(&pdev->dev, "Could not register PCM: %d\n", ret);
  109321. + snd_soc_unregister_component(&pdev->dev);
  109322. + return ret;
  109323. + }
  109324. +
  109325. + return 0;
  109326. +}
  109327. +
  109328. +static int bcm2708_i2s_remove(struct platform_device *pdev)
  109329. +{
  109330. + snd_dmaengine_pcm_unregister(&pdev->dev);
  109331. + snd_soc_unregister_component(&pdev->dev);
  109332. + return 0;
  109333. +}
  109334. +
  109335. +static struct platform_driver bcm2708_i2s_driver = {
  109336. + .probe = bcm2708_i2s_probe,
  109337. + .remove = bcm2708_i2s_remove,
  109338. + .driver = {
  109339. + .name = "bcm2708-i2s",
  109340. + .owner = THIS_MODULE,
  109341. + },
  109342. +};
  109343. +
  109344. +module_platform_driver(bcm2708_i2s_driver);
  109345. +
  109346. +MODULE_ALIAS("platform:bcm2708-i2s");
  109347. +MODULE_DESCRIPTION("BCM2708 I2S interface");
  109348. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  109349. +MODULE_LICENSE("GPL v2");
  109350. diff -Nur linux-3.16.2/sound/soc/bcm/bcm2708-i2s.h linux-3.16-rpi/sound/soc/bcm/bcm2708-i2s.h
  109351. --- linux-3.16.2/sound/soc/bcm/bcm2708-i2s.h 1970-01-01 01:00:00.000000000 +0100
  109352. +++ linux-3.16-rpi/sound/soc/bcm/bcm2708-i2s.h 2014-09-14 19:04:34.000000000 +0200
  109353. @@ -0,0 +1,35 @@
  109354. +/*
  109355. + * I2S configuration for sound cards.
  109356. + *
  109357. + * Copyright (c) 2014 Daniel Matuschek <daniel@hifiberry.com>
  109358. + *
  109359. + * This program is free software; you can redistribute it and/or modify
  109360. + * it under the terms of the GNU General Public License as published by
  109361. + * the Free Software Foundation; either version 2 of the License, or
  109362. + * (at your option) any later version.
  109363. + *
  109364. + * This program is distributed in the hope that it will be useful,
  109365. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  109366. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  109367. + * GNU General Public License for more details.
  109368. + *
  109369. + * You should have received a copy of the GNU General Public License
  109370. + * along with this program; if not, write to the Free Software
  109371. + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  109372. + */
  109373. +
  109374. +#ifndef BCM2708_I2S_H
  109375. +#define BCM2708_I2S_H
  109376. +
  109377. +/* I2S pin assignment */
  109378. +#define BCM2708_I2S_GPIO_AUTO 0
  109379. +#define BCM2708_I2S_GPIO_PIN18 1
  109380. +#define BCM2708_I2S_GPIO_PIN28 2
  109381. +
  109382. +/* Alt mode to enable I2S */
  109383. +#define BCM2708_I2S_GPIO_PIN18_ALT 0
  109384. +#define BCM2708_I2S_GPIO_PIN28_ALT 2
  109385. +
  109386. +extern void bcm2708_i2s_set_gpio(int gpio);
  109387. +
  109388. +#endif
  109389. diff -Nur linux-3.16.2/sound/soc/bcm/hifiberry_dac.c linux-3.16-rpi/sound/soc/bcm/hifiberry_dac.c
  109390. --- linux-3.16.2/sound/soc/bcm/hifiberry_dac.c 1970-01-01 01:00:00.000000000 +0100
  109391. +++ linux-3.16-rpi/sound/soc/bcm/hifiberry_dac.c 2014-04-13 17:33:28.000000000 +0200
  109392. @@ -0,0 +1,100 @@
  109393. +/*
  109394. + * ASoC Driver for HifiBerry DAC
  109395. + *
  109396. + * Author: Florian Meier <florian.meier@koalo.de>
  109397. + * Copyright 2013
  109398. + *
  109399. + * This program is free software; you can redistribute it and/or
  109400. + * modify it under the terms of the GNU General Public License
  109401. + * version 2 as published by the Free Software Foundation.
  109402. + *
  109403. + * This program is distributed in the hope that it will be useful, but
  109404. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109405. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109406. + * General Public License for more details.
  109407. + */
  109408. +
  109409. +#include <linux/module.h>
  109410. +#include <linux/platform_device.h>
  109411. +
  109412. +#include <sound/core.h>
  109413. +#include <sound/pcm.h>
  109414. +#include <sound/pcm_params.h>
  109415. +#include <sound/soc.h>
  109416. +#include <sound/jack.h>
  109417. +
  109418. +static int snd_rpi_hifiberry_dac_init(struct snd_soc_pcm_runtime *rtd)
  109419. +{
  109420. + return 0;
  109421. +}
  109422. +
  109423. +static int snd_rpi_hifiberry_dac_hw_params(struct snd_pcm_substream *substream,
  109424. + struct snd_pcm_hw_params *params)
  109425. +{
  109426. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109427. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109428. +
  109429. + unsigned int sample_bits =
  109430. + snd_pcm_format_physical_width(params_format(params));
  109431. +
  109432. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  109433. +}
  109434. +
  109435. +/* machine stream operations */
  109436. +static struct snd_soc_ops snd_rpi_hifiberry_dac_ops = {
  109437. + .hw_params = snd_rpi_hifiberry_dac_hw_params,
  109438. +};
  109439. +
  109440. +static struct snd_soc_dai_link snd_rpi_hifiberry_dac_dai[] = {
  109441. +{
  109442. + .name = "HifiBerry DAC",
  109443. + .stream_name = "HifiBerry DAC HiFi",
  109444. + .cpu_dai_name = "bcm2708-i2s.0",
  109445. + .codec_dai_name = "pcm5102a-hifi",
  109446. + .platform_name = "bcm2708-i2s.0",
  109447. + .codec_name = "pcm5102a-codec",
  109448. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  109449. + SND_SOC_DAIFMT_CBS_CFS,
  109450. + .ops = &snd_rpi_hifiberry_dac_ops,
  109451. + .init = snd_rpi_hifiberry_dac_init,
  109452. +},
  109453. +};
  109454. +
  109455. +/* audio machine driver */
  109456. +static struct snd_soc_card snd_rpi_hifiberry_dac = {
  109457. + .name = "snd_rpi_hifiberry_dac",
  109458. + .dai_link = snd_rpi_hifiberry_dac_dai,
  109459. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dac_dai),
  109460. +};
  109461. +
  109462. +static int snd_rpi_hifiberry_dac_probe(struct platform_device *pdev)
  109463. +{
  109464. + int ret = 0;
  109465. +
  109466. + snd_rpi_hifiberry_dac.dev = &pdev->dev;
  109467. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dac);
  109468. + if (ret)
  109469. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  109470. +
  109471. + return ret;
  109472. +}
  109473. +
  109474. +static int snd_rpi_hifiberry_dac_remove(struct platform_device *pdev)
  109475. +{
  109476. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dac);
  109477. +}
  109478. +
  109479. +static struct platform_driver snd_rpi_hifiberry_dac_driver = {
  109480. + .driver = {
  109481. + .name = "snd-hifiberry-dac",
  109482. + .owner = THIS_MODULE,
  109483. + },
  109484. + .probe = snd_rpi_hifiberry_dac_probe,
  109485. + .remove = snd_rpi_hifiberry_dac_remove,
  109486. +};
  109487. +
  109488. +module_platform_driver(snd_rpi_hifiberry_dac_driver);
  109489. +
  109490. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  109491. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry DAC");
  109492. +MODULE_LICENSE("GPL v2");
  109493. diff -Nur linux-3.16.2/sound/soc/bcm/hifiberry_dacplus.c linux-3.16-rpi/sound/soc/bcm/hifiberry_dacplus.c
  109494. --- linux-3.16.2/sound/soc/bcm/hifiberry_dacplus.c 1970-01-01 01:00:00.000000000 +0100
  109495. +++ linux-3.16-rpi/sound/soc/bcm/hifiberry_dacplus.c 2014-09-14 19:04:34.000000000 +0200
  109496. @@ -0,0 +1,119 @@
  109497. +/*
  109498. + * ASoC Driver for HiFiBerry DAC+
  109499. + *
  109500. + * Author: Daniel Matuschek
  109501. + * Copyright 2014
  109502. + * based on code by Florian Meier <florian.meier@koalo.de>
  109503. + *
  109504. + * This program is free software; you can redistribute it and/or
  109505. + * modify it under the terms of the GNU General Public License
  109506. + * version 2 as published by the Free Software Foundation.
  109507. + *
  109508. + * This program is distributed in the hope that it will be useful, but
  109509. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109510. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109511. + * General Public License for more details.
  109512. + */
  109513. +
  109514. +#include <linux/module.h>
  109515. +#include <linux/platform_device.h>
  109516. +
  109517. +#include <sound/core.h>
  109518. +#include <sound/pcm.h>
  109519. +#include <sound/pcm_params.h>
  109520. +#include <sound/soc.h>
  109521. +#include <sound/jack.h>
  109522. +
  109523. +#include "../codecs/pcm512x.h"
  109524. +
  109525. +static int snd_rpi_hifiberry_dacplus_init(struct snd_soc_pcm_runtime *rtd)
  109526. +{
  109527. + struct snd_soc_codec *codec = rtd->codec;
  109528. + snd_soc_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
  109529. + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0xf, 0x02);
  109530. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
  109531. + return 0;
  109532. +}
  109533. +
  109534. +static int snd_rpi_hifiberry_dacplus_hw_params(struct snd_pcm_substream *substream,
  109535. + struct snd_pcm_hw_params *params)
  109536. +{
  109537. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109538. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109539. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
  109540. +}
  109541. +
  109542. +static int snd_rpi_hifiberry_dacplus_startup(struct snd_pcm_substream *substream) {
  109543. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109544. + struct snd_soc_codec *codec = rtd->codec;
  109545. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
  109546. + return 0;
  109547. +}
  109548. +
  109549. +static void snd_rpi_hifiberry_dacplus_shutdown(struct snd_pcm_substream *substream) {
  109550. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109551. + struct snd_soc_codec *codec = rtd->codec;
  109552. + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x00);
  109553. +}
  109554. +
  109555. +/* machine stream operations */
  109556. +static struct snd_soc_ops snd_rpi_hifiberry_dacplus_ops = {
  109557. + .hw_params = snd_rpi_hifiberry_dacplus_hw_params,
  109558. + .startup = snd_rpi_hifiberry_dacplus_startup,
  109559. + .shutdown = snd_rpi_hifiberry_dacplus_shutdown,
  109560. +};
  109561. +
  109562. +static struct snd_soc_dai_link snd_rpi_hifiberry_dacplus_dai[] = {
  109563. +{
  109564. + .name = "HiFiBerry DAC+",
  109565. + .stream_name = "HiFiBerry DAC+ HiFi",
  109566. + .cpu_dai_name = "bcm2708-i2s.0",
  109567. + .codec_dai_name = "pcm512x-hifi",
  109568. + .platform_name = "bcm2708-i2s.0",
  109569. + .codec_name = "pcm512x.1-004d",
  109570. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  109571. + SND_SOC_DAIFMT_CBS_CFS,
  109572. + .ops = &snd_rpi_hifiberry_dacplus_ops,
  109573. + .init = snd_rpi_hifiberry_dacplus_init,
  109574. +},
  109575. +};
  109576. +
  109577. +/* audio machine driver */
  109578. +static struct snd_soc_card snd_rpi_hifiberry_dacplus = {
  109579. + .name = "snd_rpi_hifiberry_dacplus",
  109580. + .dai_link = snd_rpi_hifiberry_dacplus_dai,
  109581. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dacplus_dai),
  109582. +};
  109583. +
  109584. +static int snd_rpi_hifiberry_dacplus_probe(struct platform_device *pdev)
  109585. +{
  109586. + int ret = 0;
  109587. +
  109588. + snd_rpi_hifiberry_dacplus.dev = &pdev->dev;
  109589. + ret = snd_soc_register_card(&snd_rpi_hifiberry_dacplus);
  109590. + if (ret)
  109591. + dev_err(&pdev->dev,
  109592. + "snd_soc_register_card() failed: %d\n", ret);
  109593. +
  109594. + return ret;
  109595. +}
  109596. +
  109597. +static int snd_rpi_hifiberry_dacplus_remove(struct platform_device *pdev)
  109598. +{
  109599. + return snd_soc_unregister_card(&snd_rpi_hifiberry_dacplus);
  109600. +}
  109601. +
  109602. +static struct platform_driver snd_rpi_hifiberry_dacplus_driver = {
  109603. + .driver = {
  109604. + .name = "snd-rpi-hifiberry-dacplus",
  109605. + .owner = THIS_MODULE,
  109606. + },
  109607. + .probe = snd_rpi_hifiberry_dacplus_probe,
  109608. + .remove = snd_rpi_hifiberry_dacplus_remove,
  109609. +};
  109610. +
  109611. +module_platform_driver(snd_rpi_hifiberry_dacplus_driver);
  109612. +
  109613. +MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
  109614. +MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+");
  109615. +MODULE_LICENSE("GPL v2");
  109616. diff -Nur linux-3.16.2/sound/soc/bcm/hifiberry_digi.c linux-3.16-rpi/sound/soc/bcm/hifiberry_digi.c
  109617. --- linux-3.16.2/sound/soc/bcm/hifiberry_digi.c 1970-01-01 01:00:00.000000000 +0100
  109618. +++ linux-3.16-rpi/sound/soc/bcm/hifiberry_digi.c 2014-09-14 19:04:34.000000000 +0200
  109619. @@ -0,0 +1,153 @@
  109620. +/*
  109621. + * ASoC Driver for HifiBerry Digi
  109622. + *
  109623. + * Author: Daniel Matuschek <info@crazy-audio.com>
  109624. + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
  109625. + * Copyright 2013
  109626. + *
  109627. + * This program is free software; you can redistribute it and/or
  109628. + * modify it under the terms of the GNU General Public License
  109629. + * version 2 as published by the Free Software Foundation.
  109630. + *
  109631. + * This program is distributed in the hope that it will be useful, but
  109632. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109633. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109634. + * General Public License for more details.
  109635. + */
  109636. +
  109637. +#include <linux/module.h>
  109638. +#include <linux/platform_device.h>
  109639. +
  109640. +#include <sound/core.h>
  109641. +#include <sound/pcm.h>
  109642. +#include <sound/pcm_params.h>
  109643. +#include <sound/soc.h>
  109644. +#include <sound/jack.h>
  109645. +
  109646. +#include "../codecs/wm8804.h"
  109647. +
  109648. +static int samplerate=44100;
  109649. +
  109650. +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
  109651. +{
  109652. + struct snd_soc_codec *codec = rtd->codec;
  109653. +
  109654. + /* enable TX output */
  109655. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  109656. +
  109657. + return 0;
  109658. +}
  109659. +
  109660. +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
  109661. + struct snd_pcm_hw_params *params)
  109662. +{
  109663. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109664. + struct snd_soc_dai *codec_dai = rtd->codec_dai;
  109665. + struct snd_soc_codec *codec = rtd->codec;
  109666. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109667. +
  109668. + int sysclk = 27000000; /* This is fixed on this board */
  109669. +
  109670. + long mclk_freq=0;
  109671. + int mclk_div=1;
  109672. +
  109673. + int ret;
  109674. +
  109675. + samplerate = params_rate(params);
  109676. +
  109677. + switch (samplerate) {
  109678. + case 44100:
  109679. + case 48000:
  109680. + case 88200:
  109681. + case 96000:
  109682. + mclk_freq=samplerate*256;
  109683. + mclk_div=WM8804_MCLKDIV_256FS;
  109684. + break;
  109685. + case 176400:
  109686. + case 192000:
  109687. + mclk_freq=samplerate*128;
  109688. + mclk_div=WM8804_MCLKDIV_128FS;
  109689. + break;
  109690. + default:
  109691. + dev_err(substream->pcm->dev,
  109692. + "Failed to set WM8804 SYSCLK, unsupported samplerate\n");
  109693. + }
  109694. +
  109695. + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
  109696. + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
  109697. +
  109698. + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
  109699. + sysclk, SND_SOC_CLOCK_OUT);
  109700. + if (ret < 0) {
  109701. + dev_err(substream->pcm->dev,
  109702. + "Failed to set WM8804 SYSCLK: %d\n", ret);
  109703. + return ret;
  109704. + }
  109705. +
  109706. + /* Enable TX output */
  109707. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
  109708. +
  109709. + /* Power on */
  109710. + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
  109711. +
  109712. + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
  109713. +}
  109714. +
  109715. +/* machine stream operations */
  109716. +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
  109717. + .hw_params = snd_rpi_hifiberry_digi_hw_params,
  109718. +};
  109719. +
  109720. +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
  109721. +{
  109722. + .name = "HifiBerry Digi",
  109723. + .stream_name = "HifiBerry Digi HiFi",
  109724. + .cpu_dai_name = "bcm2708-i2s.0",
  109725. + .codec_dai_name = "wm8804-spdif",
  109726. + .platform_name = "bcm2708-i2s.0",
  109727. + .codec_name = "wm8804.1-003b",
  109728. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  109729. + SND_SOC_DAIFMT_CBM_CFM,
  109730. + .ops = &snd_rpi_hifiberry_digi_ops,
  109731. + .init = snd_rpi_hifiberry_digi_init,
  109732. +},
  109733. +};
  109734. +
  109735. +/* audio machine driver */
  109736. +static struct snd_soc_card snd_rpi_hifiberry_digi = {
  109737. + .name = "snd_rpi_hifiberry_digi",
  109738. + .dai_link = snd_rpi_hifiberry_digi_dai,
  109739. + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
  109740. +};
  109741. +
  109742. +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
  109743. +{
  109744. + int ret = 0;
  109745. +
  109746. + snd_rpi_hifiberry_digi.dev = &pdev->dev;
  109747. + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
  109748. + if (ret)
  109749. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  109750. +
  109751. + return ret;
  109752. +}
  109753. +
  109754. +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
  109755. +{
  109756. + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
  109757. +}
  109758. +
  109759. +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
  109760. + .driver = {
  109761. + .name = "snd-hifiberry-digi",
  109762. + .owner = THIS_MODULE,
  109763. + },
  109764. + .probe = snd_rpi_hifiberry_digi_probe,
  109765. + .remove = snd_rpi_hifiberry_digi_remove,
  109766. +};
  109767. +
  109768. +module_platform_driver(snd_rpi_hifiberry_digi_driver);
  109769. +
  109770. +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
  109771. +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
  109772. +MODULE_LICENSE("GPL v2");
  109773. diff -Nur linux-3.16.2/sound/soc/bcm/iqaudio-dac.c linux-3.16-rpi/sound/soc/bcm/iqaudio-dac.c
  109774. --- linux-3.16.2/sound/soc/bcm/iqaudio-dac.c 1970-01-01 01:00:00.000000000 +0100
  109775. +++ linux-3.16-rpi/sound/soc/bcm/iqaudio-dac.c 2014-09-14 19:04:34.000000000 +0200
  109776. @@ -0,0 +1,111 @@
  109777. +/*
  109778. + * ASoC Driver for IQaudIO DAC
  109779. + *
  109780. + * Author: Florian Meier <florian.meier@koalo.de>
  109781. + * Copyright 2013
  109782. + *
  109783. + * This program is free software; you can redistribute it and/or
  109784. + * modify it under the terms of the GNU General Public License
  109785. + * version 2 as published by the Free Software Foundation.
  109786. + *
  109787. + * This program is distributed in the hope that it will be useful, but
  109788. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109789. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109790. + * General Public License for more details.
  109791. + */
  109792. +
  109793. +#include <linux/module.h>
  109794. +#include <linux/platform_device.h>
  109795. +
  109796. +#include <sound/core.h>
  109797. +#include <sound/pcm.h>
  109798. +#include <sound/pcm_params.h>
  109799. +#include <sound/soc.h>
  109800. +#include <sound/jack.h>
  109801. +
  109802. +static int snd_rpi_iqaudio_dac_init(struct snd_soc_pcm_runtime *rtd)
  109803. +{
  109804. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  109805. +
  109806. + return 0;
  109807. +}
  109808. +
  109809. +static int snd_rpi_iqaudio_dac_hw_params(struct snd_pcm_substream *substream,
  109810. + struct snd_pcm_hw_params *params)
  109811. +{
  109812. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109813. +// NOT USED struct snd_soc_dai *codec_dai = rtd->codec_dai;
  109814. +// NOT USED struct snd_soc_codec *codec = rtd->codec;
  109815. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109816. +
  109817. + unsigned int sample_bits =
  109818. + snd_pcm_format_physical_width(params_format(params));
  109819. +
  109820. + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
  109821. +}
  109822. +
  109823. +/* machine stream operations */
  109824. +static struct snd_soc_ops snd_rpi_iqaudio_dac_ops = {
  109825. + .hw_params = snd_rpi_iqaudio_dac_hw_params,
  109826. +};
  109827. +
  109828. +static struct snd_soc_dai_link snd_rpi_iqaudio_dac_dai[] = {
  109829. +{
  109830. + .name = "IQaudIO DAC",
  109831. + .stream_name = "IQaudIO DAC HiFi",
  109832. + .cpu_dai_name = "bcm2708-i2s.0",
  109833. + .codec_dai_name = "pcm512x-hifi",
  109834. + .platform_name = "bcm2708-i2s.0",
  109835. + .codec_name = "pcm512x.1-004c",
  109836. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  109837. + SND_SOC_DAIFMT_CBS_CFS,
  109838. + .ops = &snd_rpi_iqaudio_dac_ops,
  109839. + .init = snd_rpi_iqaudio_dac_init,
  109840. +},
  109841. +};
  109842. +
  109843. +/* audio machine driver */
  109844. +static struct snd_soc_card snd_rpi_iqaudio_dac = {
  109845. + .name = "IQaudIODAC",
  109846. + .dai_link = snd_rpi_iqaudio_dac_dai,
  109847. + .num_links = ARRAY_SIZE(snd_rpi_iqaudio_dac_dai),
  109848. +};
  109849. +
  109850. +static int snd_rpi_iqaudio_dac_probe(struct platform_device *pdev)
  109851. +{
  109852. + int ret = 0;
  109853. +
  109854. + snd_rpi_iqaudio_dac.dev = &pdev->dev;
  109855. + ret = snd_soc_register_card(&snd_rpi_iqaudio_dac);
  109856. + if (ret)
  109857. + dev_err(&pdev->dev,
  109858. + "snd_soc_register_card() failed: %d\n", ret);
  109859. +
  109860. + return ret;
  109861. +}
  109862. +
  109863. +static int snd_rpi_iqaudio_dac_remove(struct platform_device *pdev)
  109864. +{
  109865. + return snd_soc_unregister_card(&snd_rpi_iqaudio_dac);
  109866. +}
  109867. +
  109868. +static const struct of_device_id iqaudio_of_match[] = {
  109869. + { .compatible = "iqaudio,iqaudio-dac", },
  109870. + {},
  109871. +};
  109872. +
  109873. +static struct platform_driver snd_rpi_iqaudio_dac_driver = {
  109874. + .driver = {
  109875. + .name = "snd-rpi-iqaudio-dac",
  109876. + .owner = THIS_MODULE,
  109877. + .of_match_table = iqaudio_of_match,
  109878. + },
  109879. + .probe = snd_rpi_iqaudio_dac_probe,
  109880. + .remove = snd_rpi_iqaudio_dac_remove,
  109881. +};
  109882. +
  109883. +module_platform_driver(snd_rpi_iqaudio_dac_driver);
  109884. +
  109885. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  109886. +MODULE_DESCRIPTION("ASoC Driver for IQAudio DAC");
  109887. +MODULE_LICENSE("GPL v2");
  109888. diff -Nur linux-3.16.2/sound/soc/bcm/Kconfig linux-3.16-rpi/sound/soc/bcm/Kconfig
  109889. --- linux-3.16.2/sound/soc/bcm/Kconfig 2014-09-06 01:37:11.000000000 +0200
  109890. +++ linux-3.16-rpi/sound/soc/bcm/Kconfig 2014-09-15 06:01:48.000000000 +0200
  109891. @@ -7,3 +7,42 @@
  109892. Say Y or M if you want to add support for codecs attached to
  109893. the BCM2835 I2S interface. You will also need
  109894. to select the audio interfaces to support below.
  109895. +
  109896. +config SND_BCM2708_SOC_I2S
  109897. + tristate "SoC Audio support for the Broadcom BCM2708 I2S module"
  109898. + depends on MACH_BCM2708
  109899. + select REGMAP_MMIO
  109900. + select SND_SOC_DMAENGINE_PCM
  109901. + select SND_SOC_GENERIC_DMAENGINE_PCM
  109902. + help
  109903. + Say Y or M if you want to add support for codecs attached to
  109904. + the BCM2708 I2S interface. You will also need
  109905. + to select the audio interfaces to support below.
  109906. +
  109907. +config SND_BCM2708_SOC_HIFIBERRY_DAC
  109908. + tristate "Support for HifiBerry DAC"
  109909. + depends on SND_BCM2708_SOC_I2S
  109910. + select SND_SOC_PCM5102A
  109911. + help
  109912. + Say Y or M if you want to add support for HifiBerry DAC.
  109913. +
  109914. +config SND_BCM2708_SOC_HIFIBERRY_DIGI
  109915. + tristate "Support for HifiBerry Digi"
  109916. + depends on SND_BCM2708_SOC_I2S
  109917. + select SND_SOC_WM8804
  109918. + help
  109919. + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
  109920. +
  109921. +config SND_BCM2708_SOC_RPI_DAC
  109922. + tristate "Support for RPi-DAC"
  109923. + depends on SND_BCM2708_SOC_I2S
  109924. + select SND_SOC_PCM1794A
  109925. + help
  109926. + Say Y or M if you want to add support for RPi-DAC.
  109927. +
  109928. +config SND_BCM2708_SOC_IQAUDIO_DAC
  109929. + tristate "Support for IQaudIO-DAC"
  109930. + depends on SND_BCM2708_SOC_I2S
  109931. + select SND_SOC_PCM512x_I2C
  109932. + help
  109933. + Say Y or M if you want to add support for IQaudIO-DAC.
  109934. diff -Nur linux-3.16.2/sound/soc/bcm/Makefile linux-3.16-rpi/sound/soc/bcm/Makefile
  109935. --- linux-3.16.2/sound/soc/bcm/Makefile 2014-09-06 01:37:11.000000000 +0200
  109936. +++ linux-3.16-rpi/sound/soc/bcm/Makefile 2014-09-15 06:01:36.000000000 +0200
  109937. @@ -3,3 +3,18 @@
  109938. obj-$(CONFIG_SND_BCM2835_SOC_I2S) += snd-soc-bcm2835-i2s.o
  109939. +# BCM2708 Platform Support
  109940. +snd-soc-bcm2708-i2s-objs := bcm2708-i2s.o
  109941. +
  109942. +obj-$(CONFIG_SND_BCM2708_SOC_I2S) += snd-soc-bcm2708-i2s.o
  109943. +
  109944. +# BCM2708 Machine Support
  109945. +snd-soc-hifiberry-dac-objs := hifiberry_dac.o
  109946. +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
  109947. +snd-soc-rpi-dac-objs := rpi-dac.o
  109948. +snd-soc-iqaudio-dac-objs := iqaudio-dac.o
  109949. +
  109950. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
  109951. +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
  109952. +obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
  109953. +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
  109954. diff -Nur linux-3.16.2/sound/soc/bcm/rpi-dac.c linux-3.16-rpi/sound/soc/bcm/rpi-dac.c
  109955. --- linux-3.16.2/sound/soc/bcm/rpi-dac.c 1970-01-01 01:00:00.000000000 +0100
  109956. +++ linux-3.16-rpi/sound/soc/bcm/rpi-dac.c 2014-09-14 19:04:34.000000000 +0200
  109957. @@ -0,0 +1,97 @@
  109958. +/*
  109959. + * ASoC Driver for RPi-DAC.
  109960. + *
  109961. + * Author: Florian Meier <florian.meier@koalo.de>
  109962. + * Copyright 2013
  109963. + *
  109964. + * This program is free software; you can redistribute it and/or
  109965. + * modify it under the terms of the GNU General Public License
  109966. + * version 2 as published by the Free Software Foundation.
  109967. + *
  109968. + * This program is distributed in the hope that it will be useful, but
  109969. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  109970. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  109971. + * General Public License for more details.
  109972. + */
  109973. +
  109974. +#include <linux/module.h>
  109975. +#include <linux/platform_device.h>
  109976. +
  109977. +#include <sound/core.h>
  109978. +#include <sound/pcm.h>
  109979. +#include <sound/pcm_params.h>
  109980. +#include <sound/soc.h>
  109981. +#include <sound/jack.h>
  109982. +
  109983. +static int snd_rpi_rpi_dac_init(struct snd_soc_pcm_runtime *rtd)
  109984. +{
  109985. + return 0;
  109986. +}
  109987. +
  109988. +static int snd_rpi_rpi_dac_hw_params(struct snd_pcm_substream *substream,
  109989. + struct snd_pcm_hw_params *params)
  109990. +{
  109991. + struct snd_soc_pcm_runtime *rtd = substream->private_data;
  109992. + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  109993. +
  109994. + return snd_soc_dai_set_bclk_ratio(cpu_dai, 32*2);
  109995. +}
  109996. +
  109997. +/* machine stream operations */
  109998. +static struct snd_soc_ops snd_rpi_rpi_dac_ops = {
  109999. + .hw_params = snd_rpi_rpi_dac_hw_params,
  110000. +};
  110001. +
  110002. +static struct snd_soc_dai_link snd_rpi_rpi_dac_dai[] = {
  110003. +{
  110004. + .name = "RPi-DAC",
  110005. + .stream_name = "RPi-DAC HiFi",
  110006. + .cpu_dai_name = "bcm2708-i2s.0",
  110007. + .codec_dai_name = "pcm1794a-hifi",
  110008. + .platform_name = "bcm2708-i2s.0",
  110009. + .codec_name = "pcm1794a-codec",
  110010. + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  110011. + SND_SOC_DAIFMT_CBS_CFS,
  110012. + .ops = &snd_rpi_rpi_dac_ops,
  110013. + .init = snd_rpi_rpi_dac_init,
  110014. +},
  110015. +};
  110016. +
  110017. +/* audio machine driver */
  110018. +static struct snd_soc_card snd_rpi_rpi_dac = {
  110019. + .name = "snd_rpi_rpi_dac",
  110020. + .dai_link = snd_rpi_rpi_dac_dai,
  110021. + .num_links = ARRAY_SIZE(snd_rpi_rpi_dac_dai),
  110022. +};
  110023. +
  110024. +static int snd_rpi_rpi_dac_probe(struct platform_device *pdev)
  110025. +{
  110026. + int ret = 0;
  110027. +
  110028. + snd_rpi_rpi_dac.dev = &pdev->dev;
  110029. + ret = snd_soc_register_card(&snd_rpi_rpi_dac);
  110030. + if (ret)
  110031. + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
  110032. +
  110033. + return ret;
  110034. +}
  110035. +
  110036. +static int snd_rpi_rpi_dac_remove(struct platform_device *pdev)
  110037. +{
  110038. + return snd_soc_unregister_card(&snd_rpi_rpi_dac);
  110039. +}
  110040. +
  110041. +static struct platform_driver snd_rpi_rpi_dac_driver = {
  110042. + .driver = {
  110043. + .name = "snd-rpi-dac",
  110044. + .owner = THIS_MODULE,
  110045. + },
  110046. + .probe = snd_rpi_rpi_dac_probe,
  110047. + .remove = snd_rpi_rpi_dac_remove,
  110048. +};
  110049. +
  110050. +module_platform_driver(snd_rpi_rpi_dac_driver);
  110051. +
  110052. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  110053. +MODULE_DESCRIPTION("ASoC Driver for RPi-DAC");
  110054. +MODULE_LICENSE("GPL v2");
  110055. diff -Nur linux-3.16.2/sound/soc/codecs/Kconfig linux-3.16-rpi/sound/soc/codecs/Kconfig
  110056. --- linux-3.16.2/sound/soc/codecs/Kconfig 2014-09-06 01:37:11.000000000 +0200
  110057. +++ linux-3.16-rpi/sound/soc/codecs/Kconfig 2014-09-15 06:00:43.000000000 +0200
  110058. @@ -72,8 +72,9 @@
  110059. select SND_SOC_PCM1681 if I2C
  110060. select SND_SOC_PCM1792A if SPI_MASTER
  110061. select SND_SOC_PCM3008
  110062. - select SND_SOC_PCM512x_I2C if I2C
  110063. select SND_SOC_PCM512x_SPI if SPI_MASTER
  110064. + select SND_SOC_PCM1794A
  110065. + select SND_SOC_PCM5102A
  110066. select SND_SOC_RT5631 if I2C
  110067. select SND_SOC_RT5640 if I2C
  110068. select SND_SOC_RT5645 if I2C
  110069. @@ -449,6 +444,12 @@
  110070. default m if SND_SOC_RT5645=m
  110071. default m if SND_SOC_RT5651=m
  110072. +config SND_SOC_PCM1794A
  110073. + tristate
  110074. +
  110075. +config SND_SOC_PCM5102A
  110076. + tristate
  110077. +
  110078. config SND_SOC_RT5631
  110079. tristate
  110080. diff -Nur linux-3.16.2/sound/soc/codecs/Makefile linux-3.16-rpi/sound/soc/codecs/Makefile
  110081. --- linux-3.16.2/sound/soc/codecs/Makefile 2014-09-06 01:37:11.000000000 +0200
  110082. +++ linux-3.16-rpi/sound/soc/codecs/Makefile 2014-09-14 19:04:35.000000000 +0200
  110083. @@ -68,6 +68,8 @@
  110084. snd-soc-pcm512x-i2c-objs := pcm512x-i2c.o
  110085. snd-soc-pcm512x-spi-objs := pcm512x-spi.o
  110086. snd-soc-rl6231-objs := rl6231.o
  110087. +snd-soc-pcm1794a-objs := pcm1794a.o
  110088. +snd-soc-pcm5102a-objs := pcm5102a.o
  110089. snd-soc-rt5631-objs := rt5631.o
  110090. snd-soc-rt5640-objs := rt5640.o
  110091. snd-soc-rt5645-objs := rt5645.o
  110092. @@ -235,6 +237,8 @@
  110093. obj-$(CONFIG_SND_SOC_PCM512x_I2C) += snd-soc-pcm512x-i2c.o
  110094. obj-$(CONFIG_SND_SOC_PCM512x_SPI) += snd-soc-pcm512x-spi.o
  110095. obj-$(CONFIG_SND_SOC_RL6231) += snd-soc-rl6231.o
  110096. +obj-$(CONFIG_SND_SOC_PCM1794A) += snd-soc-pcm1794a.o
  110097. +obj-$(CONFIG_SND_SOC_PCM5102A) += snd-soc-pcm5102a.o
  110098. obj-$(CONFIG_SND_SOC_RT5631) += snd-soc-rt5631.o
  110099. obj-$(CONFIG_SND_SOC_RT5640) += snd-soc-rt5640.o
  110100. obj-$(CONFIG_SND_SOC_RT5645) += snd-soc-rt5645.o
  110101. diff -Nur linux-3.16.2/sound/soc/codecs/pcm1794a.c linux-3.16-rpi/sound/soc/codecs/pcm1794a.c
  110102. --- linux-3.16.2/sound/soc/codecs/pcm1794a.c 1970-01-01 01:00:00.000000000 +0100
  110103. +++ linux-3.16-rpi/sound/soc/codecs/pcm1794a.c 2014-04-13 17:33:29.000000000 +0200
  110104. @@ -0,0 +1,62 @@
  110105. +/*
  110106. + * Driver for the PCM1794A codec
  110107. + *
  110108. + * Author: Florian Meier <florian.meier@koalo.de>
  110109. + * Copyright 2013
  110110. + *
  110111. + * This program is free software; you can redistribute it and/or
  110112. + * modify it under the terms of the GNU General Public License
  110113. + * version 2 as published by the Free Software Foundation.
  110114. + *
  110115. + * This program is distributed in the hope that it will be useful, but
  110116. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  110117. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  110118. + * General Public License for more details.
  110119. + */
  110120. +
  110121. +
  110122. +#include <linux/init.h>
  110123. +#include <linux/module.h>
  110124. +#include <linux/platform_device.h>
  110125. +
  110126. +#include <sound/soc.h>
  110127. +
  110128. +static struct snd_soc_dai_driver pcm1794a_dai = {
  110129. + .name = "pcm1794a-hifi",
  110130. + .playback = {
  110131. + .channels_min = 2,
  110132. + .channels_max = 2,
  110133. + .rates = SNDRV_PCM_RATE_8000_192000,
  110134. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  110135. + SNDRV_PCM_FMTBIT_S24_LE
  110136. + },
  110137. +};
  110138. +
  110139. +static struct snd_soc_codec_driver soc_codec_dev_pcm1794a;
  110140. +
  110141. +static int pcm1794a_probe(struct platform_device *pdev)
  110142. +{
  110143. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm1794a,
  110144. + &pcm1794a_dai, 1);
  110145. +}
  110146. +
  110147. +static int pcm1794a_remove(struct platform_device *pdev)
  110148. +{
  110149. + snd_soc_unregister_codec(&pdev->dev);
  110150. + return 0;
  110151. +}
  110152. +
  110153. +static struct platform_driver pcm1794a_codec_driver = {
  110154. + .probe = pcm1794a_probe,
  110155. + .remove = pcm1794a_remove,
  110156. + .driver = {
  110157. + .name = "pcm1794a-codec",
  110158. + .owner = THIS_MODULE,
  110159. + },
  110160. +};
  110161. +
  110162. +module_platform_driver(pcm1794a_codec_driver);
  110163. +
  110164. +MODULE_DESCRIPTION("ASoC PCM1794A codec driver");
  110165. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  110166. +MODULE_LICENSE("GPL v2");
  110167. diff -Nur linux-3.16.2/sound/soc/codecs/pcm5102a.c linux-3.16-rpi/sound/soc/codecs/pcm5102a.c
  110168. --- linux-3.16.2/sound/soc/codecs/pcm5102a.c 1970-01-01 01:00:00.000000000 +0100
  110169. +++ linux-3.16-rpi/sound/soc/codecs/pcm5102a.c 2014-04-13 17:33:29.000000000 +0200
  110170. @@ -0,0 +1,63 @@
  110171. +/*
  110172. + * Driver for the PCM5102A codec
  110173. + *
  110174. + * Author: Florian Meier <florian.meier@koalo.de>
  110175. + * Copyright 2013
  110176. + *
  110177. + * This program is free software; you can redistribute it and/or
  110178. + * modify it under the terms of the GNU General Public License
  110179. + * version 2 as published by the Free Software Foundation.
  110180. + *
  110181. + * This program is distributed in the hope that it will be useful, but
  110182. + * WITHOUT ANY WARRANTY; without even the implied warranty of
  110183. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  110184. + * General Public License for more details.
  110185. + */
  110186. +
  110187. +
  110188. +#include <linux/init.h>
  110189. +#include <linux/module.h>
  110190. +#include <linux/platform_device.h>
  110191. +
  110192. +#include <sound/soc.h>
  110193. +
  110194. +static struct snd_soc_dai_driver pcm5102a_dai = {
  110195. + .name = "pcm5102a-hifi",
  110196. + .playback = {
  110197. + .channels_min = 2,
  110198. + .channels_max = 2,
  110199. + .rates = SNDRV_PCM_RATE_8000_192000,
  110200. + .formats = SNDRV_PCM_FMTBIT_S16_LE |
  110201. + // SNDRV_PCM_FMTBIT_S24_LE | : disable for now, it causes white noise with xbmc
  110202. + SNDRV_PCM_FMTBIT_S32_LE
  110203. + },
  110204. +};
  110205. +
  110206. +static struct snd_soc_codec_driver soc_codec_dev_pcm5102a;
  110207. +
  110208. +static int pcm5102a_probe(struct platform_device *pdev)
  110209. +{
  110210. + return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_pcm5102a,
  110211. + &pcm5102a_dai, 1);
  110212. +}
  110213. +
  110214. +static int pcm5102a_remove(struct platform_device *pdev)
  110215. +{
  110216. + snd_soc_unregister_codec(&pdev->dev);
  110217. + return 0;
  110218. +}
  110219. +
  110220. +static struct platform_driver pcm5102a_codec_driver = {
  110221. + .probe = pcm5102a_probe,
  110222. + .remove = pcm5102a_remove,
  110223. + .driver = {
  110224. + .name = "pcm5102a-codec",
  110225. + .owner = THIS_MODULE,
  110226. + },
  110227. +};
  110228. +
  110229. +module_platform_driver(pcm5102a_codec_driver);
  110230. +
  110231. +MODULE_DESCRIPTION("ASoC PCM5102A codec driver");
  110232. +MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
  110233. +MODULE_LICENSE("GPL v2");
  110234. diff -Nur linux-3.16.2/sound/soc/codecs/pcm512x.c linux-3.16-rpi/sound/soc/codecs/pcm512x.c
  110235. --- linux-3.16.2/sound/soc/codecs/pcm512x.c 2014-09-06 01:37:11.000000000 +0200
  110236. +++ linux-3.16-rpi/sound/soc/codecs/pcm512x.c 2014-09-14 19:04:35.000000000 +0200
  110237. @@ -259,8 +259,8 @@
  110238. pcm512x_ramp_step_text);
  110239. static const struct snd_kcontrol_new pcm512x_controls[] = {
  110240. -SOC_DOUBLE_R_TLV("Playback Digital Volume", PCM512x_DIGITAL_VOLUME_2,
  110241. - PCM512x_DIGITAL_VOLUME_3, 0, 255, 1, digital_tlv),
  110242. +SOC_DOUBLE_R_RANGE_TLV("PCM", PCM512x_DIGITAL_VOLUME_2,
  110243. + PCM512x_DIGITAL_VOLUME_3, 0, 40, 255, 1, digital_tlv),
  110244. SOC_DOUBLE_TLV("Playback Volume", PCM512x_ANALOG_GAIN_CTRL,
  110245. PCM512x_LAGN_SHIFT, PCM512x_RAGN_SHIFT, 1, 1, analog_tlv),
  110246. SOC_DOUBLE_TLV("Playback Boost Volume", PCM512x_ANALOG_GAIN_BOOST,
  110247. diff -Nur linux-3.16.2/sound/soc/codecs/wm8804.c linux-3.16-rpi/sound/soc/codecs/wm8804.c
  110248. --- linux-3.16.2/sound/soc/codecs/wm8804.c 2014-09-06 01:37:11.000000000 +0200
  110249. +++ linux-3.16-rpi/sound/soc/codecs/wm8804.c 2014-09-14 19:04:35.000000000 +0200
  110250. @@ -278,6 +278,7 @@
  110251. blen = 0x1;
  110252. break;
  110253. case SNDRV_PCM_FORMAT_S24_LE:
  110254. + case SNDRV_PCM_FORMAT_S32_LE:
  110255. blen = 0x2;
  110256. break;
  110257. default:
  110258. @@ -641,7 +642,7 @@
  110259. };
  110260. #define WM8804_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
  110261. - SNDRV_PCM_FMTBIT_S24_LE)
  110262. + SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
  110263. #define WM8804_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  110264. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_64000 | \
  110265. @@ -674,7 +675,7 @@
  110266. .suspend = wm8804_suspend,
  110267. .resume = wm8804_resume,
  110268. .set_bias_level = wm8804_set_bias_level,
  110269. - .idle_bias_off = true,
  110270. + .idle_bias_off = false,
  110271. .controls = wm8804_snd_controls,
  110272. .num_controls = ARRAY_SIZE(wm8804_snd_controls),
  110273. diff -Nur linux-3.16.2/sound/soc/soc-core.c linux-3.16-rpi/sound/soc/soc-core.c
  110274. --- linux-3.16.2/sound/soc/soc-core.c 2014-09-06 01:37:11.000000000 +0200
  110275. +++ linux-3.16-rpi/sound/soc/soc-core.c 2014-09-14 19:04:42.000000000 +0200
  110276. @@ -2973,8 +2973,8 @@
  110277. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  110278. uinfo->count = snd_soc_volsw_is_stereo(mc) ? 2 : 1;
  110279. - uinfo->value.integer.min = 0;
  110280. - uinfo->value.integer.max = platform_max - min;
  110281. + uinfo->value.integer.min = min;
  110282. + uinfo->value.integer.max = platform_max;
  110283. return 0;
  110284. }
  110285. @@ -3005,9 +3005,10 @@
  110286. unsigned int val, val_mask;
  110287. int ret;
  110288. - val = ((ucontrol->value.integer.value[0] + min) & mask);
  110289. if (invert)
  110290. - val = max - val;
  110291. + val = ((max - ucontrol->value.integer.value[0] + min) & mask);
  110292. + else
  110293. + val = (ucontrol->value.integer.value[0] & mask);
  110294. val_mask = mask << shift;
  110295. val = val << shift;
  110296. @@ -3016,9 +3017,10 @@
  110297. return ret;
  110298. if (snd_soc_volsw_is_stereo(mc)) {
  110299. - val = ((ucontrol->value.integer.value[1] + min) & mask);
  110300. if (invert)
  110301. - val = max - val;
  110302. + val = ((max - ucontrol->value.integer.value[1] + min) & mask);
  110303. + else
  110304. + val = (ucontrol->value.integer.value[1] & mask);
  110305. val_mask = mask << shift;
  110306. val = val << shift;
  110307. @@ -3062,9 +3064,7 @@
  110308. ucontrol->value.integer.value[0] = (val >> shift) & mask;
  110309. if (invert)
  110310. ucontrol->value.integer.value[0] =
  110311. - max - ucontrol->value.integer.value[0];
  110312. - ucontrol->value.integer.value[0] =
  110313. - ucontrol->value.integer.value[0] - min;
  110314. + max - ucontrol->value.integer.value[0] + min;
  110315. if (snd_soc_volsw_is_stereo(mc)) {
  110316. ret = snd_soc_component_read(component, rreg, &val);
  110317. @@ -3074,9 +3074,7 @@
  110318. ucontrol->value.integer.value[1] = (val >> shift) & mask;
  110319. if (invert)
  110320. ucontrol->value.integer.value[1] =
  110321. - max - ucontrol->value.integer.value[1];
  110322. - ucontrol->value.integer.value[1] =
  110323. - ucontrol->value.integer.value[1] - min;
  110324. + max - ucontrol->value.integer.value[1] + min;
  110325. }
  110326. return 0;